blob: 237d1856402cb18ee7827cc893a9671bbde9b632 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
Flavio Ceolin67ca1762018-09-14 10:43:44 -070013#ifndef ZEPHYR_INCLUDE_KERNEL_H_
14#define ZEPHYR_INCLUDE_KERNEL_H_
Benjamin Walsh456c6da2016-09-02 18:55:39 -040015
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Ioannis Glaropoulos92b8a412018-06-20 17:30:48 +020017#include <kernel_includes.h>
Kumar Gala8777ff12018-07-25 20:24:34 -050018#include <errno.h>
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -070019#include <stdbool.h>
Stephanos Ioannidis33fbe002019-09-09 21:26:59 +090020#include <toolchain.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040021
22#ifdef __cplusplus
23extern "C" {
24#endif
25
Anas Nashifbbb157d2017-01-15 08:46:31 -050026/**
27 * @brief Kernel APIs
28 * @defgroup kernel_apis Kernel APIs
29 * @{
30 * @}
31 */
32
Anas Nashif61f4b242016-11-18 10:53:59 -050033#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040034#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
35#else
36#define K_DEBUG(fmt, ...)
37#endif
38
Benjamin Walsh2f280412017-01-14 19:23:46 -050039#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
40#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
41#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
42#elif defined(CONFIG_COOP_ENABLED)
43#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
44#define _NUM_PREEMPT_PRIO (0)
45#elif defined(CONFIG_PREEMPT_ENABLED)
46#define _NUM_COOP_PRIO (0)
47#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
48#else
49#error "invalid configuration"
50#endif
51
52#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040053#define K_PRIO_PREEMPT(x) (x)
54
Benjamin Walsh456c6da2016-09-02 18:55:39 -040055#define K_ANY NULL
56#define K_END NULL
57
Benjamin Walshedb35702017-01-14 18:47:22 -050058#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040059#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050060#elif defined(CONFIG_COOP_ENABLED)
61#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
62#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040063#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050064#else
65#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040066#endif
67
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050068#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040069#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
70#else
71#define K_LOWEST_THREAD_PRIO -1
72#endif
73
Benjamin Walshfab8d922016-11-08 15:36:36 -050074#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
75
Benjamin Walsh456c6da2016-09-02 18:55:39 -040076#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
77#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
78
Andy Ross225c74b2018-06-27 11:20:50 -070079#ifdef CONFIG_WAITQ_SCALABLE
Andy Ross1acd8c22018-05-03 14:51:49 -070080
81typedef struct {
82 struct _priq_rb waitq;
83} _wait_q_t;
84
Patrik Flykt4344e272019-03-08 14:19:05 -070085extern bool z_priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
Andy Ross1acd8c22018-05-03 14:51:49 -070086
Patrik Flykt4344e272019-03-08 14:19:05 -070087#define Z_WAIT_Q_INIT(wait_q) { { { .lessthan_fn = z_priq_rb_lessthan } } }
Andy Ross1acd8c22018-05-03 14:51:49 -070088
89#else
90
Andy Rossccf3bf72018-05-10 11:10:34 -070091typedef struct {
92 sys_dlist_t waitq;
93} _wait_q_t;
94
Patrik Flykt4344e272019-03-08 14:19:05 -070095#define Z_WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096
Andy Ross1acd8c22018-05-03 14:51:49 -070097#endif
98
Anas Nashif2f203c22016-12-18 06:57:45 -050099#ifdef CONFIG_OBJECT_TRACING
Flavio Ceolind1ed3362018-12-07 11:39:13 -0800100#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next;
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +0800101#define _OBJECT_TRACING_LINKED_FLAG u8_t __linked;
102#define _OBJECT_TRACING_INIT \
103 .__next = NULL, \
104 .__linked = 0,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400105#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500106#define _OBJECT_TRACING_INIT
Flavio Ceolind1ed3362018-12-07 11:39:13 -0800107#define _OBJECT_TRACING_NEXT_PTR(type)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +0800108#define _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400109#endif
110
Benjamin Walshacc68c12017-01-29 18:57:45 -0500111#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300112#define _POLL_EVENT_OBJ_INIT(obj) \
113 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
114#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500115#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300116#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500117#define _POLL_EVENT
118#endif
119
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500120struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400121struct k_mutex;
122struct k_sem;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400123struct k_msgq;
124struct k_mbox;
125struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200126struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400127struct k_fifo;
128struct k_lifo;
129struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400130struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400131struct k_mem_pool;
132struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500133struct k_poll_event;
134struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800135struct k_mem_domain;
136struct k_mem_partition;
Wentong Wu5611e922019-06-20 23:51:27 +0800137struct k_futex;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400138
Anas Nashife71293e2019-12-04 20:00:14 -0500139/**
140 * @brief Kernel Object Types
141 *
142 * This enumeration needs to be kept in sync with the lists of kernel objects
Andrew Boie5bd891d2017-09-27 12:59:28 -0700143 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
144 * function in kernel/userspace.c
145 */
Andrew Boie945af952017-08-22 13:15:23 -0700146enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700147 K_OBJ_ANY,
148
Leandro Pereirac2003672018-04-04 13:50:32 -0700149 /** @cond
150 * Doxygen should ignore this build-time generated include file
151 * when genrating API documentation. Enumeration values are
152 * generated during build by gen_kobject_list.py. It includes
153 * basic kernel objects (e.g. pipes and mutexes) and driver types.
154 */
155#include <kobj-types-enum.h>
156 /** @endcond
157 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700158
Andrew Boie945af952017-08-22 13:15:23 -0700159 K_OBJ_LAST
160};
Anas Nashif4bcb2942019-01-23 23:06:29 -0500161/**
162 * @defgroup usermode_apis User Mode APIs
163 * @ingroup kernel_apis
164 * @{
165 */
Andrew Boie945af952017-08-22 13:15:23 -0700166
167#ifdef CONFIG_USERSPACE
168/* Table generated by gperf, these objects are retrieved via
Patrik Flykt4344e272019-03-08 14:19:05 -0700169 * z_object_find() */
Andrew Boie945af952017-08-22 13:15:23 -0700170struct _k_object {
Andrew Boie22553a72019-11-19 18:27:42 -0800171 void *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700172 u8_t perms[CONFIG_MAX_THREAD_BYTES];
173 u8_t type;
174 u8_t flags;
Andrew Boiee48ed6a2019-11-13 12:52:00 -0800175 uintptr_t data;
Andrew Boiedf555242018-05-25 07:28:54 -0700176} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700177
Andrew Boie877f82e2017-10-17 11:20:22 -0700178struct _k_object_assignment {
179 struct k_thread *thread;
180 void * const *objects;
181};
182
183/**
184 * @brief Grant a static thread access to a list of kernel objects
185 *
186 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
187 * a set of kernel objects. These objects do not need to be in an initialized
188 * state. The permissions will be granted when the threads are initialized
189 * in the early boot sequence.
190 *
191 * All arguments beyond the first must be pointers to kernel objects.
192 *
193 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
194 */
195#define K_THREAD_ACCESS_GRANT(name_, ...) \
196 static void * const _CONCAT(_object_list_, name_)[] = \
197 { __VA_ARGS__, NULL }; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400198 static const Z_STRUCT_SECTION_ITERABLE(_k_object_assignment, \
199 _CONCAT(_object_access_, name_)) = \
Andrew Boie877f82e2017-10-17 11:20:22 -0700200 { (&_k_thread_obj_ ## name_), \
201 (_CONCAT(_object_list_, name_)) }
202
Anas Nashife71293e2019-12-04 20:00:14 -0500203/** Object initialized */
Andrew Boie945af952017-08-22 13:15:23 -0700204#define K_OBJ_FLAG_INITIALIZED BIT(0)
Anas Nashife71293e2019-12-04 20:00:14 -0500205/** Object is Public */
Andrew Boie04caa672017-10-13 13:57:07 -0700206#define K_OBJ_FLAG_PUBLIC BIT(1)
Anas Nashife71293e2019-12-04 20:00:14 -0500207/** Object allocated */
Andrew Boie97bf0012018-04-24 17:01:37 -0700208#define K_OBJ_FLAG_ALLOC BIT(2)
Anas Nashife71293e2019-12-04 20:00:14 -0500209/** Driver Object */
Andrew Boie78757072019-07-23 13:29:30 -0700210#define K_OBJ_FLAG_DRIVER BIT(3)
Andrew Boie945af952017-08-22 13:15:23 -0700211
212/**
213 * Lookup a kernel object and init its metadata if it exists
214 *
215 * Calling this on an object will make it usable from userspace.
216 * Intended to be called as the last statement in kernel object init
217 * functions.
218 *
Anas Nashif50e3acd2018-12-08 13:26:18 -0500219 * @param obj Address of the kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700220 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700221void z_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700222#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700223/* LCOV_EXCL_START */
Andrew Boie877f82e2017-10-17 11:20:22 -0700224#define K_THREAD_ACCESS_GRANT(thread, ...)
225
Anas Nashif954d5502018-02-25 08:37:28 -0600226/**
227 * @internal
228 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700229static inline void z_object_init(void *obj)
Andrew Boie743e4682017-10-04 12:25:50 -0700230{
231 ARG_UNUSED(obj);
232}
233
Anas Nashif954d5502018-02-25 08:37:28 -0600234/**
235 * @internal
236 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700237static inline void z_impl_k_object_access_grant(void *object,
Andrew Boie743e4682017-10-04 12:25:50 -0700238 struct k_thread *thread)
239{
240 ARG_UNUSED(object);
241 ARG_UNUSED(thread);
242}
243
Anas Nashif954d5502018-02-25 08:37:28 -0600244/**
245 * @internal
246 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700247static inline void k_object_access_revoke(void *object,
248 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700249{
250 ARG_UNUSED(object);
251 ARG_UNUSED(thread);
252}
253
Andrew Boiee9cfc542018-04-13 13:15:28 -0700254/**
255 * @internal
256 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700257static inline void z_impl_k_object_release(void *object)
Andrew Boiee9cfc542018-04-13 13:15:28 -0700258{
259 ARG_UNUSED(object);
260}
261
Andrew Boie41bab6e2017-10-14 14:42:23 -0700262static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700263{
264 ARG_UNUSED(object);
265}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700266/* LCOV_EXCL_STOP */
Andrew Boie743e4682017-10-04 12:25:50 -0700267#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700268
269/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600270 * Grant a thread access to a kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700271 *
272 * The thread will be granted access to the object if the caller is from
273 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700274 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700275 *
276 * @param object Address of kernel object
277 * @param thread Thread to grant access to the object
278 */
Andrew Boie743e4682017-10-04 12:25:50 -0700279__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700280
Andrew Boiea89bf012017-10-09 14:47:55 -0700281/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600282 * Revoke a thread's access to a kernel object
Andrew Boiea89bf012017-10-09 14:47:55 -0700283 *
284 * The thread will lose access to the object if the caller is from
285 * supervisor mode, or the caller is from user mode AND has permissions
286 * on both the object and the thread whose access is being revoked.
287 *
288 * @param object Address of kernel object
289 * @param thread Thread to remove access to the object
290 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700291void k_object_access_revoke(void *object, struct k_thread *thread);
292
Anas Nashife71293e2019-12-04 20:00:14 -0500293/**
294 * @brief Release an object
295 *
296 * Allows user threads to drop their own permission on an object
297 * Their permissions are automatically cleared when a thread terminates.
298 *
299 * @param object The object to be released
300 *
301 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700302__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700303
304/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600305 * Grant all present and future threads access to an object
Andrew Boie3b5ae802017-10-04 12:10:32 -0700306 *
307 * If the caller is from supervisor mode, or the caller is from user mode and
308 * have sufficient permissions on the object, then that object will have
309 * permissions granted to it for *all* current and future threads running in
310 * the system, effectively becoming a public kernel object.
311 *
312 * Use of this API should be avoided on systems that are running untrusted code
313 * as it is possible for such code to derive the addresses of kernel objects
314 * and perform unwanted operations on them.
315 *
Andrew Boie04caa672017-10-13 13:57:07 -0700316 * It is not possible to revoke permissions on public objects; once public,
317 * any thread may use it.
318 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700319 * @param object Address of kernel object
320 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700321void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700322
Andrew Boie31bdfc02017-11-08 16:38:03 -0800323/**
324 * Allocate a kernel object of a designated type
325 *
326 * This will instantiate at runtime a kernel object of the specified type,
327 * returning a pointer to it. The object will be returned in an uninitialized
328 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700329 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800330 *
331 * Currently, allocation of thread stacks is not supported.
332 *
333 * @param otype Requested kernel object type
334 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
335 * available
336 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700337__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800338
Andrew Boie97bf0012018-04-24 17:01:37 -0700339#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800340/**
341 * Free a kernel object previously allocated with k_object_alloc()
342 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700343 * This will return memory for a kernel object back to resource pool it was
344 * allocated from. Care must be exercised that the object will not be used
345 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800346 *
347 * @param obj Pointer to the kernel object memory address.
348 */
349void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700350#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700351/* LCOV_EXCL_START */
Patrik Flykt4344e272019-03-08 14:19:05 -0700352static inline void *z_impl_k_object_alloc(enum k_objects otype)
Andrew Boie97bf0012018-04-24 17:01:37 -0700353{
Kumar Gala85699f72018-05-17 09:26:03 -0500354 ARG_UNUSED(otype);
355
Andrew Boie97bf0012018-04-24 17:01:37 -0700356 return NULL;
357}
Anas Nashife71293e2019-12-04 20:00:14 -0500358/**
359 * @brief Free an object
360 *
361 * @param obj
362 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700363static inline void k_obj_free(void *obj)
364{
365 ARG_UNUSED(obj);
366}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700367/* LCOV_EXCL_STOP */
Andrew Boie31bdfc02017-11-08 16:38:03 -0800368#endif /* CONFIG_DYNAMIC_OBJECTS */
369
Anas Nashif4bcb2942019-01-23 23:06:29 -0500370/** @} */
371
Andrew Boiebca15da2017-10-15 14:17:48 -0700372/* Using typedef deliberately here, this is quite intended to be an opaque
Andrew Boie4e5c0932019-04-04 12:05:28 -0700373 * type.
Andrew Boiebca15da2017-10-15 14:17:48 -0700374 *
375 * The purpose of this data type is to clearly distinguish between the
376 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
377 * buffer which composes the stack data actually used by the underlying
Anas Nashiff2cb20c2019-06-18 14:45:40 -0400378 * thread; they cannot be used interchangeably as some arches precede the
Andrew Boiebca15da2017-10-15 14:17:48 -0700379 * stack buffer region with guard areas that trigger a MPU or MMU fault
380 * if written to.
381 *
382 * APIs that want to work with the buffer inside should continue to use
383 * char *.
384 *
385 * Stacks should always be created with K_THREAD_STACK_DEFINE().
386 */
387struct __packed _k_thread_stack_element {
388 char data;
389};
Daniel Leung7476a6e2019-11-25 13:58:40 -0800390
391/**
392 * @typedef k_thread_stack_t
393 * @brief Typedef of struct _k_thread_stack_element
394 *
395 * @see _k_thread_stack_element
396 */
Andrew Boiebca15da2017-10-15 14:17:48 -0700397
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700398/**
399 * @typedef k_thread_entry_t
400 * @brief Thread entry point function type.
401 *
402 * A thread's entry point function is invoked when the thread starts executing.
403 * Up to 3 argument values can be passed to the function.
404 *
405 * The thread terminates execution permanently if the entry point function
406 * returns. The thread is responsible for releasing any shared resources
407 * it may own (such as mutexes and dynamically allocated memory), prior to
408 * returning.
409 *
410 * @param p1 First argument.
411 * @param p2 Second argument.
412 * @param p3 Third argument.
413 *
414 * @return N/A
415 */
Andrew Boie73abd322017-04-04 13:19:13 -0700416
417#ifdef CONFIG_THREAD_MONITOR
418struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700419 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700420 void *parameter1;
421 void *parameter2;
422 void *parameter3;
423};
424#endif
425
426/* can be used for creating 'dummy' threads, e.g. for pending on objects */
427struct _thread_base {
428
429 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700430 union {
Peter A. Bigot82ad0d22019-01-03 23:49:28 -0600431 sys_dnode_t qnode_dlist;
Andy Ross1acd8c22018-05-03 14:51:49 -0700432 struct rbnode qnode_rb;
433 };
434
Andy Ross1acd8c22018-05-03 14:51:49 -0700435 /* wait queue on which the thread is pended (needed only for
436 * trees, not dumb lists)
437 */
438 _wait_q_t *pended_on;
Andrew Boie73abd322017-04-04 13:19:13 -0700439
440 /* user facing 'thread options'; values defined in include/kernel.h */
441 u8_t user_options;
442
443 /* thread state */
444 u8_t thread_state;
445
446 /*
447 * scheduler lock count and thread priority
448 *
449 * These two fields control the preemptibility of a thread.
450 *
451 * When the scheduler is locked, sched_locked is decremented, which
452 * means that the scheduler is locked for values from 0xff to 0x01. A
453 * thread is coop if its prio is negative, thus 0x80 to 0xff when
454 * looked at the value as unsigned.
455 *
456 * By putting them end-to-end, this means that a thread is
457 * non-preemptible if the bundled value is greater than or equal to
458 * 0x0080.
459 */
460 union {
461 struct {
462#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
463 u8_t sched_locked;
464 s8_t prio;
465#else /* LITTLE and PDP */
466 s8_t prio;
467 u8_t sched_locked;
468#endif
469 };
470 u16_t preempt;
471 };
472
Andy Ross4a2e50f2018-05-15 11:06:25 -0700473#ifdef CONFIG_SCHED_DEADLINE
474 int prio_deadline;
475#endif
476
Andy Ross1acd8c22018-05-03 14:51:49 -0700477 u32_t order_key;
478
Andy Ross2724fd12018-01-29 14:55:20 -0800479#ifdef CONFIG_SMP
480 /* True for the per-CPU idle threads */
481 u8_t is_idle;
482
Andy Ross2724fd12018-01-29 14:55:20 -0800483 /* CPU index on which thread was last run */
484 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700485
486 /* Recursive count of irq_lock() calls */
487 u8_t global_lock_count;
Andy Rossab46b1b2019-01-30 15:00:42 -0800488
489#endif
490
491#ifdef CONFIG_SCHED_CPU_MASK
492 /* "May run on" bits for each CPU */
493 u8_t cpu_mask;
Andy Ross2724fd12018-01-29 14:55:20 -0800494#endif
495
Andrew Boie73abd322017-04-04 13:19:13 -0700496 /* data returned by APIs */
497 void *swap_data;
498
499#ifdef CONFIG_SYS_CLOCK_EXISTS
500 /* this thread's entry in a timeout queue */
501 struct _timeout timeout;
502#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700503};
504
505typedef struct _thread_base _thread_base_t;
506
507#if defined(CONFIG_THREAD_STACK_INFO)
508/* Contains the stack information of a thread */
509struct _thread_stack_info {
Andrew Boie4e5c0932019-04-04 12:05:28 -0700510 /* Stack start - Represents the start address of the thread-writable
511 * stack area.
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700512 */
Nicolas Pitre58d839b2019-05-21 11:32:21 -0400513 uintptr_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700514
515 /* Stack Size - Thread writable stack buffer size. Represents
516 * the size of the actual area, starting from the start member,
517 * that should be writable by the thread
518 */
Andrew Boie528233e2019-12-11 14:54:15 -0800519 size_t size;
Andrew Boie73abd322017-04-04 13:19:13 -0700520};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700521
522typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700523#endif /* CONFIG_THREAD_STACK_INFO */
524
Chunlin Hane9c97022017-07-07 20:29:30 +0800525#if defined(CONFIG_USERSPACE)
526struct _mem_domain_info {
Anas Nashife71293e2019-12-04 20:00:14 -0500527 /** memory domain queue node */
Chunlin Hane9c97022017-07-07 20:29:30 +0800528 sys_dnode_t mem_domain_q_node;
Anas Nashife71293e2019-12-04 20:00:14 -0500529 /** memory domain of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800530 struct k_mem_domain *mem_domain;
531};
532
533#endif /* CONFIG_USERSPACE */
534
Daniel Leungfc182432018-08-16 15:42:28 -0700535#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
536struct _thread_userspace_local_data {
537 int errno_var;
538};
539#endif
540
Anas Nashifce78d162018-05-24 12:43:11 -0500541/**
542 * @ingroup thread_apis
543 * Thread Structure
544 */
Andrew Boie73abd322017-04-04 13:19:13 -0700545struct k_thread {
546
547 struct _thread_base base;
548
Anas Nashifce78d162018-05-24 12:43:11 -0500549 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700550 struct _callee_saved callee_saved;
551
Anas Nashifce78d162018-05-24 12:43:11 -0500552 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700553 void *init_data;
554
Anas Nashifce78d162018-05-24 12:43:11 -0500555 /**
556 * abort function
557 * @req K-THREAD-002
558 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700559 void (*fn_abort)(void);
560
561#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500562 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700563 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700564
Anas Nashifce78d162018-05-24 12:43:11 -0500565 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700566 struct k_thread *next_thread;
567#endif
568
Anas Nashif57554052018-03-03 02:31:05 -0600569#if defined(CONFIG_THREAD_NAME)
Anas Nashife71293e2019-12-04 20:00:14 -0500570 /** Thread name */
Andrew Boie38129ce2019-06-25 08:54:37 -0700571 char name[CONFIG_THREAD_MAX_NAME_LEN];
Anas Nashif57554052018-03-03 02:31:05 -0600572#endif
573
Andrew Boie73abd322017-04-04 13:19:13 -0700574#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500575 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700576 void *custom_data;
577#endif
578
Daniel Leungfc182432018-08-16 15:42:28 -0700579#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
580 struct _thread_userspace_local_data *userspace_local_data;
581#endif
582
Andrew Boie73abd322017-04-04 13:19:13 -0700583#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700584#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500585 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700586 int errno_var;
587#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700588#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700589
590#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500591 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700592 struct _thread_stack_info stack_info;
593#endif /* CONFIG_THREAD_STACK_INFO */
594
Chunlin Hane9c97022017-07-07 20:29:30 +0800595#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500596 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800597 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500598 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700599 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800600#endif /* CONFIG_USERSPACE */
601
Andy Ross042d8ec2017-12-09 08:37:20 -0800602#if defined(CONFIG_USE_SWITCH)
603 /* When using __switch() a few previously arch-specific items
604 * become part of the core OS
605 */
606
Patrik Flykt4344e272019-03-08 14:19:05 -0700607 /** z_swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800608 int swap_retval;
609
Andrew Boie4f77c2a2019-11-07 12:43:29 -0800610 /** Context handle returned via arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800611 void *switch_handle;
612#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500613 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700614 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800615
Anas Nashifce78d162018-05-24 12:43:11 -0500616 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700617 struct _thread_arch arch;
618};
619
620typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400621typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400622
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400623enum execution_context_types {
624 K_ISR = 0,
625 K_COOP_THREAD,
626 K_PREEMPT_THREAD,
627};
628
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400629/**
Anas Nashif4bcb2942019-01-23 23:06:29 -0500630 * @addtogroup thread_apis
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100631 * @{
632 */
Anas Nashife71293e2019-12-04 20:00:14 -0500633
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530634typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
635 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100636
637/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530638 * @brief Iterate over all the threads in the system.
639 *
640 * This routine iterates over all the threads in the system and
641 * calls the user_cb function for each thread.
642 *
643 * @param user_cb Pointer to the user callback function.
644 * @param user_data Pointer to user data.
645 *
646 * @note CONFIG_THREAD_MONITOR must be set for this function
647 * to be effective. Also this API uses irq_lock to protect the
648 * _kernel.threads list which means creation of new threads and
649 * terminations of existing threads are blocked until this
650 * API returns.
651 *
652 * @return N/A
653 */
654extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
655
Anas Nashif166f5192018-02-25 08:02:36 -0600656/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100657
658/**
Allan Stephensc98da842016-11-11 15:45:03 -0500659 * @defgroup thread_apis Thread APIs
660 * @ingroup kernel_apis
661 * @{
662 */
663
Benjamin Walshed240f22017-01-22 13:05:08 -0500664#endif /* !_ASMLANGUAGE */
665
666
667/*
668 * Thread user options. May be needed by assembly code. Common part uses low
669 * bits, arch-specific use high bits.
670 */
671
Anas Nashifa541e932018-05-24 11:19:16 -0500672/**
673 * @brief system thread that must not abort
674 * @req K-THREAD-000
675 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700676#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500677
678#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500679/**
680 * @brief thread uses floating point registers
681 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700682#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500683#endif
684
Anas Nashifa541e932018-05-24 11:19:16 -0500685/**
686 * @brief user mode thread
687 *
688 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700689 * has additional restrictions
690 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700691#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700692
Anas Nashifa541e932018-05-24 11:19:16 -0500693/**
694 * @brief Inherit Permissions
695 *
696 * @details
697 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700698 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
699 * is not enabled.
700 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700701#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700702
Benjamin Walshed240f22017-01-22 13:05:08 -0500703#ifdef CONFIG_X86
704/* x86 Bitmask definitions for threads user options */
705
706#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
707/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700708#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500709#endif
710#endif
711
712/* end - thread options */
713
714#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400715/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700716 * @brief Create a thread.
717 *
718 * This routine initializes a thread, then schedules it for execution.
719 *
720 * The new thread may be scheduled for immediate execution or a delayed start.
721 * If the newly spawned thread does not have a delayed start the kernel
722 * scheduler may preempt the current thread to allow the new thread to
723 * execute.
724 *
725 * Thread options are architecture-specific, and can include K_ESSENTIAL,
726 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
727 * them using "|" (the logical OR operator).
728 *
729 * Historically, users often would use the beginning of the stack memory region
730 * to store the struct k_thread data, although corruption will occur if the
731 * stack overflows this region and stack protection features may not detect this
732 * situation.
733 *
734 * @param new_thread Pointer to uninitialized struct k_thread
735 * @param stack Pointer to the stack space.
736 * @param stack_size Stack size in bytes.
737 * @param entry Thread entry function.
738 * @param p1 1st entry point parameter.
739 * @param p2 2nd entry point parameter.
740 * @param p3 3rd entry point parameter.
741 * @param prio Thread priority.
742 * @param options Thread options.
743 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
744 *
745 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400746 *
747 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700748 */
Andrew Boie662c3452017-10-02 10:51:18 -0700749__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700750 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700751 size_t stack_size,
752 k_thread_entry_t entry,
753 void *p1, void *p2, void *p3,
754 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700755
Andrew Boie3f091b52017-08-30 14:34:14 -0700756/**
757 * @brief Drop a thread's privileges permanently to user mode
758 *
759 * @param entry Function to start executing from
760 * @param p1 1st entry point parameter
761 * @param p2 2nd entry point parameter
762 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400763 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700764 */
765extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
766 void *p1, void *p2,
767 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700768
Andrew Boied26cf2d2017-03-30 13:07:02 -0700769/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530770 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700771 *
772 * This is a convenience function. For the provided thread, grant access to
773 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700774 *
775 * The thread object must be initialized (i.e. running). The objects don't
776 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530777 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700778 *
779 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530780 * @param ... list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400781 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700782 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530783#define k_thread_access_grant(thread, ...) \
784 FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700785
786/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700787 * @brief Assign a resource memory pool to a thread
788 *
789 * By default, threads have no resource pool assigned unless their parent
790 * thread has a resource pool, in which case it is inherited. Multiple
791 * threads may be assigned to the same memory pool.
792 *
793 * Changing a thread's resource pool will not migrate allocations from the
794 * previous pool.
795 *
796 * @param thread Target thread to assign a memory pool for resource requests,
797 * or NULL if the thread should no longer have a memory pool.
798 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400799 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700800 */
801static inline void k_thread_resource_pool_assign(struct k_thread *thread,
802 struct k_mem_pool *pool)
803{
804 thread->resource_pool = pool;
805}
806
807#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
808/**
809 * @brief Assign the system heap as a thread's resource pool
810 *
811 * Similar to k_thread_resource_pool_assign(), but the thread will use
812 * the kernel heap to draw memory.
813 *
814 * Use with caution, as a malicious thread could perform DoS attacks on the
815 * kernel heap.
816 *
817 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400818 *
819 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700820 */
821void k_thread_system_pool_assign(struct k_thread *thread);
822#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
823
824/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500825 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400826 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700827 * This routine puts the current thread to sleep for @a duration milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400828 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700829 * @param ms Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200831 * @return Zero if the requested time has elapsed or the number of milliseconds
832 * left to sleep, if thread was woken up by \ref k_wakeup call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400833 */
Charles E. Yousea5678312019-05-09 16:46:46 -0700834__syscall s32_t k_sleep(s32_t ms);
835
836/**
837 * @brief Put the current thread to sleep with microsecond resolution.
838 *
839 * This function is unlikely to work as expected without kernel tuning.
840 * In particular, because the lower bound on the duration of a sleep is
841 * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted
842 * to achieve the resolution desired. The implications of doing this must
843 * be understood before attempting to use k_usleep(). Use with caution.
844 *
845 * @param us Number of microseconds to sleep.
846 *
847 * @return Zero if the requested time has elapsed or the number of microseconds
848 * left to sleep, if thread was woken up by \ref k_wakeup call.
849 */
850__syscall s32_t k_usleep(s32_t us);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851
852/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500853 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400854 *
855 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500856 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400857 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400858 * @return N/A
859 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800860__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400861
862/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500863 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500865 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400866 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500867 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400868 *
869 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400870 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400871 */
Andrew Boie468190a2017-09-29 14:00:48 -0700872__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400873
874/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500875 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500877 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400878 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500879 * If @a thread is not currently sleeping, the routine has no effect.
880 *
881 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400882 *
883 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400884 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400885 */
Andrew Boie468190a2017-09-29 14:00:48 -0700886__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400887
888/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500889 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500891 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400892 *
893 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400894 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700895__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400896
897/**
Allan Stephensc98da842016-11-11 15:45:03 -0500898 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500900 * This routine permanently stops execution of @a thread. The thread is taken
901 * off all kernel queues it is part of (i.e. the ready queue, the timeout
902 * queue, or a kernel object wait queue). However, any kernel resources the
903 * thread might currently own (such as mutexes or memory blocks) are not
904 * released. It is the responsibility of the caller of this routine to ensure
905 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400906 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500907 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400908 *
909 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400910 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400911 */
Andrew Boie468190a2017-09-29 14:00:48 -0700912__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400913
Andrew Boie7d627c52017-08-30 11:01:56 -0700914
915/**
916 * @brief Start an inactive thread
917 *
918 * If a thread was created with K_FOREVER in the delay parameter, it will
919 * not be added to the scheduling queue until this function is called
920 * on it.
921 *
922 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400923 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700924 */
Andrew Boie468190a2017-09-29 14:00:48 -0700925__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700926
Allan Stephensc98da842016-11-11 15:45:03 -0500927/**
928 * @cond INTERNAL_HIDDEN
929 */
930
Benjamin Walshd211a522016-12-06 11:44:01 -0500931/* timeout has timed out and is not on _timeout_q anymore */
932#define _EXPIRED (-2)
933
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400934struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700935 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700936 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400937 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700938 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500939 void *init_p1;
940 void *init_p2;
941 void *init_p3;
942 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500943 u32_t init_options;
944 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500945 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600946 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400947};
948
Andrew Boied26cf2d2017-03-30 13:07:02 -0700949#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400950 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600951 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500952 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700953 .init_thread = (thread), \
954 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500955 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700956 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400957 .init_p1 = (void *)p1, \
958 .init_p2 = (void *)p2, \
959 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500960 .init_prio = (prio), \
961 .init_options = (options), \
962 .init_delay = (delay), \
963 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600964 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400965 }
966
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400967/**
Allan Stephensc98da842016-11-11 15:45:03 -0500968 * INTERNAL_HIDDEN @endcond
969 */
970
971/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500972 * @brief Statically define and initialize a thread.
973 *
974 * The thread may be scheduled for immediate execution or a delayed start.
975 *
976 * Thread options are architecture-specific, and can include K_ESSENTIAL,
977 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
978 * them using "|" (the logical OR operator).
979 *
980 * The ID of the thread can be accessed using:
981 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500982 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500983 *
984 * @param name Name of the thread.
985 * @param stack_size Stack size in bytes.
986 * @param entry Thread entry function.
987 * @param p1 1st entry point parameter.
988 * @param p2 2nd entry point parameter.
989 * @param p3 3rd entry point parameter.
990 * @param prio Thread priority.
991 * @param options Thread options.
992 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400993 *
Anas Nashif47420d02018-05-24 14:20:56 -0400994 * @req K-THREAD-010
995 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400996 * @internal It has been observed that the x86 compiler by default aligns
997 * these _static_thread_data structures to 32-byte boundaries, thereby
998 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400999 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -04001000 */
Allan Stephens6cfe1322016-10-26 10:16:51 -05001001#define K_THREAD_DEFINE(name, stack_size, \
1002 entry, p1, p2, p3, \
1003 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -07001004 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001005 struct k_thread _k_thread_obj_##name; \
1006 Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\
Andrew Boied26cf2d2017-03-30 13:07:02 -07001007 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
1008 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -05001009 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -06001010 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -07001011 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001012
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001013/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001014 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001016 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001018 * @param thread ID of thread whose priority is needed.
1019 *
1020 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -04001021 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001022 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001023__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001024
1025/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001026 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001028 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001029 *
1030 * Rescheduling can occur immediately depending on the priority @a thread is
1031 * set to:
1032 *
1033 * - If its priority is raised above the priority of the caller of this
1034 * function, and the caller is preemptible, @a thread will be scheduled in.
1035 *
1036 * - If the caller operates on itself, it lowers its priority below that of
1037 * other threads in the system, and the caller is preemptible, the thread of
1038 * highest priority will be scheduled in.
1039 *
1040 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1041 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1042 * highest priority.
1043 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001044 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001045 * @param prio New priority.
1046 *
1047 * @warning Changing the priority of a thread currently involved in mutex
1048 * priority inheritance may result in undefined behavior.
1049 *
1050 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001051 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001052 */
Andrew Boie468190a2017-09-29 14:00:48 -07001053__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001054
Andy Ross4a2e50f2018-05-15 11:06:25 -07001055
1056#ifdef CONFIG_SCHED_DEADLINE
1057/**
1058 * @brief Set deadline expiration time for scheduler
1059 *
1060 * This sets the "deadline" expiration as a time delta from the
1061 * current time, in the same units used by k_cycle_get_32(). The
1062 * scheduler (when deadline scheduling is enabled) will choose the
1063 * next expiring thread when selecting between threads at the same
1064 * static priority. Threads at different priorities will be scheduled
1065 * according to their static priority.
1066 *
1067 * @note Deadlines that are negative (i.e. in the past) are still seen
1068 * as higher priority than others, even if the thread has "finished"
1069 * its work. If you don't want it scheduled anymore, you have to
1070 * reset the deadline into the future, block/pend the thread, or
1071 * modify its priority with k_thread_priority_set().
1072 *
1073 * @note Despite the API naming, the scheduler makes no guarantees the
1074 * the thread WILL be scheduled within that deadline, nor does it take
1075 * extra metadata (like e.g. the "runtime" and "period" parameters in
1076 * Linux sched_setattr()) that allows the kernel to validate the
1077 * scheduling for achievability. Such features could be implemented
1078 * above this call, which is simply input to the priority selection
1079 * logic.
1080 *
Anas Nashif240c5162019-06-10 12:25:50 -04001081 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001082 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001083 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1084 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001085 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001086 *
Andy Ross4a2e50f2018-05-15 11:06:25 -07001087 * @param thread A thread on which to set the deadline
1088 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001089 *
1090 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001091 */
1092__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1093#endif
1094
Andy Rossab46b1b2019-01-30 15:00:42 -08001095#ifdef CONFIG_SCHED_CPU_MASK
1096/**
1097 * @brief Sets all CPU enable masks to zero
1098 *
1099 * After this returns, the thread will no longer be schedulable on any
1100 * CPUs. The thread must not be currently runnable.
1101 *
Anas Nashif240c5162019-06-10 12:25:50 -04001102 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001103 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001104 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1105 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001106 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001107 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001108 * @param thread Thread to operate upon
1109 * @return Zero on success, otherwise error code
1110 */
1111int k_thread_cpu_mask_clear(k_tid_t thread);
1112
1113/**
1114 * @brief Sets all CPU enable masks to one
1115 *
1116 * After this returns, the thread will be schedulable on any CPU. The
1117 * thread must not be currently runnable.
1118 *
Anas Nashif240c5162019-06-10 12:25:50 -04001119 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001120 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001121 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1122 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001123 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001124 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001125 * @param thread Thread to operate upon
1126 * @return Zero on success, otherwise error code
1127 */
1128int k_thread_cpu_mask_enable_all(k_tid_t thread);
1129
1130/**
1131 * @brief Enable thread to run on specified CPU
1132 *
1133 * The thread must not be currently runnable.
1134 *
Anas Nashif240c5162019-06-10 12:25:50 -04001135 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001136 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001137 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1138 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001139 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001140 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001141 * @param thread Thread to operate upon
1142 * @param cpu CPU index
1143 * @return Zero on success, otherwise error code
1144 */
1145int k_thread_cpu_mask_enable(k_tid_t thread, int cpu);
1146
1147/**
1148 * @brief Prevent thread to run on specified CPU
1149 *
1150 * The thread must not be currently runnable.
1151 *
Anas Nashif240c5162019-06-10 12:25:50 -04001152 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001153 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001154 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1155 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001156 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001157 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001158 * @param thread Thread to operate upon
1159 * @param cpu CPU index
1160 * @return Zero on success, otherwise error code
1161 */
1162int k_thread_cpu_mask_disable(k_tid_t thread, int cpu);
1163#endif
1164
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001165/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001166 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001167 *
Andy Ross50d09422019-11-19 11:20:07 -08001168 * This routine prevents the kernel scheduler from making @a thread
1169 * the current thread. All other internal operations on @a thread are
1170 * still performed; for example, kernel objects it is waiting on are
1171 * still handed to it. Note that any existing timeouts
1172 * (e.g. k_sleep(), or a timeout argument to k_sem_take() et. al.)
1173 * will be canceled. On resume, the thread will begin running
1174 * immediately and return from the blocked call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001175 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001176 * If @a thread is already suspended, the routine has no effect.
1177 *
1178 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001179 *
1180 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001181 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001182 */
Andrew Boie468190a2017-09-29 14:00:48 -07001183__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001184
1185/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001186 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001187 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001188 * This routine allows the kernel scheduler to make @a thread the current
1189 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001190 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001191 * If @a thread is not currently suspended, the routine has no effect.
1192 *
1193 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001194 *
1195 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001196 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001197 */
Andrew Boie468190a2017-09-29 14:00:48 -07001198__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001199
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001200/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001201 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001202 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001203 * This routine specifies how the scheduler will perform time slicing of
1204 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001205 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001206 * To enable time slicing, @a slice must be non-zero. The scheduler
1207 * ensures that no thread runs for more than the specified time limit
1208 * before other threads of that priority are given a chance to execute.
1209 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001210 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001211 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001212 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001213 * execute. Once the scheduler selects a thread for execution, there is no
1214 * minimum guaranteed time the thread will execute before threads of greater or
1215 * equal priority are scheduled.
1216 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001217 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001218 * for execution, this routine has no effect; the thread is immediately
1219 * rescheduled after the slice period expires.
1220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001221 * To disable timeslicing, set both @a slice and @a prio to zero.
1222 *
1223 * @param slice Maximum time slice length (in milliseconds).
1224 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001225 *
1226 * @return N/A
1227 */
Kumar Galacc334c72017-04-21 10:55:34 -05001228extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001229
Anas Nashif166f5192018-02-25 08:02:36 -06001230/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001231
1232/**
1233 * @addtogroup isr_apis
1234 * @{
1235 */
1236
1237/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001238 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001239 *
Allan Stephensc98da842016-11-11 15:45:03 -05001240 * This routine allows the caller to customize its actions, depending on
1241 * whether it is a thread or an ISR.
1242 *
1243 * @note Can be called by ISRs.
1244 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001245 * @return false if invoked by a thread.
1246 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001247 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001248extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001249
Benjamin Walsh445830d2016-11-10 15:54:27 -05001250/**
1251 * @brief Determine if code is running in a preemptible thread.
1252 *
Allan Stephensc98da842016-11-11 15:45:03 -05001253 * This routine allows the caller to customize its actions, depending on
1254 * whether it can be preempted by another thread. The routine returns a 'true'
1255 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001256 *
Allan Stephensc98da842016-11-11 15:45:03 -05001257 * - The code is running in a thread, not at ISR.
1258 * - The thread's priority is in the preemptible range.
1259 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001260 *
Allan Stephensc98da842016-11-11 15:45:03 -05001261 * @note Can be called by ISRs.
1262 *
1263 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001264 * @return Non-zero if invoked by a preemptible thread.
1265 */
Andrew Boie468190a2017-09-29 14:00:48 -07001266__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001267
Allan Stephensc98da842016-11-11 15:45:03 -05001268/**
Anas Nashif166f5192018-02-25 08:02:36 -06001269 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001270 */
1271
1272/**
1273 * @addtogroup thread_apis
1274 * @{
1275 */
1276
1277/**
1278 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001279 *
Allan Stephensc98da842016-11-11 15:45:03 -05001280 * This routine prevents the current thread from being preempted by another
1281 * thread by instructing the scheduler to treat it as a cooperative thread.
1282 * If the thread subsequently performs an operation that makes it unready,
1283 * it will be context switched out in the normal manner. When the thread
1284 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001285 *
Allan Stephensc98da842016-11-11 15:45:03 -05001286 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001287 *
Allan Stephensc98da842016-11-11 15:45:03 -05001288 * @note k_sched_lock() and k_sched_unlock() should normally be used
1289 * when the operation being performed can be safely interrupted by ISRs.
1290 * However, if the amount of processing involved is very small, better
1291 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001292 *
1293 * @return N/A
1294 */
1295extern void k_sched_lock(void);
1296
Allan Stephensc98da842016-11-11 15:45:03 -05001297/**
1298 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001299 *
Allan Stephensc98da842016-11-11 15:45:03 -05001300 * This routine reverses the effect of a previous call to k_sched_lock().
1301 * A thread must call the routine once for each time it called k_sched_lock()
1302 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001303 *
1304 * @return N/A
1305 */
1306extern void k_sched_unlock(void);
1307
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001308/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001309 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001310 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001311 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001312 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001313 * Custom data is not used by the kernel itself, and is freely available
1314 * for a thread to use as it sees fit. It can be used as a framework
1315 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001316 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001317 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001318 *
1319 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001320 *
1321 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001322 */
Andrew Boie468190a2017-09-29 14:00:48 -07001323__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001324
1325/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001326 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001327 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001328 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001329 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001330 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001331 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001332 */
Andrew Boie468190a2017-09-29 14:00:48 -07001333__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001334
1335/**
Anas Nashif57554052018-03-03 02:31:05 -06001336 * @brief Set current thread name
1337 *
1338 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1339 * tracing and debugging.
1340 *
Andrew Boie38129ce2019-06-25 08:54:37 -07001341 * @param thread_id Thread to set name, or NULL to set the current thread
1342 * @param value Name string
1343 * @retval 0 on success
1344 * @retval -EFAULT Memory access error with supplied string
1345 * @retval -ENOSYS Thread name configuration option not enabled
1346 * @retval -EINVAL Thread name too long
Anas Nashif57554052018-03-03 02:31:05 -06001347 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001348__syscall int k_thread_name_set(k_tid_t thread_id, const char *value);
Anas Nashif57554052018-03-03 02:31:05 -06001349
1350/**
1351 * @brief Get thread name
1352 *
1353 * Get the name of a thread
1354 *
1355 * @param thread_id Thread ID
Andrew Boie38129ce2019-06-25 08:54:37 -07001356 * @retval Thread name, or NULL if configuration not enabled
Anas Nashif57554052018-03-03 02:31:05 -06001357 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001358const char *k_thread_name_get(k_tid_t thread_id);
1359
1360/**
1361 * @brief Copy the thread name into a supplied buffer
1362 *
1363 * @param thread_id Thread to obtain name information
1364 * @param buf Destination buffer
David B. Kinder73896c02019-10-28 16:27:57 -07001365 * @param size Destination buffer size
Andrew Boie38129ce2019-06-25 08:54:37 -07001366 * @retval -ENOSPC Destination buffer too small
1367 * @retval -EFAULT Memory access error
1368 * @retval -ENOSYS Thread name feature not enabled
1369 * @retval 0 Success
1370 */
1371__syscall int k_thread_name_copy(k_tid_t thread_id, char *buf,
1372 size_t size);
Anas Nashif57554052018-03-03 02:31:05 -06001373
1374/**
Pavlo Hamov8076c802019-07-31 12:43:54 +03001375 * @brief Get thread state string
1376 *
1377 * Get the human friendly thread state string
1378 *
1379 * @param thread_id Thread ID
1380 * @retval Thread state string, empty if no state flag is set
1381 */
1382const char *k_thread_state_str(k_tid_t thread_id);
1383
1384/**
Andy Rosscfe62032018-09-29 07:34:55 -07001385 * @}
1386 */
1387
1388/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001389 * @addtogroup clock_apis
1390 * @{
1391 */
1392
1393/**
1394 * @brief Generate null timeout delay.
1395 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001396 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001397 * not to wait if the requested operation cannot be performed immediately.
1398 *
1399 * @return Timeout delay value.
1400 */
1401#define K_NO_WAIT 0
1402
1403/**
1404 * @brief Generate timeout delay from milliseconds.
1405 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001406 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001407 * to wait up to @a ms milliseconds to perform the requested operation.
1408 *
1409 * @param ms Duration in milliseconds.
1410 *
1411 * @return Timeout delay value.
1412 */
Johan Hedberg14471692016-11-13 10:52:15 +02001413#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001414
1415/**
1416 * @brief Generate timeout delay from seconds.
1417 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001418 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001419 * to wait up to @a s seconds to perform the requested operation.
1420 *
1421 * @param s Duration in seconds.
1422 *
1423 * @return Timeout delay value.
1424 */
Johan Hedberg14471692016-11-13 10:52:15 +02001425#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001426
1427/**
1428 * @brief Generate timeout delay from minutes.
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001429
1430 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001431 * to wait up to @a m minutes to perform the requested operation.
1432 *
1433 * @param m Duration in minutes.
1434 *
1435 * @return Timeout delay value.
1436 */
Johan Hedberg14471692016-11-13 10:52:15 +02001437#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001438
1439/**
1440 * @brief Generate timeout delay from hours.
1441 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001442 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001443 * to wait up to @a h hours to perform the requested operation.
1444 *
1445 * @param h Duration in hours.
1446 *
1447 * @return Timeout delay value.
1448 */
Johan Hedberg14471692016-11-13 10:52:15 +02001449#define K_HOURS(h) K_MINUTES((h) * 60)
1450
Allan Stephensc98da842016-11-11 15:45:03 -05001451/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001452 * @brief Generate infinite timeout delay.
1453 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001454 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001455 * to wait as long as necessary to perform the requested operation.
1456 *
1457 * @return Timeout delay value.
1458 */
1459#define K_FOREVER (-1)
1460
1461/**
Anas Nashif166f5192018-02-25 08:02:36 -06001462 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001463 */
1464
1465/**
Allan Stephensc98da842016-11-11 15:45:03 -05001466 * @cond INTERNAL_HIDDEN
1467 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001468
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001469struct k_timer {
1470 /*
1471 * _timeout structure must be first here if we want to use
1472 * dynamic timer allocation. timeout.node is used in the double-linked
1473 * list of free timers
1474 */
1475 struct _timeout timeout;
1476
Allan Stephens45bfa372016-10-12 12:39:42 -05001477 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001478 _wait_q_t wait_q;
1479
1480 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001481 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001482
1483 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001484 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001485
1486 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001487 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001488
Allan Stephens45bfa372016-10-12 12:39:42 -05001489 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001490 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001491
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001492 /* user-specific data, also used to support legacy features */
1493 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001494
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001495 _OBJECT_TRACING_NEXT_PTR(k_timer)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08001496 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001497};
1498
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001499#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001500 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001501 .timeout = { \
1502 .node = {},\
1503 .dticks = 0, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001504 .fn = z_timer_expiration_handler \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001505 }, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001506 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001507 .expiry_fn = expiry, \
1508 .stop_fn = stop, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001509 .period = 0, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001510 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001511 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001512 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001513 }
1514
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05001515#define K_TIMER_INITIALIZER __DEPRECATED_MACRO Z_TIMER_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001516
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001517/**
Allan Stephensc98da842016-11-11 15:45:03 -05001518 * INTERNAL_HIDDEN @endcond
1519 */
1520
1521/**
1522 * @defgroup timer_apis Timer APIs
1523 * @ingroup kernel_apis
1524 * @{
1525 */
1526
1527/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001528 * @typedef k_timer_expiry_t
1529 * @brief Timer expiry function type.
1530 *
1531 * A timer's expiry function is executed by the system clock interrupt handler
1532 * each time the timer expires. The expiry function is optional, and is only
1533 * invoked if the timer has been initialized with one.
1534 *
1535 * @param timer Address of timer.
1536 *
1537 * @return N/A
1538 */
1539typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1540
1541/**
1542 * @typedef k_timer_stop_t
1543 * @brief Timer stop function type.
1544 *
1545 * A timer's stop function is executed if the timer is stopped prematurely.
1546 * The function runs in the context of the thread that stops the timer.
1547 * The stop function is optional, and is only invoked if the timer has been
1548 * initialized with one.
1549 *
1550 * @param timer Address of timer.
1551 *
1552 * @return N/A
1553 */
1554typedef void (*k_timer_stop_t)(struct k_timer *timer);
1555
1556/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001557 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001558 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001559 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001560 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001561 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001562 *
1563 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001564 * @param expiry_fn Function to invoke each time the timer expires.
1565 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001566 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001567#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001568 Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001569 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001570
Allan Stephens45bfa372016-10-12 12:39:42 -05001571/**
1572 * @brief Initialize a timer.
1573 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001574 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001575 *
1576 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001577 * @param expiry_fn Function to invoke each time the timer expires.
1578 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001579 *
1580 * @return N/A
1581 */
1582extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001583 k_timer_expiry_t expiry_fn,
1584 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001585
Allan Stephens45bfa372016-10-12 12:39:42 -05001586/**
1587 * @brief Start a timer.
1588 *
1589 * This routine starts a timer, and resets its status to zero. The timer
1590 * begins counting down using the specified duration and period values.
1591 *
1592 * Attempting to start a timer that is already running is permitted.
1593 * The timer's status is reset to zero and the timer begins counting down
1594 * using the new duration and period values.
1595 *
1596 * @param timer Address of timer.
1597 * @param duration Initial timer duration (in milliseconds).
1598 * @param period Timer period (in milliseconds).
1599 *
1600 * @return N/A
1601 */
Andrew Boiea354d492017-09-29 16:22:28 -07001602__syscall void k_timer_start(struct k_timer *timer,
1603 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001604
1605/**
1606 * @brief Stop a timer.
1607 *
1608 * This routine stops a running timer prematurely. The timer's stop function,
1609 * if one exists, is invoked by the caller.
1610 *
1611 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001612 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001613 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001614 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1615 * if @a k_timer_stop is to be called from ISRs.
1616 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001617 * @param timer Address of timer.
1618 *
1619 * @return N/A
1620 */
Andrew Boiea354d492017-09-29 16:22:28 -07001621__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001622
1623/**
1624 * @brief Read timer status.
1625 *
1626 * This routine reads the timer's status, which indicates the number of times
1627 * it has expired since its status was last read.
1628 *
1629 * Calling this routine resets the timer's status to zero.
1630 *
1631 * @param timer Address of timer.
1632 *
1633 * @return Timer status.
1634 */
Andrew Boiea354d492017-09-29 16:22:28 -07001635__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001636
1637/**
1638 * @brief Synchronize thread to timer expiration.
1639 *
1640 * This routine blocks the calling thread until the timer's status is non-zero
1641 * (indicating that it has expired at least once since it was last examined)
1642 * or the timer is stopped. If the timer status is already non-zero,
1643 * or the timer is already stopped, the caller continues without waiting.
1644 *
1645 * Calling this routine resets the timer's status to zero.
1646 *
1647 * This routine must not be used by interrupt handlers, since they are not
1648 * allowed to block.
1649 *
1650 * @param timer Address of timer.
1651 *
1652 * @return Timer status.
1653 */
Andrew Boiea354d492017-09-29 16:22:28 -07001654__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001655
Andy Ross52e444b2018-09-28 09:06:37 -07001656extern s32_t z_timeout_remaining(struct _timeout *timeout);
1657
Allan Stephens45bfa372016-10-12 12:39:42 -05001658/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001659 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001660 *
1661 * This routine computes the (approximate) time remaining before a running
1662 * timer next expires. If the timer is not running, it returns zero.
1663 *
1664 * @param timer Address of timer.
1665 *
1666 * @return Remaining time (in milliseconds).
1667 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001668__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001669
Patrik Flykt4344e272019-03-08 14:19:05 -07001670static inline u32_t z_impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001671{
Charles E. Youse0ad40222019-03-01 10:51:04 -08001672 const s32_t ticks = z_timeout_remaining(&timer->timeout);
Andy Ross88924062019-10-03 11:43:10 -07001673 return (ticks > 0) ? (u32_t)k_ticks_to_ms_floor64(ticks) : 0U;
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001674}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001675
Allan Stephensc98da842016-11-11 15:45:03 -05001676/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001677 * @brief Associate user-specific data with a timer.
1678 *
1679 * This routine records the @a user_data with the @a timer, to be retrieved
1680 * later.
1681 *
1682 * It can be used e.g. in a timer handler shared across multiple subsystems to
1683 * retrieve data specific to the subsystem this timer is associated with.
1684 *
1685 * @param timer Address of timer.
1686 * @param user_data User data to associate with the timer.
1687 *
1688 * @return N/A
1689 */
Andrew Boiea354d492017-09-29 16:22:28 -07001690__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1691
Anas Nashif954d5502018-02-25 08:37:28 -06001692/**
1693 * @internal
1694 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001695static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
Andrew Boiea354d492017-09-29 16:22:28 -07001696 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001697{
1698 timer->user_data = user_data;
1699}
1700
1701/**
1702 * @brief Retrieve the user-specific data from a timer.
1703 *
1704 * @param timer Address of timer.
1705 *
1706 * @return The user data.
1707 */
Andrew Boiea354d492017-09-29 16:22:28 -07001708__syscall void *k_timer_user_data_get(struct k_timer *timer);
1709
Patrik Flykt4344e272019-03-08 14:19:05 -07001710static inline void *z_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001711{
1712 return timer->user_data;
1713}
1714
Anas Nashif166f5192018-02-25 08:02:36 -06001715/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001716
Allan Stephensc98da842016-11-11 15:45:03 -05001717/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001718 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001719 * @{
1720 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001721
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001722/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001723 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001724 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001725 * This routine returns the elapsed time since the system booted,
1726 * in milliseconds.
1727 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001728 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001729 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001730 * While this function returns time in milliseconds, it does
1731 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001732 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001733 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001734 *
1735 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001736 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001737__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001738
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001739/**
1740 * @brief Enable clock always on in tickless kernel
1741 *
Andy Ross1db9f182019-06-25 10:09:45 -07001742 * Deprecated. This does nothing (it was always just a hint). This
1743 * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE
1744 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001745 *
1746 * @retval prev_status Previous status of always on flag
1747 */
Andy Ross1db9f182019-06-25 10:09:45 -07001748/* LCOV_EXCL_START */
1749__deprecated static inline int k_enable_sys_clock_always_on(void)
1750{
1751 __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1752 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1753
1754 return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE);
1755}
1756/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001757
1758/**
1759 * @brief Disable clock always on in tickless kernel
1760 *
Andy Ross1db9f182019-06-25 10:09:45 -07001761 * Deprecated. This does nothing (it was always just a hint). This
1762 * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE
1763 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001764 */
Andy Ross1db9f182019-06-25 10:09:45 -07001765/* LCOV_EXCL_START */
1766__deprecated static inline void k_disable_sys_clock_always_on(void)
1767{
1768 __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1769 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1770}
1771/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001772
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001773/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001774 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001775 *
Peter Bigota6067a32019-08-28 08:19:26 -05001776 * This routine returns the lower 32 bits of the system uptime in
1777 * milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001778 *
Peter Bigota6067a32019-08-28 08:19:26 -05001779 * Because correct conversion requires full precision of the system
1780 * clock there is no benefit to using this over k_uptime_get() unless
1781 * you know the application will never run long enough for the system
1782 * clock to approach 2^32 ticks. Calls to this function may involve
1783 * interrupt blocking and 64-bit math.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001784 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001785 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001786 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001787 * While this function returns time in milliseconds, it does
1788 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001789 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option
David B. Kinder8de9cc72019-06-25 10:44:55 -07001790 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001791 *
Peter Bigota6067a32019-08-28 08:19:26 -05001792 * @return The low 32 bits of the current uptime, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001793 */
Peter Bigota6067a32019-08-28 08:19:26 -05001794static inline u32_t k_uptime_get_32(void)
1795{
1796 return (u32_t)k_uptime_get();
1797}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001798
1799/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001800 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001801 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001802 * This routine computes the elapsed time between the current system uptime
1803 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001804 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001805 * @param reftime Pointer to a reference time, which is updated to the current
1806 * uptime upon return.
1807 *
1808 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001809 */
Andy Ross987c0e52018-09-27 16:50:00 -07001810static inline s64_t k_uptime_delta(s64_t *reftime)
1811{
1812 s64_t uptime, delta;
1813
1814 uptime = k_uptime_get();
1815 delta = uptime - *reftime;
1816 *reftime = uptime;
1817
1818 return delta;
1819}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001820
1821/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001822 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001824 * This routine computes the elapsed time between the current system uptime
1825 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001826 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001827 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1828 * need for interrupt locking and 64-bit math. However, the 32-bit result
1829 * cannot hold an elapsed time larger than approximately 50 days, so the
1830 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001831 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001832 * @param reftime Pointer to a reference time, which is updated to the current
1833 * uptime upon return.
1834 *
1835 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001836 */
Andy Ross987c0e52018-09-27 16:50:00 -07001837static inline u32_t k_uptime_delta_32(s64_t *reftime)
1838{
1839 return (u32_t)k_uptime_delta(reftime);
1840}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001841
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001842/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001843 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001845 * This routine returns the current time, as measured by the system's hardware
1846 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001848 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001849 */
Andrew Boie979b17f2019-10-03 15:20:41 -07001850static inline u32_t k_cycle_get_32(void)
1851{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08001852 return arch_k_cycle_get_32();
Andrew Boie979b17f2019-10-03 15:20:41 -07001853}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001854
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001855/**
Anas Nashif166f5192018-02-25 08:02:36 -06001856 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001857 */
1858
Allan Stephensc98da842016-11-11 15:45:03 -05001859/**
1860 * @cond INTERNAL_HIDDEN
1861 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001862
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001863struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001864 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001865 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001866 union {
1867 _wait_q_t wait_q;
1868
1869 _POLL_EVENT;
1870 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001871
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001872 _OBJECT_TRACING_NEXT_PTR(k_queue)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08001873 _OBJECT_TRACING_LINKED_FLAG
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001874};
1875
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001876#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001877 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001878 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Stephanos Ioannidisf628dcd2019-09-11 18:09:49 +09001879 .lock = { }, \
1880 { \
1881 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
1882 _POLL_EVENT_OBJ_INIT(obj) \
1883 }, \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001884 _OBJECT_TRACING_INIT \
1885 }
1886
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05001887#define K_QUEUE_INITIALIZER __DEPRECATED_MACRO _K_QUEUE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001888
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001889extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1890
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001891/**
1892 * INTERNAL_HIDDEN @endcond
1893 */
1894
1895/**
1896 * @defgroup queue_apis Queue APIs
1897 * @ingroup kernel_apis
1898 * @{
1899 */
1900
1901/**
1902 * @brief Initialize a queue.
1903 *
1904 * This routine initializes a queue object, prior to its first use.
1905 *
1906 * @param queue Address of the queue.
1907 *
1908 * @return N/A
1909 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001910__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001911
1912/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001913 * @brief Cancel waiting on a queue.
1914 *
1915 * This routine causes first thread pending on @a queue, if any, to
1916 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001917 * If the queue is being waited on by k_poll(), it will return with
1918 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1919 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001920 *
1921 * @note Can be called by ISRs.
1922 *
1923 * @param queue Address of the queue.
1924 *
1925 * @return N/A
1926 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001927__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001928
1929/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001930 * @brief Append an element to the end of a queue.
1931 *
1932 * This routine appends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001933 * aligned on a word boundary, and the first word of the item is reserved
1934 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001935 *
1936 * @note Can be called by ISRs.
1937 *
1938 * @param queue Address of the queue.
1939 * @param data Address of the data item.
1940 *
1941 * @return N/A
1942 */
1943extern void k_queue_append(struct k_queue *queue, void *data);
1944
1945/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001946 * @brief Append an element to a queue.
1947 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001948 * This routine appends a data item to @a queue. There is an implicit memory
1949 * allocation to create an additional temporary bookkeeping data structure from
1950 * the calling thread's resource pool, which is automatically freed when the
1951 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001952 *
1953 * @note Can be called by ISRs.
1954 *
1955 * @param queue Address of the queue.
1956 * @param data Address of the data item.
1957 *
1958 * @retval 0 on success
1959 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1960 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301961__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001962
1963/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001964 * @brief Prepend an element to a queue.
1965 *
1966 * This routine prepends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001967 * aligned on a word boundary, and the first word of the item is reserved
1968 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001969 *
1970 * @note Can be called by ISRs.
1971 *
1972 * @param queue Address of the queue.
1973 * @param data Address of the data item.
1974 *
1975 * @return N/A
1976 */
1977extern void k_queue_prepend(struct k_queue *queue, void *data);
1978
1979/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001980 * @brief Prepend an element to a queue.
1981 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001982 * This routine prepends a data item to @a queue. There is an implicit memory
1983 * allocation to create an additional temporary bookkeeping data structure from
1984 * the calling thread's resource pool, which is automatically freed when the
1985 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001986 *
1987 * @note Can be called by ISRs.
1988 *
1989 * @param queue Address of the queue.
1990 * @param data Address of the data item.
1991 *
1992 * @retval 0 on success
1993 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1994 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301995__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001996
1997/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001998 * @brief Inserts an element to a queue.
1999 *
2000 * This routine inserts a data item to @a queue after previous item. A queue
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002001 * data item must be aligned on a word boundary, and the first word of
2002 * the item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002003 *
2004 * @note Can be called by ISRs.
2005 *
2006 * @param queue Address of the queue.
2007 * @param prev Address of the previous data item.
2008 * @param data Address of the data item.
2009 *
2010 * @return N/A
2011 */
2012extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
2013
2014/**
2015 * @brief Atomically append a list of elements to a queue.
2016 *
2017 * This routine adds a list of data items to @a queue in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002018 * The data items must be in a singly-linked list, with the first word
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002019 * in each data item pointing to the next data item; the list must be
2020 * NULL-terminated.
2021 *
2022 * @note Can be called by ISRs.
2023 *
2024 * @param queue Address of the queue.
2025 * @param head Pointer to first node in singly-linked list.
2026 * @param tail Pointer to last node in singly-linked list.
2027 *
2028 * @return N/A
2029 */
2030extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
2031
2032/**
2033 * @brief Atomically add a list of elements to a queue.
2034 *
2035 * This routine adds a list of data items to @a queue in one operation.
2036 * The data items must be in a singly-linked list implemented using a
2037 * sys_slist_t object. Upon completion, the original list is empty.
2038 *
2039 * @note Can be called by ISRs.
2040 *
2041 * @param queue Address of the queue.
2042 * @param list Pointer to sys_slist_t object.
2043 *
2044 * @return N/A
2045 */
2046extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
2047
2048/**
2049 * @brief Get an element from a queue.
2050 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002051 * This routine removes first data item from @a queue. The first word of the
2052 * data item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002053 *
2054 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2055 *
2056 * @param queue Address of the queue.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002057 * @param timeout Non-negative waiting period to obtain a data item (in
2058 * milliseconds), or one of the special values K_NO_WAIT and
2059 * K_FOREVER.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002060 *
2061 * @return Address of the data item if successful; NULL if returned
2062 * without waiting, or waiting period timed out.
2063 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002064__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002065
2066/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002067 * @brief Remove an element from a queue.
2068 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002069 * This routine removes data item from @a queue. The first word of the
2070 * data item is reserved for the kernel's use. Removing elements from k_queue
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002071 * rely on sys_slist_find_and_remove which is not a constant time operation.
2072 *
2073 * @note Can be called by ISRs
2074 *
2075 * @param queue Address of the queue.
2076 * @param data Address of the data item.
2077 *
2078 * @return true if data item was removed
2079 */
2080static inline bool k_queue_remove(struct k_queue *queue, void *data)
2081{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002082 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002083}
2084
2085/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002086 * @brief Append an element to a queue only if it's not present already.
2087 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002088 * This routine appends data item to @a queue. The first word of the data
2089 * item is reserved for the kernel's use. Appending elements to k_queue
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002090 * relies on sys_slist_is_node_in_list which is not a constant time operation.
2091 *
2092 * @note Can be called by ISRs
2093 *
2094 * @param queue Address of the queue.
2095 * @param data Address of the data item.
2096 *
2097 * @return true if data item was added, false if not
2098 */
2099static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
2100{
2101 sys_sfnode_t *test;
2102
2103 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
2104 if (test == (sys_sfnode_t *) data) {
2105 return false;
2106 }
2107 }
2108
2109 k_queue_append(queue, data);
2110 return true;
2111}
2112
2113/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002114 * @brief Query a queue to see if it has data available.
2115 *
2116 * Note that the data might be already gone by the time this function returns
2117 * if other threads are also trying to read from the queue.
2118 *
2119 * @note Can be called by ISRs.
2120 *
2121 * @param queue Address of the queue.
2122 *
2123 * @return Non-zero if the queue is empty.
2124 * @return 0 if data is available.
2125 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002126__syscall int k_queue_is_empty(struct k_queue *queue);
2127
Patrik Flykt4344e272019-03-08 14:19:05 -07002128static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002129{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002130 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002131}
2132
2133/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002134 * @brief Peek element at the head of queue.
2135 *
2136 * Return element from the head of queue without removing it.
2137 *
2138 * @param queue Address of the queue.
2139 *
2140 * @return Head element, or NULL if queue is empty.
2141 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002142__syscall void *k_queue_peek_head(struct k_queue *queue);
2143
Patrik Flykt4344e272019-03-08 14:19:05 -07002144static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002145{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002146 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002147}
2148
2149/**
2150 * @brief Peek element at the tail of queue.
2151 *
2152 * Return element from the tail of queue without removing it.
2153 *
2154 * @param queue Address of the queue.
2155 *
2156 * @return Tail element, or NULL if queue is empty.
2157 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002158__syscall void *k_queue_peek_tail(struct k_queue *queue);
2159
Patrik Flykt4344e272019-03-08 14:19:05 -07002160static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002161{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002162 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002163}
2164
2165/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002166 * @brief Statically define and initialize a queue.
2167 *
2168 * The queue can be accessed outside the module where it is defined using:
2169 *
2170 * @code extern struct k_queue <name>; @endcode
2171 *
2172 * @param name Name of the queue.
2173 */
2174#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002175 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002176 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002177
Anas Nashif166f5192018-02-25 08:02:36 -06002178/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002179
Wentong Wu5611e922019-06-20 23:51:27 +08002180#ifdef CONFIG_USERSPACE
2181/**
2182 * @brief futex structure
2183 *
2184 * A k_futex is a lightweight mutual exclusion primitive designed
2185 * to minimize kernel involvement. Uncontended operation relies
2186 * only on atomic access to shared memory. k_futex are tracked as
2187 * kernel objects and can live in user memory so any access bypass
2188 * the kernel object permission management mechanism.
2189 */
2190struct k_futex {
2191 atomic_t val;
2192};
2193
2194/**
2195 * @brief futex kernel data structure
2196 *
2197 * z_futex_data are the helper data structure for k_futex to complete
2198 * futex contended operation on kernel side, structure z_futex_data
2199 * of every futex object is invisible in user mode.
2200 */
2201struct z_futex_data {
2202 _wait_q_t wait_q;
2203 struct k_spinlock lock;
2204};
2205
2206#define Z_FUTEX_DATA_INITIALIZER(obj) \
2207 { \
2208 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \
2209 }
2210
2211/**
2212 * @defgroup futex_apis FUTEX APIs
2213 * @ingroup kernel_apis
2214 * @{
2215 */
2216
2217/**
Wentong Wu5611e922019-06-20 23:51:27 +08002218 * @brief Pend the current thread on a futex
2219 *
2220 * Tests that the supplied futex contains the expected value, and if so,
2221 * goes to sleep until some other thread calls k_futex_wake() on it.
2222 *
2223 * @param futex Address of the futex.
2224 * @param expected Expected value of the futex, if it is different the caller
2225 * will not wait on it.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002226 * @param timeout Non-negative waiting period on the futex, in milliseconds, or
2227 * one of the special values K_NO_WAIT or K_FOREVER.
Wentong Wu5611e922019-06-20 23:51:27 +08002228 * @retval -EACCES Caller does not have read access to futex address.
2229 * @retval -EAGAIN If the futex value did not match the expected parameter.
2230 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2231 * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup.
2232 * @retval 0 if the caller went to sleep and was woken up. The caller
2233 * should check the futex's value on wakeup to determine if it needs
2234 * to block again.
2235 */
2236__syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout);
2237
2238/**
2239 * @brief Wake one/all threads pending on a futex
2240 *
2241 * Wake up the highest priority thread pending on the supplied futex, or
2242 * wakeup all the threads pending on the supplied futex, and the behavior
2243 * depends on wake_all.
2244 *
2245 * @param futex Futex to wake up pending threads.
2246 * @param wake_all If true, wake up all pending threads; If false,
2247 * wakeup the highest priority thread.
2248 * @retval -EACCES Caller does not have access to the futex address.
2249 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2250 * @retval Number of threads that were woken up.
2251 */
2252__syscall int k_futex_wake(struct k_futex *futex, bool wake_all);
2253
2254/** @} */
2255#endif
2256
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002257struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002258 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002259};
2260
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002261/**
2262 * @cond INTERNAL_HIDDEN
2263 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002264#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002265 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002266 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002267 }
2268
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002269#define K_FIFO_INITIALIZER __DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002270
Allan Stephensc98da842016-11-11 15:45:03 -05002271/**
2272 * INTERNAL_HIDDEN @endcond
2273 */
2274
2275/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002276 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002277 * @ingroup kernel_apis
2278 * @{
2279 */
2280
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002281/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002282 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002283 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002284 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002285 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002286 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002287 *
2288 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002289 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002290 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002291#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002292 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002293
2294/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002295 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002296 *
2297 * This routine causes first thread pending on @a fifo, if any, to
2298 * return from k_fifo_get() call with NULL value (as if timeout
2299 * expired).
2300 *
2301 * @note Can be called by ISRs.
2302 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002303 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002304 *
2305 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002306 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002307 */
2308#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002309 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002310
2311/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002312 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002313 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002314 * This routine adds a data item to @a fifo. A FIFO data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002315 * aligned on a word boundary, and the first word of the item is reserved
2316 * for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002318 * @note Can be called by ISRs.
2319 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002320 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002321 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002322 *
2323 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002324 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002325 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002326#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002327 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002328
2329/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002330 * @brief Add an element to a FIFO queue.
2331 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002332 * This routine adds a data item to @a fifo. There is an implicit memory
2333 * allocation to create an additional temporary bookkeeping data structure from
2334 * the calling thread's resource pool, which is automatically freed when the
2335 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002336 *
2337 * @note Can be called by ISRs.
2338 *
2339 * @param fifo Address of the FIFO.
2340 * @param data Address of the data item.
2341 *
2342 * @retval 0 on success
2343 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002344 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002345 */
2346#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002347 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002348
2349/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002350 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002351 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002352 * This routine adds a list of data items to @a fifo in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002353 * The data items must be in a singly-linked list, with the first word of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002354 * each data item pointing to the next data item; the list must be
2355 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002356 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002357 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002358 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002359 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002360 * @param head Pointer to first node in singly-linked list.
2361 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002362 *
2363 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002364 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002365 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002366#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002367 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002368
2369/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002370 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002371 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002372 * This routine adds a list of data items to @a fifo in one operation.
2373 * The data items must be in a singly-linked list implemented using a
2374 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002375 * and must be re-initialized via sys_slist_init().
2376 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002377 * @note Can be called by ISRs.
2378 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002379 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002380 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002381 *
2382 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002383 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002384 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002385#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002386 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002387
2388/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002389 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002391 * This routine removes a data item from @a fifo in a "first in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002392 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002393 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002394 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2395 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002396 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002397 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002398 * or one of the special values K_NO_WAIT and K_FOREVER.
2399 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002400 * @return Address of the data item if successful; NULL if returned
2401 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002402 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002403 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002404#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002405 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002406
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002407/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002408 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002409 *
2410 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002411 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002412 *
2413 * @note Can be called by ISRs.
2414 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002415 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002416 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002417 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002418 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002419 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002420 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002421#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002422 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002423
2424/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002425 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002426 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002427 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302428 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002429 * on each iteration of processing, a head container will be peeked,
2430 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002431 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002432 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002433 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002434 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002435 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002436 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002437 */
2438#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002439 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002440
2441/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002442 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002443 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002444 * Return element from the tail of FIFO queue (without removing it). A usecase
2445 * for this is if elements of the FIFO queue are themselves containers. Then
2446 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002447 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002448 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002449 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002450 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002451 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002452 */
2453#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002454 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002455
2456/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002457 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002458 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002459 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002460 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002461 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002462 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002463 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002464 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002465 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002466#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002467 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002468 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002469
Anas Nashif166f5192018-02-25 08:02:36 -06002470/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002471
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002472struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002473 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002474};
2475
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002476/**
2477 * @cond INTERNAL_HIDDEN
2478 */
2479
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002480#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002481 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002482 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002483 }
2484
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002485#define K_LIFO_INITIALIZER __DEPRECATED_MACRO _K_LIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002486
Allan Stephensc98da842016-11-11 15:45:03 -05002487/**
2488 * INTERNAL_HIDDEN @endcond
2489 */
2490
2491/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002492 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002493 * @ingroup kernel_apis
2494 * @{
2495 */
2496
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002497/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002498 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002499 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002500 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002501 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002502 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002503 *
2504 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002505 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002506 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002507#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002508 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002509
2510/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002511 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002512 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002513 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002514 * aligned on a word boundary, and the first word of the item is
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002515 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002516 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002517 * @note Can be called by ISRs.
2518 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002519 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002520 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002521 *
2522 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002523 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002524 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002525#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002526 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002527
2528/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002529 * @brief Add an element to a LIFO queue.
2530 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002531 * This routine adds a data item to @a lifo. There is an implicit memory
2532 * allocation to create an additional temporary bookkeeping data structure from
2533 * the calling thread's resource pool, which is automatically freed when the
2534 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002535 *
2536 * @note Can be called by ISRs.
2537 *
2538 * @param lifo Address of the LIFO.
2539 * @param data Address of the data item.
2540 *
2541 * @retval 0 on success
2542 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002543 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002544 */
2545#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002546 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002547
2548/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002549 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002551 * This routine removes a data item from @a lifo in a "last in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002552 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002554 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2555 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002556 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002557 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002558 * or one of the special values K_NO_WAIT and K_FOREVER.
2559 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002560 * @return Address of the data item if successful; NULL if returned
2561 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002562 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002563 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002564#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002565 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002566
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002567/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002568 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002569 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002570 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002571 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002572 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002573 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002574 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002575 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002576 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002577#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002578 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002579 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002580
Anas Nashif166f5192018-02-25 08:02:36 -06002581/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002582
2583/**
2584 * @cond INTERNAL_HIDDEN
2585 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302586#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002587
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002588typedef uintptr_t stack_data_t;
2589
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002590struct k_stack {
2591 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002592 struct k_spinlock lock;
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002593 stack_data_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002594
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002595 _OBJECT_TRACING_NEXT_PTR(k_stack)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08002596 _OBJECT_TRACING_LINKED_FLAG
Andrew Boief3bee952018-05-02 17:44:39 -07002597 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002598};
2599
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002600#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002601 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002602 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002603 .base = stack_buffer, \
2604 .next = stack_buffer, \
2605 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002606 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002607 }
2608
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002609#define K_STACK_INITIALIZER __DEPRECATED_MACRO _K_STACK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002610
Allan Stephensc98da842016-11-11 15:45:03 -05002611/**
2612 * INTERNAL_HIDDEN @endcond
2613 */
2614
2615/**
2616 * @defgroup stack_apis Stack APIs
2617 * @ingroup kernel_apis
2618 * @{
2619 */
2620
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002621/**
2622 * @brief Initialize a stack.
2623 *
2624 * This routine initializes a stack object, prior to its first use.
2625 *
2626 * @param stack Address of the stack.
2627 * @param buffer Address of array used to hold stacked values.
2628 * @param num_entries Maximum number of values that can be stacked.
2629 *
2630 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002631 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002632 */
Andrew Boief3bee952018-05-02 17:44:39 -07002633void k_stack_init(struct k_stack *stack,
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002634 stack_data_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002635
2636
2637/**
2638 * @brief Initialize a stack.
2639 *
2640 * This routine initializes a stack object, prior to its first use. Internal
2641 * buffers will be allocated from the calling thread's resource pool.
2642 * This memory will be released if k_stack_cleanup() is called, or
2643 * userspace is enabled and the stack object loses all references to it.
2644 *
2645 * @param stack Address of the stack.
2646 * @param num_entries Maximum number of values that can be stacked.
2647 *
2648 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002649 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002650 */
2651
Adithya Baglody28080d32018-10-15 11:48:51 +05302652__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2653 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002654
2655/**
2656 * @brief Release a stack's allocated buffer
2657 *
2658 * If a stack object was given a dynamically allocated buffer via
2659 * k_stack_alloc_init(), this will free it. This function does nothing
2660 * if the buffer wasn't dynamically allocated.
2661 *
2662 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002663 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002664 */
2665void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002666
2667/**
2668 * @brief Push an element onto a stack.
2669 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002670 * This routine adds a stack_data_t value @a data to @a stack.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002671 *
2672 * @note Can be called by ISRs.
2673 *
2674 * @param stack Address of the stack.
2675 * @param data Value to push onto the stack.
2676 *
2677 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002678 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002679 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002680__syscall void k_stack_push(struct k_stack *stack, stack_data_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002681
2682/**
2683 * @brief Pop an element from a stack.
2684 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002685 * This routine removes a stack_data_t value from @a stack in a "last in,
2686 * first out" manner and stores the value in @a data.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002687 *
2688 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2689 *
2690 * @param stack Address of the stack.
2691 * @param data Address of area to hold the value popped from the stack.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002692 * @param timeout Non-negative waiting period to obtain a value (in
2693 * milliseconds), or one of the special values K_NO_WAIT and
2694 * K_FOREVER.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002695 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002696 * @retval 0 Element popped from stack.
2697 * @retval -EBUSY Returned without waiting.
2698 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002699 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002700 */
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002701__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data,
2702 s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002703
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002704/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002705 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002706 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002707 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002708 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002709 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002710 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002711 * @param name Name of the stack.
2712 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002713 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002714 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002715#define K_STACK_DEFINE(name, stack_num_entries) \
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002716 stack_data_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002717 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002718 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002719 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002720 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002721
Anas Nashif166f5192018-02-25 08:02:36 -06002722/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002723
Allan Stephens6bba9b02016-11-16 14:56:54 -05002724struct k_work;
Piotr Zięcik19d83492019-09-27 09:16:25 +02002725struct k_work_poll;
Allan Stephens6bba9b02016-11-16 14:56:54 -05002726
Piotr Zięcik19d83492019-09-27 09:16:25 +02002727/* private, used by k_poll and k_work_poll */
Piotr Zięcik1c4177d2019-08-27 12:19:26 +02002728typedef int (*_poller_cb_t)(struct k_poll_event *event, u32_t state);
2729struct _poller {
2730 volatile bool is_polling;
2731 struct k_thread *thread;
2732 _poller_cb_t cb;
2733};
2734
Allan Stephensc98da842016-11-11 15:45:03 -05002735/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002736 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002737 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002738 */
2739
Allan Stephens6bba9b02016-11-16 14:56:54 -05002740/**
2741 * @typedef k_work_handler_t
2742 * @brief Work item handler function type.
2743 *
2744 * A work item's handler function is executed by a workqueue's thread
2745 * when the work item is processed by the workqueue.
2746 *
2747 * @param work Address of the work item.
2748 *
2749 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002750 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002751 */
2752typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002753
2754/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002755 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002756 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002757
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002758struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002759 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002760 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002761};
2762
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002763enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002764 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002765};
2766
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002767struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002768 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002769 k_work_handler_t handler;
2770 atomic_t flags[1];
2771};
2772
Allan Stephens6bba9b02016-11-16 14:56:54 -05002773struct k_delayed_work {
2774 struct k_work work;
2775 struct _timeout timeout;
2776 struct k_work_q *work_q;
2777};
2778
Piotr Zięcik19d83492019-09-27 09:16:25 +02002779struct k_work_poll {
2780 struct k_work work;
2781 struct _poller poller;
2782 struct k_poll_event *events;
2783 int num_events;
2784 k_work_handler_t real_handler;
2785 struct _timeout timeout;
2786 int poll_result;
2787};
2788
Allan Stephens6bba9b02016-11-16 14:56:54 -05002789extern struct k_work_q k_sys_work_q;
2790
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002791/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002792 * INTERNAL_HIDDEN @endcond
2793 */
2794
Patrik Flykt4344e272019-03-08 14:19:05 -07002795#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002796 { \
2797 ._reserved = NULL, \
2798 .handler = work_handler, \
2799 .flags = { 0 } \
2800 }
2801
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002802#define K_WORK_INITIALIZER __DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002803
Allan Stephens6bba9b02016-11-16 14:56:54 -05002804/**
2805 * @brief Initialize a statically-defined work item.
2806 *
2807 * This macro can be used to initialize a statically-defined workqueue work
2808 * item, prior to its first use. For example,
2809 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002810 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002811 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002812 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002813 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002814 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002815 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002816#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002817 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002818
2819/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002820 * @brief Initialize a work item.
2821 *
2822 * This routine initializes a workqueue work item, prior to its first use.
2823 *
2824 * @param work Address of work item.
2825 * @param handler Function to invoke each time work item is processed.
2826 *
2827 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002828 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002829 */
2830static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2831{
Patrik Flykt4344e272019-03-08 14:19:05 -07002832 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002833}
2834
2835/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002836 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002837 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002838 * This routine submits work item @a work to be processed by workqueue
2839 * @a work_q. If the work item is already pending in the workqueue's queue
2840 * as a result of an earlier submission, this routine has no effect on the
2841 * work item. If the work item has already been processed, or is currently
2842 * being processed, its work is considered complete and the work item can be
2843 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002844 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002845 * @warning
2846 * A submitted work item must not be modified until it has been processed
2847 * by the workqueue.
2848 *
2849 * @note Can be called by ISRs.
2850 *
2851 * @param work_q Address of workqueue.
2852 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002853 *
2854 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002855 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002856 */
2857static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2858 struct k_work *work)
2859{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002860 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002861 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002862 }
2863}
2864
2865/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002866 * @brief Submit a work item to a user mode workqueue
2867 *
David B. Kinder06d78352018-12-17 14:32:40 -08002868 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002869 * memory allocation is made from the caller's resource pool which is freed
2870 * once the worker thread consumes the k_work item. The workqueue
2871 * thread must have memory access to the k_work item being submitted. The caller
2872 * must have permission granted on the work_q parameter's queue object.
2873 *
2874 * Otherwise this works the same as k_work_submit_to_queue().
2875 *
2876 * @note Can be called by ISRs.
2877 *
2878 * @param work_q Address of workqueue.
2879 * @param work Address of work item.
2880 *
2881 * @retval -EBUSY if the work item was already in some workqueue
2882 * @retval -ENOMEM if no memory for thread resource pool allocation
2883 * @retval 0 Success
2884 * @req K-WORK-001
2885 */
2886static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2887 struct k_work *work)
2888{
2889 int ret = -EBUSY;
2890
2891 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2892 ret = k_queue_alloc_append(&work_q->queue, work);
2893
2894 /* Couldn't insert into the queue. Clear the pending bit
2895 * so the work item can be submitted again
2896 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002897 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002898 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2899 }
2900 }
2901
2902 return ret;
2903}
2904
2905/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002906 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002907 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002908 * This routine indicates if work item @a work is pending in a workqueue's
2909 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002910 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002911 * @note Can be called by ISRs.
2912 *
2913 * @param work Address of work item.
2914 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002915 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002916 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002917 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002918static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002919{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002920 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002921}
2922
2923/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002924 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002925 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002926 * This routine starts workqueue @a work_q. The workqueue spawns its work
2927 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002928 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002929 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002930 * @param stack Pointer to work queue thread's stack space, as defined by
2931 * K_THREAD_STACK_DEFINE()
2932 * @param stack_size Size of the work queue thread's stack (in bytes), which
2933 * should either be the same constant passed to
2934 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002935 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002936 *
2937 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002938 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002939 */
Andrew Boie507852a2017-07-25 18:47:07 -07002940extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002941 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002942 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002943
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002944/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002945 * @brief Start a workqueue in user mode
2946 *
2947 * This works identically to k_work_q_start() except it is callable from user
2948 * mode, and the worker thread created will run in user mode.
2949 * The caller must have permissions granted on both the work_q parameter's
2950 * thread and queue objects, and the same restrictions on priority apply as
2951 * k_thread_create().
2952 *
2953 * @param work_q Address of workqueue.
2954 * @param stack Pointer to work queue thread's stack space, as defined by
2955 * K_THREAD_STACK_DEFINE()
2956 * @param stack_size Size of the work queue thread's stack (in bytes), which
2957 * should either be the same constant passed to
2958 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2959 * @param prio Priority of the work queue's thread.
2960 *
2961 * @return N/A
2962 * @req K-WORK-001
2963 */
2964extern void k_work_q_user_start(struct k_work_q *work_q,
2965 k_thread_stack_t *stack,
2966 size_t stack_size, int prio);
2967
2968/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002969 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002970 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002971 * This routine initializes a workqueue delayed work item, prior to
2972 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002973 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002974 * @param work Address of delayed work item.
2975 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002976 *
2977 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002978 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002979 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002980extern void k_delayed_work_init(struct k_delayed_work *work,
2981 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002982
2983/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002984 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002985 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002986 * This routine schedules work item @a work to be processed by workqueue
2987 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002988 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002989 * Only when the countdown completes is the work item actually submitted to
2990 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002991 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002992 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002993 * counting down cancels the existing submission and restarts the
2994 * countdown using the new delay. Note that this behavior is
2995 * inherently subject to race conditions with the pre-existing
2996 * timeouts and work queue, so care must be taken to synchronize such
2997 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002998 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002999 * @warning
3000 * A delayed work item must not be modified until it has been processed
3001 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003002 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003003 * @note Can be called by ISRs.
3004 *
3005 * @param work_q Address of workqueue.
3006 * @param work Address of delayed work item.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003007 * @param delay Non-negative delay before submitting the work item (in
3008 * milliseconds).
Allan Stephens6bba9b02016-11-16 14:56:54 -05003009 *
3010 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003011 * @retval -EINVAL Work item is being processed or has completed its work.
3012 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003013 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003014 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003015extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
3016 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003017 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003018
3019/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003020 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003021 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003022 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07003023 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05003024 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003025 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003026 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003027 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08003028 * @note The result of calling this on a k_delayed_work item that has
3029 * not been submitted (i.e. before the return of the
3030 * k_delayed_work_submit_to_queue() call) is undefined.
3031 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003032 * @param work Address of delayed work item.
3033 *
David B. Kinder8b986d72017-04-18 15:56:26 -07003034 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003035 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003036 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003037 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003038extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003039
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003040/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003041 * @brief Submit a work item to the system workqueue.
3042 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003043 * This routine submits work item @a work to be processed by the system
3044 * workqueue. If the work item is already pending in the workqueue's queue
3045 * as a result of an earlier submission, this routine has no effect on the
3046 * work item. If the work item has already been processed, or is currently
3047 * being processed, its work is considered complete and the work item can be
3048 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003049 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003050 * @warning
3051 * Work items submitted to the system workqueue should avoid using handlers
3052 * that block or yield since this may prevent the system workqueue from
3053 * processing other work items in a timely manner.
3054 *
3055 * @note Can be called by ISRs.
3056 *
3057 * @param work Address of work item.
3058 *
3059 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003060 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003061 */
3062static inline void k_work_submit(struct k_work *work)
3063{
3064 k_work_submit_to_queue(&k_sys_work_q, work);
3065}
3066
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003067/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003068 * @brief Submit a delayed work item to the system workqueue.
3069 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003070 * This routine schedules work item @a work to be processed by the system
3071 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003072 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003073 * Only when the countdown completes is the work item actually submitted to
3074 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003075 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003076 * Submitting a previously submitted delayed work item that is still
3077 * counting down cancels the existing submission and restarts the countdown
3078 * using the new delay. If the work item is currently pending on the
3079 * workqueue's queue because the countdown has completed it is too late to
3080 * resubmit the item, and resubmission fails without impacting the work item.
3081 * If the work item has already been processed, or is currently being processed,
3082 * its work is considered complete and the work item can be resubmitted.
3083 *
3084 * @warning
3085 * Work items submitted to the system workqueue should avoid using handlers
3086 * that block or yield since this may prevent the system workqueue from
3087 * processing other work items in a timely manner.
3088 *
3089 * @note Can be called by ISRs.
3090 *
3091 * @param work Address of delayed work item.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003092 * @param delay Non-negative delay before submitting the work item (in
3093 * milliseconds).
Allan Stephens6bba9b02016-11-16 14:56:54 -05003094 *
3095 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003096 * @retval -EINVAL Work item is being processed or has completed its work.
3097 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003098 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003099 */
3100static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003101 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003102{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05003103 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003104}
3105
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003106/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02003107 * @brief Get time remaining before a delayed work gets scheduled.
3108 *
3109 * This routine computes the (approximate) time remaining before a
3110 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02003111 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02003112 *
3113 * @param work Delayed work item.
3114 *
3115 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003116 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02003117 */
Kumar Galacc334c72017-04-21 10:55:34 -05003118static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02003119{
Andy Ross88924062019-10-03 11:43:10 -07003120 return k_ticks_to_ms_floor64(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02003121}
3122
Piotr Zięcik19d83492019-09-27 09:16:25 +02003123/**
3124 * @brief Initialize a triggered work item.
3125 *
3126 * This routine initializes a workqueue triggered work item, prior to
3127 * its first use.
3128 *
3129 * @param work Address of triggered work item.
3130 * @param handler Function to invoke each time work item is processed.
3131 *
3132 * @return N/A
3133 */
3134extern void k_work_poll_init(struct k_work_poll *work,
3135 k_work_handler_t handler);
3136
3137/**
3138 * @brief Submit a triggered work item.
3139 *
3140 * This routine schedules work item @a work to be processed by workqueue
3141 * @a work_q when one of the given @a events is signaled. The routine
3142 * initiates internal poller for the work item and then returns to the caller.
3143 * Only when one of the watched events happen the work item is actually
3144 * submitted to the workqueue and becomes pending.
3145 *
3146 * Submitting a previously submitted triggered work item that is still
3147 * waiting for the event cancels the existing submission and reschedules it
3148 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003149 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003150 * so care must be taken to synchronize such resubmissions externally.
3151 *
3152 * @note Can be called by ISRs.
3153 *
3154 * @warning
3155 * Provided array of events as well as a triggered work item must be placed
3156 * in persistent memory (valid until work handler execution or work
3157 * cancellation) and cannot be modified after submission.
3158 *
3159 * @param work_q Address of workqueue.
3160 * @param work Address of delayed work item.
3161 * @param events An array of pointers to events which trigger the work.
3162 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003163 * @param timeout Non-negative timeout after which the work will be scheduled
3164 * for execution even if not triggered.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003165 *
3166 *
3167 * @retval 0 Work item started watching for events.
3168 * @retval -EINVAL Work item is being processed or has completed its work.
3169 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3170 */
3171extern int k_work_poll_submit_to_queue(struct k_work_q *work_q,
3172 struct k_work_poll *work,
3173 struct k_poll_event *events,
3174 int num_events,
3175 s32_t timeout);
3176
3177/**
3178 * @brief Submit a triggered work item to the system workqueue.
3179 *
3180 * This routine schedules work item @a work to be processed by system
3181 * workqueue when one of the given @a events is signaled. The routine
3182 * initiates internal poller for the work item and then returns to the caller.
3183 * Only when one of the watched events happen the work item is actually
3184 * submitted to the workqueue and becomes pending.
3185 *
3186 * Submitting a previously submitted triggered work item that is still
3187 * waiting for the event cancels the existing submission and reschedules it
3188 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003189 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003190 * so care must be taken to synchronize such resubmissions externally.
3191 *
3192 * @note Can be called by ISRs.
3193 *
3194 * @warning
3195 * Provided array of events as well as a triggered work item must not be
3196 * modified until the item has been processed by the workqueue.
3197 *
3198 * @param work Address of delayed work item.
3199 * @param events An array of pointers to events which trigger the work.
3200 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003201 * @param timeout Non-negative timeout after which the work will be scheduled
3202 * for execution even if not triggered.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003203 *
3204 * @retval 0 Work item started watching for events.
3205 * @retval -EINVAL Work item is being processed or has completed its work.
3206 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3207 */
3208static inline int k_work_poll_submit(struct k_work_poll *work,
3209 struct k_poll_event *events,
3210 int num_events,
3211 s32_t timeout)
3212{
3213 return k_work_poll_submit_to_queue(&k_sys_work_q, work,
3214 events, num_events, timeout);
3215}
3216
3217/**
3218 * @brief Cancel a triggered work item.
3219 *
3220 * This routine cancels the submission of triggered work item @a work.
3221 * A triggered work item can only be canceled if no event triggered work
3222 * submission.
3223 *
3224 * @note Can be called by ISRs.
3225 *
3226 * @param work Address of delayed work item.
3227 *
David B. Kinder73896c02019-10-28 16:27:57 -07003228 * @retval 0 Work item canceled.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003229 * @retval -EINVAL Work item is being processed or has completed its work.
3230 */
3231extern int k_work_poll_cancel(struct k_work_poll *work);
3232
Anas Nashif166f5192018-02-25 08:02:36 -06003233/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003234/**
Anas Nashifce78d162018-05-24 12:43:11 -05003235 * @defgroup mutex_apis Mutex APIs
3236 * @ingroup kernel_apis
3237 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003238 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003239
Anas Nashifce78d162018-05-24 12:43:11 -05003240/**
3241 * Mutex Structure
3242 * @ingroup mutex_apis
3243 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003244struct k_mutex {
Anas Nashife71293e2019-12-04 20:00:14 -05003245 /** Mutex wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003246 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05003247 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04003248 struct k_thread *owner;
Anas Nashife71293e2019-12-04 20:00:14 -05003249
3250 /** Current lock count */
Kumar Galacc334c72017-04-21 10:55:34 -05003251 u32_t lock_count;
Anas Nashife71293e2019-12-04 20:00:14 -05003252
3253 /** Original thread priority */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003254 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003255
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003256 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003257 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003258};
3259
Anas Nashifce78d162018-05-24 12:43:11 -05003260/**
3261 * @cond INTERNAL_HIDDEN
3262 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003263#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003264 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003265 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003266 .owner = NULL, \
3267 .lock_count = 0, \
3268 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003269 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003270 }
3271
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003272#define K_MUTEX_INITIALIZER __DEPRECATED_MACRO _K_MUTEX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003273
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003274/**
Allan Stephensc98da842016-11-11 15:45:03 -05003275 * INTERNAL_HIDDEN @endcond
3276 */
3277
3278/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003279 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003280 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003281 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003282 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003283 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003285 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003286 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003287 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003288#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003289 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003290 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003291
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003292/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003293 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003294 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003295 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003297 * Upon completion, the mutex is available and does not have an owner.
3298 *
3299 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003300 *
3301 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003302 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003303 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003304__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003305
3306/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003307 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003308 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003309 * This routine locks @a mutex. If the mutex is locked by another thread,
3310 * the calling thread waits until the mutex becomes available or until
3311 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003312 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003313 * A thread is permitted to lock a mutex it has already locked. The operation
3314 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003315 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003316 * @param mutex Address of the mutex.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003317 * @param timeout Non-negative waiting period to lock the mutex (in
3318 * milliseconds), or one of the special values K_NO_WAIT and
3319 * K_FOREVER.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003320 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003321 * @retval 0 Mutex locked.
3322 * @retval -EBUSY Returned without waiting.
3323 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003324 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003325 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003326__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003327
3328/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003329 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003330 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003331 * This routine unlocks @a mutex. The mutex must already be locked by the
3332 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003333 *
3334 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003335 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003336 * thread.
3337 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003338 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003339 *
3340 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003341 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003342 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003343__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003344
Allan Stephensc98da842016-11-11 15:45:03 -05003345/**
Anas Nashif166f5192018-02-25 08:02:36 -06003346 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003347 */
3348
3349/**
3350 * @cond INTERNAL_HIDDEN
3351 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003352
3353struct k_sem {
3354 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303355 u32_t count;
3356 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003357 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003358
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003359 _OBJECT_TRACING_NEXT_PTR(k_sem)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003360 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003361};
3362
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003363#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003364 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003365 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003366 .count = initial_count, \
3367 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003368 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003369 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003370 }
3371
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003372#define K_SEM_INITIALIZER __DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003373
Allan Stephensc98da842016-11-11 15:45:03 -05003374/**
3375 * INTERNAL_HIDDEN @endcond
3376 */
3377
3378/**
3379 * @defgroup semaphore_apis Semaphore APIs
3380 * @ingroup kernel_apis
3381 * @{
3382 */
3383
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003384/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003385 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003386 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003387 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003389 * @param sem Address of the semaphore.
3390 * @param initial_count Initial semaphore count.
3391 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003392 *
3393 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003394 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003395 */
Andrew Boie99280232017-09-29 14:17:47 -07003396__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
3397 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003398
3399/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003400 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003401 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003402 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003404 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3405 *
3406 * @param sem Address of the semaphore.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003407 * @param timeout Non-negative waiting period to take the semaphore (in
3408 * milliseconds), or one of the special values K_NO_WAIT and
3409 * K_FOREVER.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003410 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003411 * @retval 0 Semaphore taken.
3412 * @retval -EBUSY Returned without waiting.
3413 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003414 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003415 */
Andrew Boie99280232017-09-29 14:17:47 -07003416__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003417
3418/**
3419 * @brief Give a semaphore.
3420 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003421 * This routine gives @a sem, unless the semaphore is already at its maximum
3422 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003423 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003424 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003426 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003427 *
3428 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003429 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003430 */
Andrew Boie99280232017-09-29 14:17:47 -07003431__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003432
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003433/**
3434 * @brief Reset a semaphore's count to zero.
3435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003436 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003437 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003438 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003439 *
3440 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003441 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003442 */
Andrew Boie990bf162017-10-03 12:36:49 -07003443__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003444
Anas Nashif954d5502018-02-25 08:37:28 -06003445/**
3446 * @internal
3447 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003448static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003449{
Patrik Flykt24d71432019-03-26 19:57:45 -06003450 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003451}
3452
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003453/**
3454 * @brief Get a semaphore's count.
3455 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003456 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003457 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003458 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003459 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003460 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003461 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003462 */
Andrew Boie990bf162017-10-03 12:36:49 -07003463__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003464
Anas Nashif954d5502018-02-25 08:37:28 -06003465/**
3466 * @internal
3467 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003468static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003469{
3470 return sem->count;
3471}
3472
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003473/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003474 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003475 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003476 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003477 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003478 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003480 * @param name Name of the semaphore.
3481 * @param initial_count Initial semaphore count.
3482 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003483 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003484 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003485#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003486 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003487 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303488 BUILD_ASSERT(((count_limit) != 0) && \
3489 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003490
Anas Nashif166f5192018-02-25 08:02:36 -06003491/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003492
3493/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003494 * @defgroup msgq_apis Message Queue APIs
3495 * @ingroup kernel_apis
3496 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003497 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003498
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003499/**
3500 * @brief Message Queue Structure
3501 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003502struct k_msgq {
Anas Nashife71293e2019-12-04 20:00:14 -05003503 /** Message queue wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003504 _wait_q_t wait_q;
Anas Nashife71293e2019-12-04 20:00:14 -05003505 /** Lock */
Andy Rossbe03dbd2018-07-26 10:23:02 -07003506 struct k_spinlock lock;
Anas Nashife71293e2019-12-04 20:00:14 -05003507 /** Message size */
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003508 size_t msg_size;
Anas Nashife71293e2019-12-04 20:00:14 -05003509 /** Maximal number of messages */
Kumar Galacc334c72017-04-21 10:55:34 -05003510 u32_t max_msgs;
Anas Nashife71293e2019-12-04 20:00:14 -05003511 /** Start of message buffer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003512 char *buffer_start;
Anas Nashife71293e2019-12-04 20:00:14 -05003513 /** End of message buffer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003514 char *buffer_end;
Anas Nashife71293e2019-12-04 20:00:14 -05003515 /** Read pointer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003516 char *read_ptr;
Anas Nashife71293e2019-12-04 20:00:14 -05003517 /** Write pointer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003518 char *write_ptr;
Anas Nashife71293e2019-12-04 20:00:14 -05003519 /** Number of used messages */
Kumar Galacc334c72017-04-21 10:55:34 -05003520 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003521
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003522 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003523 _OBJECT_TRACING_LINKED_FLAG
Anas Nashife71293e2019-12-04 20:00:14 -05003524
3525 /** Message queue */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003526 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003527};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003528/**
3529 * @cond INTERNAL_HIDDEN
3530 */
3531
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003532
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003533#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003534 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003535 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003536 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003537 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003538 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003539 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003540 .read_ptr = q_buffer, \
3541 .write_ptr = q_buffer, \
3542 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003543 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003544 }
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003545#define K_MSGQ_INITIALIZER __DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003546/**
3547 * INTERNAL_HIDDEN @endcond
3548 */
3549
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003550
Andrew Boie0fe789f2018-04-12 18:35:56 -07003551#define K_MSGQ_FLAG_ALLOC BIT(0)
3552
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003553/**
3554 * @brief Message Queue Attributes
3555 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303556struct k_msgq_attrs {
Anas Nashife71293e2019-12-04 20:00:14 -05003557 /** Message Size */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303558 size_t msg_size;
Anas Nashife71293e2019-12-04 20:00:14 -05003559 /** Maximal number of messages */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303560 u32_t max_msgs;
Anas Nashife71293e2019-12-04 20:00:14 -05003561 /** Used messages */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303562 u32_t used_msgs;
3563};
3564
Allan Stephensc98da842016-11-11 15:45:03 -05003565
3566/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003567 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003568 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003569 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3570 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003571 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3572 * message is similarly aligned to this boundary, @a q_msg_size must also be
3573 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003574 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003575 * The message queue can be accessed outside the module where it is defined
3576 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003577 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003578 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003579 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003580 * @param q_name Name of the message queue.
3581 * @param q_msg_size Message size (in bytes).
3582 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003583 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003584 *
3585 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003586 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003587#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3588 static char __noinit __aligned(q_align) \
3589 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3590 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
3591 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003592 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003593
Peter Mitsisd7a37502016-10-13 11:37:40 -04003594/**
3595 * @brief Initialize a message queue.
3596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003597 * This routine initializes a message queue object, prior to its first use.
3598 *
Allan Stephensda827222016-11-09 14:23:58 -06003599 * The message queue's ring buffer must contain space for @a max_msgs messages,
3600 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3601 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3602 * that each message is similarly aligned to this boundary, @a q_msg_size
3603 * must also be a multiple of N.
3604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003605 * @param q Address of the message queue.
3606 * @param buffer Pointer to ring buffer that holds queued messages.
3607 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003608 * @param max_msgs Maximum number of messages that can be queued.
3609 *
3610 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003611 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003612 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003613void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3614 u32_t max_msgs);
3615
3616/**
3617 * @brief Initialize a message queue.
3618 *
3619 * This routine initializes a message queue object, prior to its first use,
3620 * allocating its internal ring buffer from the calling thread's resource
3621 * pool.
3622 *
3623 * Memory allocated for the ring buffer can be released by calling
3624 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3625 * all of its references.
3626 *
3627 * @param q Address of the message queue.
3628 * @param msg_size Message size (in bytes).
3629 * @param max_msgs Maximum number of messages that can be queued.
3630 *
3631 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3632 * thread's resource pool, or -EINVAL if the size parameters cause
3633 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003634 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003635 */
3636__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3637 u32_t max_msgs);
3638
Anas Nashife71293e2019-12-04 20:00:14 -05003639/**
3640 * @brief Cleanup message queue
3641 *
3642 * Releases memory allocated for the ring buffer.
3643 * @param q
3644 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003645void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003646
3647/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003648 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003649 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003650 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003651 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003652 * @note Can be called by ISRs.
3653 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003654 * @param q Address of the message queue.
3655 * @param data Pointer to the message.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003656 * @param timeout Non-negative waiting period to add the message (in
3657 * milliseconds), or one of the special values K_NO_WAIT and
3658 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003659 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003660 * @retval 0 Message sent.
3661 * @retval -ENOMSG Returned without waiting or queue purged.
3662 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003663 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003664 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003665__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003666
3667/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003668 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003670 * This routine receives a message from message queue @a q in a "first in,
3671 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003672 *
Allan Stephensc98da842016-11-11 15:45:03 -05003673 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003674 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003675 * @param q Address of the message queue.
3676 * @param data Address of area to hold the received message.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003677 * @param timeout Non-negative waiting period to receive the message (in
3678 * milliseconds), or one of the special values K_NO_WAIT and
3679 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003680 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003681 * @retval 0 Message received.
3682 * @retval -ENOMSG Returned without waiting.
3683 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003684 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003685 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003686__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003687
3688/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003689 * @brief Peek/read a message from a message queue.
3690 *
3691 * This routine reads a message from message queue @a q in a "first in,
3692 * first out" manner and leaves the message in the queue.
3693 *
3694 * @note Can be called by ISRs.
3695 *
3696 * @param q Address of the message queue.
3697 * @param data Address of area to hold the message read from the queue.
3698 *
3699 * @retval 0 Message read.
3700 * @retval -ENOMSG Returned when the queue has no message.
3701 * @req K-MSGQ-002
3702 */
3703__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3704
3705/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003706 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003708 * This routine discards all unreceived messages in a message queue's ring
3709 * buffer. Any threads that are blocked waiting to send a message to the
3710 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003711 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003712 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003713 *
3714 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003715 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003716 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003717__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003718
Peter Mitsis67be2492016-10-07 11:44:34 -04003719/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003720 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003722 * This routine returns the number of unused entries in a message queue's
3723 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003724 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003725 * @param q Address of the message queue.
3726 *
3727 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003728 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003729 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003730__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3731
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303732/**
3733 * @brief Get basic attributes of a message queue.
3734 *
3735 * This routine fetches basic attributes of message queue into attr argument.
3736 *
3737 * @param q Address of the message queue.
3738 * @param attrs pointer to message queue attribute structure.
3739 *
3740 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003741 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303742 */
3743__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3744
3745
Patrik Flykt4344e272019-03-08 14:19:05 -07003746static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003747{
3748 return q->max_msgs - q->used_msgs;
3749}
3750
Peter Mitsisd7a37502016-10-13 11:37:40 -04003751/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003752 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003753 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003754 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003755 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003756 * @param q Address of the message queue.
3757 *
3758 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003759 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003760 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003761__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3762
Patrik Flykt4344e272019-03-08 14:19:05 -07003763static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003764{
3765 return q->used_msgs;
3766}
3767
Anas Nashif166f5192018-02-25 08:02:36 -06003768/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003769
3770/**
3771 * @defgroup mem_pool_apis Memory Pool APIs
3772 * @ingroup kernel_apis
3773 * @{
3774 */
3775
Andy Ross73cb9582017-05-09 10:42:39 -07003776/* Note on sizing: the use of a 20 bit field for block means that,
3777 * assuming a reasonable minimum block size of 16 bytes, we're limited
3778 * to 16M of memory managed by a single pool. Long term it would be
3779 * good to move to a variable bit size based on configuration.
3780 */
3781struct k_mem_block_id {
3782 u32_t pool : 8;
3783 u32_t level : 4;
3784 u32_t block : 20;
3785};
3786
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003787struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003788 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003789 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003790};
3791
Anas Nashif166f5192018-02-25 08:02:36 -06003792/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003793
3794/**
3795 * @defgroup mailbox_apis Mailbox APIs
3796 * @ingroup kernel_apis
3797 * @{
3798 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003799
Anas Nashife71293e2019-12-04 20:00:14 -05003800/**
3801 * @brief Mailbox Message Structure
3802 *
3803 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003804struct k_mbox_msg {
3805 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003806 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003807 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003808 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003809 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003810 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003811 /** sender's message data buffer */
3812 void *tx_data;
3813 /** internal use only - needed for legacy API support */
3814 void *_rx_data;
3815 /** message data block descriptor */
3816 struct k_mem_block tx_block;
3817 /** source thread id */
3818 k_tid_t rx_source_thread;
3819 /** target thread id */
3820 k_tid_t tx_target_thread;
3821 /** internal use only - thread waiting on send (may be a dummy) */
3822 k_tid_t _syncing_thread;
3823#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3824 /** internal use only - semaphore used during asynchronous send */
3825 struct k_sem *_async_sem;
3826#endif
3827};
Anas Nashife71293e2019-12-04 20:00:14 -05003828/**
3829 * @brief Mailbox Structure
3830 *
3831 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003832struct k_mbox {
Anas Nashife71293e2019-12-04 20:00:14 -05003833 /** Transmit messages queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003834 _wait_q_t tx_msg_queue;
Anas Nashife71293e2019-12-04 20:00:14 -05003835 /** Receive message queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003836 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003837 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003838
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003839 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003840 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003841};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003842/**
3843 * @cond INTERNAL_HIDDEN
3844 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003845
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003846#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003847 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003848 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3849 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003850 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003851 }
3852
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003853#define K_MBOX_INITIALIZER __DEPRECATED_MACRO _K_MBOX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003854
Peter Mitsis12092702016-10-14 12:57:23 -04003855/**
Allan Stephensc98da842016-11-11 15:45:03 -05003856 * INTERNAL_HIDDEN @endcond
3857 */
3858
3859/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003860 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003862 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003863 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003864 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003866 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003867 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003868 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003869#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003870 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003871 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003872
Peter Mitsis12092702016-10-14 12:57:23 -04003873/**
3874 * @brief Initialize a mailbox.
3875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003876 * This routine initializes a mailbox object, prior to its first use.
3877 *
3878 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003879 *
3880 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003881 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003882 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003883extern void k_mbox_init(struct k_mbox *mbox);
3884
Peter Mitsis12092702016-10-14 12:57:23 -04003885/**
3886 * @brief Send a mailbox message in a synchronous manner.
3887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003888 * This routine sends a message to @a mbox and waits for a receiver to both
3889 * receive and process it. The message data may be in a buffer, in a memory
3890 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003892 * @param mbox Address of the mailbox.
3893 * @param tx_msg Address of the transmit message descriptor.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003894 * @param timeout Non-negative waiting period for the message to be received (in
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003895 * milliseconds), or one of the special values K_NO_WAIT
3896 * and K_FOREVER. Once the message has been received,
3897 * this routine waits as long as necessary for the message
3898 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003899 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003900 * @retval 0 Message sent.
3901 * @retval -ENOMSG Returned without waiting.
3902 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003903 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003904 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003905extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003906 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003907
Peter Mitsis12092702016-10-14 12:57:23 -04003908/**
3909 * @brief Send a mailbox message in an asynchronous manner.
3910 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003911 * This routine sends a message to @a mbox without waiting for a receiver
3912 * to process it. The message data may be in a buffer, in a memory pool block,
3913 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3914 * will be given when the message has been both received and completely
3915 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003917 * @param mbox Address of the mailbox.
3918 * @param tx_msg Address of the transmit message descriptor.
3919 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003920 *
3921 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003922 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003923 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003924extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003925 struct k_sem *sem);
3926
Peter Mitsis12092702016-10-14 12:57:23 -04003927/**
3928 * @brief Receive a mailbox message.
3929 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003930 * This routine receives a message from @a mbox, then optionally retrieves
3931 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003933 * @param mbox Address of the mailbox.
3934 * @param rx_msg Address of the receive message descriptor.
3935 * @param buffer Address of the buffer to receive data, or NULL to defer data
3936 * retrieval and message disposal until later.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003937 * @param timeout Non-negative waiting period for a message to be received (in
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003938 * milliseconds), or one of the special values K_NO_WAIT
3939 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003940 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003941 * @retval 0 Message received.
3942 * @retval -ENOMSG Returned without waiting.
3943 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003944 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003945 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003946extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003947 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003948
3949/**
3950 * @brief Retrieve mailbox message data into a buffer.
3951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003952 * This routine completes the processing of a received message by retrieving
3953 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003954 *
3955 * Alternatively, this routine can be used to dispose of a received message
3956 * without retrieving its data.
3957 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003958 * @param rx_msg Address of the receive message descriptor.
3959 * @param buffer Address of the buffer to receive data, or NULL to discard
3960 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003961 *
3962 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003963 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003964 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003965extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003966
3967/**
3968 * @brief Retrieve mailbox message data into a memory pool block.
3969 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003970 * This routine completes the processing of a received message by retrieving
3971 * its data into a memory pool block, then disposing of the message.
3972 * The memory pool block that results from successful retrieval must be
3973 * returned to the pool once the data has been processed, even in cases
3974 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003975 *
3976 * Alternatively, this routine can be used to dispose of a received message
3977 * without retrieving its data. In this case there is no need to return a
3978 * memory pool block to the pool.
3979 *
3980 * This routine allocates a new memory pool block for the data only if the
3981 * data is not already in one. If a new block cannot be allocated, the routine
3982 * returns a failure code and the received message is left unchanged. This
3983 * permits the caller to reattempt data retrieval at a later time or to dispose
3984 * of the received message without retrieving its data.
3985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003986 * @param rx_msg Address of a receive message descriptor.
3987 * @param pool Address of memory pool, or NULL to discard data.
3988 * @param block Address of the area to hold memory pool block info.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003989 * @param timeout Non-negative waiting period to wait for a memory pool block
3990 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003991 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003992 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003993 * @retval 0 Data retrieved.
3994 * @retval -ENOMEM Returned without waiting.
3995 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003996 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003997 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003998extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003999 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05004000 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004001
Anas Nashif166f5192018-02-25 08:02:36 -06004002/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004003
4004/**
Anas Nashifce78d162018-05-24 12:43:11 -05004005 * @defgroup pipe_apis Pipe APIs
4006 * @ingroup kernel_apis
4007 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05004008 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004009
Anas Nashifce78d162018-05-24 12:43:11 -05004010/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004011struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05004012 unsigned char *buffer; /**< Pipe buffer: may be NULL */
4013 size_t size; /**< Buffer size */
4014 size_t bytes_used; /**< # bytes used in buffer */
4015 size_t read_index; /**< Where in buffer to read from */
4016 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08004017 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004018
4019 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05004020 _wait_q_t readers; /**< Reader wait queue */
4021 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004022 } wait_q;
4023
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004024 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004025 _OBJECT_TRACING_LINKED_FLAG
Anas Nashifce78d162018-05-24 12:43:11 -05004026 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004027};
4028
Anas Nashifce78d162018-05-24 12:43:11 -05004029/**
4030 * @cond INTERNAL_HIDDEN
4031 */
4032#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
4033
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01004034#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
4035 { \
4036 .buffer = pipe_buffer, \
4037 .size = pipe_buffer_size, \
4038 .bytes_used = 0, \
4039 .read_index = 0, \
4040 .write_index = 0, \
4041 .lock = {}, \
4042 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004043 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
4044 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01004045 }, \
4046 _OBJECT_TRACING_INIT \
4047 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004048 }
4049
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05004050#define K_PIPE_INITIALIZER __DEPRECATED_MACRO _K_PIPE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004051
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004052/**
Allan Stephensc98da842016-11-11 15:45:03 -05004053 * INTERNAL_HIDDEN @endcond
4054 */
4055
4056/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004057 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004058 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004059 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004060 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004061 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004062 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004063 * @param name Name of the pipe.
4064 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
4065 * or zero if no ring buffer is used.
4066 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004067 *
4068 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004069 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004070#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08004071 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07004072 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004073 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004074 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004075
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004076/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004077 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004078 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004079 * This routine initializes a pipe object, prior to its first use.
4080 *
4081 * @param pipe Address of the pipe.
4082 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
4083 * is used.
4084 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4085 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004086 *
4087 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004088 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004089 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004090void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
4091
4092/**
4093 * @brief Release a pipe's allocated buffer
4094 *
4095 * If a pipe object was given a dynamically allocated buffer via
4096 * k_pipe_alloc_init(), this will free it. This function does nothing
4097 * if the buffer wasn't dynamically allocated.
4098 *
4099 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004100 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004101 */
4102void k_pipe_cleanup(struct k_pipe *pipe);
4103
4104/**
4105 * @brief Initialize a pipe and allocate a buffer for it
4106 *
4107 * Storage for the buffer region will be allocated from the calling thread's
4108 * resource pool. This memory will be released if k_pipe_cleanup() is called,
4109 * or userspace is enabled and the pipe object loses all references to it.
4110 *
4111 * This function should only be called on uninitialized pipe objects.
4112 *
4113 * @param pipe Address of the pipe.
4114 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4115 * buffer is used.
4116 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004117 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004118 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004119 */
4120__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004121
4122/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004123 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004125 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004127 * @param pipe Address of the pipe.
4128 * @param data Address of data to write.
4129 * @param bytes_to_write Size of data (in bytes).
4130 * @param bytes_written Address of area to hold the number of bytes written.
4131 * @param min_xfer Minimum number of bytes to write.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004132 * @param timeout Non-negative waiting period to wait for the data to be written
4133 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004134 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004135 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004136 * @retval 0 At least @a min_xfer bytes of data were written.
4137 * @retval -EIO Returned without waiting; zero data bytes were written.
4138 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004139 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004140 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004141 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004142__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
4143 size_t bytes_to_write, size_t *bytes_written,
4144 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004145
4146/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004147 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004148 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004149 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004150 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004151 * @param pipe Address of the pipe.
4152 * @param data Address to place the data read from pipe.
4153 * @param bytes_to_read Maximum number of data bytes to read.
4154 * @param bytes_read Address of area to hold the number of bytes read.
4155 * @param min_xfer Minimum number of data bytes to read.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004156 * @param timeout Non-negative waiting period to wait for the data to be read
4157 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004158 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004159 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004160 * @retval 0 At least @a min_xfer bytes of data were read.
4161 * @retval -EIO Returned without waiting; zero data bytes were read.
4162 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004163 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004164 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004165 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004166__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
4167 size_t bytes_to_read, size_t *bytes_read,
4168 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004169
4170/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004171 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004172 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004173 * This routine writes the data contained in a memory block to @a pipe.
4174 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004175 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004176 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004177 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004178 * @param block Memory block containing data to send
4179 * @param size Number of data bytes in memory block to send
4180 * @param sem Semaphore to signal upon completion (else NULL)
4181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004182 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004183 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004184 */
4185extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
4186 size_t size, struct k_sem *sem);
4187
Anas Nashif166f5192018-02-25 08:02:36 -06004188/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004189
Allan Stephensc98da842016-11-11 15:45:03 -05004190/**
4191 * @cond INTERNAL_HIDDEN
4192 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004193
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004194struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004195 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05004196 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04004197 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004198 char *buffer;
4199 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05004200 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004201
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004202 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004203 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004204};
4205
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004206#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004207 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004208 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004209 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004210 .num_blocks = slab_num_blocks, \
4211 .block_size = slab_block_size, \
4212 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004213 .free_list = NULL, \
4214 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05004215 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004216 }
4217
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05004218#define K_MEM_SLAB_INITIALIZER __DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004219
4220
Peter Mitsis578f9112016-10-07 13:50:31 -04004221/**
Allan Stephensc98da842016-11-11 15:45:03 -05004222 * INTERNAL_HIDDEN @endcond
4223 */
4224
4225/**
4226 * @defgroup mem_slab_apis Memory Slab APIs
4227 * @ingroup kernel_apis
4228 * @{
4229 */
4230
4231/**
Allan Stephensda827222016-11-09 14:23:58 -06004232 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04004233 *
Allan Stephensda827222016-11-09 14:23:58 -06004234 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004235 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06004236 * @a slab_align -byte boundary. To ensure that each memory block is similarly
4237 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004238 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04004239 *
Allan Stephensda827222016-11-09 14:23:58 -06004240 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004241 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004242 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004243 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004244 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004245 * @param name Name of the memory slab.
4246 * @param slab_block_size Size of each memory block (in bytes).
4247 * @param slab_num_blocks Number memory blocks.
4248 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004249 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04004250 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004251#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004252 char __noinit __aligned(WB_UP(slab_align)) \
4253 _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004254 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004255 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004256 WB_UP(slab_block_size), slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004257
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004258/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004259 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004260 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004261 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004262 *
Allan Stephensda827222016-11-09 14:23:58 -06004263 * The memory slab's buffer contains @a slab_num_blocks memory blocks
4264 * that are @a slab_block_size bytes long. The buffer must be aligned to an
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004265 * N-byte boundary matching a word boundary, where N is a power of 2
4266 * (i.e. 4 on 32-bit systems, 8, 16, ...).
Allan Stephensda827222016-11-09 14:23:58 -06004267 * To ensure that each memory block is similarly aligned to this boundary,
4268 * @a slab_block_size must also be a multiple of N.
4269 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004270 * @param slab Address of the memory slab.
4271 * @param buffer Pointer to buffer used for the memory blocks.
4272 * @param block_size Size of each memory block (in bytes).
4273 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004274 *
4275 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004276 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004277 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004278extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05004279 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004280
4281/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004282 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004283 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004284 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004285 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004286 * @param slab Address of the memory slab.
4287 * @param mem Pointer to block address area.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004288 * @param timeout Non-negative waiting period to wait for operation to complete
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004289 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4290 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004291 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004292 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004293 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004294 * @retval -ENOMEM Returned without waiting.
4295 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004296 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004297 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004298extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05004299 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004300
4301/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004302 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004303 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004304 * This routine releases a previously allocated memory block back to its
4305 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004306 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004307 * @param slab Address of the memory slab.
4308 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004309 *
4310 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004311 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004312 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004313extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004314
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004315/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004316 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004318 * This routine gets the number of memory blocks that are currently
4319 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004320 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004321 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004322 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004323 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004324 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004325 */
Kumar Galacc334c72017-04-21 10:55:34 -05004326static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004327{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004328 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004329}
4330
Peter Mitsisc001aa82016-10-13 13:53:37 -04004331/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004332 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004333 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004334 * This routine gets the number of memory blocks that are currently
4335 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004337 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004338 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004339 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004340 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004341 */
Kumar Galacc334c72017-04-21 10:55:34 -05004342static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004343{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004344 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004345}
4346
Anas Nashif166f5192018-02-25 08:02:36 -06004347/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004348
4349/**
4350 * @cond INTERNAL_HIDDEN
4351 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004352
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004353struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004354 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004355 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004356};
4357
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004358/**
Allan Stephensc98da842016-11-11 15:45:03 -05004359 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004360 */
4361
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004362/**
Allan Stephensc98da842016-11-11 15:45:03 -05004363 * @addtogroup mem_pool_apis
4364 * @{
4365 */
4366
4367/**
4368 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004369 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004370 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4371 * long. The memory pool allows blocks to be repeatedly partitioned into
4372 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004373 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004374 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004375 * If the pool is to be accessed outside the module where it is defined, it
4376 * can be declared via
4377 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004378 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004379 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004380 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004381 * @param minsz Size of the smallest blocks in the pool (in bytes).
4382 * @param maxsz Size of the largest blocks in the pool (in bytes).
4383 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004384 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004385 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004386 */
Andy Ross73cb9582017-05-09 10:42:39 -07004387#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004388 char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \
Andy Ross73cb9582017-05-09 10:42:39 -07004389 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004390 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004391 Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004392 .base = { \
4393 .buf = _mpool_buf_##name, \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004394 .max_sz = WB_UP(maxsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004395 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004396 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004397 .levels = _mpool_lvls_##name, \
4398 .flags = SYS_MEM_POOL_KERNEL \
4399 } \
Johann Fischer223a2b92019-07-04 15:55:20 +02004400 }; \
Nicolas Pitreb2a022b2019-09-26 16:36:40 -04004401 BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004402
Peter Mitsis937042c2016-10-13 13:18:26 -04004403/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004404 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004406 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004407 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004408 * @param pool Address of the memory pool.
4409 * @param block Pointer to block descriptor for the allocated memory.
4410 * @param size Amount of memory to allocate (in bytes).
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004411 * @param timeout Non-negative waiting period to wait for operation to complete
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004412 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4413 * or K_FOREVER to wait as long as necessary.
4414 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004415 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004416 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004417 * @retval -ENOMEM Returned without waiting.
4418 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004419 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004420 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004421extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004422 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004423
4424/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004425 * @brief Allocate memory from a memory pool with malloc() semantics
4426 *
4427 * Such memory must be released using k_free().
4428 *
4429 * @param pool Address of the memory pool.
4430 * @param size Amount of memory to allocate (in bytes).
4431 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004432 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004433 */
4434extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4435
4436/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004437 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004439 * This routine releases a previously allocated memory block back to its
4440 * memory pool.
4441 *
4442 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004443 *
4444 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004445 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004446 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004447extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004448
4449/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004450 * @brief Free memory allocated from a memory pool.
4451 *
4452 * This routine releases a previously allocated memory block back to its
4453 * memory pool
4454 *
4455 * @param id Memory block identifier.
4456 *
4457 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004458 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004459 */
4460extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4461
4462/**
Anas Nashif166f5192018-02-25 08:02:36 -06004463 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004464 */
4465
4466/**
4467 * @defgroup heap_apis Heap Memory Pool APIs
4468 * @ingroup kernel_apis
4469 * @{
4470 */
4471
4472/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004473 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004475 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004476 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004478 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004480 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004481 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004482 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004483extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004484
4485/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004486 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004487 *
4488 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004489 * returned must have been allocated from the heap memory pool or
4490 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004491 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004492 * If @a ptr is NULL, no operation is performed.
4493 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004494 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004495 *
4496 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004497 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004498 */
4499extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004500
Allan Stephensc98da842016-11-11 15:45:03 -05004501/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004502 * @brief Allocate memory from heap, array style
4503 *
4504 * This routine provides traditional calloc() semantics. Memory is
4505 * allocated from the heap memory pool and zeroed.
4506 *
4507 * @param nmemb Number of elements in the requested array
4508 * @param size Size of each array element (in bytes).
4509 *
4510 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004511 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004512 */
4513extern void *k_calloc(size_t nmemb, size_t size);
4514
Anas Nashif166f5192018-02-25 08:02:36 -06004515/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004516
Benjamin Walshacc68c12017-01-29 18:57:45 -05004517/* polling API - PRIVATE */
4518
Benjamin Walshb0179862017-02-02 16:39:57 -05004519#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004520#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004521#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004522#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004523#endif
4524
Benjamin Walshacc68c12017-01-29 18:57:45 -05004525/* private - types bit positions */
4526enum _poll_types_bits {
4527 /* can be used to ignore an event */
4528 _POLL_TYPE_IGNORE,
4529
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004530 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004531 _POLL_TYPE_SIGNAL,
4532
4533 /* semaphore availability */
4534 _POLL_TYPE_SEM_AVAILABLE,
4535
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004536 /* queue/fifo/lifo data availability */
4537 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004538
4539 _POLL_NUM_TYPES
4540};
4541
Patrik Flykt4344e272019-03-08 14:19:05 -07004542#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004543
4544/* private - states bit positions */
4545enum _poll_states_bits {
4546 /* default state when creating event */
4547 _POLL_STATE_NOT_READY,
4548
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004549 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004550 _POLL_STATE_SIGNALED,
4551
4552 /* semaphore is available */
4553 _POLL_STATE_SEM_AVAILABLE,
4554
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004555 /* data is available to read on queue/fifo/lifo */
4556 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004557
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004558 /* queue/fifo/lifo wait was cancelled */
4559 _POLL_STATE_CANCELLED,
4560
Benjamin Walshacc68c12017-01-29 18:57:45 -05004561 _POLL_NUM_STATES
4562};
4563
Patrik Flykt4344e272019-03-08 14:19:05 -07004564#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004565
4566#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004567 (32 - (0 \
4568 + 8 /* tag */ \
4569 + _POLL_NUM_TYPES \
4570 + _POLL_NUM_STATES \
4571 + 1 /* modes */ \
4572 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004573
Benjamin Walshacc68c12017-01-29 18:57:45 -05004574/* end of polling API - PRIVATE */
4575
4576
4577/**
4578 * @defgroup poll_apis Async polling APIs
4579 * @ingroup kernel_apis
4580 * @{
4581 */
4582
4583/* Public polling API */
4584
4585/* public - values for k_poll_event.type bitfield */
4586#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004587#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4588#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4589#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004590#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004591
4592/* public - polling modes */
4593enum k_poll_modes {
4594 /* polling thread does not take ownership of objects when available */
4595 K_POLL_MODE_NOTIFY_ONLY = 0,
4596
4597 K_POLL_NUM_MODES
4598};
4599
4600/* public - values for k_poll_event.state bitfield */
4601#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004602#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4603#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4604#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004605#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004606#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004607
4608/* public - poll signal object */
4609struct k_poll_signal {
Anas Nashife71293e2019-12-04 20:00:14 -05004610 /** PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004611 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004612
Anas Nashife71293e2019-12-04 20:00:14 -05004613 /**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004614 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4615 * user resets it to 0.
4616 */
4617 unsigned int signaled;
4618
Anas Nashife71293e2019-12-04 20:00:14 -05004619 /** custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004620 int result;
4621};
4622
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004623#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004624 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004625 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004626 .signaled = 0, \
4627 .result = 0, \
4628 }
Anas Nashife71293e2019-12-04 20:00:14 -05004629/**
4630 * @brief Poll Event
4631 *
4632 */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004633struct k_poll_event {
Anas Nashife71293e2019-12-04 20:00:14 -05004634 /** PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004635 sys_dnode_t _node;
4636
Anas Nashife71293e2019-12-04 20:00:14 -05004637 /** PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004638 struct _poller *poller;
4639
Anas Nashife71293e2019-12-04 20:00:14 -05004640 /** optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004641 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004642
Anas Nashife71293e2019-12-04 20:00:14 -05004643 /** bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004644 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004645
Anas Nashife71293e2019-12-04 20:00:14 -05004646 /** bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004647 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004648
Anas Nashife71293e2019-12-04 20:00:14 -05004649 /** mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004650 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004651
Anas Nashife71293e2019-12-04 20:00:14 -05004652 /** unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004653 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004654
Anas Nashife71293e2019-12-04 20:00:14 -05004655 /** per-type data */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004656 union {
4657 void *obj;
4658 struct k_poll_signal *signal;
4659 struct k_sem *sem;
4660 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004661 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004662 };
4663};
4664
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004665#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004666 { \
4667 .poller = NULL, \
4668 .type = event_type, \
4669 .state = K_POLL_STATE_NOT_READY, \
4670 .mode = event_mode, \
4671 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004672 { .obj = event_obj }, \
4673 }
4674
4675#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4676 event_tag) \
4677 { \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004678 .tag = event_tag, \
Markus Fuchsbe21d3f2019-10-09 21:31:25 +02004679 .type = event_type, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004680 .state = K_POLL_STATE_NOT_READY, \
4681 .mode = event_mode, \
4682 .unused = 0, \
4683 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004684 }
4685
4686/**
4687 * @brief Initialize one struct k_poll_event instance
4688 *
4689 * After this routine is called on a poll event, the event it ready to be
4690 * placed in an event array to be passed to k_poll().
4691 *
4692 * @param event The event to initialize.
4693 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4694 * values. Only values that apply to the same object being polled
4695 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4696 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004697 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004698 * @param obj Kernel object or poll signal.
4699 *
4700 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004701 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004702 */
4703
Kumar Galacc334c72017-04-21 10:55:34 -05004704extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004705 int mode, void *obj);
4706
4707/**
4708 * @brief Wait for one or many of multiple poll events to occur
4709 *
4710 * This routine allows a thread to wait concurrently for one or many of
4711 * multiple poll events to have occurred. Such events can be a kernel object
4712 * being available, like a semaphore, or a poll signal event.
4713 *
4714 * When an event notifies that a kernel object is available, the kernel object
4715 * is not "given" to the thread calling k_poll(): it merely signals the fact
4716 * that the object was available when the k_poll() call was in effect. Also,
4717 * all threads trying to acquire an object the regular way, i.e. by pending on
4718 * the object, have precedence over the thread polling on the object. This
4719 * means that the polling thread will never get the poll event on an object
4720 * until the object becomes available and its pend queue is empty. For this
4721 * reason, the k_poll() call is more effective when the objects being polled
4722 * only have one thread, the polling thread, trying to acquire them.
4723 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004724 * When k_poll() returns 0, the caller should loop on all the events that were
4725 * passed to k_poll() and check the state field for the values that were
4726 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004727 *
4728 * Before being reused for another call to k_poll(), the user has to reset the
4729 * state field to K_POLL_STATE_NOT_READY.
4730 *
Andrew Boie3772f772018-05-07 16:52:57 -07004731 * When called from user mode, a temporary memory allocation is required from
4732 * the caller's resource pool.
4733 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004734 * @param events An array of pointers to events to be polled for.
4735 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004736 * @param timeout Non-negative waiting period for an event to be ready (in
4737 * milliseconds), or one of the special values K_NO_WAIT and
4738 * K_FOREVER.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004739 *
4740 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004741 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004742 * @retval -EINTR Polling has been interrupted, e.g. with
4743 * k_queue_cancel_wait(). All output events are still set and valid,
4744 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4745 * words, -EINTR status means that at least one of output events is
4746 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004747 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4748 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004749 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004750 */
4751
Andrew Boie3772f772018-05-07 16:52:57 -07004752__syscall int k_poll(struct k_poll_event *events, int num_events,
4753 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004754
4755/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004756 * @brief Initialize a poll signal object.
4757 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004758 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004759 *
4760 * @param signal A poll signal.
4761 *
4762 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004763 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004764 */
4765
Andrew Boie3772f772018-05-07 16:52:57 -07004766__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4767
4768/*
4769 * @brief Reset a poll signal object's state to unsignaled.
4770 *
4771 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004772 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004773 */
4774__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4775
Patrik Flykt4344e272019-03-08 14:19:05 -07004776static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004777{
Patrik Flykt24d71432019-03-26 19:57:45 -06004778 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004779}
4780
4781/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004782 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004783 *
4784 * @param signal A poll signal object
4785 * @param signaled An integer buffer which will be written nonzero if the
4786 * object was signaled
4787 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004788 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004789 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004790 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004791 */
4792__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4793 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004794
4795/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004796 * @brief Signal a poll signal object.
4797 *
4798 * This routine makes ready a poll signal, which is basically a poll event of
4799 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4800 * made ready to run. A @a result value can be specified.
4801 *
4802 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004803 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004804 * k_poll_signal_reset(). It thus has to be reset by the user before being
4805 * passed again to k_poll() or k_poll() will consider it being signaled, and
4806 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004807 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004808 * @note The result is stored and the 'signaled' field is set even if
4809 * this function returns an error indicating that an expiring poll was
4810 * not notified. The next k_poll() will detect the missed raise.
4811 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004812 * @param signal A poll signal.
4813 * @param result The value to store in the result field of the signal.
4814 *
4815 * @retval 0 The signal was delivered successfully.
4816 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004817 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004818 */
4819
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004820__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004821
Anas Nashif954d5502018-02-25 08:37:28 -06004822/**
4823 * @internal
4824 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004825extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004826
Anas Nashif166f5192018-02-25 08:02:36 -06004827/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004828
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004829/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004830 * @defgroup cpu_idle_apis CPU Idling APIs
4831 * @ingroup kernel_apis
4832 * @{
4833 */
Anas Nashif30c3cff2019-01-22 08:18:13 -05004834/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004835 * @brief Make the CPU idle.
4836 *
4837 * This function makes the CPU idle until an event wakes it up.
4838 *
4839 * In a regular system, the idle thread should be the only thread responsible
4840 * for making the CPU idle and triggering any type of power management.
4841 * However, in some more constrained systems, such as a single-threaded system,
4842 * the only thread would be responsible for this if needed.
4843 *
4844 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004845 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004846 */
Andrew Boie07525a32019-09-21 16:17:23 -07004847static inline void k_cpu_idle(void)
4848{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004849 arch_cpu_idle();
Andrew Boie07525a32019-09-21 16:17:23 -07004850}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004851
4852/**
4853 * @brief Make the CPU idle in an atomic fashion.
4854 *
4855 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4856 * must be done atomically before making the CPU idle.
4857 *
4858 * @param key Interrupt locking key obtained from irq_lock().
4859 *
4860 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004861 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004862 */
Andrew Boie07525a32019-09-21 16:17:23 -07004863static inline void k_cpu_atomic_idle(unsigned int key)
4864{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004865 arch_cpu_atomic_idle(key);
Andrew Boie07525a32019-09-21 16:17:23 -07004866}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004867
Anas Nashif30c3cff2019-01-22 08:18:13 -05004868/**
4869 * @}
4870 */
Anas Nashif954d5502018-02-25 08:37:28 -06004871
4872/**
4873 * @internal
4874 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004875extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004876
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004877#ifdef ARCH_EXCEPT
Ioannis Glaropoulosdf029232019-10-07 11:24:36 +02004878/* This architecture has direct support for triggering a CPU exception */
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004879#define z_except_reason(reason) ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004880#else
4881
Andrew Boiecdb94d62017-04-18 15:22:05 -07004882/* NOTE: This is the implementation for arches that do not implement
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004883 * ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004884 *
4885 * We won't have a real exception frame to determine the PC value when
4886 * the oops occurred, so print file and line number before we jump into
4887 * the fatal error handler.
4888 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004889#define z_except_reason(reason) do { \
Andrew Boiecdb94d62017-04-18 15:22:05 -07004890 printk("@ %s:%d:\n", __FILE__, __LINE__); \
Andrew Boie56236372019-07-15 15:22:29 -07004891 z_fatal_error(reason, NULL); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004892 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004893
4894#endif /* _ARCH__EXCEPT */
4895
4896/**
4897 * @brief Fatally terminate a thread
4898 *
4899 * This should be called when a thread has encountered an unrecoverable
4900 * runtime condition and needs to terminate. What this ultimately
4901 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004902 * will be called will reason code K_ERR_KERNEL_OOPS.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004903 *
4904 * If this is called from ISR context, the default system fatal error handler
4905 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004906 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004907 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004908#define k_oops() z_except_reason(K_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004909
4910/**
4911 * @brief Fatally terminate the system
4912 *
4913 * This should be called when the Zephyr kernel has encountered an
4914 * unrecoverable runtime condition and needs to terminate. What this ultimately
4915 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004916 * will be called will reason code K_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004917 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004918 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004919#define k_panic() z_except_reason(K_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004920
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004921/*
4922 * private APIs that are utilized by one or more public APIs
4923 */
4924
Stephanos Ioannidis2d746042019-10-25 00:08:21 +09004925/**
4926 * @internal
4927 */
4928extern void z_init_thread_base(struct _thread_base *thread_base,
4929 int priority, u32_t initial_state,
4930 unsigned int options);
4931
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004932#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004933/**
4934 * @internal
4935 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004936extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004937#else
Anas Nashif954d5502018-02-25 08:37:28 -06004938/**
4939 * @internal
4940 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004941#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004942#endif
4943
Anas Nashif954d5502018-02-25 08:37:28 -06004944/**
4945 * @internal
4946 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004947extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004948/**
4949 * @internal
4950 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004951extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004952
Andrew Boiedc5d9352017-06-02 12:56:47 -07004953/* arch/cpu.h may declare an architecture or platform-specific macro
4954 * for properly declaring stacks, compatible with MMU/MPU constraints if
4955 * enabled
4956 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004957
4958/**
4959 * @brief Obtain an extern reference to a stack
4960 *
4961 * This macro properly brings the symbol of a thread stack declared
4962 * elsewhere into scope.
4963 *
4964 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004965 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004966 */
4967#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4968
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004969#ifdef ARCH_THREAD_STACK_DEFINE
4970#define K_THREAD_STACK_DEFINE(sym, size) ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004971#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004972 ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4973#define K_THREAD_STACK_LEN(size) ARCH_THREAD_STACK_LEN(size)
4974#define K_THREAD_STACK_MEMBER(sym, size) ARCH_THREAD_STACK_MEMBER(sym, size)
4975#define K_THREAD_STACK_SIZEOF(sym) ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boieb5c68102019-11-21 17:38:17 -08004976#define K_THREAD_STACK_RESERVED ((size_t)ARCH_THREAD_STACK_RESERVED)
Andrew Boie4e5c0932019-04-04 12:05:28 -07004977static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004978{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004979 return ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07004980}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004981#else
4982/**
4983 * @brief Declare a toplevel thread stack memory region
4984 *
4985 * This declares a region of memory suitable for use as a thread's stack.
4986 *
4987 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4988 * 'noinit' section so that it isn't zeroed at boot
4989 *
Andrew Boie507852a2017-07-25 18:47:07 -07004990 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004991 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07004992 * inside needs to be examined, examine thread->stack_info for the associated
4993 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004994 *
4995 * It is legal to precede this definition with the 'static' keyword.
4996 *
4997 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4998 * parameter of k_thread_create(), it may not be the same as the
4999 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
5000 *
Andrew Boiee2d77912018-05-30 09:45:35 -07005001 * Some arches may round the size of the usable stack region up to satisfy
5002 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
5003 * size.
5004 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07005005 * @param sym Thread stack symbol name
5006 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005007 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005008 */
5009#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07005010 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07005011
5012/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05305013 * @brief Calculate size of stacks to be allocated in a stack array
5014 *
5015 * This macro calculates the size to be allocated for the stacks
5016 * inside a stack array. It accepts the indicated "size" as a parameter
5017 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
5018 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
5019 *
5020 * @param size Size of the stack memory region
5021 * @req K-TSTACK-001
5022 */
5023#define K_THREAD_STACK_LEN(size) (size)
5024
5025/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07005026 * @brief Declare a toplevel array of thread stack memory regions
5027 *
5028 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
5029 * definition for additional details and constraints.
5030 *
5031 * This is the generic, historical definition. Align to STACK_ALIGN and put in
5032 * 'noinit' section so that it isn't zeroed at boot
5033 *
5034 * @param sym Thread stack symbol name
5035 * @param nmemb Number of stacks to declare
5036 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005037 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005038 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07005039#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07005040 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05305041 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07005042
5043/**
5044 * @brief Declare an embedded stack memory region
5045 *
5046 * Used for stacks embedded within other data structures. Use is highly
5047 * discouraged but in some cases necessary. For memory protection scenarios,
5048 * it is very important that any RAM preceding this member not be writable
5049 * by threads else a stack overflow will lead to silent corruption. In other
5050 * words, the containing data structure should live in RAM owned by the kernel.
5051 *
5052 * @param sym Thread stack symbol name
5053 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005054 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005055 */
5056#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07005057 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07005058
5059/**
5060 * @brief Return the size in bytes of a stack memory region
5061 *
5062 * Convenience macro for passing the desired stack size to k_thread_create()
5063 * since the underlying implementation may actually create something larger
5064 * (for instance a guard area).
5065 *
Andrew Boiee2d77912018-05-30 09:45:35 -07005066 * The value returned here is not guaranteed to match the 'size' parameter
5067 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005068 *
5069 * @param sym Stack memory symbol
5070 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005071 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005072 */
5073#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
5074
Andrew Boie575abc02019-03-19 10:42:24 -07005075
5076/**
5077 * @brief Indicate how much additional memory is reserved for stack objects
5078 *
5079 * Any given stack declaration may have additional memory in it for guard
5080 * areas or supervisor mode stacks. This macro indicates how much space
5081 * is reserved for this. The memory reserved may not be contiguous within
5082 * the stack object, and does not account for additional space used due to
5083 * enforce alignment.
5084 */
Andrew Boieb5c68102019-11-21 17:38:17 -08005085#define K_THREAD_STACK_RESERVED ((size_t)0U)
Andrew Boie575abc02019-03-19 10:42:24 -07005086
Andrew Boiedc5d9352017-06-02 12:56:47 -07005087/**
5088 * @brief Get a pointer to the physical stack buffer
5089 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07005090 * This macro is deprecated. If a stack buffer needs to be examined, the
5091 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005092 *
5093 * @param sym Declared stack symbol name
5094 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005095 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005096 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07005097static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07005098{
5099 return (char *)sym;
5100}
Andrew Boiedc5d9352017-06-02 12:56:47 -07005101
5102#endif /* _ARCH_DECLARE_STACK */
5103
Chunlin Hane9c97022017-07-07 20:29:30 +08005104/**
5105 * @defgroup mem_domain_apis Memory domain APIs
5106 * @ingroup kernel_apis
5107 * @{
5108 */
5109
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005110/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005111 * @def K_MEM_PARTITION_DEFINE
5112 * @brief Used to declare a memory partition
5113 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005114 */
5115#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
5116#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
5117 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08005118 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005119 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005120#else
5121#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08005122 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005123 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005124#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
5125
Chunlin Hane9c97022017-07-07 20:29:30 +08005126/* memory partition */
5127struct k_mem_partition {
Anas Nashife71293e2019-12-04 20:00:14 -05005128 /** start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005129 uintptr_t start;
Anas Nashife71293e2019-12-04 20:00:14 -05005130 /** size of memory partition */
Andrew Boiea8248212019-11-13 12:10:56 -08005131 size_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005132#if defined(CONFIG_MEMORY_PROTECTION)
Anas Nashife71293e2019-12-04 20:00:14 -05005133 /** attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305134 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005135#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08005136};
5137
Anas Nashife71293e2019-12-04 20:00:14 -05005138/**
5139 * @brief Memory Domain
5140 *
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05305141 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005142struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305143#ifdef CONFIG_USERSPACE
Anas Nashife71293e2019-12-04 20:00:14 -05005144 /** partitions in the domain */
Chunlin Hane9c97022017-07-07 20:29:30 +08005145 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305146#endif /* CONFIG_USERSPACE */
Anas Nashife71293e2019-12-04 20:00:14 -05005147 /** domain q */
Chunlin Hane9c97022017-07-07 20:29:30 +08005148 sys_dlist_t mem_domain_q;
Anas Nashife71293e2019-12-04 20:00:14 -05005149 /** number of partitions in the domain */
Leandro Pereira08de6582018-02-28 14:22:57 -08005150 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08005151};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305152
Chunlin Hane9c97022017-07-07 20:29:30 +08005153
5154/**
5155 * @brief Initialize a memory domain.
5156 *
5157 * Initialize a memory domain with given name and memory partitions.
5158 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005159 * See documentation for k_mem_domain_add_partition() for details about
5160 * partition constraints.
5161 *
Chunlin Hane9c97022017-07-07 20:29:30 +08005162 * @param domain The memory domain to be initialized.
5163 * @param num_parts The number of array items of "parts" parameter.
5164 * @param parts An array of pointers to the memory partitions. Can be NULL
5165 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005166 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005167 */
Leandro Pereira08de6582018-02-28 14:22:57 -08005168extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08005169 struct k_mem_partition *parts[]);
5170/**
5171 * @brief Destroy a memory domain.
5172 *
5173 * Destroy a memory domain.
5174 *
5175 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005176 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005177 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005178extern void k_mem_domain_destroy(struct k_mem_domain *domain);
5179
5180/**
5181 * @brief Add a memory partition into a memory domain.
5182 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005183 * Add a memory partition into a memory domain. Partitions must conform to
5184 * the following constraints:
5185 *
5186 * - Partition bounds must be within system RAM boundaries on MMU-based
5187 * systems.
5188 * - Partitions in the same memory domain may not overlap each other.
5189 * - Partitions must not be defined which expose private kernel
5190 * data structures or kernel objects.
5191 * - The starting address alignment, and the partition size must conform to
5192 * the constraints of the underlying memory management hardware, which
5193 * varies per architecture.
5194 * - Memory domain partitions are only intended to control access to memory
5195 * from user mode threads.
5196 *
5197 * Violating these constraints may lead to CPU exceptions or undefined
5198 * behavior.
Chunlin Hane9c97022017-07-07 20:29:30 +08005199 *
5200 * @param domain The memory domain to be added a memory partition.
5201 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005202 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005203 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005204extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
5205 struct k_mem_partition *part);
5206
5207/**
5208 * @brief Remove a memory partition from a memory domain.
5209 *
5210 * Remove a memory partition from a memory domain.
5211 *
5212 * @param domain The memory domain to be removed a memory partition.
5213 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005214 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005215 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005216extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
5217 struct k_mem_partition *part);
5218
5219/**
5220 * @brief Add a thread into a memory domain.
5221 *
5222 * Add a thread into a memory domain.
5223 *
5224 * @param domain The memory domain that the thread is going to be added into.
5225 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005226 *
5227 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005228 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005229extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
5230 k_tid_t thread);
5231
5232/**
5233 * @brief Remove a thread from its memory domain.
5234 *
5235 * Remove a thread from its memory domain.
5236 *
5237 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005238 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005239 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005240extern void k_mem_domain_remove_thread(k_tid_t thread);
5241
Anas Nashif166f5192018-02-25 08:02:36 -06005242/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08005243
Andrew Boie756f9072017-10-10 16:01:49 -07005244/**
5245 * @brief Emit a character buffer to the console device
5246 *
5247 * @param c String of characters to print
5248 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005249 *
5250 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07005251 */
5252__syscall void k_str_out(char *c, size_t n);
5253
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005254/**
5255 * @brief Disable preservation of floating point context information.
5256 *
5257 * This routine informs the kernel that the specified thread
5258 * will no longer be using the floating point registers.
5259 *
5260 * @warning
5261 * Some architectures apply restrictions on how the disabling of floating
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005262 * point preservation may be requested, see arch_float_disable.
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005263 *
5264 * @warning
5265 * This routine should only be used to disable floating point support for
5266 * a thread that currently has such support enabled.
5267 *
5268 * @param thread ID of thread.
5269 *
5270 * @retval 0 On success.
5271 * @retval -ENOSYS If the floating point disabling is not implemented.
5272 * -EINVAL If the floating point disabling could not be performed.
5273 */
5274__syscall int k_float_disable(struct k_thread *thread);
5275
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005276#ifdef __cplusplus
5277}
5278#endif
5279
Anas Nashif10291a02019-06-25 12:25:12 -04005280#include <debug/tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07005281#include <syscalls/kernel.h>
5282
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05005283#endif /* !_ASMLANGUAGE */
5284
Flavio Ceolin67ca1762018-09-14 10:43:44 -07005285#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */