blob: 251079cc50b4497ffa14ce3626248f80ffda3474 [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
Andrew Boie5bd891d2017-09-27 12:59:28 -0700139/* This enumeration needs to be kept in sync with the lists of kernel objects
140 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
141 * function in kernel/userspace.c
142 */
Andrew Boie945af952017-08-22 13:15:23 -0700143enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700144 K_OBJ_ANY,
145
Leandro Pereirac2003672018-04-04 13:50:32 -0700146 /** @cond
147 * Doxygen should ignore this build-time generated include file
148 * when genrating API documentation. Enumeration values are
149 * generated during build by gen_kobject_list.py. It includes
150 * basic kernel objects (e.g. pipes and mutexes) and driver types.
151 */
152#include <kobj-types-enum.h>
153 /** @endcond
154 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700155
Andrew Boie945af952017-08-22 13:15:23 -0700156 K_OBJ_LAST
157};
Anas Nashif4bcb2942019-01-23 23:06:29 -0500158/**
159 * @defgroup usermode_apis User Mode APIs
160 * @ingroup kernel_apis
161 * @{
162 */
Andrew Boie945af952017-08-22 13:15:23 -0700163
164#ifdef CONFIG_USERSPACE
165/* Table generated by gperf, these objects are retrieved via
Patrik Flykt4344e272019-03-08 14:19:05 -0700166 * z_object_find() */
Andrew Boie945af952017-08-22 13:15:23 -0700167struct _k_object {
168 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700169 u8_t perms[CONFIG_MAX_THREAD_BYTES];
170 u8_t type;
171 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700172 u32_t data;
Andrew Boiedf555242018-05-25 07:28:54 -0700173} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700174
Andrew Boie877f82e2017-10-17 11:20:22 -0700175struct _k_object_assignment {
176 struct k_thread *thread;
177 void * const *objects;
178};
179
180/**
181 * @brief Grant a static thread access to a list of kernel objects
182 *
183 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
184 * a set of kernel objects. These objects do not need to be in an initialized
185 * state. The permissions will be granted when the threads are initialized
186 * in the early boot sequence.
187 *
188 * All arguments beyond the first must be pointers to kernel objects.
189 *
190 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
191 */
192#define K_THREAD_ACCESS_GRANT(name_, ...) \
193 static void * const _CONCAT(_object_list_, name_)[] = \
194 { __VA_ARGS__, NULL }; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400195 static const Z_STRUCT_SECTION_ITERABLE(_k_object_assignment, \
196 _CONCAT(_object_access_, name_)) = \
Andrew Boie877f82e2017-10-17 11:20:22 -0700197 { (&_k_thread_obj_ ## name_), \
198 (_CONCAT(_object_list_, name_)) }
199
Andrew Boie945af952017-08-22 13:15:23 -0700200#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700201#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700202#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie78757072019-07-23 13:29:30 -0700203#define K_OBJ_FLAG_DRIVER BIT(3)
Andrew Boie945af952017-08-22 13:15:23 -0700204
205/**
206 * Lookup a kernel object and init its metadata if it exists
207 *
208 * Calling this on an object will make it usable from userspace.
209 * Intended to be called as the last statement in kernel object init
210 * functions.
211 *
Anas Nashif50e3acd2018-12-08 13:26:18 -0500212 * @param obj Address of the kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700213 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700214void z_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700215#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700216/* LCOV_EXCL_START */
Andrew Boie877f82e2017-10-17 11:20:22 -0700217#define K_THREAD_ACCESS_GRANT(thread, ...)
218
Anas Nashif954d5502018-02-25 08:37:28 -0600219/**
220 * @internal
221 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700222static inline void z_object_init(void *obj)
Andrew Boie743e4682017-10-04 12:25:50 -0700223{
224 ARG_UNUSED(obj);
225}
226
Anas Nashif954d5502018-02-25 08:37:28 -0600227/**
228 * @internal
229 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700230static inline void z_impl_k_object_access_grant(void *object,
Andrew Boie743e4682017-10-04 12:25:50 -0700231 struct k_thread *thread)
232{
233 ARG_UNUSED(object);
234 ARG_UNUSED(thread);
235}
236
Anas Nashif954d5502018-02-25 08:37:28 -0600237/**
238 * @internal
239 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700240static inline void k_object_access_revoke(void *object,
241 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700242{
243 ARG_UNUSED(object);
244 ARG_UNUSED(thread);
245}
246
Andrew Boiee9cfc542018-04-13 13:15:28 -0700247/**
248 * @internal
249 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700250static inline void z_impl_k_object_release(void *object)
Andrew Boiee9cfc542018-04-13 13:15:28 -0700251{
252 ARG_UNUSED(object);
253}
254
Andrew Boie41bab6e2017-10-14 14:42:23 -0700255static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700256{
257 ARG_UNUSED(object);
258}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700259/* LCOV_EXCL_STOP */
Andrew Boie743e4682017-10-04 12:25:50 -0700260#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700261
262/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600263 * Grant a thread access to a kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700264 *
265 * The thread will be granted access to the object if the caller is from
266 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700267 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700268 *
269 * @param object Address of kernel object
270 * @param thread Thread to grant access to the object
271 */
Andrew Boie743e4682017-10-04 12:25:50 -0700272__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700273
Andrew Boiea89bf012017-10-09 14:47:55 -0700274/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600275 * Revoke a thread's access to a kernel object
Andrew Boiea89bf012017-10-09 14:47:55 -0700276 *
277 * The thread will lose access to the object if the caller is from
278 * supervisor mode, or the caller is from user mode AND has permissions
279 * on both the object and the thread whose access is being revoked.
280 *
281 * @param object Address of kernel object
282 * @param thread Thread to remove access to the object
283 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700284void k_object_access_revoke(void *object, struct k_thread *thread);
285
286
287__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700288
289/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600290 * Grant all present and future threads access to an object
Andrew Boie3b5ae802017-10-04 12:10:32 -0700291 *
292 * If the caller is from supervisor mode, or the caller is from user mode and
293 * have sufficient permissions on the object, then that object will have
294 * permissions granted to it for *all* current and future threads running in
295 * the system, effectively becoming a public kernel object.
296 *
297 * Use of this API should be avoided on systems that are running untrusted code
298 * as it is possible for such code to derive the addresses of kernel objects
299 * and perform unwanted operations on them.
300 *
Andrew Boie04caa672017-10-13 13:57:07 -0700301 * It is not possible to revoke permissions on public objects; once public,
302 * any thread may use it.
303 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700304 * @param object Address of kernel object
305 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700306void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700307
Andrew Boie31bdfc02017-11-08 16:38:03 -0800308/**
309 * Allocate a kernel object of a designated type
310 *
311 * This will instantiate at runtime a kernel object of the specified type,
312 * returning a pointer to it. The object will be returned in an uninitialized
313 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700314 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800315 *
316 * Currently, allocation of thread stacks is not supported.
317 *
318 * @param otype Requested kernel object type
319 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
320 * available
321 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700322__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800323
Andrew Boie97bf0012018-04-24 17:01:37 -0700324#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800325/**
326 * Free a kernel object previously allocated with k_object_alloc()
327 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700328 * This will return memory for a kernel object back to resource pool it was
329 * allocated from. Care must be exercised that the object will not be used
330 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800331 *
332 * @param obj Pointer to the kernel object memory address.
333 */
334void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700335#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700336/* LCOV_EXCL_START */
Patrik Flykt4344e272019-03-08 14:19:05 -0700337static inline void *z_impl_k_object_alloc(enum k_objects otype)
Andrew Boie97bf0012018-04-24 17:01:37 -0700338{
Kumar Gala85699f72018-05-17 09:26:03 -0500339 ARG_UNUSED(otype);
340
Andrew Boie97bf0012018-04-24 17:01:37 -0700341 return NULL;
342}
343
344static inline void k_obj_free(void *obj)
345{
346 ARG_UNUSED(obj);
347}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700348/* LCOV_EXCL_STOP */
Andrew Boie31bdfc02017-11-08 16:38:03 -0800349#endif /* CONFIG_DYNAMIC_OBJECTS */
350
Anas Nashif4bcb2942019-01-23 23:06:29 -0500351/** @} */
352
Andrew Boiebca15da2017-10-15 14:17:48 -0700353/* Using typedef deliberately here, this is quite intended to be an opaque
Andrew Boie4e5c0932019-04-04 12:05:28 -0700354 * type.
Andrew Boiebca15da2017-10-15 14:17:48 -0700355 *
356 * The purpose of this data type is to clearly distinguish between the
357 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
358 * buffer which composes the stack data actually used by the underlying
Anas Nashiff2cb20c2019-06-18 14:45:40 -0400359 * thread; they cannot be used interchangeably as some arches precede the
Andrew Boiebca15da2017-10-15 14:17:48 -0700360 * stack buffer region with guard areas that trigger a MPU or MMU fault
361 * if written to.
362 *
363 * APIs that want to work with the buffer inside should continue to use
364 * char *.
365 *
366 * Stacks should always be created with K_THREAD_STACK_DEFINE().
367 */
368struct __packed _k_thread_stack_element {
369 char data;
370};
Daniel Leung7476a6e2019-11-25 13:58:40 -0800371
372/**
373 * @typedef k_thread_stack_t
374 * @brief Typedef of struct _k_thread_stack_element
375 *
376 * @see _k_thread_stack_element
377 */
Andrew Boiebca15da2017-10-15 14:17:48 -0700378
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700379/**
380 * @typedef k_thread_entry_t
381 * @brief Thread entry point function type.
382 *
383 * A thread's entry point function is invoked when the thread starts executing.
384 * Up to 3 argument values can be passed to the function.
385 *
386 * The thread terminates execution permanently if the entry point function
387 * returns. The thread is responsible for releasing any shared resources
388 * it may own (such as mutexes and dynamically allocated memory), prior to
389 * returning.
390 *
391 * @param p1 First argument.
392 * @param p2 Second argument.
393 * @param p3 Third argument.
394 *
395 * @return N/A
396 */
Andrew Boie73abd322017-04-04 13:19:13 -0700397
398#ifdef CONFIG_THREAD_MONITOR
399struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700400 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700401 void *parameter1;
402 void *parameter2;
403 void *parameter3;
404};
405#endif
406
407/* can be used for creating 'dummy' threads, e.g. for pending on objects */
408struct _thread_base {
409
410 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700411 union {
Peter A. Bigot82ad0d22019-01-03 23:49:28 -0600412 sys_dnode_t qnode_dlist;
Andy Ross1acd8c22018-05-03 14:51:49 -0700413 struct rbnode qnode_rb;
414 };
415
Andy Ross1acd8c22018-05-03 14:51:49 -0700416 /* wait queue on which the thread is pended (needed only for
417 * trees, not dumb lists)
418 */
419 _wait_q_t *pended_on;
Andrew Boie73abd322017-04-04 13:19:13 -0700420
421 /* user facing 'thread options'; values defined in include/kernel.h */
422 u8_t user_options;
423
424 /* thread state */
425 u8_t thread_state;
426
427 /*
428 * scheduler lock count and thread priority
429 *
430 * These two fields control the preemptibility of a thread.
431 *
432 * When the scheduler is locked, sched_locked is decremented, which
433 * means that the scheduler is locked for values from 0xff to 0x01. A
434 * thread is coop if its prio is negative, thus 0x80 to 0xff when
435 * looked at the value as unsigned.
436 *
437 * By putting them end-to-end, this means that a thread is
438 * non-preemptible if the bundled value is greater than or equal to
439 * 0x0080.
440 */
441 union {
442 struct {
443#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
444 u8_t sched_locked;
445 s8_t prio;
446#else /* LITTLE and PDP */
447 s8_t prio;
448 u8_t sched_locked;
449#endif
450 };
451 u16_t preempt;
452 };
453
Andy Ross4a2e50f2018-05-15 11:06:25 -0700454#ifdef CONFIG_SCHED_DEADLINE
455 int prio_deadline;
456#endif
457
Andy Ross1acd8c22018-05-03 14:51:49 -0700458 u32_t order_key;
459
Andy Ross2724fd12018-01-29 14:55:20 -0800460#ifdef CONFIG_SMP
461 /* True for the per-CPU idle threads */
462 u8_t is_idle;
463
Andy Ross2724fd12018-01-29 14:55:20 -0800464 /* CPU index on which thread was last run */
465 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700466
467 /* Recursive count of irq_lock() calls */
468 u8_t global_lock_count;
Andy Rossab46b1b2019-01-30 15:00:42 -0800469
470#endif
471
472#ifdef CONFIG_SCHED_CPU_MASK
473 /* "May run on" bits for each CPU */
474 u8_t cpu_mask;
Andy Ross2724fd12018-01-29 14:55:20 -0800475#endif
476
Andrew Boie73abd322017-04-04 13:19:13 -0700477 /* data returned by APIs */
478 void *swap_data;
479
480#ifdef CONFIG_SYS_CLOCK_EXISTS
481 /* this thread's entry in a timeout queue */
482 struct _timeout timeout;
483#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700484};
485
486typedef struct _thread_base _thread_base_t;
487
488#if defined(CONFIG_THREAD_STACK_INFO)
489/* Contains the stack information of a thread */
490struct _thread_stack_info {
Andrew Boie4e5c0932019-04-04 12:05:28 -0700491 /* Stack start - Represents the start address of the thread-writable
492 * stack area.
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700493 */
Nicolas Pitre58d839b2019-05-21 11:32:21 -0400494 uintptr_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700495
496 /* Stack Size - Thread writable stack buffer size. Represents
497 * the size of the actual area, starting from the start member,
498 * that should be writable by the thread
499 */
Andrew Boie73abd322017-04-04 13:19:13 -0700500 u32_t size;
501};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700502
503typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700504#endif /* CONFIG_THREAD_STACK_INFO */
505
Chunlin Hane9c97022017-07-07 20:29:30 +0800506#if defined(CONFIG_USERSPACE)
507struct _mem_domain_info {
508 /* memory domain queue node */
509 sys_dnode_t mem_domain_q_node;
510 /* memory domain of the thread */
511 struct k_mem_domain *mem_domain;
512};
513
514#endif /* CONFIG_USERSPACE */
515
Daniel Leungfc182432018-08-16 15:42:28 -0700516#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
517struct _thread_userspace_local_data {
518 int errno_var;
519};
520#endif
521
Anas Nashifce78d162018-05-24 12:43:11 -0500522/**
523 * @ingroup thread_apis
524 * Thread Structure
525 */
Andrew Boie73abd322017-04-04 13:19:13 -0700526struct k_thread {
527
528 struct _thread_base base;
529
Anas Nashifce78d162018-05-24 12:43:11 -0500530 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700531 struct _callee_saved callee_saved;
532
Anas Nashifce78d162018-05-24 12:43:11 -0500533 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700534 void *init_data;
535
Anas Nashifce78d162018-05-24 12:43:11 -0500536 /**
537 * abort function
538 * @req K-THREAD-002
539 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700540 void (*fn_abort)(void);
541
542#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500543 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700544 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700545
Anas Nashifce78d162018-05-24 12:43:11 -0500546 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700547 struct k_thread *next_thread;
548#endif
549
Anas Nashif57554052018-03-03 02:31:05 -0600550#if defined(CONFIG_THREAD_NAME)
551 /* Thread name */
Andrew Boie38129ce2019-06-25 08:54:37 -0700552 char name[CONFIG_THREAD_MAX_NAME_LEN];
Anas Nashif57554052018-03-03 02:31:05 -0600553#endif
554
Andrew Boie73abd322017-04-04 13:19:13 -0700555#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500556 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700557 void *custom_data;
558#endif
559
Daniel Leungfc182432018-08-16 15:42:28 -0700560#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
561 struct _thread_userspace_local_data *userspace_local_data;
562#endif
563
Andrew Boie73abd322017-04-04 13:19:13 -0700564#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700565#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500566 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700567 int errno_var;
568#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700569#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700570
571#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500572 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700573 struct _thread_stack_info stack_info;
574#endif /* CONFIG_THREAD_STACK_INFO */
575
Chunlin Hane9c97022017-07-07 20:29:30 +0800576#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500577 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800578 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500579 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700580 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800581#endif /* CONFIG_USERSPACE */
582
Andy Ross042d8ec2017-12-09 08:37:20 -0800583#if defined(CONFIG_USE_SWITCH)
584 /* When using __switch() a few previously arch-specific items
585 * become part of the core OS
586 */
587
Patrik Flykt4344e272019-03-08 14:19:05 -0700588 /** z_swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800589 int swap_retval;
590
Andrew Boie4f77c2a2019-11-07 12:43:29 -0800591 /** Context handle returned via arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800592 void *switch_handle;
593#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500594 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700595 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800596
Anas Nashifce78d162018-05-24 12:43:11 -0500597 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700598 struct _thread_arch arch;
599};
600
601typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400602typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400603
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400604enum execution_context_types {
605 K_ISR = 0,
606 K_COOP_THREAD,
607 K_PREEMPT_THREAD,
608};
609
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400610/**
Anas Nashif4bcb2942019-01-23 23:06:29 -0500611 * @addtogroup thread_apis
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100612 * @{
613 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530614typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
615 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100616
617/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530618 * @brief Iterate over all the threads in the system.
619 *
620 * This routine iterates over all the threads in the system and
621 * calls the user_cb function for each thread.
622 *
623 * @param user_cb Pointer to the user callback function.
624 * @param user_data Pointer to user data.
625 *
626 * @note CONFIG_THREAD_MONITOR must be set for this function
627 * to be effective. Also this API uses irq_lock to protect the
628 * _kernel.threads list which means creation of new threads and
629 * terminations of existing threads are blocked until this
630 * API returns.
631 *
632 * @return N/A
633 */
634extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
635
Anas Nashif166f5192018-02-25 08:02:36 -0600636/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100637
638/**
Allan Stephensc98da842016-11-11 15:45:03 -0500639 * @defgroup thread_apis Thread APIs
640 * @ingroup kernel_apis
641 * @{
642 */
643
Benjamin Walshed240f22017-01-22 13:05:08 -0500644#endif /* !_ASMLANGUAGE */
645
646
647/*
648 * Thread user options. May be needed by assembly code. Common part uses low
649 * bits, arch-specific use high bits.
650 */
651
Anas Nashifa541e932018-05-24 11:19:16 -0500652/**
653 * @brief system thread that must not abort
654 * @req K-THREAD-000
655 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700656#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500657
658#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500659/**
660 * @brief thread uses floating point registers
661 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700662#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500663#endif
664
Anas Nashifa541e932018-05-24 11:19:16 -0500665/**
666 * @brief user mode thread
667 *
668 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700669 * has additional restrictions
670 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700671#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700672
Anas Nashifa541e932018-05-24 11:19:16 -0500673/**
674 * @brief Inherit Permissions
675 *
676 * @details
677 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700678 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
679 * is not enabled.
680 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700681#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700682
Benjamin Walshed240f22017-01-22 13:05:08 -0500683#ifdef CONFIG_X86
684/* x86 Bitmask definitions for threads user options */
685
686#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
687/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700688#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500689#endif
690#endif
691
692/* end - thread options */
693
694#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400695/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700696 * @brief Create a thread.
697 *
698 * This routine initializes a thread, then schedules it for execution.
699 *
700 * The new thread may be scheduled for immediate execution or a delayed start.
701 * If the newly spawned thread does not have a delayed start the kernel
702 * scheduler may preempt the current thread to allow the new thread to
703 * execute.
704 *
705 * Thread options are architecture-specific, and can include K_ESSENTIAL,
706 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
707 * them using "|" (the logical OR operator).
708 *
709 * Historically, users often would use the beginning of the stack memory region
710 * to store the struct k_thread data, although corruption will occur if the
711 * stack overflows this region and stack protection features may not detect this
712 * situation.
713 *
714 * @param new_thread Pointer to uninitialized struct k_thread
715 * @param stack Pointer to the stack space.
716 * @param stack_size Stack size in bytes.
717 * @param entry Thread entry function.
718 * @param p1 1st entry point parameter.
719 * @param p2 2nd entry point parameter.
720 * @param p3 3rd entry point parameter.
721 * @param prio Thread priority.
722 * @param options Thread options.
723 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
724 *
725 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400726 *
727 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700728 */
Andrew Boie662c3452017-10-02 10:51:18 -0700729__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700730 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700731 size_t stack_size,
732 k_thread_entry_t entry,
733 void *p1, void *p2, void *p3,
734 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700735
Andrew Boie3f091b52017-08-30 14:34:14 -0700736/**
737 * @brief Drop a thread's privileges permanently to user mode
738 *
739 * @param entry Function to start executing from
740 * @param p1 1st entry point parameter
741 * @param p2 2nd entry point parameter
742 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400743 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700744 */
745extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
746 void *p1, void *p2,
747 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700748
Andrew Boied26cf2d2017-03-30 13:07:02 -0700749/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530750 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700751 *
752 * This is a convenience function. For the provided thread, grant access to
753 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700754 *
755 * The thread object must be initialized (i.e. running). The objects don't
756 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530757 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700758 *
759 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530760 * @param ... list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400761 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700762 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530763#define k_thread_access_grant(thread, ...) \
764 FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700765
766/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700767 * @brief Assign a resource memory pool to a thread
768 *
769 * By default, threads have no resource pool assigned unless their parent
770 * thread has a resource pool, in which case it is inherited. Multiple
771 * threads may be assigned to the same memory pool.
772 *
773 * Changing a thread's resource pool will not migrate allocations from the
774 * previous pool.
775 *
776 * @param thread Target thread to assign a memory pool for resource requests,
777 * or NULL if the thread should no longer have a memory pool.
778 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400779 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700780 */
781static inline void k_thread_resource_pool_assign(struct k_thread *thread,
782 struct k_mem_pool *pool)
783{
784 thread->resource_pool = pool;
785}
786
787#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
788/**
789 * @brief Assign the system heap as a thread's resource pool
790 *
791 * Similar to k_thread_resource_pool_assign(), but the thread will use
792 * the kernel heap to draw memory.
793 *
794 * Use with caution, as a malicious thread could perform DoS attacks on the
795 * kernel heap.
796 *
797 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400798 *
799 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700800 */
801void k_thread_system_pool_assign(struct k_thread *thread);
802#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
803
804/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500805 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400806 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700807 * This routine puts the current thread to sleep for @a duration milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400808 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700809 * @param ms Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200811 * @return Zero if the requested time has elapsed or the number of milliseconds
812 * left to sleep, if thread was woken up by \ref k_wakeup call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400813 */
Charles E. Yousea5678312019-05-09 16:46:46 -0700814__syscall s32_t k_sleep(s32_t ms);
815
816/**
817 * @brief Put the current thread to sleep with microsecond resolution.
818 *
819 * This function is unlikely to work as expected without kernel tuning.
820 * In particular, because the lower bound on the duration of a sleep is
821 * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted
822 * to achieve the resolution desired. The implications of doing this must
823 * be understood before attempting to use k_usleep(). Use with caution.
824 *
825 * @param us Number of microseconds to sleep.
826 *
827 * @return Zero if the requested time has elapsed or the number of microseconds
828 * left to sleep, if thread was woken up by \ref k_wakeup call.
829 */
830__syscall s32_t k_usleep(s32_t us);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400831
832/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500833 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400834 *
835 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500836 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400837 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838 * @return N/A
839 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800840__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400841
842/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500843 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500845 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400846 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500847 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848 *
849 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400850 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 */
Andrew Boie468190a2017-09-29 14:00:48 -0700852__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400853
854/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500855 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500857 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400858 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500859 * If @a thread is not currently sleeping, the routine has no effect.
860 *
861 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400862 *
863 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400864 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865 */
Andrew Boie468190a2017-09-29 14:00:48 -0700866__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400867
868/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500869 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500871 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400872 *
873 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400874 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700875__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400876
877/**
Allan Stephensc98da842016-11-11 15:45:03 -0500878 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500880 * This routine permanently stops execution of @a thread. The thread is taken
881 * off all kernel queues it is part of (i.e. the ready queue, the timeout
882 * queue, or a kernel object wait queue). However, any kernel resources the
883 * thread might currently own (such as mutexes or memory blocks) are not
884 * released. It is the responsibility of the caller of this routine to ensure
885 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500887 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400888 *
889 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400890 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400891 */
Andrew Boie468190a2017-09-29 14:00:48 -0700892__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400893
Andrew Boie7d627c52017-08-30 11:01:56 -0700894
895/**
896 * @brief Start an inactive thread
897 *
898 * If a thread was created with K_FOREVER in the delay parameter, it will
899 * not be added to the scheduling queue until this function is called
900 * on it.
901 *
902 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400903 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700904 */
Andrew Boie468190a2017-09-29 14:00:48 -0700905__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700906
Allan Stephensc98da842016-11-11 15:45:03 -0500907/**
908 * @cond INTERNAL_HIDDEN
909 */
910
Benjamin Walshd211a522016-12-06 11:44:01 -0500911/* timeout has timed out and is not on _timeout_q anymore */
912#define _EXPIRED (-2)
913
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400914struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700915 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700916 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400917 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700918 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500919 void *init_p1;
920 void *init_p2;
921 void *init_p3;
922 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500923 u32_t init_options;
924 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500925 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600926 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400927};
928
Andrew Boied26cf2d2017-03-30 13:07:02 -0700929#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400930 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600931 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500932 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700933 .init_thread = (thread), \
934 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500935 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700936 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400937 .init_p1 = (void *)p1, \
938 .init_p2 = (void *)p2, \
939 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500940 .init_prio = (prio), \
941 .init_options = (options), \
942 .init_delay = (delay), \
943 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600944 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400945 }
946
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400947/**
Allan Stephensc98da842016-11-11 15:45:03 -0500948 * INTERNAL_HIDDEN @endcond
949 */
950
951/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500952 * @brief Statically define and initialize a thread.
953 *
954 * The thread may be scheduled for immediate execution or a delayed start.
955 *
956 * Thread options are architecture-specific, and can include K_ESSENTIAL,
957 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
958 * them using "|" (the logical OR operator).
959 *
960 * The ID of the thread can be accessed using:
961 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500962 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500963 *
964 * @param name Name of the thread.
965 * @param stack_size Stack size in bytes.
966 * @param entry Thread entry function.
967 * @param p1 1st entry point parameter.
968 * @param p2 2nd entry point parameter.
969 * @param p3 3rd entry point parameter.
970 * @param prio Thread priority.
971 * @param options Thread options.
972 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400973 *
Anas Nashif47420d02018-05-24 14:20:56 -0400974 * @req K-THREAD-010
975 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400976 * @internal It has been observed that the x86 compiler by default aligns
977 * these _static_thread_data structures to 32-byte boundaries, thereby
978 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400979 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400980 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500981#define K_THREAD_DEFINE(name, stack_size, \
982 entry, p1, p2, p3, \
983 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700984 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400985 struct k_thread _k_thread_obj_##name; \
986 Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\
Andrew Boied26cf2d2017-03-30 13:07:02 -0700987 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
988 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500989 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -0600990 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700991 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400992
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400993/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500994 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500996 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400997 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500998 * @param thread ID of thread whose priority is needed.
999 *
1000 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -04001001 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001002 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001003__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001004
1005/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001006 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001008 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001009 *
1010 * Rescheduling can occur immediately depending on the priority @a thread is
1011 * set to:
1012 *
1013 * - If its priority is raised above the priority of the caller of this
1014 * function, and the caller is preemptible, @a thread will be scheduled in.
1015 *
1016 * - If the caller operates on itself, it lowers its priority below that of
1017 * other threads in the system, and the caller is preemptible, the thread of
1018 * highest priority will be scheduled in.
1019 *
1020 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1021 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1022 * highest priority.
1023 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001024 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001025 * @param prio New priority.
1026 *
1027 * @warning Changing the priority of a thread currently involved in mutex
1028 * priority inheritance may result in undefined behavior.
1029 *
1030 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001031 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001032 */
Andrew Boie468190a2017-09-29 14:00:48 -07001033__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001034
Andy Ross4a2e50f2018-05-15 11:06:25 -07001035
1036#ifdef CONFIG_SCHED_DEADLINE
1037/**
1038 * @brief Set deadline expiration time for scheduler
1039 *
1040 * This sets the "deadline" expiration as a time delta from the
1041 * current time, in the same units used by k_cycle_get_32(). The
1042 * scheduler (when deadline scheduling is enabled) will choose the
1043 * next expiring thread when selecting between threads at the same
1044 * static priority. Threads at different priorities will be scheduled
1045 * according to their static priority.
1046 *
1047 * @note Deadlines that are negative (i.e. in the past) are still seen
1048 * as higher priority than others, even if the thread has "finished"
1049 * its work. If you don't want it scheduled anymore, you have to
1050 * reset the deadline into the future, block/pend the thread, or
1051 * modify its priority with k_thread_priority_set().
1052 *
1053 * @note Despite the API naming, the scheduler makes no guarantees the
1054 * the thread WILL be scheduled within that deadline, nor does it take
1055 * extra metadata (like e.g. the "runtime" and "period" parameters in
1056 * Linux sched_setattr()) that allows the kernel to validate the
1057 * scheduling for achievability. Such features could be implemented
1058 * above this call, which is simply input to the priority selection
1059 * logic.
1060 *
Anas Nashif240c5162019-06-10 12:25:50 -04001061 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001062 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001063 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1064 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001065 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001066 *
Andy Ross4a2e50f2018-05-15 11:06:25 -07001067 * @param thread A thread on which to set the deadline
1068 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001069 *
1070 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001071 */
1072__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1073#endif
1074
Andy Rossab46b1b2019-01-30 15:00:42 -08001075#ifdef CONFIG_SCHED_CPU_MASK
1076/**
1077 * @brief Sets all CPU enable masks to zero
1078 *
1079 * After this returns, the thread will no longer be schedulable on any
1080 * CPUs. The thread must not be currently runnable.
1081 *
Anas Nashif240c5162019-06-10 12:25:50 -04001082 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001083 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001084 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1085 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001086 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001087 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001088 * @param thread Thread to operate upon
1089 * @return Zero on success, otherwise error code
1090 */
1091int k_thread_cpu_mask_clear(k_tid_t thread);
1092
1093/**
1094 * @brief Sets all CPU enable masks to one
1095 *
1096 * After this returns, the thread will be schedulable on any CPU. The
1097 * thread must not be currently runnable.
1098 *
Anas Nashif240c5162019-06-10 12:25:50 -04001099 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001100 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001101 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1102 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001103 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001104 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001105 * @param thread Thread to operate upon
1106 * @return Zero on success, otherwise error code
1107 */
1108int k_thread_cpu_mask_enable_all(k_tid_t thread);
1109
1110/**
1111 * @brief Enable thread to run on specified CPU
1112 *
1113 * The thread must not be currently runnable.
1114 *
Anas Nashif240c5162019-06-10 12:25:50 -04001115 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001116 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001117 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1118 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001119 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001120 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001121 * @param thread Thread to operate upon
1122 * @param cpu CPU index
1123 * @return Zero on success, otherwise error code
1124 */
1125int k_thread_cpu_mask_enable(k_tid_t thread, int cpu);
1126
1127/**
1128 * @brief Prevent thread to run on specified CPU
1129 *
1130 * The thread must not be currently runnable.
1131 *
Anas Nashif240c5162019-06-10 12:25:50 -04001132 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001133 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001134 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1135 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001136 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001137 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001138 * @param thread Thread to operate upon
1139 * @param cpu CPU index
1140 * @return Zero on success, otherwise error code
1141 */
1142int k_thread_cpu_mask_disable(k_tid_t thread, int cpu);
1143#endif
1144
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001145/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001146 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001147 *
Andy Ross50d09422019-11-19 11:20:07 -08001148 * This routine prevents the kernel scheduler from making @a thread
1149 * the current thread. All other internal operations on @a thread are
1150 * still performed; for example, kernel objects it is waiting on are
1151 * still handed to it. Note that any existing timeouts
1152 * (e.g. k_sleep(), or a timeout argument to k_sem_take() et. al.)
1153 * will be canceled. On resume, the thread will begin running
1154 * immediately and return from the blocked call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001155 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001156 * If @a thread is already suspended, the routine has no effect.
1157 *
1158 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001159 *
1160 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001161 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001162 */
Andrew Boie468190a2017-09-29 14:00:48 -07001163__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001164
1165/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001166 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001168 * This routine allows the kernel scheduler to make @a thread the current
1169 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001170 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001171 * If @a thread is not currently suspended, the routine has no effect.
1172 *
1173 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001174 *
1175 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001176 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001177 */
Andrew Boie468190a2017-09-29 14:00:48 -07001178__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001179
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001180/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001181 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001183 * This routine specifies how the scheduler will perform time slicing of
1184 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001185 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001186 * To enable time slicing, @a slice must be non-zero. The scheduler
1187 * ensures that no thread runs for more than the specified time limit
1188 * before other threads of that priority are given a chance to execute.
1189 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001190 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001191 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001192 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001193 * execute. Once the scheduler selects a thread for execution, there is no
1194 * minimum guaranteed time the thread will execute before threads of greater or
1195 * equal priority are scheduled.
1196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001197 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001198 * for execution, this routine has no effect; the thread is immediately
1199 * rescheduled after the slice period expires.
1200 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001201 * To disable timeslicing, set both @a slice and @a prio to zero.
1202 *
1203 * @param slice Maximum time slice length (in milliseconds).
1204 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001205 *
1206 * @return N/A
1207 */
Kumar Galacc334c72017-04-21 10:55:34 -05001208extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001209
Anas Nashif166f5192018-02-25 08:02:36 -06001210/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001211
1212/**
1213 * @addtogroup isr_apis
1214 * @{
1215 */
1216
1217/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001218 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001219 *
Allan Stephensc98da842016-11-11 15:45:03 -05001220 * This routine allows the caller to customize its actions, depending on
1221 * whether it is a thread or an ISR.
1222 *
1223 * @note Can be called by ISRs.
1224 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001225 * @return false if invoked by a thread.
1226 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001227 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001228extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001229
Benjamin Walsh445830d2016-11-10 15:54:27 -05001230/**
1231 * @brief Determine if code is running in a preemptible thread.
1232 *
Allan Stephensc98da842016-11-11 15:45:03 -05001233 * This routine allows the caller to customize its actions, depending on
1234 * whether it can be preempted by another thread. The routine returns a 'true'
1235 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001236 *
Allan Stephensc98da842016-11-11 15:45:03 -05001237 * - The code is running in a thread, not at ISR.
1238 * - The thread's priority is in the preemptible range.
1239 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001240 *
Allan Stephensc98da842016-11-11 15:45:03 -05001241 * @note Can be called by ISRs.
1242 *
1243 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001244 * @return Non-zero if invoked by a preemptible thread.
1245 */
Andrew Boie468190a2017-09-29 14:00:48 -07001246__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001247
Allan Stephensc98da842016-11-11 15:45:03 -05001248/**
Anas Nashif166f5192018-02-25 08:02:36 -06001249 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001250 */
1251
1252/**
1253 * @addtogroup thread_apis
1254 * @{
1255 */
1256
1257/**
1258 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001259 *
Allan Stephensc98da842016-11-11 15:45:03 -05001260 * This routine prevents the current thread from being preempted by another
1261 * thread by instructing the scheduler to treat it as a cooperative thread.
1262 * If the thread subsequently performs an operation that makes it unready,
1263 * it will be context switched out in the normal manner. When the thread
1264 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001265 *
Allan Stephensc98da842016-11-11 15:45:03 -05001266 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001267 *
Allan Stephensc98da842016-11-11 15:45:03 -05001268 * @note k_sched_lock() and k_sched_unlock() should normally be used
1269 * when the operation being performed can be safely interrupted by ISRs.
1270 * However, if the amount of processing involved is very small, better
1271 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001272 *
1273 * @return N/A
1274 */
1275extern void k_sched_lock(void);
1276
Allan Stephensc98da842016-11-11 15:45:03 -05001277/**
1278 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001279 *
Allan Stephensc98da842016-11-11 15:45:03 -05001280 * This routine reverses the effect of a previous call to k_sched_lock().
1281 * A thread must call the routine once for each time it called k_sched_lock()
1282 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001283 *
1284 * @return N/A
1285 */
1286extern void k_sched_unlock(void);
1287
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001288/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001289 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001290 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001291 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001293 * Custom data is not used by the kernel itself, and is freely available
1294 * for a thread to use as it sees fit. It can be used as a framework
1295 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001297 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001298 *
1299 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001300 *
1301 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001302 */
Andrew Boie468190a2017-09-29 14:00:48 -07001303__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001304
1305/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001306 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001307 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001308 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001309 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001310 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001311 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001312 */
Andrew Boie468190a2017-09-29 14:00:48 -07001313__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001314
1315/**
Anas Nashif57554052018-03-03 02:31:05 -06001316 * @brief Set current thread name
1317 *
1318 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1319 * tracing and debugging.
1320 *
Andrew Boie38129ce2019-06-25 08:54:37 -07001321 * @param thread_id Thread to set name, or NULL to set the current thread
1322 * @param value Name string
1323 * @retval 0 on success
1324 * @retval -EFAULT Memory access error with supplied string
1325 * @retval -ENOSYS Thread name configuration option not enabled
1326 * @retval -EINVAL Thread name too long
Anas Nashif57554052018-03-03 02:31:05 -06001327 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001328__syscall int k_thread_name_set(k_tid_t thread_id, const char *value);
Anas Nashif57554052018-03-03 02:31:05 -06001329
1330/**
1331 * @brief Get thread name
1332 *
1333 * Get the name of a thread
1334 *
1335 * @param thread_id Thread ID
Andrew Boie38129ce2019-06-25 08:54:37 -07001336 * @retval Thread name, or NULL if configuration not enabled
Anas Nashif57554052018-03-03 02:31:05 -06001337 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001338const char *k_thread_name_get(k_tid_t thread_id);
1339
1340/**
1341 * @brief Copy the thread name into a supplied buffer
1342 *
1343 * @param thread_id Thread to obtain name information
1344 * @param buf Destination buffer
David B. Kinder73896c02019-10-28 16:27:57 -07001345 * @param size Destination buffer size
Andrew Boie38129ce2019-06-25 08:54:37 -07001346 * @retval -ENOSPC Destination buffer too small
1347 * @retval -EFAULT Memory access error
1348 * @retval -ENOSYS Thread name feature not enabled
1349 * @retval 0 Success
1350 */
1351__syscall int k_thread_name_copy(k_tid_t thread_id, char *buf,
1352 size_t size);
Anas Nashif57554052018-03-03 02:31:05 -06001353
1354/**
Pavlo Hamov8076c802019-07-31 12:43:54 +03001355 * @brief Get thread state string
1356 *
1357 * Get the human friendly thread state string
1358 *
1359 * @param thread_id Thread ID
1360 * @retval Thread state string, empty if no state flag is set
1361 */
1362const char *k_thread_state_str(k_tid_t thread_id);
1363
1364/**
Andy Rosscfe62032018-09-29 07:34:55 -07001365 * @}
1366 */
1367
1368/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001369 * @addtogroup clock_apis
1370 * @{
1371 */
1372
1373/**
1374 * @brief Generate null timeout delay.
1375 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001376 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001377 * not to wait if the requested operation cannot be performed immediately.
1378 *
1379 * @return Timeout delay value.
1380 */
1381#define K_NO_WAIT 0
1382
1383/**
1384 * @brief Generate timeout delay from milliseconds.
1385 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001386 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001387 * to wait up to @a ms milliseconds to perform the requested operation.
1388 *
1389 * @param ms Duration in milliseconds.
1390 *
1391 * @return Timeout delay value.
1392 */
Johan Hedberg14471692016-11-13 10:52:15 +02001393#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001394
1395/**
1396 * @brief Generate timeout delay from seconds.
1397 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001398 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001399 * to wait up to @a s seconds to perform the requested operation.
1400 *
1401 * @param s Duration in seconds.
1402 *
1403 * @return Timeout delay value.
1404 */
Johan Hedberg14471692016-11-13 10:52:15 +02001405#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001406
1407/**
1408 * @brief Generate timeout delay from minutes.
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001409
1410 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001411 * to wait up to @a m minutes to perform the requested operation.
1412 *
1413 * @param m Duration in minutes.
1414 *
1415 * @return Timeout delay value.
1416 */
Johan Hedberg14471692016-11-13 10:52:15 +02001417#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001418
1419/**
1420 * @brief Generate timeout delay from hours.
1421 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001422 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001423 * to wait up to @a h hours to perform the requested operation.
1424 *
1425 * @param h Duration in hours.
1426 *
1427 * @return Timeout delay value.
1428 */
Johan Hedberg14471692016-11-13 10:52:15 +02001429#define K_HOURS(h) K_MINUTES((h) * 60)
1430
Allan Stephensc98da842016-11-11 15:45:03 -05001431/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001432 * @brief Generate infinite timeout delay.
1433 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001434 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001435 * to wait as long as necessary to perform the requested operation.
1436 *
1437 * @return Timeout delay value.
1438 */
1439#define K_FOREVER (-1)
1440
1441/**
Anas Nashif166f5192018-02-25 08:02:36 -06001442 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001443 */
1444
1445/**
Allan Stephensc98da842016-11-11 15:45:03 -05001446 * @cond INTERNAL_HIDDEN
1447 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001448
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001449struct k_timer {
1450 /*
1451 * _timeout structure must be first here if we want to use
1452 * dynamic timer allocation. timeout.node is used in the double-linked
1453 * list of free timers
1454 */
1455 struct _timeout timeout;
1456
Allan Stephens45bfa372016-10-12 12:39:42 -05001457 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001458 _wait_q_t wait_q;
1459
1460 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001461 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001462
1463 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001464 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001465
1466 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001467 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001468
Allan Stephens45bfa372016-10-12 12:39:42 -05001469 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001470 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001471
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001472 /* user-specific data, also used to support legacy features */
1473 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001474
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001475 _OBJECT_TRACING_NEXT_PTR(k_timer)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08001476 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001477};
1478
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001479#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001480 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001481 .timeout = { \
1482 .node = {},\
1483 .dticks = 0, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001484 .fn = z_timer_expiration_handler \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001485 }, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001486 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001487 .expiry_fn = expiry, \
1488 .stop_fn = stop, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001489 .period = 0, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001490 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001491 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001492 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001493 }
1494
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05001495#define K_TIMER_INITIALIZER __DEPRECATED_MACRO Z_TIMER_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001496
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001497/**
Allan Stephensc98da842016-11-11 15:45:03 -05001498 * INTERNAL_HIDDEN @endcond
1499 */
1500
1501/**
1502 * @defgroup timer_apis Timer APIs
1503 * @ingroup kernel_apis
1504 * @{
1505 */
1506
1507/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001508 * @typedef k_timer_expiry_t
1509 * @brief Timer expiry function type.
1510 *
1511 * A timer's expiry function is executed by the system clock interrupt handler
1512 * each time the timer expires. The expiry function is optional, and is only
1513 * invoked if the timer has been initialized with one.
1514 *
1515 * @param timer Address of timer.
1516 *
1517 * @return N/A
1518 */
1519typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1520
1521/**
1522 * @typedef k_timer_stop_t
1523 * @brief Timer stop function type.
1524 *
1525 * A timer's stop function is executed if the timer is stopped prematurely.
1526 * The function runs in the context of the thread that stops the timer.
1527 * The stop function is optional, and is only invoked if the timer has been
1528 * initialized with one.
1529 *
1530 * @param timer Address of timer.
1531 *
1532 * @return N/A
1533 */
1534typedef void (*k_timer_stop_t)(struct k_timer *timer);
1535
1536/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001537 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001539 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001540 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001541 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001542 *
1543 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001544 * @param expiry_fn Function to invoke each time the timer expires.
1545 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001546 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001547#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001548 Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001549 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001550
Allan Stephens45bfa372016-10-12 12:39:42 -05001551/**
1552 * @brief Initialize a timer.
1553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001554 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001555 *
1556 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001557 * @param expiry_fn Function to invoke each time the timer expires.
1558 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001559 *
1560 * @return N/A
1561 */
1562extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001563 k_timer_expiry_t expiry_fn,
1564 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001565
Allan Stephens45bfa372016-10-12 12:39:42 -05001566/**
1567 * @brief Start a timer.
1568 *
1569 * This routine starts a timer, and resets its status to zero. The timer
1570 * begins counting down using the specified duration and period values.
1571 *
1572 * Attempting to start a timer that is already running is permitted.
1573 * The timer's status is reset to zero and the timer begins counting down
1574 * using the new duration and period values.
1575 *
1576 * @param timer Address of timer.
1577 * @param duration Initial timer duration (in milliseconds).
1578 * @param period Timer period (in milliseconds).
1579 *
1580 * @return N/A
1581 */
Andrew Boiea354d492017-09-29 16:22:28 -07001582__syscall void k_timer_start(struct k_timer *timer,
1583 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001584
1585/**
1586 * @brief Stop a timer.
1587 *
1588 * This routine stops a running timer prematurely. The timer's stop function,
1589 * if one exists, is invoked by the caller.
1590 *
1591 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001592 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001593 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001594 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1595 * if @a k_timer_stop is to be called from ISRs.
1596 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001597 * @param timer Address of timer.
1598 *
1599 * @return N/A
1600 */
Andrew Boiea354d492017-09-29 16:22:28 -07001601__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001602
1603/**
1604 * @brief Read timer status.
1605 *
1606 * This routine reads the timer's status, which indicates the number of times
1607 * it has expired since its status was last read.
1608 *
1609 * Calling this routine resets the timer's status to zero.
1610 *
1611 * @param timer Address of timer.
1612 *
1613 * @return Timer status.
1614 */
Andrew Boiea354d492017-09-29 16:22:28 -07001615__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001616
1617/**
1618 * @brief Synchronize thread to timer expiration.
1619 *
1620 * This routine blocks the calling thread until the timer's status is non-zero
1621 * (indicating that it has expired at least once since it was last examined)
1622 * or the timer is stopped. If the timer status is already non-zero,
1623 * or the timer is already stopped, the caller continues without waiting.
1624 *
1625 * Calling this routine resets the timer's status to zero.
1626 *
1627 * This routine must not be used by interrupt handlers, since they are not
1628 * allowed to block.
1629 *
1630 * @param timer Address of timer.
1631 *
1632 * @return Timer status.
1633 */
Andrew Boiea354d492017-09-29 16:22:28 -07001634__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001635
Andy Ross52e444b2018-09-28 09:06:37 -07001636extern s32_t z_timeout_remaining(struct _timeout *timeout);
1637
Allan Stephens45bfa372016-10-12 12:39:42 -05001638/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001639 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001640 *
1641 * This routine computes the (approximate) time remaining before a running
1642 * timer next expires. If the timer is not running, it returns zero.
1643 *
1644 * @param timer Address of timer.
1645 *
1646 * @return Remaining time (in milliseconds).
1647 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001648__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001649
Patrik Flykt4344e272019-03-08 14:19:05 -07001650static inline u32_t z_impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001651{
Charles E. Youse0ad40222019-03-01 10:51:04 -08001652 const s32_t ticks = z_timeout_remaining(&timer->timeout);
Andy Ross88924062019-10-03 11:43:10 -07001653 return (ticks > 0) ? (u32_t)k_ticks_to_ms_floor64(ticks) : 0U;
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001654}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001655
Allan Stephensc98da842016-11-11 15:45:03 -05001656/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001657 * @brief Associate user-specific data with a timer.
1658 *
1659 * This routine records the @a user_data with the @a timer, to be retrieved
1660 * later.
1661 *
1662 * It can be used e.g. in a timer handler shared across multiple subsystems to
1663 * retrieve data specific to the subsystem this timer is associated with.
1664 *
1665 * @param timer Address of timer.
1666 * @param user_data User data to associate with the timer.
1667 *
1668 * @return N/A
1669 */
Andrew Boiea354d492017-09-29 16:22:28 -07001670__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1671
Anas Nashif954d5502018-02-25 08:37:28 -06001672/**
1673 * @internal
1674 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001675static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
Andrew Boiea354d492017-09-29 16:22:28 -07001676 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001677{
1678 timer->user_data = user_data;
1679}
1680
1681/**
1682 * @brief Retrieve the user-specific data from a timer.
1683 *
1684 * @param timer Address of timer.
1685 *
1686 * @return The user data.
1687 */
Andrew Boiea354d492017-09-29 16:22:28 -07001688__syscall void *k_timer_user_data_get(struct k_timer *timer);
1689
Patrik Flykt4344e272019-03-08 14:19:05 -07001690static inline void *z_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001691{
1692 return timer->user_data;
1693}
1694
Anas Nashif166f5192018-02-25 08:02:36 -06001695/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001696
Allan Stephensc98da842016-11-11 15:45:03 -05001697/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001698 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001699 * @{
1700 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001701
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001702/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001703 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001705 * This routine returns the elapsed time since the system booted,
1706 * in milliseconds.
1707 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001708 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001709 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001710 * While this function returns time in milliseconds, it does
1711 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001712 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001713 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001714 *
1715 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001716 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001717__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001718
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001719/**
1720 * @brief Enable clock always on in tickless kernel
1721 *
Andy Ross1db9f182019-06-25 10:09:45 -07001722 * Deprecated. This does nothing (it was always just a hint). This
1723 * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE
1724 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001725 *
1726 * @retval prev_status Previous status of always on flag
1727 */
Andy Ross1db9f182019-06-25 10:09:45 -07001728/* LCOV_EXCL_START */
1729__deprecated static inline int k_enable_sys_clock_always_on(void)
1730{
1731 __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1732 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1733
1734 return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE);
1735}
1736/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001737
1738/**
1739 * @brief Disable clock always on in tickless kernel
1740 *
Andy Ross1db9f182019-06-25 10:09:45 -07001741 * Deprecated. This does nothing (it was always just a hint). This
1742 * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE
1743 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001744 */
Andy Ross1db9f182019-06-25 10:09:45 -07001745/* LCOV_EXCL_START */
1746__deprecated static inline void k_disable_sys_clock_always_on(void)
1747{
1748 __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1749 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1750}
1751/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001752
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001753/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001754 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001755 *
Peter Bigota6067a32019-08-28 08:19:26 -05001756 * This routine returns the lower 32 bits of the system uptime in
1757 * milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001758 *
Peter Bigota6067a32019-08-28 08:19:26 -05001759 * Because correct conversion requires full precision of the system
1760 * clock there is no benefit to using this over k_uptime_get() unless
1761 * you know the application will never run long enough for the system
1762 * clock to approach 2^32 ticks. Calls to this function may involve
1763 * interrupt blocking and 64-bit math.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001764 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001765 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001766 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001767 * While this function returns time in milliseconds, it does
1768 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001769 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option
David B. Kinder8de9cc72019-06-25 10:44:55 -07001770 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001771 *
Peter Bigota6067a32019-08-28 08:19:26 -05001772 * @return The low 32 bits of the current uptime, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001773 */
Peter Bigota6067a32019-08-28 08:19:26 -05001774static inline u32_t k_uptime_get_32(void)
1775{
1776 return (u32_t)k_uptime_get();
1777}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001778
1779/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001780 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001782 * This routine computes the elapsed time between the current system uptime
1783 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001784 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001785 * @param reftime Pointer to a reference time, which is updated to the current
1786 * uptime upon return.
1787 *
1788 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001789 */
Andy Ross987c0e52018-09-27 16:50:00 -07001790static inline s64_t k_uptime_delta(s64_t *reftime)
1791{
1792 s64_t uptime, delta;
1793
1794 uptime = k_uptime_get();
1795 delta = uptime - *reftime;
1796 *reftime = uptime;
1797
1798 return delta;
1799}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001800
1801/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001802 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001804 * This routine computes the elapsed time between the current system uptime
1805 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001806 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001807 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1808 * need for interrupt locking and 64-bit math. However, the 32-bit result
1809 * cannot hold an elapsed time larger than approximately 50 days, so the
1810 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001812 * @param reftime Pointer to a reference time, which is updated to the current
1813 * uptime upon return.
1814 *
1815 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001816 */
Andy Ross987c0e52018-09-27 16:50:00 -07001817static inline u32_t k_uptime_delta_32(s64_t *reftime)
1818{
1819 return (u32_t)k_uptime_delta(reftime);
1820}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001821
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001822/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001823 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001825 * This routine returns the current time, as measured by the system's hardware
1826 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001828 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001829 */
Andrew Boie979b17f2019-10-03 15:20:41 -07001830static inline u32_t k_cycle_get_32(void)
1831{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08001832 return arch_k_cycle_get_32();
Andrew Boie979b17f2019-10-03 15:20:41 -07001833}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001834
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001835/**
Anas Nashif166f5192018-02-25 08:02:36 -06001836 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001837 */
1838
Allan Stephensc98da842016-11-11 15:45:03 -05001839/**
1840 * @cond INTERNAL_HIDDEN
1841 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001842
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001843struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001844 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001845 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001846 union {
1847 _wait_q_t wait_q;
1848
1849 _POLL_EVENT;
1850 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001851
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001852 _OBJECT_TRACING_NEXT_PTR(k_queue)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08001853 _OBJECT_TRACING_LINKED_FLAG
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001854};
1855
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001856#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001857 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001858 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Stephanos Ioannidisf628dcd2019-09-11 18:09:49 +09001859 .lock = { }, \
1860 { \
1861 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
1862 _POLL_EVENT_OBJ_INIT(obj) \
1863 }, \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001864 _OBJECT_TRACING_INIT \
1865 }
1866
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05001867#define K_QUEUE_INITIALIZER __DEPRECATED_MACRO _K_QUEUE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001868
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001869extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1870
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001871/**
1872 * INTERNAL_HIDDEN @endcond
1873 */
1874
1875/**
1876 * @defgroup queue_apis Queue APIs
1877 * @ingroup kernel_apis
1878 * @{
1879 */
1880
1881/**
1882 * @brief Initialize a queue.
1883 *
1884 * This routine initializes a queue object, prior to its first use.
1885 *
1886 * @param queue Address of the queue.
1887 *
1888 * @return N/A
1889 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001890__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001891
1892/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001893 * @brief Cancel waiting on a queue.
1894 *
1895 * This routine causes first thread pending on @a queue, if any, to
1896 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001897 * If the queue is being waited on by k_poll(), it will return with
1898 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1899 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001900 *
1901 * @note Can be called by ISRs.
1902 *
1903 * @param queue Address of the queue.
1904 *
1905 * @return N/A
1906 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001907__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001908
1909/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001910 * @brief Append an element to the end of a queue.
1911 *
1912 * This routine appends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001913 * aligned on a word boundary, and the first word of the item is reserved
1914 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001915 *
1916 * @note Can be called by ISRs.
1917 *
1918 * @param queue Address of the queue.
1919 * @param data Address of the data item.
1920 *
1921 * @return N/A
1922 */
1923extern void k_queue_append(struct k_queue *queue, void *data);
1924
1925/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001926 * @brief Append an element to a queue.
1927 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001928 * This routine appends a data item to @a queue. There is an implicit memory
1929 * allocation to create an additional temporary bookkeeping data structure from
1930 * the calling thread's resource pool, which is automatically freed when the
1931 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001932 *
1933 * @note Can be called by ISRs.
1934 *
1935 * @param queue Address of the queue.
1936 * @param data Address of the data item.
1937 *
1938 * @retval 0 on success
1939 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1940 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301941__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001942
1943/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001944 * @brief Prepend an element to a queue.
1945 *
1946 * This routine prepends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001947 * aligned on a word boundary, and the first word of the item is reserved
1948 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001949 *
1950 * @note Can be called by ISRs.
1951 *
1952 * @param queue Address of the queue.
1953 * @param data Address of the data item.
1954 *
1955 * @return N/A
1956 */
1957extern void k_queue_prepend(struct k_queue *queue, void *data);
1958
1959/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001960 * @brief Prepend an element to a queue.
1961 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001962 * This routine prepends a data item to @a queue. There is an implicit memory
1963 * allocation to create an additional temporary bookkeeping data structure from
1964 * the calling thread's resource pool, which is automatically freed when the
1965 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001966 *
1967 * @note Can be called by ISRs.
1968 *
1969 * @param queue Address of the queue.
1970 * @param data Address of the data item.
1971 *
1972 * @retval 0 on success
1973 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1974 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301975__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001976
1977/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001978 * @brief Inserts an element to a queue.
1979 *
1980 * This routine inserts a data item to @a queue after previous item. A queue
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001981 * data item must be aligned on a word boundary, and the first word of
1982 * the item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001983 *
1984 * @note Can be called by ISRs.
1985 *
1986 * @param queue Address of the queue.
1987 * @param prev Address of the previous data item.
1988 * @param data Address of the data item.
1989 *
1990 * @return N/A
1991 */
1992extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1993
1994/**
1995 * @brief Atomically append a list of elements to a queue.
1996 *
1997 * This routine adds a list of data items to @a queue in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001998 * The data items must be in a singly-linked list, with the first word
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001999 * in each data item pointing to the next data item; the list must be
2000 * NULL-terminated.
2001 *
2002 * @note Can be called by ISRs.
2003 *
2004 * @param queue Address of the queue.
2005 * @param head Pointer to first node in singly-linked list.
2006 * @param tail Pointer to last node in singly-linked list.
2007 *
2008 * @return N/A
2009 */
2010extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
2011
2012/**
2013 * @brief Atomically add a list of elements to a queue.
2014 *
2015 * This routine adds a list of data items to @a queue in one operation.
2016 * The data items must be in a singly-linked list implemented using a
2017 * sys_slist_t object. Upon completion, the original list is empty.
2018 *
2019 * @note Can be called by ISRs.
2020 *
2021 * @param queue Address of the queue.
2022 * @param list Pointer to sys_slist_t object.
2023 *
2024 * @return N/A
2025 */
2026extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
2027
2028/**
2029 * @brief Get an element from a queue.
2030 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002031 * This routine removes first data item from @a queue. The first word of the
2032 * data item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002033 *
2034 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2035 *
2036 * @param queue Address of the queue.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002037 * @param timeout Non-negative waiting period to obtain a data item (in
2038 * milliseconds), or one of the special values K_NO_WAIT and
2039 * K_FOREVER.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002040 *
2041 * @return Address of the data item if successful; NULL if returned
2042 * without waiting, or waiting period timed out.
2043 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002044__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002045
2046/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002047 * @brief Remove an element from a queue.
2048 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002049 * This routine removes data item from @a queue. The first word of the
2050 * data item is reserved for the kernel's use. Removing elements from k_queue
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002051 * rely on sys_slist_find_and_remove which is not a constant time operation.
2052 *
2053 * @note Can be called by ISRs
2054 *
2055 * @param queue Address of the queue.
2056 * @param data Address of the data item.
2057 *
2058 * @return true if data item was removed
2059 */
2060static inline bool k_queue_remove(struct k_queue *queue, void *data)
2061{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002062 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002063}
2064
2065/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002066 * @brief Append an element to a queue only if it's not present already.
2067 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002068 * This routine appends data item to @a queue. The first word of the data
2069 * item is reserved for the kernel's use. Appending elements to k_queue
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002070 * relies on sys_slist_is_node_in_list which is not a constant time operation.
2071 *
2072 * @note Can be called by ISRs
2073 *
2074 * @param queue Address of the queue.
2075 * @param data Address of the data item.
2076 *
2077 * @return true if data item was added, false if not
2078 */
2079static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
2080{
2081 sys_sfnode_t *test;
2082
2083 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
2084 if (test == (sys_sfnode_t *) data) {
2085 return false;
2086 }
2087 }
2088
2089 k_queue_append(queue, data);
2090 return true;
2091}
2092
2093/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002094 * @brief Query a queue to see if it has data available.
2095 *
2096 * Note that the data might be already gone by the time this function returns
2097 * if other threads are also trying to read from the queue.
2098 *
2099 * @note Can be called by ISRs.
2100 *
2101 * @param queue Address of the queue.
2102 *
2103 * @return Non-zero if the queue is empty.
2104 * @return 0 if data is available.
2105 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002106__syscall int k_queue_is_empty(struct k_queue *queue);
2107
Patrik Flykt4344e272019-03-08 14:19:05 -07002108static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002109{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002110 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002111}
2112
2113/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002114 * @brief Peek element at the head of queue.
2115 *
2116 * Return element from the head of queue without removing it.
2117 *
2118 * @param queue Address of the queue.
2119 *
2120 * @return Head element, or NULL if queue is empty.
2121 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002122__syscall void *k_queue_peek_head(struct k_queue *queue);
2123
Patrik Flykt4344e272019-03-08 14:19:05 -07002124static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002125{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002126 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002127}
2128
2129/**
2130 * @brief Peek element at the tail of queue.
2131 *
2132 * Return element from the tail of queue without removing it.
2133 *
2134 * @param queue Address of the queue.
2135 *
2136 * @return Tail element, or NULL if queue is empty.
2137 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002138__syscall void *k_queue_peek_tail(struct k_queue *queue);
2139
Patrik Flykt4344e272019-03-08 14:19:05 -07002140static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002141{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002142 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002143}
2144
2145/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002146 * @brief Statically define and initialize a queue.
2147 *
2148 * The queue can be accessed outside the module where it is defined using:
2149 *
2150 * @code extern struct k_queue <name>; @endcode
2151 *
2152 * @param name Name of the queue.
2153 */
2154#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002155 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002156 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002157
Anas Nashif166f5192018-02-25 08:02:36 -06002158/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002159
Wentong Wu5611e922019-06-20 23:51:27 +08002160#ifdef CONFIG_USERSPACE
2161/**
2162 * @brief futex structure
2163 *
2164 * A k_futex is a lightweight mutual exclusion primitive designed
2165 * to minimize kernel involvement. Uncontended operation relies
2166 * only on atomic access to shared memory. k_futex are tracked as
2167 * kernel objects and can live in user memory so any access bypass
2168 * the kernel object permission management mechanism.
2169 */
2170struct k_futex {
2171 atomic_t val;
2172};
2173
2174/**
2175 * @brief futex kernel data structure
2176 *
2177 * z_futex_data are the helper data structure for k_futex to complete
2178 * futex contended operation on kernel side, structure z_futex_data
2179 * of every futex object is invisible in user mode.
2180 */
2181struct z_futex_data {
2182 _wait_q_t wait_q;
2183 struct k_spinlock lock;
2184};
2185
2186#define Z_FUTEX_DATA_INITIALIZER(obj) \
2187 { \
2188 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \
2189 }
2190
2191/**
2192 * @defgroup futex_apis FUTEX APIs
2193 * @ingroup kernel_apis
2194 * @{
2195 */
2196
2197/**
Wentong Wu5611e922019-06-20 23:51:27 +08002198 * @brief Pend the current thread on a futex
2199 *
2200 * Tests that the supplied futex contains the expected value, and if so,
2201 * goes to sleep until some other thread calls k_futex_wake() on it.
2202 *
2203 * @param futex Address of the futex.
2204 * @param expected Expected value of the futex, if it is different the caller
2205 * will not wait on it.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002206 * @param timeout Non-negative waiting period on the futex, in milliseconds, or
2207 * one of the special values K_NO_WAIT or K_FOREVER.
Wentong Wu5611e922019-06-20 23:51:27 +08002208 * @retval -EACCES Caller does not have read access to futex address.
2209 * @retval -EAGAIN If the futex value did not match the expected parameter.
2210 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2211 * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup.
2212 * @retval 0 if the caller went to sleep and was woken up. The caller
2213 * should check the futex's value on wakeup to determine if it needs
2214 * to block again.
2215 */
2216__syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout);
2217
2218/**
2219 * @brief Wake one/all threads pending on a futex
2220 *
2221 * Wake up the highest priority thread pending on the supplied futex, or
2222 * wakeup all the threads pending on the supplied futex, and the behavior
2223 * depends on wake_all.
2224 *
2225 * @param futex Futex to wake up pending threads.
2226 * @param wake_all If true, wake up all pending threads; If false,
2227 * wakeup the highest priority thread.
2228 * @retval -EACCES Caller does not have access to the futex address.
2229 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2230 * @retval Number of threads that were woken up.
2231 */
2232__syscall int k_futex_wake(struct k_futex *futex, bool wake_all);
2233
2234/** @} */
2235#endif
2236
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002237struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002238 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002239};
2240
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002241/**
2242 * @cond INTERNAL_HIDDEN
2243 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002244#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002245 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002246 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002247 }
2248
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002249#define K_FIFO_INITIALIZER __DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002250
Allan Stephensc98da842016-11-11 15:45:03 -05002251/**
2252 * INTERNAL_HIDDEN @endcond
2253 */
2254
2255/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002256 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002257 * @ingroup kernel_apis
2258 * @{
2259 */
2260
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002261/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002262 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002263 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002264 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002265 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002266 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002267 *
2268 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002269 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002270 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002271#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002272 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002273
2274/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002275 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002276 *
2277 * This routine causes first thread pending on @a fifo, if any, to
2278 * return from k_fifo_get() call with NULL value (as if timeout
2279 * expired).
2280 *
2281 * @note Can be called by ISRs.
2282 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002283 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002284 *
2285 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002286 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002287 */
2288#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002289 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002290
2291/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002292 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002293 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002294 * This routine adds a data item to @a fifo. A FIFO data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002295 * aligned on a word boundary, and the first word of the item is reserved
2296 * for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002297 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002298 * @note Can be called by ISRs.
2299 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002300 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002301 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002302 *
2303 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002304 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002305 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002306#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002307 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002308
2309/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002310 * @brief Add an element to a FIFO queue.
2311 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002312 * This routine adds a data item to @a fifo. There is an implicit memory
2313 * allocation to create an additional temporary bookkeeping data structure from
2314 * the calling thread's resource pool, which is automatically freed when the
2315 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002316 *
2317 * @note Can be called by ISRs.
2318 *
2319 * @param fifo Address of the FIFO.
2320 * @param data Address of the data item.
2321 *
2322 * @retval 0 on success
2323 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002324 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002325 */
2326#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002327 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002328
2329/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002330 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002331 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002332 * This routine adds a list of data items to @a fifo in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002333 * The data items must be in a singly-linked list, with the first word of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002334 * each data item pointing to the next data item; the list must be
2335 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002337 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002338 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002339 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002340 * @param head Pointer to first node in singly-linked list.
2341 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002342 *
2343 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002344 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002345 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002346#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002347 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002348
2349/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002350 * @brief Atomically add a list of elements to a FIFO queue.
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.
2353 * The data items must be in a singly-linked list implemented using a
2354 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002355 * and must be re-initialized via sys_slist_init().
2356 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002357 * @note Can be called by ISRs.
2358 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002359 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002360 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002361 *
2362 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002363 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002364 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002365#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002366 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002367
2368/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002369 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002370 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002371 * This routine removes a data item from @a fifo in a "first in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002372 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002374 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2375 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002376 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002377 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002378 * or one of the special values K_NO_WAIT and K_FOREVER.
2379 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002380 * @return Address of the data item if successful; NULL if returned
2381 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002382 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002383 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002384#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002385 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002386
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002387/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002388 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002389 *
2390 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002391 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002392 *
2393 * @note Can be called by ISRs.
2394 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002395 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002396 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002397 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002398 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002399 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002400 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002401#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002402 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002403
2404/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002405 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002406 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002407 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302408 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002409 * on each iteration of processing, a head container will be peeked,
2410 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002411 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002412 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002413 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002414 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002415 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002416 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002417 */
2418#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002419 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002420
2421/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002422 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002423 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002424 * Return element from the tail of FIFO queue (without removing it). A usecase
2425 * for this is if elements of the FIFO queue are themselves containers. Then
2426 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002427 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002428 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002429 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002430 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002431 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002432 */
2433#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002434 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002435
2436/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002437 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002438 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002439 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002440 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002441 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002442 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002443 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002444 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002445 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002446#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002447 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002448 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002449
Anas Nashif166f5192018-02-25 08:02:36 -06002450/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002451
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002452struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002453 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002454};
2455
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002456/**
2457 * @cond INTERNAL_HIDDEN
2458 */
2459
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002460#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002461 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002462 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002463 }
2464
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002465#define K_LIFO_INITIALIZER __DEPRECATED_MACRO _K_LIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002466
Allan Stephensc98da842016-11-11 15:45:03 -05002467/**
2468 * INTERNAL_HIDDEN @endcond
2469 */
2470
2471/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002472 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002473 * @ingroup kernel_apis
2474 * @{
2475 */
2476
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002477/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002478 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002479 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002480 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002481 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002482 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002483 *
2484 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002485 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002486 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002487#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002488 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002489
2490/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002491 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002492 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002493 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002494 * aligned on a word boundary, and the first word of the item is
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002495 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002497 * @note Can be called by ISRs.
2498 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002499 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002500 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002501 *
2502 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002503 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002504 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002505#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002506 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002507
2508/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002509 * @brief Add an element to a LIFO queue.
2510 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002511 * This routine adds a data item to @a lifo. There is an implicit memory
2512 * allocation to create an additional temporary bookkeeping data structure from
2513 * the calling thread's resource pool, which is automatically freed when the
2514 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002515 *
2516 * @note Can be called by ISRs.
2517 *
2518 * @param lifo Address of the LIFO.
2519 * @param data Address of the data item.
2520 *
2521 * @retval 0 on success
2522 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002523 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002524 */
2525#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002526 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002527
2528/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002529 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002531 * This routine removes a data item from @a lifo in a "last in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002532 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002533 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002534 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2535 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002536 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002537 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002538 * or one of the special values K_NO_WAIT and K_FOREVER.
2539 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002540 * @return Address of the data item if successful; NULL if returned
2541 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002542 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002543 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002544#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002545 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002546
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002547/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002548 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002549 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002550 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002551 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002552 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002554 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002555 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002556 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002557#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002558 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002559 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002560
Anas Nashif166f5192018-02-25 08:02:36 -06002561/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002562
2563/**
2564 * @cond INTERNAL_HIDDEN
2565 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302566#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002567
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002568typedef uintptr_t stack_data_t;
2569
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002570struct k_stack {
2571 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002572 struct k_spinlock lock;
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002573 stack_data_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002574
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002575 _OBJECT_TRACING_NEXT_PTR(k_stack)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08002576 _OBJECT_TRACING_LINKED_FLAG
Andrew Boief3bee952018-05-02 17:44:39 -07002577 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002578};
2579
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002580#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002581 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002582 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002583 .base = stack_buffer, \
2584 .next = stack_buffer, \
2585 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002586 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002587 }
2588
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002589#define K_STACK_INITIALIZER __DEPRECATED_MACRO _K_STACK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002590
Allan Stephensc98da842016-11-11 15:45:03 -05002591/**
2592 * INTERNAL_HIDDEN @endcond
2593 */
2594
2595/**
2596 * @defgroup stack_apis Stack APIs
2597 * @ingroup kernel_apis
2598 * @{
2599 */
2600
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002601/**
2602 * @brief Initialize a stack.
2603 *
2604 * This routine initializes a stack object, prior to its first use.
2605 *
2606 * @param stack Address of the stack.
2607 * @param buffer Address of array used to hold stacked values.
2608 * @param num_entries Maximum number of values that can be stacked.
2609 *
2610 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002611 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002612 */
Andrew Boief3bee952018-05-02 17:44:39 -07002613void k_stack_init(struct k_stack *stack,
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002614 stack_data_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002615
2616
2617/**
2618 * @brief Initialize a stack.
2619 *
2620 * This routine initializes a stack object, prior to its first use. Internal
2621 * buffers will be allocated from the calling thread's resource pool.
2622 * This memory will be released if k_stack_cleanup() is called, or
2623 * userspace is enabled and the stack object loses all references to it.
2624 *
2625 * @param stack Address of the stack.
2626 * @param num_entries Maximum number of values that can be stacked.
2627 *
2628 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002629 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002630 */
2631
Adithya Baglody28080d32018-10-15 11:48:51 +05302632__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2633 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002634
2635/**
2636 * @brief Release a stack's allocated buffer
2637 *
2638 * If a stack object was given a dynamically allocated buffer via
2639 * k_stack_alloc_init(), this will free it. This function does nothing
2640 * if the buffer wasn't dynamically allocated.
2641 *
2642 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002643 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002644 */
2645void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002646
2647/**
2648 * @brief Push an element onto a stack.
2649 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002650 * This routine adds a stack_data_t value @a data to @a stack.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002651 *
2652 * @note Can be called by ISRs.
2653 *
2654 * @param stack Address of the stack.
2655 * @param data Value to push onto the stack.
2656 *
2657 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002658 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002659 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002660__syscall void k_stack_push(struct k_stack *stack, stack_data_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002661
2662/**
2663 * @brief Pop an element from a stack.
2664 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002665 * This routine removes a stack_data_t value from @a stack in a "last in,
2666 * first out" manner and stores the value in @a data.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002667 *
2668 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2669 *
2670 * @param stack Address of the stack.
2671 * @param data Address of area to hold the value popped from the stack.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002672 * @param timeout Non-negative waiting period to obtain a value (in
2673 * milliseconds), or one of the special values K_NO_WAIT and
2674 * K_FOREVER.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002675 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002676 * @retval 0 Element popped from stack.
2677 * @retval -EBUSY Returned without waiting.
2678 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002679 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002680 */
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002681__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data,
2682 s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002683
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002684/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002685 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002686 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002687 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002688 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002689 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002691 * @param name Name of the stack.
2692 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002693 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002694 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002695#define K_STACK_DEFINE(name, stack_num_entries) \
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002696 stack_data_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002697 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002698 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002699 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002700 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002701
Anas Nashif166f5192018-02-25 08:02:36 -06002702/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002703
Allan Stephens6bba9b02016-11-16 14:56:54 -05002704struct k_work;
Piotr Zięcik19d83492019-09-27 09:16:25 +02002705struct k_work_poll;
Allan Stephens6bba9b02016-11-16 14:56:54 -05002706
Piotr Zięcik19d83492019-09-27 09:16:25 +02002707/* private, used by k_poll and k_work_poll */
Piotr Zięcik1c4177d2019-08-27 12:19:26 +02002708typedef int (*_poller_cb_t)(struct k_poll_event *event, u32_t state);
2709struct _poller {
2710 volatile bool is_polling;
2711 struct k_thread *thread;
2712 _poller_cb_t cb;
2713};
2714
Allan Stephensc98da842016-11-11 15:45:03 -05002715/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002716 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002717 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002718 */
2719
Allan Stephens6bba9b02016-11-16 14:56:54 -05002720/**
2721 * @typedef k_work_handler_t
2722 * @brief Work item handler function type.
2723 *
2724 * A work item's handler function is executed by a workqueue's thread
2725 * when the work item is processed by the workqueue.
2726 *
2727 * @param work Address of the work item.
2728 *
2729 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002730 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002731 */
2732typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002733
2734/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002735 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002736 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002737
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002738struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002739 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002740 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002741};
2742
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002743enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002744 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002745};
2746
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002747struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002748 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002749 k_work_handler_t handler;
2750 atomic_t flags[1];
2751};
2752
Allan Stephens6bba9b02016-11-16 14:56:54 -05002753struct k_delayed_work {
2754 struct k_work work;
2755 struct _timeout timeout;
2756 struct k_work_q *work_q;
2757};
2758
Piotr Zięcik19d83492019-09-27 09:16:25 +02002759struct k_work_poll {
2760 struct k_work work;
2761 struct _poller poller;
2762 struct k_poll_event *events;
2763 int num_events;
2764 k_work_handler_t real_handler;
2765 struct _timeout timeout;
2766 int poll_result;
2767};
2768
Allan Stephens6bba9b02016-11-16 14:56:54 -05002769extern struct k_work_q k_sys_work_q;
2770
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002771/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002772 * INTERNAL_HIDDEN @endcond
2773 */
2774
Patrik Flykt4344e272019-03-08 14:19:05 -07002775#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002776 { \
2777 ._reserved = NULL, \
2778 .handler = work_handler, \
2779 .flags = { 0 } \
2780 }
2781
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002782#define K_WORK_INITIALIZER __DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002783
Allan Stephens6bba9b02016-11-16 14:56:54 -05002784/**
2785 * @brief Initialize a statically-defined work item.
2786 *
2787 * This macro can be used to initialize a statically-defined workqueue work
2788 * item, prior to its first use. For example,
2789 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002790 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002791 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002792 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002793 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002794 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002795 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002796#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002797 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002798
2799/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002800 * @brief Initialize a work item.
2801 *
2802 * This routine initializes a workqueue work item, prior to its first use.
2803 *
2804 * @param work Address of work item.
2805 * @param handler Function to invoke each time work item is processed.
2806 *
2807 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002808 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002809 */
2810static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2811{
Patrik Flykt4344e272019-03-08 14:19:05 -07002812 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002813}
2814
2815/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002816 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002817 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002818 * This routine submits work item @a work to be processed by workqueue
2819 * @a work_q. If the work item is already pending in the workqueue's queue
2820 * as a result of an earlier submission, this routine has no effect on the
2821 * work item. If the work item has already been processed, or is currently
2822 * being processed, its work is considered complete and the work item can be
2823 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002824 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002825 * @warning
2826 * A submitted work item must not be modified until it has been processed
2827 * by the workqueue.
2828 *
2829 * @note Can be called by ISRs.
2830 *
2831 * @param work_q Address of workqueue.
2832 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002833 *
2834 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002835 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002836 */
2837static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2838 struct k_work *work)
2839{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002840 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002841 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002842 }
2843}
2844
2845/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002846 * @brief Submit a work item to a user mode workqueue
2847 *
David B. Kinder06d78352018-12-17 14:32:40 -08002848 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002849 * memory allocation is made from the caller's resource pool which is freed
2850 * once the worker thread consumes the k_work item. The workqueue
2851 * thread must have memory access to the k_work item being submitted. The caller
2852 * must have permission granted on the work_q parameter's queue object.
2853 *
2854 * Otherwise this works the same as k_work_submit_to_queue().
2855 *
2856 * @note Can be called by ISRs.
2857 *
2858 * @param work_q Address of workqueue.
2859 * @param work Address of work item.
2860 *
2861 * @retval -EBUSY if the work item was already in some workqueue
2862 * @retval -ENOMEM if no memory for thread resource pool allocation
2863 * @retval 0 Success
2864 * @req K-WORK-001
2865 */
2866static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2867 struct k_work *work)
2868{
2869 int ret = -EBUSY;
2870
2871 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2872 ret = k_queue_alloc_append(&work_q->queue, work);
2873
2874 /* Couldn't insert into the queue. Clear the pending bit
2875 * so the work item can be submitted again
2876 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002877 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002878 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2879 }
2880 }
2881
2882 return ret;
2883}
2884
2885/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002886 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002887 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002888 * This routine indicates if work item @a work is pending in a workqueue's
2889 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002890 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002891 * @note Can be called by ISRs.
2892 *
2893 * @param work Address of work item.
2894 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002895 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002896 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002897 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002898static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002899{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002900 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002901}
2902
2903/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002904 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002905 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002906 * This routine starts workqueue @a work_q. The workqueue spawns its work
2907 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002908 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002909 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002910 * @param stack Pointer to work queue thread's stack space, as defined by
2911 * K_THREAD_STACK_DEFINE()
2912 * @param stack_size Size of the work queue thread's stack (in bytes), which
2913 * should either be the same constant passed to
2914 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002915 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002916 *
2917 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002918 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002919 */
Andrew Boie507852a2017-07-25 18:47:07 -07002920extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002921 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002922 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002923
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002924/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002925 * @brief Start a workqueue in user mode
2926 *
2927 * This works identically to k_work_q_start() except it is callable from user
2928 * mode, and the worker thread created will run in user mode.
2929 * The caller must have permissions granted on both the work_q parameter's
2930 * thread and queue objects, and the same restrictions on priority apply as
2931 * k_thread_create().
2932 *
2933 * @param work_q Address of workqueue.
2934 * @param stack Pointer to work queue thread's stack space, as defined by
2935 * K_THREAD_STACK_DEFINE()
2936 * @param stack_size Size of the work queue thread's stack (in bytes), which
2937 * should either be the same constant passed to
2938 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2939 * @param prio Priority of the work queue's thread.
2940 *
2941 * @return N/A
2942 * @req K-WORK-001
2943 */
2944extern void k_work_q_user_start(struct k_work_q *work_q,
2945 k_thread_stack_t *stack,
2946 size_t stack_size, int prio);
2947
2948/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002949 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002950 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002951 * This routine initializes a workqueue delayed work item, prior to
2952 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002953 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002954 * @param work Address of delayed work item.
2955 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002956 *
2957 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002958 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002959 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002960extern void k_delayed_work_init(struct k_delayed_work *work,
2961 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002962
2963/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002964 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002965 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002966 * This routine schedules work item @a work to be processed by workqueue
2967 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002968 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002969 * Only when the countdown completes is the work item actually submitted to
2970 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002971 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002972 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002973 * counting down cancels the existing submission and restarts the
2974 * countdown using the new delay. Note that this behavior is
2975 * inherently subject to race conditions with the pre-existing
2976 * timeouts and work queue, so care must be taken to synchronize such
2977 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002978 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002979 * @warning
2980 * A delayed work item must not be modified until it has been processed
2981 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002982 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002983 * @note Can be called by ISRs.
2984 *
2985 * @param work_q Address of workqueue.
2986 * @param work Address of delayed work item.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002987 * @param delay Non-negative delay before submitting the work item (in
2988 * milliseconds).
Allan Stephens6bba9b02016-11-16 14:56:54 -05002989 *
2990 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002991 * @retval -EINVAL Work item is being processed or has completed its work.
2992 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002993 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002994 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002995extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2996 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002997 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002998
2999/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05003000 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003001 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003002 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07003003 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05003004 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003005 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003006 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003007 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08003008 * @note The result of calling this on a k_delayed_work item that has
3009 * not been submitted (i.e. before the return of the
3010 * k_delayed_work_submit_to_queue() call) is undefined.
3011 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003012 * @param work Address of delayed work item.
3013 *
David B. Kinder8b986d72017-04-18 15:56:26 -07003014 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003015 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003016 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003017 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003018extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003019
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003020/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003021 * @brief Submit a work item to the system workqueue.
3022 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003023 * This routine submits work item @a work to be processed by the system
3024 * workqueue. If the work item is already pending in the workqueue's queue
3025 * as a result of an earlier submission, this routine has no effect on the
3026 * work item. If the work item has already been processed, or is currently
3027 * being processed, its work is considered complete and the work item can be
3028 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003029 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003030 * @warning
3031 * Work items submitted to the system workqueue should avoid using handlers
3032 * that block or yield since this may prevent the system workqueue from
3033 * processing other work items in a timely manner.
3034 *
3035 * @note Can be called by ISRs.
3036 *
3037 * @param work Address of work item.
3038 *
3039 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003040 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003041 */
3042static inline void k_work_submit(struct k_work *work)
3043{
3044 k_work_submit_to_queue(&k_sys_work_q, work);
3045}
3046
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003047/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003048 * @brief Submit a delayed work item to the system workqueue.
3049 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003050 * This routine schedules work item @a work to be processed by the system
3051 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003052 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003053 * Only when the countdown completes is the work item actually submitted to
3054 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003055 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003056 * Submitting a previously submitted delayed work item that is still
3057 * counting down cancels the existing submission and restarts the countdown
3058 * using the new delay. If the work item is currently pending on the
3059 * workqueue's queue because the countdown has completed it is too late to
3060 * resubmit the item, and resubmission fails without impacting the work item.
3061 * If the work item has already been processed, or is currently being processed,
3062 * its work is considered complete and the work item can be resubmitted.
3063 *
3064 * @warning
3065 * Work items submitted to the system workqueue should avoid using handlers
3066 * that block or yield since this may prevent the system workqueue from
3067 * processing other work items in a timely manner.
3068 *
3069 * @note Can be called by ISRs.
3070 *
3071 * @param work Address of delayed work item.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003072 * @param delay Non-negative delay before submitting the work item (in
3073 * milliseconds).
Allan Stephens6bba9b02016-11-16 14:56:54 -05003074 *
3075 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003076 * @retval -EINVAL Work item is being processed or has completed its work.
3077 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003078 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003079 */
3080static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003081 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003082{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05003083 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003084}
3085
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003086/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02003087 * @brief Get time remaining before a delayed work gets scheduled.
3088 *
3089 * This routine computes the (approximate) time remaining before a
3090 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02003091 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02003092 *
3093 * @param work Delayed work item.
3094 *
3095 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003096 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02003097 */
Kumar Galacc334c72017-04-21 10:55:34 -05003098static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02003099{
Andy Ross88924062019-10-03 11:43:10 -07003100 return k_ticks_to_ms_floor64(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02003101}
3102
Piotr Zięcik19d83492019-09-27 09:16:25 +02003103/**
3104 * @brief Initialize a triggered work item.
3105 *
3106 * This routine initializes a workqueue triggered work item, prior to
3107 * its first use.
3108 *
3109 * @param work Address of triggered work item.
3110 * @param handler Function to invoke each time work item is processed.
3111 *
3112 * @return N/A
3113 */
3114extern void k_work_poll_init(struct k_work_poll *work,
3115 k_work_handler_t handler);
3116
3117/**
3118 * @brief Submit a triggered work item.
3119 *
3120 * This routine schedules work item @a work to be processed by workqueue
3121 * @a work_q when one of the given @a events is signaled. The routine
3122 * initiates internal poller for the work item and then returns to the caller.
3123 * Only when one of the watched events happen the work item is actually
3124 * submitted to the workqueue and becomes pending.
3125 *
3126 * Submitting a previously submitted triggered work item that is still
3127 * waiting for the event cancels the existing submission and reschedules it
3128 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003129 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003130 * so care must be taken to synchronize such resubmissions externally.
3131 *
3132 * @note Can be called by ISRs.
3133 *
3134 * @warning
3135 * Provided array of events as well as a triggered work item must be placed
3136 * in persistent memory (valid until work handler execution or work
3137 * cancellation) and cannot be modified after submission.
3138 *
3139 * @param work_q Address of workqueue.
3140 * @param work Address of delayed work item.
3141 * @param events An array of pointers to events which trigger the work.
3142 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003143 * @param timeout Non-negative timeout after which the work will be scheduled
3144 * for execution even if not triggered.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003145 *
3146 *
3147 * @retval 0 Work item started watching for events.
3148 * @retval -EINVAL Work item is being processed or has completed its work.
3149 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3150 */
3151extern int k_work_poll_submit_to_queue(struct k_work_q *work_q,
3152 struct k_work_poll *work,
3153 struct k_poll_event *events,
3154 int num_events,
3155 s32_t timeout);
3156
3157/**
3158 * @brief Submit a triggered work item to the system workqueue.
3159 *
3160 * This routine schedules work item @a work to be processed by system
3161 * workqueue when one of the given @a events is signaled. The routine
3162 * initiates internal poller for the work item and then returns to the caller.
3163 * Only when one of the watched events happen the work item is actually
3164 * submitted to the workqueue and becomes pending.
3165 *
3166 * Submitting a previously submitted triggered work item that is still
3167 * waiting for the event cancels the existing submission and reschedules it
3168 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003169 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003170 * so care must be taken to synchronize such resubmissions externally.
3171 *
3172 * @note Can be called by ISRs.
3173 *
3174 * @warning
3175 * Provided array of events as well as a triggered work item must not be
3176 * modified until the item has been processed by the workqueue.
3177 *
3178 * @param work Address of delayed work item.
3179 * @param events An array of pointers to events which trigger the work.
3180 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003181 * @param timeout Non-negative timeout after which the work will be scheduled
3182 * for execution even if not triggered.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003183 *
3184 * @retval 0 Work item started watching for events.
3185 * @retval -EINVAL Work item is being processed or has completed its work.
3186 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3187 */
3188static inline int k_work_poll_submit(struct k_work_poll *work,
3189 struct k_poll_event *events,
3190 int num_events,
3191 s32_t timeout)
3192{
3193 return k_work_poll_submit_to_queue(&k_sys_work_q, work,
3194 events, num_events, timeout);
3195}
3196
3197/**
3198 * @brief Cancel a triggered work item.
3199 *
3200 * This routine cancels the submission of triggered work item @a work.
3201 * A triggered work item can only be canceled if no event triggered work
3202 * submission.
3203 *
3204 * @note Can be called by ISRs.
3205 *
3206 * @param work Address of delayed work item.
3207 *
David B. Kinder73896c02019-10-28 16:27:57 -07003208 * @retval 0 Work item canceled.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003209 * @retval -EINVAL Work item is being processed or has completed its work.
3210 */
3211extern int k_work_poll_cancel(struct k_work_poll *work);
3212
Anas Nashif166f5192018-02-25 08:02:36 -06003213/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003214/**
Anas Nashifce78d162018-05-24 12:43:11 -05003215 * @defgroup mutex_apis Mutex APIs
3216 * @ingroup kernel_apis
3217 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003218 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003219
Anas Nashifce78d162018-05-24 12:43:11 -05003220/**
3221 * Mutex Structure
3222 * @ingroup mutex_apis
3223 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003224struct k_mutex {
3225 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05003226 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04003227 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05003228 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003229 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003230
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003231 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003232 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003233};
3234
Anas Nashifce78d162018-05-24 12:43:11 -05003235/**
3236 * @cond INTERNAL_HIDDEN
3237 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003238#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003239 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003240 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003241 .owner = NULL, \
3242 .lock_count = 0, \
3243 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003244 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003245 }
3246
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003247#define K_MUTEX_INITIALIZER __DEPRECATED_MACRO _K_MUTEX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003248
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003249/**
Allan Stephensc98da842016-11-11 15:45:03 -05003250 * INTERNAL_HIDDEN @endcond
3251 */
3252
3253/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003254 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003255 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003256 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003257 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003258 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003259 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003260 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003261 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003262 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003263#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003264 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003265 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003266
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003267/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003268 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003269 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003270 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003271 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003272 * Upon completion, the mutex is available and does not have an owner.
3273 *
3274 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003275 *
3276 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003277 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003278 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003279__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003280
3281/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003282 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003283 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003284 * This routine locks @a mutex. If the mutex is locked by another thread,
3285 * the calling thread waits until the mutex becomes available or until
3286 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003287 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003288 * A thread is permitted to lock a mutex it has already locked. The operation
3289 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003290 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003291 * @param mutex Address of the mutex.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003292 * @param timeout Non-negative waiting period to lock the mutex (in
3293 * milliseconds), or one of the special values K_NO_WAIT and
3294 * K_FOREVER.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003295 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003296 * @retval 0 Mutex locked.
3297 * @retval -EBUSY Returned without waiting.
3298 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003299 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003300 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003301__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003302
3303/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003304 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003305 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003306 * This routine unlocks @a mutex. The mutex must already be locked by the
3307 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003308 *
3309 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003310 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003311 * thread.
3312 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003313 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003314 *
3315 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003316 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003317 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003318__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003319
Allan Stephensc98da842016-11-11 15:45:03 -05003320/**
Anas Nashif166f5192018-02-25 08:02:36 -06003321 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003322 */
3323
3324/**
3325 * @cond INTERNAL_HIDDEN
3326 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003327
3328struct k_sem {
3329 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303330 u32_t count;
3331 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003332 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003333
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003334 _OBJECT_TRACING_NEXT_PTR(k_sem)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003335 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003336};
3337
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003338#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003339 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003340 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003341 .count = initial_count, \
3342 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003343 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003344 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003345 }
3346
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003347#define K_SEM_INITIALIZER __DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003348
Allan Stephensc98da842016-11-11 15:45:03 -05003349/**
3350 * INTERNAL_HIDDEN @endcond
3351 */
3352
3353/**
3354 * @defgroup semaphore_apis Semaphore APIs
3355 * @ingroup kernel_apis
3356 * @{
3357 */
3358
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003359/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003360 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003362 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003363 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003364 * @param sem Address of the semaphore.
3365 * @param initial_count Initial semaphore count.
3366 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003367 *
3368 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003369 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003370 */
Andrew Boie99280232017-09-29 14:17:47 -07003371__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
3372 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003373
3374/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003375 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003376 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003377 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003379 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3380 *
3381 * @param sem Address of the semaphore.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003382 * @param timeout Non-negative waiting period to take the semaphore (in
3383 * milliseconds), or one of the special values K_NO_WAIT and
3384 * K_FOREVER.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003385 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003386 * @retval 0 Semaphore taken.
3387 * @retval -EBUSY Returned without waiting.
3388 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003389 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003390 */
Andrew Boie99280232017-09-29 14:17:47 -07003391__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003392
3393/**
3394 * @brief Give a semaphore.
3395 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003396 * This routine gives @a sem, unless the semaphore is already at its maximum
3397 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003398 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003399 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003400 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003401 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003402 *
3403 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003404 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003405 */
Andrew Boie99280232017-09-29 14:17:47 -07003406__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003407
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003408/**
3409 * @brief Reset a semaphore's count to zero.
3410 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003411 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003412 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003413 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003414 *
3415 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003416 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003417 */
Andrew Boie990bf162017-10-03 12:36:49 -07003418__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003419
Anas Nashif954d5502018-02-25 08:37:28 -06003420/**
3421 * @internal
3422 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003423static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003424{
Patrik Flykt24d71432019-03-26 19:57:45 -06003425 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003426}
3427
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003428/**
3429 * @brief Get a semaphore's count.
3430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003431 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003432 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003433 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003434 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003435 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003436 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003437 */
Andrew Boie990bf162017-10-03 12:36:49 -07003438__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003439
Anas Nashif954d5502018-02-25 08:37:28 -06003440/**
3441 * @internal
3442 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003443static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003444{
3445 return sem->count;
3446}
3447
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003448/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003449 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003450 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003451 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003452 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003453 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003455 * @param name Name of the semaphore.
3456 * @param initial_count Initial semaphore count.
3457 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003458 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003459 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003460#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003461 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003462 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303463 BUILD_ASSERT(((count_limit) != 0) && \
3464 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003465
Anas Nashif166f5192018-02-25 08:02:36 -06003466/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003467
3468/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003469 * @defgroup msgq_apis Message Queue APIs
3470 * @ingroup kernel_apis
3471 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003472 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003473
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003474/**
3475 * @brief Message Queue Structure
3476 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003477struct k_msgq {
3478 _wait_q_t wait_q;
Andy Rossbe03dbd2018-07-26 10:23:02 -07003479 struct k_spinlock lock;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003480 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003481 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003482 char *buffer_start;
3483 char *buffer_end;
3484 char *read_ptr;
3485 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003486 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003487
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003488 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003489 _OBJECT_TRACING_LINKED_FLAG
Andrew Boie0fe789f2018-04-12 18:35:56 -07003490 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003491};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003492/**
3493 * @cond INTERNAL_HIDDEN
3494 */
3495
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003496
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003497#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003498 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003499 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003500 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003501 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003502 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003503 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003504 .read_ptr = q_buffer, \
3505 .write_ptr = q_buffer, \
3506 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003507 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003508 }
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003509#define K_MSGQ_INITIALIZER __DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003510/**
3511 * INTERNAL_HIDDEN @endcond
3512 */
3513
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003514
Andrew Boie0fe789f2018-04-12 18:35:56 -07003515#define K_MSGQ_FLAG_ALLOC BIT(0)
3516
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003517/**
3518 * @brief Message Queue Attributes
3519 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303520struct k_msgq_attrs {
3521 size_t msg_size;
3522 u32_t max_msgs;
3523 u32_t used_msgs;
3524};
3525
Allan Stephensc98da842016-11-11 15:45:03 -05003526
3527/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003528 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003530 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3531 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003532 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3533 * message is similarly aligned to this boundary, @a q_msg_size must also be
3534 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003536 * The message queue can be accessed outside the module where it is defined
3537 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003538 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003539 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003541 * @param q_name Name of the message queue.
3542 * @param q_msg_size Message size (in bytes).
3543 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003544 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003545 *
3546 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003547 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003548#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3549 static char __noinit __aligned(q_align) \
3550 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3551 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
3552 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003553 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003554
Peter Mitsisd7a37502016-10-13 11:37:40 -04003555/**
3556 * @brief Initialize a message queue.
3557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003558 * This routine initializes a message queue object, prior to its first use.
3559 *
Allan Stephensda827222016-11-09 14:23:58 -06003560 * The message queue's ring buffer must contain space for @a max_msgs messages,
3561 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3562 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3563 * that each message is similarly aligned to this boundary, @a q_msg_size
3564 * must also be a multiple of N.
3565 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003566 * @param q Address of the message queue.
3567 * @param buffer Pointer to ring buffer that holds queued messages.
3568 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003569 * @param max_msgs Maximum number of messages that can be queued.
3570 *
3571 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003572 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003573 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003574void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3575 u32_t max_msgs);
3576
3577/**
3578 * @brief Initialize a message queue.
3579 *
3580 * This routine initializes a message queue object, prior to its first use,
3581 * allocating its internal ring buffer from the calling thread's resource
3582 * pool.
3583 *
3584 * Memory allocated for the ring buffer can be released by calling
3585 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3586 * all of its references.
3587 *
3588 * @param q Address of the message queue.
3589 * @param msg_size Message size (in bytes).
3590 * @param max_msgs Maximum number of messages that can be queued.
3591 *
3592 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3593 * thread's resource pool, or -EINVAL if the size parameters cause
3594 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003595 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003596 */
3597__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3598 u32_t max_msgs);
3599
3600
3601void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003602
3603/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003604 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003606 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003607 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003608 * @note Can be called by ISRs.
3609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003610 * @param q Address of the message queue.
3611 * @param data Pointer to the message.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003612 * @param timeout Non-negative waiting period to add the message (in
3613 * milliseconds), or one of the special values K_NO_WAIT and
3614 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003615 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003616 * @retval 0 Message sent.
3617 * @retval -ENOMSG Returned without waiting or queue purged.
3618 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003619 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003620 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003621__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003622
3623/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003624 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003626 * This routine receives a message from message queue @a q in a "first in,
3627 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003628 *
Allan Stephensc98da842016-11-11 15:45:03 -05003629 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003631 * @param q Address of the message queue.
3632 * @param data Address of area to hold the received message.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003633 * @param timeout Non-negative waiting period to receive the message (in
3634 * milliseconds), or one of the special values K_NO_WAIT and
3635 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003636 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003637 * @retval 0 Message received.
3638 * @retval -ENOMSG Returned without waiting.
3639 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003640 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003641 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003642__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003643
3644/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003645 * @brief Peek/read a message from a message queue.
3646 *
3647 * This routine reads a message from message queue @a q in a "first in,
3648 * first out" manner and leaves the message in the queue.
3649 *
3650 * @note Can be called by ISRs.
3651 *
3652 * @param q Address of the message queue.
3653 * @param data Address of area to hold the message read from the queue.
3654 *
3655 * @retval 0 Message read.
3656 * @retval -ENOMSG Returned when the queue has no message.
3657 * @req K-MSGQ-002
3658 */
3659__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3660
3661/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003662 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003663 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003664 * This routine discards all unreceived messages in a message queue's ring
3665 * buffer. Any threads that are blocked waiting to send a message to the
3666 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003667 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003668 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003669 *
3670 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003671 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003672 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003673__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003674
Peter Mitsis67be2492016-10-07 11:44:34 -04003675/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003676 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003677 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003678 * This routine returns the number of unused entries in a message queue's
3679 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003680 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003681 * @param q Address of the message queue.
3682 *
3683 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003684 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003685 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003686__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3687
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303688/**
3689 * @brief Get basic attributes of a message queue.
3690 *
3691 * This routine fetches basic attributes of message queue into attr argument.
3692 *
3693 * @param q Address of the message queue.
3694 * @param attrs pointer to message queue attribute structure.
3695 *
3696 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003697 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303698 */
3699__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3700
3701
Patrik Flykt4344e272019-03-08 14:19:05 -07003702static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003703{
3704 return q->max_msgs - q->used_msgs;
3705}
3706
Peter Mitsisd7a37502016-10-13 11:37:40 -04003707/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003708 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003709 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003710 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003711 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003712 * @param q Address of the message queue.
3713 *
3714 * @return Number of messages.
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 u32_t k_msgq_num_used_get(struct k_msgq *q);
3718
Patrik Flykt4344e272019-03-08 14:19:05 -07003719static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003720{
3721 return q->used_msgs;
3722}
3723
Anas Nashif166f5192018-02-25 08:02:36 -06003724/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003725
3726/**
3727 * @defgroup mem_pool_apis Memory Pool APIs
3728 * @ingroup kernel_apis
3729 * @{
3730 */
3731
Andy Ross73cb9582017-05-09 10:42:39 -07003732/* Note on sizing: the use of a 20 bit field for block means that,
3733 * assuming a reasonable minimum block size of 16 bytes, we're limited
3734 * to 16M of memory managed by a single pool. Long term it would be
3735 * good to move to a variable bit size based on configuration.
3736 */
3737struct k_mem_block_id {
3738 u32_t pool : 8;
3739 u32_t level : 4;
3740 u32_t block : 20;
3741};
3742
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003743struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003744 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003745 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003746};
3747
Anas Nashif166f5192018-02-25 08:02:36 -06003748/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003749
3750/**
3751 * @defgroup mailbox_apis Mailbox APIs
3752 * @ingroup kernel_apis
3753 * @{
3754 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003755
3756struct k_mbox_msg {
3757 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003758 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003759 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003760 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003761 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003762 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003763 /** sender's message data buffer */
3764 void *tx_data;
3765 /** internal use only - needed for legacy API support */
3766 void *_rx_data;
3767 /** message data block descriptor */
3768 struct k_mem_block tx_block;
3769 /** source thread id */
3770 k_tid_t rx_source_thread;
3771 /** target thread id */
3772 k_tid_t tx_target_thread;
3773 /** internal use only - thread waiting on send (may be a dummy) */
3774 k_tid_t _syncing_thread;
3775#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3776 /** internal use only - semaphore used during asynchronous send */
3777 struct k_sem *_async_sem;
3778#endif
3779};
3780
3781struct k_mbox {
3782 _wait_q_t tx_msg_queue;
3783 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003784 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003785
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003786 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003787 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003788};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003789/**
3790 * @cond INTERNAL_HIDDEN
3791 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003792
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003793#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003794 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003795 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3796 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003797 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003798 }
3799
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003800#define K_MBOX_INITIALIZER __DEPRECATED_MACRO _K_MBOX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003801
Peter Mitsis12092702016-10-14 12:57:23 -04003802/**
Allan Stephensc98da842016-11-11 15:45:03 -05003803 * INTERNAL_HIDDEN @endcond
3804 */
3805
3806/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003807 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003809 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003810 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003811 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003813 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003814 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003815 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003816#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003817 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003818 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003819
Peter Mitsis12092702016-10-14 12:57:23 -04003820/**
3821 * @brief Initialize a mailbox.
3822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003823 * This routine initializes a mailbox object, prior to its first use.
3824 *
3825 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003826 *
3827 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003828 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003829 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003830extern void k_mbox_init(struct k_mbox *mbox);
3831
Peter Mitsis12092702016-10-14 12:57:23 -04003832/**
3833 * @brief Send a mailbox message in a synchronous manner.
3834 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003835 * This routine sends a message to @a mbox and waits for a receiver to both
3836 * receive and process it. The message data may be in a buffer, in a memory
3837 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003838 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003839 * @param mbox Address of the mailbox.
3840 * @param tx_msg Address of the transmit message descriptor.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003841 * @param timeout Non-negative waiting period for the message to be received (in
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003842 * milliseconds), or one of the special values K_NO_WAIT
3843 * and K_FOREVER. Once the message has been received,
3844 * this routine waits as long as necessary for the message
3845 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003846 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003847 * @retval 0 Message sent.
3848 * @retval -ENOMSG Returned without waiting.
3849 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003850 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003851 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003852extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003853 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003854
Peter Mitsis12092702016-10-14 12:57:23 -04003855/**
3856 * @brief Send a mailbox message in an asynchronous manner.
3857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003858 * This routine sends a message to @a mbox without waiting for a receiver
3859 * to process it. The message data may be in a buffer, in a memory pool block,
3860 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3861 * will be given when the message has been both received and completely
3862 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003864 * @param mbox Address of the mailbox.
3865 * @param tx_msg Address of the transmit message descriptor.
3866 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003867 *
3868 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003869 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003870 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003871extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003872 struct k_sem *sem);
3873
Peter Mitsis12092702016-10-14 12:57:23 -04003874/**
3875 * @brief Receive a mailbox message.
3876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003877 * This routine receives a message from @a mbox, then optionally retrieves
3878 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003880 * @param mbox Address of the mailbox.
3881 * @param rx_msg Address of the receive message descriptor.
3882 * @param buffer Address of the buffer to receive data, or NULL to defer data
3883 * retrieval and message disposal until later.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003884 * @param timeout Non-negative waiting period for a message to be received (in
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003885 * milliseconds), or one of the special values K_NO_WAIT
3886 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003887 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003888 * @retval 0 Message received.
3889 * @retval -ENOMSG Returned without waiting.
3890 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003891 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003892 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003893extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003894 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003895
3896/**
3897 * @brief Retrieve mailbox message data into a buffer.
3898 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003899 * This routine completes the processing of a received message by retrieving
3900 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003901 *
3902 * Alternatively, this routine can be used to dispose of a received message
3903 * without retrieving its data.
3904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003905 * @param rx_msg Address of the receive message descriptor.
3906 * @param buffer Address of the buffer to receive data, or NULL to discard
3907 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003908 *
3909 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003910 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003911 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003912extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003913
3914/**
3915 * @brief Retrieve mailbox message data into a memory pool block.
3916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003917 * This routine completes the processing of a received message by retrieving
3918 * its data into a memory pool block, then disposing of the message.
3919 * The memory pool block that results from successful retrieval must be
3920 * returned to the pool once the data has been processed, even in cases
3921 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003922 *
3923 * Alternatively, this routine can be used to dispose of a received message
3924 * without retrieving its data. In this case there is no need to return a
3925 * memory pool block to the pool.
3926 *
3927 * This routine allocates a new memory pool block for the data only if the
3928 * data is not already in one. If a new block cannot be allocated, the routine
3929 * returns a failure code and the received message is left unchanged. This
3930 * permits the caller to reattempt data retrieval at a later time or to dispose
3931 * of the received message without retrieving its data.
3932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003933 * @param rx_msg Address of a receive message descriptor.
3934 * @param pool Address of memory pool, or NULL to discard data.
3935 * @param block Address of the area to hold memory pool block info.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003936 * @param timeout Non-negative waiting period to wait for a memory pool block
3937 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003938 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003939 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003940 * @retval 0 Data retrieved.
3941 * @retval -ENOMEM Returned without waiting.
3942 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003943 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003944 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003945extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003946 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003947 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003948
Anas Nashif166f5192018-02-25 08:02:36 -06003949/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003950
3951/**
Anas Nashifce78d162018-05-24 12:43:11 -05003952 * @defgroup pipe_apis Pipe APIs
3953 * @ingroup kernel_apis
3954 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003955 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003956
Anas Nashifce78d162018-05-24 12:43:11 -05003957/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003958struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003959 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3960 size_t size; /**< Buffer size */
3961 size_t bytes_used; /**< # bytes used in buffer */
3962 size_t read_index; /**< Where in buffer to read from */
3963 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08003964 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003965
3966 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003967 _wait_q_t readers; /**< Reader wait queue */
3968 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003969 } wait_q;
3970
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003971 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003972 _OBJECT_TRACING_LINKED_FLAG
Anas Nashifce78d162018-05-24 12:43:11 -05003973 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003974};
3975
Anas Nashifce78d162018-05-24 12:43:11 -05003976/**
3977 * @cond INTERNAL_HIDDEN
3978 */
3979#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3980
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003981#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
3982 { \
3983 .buffer = pipe_buffer, \
3984 .size = pipe_buffer_size, \
3985 .bytes_used = 0, \
3986 .read_index = 0, \
3987 .write_index = 0, \
3988 .lock = {}, \
3989 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003990 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
3991 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003992 }, \
3993 _OBJECT_TRACING_INIT \
3994 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003995 }
3996
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003997#define K_PIPE_INITIALIZER __DEPRECATED_MACRO _K_PIPE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003998
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003999/**
Allan Stephensc98da842016-11-11 15:45:03 -05004000 * INTERNAL_HIDDEN @endcond
4001 */
4002
4003/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004004 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004006 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004007 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004008 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004010 * @param name Name of the pipe.
4011 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
4012 * or zero if no ring buffer is used.
4013 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004014 *
4015 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004016 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004017#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08004018 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07004019 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004020 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004021 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004022
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004023/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004024 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004025 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004026 * This routine initializes a pipe object, prior to its first use.
4027 *
4028 * @param pipe Address of the pipe.
4029 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
4030 * is used.
4031 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4032 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004033 *
4034 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004035 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004036 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004037void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
4038
4039/**
4040 * @brief Release a pipe's allocated buffer
4041 *
4042 * If a pipe object was given a dynamically allocated buffer via
4043 * k_pipe_alloc_init(), this will free it. This function does nothing
4044 * if the buffer wasn't dynamically allocated.
4045 *
4046 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004047 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004048 */
4049void k_pipe_cleanup(struct k_pipe *pipe);
4050
4051/**
4052 * @brief Initialize a pipe and allocate a buffer for it
4053 *
4054 * Storage for the buffer region will be allocated from the calling thread's
4055 * resource pool. This memory will be released if k_pipe_cleanup() is called,
4056 * or userspace is enabled and the pipe object loses all references to it.
4057 *
4058 * This function should only be called on uninitialized pipe objects.
4059 *
4060 * @param pipe Address of the pipe.
4061 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4062 * buffer is used.
4063 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004064 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004065 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004066 */
4067__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004068
4069/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004070 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004071 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004072 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004074 * @param pipe Address of the pipe.
4075 * @param data Address of data to write.
4076 * @param bytes_to_write Size of data (in bytes).
4077 * @param bytes_written Address of area to hold the number of bytes written.
4078 * @param min_xfer Minimum number of bytes to write.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004079 * @param timeout Non-negative waiting period to wait for the data to be written
4080 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004081 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004082 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004083 * @retval 0 At least @a min_xfer bytes of data were written.
4084 * @retval -EIO Returned without waiting; zero data bytes were written.
4085 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004086 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004087 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004088 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004089__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
4090 size_t bytes_to_write, size_t *bytes_written,
4091 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004092
4093/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004094 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004095 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004096 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004097 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004098 * @param pipe Address of the pipe.
4099 * @param data Address to place the data read from pipe.
4100 * @param bytes_to_read Maximum number of data bytes to read.
4101 * @param bytes_read Address of area to hold the number of bytes read.
4102 * @param min_xfer Minimum number of data bytes to read.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004103 * @param timeout Non-negative waiting period to wait for the data to be read
4104 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004105 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004106 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004107 * @retval 0 At least @a min_xfer bytes of data were read.
4108 * @retval -EIO Returned without waiting; zero data bytes were read.
4109 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004110 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004111 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004112 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004113__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
4114 size_t bytes_to_read, size_t *bytes_read,
4115 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004116
4117/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004118 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004119 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004120 * This routine writes the data contained in a memory block to @a pipe.
4121 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004122 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004124 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004125 * @param block Memory block containing data to send
4126 * @param size Number of data bytes in memory block to send
4127 * @param sem Semaphore to signal upon completion (else NULL)
4128 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004129 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004130 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004131 */
4132extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
4133 size_t size, struct k_sem *sem);
4134
Anas Nashif166f5192018-02-25 08:02:36 -06004135/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004136
Allan Stephensc98da842016-11-11 15:45:03 -05004137/**
4138 * @cond INTERNAL_HIDDEN
4139 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004140
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004141struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004142 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05004143 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04004144 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004145 char *buffer;
4146 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05004147 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004148
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004149 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004150 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004151};
4152
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004153#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004154 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004155 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004156 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004157 .num_blocks = slab_num_blocks, \
4158 .block_size = slab_block_size, \
4159 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004160 .free_list = NULL, \
4161 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05004162 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004163 }
4164
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05004165#define K_MEM_SLAB_INITIALIZER __DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004166
4167
Peter Mitsis578f9112016-10-07 13:50:31 -04004168/**
Allan Stephensc98da842016-11-11 15:45:03 -05004169 * INTERNAL_HIDDEN @endcond
4170 */
4171
4172/**
4173 * @defgroup mem_slab_apis Memory Slab APIs
4174 * @ingroup kernel_apis
4175 * @{
4176 */
4177
4178/**
Allan Stephensda827222016-11-09 14:23:58 -06004179 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04004180 *
Allan Stephensda827222016-11-09 14:23:58 -06004181 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004182 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06004183 * @a slab_align -byte boundary. To ensure that each memory block is similarly
4184 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004185 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04004186 *
Allan Stephensda827222016-11-09 14:23:58 -06004187 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004188 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004189 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004190 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004191 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004192 * @param name Name of the memory slab.
4193 * @param slab_block_size Size of each memory block (in bytes).
4194 * @param slab_num_blocks Number memory blocks.
4195 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004196 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04004197 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004198#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004199 char __noinit __aligned(WB_UP(slab_align)) \
4200 _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004201 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004202 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004203 WB_UP(slab_block_size), slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004204
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004205/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004206 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004207 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004208 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004209 *
Allan Stephensda827222016-11-09 14:23:58 -06004210 * The memory slab's buffer contains @a slab_num_blocks memory blocks
4211 * that are @a slab_block_size bytes long. The buffer must be aligned to an
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004212 * N-byte boundary matching a word boundary, where N is a power of 2
4213 * (i.e. 4 on 32-bit systems, 8, 16, ...).
Allan Stephensda827222016-11-09 14:23:58 -06004214 * To ensure that each memory block is similarly aligned to this boundary,
4215 * @a slab_block_size must also be a multiple of N.
4216 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004217 * @param slab Address of the memory slab.
4218 * @param buffer Pointer to buffer used for the memory blocks.
4219 * @param block_size Size of each memory block (in bytes).
4220 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004221 *
4222 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004223 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004224 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004225extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05004226 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004227
4228/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004229 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004231 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004233 * @param slab Address of the memory slab.
4234 * @param mem Pointer to block address area.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004235 * @param timeout Non-negative waiting period to wait for operation to complete
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004236 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4237 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004238 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004239 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004240 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004241 * @retval -ENOMEM Returned without waiting.
4242 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004243 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004244 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004245extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05004246 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004247
4248/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004249 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004251 * This routine releases a previously allocated memory block back to its
4252 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004253 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004254 * @param slab Address of the memory slab.
4255 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004256 *
4257 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004258 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004259 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004260extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004261
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004262/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004263 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004264 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004265 * This routine gets the number of memory blocks that are currently
4266 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004267 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004268 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004269 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004270 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004271 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004272 */
Kumar Galacc334c72017-04-21 10:55:34 -05004273static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004274{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004275 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004276}
4277
Peter Mitsisc001aa82016-10-13 13:53:37 -04004278/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004279 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004280 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004281 * This routine gets the number of memory blocks that are currently
4282 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004283 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004284 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004285 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004286 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004287 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004288 */
Kumar Galacc334c72017-04-21 10:55:34 -05004289static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004290{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004291 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004292}
4293
Anas Nashif166f5192018-02-25 08:02:36 -06004294/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004295
4296/**
4297 * @cond INTERNAL_HIDDEN
4298 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004299
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004300struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004301 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004302 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004303};
4304
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004305/**
Allan Stephensc98da842016-11-11 15:45:03 -05004306 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004307 */
4308
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004309/**
Allan Stephensc98da842016-11-11 15:45:03 -05004310 * @addtogroup mem_pool_apis
4311 * @{
4312 */
4313
4314/**
4315 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004316 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004317 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4318 * long. The memory pool allows blocks to be repeatedly partitioned into
4319 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004320 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004321 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004322 * If the pool is to be accessed outside the module where it is defined, it
4323 * can be declared via
4324 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004325 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004326 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004327 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004328 * @param minsz Size of the smallest blocks in the pool (in bytes).
4329 * @param maxsz Size of the largest blocks in the pool (in bytes).
4330 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004331 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004332 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004333 */
Andy Ross73cb9582017-05-09 10:42:39 -07004334#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004335 char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \
Andy Ross73cb9582017-05-09 10:42:39 -07004336 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004337 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004338 Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004339 .base = { \
4340 .buf = _mpool_buf_##name, \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004341 .max_sz = WB_UP(maxsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004342 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004343 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004344 .levels = _mpool_lvls_##name, \
4345 .flags = SYS_MEM_POOL_KERNEL \
4346 } \
Johann Fischer223a2b92019-07-04 15:55:20 +02004347 }; \
Nicolas Pitreb2a022b2019-09-26 16:36:40 -04004348 BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004349
Peter Mitsis937042c2016-10-13 13:18:26 -04004350/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004351 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004353 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004355 * @param pool Address of the memory pool.
4356 * @param block Pointer to block descriptor for the allocated memory.
4357 * @param size Amount of memory to allocate (in bytes).
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004358 * @param timeout Non-negative waiting period to wait for operation to complete
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004359 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4360 * or K_FOREVER to wait as long as necessary.
4361 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004362 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004363 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004364 * @retval -ENOMEM Returned without waiting.
4365 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004366 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004367 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004368extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004369 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004370
4371/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004372 * @brief Allocate memory from a memory pool with malloc() semantics
4373 *
4374 * Such memory must be released using k_free().
4375 *
4376 * @param pool Address of the memory pool.
4377 * @param size Amount of memory to allocate (in bytes).
4378 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004379 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004380 */
4381extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4382
4383/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004384 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004386 * This routine releases a previously allocated memory block back to its
4387 * memory pool.
4388 *
4389 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004390 *
4391 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004392 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004393 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004394extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004395
4396/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004397 * @brief Free memory allocated from a memory pool.
4398 *
4399 * This routine releases a previously allocated memory block back to its
4400 * memory pool
4401 *
4402 * @param id Memory block identifier.
4403 *
4404 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004405 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004406 */
4407extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4408
4409/**
Anas Nashif166f5192018-02-25 08:02:36 -06004410 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004411 */
4412
4413/**
4414 * @defgroup heap_apis Heap Memory Pool APIs
4415 * @ingroup kernel_apis
4416 * @{
4417 */
4418
4419/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004420 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004422 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004423 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004424 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004425 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004426 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004427 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004428 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004429 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004430extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004431
4432/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004433 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004434 *
4435 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004436 * returned must have been allocated from the heap memory pool or
4437 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004438 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004439 * If @a ptr is NULL, no operation is performed.
4440 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004441 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004442 *
4443 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004444 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004445 */
4446extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004447
Allan Stephensc98da842016-11-11 15:45:03 -05004448/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004449 * @brief Allocate memory from heap, array style
4450 *
4451 * This routine provides traditional calloc() semantics. Memory is
4452 * allocated from the heap memory pool and zeroed.
4453 *
4454 * @param nmemb Number of elements in the requested array
4455 * @param size Size of each array element (in bytes).
4456 *
4457 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004458 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004459 */
4460extern void *k_calloc(size_t nmemb, size_t size);
4461
Anas Nashif166f5192018-02-25 08:02:36 -06004462/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004463
Benjamin Walshacc68c12017-01-29 18:57:45 -05004464/* polling API - PRIVATE */
4465
Benjamin Walshb0179862017-02-02 16:39:57 -05004466#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004467#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004468#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004469#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004470#endif
4471
Benjamin Walshacc68c12017-01-29 18:57:45 -05004472/* private - types bit positions */
4473enum _poll_types_bits {
4474 /* can be used to ignore an event */
4475 _POLL_TYPE_IGNORE,
4476
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004477 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004478 _POLL_TYPE_SIGNAL,
4479
4480 /* semaphore availability */
4481 _POLL_TYPE_SEM_AVAILABLE,
4482
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004483 /* queue/fifo/lifo data availability */
4484 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004485
4486 _POLL_NUM_TYPES
4487};
4488
Patrik Flykt4344e272019-03-08 14:19:05 -07004489#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004490
4491/* private - states bit positions */
4492enum _poll_states_bits {
4493 /* default state when creating event */
4494 _POLL_STATE_NOT_READY,
4495
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004496 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004497 _POLL_STATE_SIGNALED,
4498
4499 /* semaphore is available */
4500 _POLL_STATE_SEM_AVAILABLE,
4501
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004502 /* data is available to read on queue/fifo/lifo */
4503 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004504
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004505 /* queue/fifo/lifo wait was cancelled */
4506 _POLL_STATE_CANCELLED,
4507
Benjamin Walshacc68c12017-01-29 18:57:45 -05004508 _POLL_NUM_STATES
4509};
4510
Patrik Flykt4344e272019-03-08 14:19:05 -07004511#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004512
4513#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004514 (32 - (0 \
4515 + 8 /* tag */ \
4516 + _POLL_NUM_TYPES \
4517 + _POLL_NUM_STATES \
4518 + 1 /* modes */ \
4519 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004520
Benjamin Walshacc68c12017-01-29 18:57:45 -05004521/* end of polling API - PRIVATE */
4522
4523
4524/**
4525 * @defgroup poll_apis Async polling APIs
4526 * @ingroup kernel_apis
4527 * @{
4528 */
4529
4530/* Public polling API */
4531
4532/* public - values for k_poll_event.type bitfield */
4533#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004534#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4535#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4536#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004537#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004538
4539/* public - polling modes */
4540enum k_poll_modes {
4541 /* polling thread does not take ownership of objects when available */
4542 K_POLL_MODE_NOTIFY_ONLY = 0,
4543
4544 K_POLL_NUM_MODES
4545};
4546
4547/* public - values for k_poll_event.state bitfield */
4548#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004549#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4550#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4551#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004552#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004553#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004554
4555/* public - poll signal object */
4556struct k_poll_signal {
4557 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004558 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004559
4560 /*
4561 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4562 * user resets it to 0.
4563 */
4564 unsigned int signaled;
4565
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004566 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004567 int result;
4568};
4569
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004570#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004571 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004572 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004573 .signaled = 0, \
4574 .result = 0, \
4575 }
4576
4577struct k_poll_event {
4578 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004579 sys_dnode_t _node;
4580
4581 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004582 struct _poller *poller;
4583
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004584 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004585 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004586
Benjamin Walshacc68c12017-01-29 18:57:45 -05004587 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004588 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004589
4590 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004591 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004592
4593 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004594 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004595
4596 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004597 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004598
4599 /* per-type data */
4600 union {
4601 void *obj;
4602 struct k_poll_signal *signal;
4603 struct k_sem *sem;
4604 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004605 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004606 };
4607};
4608
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004609#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004610 { \
4611 .poller = NULL, \
4612 .type = event_type, \
4613 .state = K_POLL_STATE_NOT_READY, \
4614 .mode = event_mode, \
4615 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004616 { .obj = event_obj }, \
4617 }
4618
4619#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4620 event_tag) \
4621 { \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004622 .tag = event_tag, \
Markus Fuchsbe21d3f2019-10-09 21:31:25 +02004623 .type = event_type, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004624 .state = K_POLL_STATE_NOT_READY, \
4625 .mode = event_mode, \
4626 .unused = 0, \
4627 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004628 }
4629
4630/**
4631 * @brief Initialize one struct k_poll_event instance
4632 *
4633 * After this routine is called on a poll event, the event it ready to be
4634 * placed in an event array to be passed to k_poll().
4635 *
4636 * @param event The event to initialize.
4637 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4638 * values. Only values that apply to the same object being polled
4639 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4640 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004641 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004642 * @param obj Kernel object or poll signal.
4643 *
4644 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004645 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004646 */
4647
Kumar Galacc334c72017-04-21 10:55:34 -05004648extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004649 int mode, void *obj);
4650
4651/**
4652 * @brief Wait for one or many of multiple poll events to occur
4653 *
4654 * This routine allows a thread to wait concurrently for one or many of
4655 * multiple poll events to have occurred. Such events can be a kernel object
4656 * being available, like a semaphore, or a poll signal event.
4657 *
4658 * When an event notifies that a kernel object is available, the kernel object
4659 * is not "given" to the thread calling k_poll(): it merely signals the fact
4660 * that the object was available when the k_poll() call was in effect. Also,
4661 * all threads trying to acquire an object the regular way, i.e. by pending on
4662 * the object, have precedence over the thread polling on the object. This
4663 * means that the polling thread will never get the poll event on an object
4664 * until the object becomes available and its pend queue is empty. For this
4665 * reason, the k_poll() call is more effective when the objects being polled
4666 * only have one thread, the polling thread, trying to acquire them.
4667 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004668 * When k_poll() returns 0, the caller should loop on all the events that were
4669 * passed to k_poll() and check the state field for the values that were
4670 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004671 *
4672 * Before being reused for another call to k_poll(), the user has to reset the
4673 * state field to K_POLL_STATE_NOT_READY.
4674 *
Andrew Boie3772f772018-05-07 16:52:57 -07004675 * When called from user mode, a temporary memory allocation is required from
4676 * the caller's resource pool.
4677 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004678 * @param events An array of pointers to events to be polled for.
4679 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004680 * @param timeout Non-negative waiting period for an event to be ready (in
4681 * milliseconds), or one of the special values K_NO_WAIT and
4682 * K_FOREVER.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004683 *
4684 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004685 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004686 * @retval -EINTR Polling has been interrupted, e.g. with
4687 * k_queue_cancel_wait(). All output events are still set and valid,
4688 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4689 * words, -EINTR status means that at least one of output events is
4690 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004691 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4692 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004693 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004694 */
4695
Andrew Boie3772f772018-05-07 16:52:57 -07004696__syscall int k_poll(struct k_poll_event *events, int num_events,
4697 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004698
4699/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004700 * @brief Initialize a poll signal object.
4701 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004702 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004703 *
4704 * @param signal A poll signal.
4705 *
4706 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004707 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004708 */
4709
Andrew Boie3772f772018-05-07 16:52:57 -07004710__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4711
4712/*
4713 * @brief Reset a poll signal object's state to unsignaled.
4714 *
4715 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004716 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004717 */
4718__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4719
Patrik Flykt4344e272019-03-08 14:19:05 -07004720static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004721{
Patrik Flykt24d71432019-03-26 19:57:45 -06004722 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004723}
4724
4725/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004726 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004727 *
4728 * @param signal A poll signal object
4729 * @param signaled An integer buffer which will be written nonzero if the
4730 * object was signaled
4731 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004732 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004733 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004734 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004735 */
4736__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4737 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004738
4739/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004740 * @brief Signal a poll signal object.
4741 *
4742 * This routine makes ready a poll signal, which is basically a poll event of
4743 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4744 * made ready to run. A @a result value can be specified.
4745 *
4746 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004747 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004748 * k_poll_signal_reset(). It thus has to be reset by the user before being
4749 * passed again to k_poll() or k_poll() will consider it being signaled, and
4750 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004751 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004752 * @note The result is stored and the 'signaled' field is set even if
4753 * this function returns an error indicating that an expiring poll was
4754 * not notified. The next k_poll() will detect the missed raise.
4755 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004756 * @param signal A poll signal.
4757 * @param result The value to store in the result field of the signal.
4758 *
4759 * @retval 0 The signal was delivered successfully.
4760 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004761 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004762 */
4763
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004764__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004765
Anas Nashif954d5502018-02-25 08:37:28 -06004766/**
4767 * @internal
4768 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004769extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004770
Anas Nashif166f5192018-02-25 08:02:36 -06004771/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004772
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004773/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004774 * @defgroup cpu_idle_apis CPU Idling APIs
4775 * @ingroup kernel_apis
4776 * @{
4777 */
Anas Nashif30c3cff2019-01-22 08:18:13 -05004778/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004779 * @brief Make the CPU idle.
4780 *
4781 * This function makes the CPU idle until an event wakes it up.
4782 *
4783 * In a regular system, the idle thread should be the only thread responsible
4784 * for making the CPU idle and triggering any type of power management.
4785 * However, in some more constrained systems, such as a single-threaded system,
4786 * the only thread would be responsible for this if needed.
4787 *
4788 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004789 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004790 */
Andrew Boie07525a32019-09-21 16:17:23 -07004791static inline void k_cpu_idle(void)
4792{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004793 arch_cpu_idle();
Andrew Boie07525a32019-09-21 16:17:23 -07004794}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004795
4796/**
4797 * @brief Make the CPU idle in an atomic fashion.
4798 *
4799 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4800 * must be done atomically before making the CPU idle.
4801 *
4802 * @param key Interrupt locking key obtained from irq_lock().
4803 *
4804 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004805 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004806 */
Andrew Boie07525a32019-09-21 16:17:23 -07004807static inline void k_cpu_atomic_idle(unsigned int key)
4808{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004809 arch_cpu_atomic_idle(key);
Andrew Boie07525a32019-09-21 16:17:23 -07004810}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004811
Anas Nashif30c3cff2019-01-22 08:18:13 -05004812/**
4813 * @}
4814 */
Anas Nashif954d5502018-02-25 08:37:28 -06004815
4816/**
4817 * @internal
4818 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004819extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004820
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004821#ifdef ARCH_EXCEPT
Ioannis Glaropoulosdf029232019-10-07 11:24:36 +02004822/* This architecture has direct support for triggering a CPU exception */
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004823#define z_except_reason(reason) ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004824#else
4825
Andrew Boiecdb94d62017-04-18 15:22:05 -07004826/* NOTE: This is the implementation for arches that do not implement
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004827 * ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004828 *
4829 * We won't have a real exception frame to determine the PC value when
4830 * the oops occurred, so print file and line number before we jump into
4831 * the fatal error handler.
4832 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004833#define z_except_reason(reason) do { \
Andrew Boiecdb94d62017-04-18 15:22:05 -07004834 printk("@ %s:%d:\n", __FILE__, __LINE__); \
Andrew Boie56236372019-07-15 15:22:29 -07004835 z_fatal_error(reason, NULL); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004836 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004837
4838#endif /* _ARCH__EXCEPT */
4839
4840/**
4841 * @brief Fatally terminate a thread
4842 *
4843 * This should be called when a thread has encountered an unrecoverable
4844 * runtime condition and needs to terminate. What this ultimately
4845 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004846 * will be called will reason code K_ERR_KERNEL_OOPS.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004847 *
4848 * If this is called from ISR context, the default system fatal error handler
4849 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004850 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004851 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004852#define k_oops() z_except_reason(K_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004853
4854/**
4855 * @brief Fatally terminate the system
4856 *
4857 * This should be called when the Zephyr kernel has encountered an
4858 * unrecoverable runtime condition and needs to terminate. What this ultimately
4859 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004860 * will be called will reason code K_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004861 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004862 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004863#define k_panic() z_except_reason(K_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004864
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004865/*
4866 * private APIs that are utilized by one or more public APIs
4867 */
4868
Stephanos Ioannidis2d746042019-10-25 00:08:21 +09004869/**
4870 * @internal
4871 */
4872extern void z_init_thread_base(struct _thread_base *thread_base,
4873 int priority, u32_t initial_state,
4874 unsigned int options);
4875
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004876#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004877/**
4878 * @internal
4879 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004880extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004881#else
Anas Nashif954d5502018-02-25 08:37:28 -06004882/**
4883 * @internal
4884 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004885#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004886#endif
4887
Anas Nashif954d5502018-02-25 08:37:28 -06004888/**
4889 * @internal
4890 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004891extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004892/**
4893 * @internal
4894 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004895extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004896
Andrew Boiedc5d9352017-06-02 12:56:47 -07004897/* arch/cpu.h may declare an architecture or platform-specific macro
4898 * for properly declaring stacks, compatible with MMU/MPU constraints if
4899 * enabled
4900 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004901
4902/**
4903 * @brief Obtain an extern reference to a stack
4904 *
4905 * This macro properly brings the symbol of a thread stack declared
4906 * elsewhere into scope.
4907 *
4908 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004909 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004910 */
4911#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4912
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004913#ifdef ARCH_THREAD_STACK_DEFINE
4914#define K_THREAD_STACK_DEFINE(sym, size) ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004915#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004916 ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4917#define K_THREAD_STACK_LEN(size) ARCH_THREAD_STACK_LEN(size)
4918#define K_THREAD_STACK_MEMBER(sym, size) ARCH_THREAD_STACK_MEMBER(sym, size)
4919#define K_THREAD_STACK_SIZEOF(sym) ARCH_THREAD_STACK_SIZEOF(sym)
4920#define K_THREAD_STACK_RESERVED ARCH_THREAD_STACK_RESERVED
Andrew Boie4e5c0932019-04-04 12:05:28 -07004921static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004922{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004923 return ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07004924}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004925#else
4926/**
4927 * @brief Declare a toplevel thread stack memory region
4928 *
4929 * This declares a region of memory suitable for use as a thread's stack.
4930 *
4931 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4932 * 'noinit' section so that it isn't zeroed at boot
4933 *
Andrew Boie507852a2017-07-25 18:47:07 -07004934 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004935 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07004936 * inside needs to be examined, examine thread->stack_info for the associated
4937 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004938 *
4939 * It is legal to precede this definition with the 'static' keyword.
4940 *
4941 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4942 * parameter of k_thread_create(), it may not be the same as the
4943 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4944 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004945 * Some arches may round the size of the usable stack region up to satisfy
4946 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4947 * size.
4948 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004949 * @param sym Thread stack symbol name
4950 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004951 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004952 */
4953#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004954 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004955
4956/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304957 * @brief Calculate size of stacks to be allocated in a stack array
4958 *
4959 * This macro calculates the size to be allocated for the stacks
4960 * inside a stack array. It accepts the indicated "size" as a parameter
4961 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4962 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4963 *
4964 * @param size Size of the stack memory region
4965 * @req K-TSTACK-001
4966 */
4967#define K_THREAD_STACK_LEN(size) (size)
4968
4969/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004970 * @brief Declare a toplevel array of thread stack memory regions
4971 *
4972 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4973 * definition for additional details and constraints.
4974 *
4975 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4976 * 'noinit' section so that it isn't zeroed at boot
4977 *
4978 * @param sym Thread stack symbol name
4979 * @param nmemb Number of stacks to declare
4980 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004981 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004982 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004983#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004984 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304985 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004986
4987/**
4988 * @brief Declare an embedded stack memory region
4989 *
4990 * Used for stacks embedded within other data structures. Use is highly
4991 * discouraged but in some cases necessary. For memory protection scenarios,
4992 * it is very important that any RAM preceding this member not be writable
4993 * by threads else a stack overflow will lead to silent corruption. In other
4994 * words, the containing data structure should live in RAM owned by the kernel.
4995 *
4996 * @param sym Thread stack symbol name
4997 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004998 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004999 */
5000#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07005001 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07005002
5003/**
5004 * @brief Return the size in bytes of a stack memory region
5005 *
5006 * Convenience macro for passing the desired stack size to k_thread_create()
5007 * since the underlying implementation may actually create something larger
5008 * (for instance a guard area).
5009 *
Andrew Boiee2d77912018-05-30 09:45:35 -07005010 * The value returned here is not guaranteed to match the 'size' parameter
5011 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005012 *
5013 * @param sym Stack memory symbol
5014 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005015 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005016 */
5017#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
5018
Andrew Boie575abc02019-03-19 10:42:24 -07005019
5020/**
5021 * @brief Indicate how much additional memory is reserved for stack objects
5022 *
5023 * Any given stack declaration may have additional memory in it for guard
5024 * areas or supervisor mode stacks. This macro indicates how much space
5025 * is reserved for this. The memory reserved may not be contiguous within
5026 * the stack object, and does not account for additional space used due to
5027 * enforce alignment.
5028 */
5029#define K_THREAD_STACK_RESERVED 0
5030
Andrew Boiedc5d9352017-06-02 12:56:47 -07005031/**
5032 * @brief Get a pointer to the physical stack buffer
5033 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07005034 * This macro is deprecated. If a stack buffer needs to be examined, the
5035 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005036 *
5037 * @param sym Declared stack symbol name
5038 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005039 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005040 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07005041static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07005042{
5043 return (char *)sym;
5044}
Andrew Boiedc5d9352017-06-02 12:56:47 -07005045
5046#endif /* _ARCH_DECLARE_STACK */
5047
Chunlin Hane9c97022017-07-07 20:29:30 +08005048/**
5049 * @defgroup mem_domain_apis Memory domain APIs
5050 * @ingroup kernel_apis
5051 * @{
5052 */
5053
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005054/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005055 * @def K_MEM_PARTITION_DEFINE
5056 * @brief Used to declare a memory partition
5057 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005058 */
5059#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
5060#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
5061 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08005062 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005063 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005064#else
5065#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08005066 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005067 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005068#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
5069
Chunlin Hane9c97022017-07-07 20:29:30 +08005070/* memory partition */
5071struct k_mem_partition {
5072 /* start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005073 uintptr_t start;
Chunlin Hane9c97022017-07-07 20:29:30 +08005074 /* size of memory partition */
5075 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005076#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08005077 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305078 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005079#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08005080};
5081
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02005082/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05305083 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005084struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305085#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08005086 /* partitions in the domain */
5087 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305088#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08005089 /* domain q */
5090 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08005091 /* number of partitions in the domain */
5092 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08005093};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305094
Chunlin Hane9c97022017-07-07 20:29:30 +08005095
5096/**
5097 * @brief Initialize a memory domain.
5098 *
5099 * Initialize a memory domain with given name and memory partitions.
5100 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005101 * See documentation for k_mem_domain_add_partition() for details about
5102 * partition constraints.
5103 *
Chunlin Hane9c97022017-07-07 20:29:30 +08005104 * @param domain The memory domain to be initialized.
5105 * @param num_parts The number of array items of "parts" parameter.
5106 * @param parts An array of pointers to the memory partitions. Can be NULL
5107 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005108 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005109 */
Leandro Pereira08de6582018-02-28 14:22:57 -08005110extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08005111 struct k_mem_partition *parts[]);
5112/**
5113 * @brief Destroy a memory domain.
5114 *
5115 * Destroy a memory domain.
5116 *
5117 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005118 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005119 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005120extern void k_mem_domain_destroy(struct k_mem_domain *domain);
5121
5122/**
5123 * @brief Add a memory partition into a memory domain.
5124 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005125 * Add a memory partition into a memory domain. Partitions must conform to
5126 * the following constraints:
5127 *
5128 * - Partition bounds must be within system RAM boundaries on MMU-based
5129 * systems.
5130 * - Partitions in the same memory domain may not overlap each other.
5131 * - Partitions must not be defined which expose private kernel
5132 * data structures or kernel objects.
5133 * - The starting address alignment, and the partition size must conform to
5134 * the constraints of the underlying memory management hardware, which
5135 * varies per architecture.
5136 * - Memory domain partitions are only intended to control access to memory
5137 * from user mode threads.
5138 *
5139 * Violating these constraints may lead to CPU exceptions or undefined
5140 * behavior.
Chunlin Hane9c97022017-07-07 20:29:30 +08005141 *
5142 * @param domain The memory domain to be added a memory partition.
5143 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005144 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005145 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005146extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
5147 struct k_mem_partition *part);
5148
5149/**
5150 * @brief Remove a memory partition from a memory domain.
5151 *
5152 * Remove a memory partition from a memory domain.
5153 *
5154 * @param domain The memory domain to be removed a memory partition.
5155 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005156 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005157 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005158extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
5159 struct k_mem_partition *part);
5160
5161/**
5162 * @brief Add a thread into a memory domain.
5163 *
5164 * Add a thread into a memory domain.
5165 *
5166 * @param domain The memory domain that the thread is going to be added into.
5167 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005168 *
5169 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005170 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005171extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
5172 k_tid_t thread);
5173
5174/**
5175 * @brief Remove a thread from its memory domain.
5176 *
5177 * Remove a thread from its memory domain.
5178 *
5179 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005180 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005181 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005182extern void k_mem_domain_remove_thread(k_tid_t thread);
5183
Anas Nashif166f5192018-02-25 08:02:36 -06005184/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08005185
Andrew Boie756f9072017-10-10 16:01:49 -07005186/**
5187 * @brief Emit a character buffer to the console device
5188 *
5189 * @param c String of characters to print
5190 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005191 *
5192 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07005193 */
5194__syscall void k_str_out(char *c, size_t n);
5195
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005196/**
5197 * @brief Disable preservation of floating point context information.
5198 *
5199 * This routine informs the kernel that the specified thread
5200 * will no longer be using the floating point registers.
5201 *
5202 * @warning
5203 * Some architectures apply restrictions on how the disabling of floating
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005204 * point preservation may be requested, see arch_float_disable.
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005205 *
5206 * @warning
5207 * This routine should only be used to disable floating point support for
5208 * a thread that currently has such support enabled.
5209 *
5210 * @param thread ID of thread.
5211 *
5212 * @retval 0 On success.
5213 * @retval -ENOSYS If the floating point disabling is not implemented.
5214 * -EINVAL If the floating point disabling could not be performed.
5215 */
5216__syscall int k_float_disable(struct k_thread *thread);
5217
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005218#ifdef __cplusplus
5219}
5220#endif
5221
Anas Nashif10291a02019-06-25 12:25:12 -04005222#include <debug/tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07005223#include <syscalls/kernel.h>
5224
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05005225#endif /* !_ASMLANGUAGE */
5226
Flavio Ceolin67ca1762018-09-14 10:43:44 -07005227#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */