blob: d809305e42df4370dcd37e4bc81b058cce6f9323 [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};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700371typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700372
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700373/**
374 * @typedef k_thread_entry_t
375 * @brief Thread entry point function type.
376 *
377 * A thread's entry point function is invoked when the thread starts executing.
378 * Up to 3 argument values can be passed to the function.
379 *
380 * The thread terminates execution permanently if the entry point function
381 * returns. The thread is responsible for releasing any shared resources
382 * it may own (such as mutexes and dynamically allocated memory), prior to
383 * returning.
384 *
385 * @param p1 First argument.
386 * @param p2 Second argument.
387 * @param p3 Third argument.
388 *
389 * @return N/A
390 */
391typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700392
393#ifdef CONFIG_THREAD_MONITOR
394struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700395 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700396 void *parameter1;
397 void *parameter2;
398 void *parameter3;
399};
400#endif
401
402/* can be used for creating 'dummy' threads, e.g. for pending on objects */
403struct _thread_base {
404
405 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700406 union {
Peter A. Bigot82ad0d22019-01-03 23:49:28 -0600407 sys_dnode_t qnode_dlist;
Andy Ross1acd8c22018-05-03 14:51:49 -0700408 struct rbnode qnode_rb;
409 };
410
Andy Ross1acd8c22018-05-03 14:51:49 -0700411 /* wait queue on which the thread is pended (needed only for
412 * trees, not dumb lists)
413 */
414 _wait_q_t *pended_on;
Andrew Boie73abd322017-04-04 13:19:13 -0700415
416 /* user facing 'thread options'; values defined in include/kernel.h */
417 u8_t user_options;
418
419 /* thread state */
420 u8_t thread_state;
421
422 /*
423 * scheduler lock count and thread priority
424 *
425 * These two fields control the preemptibility of a thread.
426 *
427 * When the scheduler is locked, sched_locked is decremented, which
428 * means that the scheduler is locked for values from 0xff to 0x01. A
429 * thread is coop if its prio is negative, thus 0x80 to 0xff when
430 * looked at the value as unsigned.
431 *
432 * By putting them end-to-end, this means that a thread is
433 * non-preemptible if the bundled value is greater than or equal to
434 * 0x0080.
435 */
436 union {
437 struct {
438#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
439 u8_t sched_locked;
440 s8_t prio;
441#else /* LITTLE and PDP */
442 s8_t prio;
443 u8_t sched_locked;
444#endif
445 };
446 u16_t preempt;
447 };
448
Andy Ross4a2e50f2018-05-15 11:06:25 -0700449#ifdef CONFIG_SCHED_DEADLINE
450 int prio_deadline;
451#endif
452
Andy Ross1acd8c22018-05-03 14:51:49 -0700453 u32_t order_key;
454
Andy Ross2724fd12018-01-29 14:55:20 -0800455#ifdef CONFIG_SMP
456 /* True for the per-CPU idle threads */
457 u8_t is_idle;
458
Andy Ross2724fd12018-01-29 14:55:20 -0800459 /* CPU index on which thread was last run */
460 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700461
462 /* Recursive count of irq_lock() calls */
463 u8_t global_lock_count;
Andy Rossab46b1b2019-01-30 15:00:42 -0800464
465#endif
466
467#ifdef CONFIG_SCHED_CPU_MASK
468 /* "May run on" bits for each CPU */
469 u8_t cpu_mask;
Andy Ross2724fd12018-01-29 14:55:20 -0800470#endif
471
Andrew Boie73abd322017-04-04 13:19:13 -0700472 /* data returned by APIs */
473 void *swap_data;
474
475#ifdef CONFIG_SYS_CLOCK_EXISTS
476 /* this thread's entry in a timeout queue */
477 struct _timeout timeout;
478#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700479};
480
481typedef struct _thread_base _thread_base_t;
482
483#if defined(CONFIG_THREAD_STACK_INFO)
484/* Contains the stack information of a thread */
485struct _thread_stack_info {
Andrew Boie4e5c0932019-04-04 12:05:28 -0700486 /* Stack start - Represents the start address of the thread-writable
487 * stack area.
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700488 */
Nicolas Pitre58d839b2019-05-21 11:32:21 -0400489 uintptr_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700490
491 /* Stack Size - Thread writable stack buffer size. Represents
492 * the size of the actual area, starting from the start member,
493 * that should be writable by the thread
494 */
Andrew Boie73abd322017-04-04 13:19:13 -0700495 u32_t size;
496};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700497
498typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700499#endif /* CONFIG_THREAD_STACK_INFO */
500
Chunlin Hane9c97022017-07-07 20:29:30 +0800501#if defined(CONFIG_USERSPACE)
502struct _mem_domain_info {
503 /* memory domain queue node */
504 sys_dnode_t mem_domain_q_node;
505 /* memory domain of the thread */
506 struct k_mem_domain *mem_domain;
507};
508
509#endif /* CONFIG_USERSPACE */
510
Daniel Leungfc182432018-08-16 15:42:28 -0700511#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
512struct _thread_userspace_local_data {
513 int errno_var;
514};
515#endif
516
Anas Nashifce78d162018-05-24 12:43:11 -0500517/**
518 * @ingroup thread_apis
519 * Thread Structure
520 */
Andrew Boie73abd322017-04-04 13:19:13 -0700521struct k_thread {
522
523 struct _thread_base base;
524
Anas Nashifce78d162018-05-24 12:43:11 -0500525 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700526 struct _callee_saved callee_saved;
527
Anas Nashifce78d162018-05-24 12:43:11 -0500528 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700529 void *init_data;
530
Anas Nashifce78d162018-05-24 12:43:11 -0500531 /**
532 * abort function
533 * @req K-THREAD-002
534 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700535 void (*fn_abort)(void);
536
537#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500538 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700539 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700540
Anas Nashifce78d162018-05-24 12:43:11 -0500541 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700542 struct k_thread *next_thread;
543#endif
544
Anas Nashif57554052018-03-03 02:31:05 -0600545#if defined(CONFIG_THREAD_NAME)
546 /* Thread name */
Andrew Boie38129ce2019-06-25 08:54:37 -0700547 char name[CONFIG_THREAD_MAX_NAME_LEN];
Anas Nashif57554052018-03-03 02:31:05 -0600548#endif
549
Andrew Boie73abd322017-04-04 13:19:13 -0700550#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500551 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700552 void *custom_data;
553#endif
554
Daniel Leungfc182432018-08-16 15:42:28 -0700555#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
556 struct _thread_userspace_local_data *userspace_local_data;
557#endif
558
Andrew Boie73abd322017-04-04 13:19:13 -0700559#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700560#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500561 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700562 int errno_var;
563#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700564#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700565
566#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500567 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700568 struct _thread_stack_info stack_info;
569#endif /* CONFIG_THREAD_STACK_INFO */
570
Chunlin Hane9c97022017-07-07 20:29:30 +0800571#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500572 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800573 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500574 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700575 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800576#endif /* CONFIG_USERSPACE */
577
Andy Ross042d8ec2017-12-09 08:37:20 -0800578#if defined(CONFIG_USE_SWITCH)
579 /* When using __switch() a few previously arch-specific items
580 * become part of the core OS
581 */
582
Patrik Flykt4344e272019-03-08 14:19:05 -0700583 /** z_swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800584 int swap_retval;
585
Andrew Boie4f77c2a2019-11-07 12:43:29 -0800586 /** Context handle returned via arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800587 void *switch_handle;
588#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500589 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700590 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800591
Anas Nashifce78d162018-05-24 12:43:11 -0500592 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700593 struct _thread_arch arch;
594};
595
596typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400597typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400598
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400599enum execution_context_types {
600 K_ISR = 0,
601 K_COOP_THREAD,
602 K_PREEMPT_THREAD,
603};
604
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400605/**
Anas Nashif4bcb2942019-01-23 23:06:29 -0500606 * @addtogroup thread_apis
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100607 * @{
608 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530609typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
610 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100611
612/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530613 * @brief Iterate over all the threads in the system.
614 *
615 * This routine iterates over all the threads in the system and
616 * calls the user_cb function for each thread.
617 *
618 * @param user_cb Pointer to the user callback function.
619 * @param user_data Pointer to user data.
620 *
621 * @note CONFIG_THREAD_MONITOR must be set for this function
622 * to be effective. Also this API uses irq_lock to protect the
623 * _kernel.threads list which means creation of new threads and
624 * terminations of existing threads are blocked until this
625 * API returns.
626 *
627 * @return N/A
628 */
629extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
630
Anas Nashif166f5192018-02-25 08:02:36 -0600631/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100632
633/**
Allan Stephensc98da842016-11-11 15:45:03 -0500634 * @defgroup thread_apis Thread APIs
635 * @ingroup kernel_apis
636 * @{
637 */
638
Benjamin Walshed240f22017-01-22 13:05:08 -0500639#endif /* !_ASMLANGUAGE */
640
641
642/*
643 * Thread user options. May be needed by assembly code. Common part uses low
644 * bits, arch-specific use high bits.
645 */
646
Anas Nashifa541e932018-05-24 11:19:16 -0500647/**
648 * @brief system thread that must not abort
649 * @req K-THREAD-000
650 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700651#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500652
653#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500654/**
655 * @brief thread uses floating point registers
656 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700657#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500658#endif
659
Anas Nashifa541e932018-05-24 11:19:16 -0500660/**
661 * @brief user mode thread
662 *
663 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700664 * has additional restrictions
665 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700666#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700667
Anas Nashifa541e932018-05-24 11:19:16 -0500668/**
669 * @brief Inherit Permissions
670 *
671 * @details
672 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700673 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
674 * is not enabled.
675 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700676#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700677
Benjamin Walshed240f22017-01-22 13:05:08 -0500678#ifdef CONFIG_X86
679/* x86 Bitmask definitions for threads user options */
680
681#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
682/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700683#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500684#endif
685#endif
686
687/* end - thread options */
688
689#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400690/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700691 * @brief Create a thread.
692 *
693 * This routine initializes a thread, then schedules it for execution.
694 *
695 * The new thread may be scheduled for immediate execution or a delayed start.
696 * If the newly spawned thread does not have a delayed start the kernel
697 * scheduler may preempt the current thread to allow the new thread to
698 * execute.
699 *
700 * Thread options are architecture-specific, and can include K_ESSENTIAL,
701 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
702 * them using "|" (the logical OR operator).
703 *
704 * Historically, users often would use the beginning of the stack memory region
705 * to store the struct k_thread data, although corruption will occur if the
706 * stack overflows this region and stack protection features may not detect this
707 * situation.
708 *
709 * @param new_thread Pointer to uninitialized struct k_thread
710 * @param stack Pointer to the stack space.
711 * @param stack_size Stack size in bytes.
712 * @param entry Thread entry function.
713 * @param p1 1st entry point parameter.
714 * @param p2 2nd entry point parameter.
715 * @param p3 3rd entry point parameter.
716 * @param prio Thread priority.
717 * @param options Thread options.
718 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
719 *
720 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400721 *
722 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700723 */
Andrew Boie662c3452017-10-02 10:51:18 -0700724__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700725 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700726 size_t stack_size,
727 k_thread_entry_t entry,
728 void *p1, void *p2, void *p3,
729 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700730
Andrew Boie3f091b52017-08-30 14:34:14 -0700731/**
732 * @brief Drop a thread's privileges permanently to user mode
733 *
734 * @param entry Function to start executing from
735 * @param p1 1st entry point parameter
736 * @param p2 2nd entry point parameter
737 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400738 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700739 */
740extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
741 void *p1, void *p2,
742 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700743
Andrew Boied26cf2d2017-03-30 13:07:02 -0700744/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530745 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700746 *
747 * This is a convenience function. For the provided thread, grant access to
748 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700749 *
750 * The thread object must be initialized (i.e. running). The objects don't
751 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530752 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700753 *
754 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530755 * @param ... list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400756 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700757 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530758#define k_thread_access_grant(thread, ...) \
759 FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700760
761/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700762 * @brief Assign a resource memory pool to a thread
763 *
764 * By default, threads have no resource pool assigned unless their parent
765 * thread has a resource pool, in which case it is inherited. Multiple
766 * threads may be assigned to the same memory pool.
767 *
768 * Changing a thread's resource pool will not migrate allocations from the
769 * previous pool.
770 *
771 * @param thread Target thread to assign a memory pool for resource requests,
772 * or NULL if the thread should no longer have a memory pool.
773 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400774 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700775 */
776static inline void k_thread_resource_pool_assign(struct k_thread *thread,
777 struct k_mem_pool *pool)
778{
779 thread->resource_pool = pool;
780}
781
782#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
783/**
784 * @brief Assign the system heap as a thread's resource pool
785 *
786 * Similar to k_thread_resource_pool_assign(), but the thread will use
787 * the kernel heap to draw memory.
788 *
789 * Use with caution, as a malicious thread could perform DoS attacks on the
790 * kernel heap.
791 *
792 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400793 *
794 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700795 */
796void k_thread_system_pool_assign(struct k_thread *thread);
797#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
798
799/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500800 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400801 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700802 * This routine puts the current thread to sleep for @a duration milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400803 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700804 * @param ms Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400805 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200806 * @return Zero if the requested time has elapsed or the number of milliseconds
807 * left to sleep, if thread was woken up by \ref k_wakeup call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400808 */
Charles E. Yousea5678312019-05-09 16:46:46 -0700809__syscall s32_t k_sleep(s32_t ms);
810
811/**
812 * @brief Put the current thread to sleep with microsecond resolution.
813 *
814 * This function is unlikely to work as expected without kernel tuning.
815 * In particular, because the lower bound on the duration of a sleep is
816 * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted
817 * to achieve the resolution desired. The implications of doing this must
818 * be understood before attempting to use k_usleep(). Use with caution.
819 *
820 * @param us Number of microseconds to sleep.
821 *
822 * @return Zero if the requested time has elapsed or the number of microseconds
823 * left to sleep, if thread was woken up by \ref k_wakeup call.
824 */
825__syscall s32_t k_usleep(s32_t us);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400826
827/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500828 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829 *
830 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500831 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400833 * @return N/A
834 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800835__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836
837/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500838 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500840 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400841 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500842 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400843 *
844 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400845 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400846 */
Andrew Boie468190a2017-09-29 14:00:48 -0700847__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848
849/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500850 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500852 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500854 * If @a thread is not currently sleeping, the routine has no effect.
855 *
856 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400857 *
858 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400859 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400860 */
Andrew Boie468190a2017-09-29 14:00:48 -0700861__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400862
863/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500864 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500866 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400867 *
868 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400869 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700870__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400871
872/**
Allan Stephensc98da842016-11-11 15:45:03 -0500873 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500875 * This routine permanently stops execution of @a thread. The thread is taken
876 * off all kernel queues it is part of (i.e. the ready queue, the timeout
877 * queue, or a kernel object wait queue). However, any kernel resources the
878 * thread might currently own (such as mutexes or memory blocks) are not
879 * released. It is the responsibility of the caller of this routine to ensure
880 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500882 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400883 *
884 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400885 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400886 */
Andrew Boie468190a2017-09-29 14:00:48 -0700887__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400888
Andrew Boie7d627c52017-08-30 11:01:56 -0700889
890/**
891 * @brief Start an inactive thread
892 *
893 * If a thread was created with K_FOREVER in the delay parameter, it will
894 * not be added to the scheduling queue until this function is called
895 * on it.
896 *
897 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400898 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700899 */
Andrew Boie468190a2017-09-29 14:00:48 -0700900__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700901
Allan Stephensc98da842016-11-11 15:45:03 -0500902/**
903 * @cond INTERNAL_HIDDEN
904 */
905
Benjamin Walshd211a522016-12-06 11:44:01 -0500906/* timeout has timed out and is not on _timeout_q anymore */
907#define _EXPIRED (-2)
908
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400909struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700910 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700911 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400912 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700913 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500914 void *init_p1;
915 void *init_p2;
916 void *init_p3;
917 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500918 u32_t init_options;
919 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500920 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600921 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400922};
923
Andrew Boied26cf2d2017-03-30 13:07:02 -0700924#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400925 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600926 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500927 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700928 .init_thread = (thread), \
929 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500930 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700931 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400932 .init_p1 = (void *)p1, \
933 .init_p2 = (void *)p2, \
934 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500935 .init_prio = (prio), \
936 .init_options = (options), \
937 .init_delay = (delay), \
938 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600939 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400940 }
941
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400942/**
Allan Stephensc98da842016-11-11 15:45:03 -0500943 * INTERNAL_HIDDEN @endcond
944 */
945
946/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500947 * @brief Statically define and initialize a thread.
948 *
949 * The thread may be scheduled for immediate execution or a delayed start.
950 *
951 * Thread options are architecture-specific, and can include K_ESSENTIAL,
952 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
953 * them using "|" (the logical OR operator).
954 *
955 * The ID of the thread can be accessed using:
956 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500957 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500958 *
959 * @param name Name of the thread.
960 * @param stack_size Stack size in bytes.
961 * @param entry Thread entry function.
962 * @param p1 1st entry point parameter.
963 * @param p2 2nd entry point parameter.
964 * @param p3 3rd entry point parameter.
965 * @param prio Thread priority.
966 * @param options Thread options.
967 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400968 *
Anas Nashif47420d02018-05-24 14:20:56 -0400969 * @req K-THREAD-010
970 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400971 * @internal It has been observed that the x86 compiler by default aligns
972 * these _static_thread_data structures to 32-byte boundaries, thereby
973 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400974 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400975 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500976#define K_THREAD_DEFINE(name, stack_size, \
977 entry, p1, p2, p3, \
978 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700979 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400980 struct k_thread _k_thread_obj_##name; \
981 Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\
Andrew Boied26cf2d2017-03-30 13:07:02 -0700982 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
983 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500984 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -0600985 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700986 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400987
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400988/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500989 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400990 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500991 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500993 * @param thread ID of thread whose priority is needed.
994 *
995 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400996 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400997 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700998__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400999
1000/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001001 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001002 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001003 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001004 *
1005 * Rescheduling can occur immediately depending on the priority @a thread is
1006 * set to:
1007 *
1008 * - If its priority is raised above the priority of the caller of this
1009 * function, and the caller is preemptible, @a thread will be scheduled in.
1010 *
1011 * - If the caller operates on itself, it lowers its priority below that of
1012 * other threads in the system, and the caller is preemptible, the thread of
1013 * highest priority will be scheduled in.
1014 *
1015 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1016 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1017 * highest priority.
1018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001019 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001020 * @param prio New priority.
1021 *
1022 * @warning Changing the priority of a thread currently involved in mutex
1023 * priority inheritance may result in undefined behavior.
1024 *
1025 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001026 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001027 */
Andrew Boie468190a2017-09-29 14:00:48 -07001028__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001029
Andy Ross4a2e50f2018-05-15 11:06:25 -07001030
1031#ifdef CONFIG_SCHED_DEADLINE
1032/**
1033 * @brief Set deadline expiration time for scheduler
1034 *
1035 * This sets the "deadline" expiration as a time delta from the
1036 * current time, in the same units used by k_cycle_get_32(). The
1037 * scheduler (when deadline scheduling is enabled) will choose the
1038 * next expiring thread when selecting between threads at the same
1039 * static priority. Threads at different priorities will be scheduled
1040 * according to their static priority.
1041 *
1042 * @note Deadlines that are negative (i.e. in the past) are still seen
1043 * as higher priority than others, even if the thread has "finished"
1044 * its work. If you don't want it scheduled anymore, you have to
1045 * reset the deadline into the future, block/pend the thread, or
1046 * modify its priority with k_thread_priority_set().
1047 *
1048 * @note Despite the API naming, the scheduler makes no guarantees the
1049 * the thread WILL be scheduled within that deadline, nor does it take
1050 * extra metadata (like e.g. the "runtime" and "period" parameters in
1051 * Linux sched_setattr()) that allows the kernel to validate the
1052 * scheduling for achievability. Such features could be implemented
1053 * above this call, which is simply input to the priority selection
1054 * logic.
1055 *
Anas Nashif240c5162019-06-10 12:25:50 -04001056 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001057 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001058 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1059 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001060 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001061 *
Andy Ross4a2e50f2018-05-15 11:06:25 -07001062 * @param thread A thread on which to set the deadline
1063 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001064 *
1065 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001066 */
1067__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1068#endif
1069
Andy Rossab46b1b2019-01-30 15:00:42 -08001070#ifdef CONFIG_SCHED_CPU_MASK
1071/**
1072 * @brief Sets all CPU enable masks to zero
1073 *
1074 * After this returns, the thread will no longer be schedulable on any
1075 * CPUs. The thread must not be currently runnable.
1076 *
Anas Nashif240c5162019-06-10 12:25:50 -04001077 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001078 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001079 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1080 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001081 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001082 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001083 * @param thread Thread to operate upon
1084 * @return Zero on success, otherwise error code
1085 */
1086int k_thread_cpu_mask_clear(k_tid_t thread);
1087
1088/**
1089 * @brief Sets all CPU enable masks to one
1090 *
1091 * After this returns, the thread will be schedulable on any CPU. The
1092 * thread must not be currently runnable.
1093 *
Anas Nashif240c5162019-06-10 12:25:50 -04001094 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001095 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001096 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1097 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001098 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001099 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001100 * @param thread Thread to operate upon
1101 * @return Zero on success, otherwise error code
1102 */
1103int k_thread_cpu_mask_enable_all(k_tid_t thread);
1104
1105/**
1106 * @brief Enable thread to run on specified CPU
1107 *
1108 * The thread must not be currently runnable.
1109 *
Anas Nashif240c5162019-06-10 12:25:50 -04001110 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001111 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001112 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1113 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001114 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001115 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001116 * @param thread Thread to operate upon
1117 * @param cpu CPU index
1118 * @return Zero on success, otherwise error code
1119 */
1120int k_thread_cpu_mask_enable(k_tid_t thread, int cpu);
1121
1122/**
1123 * @brief Prevent thread to run on specified CPU
1124 *
1125 * The thread must not be currently runnable.
1126 *
Anas Nashif240c5162019-06-10 12:25:50 -04001127 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001128 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001129 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1130 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001131 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001132 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001133 * @param thread Thread to operate upon
1134 * @param cpu CPU index
1135 * @return Zero on success, otherwise error code
1136 */
1137int k_thread_cpu_mask_disable(k_tid_t thread, int cpu);
1138#endif
1139
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001140/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001141 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001142 *
Andy Ross50d09422019-11-19 11:20:07 -08001143 * This routine prevents the kernel scheduler from making @a thread
1144 * the current thread. All other internal operations on @a thread are
1145 * still performed; for example, kernel objects it is waiting on are
1146 * still handed to it. Note that any existing timeouts
1147 * (e.g. k_sleep(), or a timeout argument to k_sem_take() et. al.)
1148 * will be canceled. On resume, the thread will begin running
1149 * immediately and return from the blocked call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001150 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001151 * If @a thread is already suspended, the routine has no effect.
1152 *
1153 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001154 *
1155 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001156 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001157 */
Andrew Boie468190a2017-09-29 14:00:48 -07001158__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001159
1160/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001161 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001162 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001163 * This routine allows the kernel scheduler to make @a thread the current
1164 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001165 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001166 * If @a thread is not currently suspended, the routine has no effect.
1167 *
1168 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001169 *
1170 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001171 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001172 */
Andrew Boie468190a2017-09-29 14:00:48 -07001173__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001174
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001175/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001176 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001177 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001178 * This routine specifies how the scheduler will perform time slicing of
1179 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001181 * To enable time slicing, @a slice must be non-zero. The scheduler
1182 * ensures that no thread runs for more than the specified time limit
1183 * before other threads of that priority are given a chance to execute.
1184 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001185 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001186 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001187 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001188 * execute. Once the scheduler selects a thread for execution, there is no
1189 * minimum guaranteed time the thread will execute before threads of greater or
1190 * equal priority are scheduled.
1191 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001192 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001193 * for execution, this routine has no effect; the thread is immediately
1194 * rescheduled after the slice period expires.
1195 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001196 * To disable timeslicing, set both @a slice and @a prio to zero.
1197 *
1198 * @param slice Maximum time slice length (in milliseconds).
1199 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001200 *
1201 * @return N/A
1202 */
Kumar Galacc334c72017-04-21 10:55:34 -05001203extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001204
Anas Nashif166f5192018-02-25 08:02:36 -06001205/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001206
1207/**
1208 * @addtogroup isr_apis
1209 * @{
1210 */
1211
1212/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001213 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001214 *
Allan Stephensc98da842016-11-11 15:45:03 -05001215 * This routine allows the caller to customize its actions, depending on
1216 * whether it is a thread or an ISR.
1217 *
1218 * @note Can be called by ISRs.
1219 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001220 * @return false if invoked by a thread.
1221 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001222 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001223extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001224
Benjamin Walsh445830d2016-11-10 15:54:27 -05001225/**
1226 * @brief Determine if code is running in a preemptible thread.
1227 *
Allan Stephensc98da842016-11-11 15:45:03 -05001228 * This routine allows the caller to customize its actions, depending on
1229 * whether it can be preempted by another thread. The routine returns a 'true'
1230 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001231 *
Allan Stephensc98da842016-11-11 15:45:03 -05001232 * - The code is running in a thread, not at ISR.
1233 * - The thread's priority is in the preemptible range.
1234 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001235 *
Allan Stephensc98da842016-11-11 15:45:03 -05001236 * @note Can be called by ISRs.
1237 *
1238 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001239 * @return Non-zero if invoked by a preemptible thread.
1240 */
Andrew Boie468190a2017-09-29 14:00:48 -07001241__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001242
Allan Stephensc98da842016-11-11 15:45:03 -05001243/**
Anas Nashif166f5192018-02-25 08:02:36 -06001244 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001245 */
1246
1247/**
1248 * @addtogroup thread_apis
1249 * @{
1250 */
1251
1252/**
1253 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001254 *
Allan Stephensc98da842016-11-11 15:45:03 -05001255 * This routine prevents the current thread from being preempted by another
1256 * thread by instructing the scheduler to treat it as a cooperative thread.
1257 * If the thread subsequently performs an operation that makes it unready,
1258 * it will be context switched out in the normal manner. When the thread
1259 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001260 *
Allan Stephensc98da842016-11-11 15:45:03 -05001261 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001262 *
Allan Stephensc98da842016-11-11 15:45:03 -05001263 * @note k_sched_lock() and k_sched_unlock() should normally be used
1264 * when the operation being performed can be safely interrupted by ISRs.
1265 * However, if the amount of processing involved is very small, better
1266 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001267 *
1268 * @return N/A
1269 */
1270extern void k_sched_lock(void);
1271
Allan Stephensc98da842016-11-11 15:45:03 -05001272/**
1273 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001274 *
Allan Stephensc98da842016-11-11 15:45:03 -05001275 * This routine reverses the effect of a previous call to k_sched_lock().
1276 * A thread must call the routine once for each time it called k_sched_lock()
1277 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001278 *
1279 * @return N/A
1280 */
1281extern void k_sched_unlock(void);
1282
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001283/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001284 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001285 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001286 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001287 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001288 * Custom data is not used by the kernel itself, and is freely available
1289 * for a thread to use as it sees fit. It can be used as a framework
1290 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001291 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001292 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001293 *
1294 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001295 *
1296 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001297 */
Andrew Boie468190a2017-09-29 14:00:48 -07001298__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001299
1300/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001301 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001302 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001303 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001304 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001305 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001306 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001307 */
Andrew Boie468190a2017-09-29 14:00:48 -07001308__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001309
1310/**
Anas Nashif57554052018-03-03 02:31:05 -06001311 * @brief Set current thread name
1312 *
1313 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1314 * tracing and debugging.
1315 *
Andrew Boie38129ce2019-06-25 08:54:37 -07001316 * @param thread_id Thread to set name, or NULL to set the current thread
1317 * @param value Name string
1318 * @retval 0 on success
1319 * @retval -EFAULT Memory access error with supplied string
1320 * @retval -ENOSYS Thread name configuration option not enabled
1321 * @retval -EINVAL Thread name too long
Anas Nashif57554052018-03-03 02:31:05 -06001322 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001323__syscall int k_thread_name_set(k_tid_t thread_id, const char *value);
Anas Nashif57554052018-03-03 02:31:05 -06001324
1325/**
1326 * @brief Get thread name
1327 *
1328 * Get the name of a thread
1329 *
1330 * @param thread_id Thread ID
Andrew Boie38129ce2019-06-25 08:54:37 -07001331 * @retval Thread name, or NULL if configuration not enabled
Anas Nashif57554052018-03-03 02:31:05 -06001332 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001333const char *k_thread_name_get(k_tid_t thread_id);
1334
1335/**
1336 * @brief Copy the thread name into a supplied buffer
1337 *
1338 * @param thread_id Thread to obtain name information
1339 * @param buf Destination buffer
David B. Kinder73896c02019-10-28 16:27:57 -07001340 * @param size Destination buffer size
Andrew Boie38129ce2019-06-25 08:54:37 -07001341 * @retval -ENOSPC Destination buffer too small
1342 * @retval -EFAULT Memory access error
1343 * @retval -ENOSYS Thread name feature not enabled
1344 * @retval 0 Success
1345 */
1346__syscall int k_thread_name_copy(k_tid_t thread_id, char *buf,
1347 size_t size);
Anas Nashif57554052018-03-03 02:31:05 -06001348
1349/**
Pavlo Hamov8076c802019-07-31 12:43:54 +03001350 * @brief Get thread state string
1351 *
1352 * Get the human friendly thread state string
1353 *
1354 * @param thread_id Thread ID
1355 * @retval Thread state string, empty if no state flag is set
1356 */
1357const char *k_thread_state_str(k_tid_t thread_id);
1358
1359/**
Andy Rosscfe62032018-09-29 07:34:55 -07001360 * @}
1361 */
1362
1363/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001364 * @addtogroup clock_apis
1365 * @{
1366 */
1367
1368/**
1369 * @brief Generate null timeout delay.
1370 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001371 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001372 * not to wait if the requested operation cannot be performed immediately.
1373 *
1374 * @return Timeout delay value.
1375 */
1376#define K_NO_WAIT 0
1377
1378/**
1379 * @brief Generate timeout delay from milliseconds.
1380 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001381 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001382 * to wait up to @a ms milliseconds to perform the requested operation.
1383 *
1384 * @param ms Duration in milliseconds.
1385 *
1386 * @return Timeout delay value.
1387 */
Johan Hedberg14471692016-11-13 10:52:15 +02001388#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001389
1390/**
1391 * @brief Generate timeout delay from seconds.
1392 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001393 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001394 * to wait up to @a s seconds to perform the requested operation.
1395 *
1396 * @param s Duration in seconds.
1397 *
1398 * @return Timeout delay value.
1399 */
Johan Hedberg14471692016-11-13 10:52:15 +02001400#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001401
1402/**
1403 * @brief Generate timeout delay from minutes.
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001404
1405 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001406 * to wait up to @a m minutes to perform the requested operation.
1407 *
1408 * @param m Duration in minutes.
1409 *
1410 * @return Timeout delay value.
1411 */
Johan Hedberg14471692016-11-13 10:52:15 +02001412#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001413
1414/**
1415 * @brief Generate timeout delay from hours.
1416 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001417 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001418 * to wait up to @a h hours to perform the requested operation.
1419 *
1420 * @param h Duration in hours.
1421 *
1422 * @return Timeout delay value.
1423 */
Johan Hedberg14471692016-11-13 10:52:15 +02001424#define K_HOURS(h) K_MINUTES((h) * 60)
1425
Allan Stephensc98da842016-11-11 15:45:03 -05001426/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001427 * @brief Generate infinite timeout delay.
1428 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001429 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001430 * to wait as long as necessary to perform the requested operation.
1431 *
1432 * @return Timeout delay value.
1433 */
1434#define K_FOREVER (-1)
1435
1436/**
Anas Nashif166f5192018-02-25 08:02:36 -06001437 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001438 */
1439
1440/**
Allan Stephensc98da842016-11-11 15:45:03 -05001441 * @cond INTERNAL_HIDDEN
1442 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001443
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001444struct k_timer {
1445 /*
1446 * _timeout structure must be first here if we want to use
1447 * dynamic timer allocation. timeout.node is used in the double-linked
1448 * list of free timers
1449 */
1450 struct _timeout timeout;
1451
Allan Stephens45bfa372016-10-12 12:39:42 -05001452 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001453 _wait_q_t wait_q;
1454
1455 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001456 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001457
1458 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001459 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001460
1461 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001462 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001463
Allan Stephens45bfa372016-10-12 12:39:42 -05001464 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001465 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001466
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001467 /* user-specific data, also used to support legacy features */
1468 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001469
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001470 _OBJECT_TRACING_NEXT_PTR(k_timer)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08001471 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001472};
1473
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001474#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001475 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001476 .timeout = { \
1477 .node = {},\
1478 .dticks = 0, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001479 .fn = z_timer_expiration_handler \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001480 }, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001481 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001482 .expiry_fn = expiry, \
1483 .stop_fn = stop, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001484 .period = 0, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001485 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001486 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001487 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001488 }
1489
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05001490#define K_TIMER_INITIALIZER __DEPRECATED_MACRO Z_TIMER_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001491
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001492/**
Allan Stephensc98da842016-11-11 15:45:03 -05001493 * INTERNAL_HIDDEN @endcond
1494 */
1495
1496/**
1497 * @defgroup timer_apis Timer APIs
1498 * @ingroup kernel_apis
1499 * @{
1500 */
1501
1502/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001503 * @typedef k_timer_expiry_t
1504 * @brief Timer expiry function type.
1505 *
1506 * A timer's expiry function is executed by the system clock interrupt handler
1507 * each time the timer expires. The expiry function is optional, and is only
1508 * invoked if the timer has been initialized with one.
1509 *
1510 * @param timer Address of timer.
1511 *
1512 * @return N/A
1513 */
1514typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1515
1516/**
1517 * @typedef k_timer_stop_t
1518 * @brief Timer stop function type.
1519 *
1520 * A timer's stop function is executed if the timer is stopped prematurely.
1521 * The function runs in the context of the thread that stops the timer.
1522 * The stop function is optional, and is only invoked if the timer has been
1523 * initialized with one.
1524 *
1525 * @param timer Address of timer.
1526 *
1527 * @return N/A
1528 */
1529typedef void (*k_timer_stop_t)(struct k_timer *timer);
1530
1531/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001532 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001533 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001534 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001535 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001536 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001537 *
1538 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001539 * @param expiry_fn Function to invoke each time the timer expires.
1540 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001541 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001542#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001543 Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001544 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001545
Allan Stephens45bfa372016-10-12 12:39:42 -05001546/**
1547 * @brief Initialize a timer.
1548 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001549 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001550 *
1551 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001552 * @param expiry_fn Function to invoke each time the timer expires.
1553 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001554 *
1555 * @return N/A
1556 */
1557extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001558 k_timer_expiry_t expiry_fn,
1559 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001560
Allan Stephens45bfa372016-10-12 12:39:42 -05001561/**
1562 * @brief Start a timer.
1563 *
1564 * This routine starts a timer, and resets its status to zero. The timer
1565 * begins counting down using the specified duration and period values.
1566 *
1567 * Attempting to start a timer that is already running is permitted.
1568 * The timer's status is reset to zero and the timer begins counting down
1569 * using the new duration and period values.
1570 *
1571 * @param timer Address of timer.
1572 * @param duration Initial timer duration (in milliseconds).
1573 * @param period Timer period (in milliseconds).
1574 *
1575 * @return N/A
1576 */
Andrew Boiea354d492017-09-29 16:22:28 -07001577__syscall void k_timer_start(struct k_timer *timer,
1578 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001579
1580/**
1581 * @brief Stop a timer.
1582 *
1583 * This routine stops a running timer prematurely. The timer's stop function,
1584 * if one exists, is invoked by the caller.
1585 *
1586 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001587 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001588 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001589 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1590 * if @a k_timer_stop is to be called from ISRs.
1591 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001592 * @param timer Address of timer.
1593 *
1594 * @return N/A
1595 */
Andrew Boiea354d492017-09-29 16:22:28 -07001596__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001597
1598/**
1599 * @brief Read timer status.
1600 *
1601 * This routine reads the timer's status, which indicates the number of times
1602 * it has expired since its status was last read.
1603 *
1604 * Calling this routine resets the timer's status to zero.
1605 *
1606 * @param timer Address of timer.
1607 *
1608 * @return Timer status.
1609 */
Andrew Boiea354d492017-09-29 16:22:28 -07001610__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001611
1612/**
1613 * @brief Synchronize thread to timer expiration.
1614 *
1615 * This routine blocks the calling thread until the timer's status is non-zero
1616 * (indicating that it has expired at least once since it was last examined)
1617 * or the timer is stopped. If the timer status is already non-zero,
1618 * or the timer is already stopped, the caller continues without waiting.
1619 *
1620 * Calling this routine resets the timer's status to zero.
1621 *
1622 * This routine must not be used by interrupt handlers, since they are not
1623 * allowed to block.
1624 *
1625 * @param timer Address of timer.
1626 *
1627 * @return Timer status.
1628 */
Andrew Boiea354d492017-09-29 16:22:28 -07001629__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001630
Andy Ross52e444b2018-09-28 09:06:37 -07001631extern s32_t z_timeout_remaining(struct _timeout *timeout);
1632
Allan Stephens45bfa372016-10-12 12:39:42 -05001633/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001634 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001635 *
1636 * This routine computes the (approximate) time remaining before a running
1637 * timer next expires. If the timer is not running, it returns zero.
1638 *
1639 * @param timer Address of timer.
1640 *
1641 * @return Remaining time (in milliseconds).
1642 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001643__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001644
Patrik Flykt4344e272019-03-08 14:19:05 -07001645static inline u32_t z_impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001646{
Charles E. Youse0ad40222019-03-01 10:51:04 -08001647 const s32_t ticks = z_timeout_remaining(&timer->timeout);
Andy Ross88924062019-10-03 11:43:10 -07001648 return (ticks > 0) ? (u32_t)k_ticks_to_ms_floor64(ticks) : 0U;
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001649}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001650
Allan Stephensc98da842016-11-11 15:45:03 -05001651/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001652 * @brief Associate user-specific data with a timer.
1653 *
1654 * This routine records the @a user_data with the @a timer, to be retrieved
1655 * later.
1656 *
1657 * It can be used e.g. in a timer handler shared across multiple subsystems to
1658 * retrieve data specific to the subsystem this timer is associated with.
1659 *
1660 * @param timer Address of timer.
1661 * @param user_data User data to associate with the timer.
1662 *
1663 * @return N/A
1664 */
Andrew Boiea354d492017-09-29 16:22:28 -07001665__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1666
Anas Nashif954d5502018-02-25 08:37:28 -06001667/**
1668 * @internal
1669 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001670static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
Andrew Boiea354d492017-09-29 16:22:28 -07001671 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001672{
1673 timer->user_data = user_data;
1674}
1675
1676/**
1677 * @brief Retrieve the user-specific data from a timer.
1678 *
1679 * @param timer Address of timer.
1680 *
1681 * @return The user data.
1682 */
Andrew Boiea354d492017-09-29 16:22:28 -07001683__syscall void *k_timer_user_data_get(struct k_timer *timer);
1684
Patrik Flykt4344e272019-03-08 14:19:05 -07001685static inline void *z_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001686{
1687 return timer->user_data;
1688}
1689
Anas Nashif166f5192018-02-25 08:02:36 -06001690/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001691
Allan Stephensc98da842016-11-11 15:45:03 -05001692/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001693 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001694 * @{
1695 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001696
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001697/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001698 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001699 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001700 * This routine returns the elapsed time since the system booted,
1701 * in milliseconds.
1702 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001703 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001704 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001705 * While this function returns time in milliseconds, it does
1706 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001707 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001708 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001709 *
1710 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001711 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001712__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001713
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001714/**
1715 * @brief Enable clock always on in tickless kernel
1716 *
Andy Ross1db9f182019-06-25 10:09:45 -07001717 * Deprecated. This does nothing (it was always just a hint). This
1718 * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE
1719 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001720 *
1721 * @retval prev_status Previous status of always on flag
1722 */
Andy Ross1db9f182019-06-25 10:09:45 -07001723/* LCOV_EXCL_START */
1724__deprecated static inline int k_enable_sys_clock_always_on(void)
1725{
1726 __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1727 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1728
1729 return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE);
1730}
1731/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001732
1733/**
1734 * @brief Disable clock always on in tickless kernel
1735 *
Andy Ross1db9f182019-06-25 10:09:45 -07001736 * Deprecated. This does nothing (it was always just a hint). This
1737 * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE
1738 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001739 */
Andy Ross1db9f182019-06-25 10:09:45 -07001740/* LCOV_EXCL_START */
1741__deprecated static inline void k_disable_sys_clock_always_on(void)
1742{
1743 __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1744 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1745}
1746/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001747
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001748/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001749 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001750 *
Peter Bigota6067a32019-08-28 08:19:26 -05001751 * This routine returns the lower 32 bits of the system uptime in
1752 * milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001753 *
Peter Bigota6067a32019-08-28 08:19:26 -05001754 * Because correct conversion requires full precision of the system
1755 * clock there is no benefit to using this over k_uptime_get() unless
1756 * you know the application will never run long enough for the system
1757 * clock to approach 2^32 ticks. Calls to this function may involve
1758 * interrupt blocking and 64-bit math.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001759 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001760 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001761 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001762 * While this function returns time in milliseconds, it does
1763 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001764 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option
David B. Kinder8de9cc72019-06-25 10:44:55 -07001765 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001766 *
Peter Bigota6067a32019-08-28 08:19:26 -05001767 * @return The low 32 bits of the current uptime, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001768 */
Peter Bigota6067a32019-08-28 08:19:26 -05001769static inline u32_t k_uptime_get_32(void)
1770{
1771 return (u32_t)k_uptime_get();
1772}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001773
1774/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001775 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001776 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001777 * This routine computes the elapsed time between the current system uptime
1778 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001779 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001780 * @param reftime Pointer to a reference time, which is updated to the current
1781 * uptime upon return.
1782 *
1783 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001784 */
Andy Ross987c0e52018-09-27 16:50:00 -07001785static inline s64_t k_uptime_delta(s64_t *reftime)
1786{
1787 s64_t uptime, delta;
1788
1789 uptime = k_uptime_get();
1790 delta = uptime - *reftime;
1791 *reftime = uptime;
1792
1793 return delta;
1794}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001795
1796/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001797 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001799 * This routine computes the elapsed time between the current system uptime
1800 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001801 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001802 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1803 * need for interrupt locking and 64-bit math. However, the 32-bit result
1804 * cannot hold an elapsed time larger than approximately 50 days, so the
1805 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001806 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001807 * @param reftime Pointer to a reference time, which is updated to the current
1808 * uptime upon return.
1809 *
1810 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001811 */
Andy Ross987c0e52018-09-27 16:50:00 -07001812static inline u32_t k_uptime_delta_32(s64_t *reftime)
1813{
1814 return (u32_t)k_uptime_delta(reftime);
1815}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001816
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001817/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001818 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001820 * This routine returns the current time, as measured by the system's hardware
1821 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001823 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001824 */
Andrew Boie979b17f2019-10-03 15:20:41 -07001825static inline u32_t k_cycle_get_32(void)
1826{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08001827 return arch_k_cycle_get_32();
Andrew Boie979b17f2019-10-03 15:20:41 -07001828}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001829
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001830/**
Anas Nashif166f5192018-02-25 08:02:36 -06001831 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001832 */
1833
Allan Stephensc98da842016-11-11 15:45:03 -05001834/**
1835 * @cond INTERNAL_HIDDEN
1836 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001837
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001838struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001839 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001840 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001841 union {
1842 _wait_q_t wait_q;
1843
1844 _POLL_EVENT;
1845 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001846
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001847 _OBJECT_TRACING_NEXT_PTR(k_queue)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08001848 _OBJECT_TRACING_LINKED_FLAG
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001849};
1850
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001851#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001852 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001853 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Stephanos Ioannidisf628dcd2019-09-11 18:09:49 +09001854 .lock = { }, \
1855 { \
1856 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
1857 _POLL_EVENT_OBJ_INIT(obj) \
1858 }, \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001859 _OBJECT_TRACING_INIT \
1860 }
1861
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05001862#define K_QUEUE_INITIALIZER __DEPRECATED_MACRO _K_QUEUE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001863
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001864extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1865
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001866/**
1867 * INTERNAL_HIDDEN @endcond
1868 */
1869
1870/**
1871 * @defgroup queue_apis Queue APIs
1872 * @ingroup kernel_apis
1873 * @{
1874 */
1875
1876/**
1877 * @brief Initialize a queue.
1878 *
1879 * This routine initializes a queue object, prior to its first use.
1880 *
1881 * @param queue Address of the queue.
1882 *
1883 * @return N/A
1884 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001885__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001886
1887/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001888 * @brief Cancel waiting on a queue.
1889 *
1890 * This routine causes first thread pending on @a queue, if any, to
1891 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001892 * If the queue is being waited on by k_poll(), it will return with
1893 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1894 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001895 *
1896 * @note Can be called by ISRs.
1897 *
1898 * @param queue Address of the queue.
1899 *
1900 * @return N/A
1901 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001902__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001903
1904/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001905 * @brief Append an element to the end of a queue.
1906 *
1907 * This routine appends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001908 * aligned on a word boundary, and the first word of the item is reserved
1909 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001910 *
1911 * @note Can be called by ISRs.
1912 *
1913 * @param queue Address of the queue.
1914 * @param data Address of the data item.
1915 *
1916 * @return N/A
1917 */
1918extern void k_queue_append(struct k_queue *queue, void *data);
1919
1920/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001921 * @brief Append an element to a queue.
1922 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001923 * This routine appends a data item to @a queue. There is an implicit memory
1924 * allocation to create an additional temporary bookkeeping data structure from
1925 * the calling thread's resource pool, which is automatically freed when the
1926 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001927 *
1928 * @note Can be called by ISRs.
1929 *
1930 * @param queue Address of the queue.
1931 * @param data Address of the data item.
1932 *
1933 * @retval 0 on success
1934 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1935 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301936__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001937
1938/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001939 * @brief Prepend an element to a queue.
1940 *
1941 * This routine prepends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001942 * aligned on a word boundary, and the first word of the item is reserved
1943 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001944 *
1945 * @note Can be called by ISRs.
1946 *
1947 * @param queue Address of the queue.
1948 * @param data Address of the data item.
1949 *
1950 * @return N/A
1951 */
1952extern void k_queue_prepend(struct k_queue *queue, void *data);
1953
1954/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001955 * @brief Prepend an element to a queue.
1956 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001957 * This routine prepends a data item to @a queue. There is an implicit memory
1958 * allocation to create an additional temporary bookkeeping data structure from
1959 * the calling thread's resource pool, which is automatically freed when the
1960 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001961 *
1962 * @note Can be called by ISRs.
1963 *
1964 * @param queue Address of the queue.
1965 * @param data Address of the data item.
1966 *
1967 * @retval 0 on success
1968 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1969 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301970__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001971
1972/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001973 * @brief Inserts an element to a queue.
1974 *
1975 * This routine inserts a data item to @a queue after previous item. A queue
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001976 * data item must be aligned on a word boundary, and the first word of
1977 * the item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001978 *
1979 * @note Can be called by ISRs.
1980 *
1981 * @param queue Address of the queue.
1982 * @param prev Address of the previous data item.
1983 * @param data Address of the data item.
1984 *
1985 * @return N/A
1986 */
1987extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1988
1989/**
1990 * @brief Atomically append a list of elements to a queue.
1991 *
1992 * This routine adds a list of data items to @a queue in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001993 * The data items must be in a singly-linked list, with the first word
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001994 * in each data item pointing to the next data item; the list must be
1995 * NULL-terminated.
1996 *
1997 * @note Can be called by ISRs.
1998 *
1999 * @param queue Address of the queue.
2000 * @param head Pointer to first node in singly-linked list.
2001 * @param tail Pointer to last node in singly-linked list.
2002 *
2003 * @return N/A
2004 */
2005extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
2006
2007/**
2008 * @brief Atomically add a list of elements to a queue.
2009 *
2010 * This routine adds a list of data items to @a queue in one operation.
2011 * The data items must be in a singly-linked list implemented using a
2012 * sys_slist_t object. Upon completion, the original list is empty.
2013 *
2014 * @note Can be called by ISRs.
2015 *
2016 * @param queue Address of the queue.
2017 * @param list Pointer to sys_slist_t object.
2018 *
2019 * @return N/A
2020 */
2021extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
2022
2023/**
2024 * @brief Get an element from a queue.
2025 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002026 * This routine removes first data item from @a queue. The first word of the
2027 * data item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002028 *
2029 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2030 *
2031 * @param queue Address of the queue.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002032 * @param timeout Non-negative waiting period to obtain a data item (in
2033 * milliseconds), or one of the special values K_NO_WAIT and
2034 * K_FOREVER.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002035 *
2036 * @return Address of the data item if successful; NULL if returned
2037 * without waiting, or waiting period timed out.
2038 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002039__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002040
2041/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002042 * @brief Remove an element from a queue.
2043 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002044 * This routine removes data item from @a queue. The first word of the
2045 * data item is reserved for the kernel's use. Removing elements from k_queue
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002046 * rely on sys_slist_find_and_remove which is not a constant time operation.
2047 *
2048 * @note Can be called by ISRs
2049 *
2050 * @param queue Address of the queue.
2051 * @param data Address of the data item.
2052 *
2053 * @return true if data item was removed
2054 */
2055static inline bool k_queue_remove(struct k_queue *queue, void *data)
2056{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002057 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002058}
2059
2060/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002061 * @brief Append an element to a queue only if it's not present already.
2062 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002063 * This routine appends data item to @a queue. The first word of the data
2064 * item is reserved for the kernel's use. Appending elements to k_queue
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002065 * relies on sys_slist_is_node_in_list which is not a constant time operation.
2066 *
2067 * @note Can be called by ISRs
2068 *
2069 * @param queue Address of the queue.
2070 * @param data Address of the data item.
2071 *
2072 * @return true if data item was added, false if not
2073 */
2074static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
2075{
2076 sys_sfnode_t *test;
2077
2078 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
2079 if (test == (sys_sfnode_t *) data) {
2080 return false;
2081 }
2082 }
2083
2084 k_queue_append(queue, data);
2085 return true;
2086}
2087
2088/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002089 * @brief Query a queue to see if it has data available.
2090 *
2091 * Note that the data might be already gone by the time this function returns
2092 * if other threads are also trying to read from the queue.
2093 *
2094 * @note Can be called by ISRs.
2095 *
2096 * @param queue Address of the queue.
2097 *
2098 * @return Non-zero if the queue is empty.
2099 * @return 0 if data is available.
2100 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002101__syscall int k_queue_is_empty(struct k_queue *queue);
2102
Patrik Flykt4344e272019-03-08 14:19:05 -07002103static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002104{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002105 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002106}
2107
2108/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002109 * @brief Peek element at the head of queue.
2110 *
2111 * Return element from the head of queue without removing it.
2112 *
2113 * @param queue Address of the queue.
2114 *
2115 * @return Head element, or NULL if queue is empty.
2116 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002117__syscall void *k_queue_peek_head(struct k_queue *queue);
2118
Patrik Flykt4344e272019-03-08 14:19:05 -07002119static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002120{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002121 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002122}
2123
2124/**
2125 * @brief Peek element at the tail of queue.
2126 *
2127 * Return element from the tail of queue without removing it.
2128 *
2129 * @param queue Address of the queue.
2130 *
2131 * @return Tail element, or NULL if queue is empty.
2132 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002133__syscall void *k_queue_peek_tail(struct k_queue *queue);
2134
Patrik Flykt4344e272019-03-08 14:19:05 -07002135static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002136{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002137 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002138}
2139
2140/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002141 * @brief Statically define and initialize a queue.
2142 *
2143 * The queue can be accessed outside the module where it is defined using:
2144 *
2145 * @code extern struct k_queue <name>; @endcode
2146 *
2147 * @param name Name of the queue.
2148 */
2149#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002150 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002151 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002152
Anas Nashif166f5192018-02-25 08:02:36 -06002153/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002154
Wentong Wu5611e922019-06-20 23:51:27 +08002155#ifdef CONFIG_USERSPACE
2156/**
2157 * @brief futex structure
2158 *
2159 * A k_futex is a lightweight mutual exclusion primitive designed
2160 * to minimize kernel involvement. Uncontended operation relies
2161 * only on atomic access to shared memory. k_futex are tracked as
2162 * kernel objects and can live in user memory so any access bypass
2163 * the kernel object permission management mechanism.
2164 */
2165struct k_futex {
2166 atomic_t val;
2167};
2168
2169/**
2170 * @brief futex kernel data structure
2171 *
2172 * z_futex_data are the helper data structure for k_futex to complete
2173 * futex contended operation on kernel side, structure z_futex_data
2174 * of every futex object is invisible in user mode.
2175 */
2176struct z_futex_data {
2177 _wait_q_t wait_q;
2178 struct k_spinlock lock;
2179};
2180
2181#define Z_FUTEX_DATA_INITIALIZER(obj) \
2182 { \
2183 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \
2184 }
2185
2186/**
2187 * @defgroup futex_apis FUTEX APIs
2188 * @ingroup kernel_apis
2189 * @{
2190 */
2191
2192/**
Wentong Wu5611e922019-06-20 23:51:27 +08002193 * @brief Pend the current thread on a futex
2194 *
2195 * Tests that the supplied futex contains the expected value, and if so,
2196 * goes to sleep until some other thread calls k_futex_wake() on it.
2197 *
2198 * @param futex Address of the futex.
2199 * @param expected Expected value of the futex, if it is different the caller
2200 * will not wait on it.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002201 * @param timeout Non-negative waiting period on the futex, in milliseconds, or
2202 * one of the special values K_NO_WAIT or K_FOREVER.
Wentong Wu5611e922019-06-20 23:51:27 +08002203 * @retval -EACCES Caller does not have read access to futex address.
2204 * @retval -EAGAIN If the futex value did not match the expected parameter.
2205 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2206 * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup.
2207 * @retval 0 if the caller went to sleep and was woken up. The caller
2208 * should check the futex's value on wakeup to determine if it needs
2209 * to block again.
2210 */
2211__syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout);
2212
2213/**
2214 * @brief Wake one/all threads pending on a futex
2215 *
2216 * Wake up the highest priority thread pending on the supplied futex, or
2217 * wakeup all the threads pending on the supplied futex, and the behavior
2218 * depends on wake_all.
2219 *
2220 * @param futex Futex to wake up pending threads.
2221 * @param wake_all If true, wake up all pending threads; If false,
2222 * wakeup the highest priority thread.
2223 * @retval -EACCES Caller does not have access to the futex address.
2224 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2225 * @retval Number of threads that were woken up.
2226 */
2227__syscall int k_futex_wake(struct k_futex *futex, bool wake_all);
2228
2229/** @} */
2230#endif
2231
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002232struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002233 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002234};
2235
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002236/**
2237 * @cond INTERNAL_HIDDEN
2238 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002239#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002240 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002241 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002242 }
2243
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002244#define K_FIFO_INITIALIZER __DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002245
Allan Stephensc98da842016-11-11 15:45:03 -05002246/**
2247 * INTERNAL_HIDDEN @endcond
2248 */
2249
2250/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002251 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002252 * @ingroup kernel_apis
2253 * @{
2254 */
2255
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002256/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002257 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002258 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002259 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002260 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002261 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002262 *
2263 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002264 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002265 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002266#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002267 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002268
2269/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002270 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002271 *
2272 * This routine causes first thread pending on @a fifo, if any, to
2273 * return from k_fifo_get() call with NULL value (as if timeout
2274 * expired).
2275 *
2276 * @note Can be called by ISRs.
2277 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002278 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002279 *
2280 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002281 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002282 */
2283#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002284 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002285
2286/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002287 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002288 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002289 * This routine adds a data item to @a fifo. A FIFO data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002290 * aligned on a word boundary, and the first word of the item is reserved
2291 * for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002293 * @note Can be called by ISRs.
2294 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002295 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002296 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002297 *
2298 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002299 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002300 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002301#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002302 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002303
2304/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002305 * @brief Add an element to a FIFO queue.
2306 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002307 * This routine adds a data item to @a fifo. There is an implicit memory
2308 * allocation to create an additional temporary bookkeeping data structure from
2309 * the calling thread's resource pool, which is automatically freed when the
2310 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002311 *
2312 * @note Can be called by ISRs.
2313 *
2314 * @param fifo Address of the FIFO.
2315 * @param data Address of the data item.
2316 *
2317 * @retval 0 on success
2318 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002319 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002320 */
2321#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002322 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002323
2324/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002325 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002326 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002327 * This routine adds a list of data items to @a fifo in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002328 * The data items must be in a singly-linked list, with the first word of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002329 * each data item pointing to the next data item; the list must be
2330 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002331 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002332 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002333 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002334 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002335 * @param head Pointer to first node in singly-linked list.
2336 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002337 *
2338 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002339 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002340 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002341#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002342 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002343
2344/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002345 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002346 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002347 * This routine adds a list of data items to @a fifo in one operation.
2348 * The data items must be in a singly-linked list implemented using a
2349 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002350 * and must be re-initialized via sys_slist_init().
2351 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002352 * @note Can be called by ISRs.
2353 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002354 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002355 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002356 *
2357 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002358 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002359 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002360#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002361 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002362
2363/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002364 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002366 * This routine removes a data item from @a fifo in a "first in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002367 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002368 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002369 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2370 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002371 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002372 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002373 * or one of the special values K_NO_WAIT and K_FOREVER.
2374 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002375 * @return Address of the data item if successful; NULL if returned
2376 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002377 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002378 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002379#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002380 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002381
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002382/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002383 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002384 *
2385 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002386 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002387 *
2388 * @note Can be called by ISRs.
2389 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002390 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002391 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002392 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002393 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002394 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002395 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002396#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002397 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002398
2399/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002400 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002401 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002402 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302403 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002404 * on each iteration of processing, a head container will be peeked,
2405 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002406 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002407 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002408 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002409 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002410 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002411 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002412 */
2413#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002414 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002415
2416/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002417 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002418 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002419 * Return element from the tail of FIFO queue (without removing it). A usecase
2420 * for this is if elements of the FIFO queue are themselves containers. Then
2421 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002422 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002423 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002424 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002425 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002426 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002427 */
2428#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002429 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002430
2431/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002432 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002433 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002434 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002435 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002436 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002437 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002438 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002439 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002440 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002441#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002442 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002443 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002444
Anas Nashif166f5192018-02-25 08:02:36 -06002445/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002446
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002447struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002448 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002449};
2450
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002451/**
2452 * @cond INTERNAL_HIDDEN
2453 */
2454
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002455#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002456 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002457 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002458 }
2459
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002460#define K_LIFO_INITIALIZER __DEPRECATED_MACRO _K_LIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002461
Allan Stephensc98da842016-11-11 15:45:03 -05002462/**
2463 * INTERNAL_HIDDEN @endcond
2464 */
2465
2466/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002467 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002468 * @ingroup kernel_apis
2469 * @{
2470 */
2471
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002472/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002473 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002474 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002475 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002476 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002477 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002478 *
2479 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002480 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002481 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002482#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002483 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002484
2485/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002486 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002487 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002488 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002489 * aligned on a word boundary, and the first word of the item is
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002490 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002492 * @note Can be called by ISRs.
2493 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002494 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002495 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002496 *
2497 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002498 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002499 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002500#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002501 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002502
2503/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002504 * @brief Add an element to a LIFO queue.
2505 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002506 * This routine adds a data item to @a lifo. There is an implicit memory
2507 * allocation to create an additional temporary bookkeeping data structure from
2508 * the calling thread's resource pool, which is automatically freed when the
2509 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002510 *
2511 * @note Can be called by ISRs.
2512 *
2513 * @param lifo Address of the LIFO.
2514 * @param data Address of the data item.
2515 *
2516 * @retval 0 on success
2517 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002518 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002519 */
2520#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002521 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002522
2523/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002524 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002526 * This routine removes a data item from @a lifo in a "last in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002527 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002529 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2530 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002531 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002532 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002533 * or one of the special values K_NO_WAIT and K_FOREVER.
2534 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002535 * @return Address of the data item if successful; NULL if returned
2536 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002537 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002538 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002539#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002540 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002541
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002542/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002543 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002544 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002545 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002546 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002547 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002548 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002549 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002550 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002551 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002552#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002553 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002554 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002555
Anas Nashif166f5192018-02-25 08:02:36 -06002556/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002557
2558/**
2559 * @cond INTERNAL_HIDDEN
2560 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302561#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002562
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002563typedef uintptr_t stack_data_t;
2564
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002565struct k_stack {
2566 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002567 struct k_spinlock lock;
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002568 stack_data_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002569
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002570 _OBJECT_TRACING_NEXT_PTR(k_stack)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08002571 _OBJECT_TRACING_LINKED_FLAG
Andrew Boief3bee952018-05-02 17:44:39 -07002572 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002573};
2574
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002575#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002576 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002577 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002578 .base = stack_buffer, \
2579 .next = stack_buffer, \
2580 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002581 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002582 }
2583
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002584#define K_STACK_INITIALIZER __DEPRECATED_MACRO _K_STACK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002585
Allan Stephensc98da842016-11-11 15:45:03 -05002586/**
2587 * INTERNAL_HIDDEN @endcond
2588 */
2589
2590/**
2591 * @defgroup stack_apis Stack APIs
2592 * @ingroup kernel_apis
2593 * @{
2594 */
2595
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002596/**
2597 * @brief Initialize a stack.
2598 *
2599 * This routine initializes a stack object, prior to its first use.
2600 *
2601 * @param stack Address of the stack.
2602 * @param buffer Address of array used to hold stacked values.
2603 * @param num_entries Maximum number of values that can be stacked.
2604 *
2605 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002606 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002607 */
Andrew Boief3bee952018-05-02 17:44:39 -07002608void k_stack_init(struct k_stack *stack,
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002609 stack_data_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002610
2611
2612/**
2613 * @brief Initialize a stack.
2614 *
2615 * This routine initializes a stack object, prior to its first use. Internal
2616 * buffers will be allocated from the calling thread's resource pool.
2617 * This memory will be released if k_stack_cleanup() is called, or
2618 * userspace is enabled and the stack object loses all references to it.
2619 *
2620 * @param stack Address of the stack.
2621 * @param num_entries Maximum number of values that can be stacked.
2622 *
2623 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002624 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002625 */
2626
Adithya Baglody28080d32018-10-15 11:48:51 +05302627__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2628 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002629
2630/**
2631 * @brief Release a stack's allocated buffer
2632 *
2633 * If a stack object was given a dynamically allocated buffer via
2634 * k_stack_alloc_init(), this will free it. This function does nothing
2635 * if the buffer wasn't dynamically allocated.
2636 *
2637 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002638 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002639 */
2640void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002641
2642/**
2643 * @brief Push an element onto a stack.
2644 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002645 * This routine adds a stack_data_t value @a data to @a stack.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002646 *
2647 * @note Can be called by ISRs.
2648 *
2649 * @param stack Address of the stack.
2650 * @param data Value to push onto the stack.
2651 *
2652 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002653 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002654 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002655__syscall void k_stack_push(struct k_stack *stack, stack_data_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002656
2657/**
2658 * @brief Pop an element from a stack.
2659 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002660 * This routine removes a stack_data_t value from @a stack in a "last in,
2661 * first out" manner and stores the value in @a data.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002662 *
2663 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2664 *
2665 * @param stack Address of the stack.
2666 * @param data Address of area to hold the value popped from the stack.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002667 * @param timeout Non-negative waiting period to obtain a value (in
2668 * milliseconds), or one of the special values K_NO_WAIT and
2669 * K_FOREVER.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002670 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002671 * @retval 0 Element popped from stack.
2672 * @retval -EBUSY Returned without waiting.
2673 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002674 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002675 */
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002676__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data,
2677 s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002678
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002679/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002680 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002681 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002682 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002683 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002684 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002686 * @param name Name of the stack.
2687 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002688 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002689 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002690#define K_STACK_DEFINE(name, stack_num_entries) \
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002691 stack_data_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002692 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002693 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002694 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002695 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002696
Anas Nashif166f5192018-02-25 08:02:36 -06002697/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002698
Allan Stephens6bba9b02016-11-16 14:56:54 -05002699struct k_work;
Piotr Zięcik19d83492019-09-27 09:16:25 +02002700struct k_work_poll;
Allan Stephens6bba9b02016-11-16 14:56:54 -05002701
Piotr Zięcik19d83492019-09-27 09:16:25 +02002702/* private, used by k_poll and k_work_poll */
Piotr Zięcik1c4177d2019-08-27 12:19:26 +02002703typedef int (*_poller_cb_t)(struct k_poll_event *event, u32_t state);
2704struct _poller {
2705 volatile bool is_polling;
2706 struct k_thread *thread;
2707 _poller_cb_t cb;
2708};
2709
Allan Stephensc98da842016-11-11 15:45:03 -05002710/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002711 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002712 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002713 */
2714
Allan Stephens6bba9b02016-11-16 14:56:54 -05002715/**
2716 * @typedef k_work_handler_t
2717 * @brief Work item handler function type.
2718 *
2719 * A work item's handler function is executed by a workqueue's thread
2720 * when the work item is processed by the workqueue.
2721 *
2722 * @param work Address of the work item.
2723 *
2724 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002725 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002726 */
2727typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002728
2729/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002730 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002731 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002732
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002733struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002734 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002735 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002736};
2737
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002738enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002739 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002740};
2741
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002742struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002743 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002744 k_work_handler_t handler;
2745 atomic_t flags[1];
2746};
2747
Allan Stephens6bba9b02016-11-16 14:56:54 -05002748struct k_delayed_work {
2749 struct k_work work;
2750 struct _timeout timeout;
2751 struct k_work_q *work_q;
2752};
2753
Piotr Zięcik19d83492019-09-27 09:16:25 +02002754struct k_work_poll {
2755 struct k_work work;
2756 struct _poller poller;
2757 struct k_poll_event *events;
2758 int num_events;
2759 k_work_handler_t real_handler;
2760 struct _timeout timeout;
2761 int poll_result;
2762};
2763
Allan Stephens6bba9b02016-11-16 14:56:54 -05002764extern struct k_work_q k_sys_work_q;
2765
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002766/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002767 * INTERNAL_HIDDEN @endcond
2768 */
2769
Patrik Flykt4344e272019-03-08 14:19:05 -07002770#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002771 { \
2772 ._reserved = NULL, \
2773 .handler = work_handler, \
2774 .flags = { 0 } \
2775 }
2776
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002777#define K_WORK_INITIALIZER __DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002778
Allan Stephens6bba9b02016-11-16 14:56:54 -05002779/**
2780 * @brief Initialize a statically-defined work item.
2781 *
2782 * This macro can be used to initialize a statically-defined workqueue work
2783 * item, prior to its first use. For example,
2784 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002785 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002786 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002787 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002788 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002789 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002790 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002791#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002792 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002793
2794/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002795 * @brief Initialize a work item.
2796 *
2797 * This routine initializes a workqueue work item, prior to its first use.
2798 *
2799 * @param work Address of work item.
2800 * @param handler Function to invoke each time work item is processed.
2801 *
2802 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002803 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002804 */
2805static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2806{
Patrik Flykt4344e272019-03-08 14:19:05 -07002807 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002808}
2809
2810/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002811 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002812 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002813 * This routine submits work item @a work to be processed by workqueue
2814 * @a work_q. If the work item is already pending in the workqueue's queue
2815 * as a result of an earlier submission, this routine has no effect on the
2816 * work item. If the work item has already been processed, or is currently
2817 * being processed, its work is considered complete and the work item can be
2818 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002819 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002820 * @warning
2821 * A submitted work item must not be modified until it has been processed
2822 * by the workqueue.
2823 *
2824 * @note Can be called by ISRs.
2825 *
2826 * @param work_q Address of workqueue.
2827 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002828 *
2829 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002830 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002831 */
2832static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2833 struct k_work *work)
2834{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002835 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002836 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002837 }
2838}
2839
2840/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002841 * @brief Submit a work item to a user mode workqueue
2842 *
David B. Kinder06d78352018-12-17 14:32:40 -08002843 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002844 * memory allocation is made from the caller's resource pool which is freed
2845 * once the worker thread consumes the k_work item. The workqueue
2846 * thread must have memory access to the k_work item being submitted. The caller
2847 * must have permission granted on the work_q parameter's queue object.
2848 *
2849 * Otherwise this works the same as k_work_submit_to_queue().
2850 *
2851 * @note Can be called by ISRs.
2852 *
2853 * @param work_q Address of workqueue.
2854 * @param work Address of work item.
2855 *
2856 * @retval -EBUSY if the work item was already in some workqueue
2857 * @retval -ENOMEM if no memory for thread resource pool allocation
2858 * @retval 0 Success
2859 * @req K-WORK-001
2860 */
2861static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2862 struct k_work *work)
2863{
2864 int ret = -EBUSY;
2865
2866 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2867 ret = k_queue_alloc_append(&work_q->queue, work);
2868
2869 /* Couldn't insert into the queue. Clear the pending bit
2870 * so the work item can be submitted again
2871 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002872 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002873 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2874 }
2875 }
2876
2877 return ret;
2878}
2879
2880/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002881 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002882 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002883 * This routine indicates if work item @a work is pending in a workqueue's
2884 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002885 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002886 * @note Can be called by ISRs.
2887 *
2888 * @param work Address of work item.
2889 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002890 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002891 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002892 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002893static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002894{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002895 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002896}
2897
2898/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002899 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002900 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002901 * This routine starts workqueue @a work_q. The workqueue spawns its work
2902 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002903 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002904 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002905 * @param stack Pointer to work queue thread's stack space, as defined by
2906 * K_THREAD_STACK_DEFINE()
2907 * @param stack_size Size of the work queue thread's stack (in bytes), which
2908 * should either be the same constant passed to
2909 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002910 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002911 *
2912 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002913 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002914 */
Andrew Boie507852a2017-07-25 18:47:07 -07002915extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002916 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002917 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002918
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002919/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002920 * @brief Start a workqueue in user mode
2921 *
2922 * This works identically to k_work_q_start() except it is callable from user
2923 * mode, and the worker thread created will run in user mode.
2924 * The caller must have permissions granted on both the work_q parameter's
2925 * thread and queue objects, and the same restrictions on priority apply as
2926 * k_thread_create().
2927 *
2928 * @param work_q Address of workqueue.
2929 * @param stack Pointer to work queue thread's stack space, as defined by
2930 * K_THREAD_STACK_DEFINE()
2931 * @param stack_size Size of the work queue thread's stack (in bytes), which
2932 * should either be the same constant passed to
2933 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2934 * @param prio Priority of the work queue's thread.
2935 *
2936 * @return N/A
2937 * @req K-WORK-001
2938 */
2939extern void k_work_q_user_start(struct k_work_q *work_q,
2940 k_thread_stack_t *stack,
2941 size_t stack_size, int prio);
2942
2943/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002944 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002945 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002946 * This routine initializes a workqueue delayed work item, prior to
2947 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002948 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002949 * @param work Address of delayed work item.
2950 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002951 *
2952 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002953 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002954 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002955extern void k_delayed_work_init(struct k_delayed_work *work,
2956 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002957
2958/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002959 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002960 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002961 * This routine schedules work item @a work to be processed by workqueue
2962 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002963 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002964 * Only when the countdown completes is the work item actually submitted to
2965 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002966 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002967 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002968 * counting down cancels the existing submission and restarts the
2969 * countdown using the new delay. Note that this behavior is
2970 * inherently subject to race conditions with the pre-existing
2971 * timeouts and work queue, so care must be taken to synchronize such
2972 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002973 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002974 * @warning
2975 * A delayed work item must not be modified until it has been processed
2976 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002977 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002978 * @note Can be called by ISRs.
2979 *
2980 * @param work_q Address of workqueue.
2981 * @param work Address of delayed work item.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002982 * @param delay Non-negative delay before submitting the work item (in
2983 * milliseconds).
Allan Stephens6bba9b02016-11-16 14:56:54 -05002984 *
2985 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002986 * @retval -EINVAL Work item is being processed or has completed its work.
2987 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002988 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002989 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002990extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2991 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002992 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002993
2994/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002995 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002996 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002997 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002998 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002999 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003000 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003001 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003002 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08003003 * @note The result of calling this on a k_delayed_work item that has
3004 * not been submitted (i.e. before the return of the
3005 * k_delayed_work_submit_to_queue() call) is undefined.
3006 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003007 * @param work Address of delayed work item.
3008 *
David B. Kinder8b986d72017-04-18 15:56:26 -07003009 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003010 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003011 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003012 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003013extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003014
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003015/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003016 * @brief Submit a work item to the system workqueue.
3017 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003018 * This routine submits work item @a work to be processed by the system
3019 * workqueue. If the work item is already pending in the workqueue's queue
3020 * as a result of an earlier submission, this routine has no effect on the
3021 * work item. If the work item has already been processed, or is currently
3022 * being processed, its work is considered complete and the work item can be
3023 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003024 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003025 * @warning
3026 * Work items submitted to the system workqueue should avoid using handlers
3027 * that block or yield since this may prevent the system workqueue from
3028 * processing other work items in a timely manner.
3029 *
3030 * @note Can be called by ISRs.
3031 *
3032 * @param work Address of work item.
3033 *
3034 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003035 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003036 */
3037static inline void k_work_submit(struct k_work *work)
3038{
3039 k_work_submit_to_queue(&k_sys_work_q, work);
3040}
3041
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003042/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003043 * @brief Submit a delayed work item to the system workqueue.
3044 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003045 * This routine schedules work item @a work to be processed by the system
3046 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003047 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003048 * Only when the countdown completes is the work item actually submitted to
3049 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003050 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003051 * Submitting a previously submitted delayed work item that is still
3052 * counting down cancels the existing submission and restarts the countdown
3053 * using the new delay. If the work item is currently pending on the
3054 * workqueue's queue because the countdown has completed it is too late to
3055 * resubmit the item, and resubmission fails without impacting the work item.
3056 * If the work item has already been processed, or is currently being processed,
3057 * its work is considered complete and the work item can be resubmitted.
3058 *
3059 * @warning
3060 * Work items submitted to the system workqueue should avoid using handlers
3061 * that block or yield since this may prevent the system workqueue from
3062 * processing other work items in a timely manner.
3063 *
3064 * @note Can be called by ISRs.
3065 *
3066 * @param work Address of delayed work item.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003067 * @param delay Non-negative delay before submitting the work item (in
3068 * milliseconds).
Allan Stephens6bba9b02016-11-16 14:56:54 -05003069 *
3070 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003071 * @retval -EINVAL Work item is being processed or has completed its work.
3072 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003073 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003074 */
3075static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003076 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003077{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05003078 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003079}
3080
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003081/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02003082 * @brief Get time remaining before a delayed work gets scheduled.
3083 *
3084 * This routine computes the (approximate) time remaining before a
3085 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02003086 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02003087 *
3088 * @param work Delayed work item.
3089 *
3090 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003091 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02003092 */
Kumar Galacc334c72017-04-21 10:55:34 -05003093static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02003094{
Andy Ross88924062019-10-03 11:43:10 -07003095 return k_ticks_to_ms_floor64(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02003096}
3097
Piotr Zięcik19d83492019-09-27 09:16:25 +02003098/**
3099 * @brief Initialize a triggered work item.
3100 *
3101 * This routine initializes a workqueue triggered work item, prior to
3102 * its first use.
3103 *
3104 * @param work Address of triggered work item.
3105 * @param handler Function to invoke each time work item is processed.
3106 *
3107 * @return N/A
3108 */
3109extern void k_work_poll_init(struct k_work_poll *work,
3110 k_work_handler_t handler);
3111
3112/**
3113 * @brief Submit a triggered work item.
3114 *
3115 * This routine schedules work item @a work to be processed by workqueue
3116 * @a work_q when one of the given @a events is signaled. The routine
3117 * initiates internal poller for the work item and then returns to the caller.
3118 * Only when one of the watched events happen the work item is actually
3119 * submitted to the workqueue and becomes pending.
3120 *
3121 * Submitting a previously submitted triggered work item that is still
3122 * waiting for the event cancels the existing submission and reschedules it
3123 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003124 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003125 * so care must be taken to synchronize such resubmissions externally.
3126 *
3127 * @note Can be called by ISRs.
3128 *
3129 * @warning
3130 * Provided array of events as well as a triggered work item must be placed
3131 * in persistent memory (valid until work handler execution or work
3132 * cancellation) and cannot be modified after submission.
3133 *
3134 * @param work_q Address of workqueue.
3135 * @param work Address of delayed work item.
3136 * @param events An array of pointers to events which trigger the work.
3137 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003138 * @param timeout Non-negative timeout after which the work will be scheduled
3139 * for execution even if not triggered.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003140 *
3141 *
3142 * @retval 0 Work item started watching for events.
3143 * @retval -EINVAL Work item is being processed or has completed its work.
3144 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3145 */
3146extern int k_work_poll_submit_to_queue(struct k_work_q *work_q,
3147 struct k_work_poll *work,
3148 struct k_poll_event *events,
3149 int num_events,
3150 s32_t timeout);
3151
3152/**
3153 * @brief Submit a triggered work item to the system workqueue.
3154 *
3155 * This routine schedules work item @a work to be processed by system
3156 * workqueue when one of the given @a events is signaled. The routine
3157 * initiates internal poller for the work item and then returns to the caller.
3158 * Only when one of the watched events happen the work item is actually
3159 * submitted to the workqueue and becomes pending.
3160 *
3161 * Submitting a previously submitted triggered work item that is still
3162 * waiting for the event cancels the existing submission and reschedules it
3163 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003164 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003165 * so care must be taken to synchronize such resubmissions externally.
3166 *
3167 * @note Can be called by ISRs.
3168 *
3169 * @warning
3170 * Provided array of events as well as a triggered work item must not be
3171 * modified until the item has been processed by the workqueue.
3172 *
3173 * @param work Address of delayed work item.
3174 * @param events An array of pointers to events which trigger the work.
3175 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003176 * @param timeout Non-negative timeout after which the work will be scheduled
3177 * for execution even if not triggered.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003178 *
3179 * @retval 0 Work item started watching for events.
3180 * @retval -EINVAL Work item is being processed or has completed its work.
3181 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3182 */
3183static inline int k_work_poll_submit(struct k_work_poll *work,
3184 struct k_poll_event *events,
3185 int num_events,
3186 s32_t timeout)
3187{
3188 return k_work_poll_submit_to_queue(&k_sys_work_q, work,
3189 events, num_events, timeout);
3190}
3191
3192/**
3193 * @brief Cancel a triggered work item.
3194 *
3195 * This routine cancels the submission of triggered work item @a work.
3196 * A triggered work item can only be canceled if no event triggered work
3197 * submission.
3198 *
3199 * @note Can be called by ISRs.
3200 *
3201 * @param work Address of delayed work item.
3202 *
David B. Kinder73896c02019-10-28 16:27:57 -07003203 * @retval 0 Work item canceled.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003204 * @retval -EINVAL Work item is being processed or has completed its work.
3205 */
3206extern int k_work_poll_cancel(struct k_work_poll *work);
3207
Anas Nashif166f5192018-02-25 08:02:36 -06003208/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003209/**
Anas Nashifce78d162018-05-24 12:43:11 -05003210 * @defgroup mutex_apis Mutex APIs
3211 * @ingroup kernel_apis
3212 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003213 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003214
Anas Nashifce78d162018-05-24 12:43:11 -05003215/**
3216 * Mutex Structure
3217 * @ingroup mutex_apis
3218 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003219struct k_mutex {
3220 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05003221 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04003222 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05003223 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003224 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003225
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003226 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003227 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003228};
3229
Anas Nashifce78d162018-05-24 12:43:11 -05003230/**
3231 * @cond INTERNAL_HIDDEN
3232 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003233#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003234 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003235 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003236 .owner = NULL, \
3237 .lock_count = 0, \
3238 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003239 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003240 }
3241
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003242#define K_MUTEX_INITIALIZER __DEPRECATED_MACRO _K_MUTEX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003243
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003244/**
Allan Stephensc98da842016-11-11 15:45:03 -05003245 * INTERNAL_HIDDEN @endcond
3246 */
3247
3248/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003249 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003251 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003252 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003253 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003255 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003256 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003257 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003258#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003259 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003260 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003261
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003262/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003263 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003264 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003265 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003267 * Upon completion, the mutex is available and does not have an owner.
3268 *
3269 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003270 *
3271 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003272 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003273 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003274__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003275
3276/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003277 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003279 * This routine locks @a mutex. If the mutex is locked by another thread,
3280 * the calling thread waits until the mutex becomes available or until
3281 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003282 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003283 * A thread is permitted to lock a mutex it has already locked. The operation
3284 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003285 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003286 * @param mutex Address of the mutex.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003287 * @param timeout Non-negative waiting period to lock the mutex (in
3288 * milliseconds), or one of the special values K_NO_WAIT and
3289 * K_FOREVER.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003290 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003291 * @retval 0 Mutex locked.
3292 * @retval -EBUSY Returned without waiting.
3293 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003294 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003295 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003296__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003297
3298/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003299 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003300 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003301 * This routine unlocks @a mutex. The mutex must already be locked by the
3302 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003303 *
3304 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003305 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003306 * thread.
3307 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003308 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003309 *
3310 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003311 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003312 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003313__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003314
Allan Stephensc98da842016-11-11 15:45:03 -05003315/**
Anas Nashif166f5192018-02-25 08:02:36 -06003316 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003317 */
3318
3319/**
3320 * @cond INTERNAL_HIDDEN
3321 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003322
3323struct k_sem {
3324 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303325 u32_t count;
3326 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003327 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003328
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003329 _OBJECT_TRACING_NEXT_PTR(k_sem)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003330 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003331};
3332
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003333#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003334 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003335 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003336 .count = initial_count, \
3337 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003338 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003339 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003340 }
3341
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003342#define K_SEM_INITIALIZER __DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003343
Allan Stephensc98da842016-11-11 15:45:03 -05003344/**
3345 * INTERNAL_HIDDEN @endcond
3346 */
3347
3348/**
3349 * @defgroup semaphore_apis Semaphore APIs
3350 * @ingroup kernel_apis
3351 * @{
3352 */
3353
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003354/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003355 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003356 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003357 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003359 * @param sem Address of the semaphore.
3360 * @param initial_count Initial semaphore count.
3361 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003362 *
3363 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003364 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003365 */
Andrew Boie99280232017-09-29 14:17:47 -07003366__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
3367 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003368
3369/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003370 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003371 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003372 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003374 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3375 *
3376 * @param sem Address of the semaphore.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003377 * @param timeout Non-negative waiting period to take the semaphore (in
3378 * milliseconds), or one of the special values K_NO_WAIT and
3379 * K_FOREVER.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003380 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003381 * @retval 0 Semaphore taken.
3382 * @retval -EBUSY Returned without waiting.
3383 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003384 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003385 */
Andrew Boie99280232017-09-29 14:17:47 -07003386__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003387
3388/**
3389 * @brief Give a semaphore.
3390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003391 * This routine gives @a sem, unless the semaphore is already at its maximum
3392 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003393 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003394 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003395 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003396 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003397 *
3398 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003399 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003400 */
Andrew Boie99280232017-09-29 14:17:47 -07003401__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003402
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003403/**
3404 * @brief Reset a semaphore's count to zero.
3405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003406 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003407 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003408 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003409 *
3410 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003411 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003412 */
Andrew Boie990bf162017-10-03 12:36:49 -07003413__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003414
Anas Nashif954d5502018-02-25 08:37:28 -06003415/**
3416 * @internal
3417 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003418static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003419{
Patrik Flykt24d71432019-03-26 19:57:45 -06003420 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003421}
3422
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003423/**
3424 * @brief Get a semaphore's count.
3425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003426 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003427 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003428 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003430 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003431 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003432 */
Andrew Boie990bf162017-10-03 12:36:49 -07003433__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003434
Anas Nashif954d5502018-02-25 08:37:28 -06003435/**
3436 * @internal
3437 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003438static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003439{
3440 return sem->count;
3441}
3442
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003443/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003444 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003446 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003447 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003448 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003450 * @param name Name of the semaphore.
3451 * @param initial_count Initial semaphore count.
3452 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003453 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003454 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003455#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003456 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003457 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303458 BUILD_ASSERT(((count_limit) != 0) && \
3459 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003460
Anas Nashif166f5192018-02-25 08:02:36 -06003461/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003462
3463/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003464 * @defgroup msgq_apis Message Queue APIs
3465 * @ingroup kernel_apis
3466 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003467 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003468
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003469/**
3470 * @brief Message Queue Structure
3471 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003472struct k_msgq {
3473 _wait_q_t wait_q;
Andy Rossbe03dbd2018-07-26 10:23:02 -07003474 struct k_spinlock lock;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003475 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003476 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003477 char *buffer_start;
3478 char *buffer_end;
3479 char *read_ptr;
3480 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003481 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003482
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003483 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003484 _OBJECT_TRACING_LINKED_FLAG
Andrew Boie0fe789f2018-04-12 18:35:56 -07003485 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003486};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003487/**
3488 * @cond INTERNAL_HIDDEN
3489 */
3490
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003491
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003492#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003493 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003494 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003495 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003496 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003497 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003498 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003499 .read_ptr = q_buffer, \
3500 .write_ptr = q_buffer, \
3501 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003502 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003503 }
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003504#define K_MSGQ_INITIALIZER __DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003505/**
3506 * INTERNAL_HIDDEN @endcond
3507 */
3508
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003509
Andrew Boie0fe789f2018-04-12 18:35:56 -07003510#define K_MSGQ_FLAG_ALLOC BIT(0)
3511
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003512/**
3513 * @brief Message Queue Attributes
3514 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303515struct k_msgq_attrs {
3516 size_t msg_size;
3517 u32_t max_msgs;
3518 u32_t used_msgs;
3519};
3520
Allan Stephensc98da842016-11-11 15:45:03 -05003521
3522/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003523 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003525 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3526 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003527 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3528 * message is similarly aligned to this boundary, @a q_msg_size must also be
3529 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003531 * The message queue can be accessed outside the module where it is defined
3532 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003533 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003534 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003536 * @param q_name Name of the message queue.
3537 * @param q_msg_size Message size (in bytes).
3538 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003539 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003540 *
3541 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003542 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003543#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3544 static char __noinit __aligned(q_align) \
3545 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3546 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
3547 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003548 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003549
Peter Mitsisd7a37502016-10-13 11:37:40 -04003550/**
3551 * @brief Initialize a message queue.
3552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003553 * This routine initializes a message queue object, prior to its first use.
3554 *
Allan Stephensda827222016-11-09 14:23:58 -06003555 * The message queue's ring buffer must contain space for @a max_msgs messages,
3556 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3557 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3558 * that each message is similarly aligned to this boundary, @a q_msg_size
3559 * must also be a multiple of N.
3560 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003561 * @param q Address of the message queue.
3562 * @param buffer Pointer to ring buffer that holds queued messages.
3563 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003564 * @param max_msgs Maximum number of messages that can be queued.
3565 *
3566 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003567 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003568 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003569void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3570 u32_t max_msgs);
3571
3572/**
3573 * @brief Initialize a message queue.
3574 *
3575 * This routine initializes a message queue object, prior to its first use,
3576 * allocating its internal ring buffer from the calling thread's resource
3577 * pool.
3578 *
3579 * Memory allocated for the ring buffer can be released by calling
3580 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3581 * all of its references.
3582 *
3583 * @param q Address of the message queue.
3584 * @param msg_size Message size (in bytes).
3585 * @param max_msgs Maximum number of messages that can be queued.
3586 *
3587 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3588 * thread's resource pool, or -EINVAL if the size parameters cause
3589 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003590 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003591 */
3592__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3593 u32_t max_msgs);
3594
3595
3596void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003597
3598/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003599 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003600 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003601 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003602 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003603 * @note Can be called by ISRs.
3604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003605 * @param q Address of the message queue.
3606 * @param data Pointer to the message.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003607 * @param timeout Non-negative waiting period to add the message (in
3608 * milliseconds), or one of the special values K_NO_WAIT and
3609 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003610 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003611 * @retval 0 Message sent.
3612 * @retval -ENOMSG Returned without waiting or queue purged.
3613 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003614 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003615 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003616__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003617
3618/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003619 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003621 * This routine receives a message from message queue @a q in a "first in,
3622 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003623 *
Allan Stephensc98da842016-11-11 15:45:03 -05003624 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003626 * @param q Address of the message queue.
3627 * @param data Address of area to hold the received message.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003628 * @param timeout Non-negative waiting period to receive the message (in
3629 * milliseconds), or one of the special values K_NO_WAIT and
3630 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003631 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003632 * @retval 0 Message received.
3633 * @retval -ENOMSG Returned without waiting.
3634 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003635 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003636 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003637__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003638
3639/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003640 * @brief Peek/read a message from a message queue.
3641 *
3642 * This routine reads a message from message queue @a q in a "first in,
3643 * first out" manner and leaves the message in the queue.
3644 *
3645 * @note Can be called by ISRs.
3646 *
3647 * @param q Address of the message queue.
3648 * @param data Address of area to hold the message read from the queue.
3649 *
3650 * @retval 0 Message read.
3651 * @retval -ENOMSG Returned when the queue has no message.
3652 * @req K-MSGQ-002
3653 */
3654__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3655
3656/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003657 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003658 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003659 * This routine discards all unreceived messages in a message queue's ring
3660 * buffer. Any threads that are blocked waiting to send a message to the
3661 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003662 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003663 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003664 *
3665 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003666 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003667 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003668__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003669
Peter Mitsis67be2492016-10-07 11:44:34 -04003670/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003671 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003672 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003673 * This routine returns the number of unused entries in a message queue's
3674 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003675 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003676 * @param q Address of the message queue.
3677 *
3678 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003679 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003680 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003681__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3682
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303683/**
3684 * @brief Get basic attributes of a message queue.
3685 *
3686 * This routine fetches basic attributes of message queue into attr argument.
3687 *
3688 * @param q Address of the message queue.
3689 * @param attrs pointer to message queue attribute structure.
3690 *
3691 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003692 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303693 */
3694__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3695
3696
Patrik Flykt4344e272019-03-08 14:19:05 -07003697static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003698{
3699 return q->max_msgs - q->used_msgs;
3700}
3701
Peter Mitsisd7a37502016-10-13 11:37:40 -04003702/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003703 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003705 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003706 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003707 * @param q Address of the message queue.
3708 *
3709 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003710 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003711 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003712__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3713
Patrik Flykt4344e272019-03-08 14:19:05 -07003714static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003715{
3716 return q->used_msgs;
3717}
3718
Anas Nashif166f5192018-02-25 08:02:36 -06003719/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003720
3721/**
3722 * @defgroup mem_pool_apis Memory Pool APIs
3723 * @ingroup kernel_apis
3724 * @{
3725 */
3726
Andy Ross73cb9582017-05-09 10:42:39 -07003727/* Note on sizing: the use of a 20 bit field for block means that,
3728 * assuming a reasonable minimum block size of 16 bytes, we're limited
3729 * to 16M of memory managed by a single pool. Long term it would be
3730 * good to move to a variable bit size based on configuration.
3731 */
3732struct k_mem_block_id {
3733 u32_t pool : 8;
3734 u32_t level : 4;
3735 u32_t block : 20;
3736};
3737
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003738struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003739 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003740 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003741};
3742
Anas Nashif166f5192018-02-25 08:02:36 -06003743/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003744
3745/**
3746 * @defgroup mailbox_apis Mailbox APIs
3747 * @ingroup kernel_apis
3748 * @{
3749 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003750
3751struct k_mbox_msg {
3752 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003753 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003754 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003755 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003756 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003757 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003758 /** sender's message data buffer */
3759 void *tx_data;
3760 /** internal use only - needed for legacy API support */
3761 void *_rx_data;
3762 /** message data block descriptor */
3763 struct k_mem_block tx_block;
3764 /** source thread id */
3765 k_tid_t rx_source_thread;
3766 /** target thread id */
3767 k_tid_t tx_target_thread;
3768 /** internal use only - thread waiting on send (may be a dummy) */
3769 k_tid_t _syncing_thread;
3770#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3771 /** internal use only - semaphore used during asynchronous send */
3772 struct k_sem *_async_sem;
3773#endif
3774};
3775
3776struct k_mbox {
3777 _wait_q_t tx_msg_queue;
3778 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003779 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003780
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003781 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003782 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003783};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003784/**
3785 * @cond INTERNAL_HIDDEN
3786 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003787
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003788#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003789 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003790 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3791 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003792 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003793 }
3794
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003795#define K_MBOX_INITIALIZER __DEPRECATED_MACRO _K_MBOX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003796
Peter Mitsis12092702016-10-14 12:57:23 -04003797/**
Allan Stephensc98da842016-11-11 15:45:03 -05003798 * INTERNAL_HIDDEN @endcond
3799 */
3800
3801/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003802 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003804 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003805 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003806 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003807 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003808 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003809 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003810 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003811#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003812 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003813 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003814
Peter Mitsis12092702016-10-14 12:57:23 -04003815/**
3816 * @brief Initialize a mailbox.
3817 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003818 * This routine initializes a mailbox object, prior to its first use.
3819 *
3820 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003821 *
3822 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003823 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003824 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003825extern void k_mbox_init(struct k_mbox *mbox);
3826
Peter Mitsis12092702016-10-14 12:57:23 -04003827/**
3828 * @brief Send a mailbox message in a synchronous manner.
3829 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003830 * This routine sends a message to @a mbox and waits for a receiver to both
3831 * receive and process it. The message data may be in a buffer, in a memory
3832 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003833 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003834 * @param mbox Address of the mailbox.
3835 * @param tx_msg Address of the transmit message descriptor.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003836 * @param timeout Non-negative waiting period for the message to be received (in
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003837 * milliseconds), or one of the special values K_NO_WAIT
3838 * and K_FOREVER. Once the message has been received,
3839 * this routine waits as long as necessary for the message
3840 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003841 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003842 * @retval 0 Message sent.
3843 * @retval -ENOMSG Returned without waiting.
3844 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003845 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003846 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003847extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003848 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003849
Peter Mitsis12092702016-10-14 12:57:23 -04003850/**
3851 * @brief Send a mailbox message in an asynchronous manner.
3852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003853 * This routine sends a message to @a mbox without waiting for a receiver
3854 * to process it. The message data may be in a buffer, in a memory pool block,
3855 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3856 * will be given when the message has been both received and completely
3857 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003858 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003859 * @param mbox Address of the mailbox.
3860 * @param tx_msg Address of the transmit message descriptor.
3861 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003862 *
3863 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003864 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003865 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003866extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003867 struct k_sem *sem);
3868
Peter Mitsis12092702016-10-14 12:57:23 -04003869/**
3870 * @brief Receive a mailbox message.
3871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003872 * This routine receives a message from @a mbox, then optionally retrieves
3873 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003875 * @param mbox Address of the mailbox.
3876 * @param rx_msg Address of the receive message descriptor.
3877 * @param buffer Address of the buffer to receive data, or NULL to defer data
3878 * retrieval and message disposal until later.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003879 * @param timeout Non-negative waiting period for a message to be received (in
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003880 * milliseconds), or one of the special values K_NO_WAIT
3881 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003882 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003883 * @retval 0 Message received.
3884 * @retval -ENOMSG Returned without waiting.
3885 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003886 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003887 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003888extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003889 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003890
3891/**
3892 * @brief Retrieve mailbox message data into a buffer.
3893 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003894 * This routine completes the processing of a received message by retrieving
3895 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003896 *
3897 * Alternatively, this routine can be used to dispose of a received message
3898 * without retrieving its data.
3899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003900 * @param rx_msg Address of the receive message descriptor.
3901 * @param buffer Address of the buffer to receive data, or NULL to discard
3902 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003903 *
3904 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003905 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003906 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003907extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003908
3909/**
3910 * @brief Retrieve mailbox message data into a memory pool block.
3911 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003912 * This routine completes the processing of a received message by retrieving
3913 * its data into a memory pool block, then disposing of the message.
3914 * The memory pool block that results from successful retrieval must be
3915 * returned to the pool once the data has been processed, even in cases
3916 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003917 *
3918 * Alternatively, this routine can be used to dispose of a received message
3919 * without retrieving its data. In this case there is no need to return a
3920 * memory pool block to the pool.
3921 *
3922 * This routine allocates a new memory pool block for the data only if the
3923 * data is not already in one. If a new block cannot be allocated, the routine
3924 * returns a failure code and the received message is left unchanged. This
3925 * permits the caller to reattempt data retrieval at a later time or to dispose
3926 * of the received message without retrieving its data.
3927 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003928 * @param rx_msg Address of a receive message descriptor.
3929 * @param pool Address of memory pool, or NULL to discard data.
3930 * @param block Address of the area to hold memory pool block info.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003931 * @param timeout Non-negative waiting period to wait for a memory pool block
3932 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003933 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003934 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003935 * @retval 0 Data retrieved.
3936 * @retval -ENOMEM Returned without waiting.
3937 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003938 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003939 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003940extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003941 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003942 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003943
Anas Nashif166f5192018-02-25 08:02:36 -06003944/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003945
3946/**
Anas Nashifce78d162018-05-24 12:43:11 -05003947 * @defgroup pipe_apis Pipe APIs
3948 * @ingroup kernel_apis
3949 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003950 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003951
Anas Nashifce78d162018-05-24 12:43:11 -05003952/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003953struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003954 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3955 size_t size; /**< Buffer size */
3956 size_t bytes_used; /**< # bytes used in buffer */
3957 size_t read_index; /**< Where in buffer to read from */
3958 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08003959 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003960
3961 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003962 _wait_q_t readers; /**< Reader wait queue */
3963 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003964 } wait_q;
3965
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003966 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08003967 _OBJECT_TRACING_LINKED_FLAG
Anas Nashifce78d162018-05-24 12:43:11 -05003968 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003969};
3970
Anas Nashifce78d162018-05-24 12:43:11 -05003971/**
3972 * @cond INTERNAL_HIDDEN
3973 */
3974#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3975
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003976#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
3977 { \
3978 .buffer = pipe_buffer, \
3979 .size = pipe_buffer_size, \
3980 .bytes_used = 0, \
3981 .read_index = 0, \
3982 .write_index = 0, \
3983 .lock = {}, \
3984 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003985 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
3986 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003987 }, \
3988 _OBJECT_TRACING_INIT \
3989 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003990 }
3991
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003992#define K_PIPE_INITIALIZER __DEPRECATED_MACRO _K_PIPE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003993
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003994/**
Allan Stephensc98da842016-11-11 15:45:03 -05003995 * INTERNAL_HIDDEN @endcond
3996 */
3997
3998/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003999 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004001 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004002 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004003 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004005 * @param name Name of the pipe.
4006 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
4007 * or zero if no ring buffer is used.
4008 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004009 *
4010 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004011 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004012#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08004013 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07004014 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004015 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004016 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004017
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004018/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004019 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004021 * This routine initializes a pipe object, prior to its first use.
4022 *
4023 * @param pipe Address of the pipe.
4024 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
4025 * is used.
4026 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4027 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004028 *
4029 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004030 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004031 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004032void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
4033
4034/**
4035 * @brief Release a pipe's allocated buffer
4036 *
4037 * If a pipe object was given a dynamically allocated buffer via
4038 * k_pipe_alloc_init(), this will free it. This function does nothing
4039 * if the buffer wasn't dynamically allocated.
4040 *
4041 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004042 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004043 */
4044void k_pipe_cleanup(struct k_pipe *pipe);
4045
4046/**
4047 * @brief Initialize a pipe and allocate a buffer for it
4048 *
4049 * Storage for the buffer region will be allocated from the calling thread's
4050 * resource pool. This memory will be released if k_pipe_cleanup() is called,
4051 * or userspace is enabled and the pipe object loses all references to it.
4052 *
4053 * This function should only be called on uninitialized pipe objects.
4054 *
4055 * @param pipe Address of the pipe.
4056 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4057 * buffer is used.
4058 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004059 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004060 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004061 */
4062__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004063
4064/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004065 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004066 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004067 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004069 * @param pipe Address of the pipe.
4070 * @param data Address of data to write.
4071 * @param bytes_to_write Size of data (in bytes).
4072 * @param bytes_written Address of area to hold the number of bytes written.
4073 * @param min_xfer Minimum number of bytes to write.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004074 * @param timeout Non-negative waiting period to wait for the data to be written
4075 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004076 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004077 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004078 * @retval 0 At least @a min_xfer bytes of data were written.
4079 * @retval -EIO Returned without waiting; zero data bytes were written.
4080 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004081 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004082 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004083 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004084__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
4085 size_t bytes_to_write, size_t *bytes_written,
4086 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004087
4088/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004089 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004090 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004091 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004092 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004093 * @param pipe Address of the pipe.
4094 * @param data Address to place the data read from pipe.
4095 * @param bytes_to_read Maximum number of data bytes to read.
4096 * @param bytes_read Address of area to hold the number of bytes read.
4097 * @param min_xfer Minimum number of data bytes to read.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004098 * @param timeout Non-negative waiting period to wait for the data to be read
4099 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004100 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004101 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004102 * @retval 0 At least @a min_xfer bytes of data were read.
4103 * @retval -EIO Returned without waiting; zero data bytes were read.
4104 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004105 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004106 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004107 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004108__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
4109 size_t bytes_to_read, size_t *bytes_read,
4110 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004111
4112/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004113 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004114 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004115 * This routine writes the data contained in a memory block to @a pipe.
4116 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004117 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004119 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004120 * @param block Memory block containing data to send
4121 * @param size Number of data bytes in memory block to send
4122 * @param sem Semaphore to signal upon completion (else NULL)
4123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004124 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004125 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004126 */
4127extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
4128 size_t size, struct k_sem *sem);
4129
Anas Nashif166f5192018-02-25 08:02:36 -06004130/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004131
Allan Stephensc98da842016-11-11 15:45:03 -05004132/**
4133 * @cond INTERNAL_HIDDEN
4134 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004135
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004136struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004137 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05004138 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04004139 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004140 char *buffer;
4141 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05004142 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004143
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004144 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Shih-Wei Teng5ebceeb2019-10-08 14:37:47 +08004145 _OBJECT_TRACING_LINKED_FLAG
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004146};
4147
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004148#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004149 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004150 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004151 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004152 .num_blocks = slab_num_blocks, \
4153 .block_size = slab_block_size, \
4154 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004155 .free_list = NULL, \
4156 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05004157 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004158 }
4159
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05004160#define K_MEM_SLAB_INITIALIZER __DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004161
4162
Peter Mitsis578f9112016-10-07 13:50:31 -04004163/**
Allan Stephensc98da842016-11-11 15:45:03 -05004164 * INTERNAL_HIDDEN @endcond
4165 */
4166
4167/**
4168 * @defgroup mem_slab_apis Memory Slab APIs
4169 * @ingroup kernel_apis
4170 * @{
4171 */
4172
4173/**
Allan Stephensda827222016-11-09 14:23:58 -06004174 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04004175 *
Allan Stephensda827222016-11-09 14:23:58 -06004176 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004177 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06004178 * @a slab_align -byte boundary. To ensure that each memory block is similarly
4179 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004180 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04004181 *
Allan Stephensda827222016-11-09 14:23:58 -06004182 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004183 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004184 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004185 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004186 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004187 * @param name Name of the memory slab.
4188 * @param slab_block_size Size of each memory block (in bytes).
4189 * @param slab_num_blocks Number memory blocks.
4190 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004191 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04004192 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004193#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004194 char __noinit __aligned(WB_UP(slab_align)) \
4195 _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004196 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004197 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004198 WB_UP(slab_block_size), slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004199
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004200/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004201 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004202 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004203 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004204 *
Allan Stephensda827222016-11-09 14:23:58 -06004205 * The memory slab's buffer contains @a slab_num_blocks memory blocks
4206 * that are @a slab_block_size bytes long. The buffer must be aligned to an
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004207 * N-byte boundary matching a word boundary, where N is a power of 2
4208 * (i.e. 4 on 32-bit systems, 8, 16, ...).
Allan Stephensda827222016-11-09 14:23:58 -06004209 * To ensure that each memory block is similarly aligned to this boundary,
4210 * @a slab_block_size must also be a multiple of N.
4211 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004212 * @param slab Address of the memory slab.
4213 * @param buffer Pointer to buffer used for the memory blocks.
4214 * @param block_size Size of each memory block (in bytes).
4215 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004216 *
4217 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004218 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004219 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004220extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05004221 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004222
4223/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004224 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004225 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004226 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004227 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004228 * @param slab Address of the memory slab.
4229 * @param mem Pointer to block address area.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004230 * @param timeout Non-negative waiting period to wait for operation to complete
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004231 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4232 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004233 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004234 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004235 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004236 * @retval -ENOMEM Returned without waiting.
4237 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004238 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004239 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004240extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05004241 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004242
4243/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004244 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004246 * This routine releases a previously allocated memory block back to its
4247 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004248 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004249 * @param slab Address of the memory slab.
4250 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004251 *
4252 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004253 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004254 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004255extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004256
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004257/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004258 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004259 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004260 * This routine gets the number of memory blocks that are currently
4261 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004262 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004263 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004264 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004265 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004266 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004267 */
Kumar Galacc334c72017-04-21 10:55:34 -05004268static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004269{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004270 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004271}
4272
Peter Mitsisc001aa82016-10-13 13:53:37 -04004273/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004274 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004275 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004276 * This routine gets the number of memory blocks that are currently
4277 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004279 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004280 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004281 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004282 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004283 */
Kumar Galacc334c72017-04-21 10:55:34 -05004284static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004285{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004286 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004287}
4288
Anas Nashif166f5192018-02-25 08:02:36 -06004289/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004290
4291/**
4292 * @cond INTERNAL_HIDDEN
4293 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004294
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004295struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004296 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004297 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004298};
4299
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004300/**
Allan Stephensc98da842016-11-11 15:45:03 -05004301 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004302 */
4303
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004304/**
Allan Stephensc98da842016-11-11 15:45:03 -05004305 * @addtogroup mem_pool_apis
4306 * @{
4307 */
4308
4309/**
4310 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004311 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004312 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4313 * long. The memory pool allows blocks to be repeatedly partitioned into
4314 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004315 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004316 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004317 * If the pool is to be accessed outside the module where it is defined, it
4318 * can be declared via
4319 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004320 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004321 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004322 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004323 * @param minsz Size of the smallest blocks in the pool (in bytes).
4324 * @param maxsz Size of the largest blocks in the pool (in bytes).
4325 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004326 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004327 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004328 */
Andy Ross73cb9582017-05-09 10:42:39 -07004329#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004330 char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \
Andy Ross73cb9582017-05-09 10:42:39 -07004331 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004332 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004333 Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004334 .base = { \
4335 .buf = _mpool_buf_##name, \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004336 .max_sz = WB_UP(maxsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004337 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004338 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004339 .levels = _mpool_lvls_##name, \
4340 .flags = SYS_MEM_POOL_KERNEL \
4341 } \
Johann Fischer223a2b92019-07-04 15:55:20 +02004342 }; \
Nicolas Pitreb2a022b2019-09-26 16:36:40 -04004343 BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004344
Peter Mitsis937042c2016-10-13 13:18:26 -04004345/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004346 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004347 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004348 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004350 * @param pool Address of the memory pool.
4351 * @param block Pointer to block descriptor for the allocated memory.
4352 * @param size Amount of memory to allocate (in bytes).
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004353 * @param timeout Non-negative waiting period to wait for operation to complete
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004354 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4355 * or K_FOREVER to wait as long as necessary.
4356 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004357 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004358 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004359 * @retval -ENOMEM Returned without waiting.
4360 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004361 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004362 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004363extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004364 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004365
4366/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004367 * @brief Allocate memory from a memory pool with malloc() semantics
4368 *
4369 * Such memory must be released using k_free().
4370 *
4371 * @param pool Address of the memory pool.
4372 * @param size Amount of memory to allocate (in bytes).
4373 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004374 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004375 */
4376extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4377
4378/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004379 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004380 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004381 * This routine releases a previously allocated memory block back to its
4382 * memory pool.
4383 *
4384 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004385 *
4386 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004387 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004388 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004389extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004390
4391/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004392 * @brief Free memory allocated from a memory pool.
4393 *
4394 * This routine releases a previously allocated memory block back to its
4395 * memory pool
4396 *
4397 * @param id Memory block identifier.
4398 *
4399 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004400 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004401 */
4402extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4403
4404/**
Anas Nashif166f5192018-02-25 08:02:36 -06004405 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004406 */
4407
4408/**
4409 * @defgroup heap_apis Heap Memory Pool APIs
4410 * @ingroup kernel_apis
4411 * @{
4412 */
4413
4414/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004415 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004416 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004417 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004418 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004419 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004420 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004422 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004423 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004424 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004425extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004426
4427/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004428 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004429 *
4430 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004431 * returned must have been allocated from the heap memory pool or
4432 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004433 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004434 * If @a ptr is NULL, no operation is performed.
4435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004436 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004437 *
4438 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004439 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004440 */
4441extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004442
Allan Stephensc98da842016-11-11 15:45:03 -05004443/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004444 * @brief Allocate memory from heap, array style
4445 *
4446 * This routine provides traditional calloc() semantics. Memory is
4447 * allocated from the heap memory pool and zeroed.
4448 *
4449 * @param nmemb Number of elements in the requested array
4450 * @param size Size of each array element (in bytes).
4451 *
4452 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004453 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004454 */
4455extern void *k_calloc(size_t nmemb, size_t size);
4456
Anas Nashif166f5192018-02-25 08:02:36 -06004457/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004458
Benjamin Walshacc68c12017-01-29 18:57:45 -05004459/* polling API - PRIVATE */
4460
Benjamin Walshb0179862017-02-02 16:39:57 -05004461#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004462#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004463#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004464#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004465#endif
4466
Benjamin Walshacc68c12017-01-29 18:57:45 -05004467/* private - types bit positions */
4468enum _poll_types_bits {
4469 /* can be used to ignore an event */
4470 _POLL_TYPE_IGNORE,
4471
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004472 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004473 _POLL_TYPE_SIGNAL,
4474
4475 /* semaphore availability */
4476 _POLL_TYPE_SEM_AVAILABLE,
4477
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004478 /* queue/fifo/lifo data availability */
4479 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004480
4481 _POLL_NUM_TYPES
4482};
4483
Patrik Flykt4344e272019-03-08 14:19:05 -07004484#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004485
4486/* private - states bit positions */
4487enum _poll_states_bits {
4488 /* default state when creating event */
4489 _POLL_STATE_NOT_READY,
4490
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004491 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004492 _POLL_STATE_SIGNALED,
4493
4494 /* semaphore is available */
4495 _POLL_STATE_SEM_AVAILABLE,
4496
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004497 /* data is available to read on queue/fifo/lifo */
4498 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004499
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004500 /* queue/fifo/lifo wait was cancelled */
4501 _POLL_STATE_CANCELLED,
4502
Benjamin Walshacc68c12017-01-29 18:57:45 -05004503 _POLL_NUM_STATES
4504};
4505
Patrik Flykt4344e272019-03-08 14:19:05 -07004506#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004507
4508#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004509 (32 - (0 \
4510 + 8 /* tag */ \
4511 + _POLL_NUM_TYPES \
4512 + _POLL_NUM_STATES \
4513 + 1 /* modes */ \
4514 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004515
Benjamin Walshacc68c12017-01-29 18:57:45 -05004516/* end of polling API - PRIVATE */
4517
4518
4519/**
4520 * @defgroup poll_apis Async polling APIs
4521 * @ingroup kernel_apis
4522 * @{
4523 */
4524
4525/* Public polling API */
4526
4527/* public - values for k_poll_event.type bitfield */
4528#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004529#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4530#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4531#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004532#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004533
4534/* public - polling modes */
4535enum k_poll_modes {
4536 /* polling thread does not take ownership of objects when available */
4537 K_POLL_MODE_NOTIFY_ONLY = 0,
4538
4539 K_POLL_NUM_MODES
4540};
4541
4542/* public - values for k_poll_event.state bitfield */
4543#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004544#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4545#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4546#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004547#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004548#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004549
4550/* public - poll signal object */
4551struct k_poll_signal {
4552 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004553 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004554
4555 /*
4556 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4557 * user resets it to 0.
4558 */
4559 unsigned int signaled;
4560
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004561 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004562 int result;
4563};
4564
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004565#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004566 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004567 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004568 .signaled = 0, \
4569 .result = 0, \
4570 }
4571
4572struct k_poll_event {
4573 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004574 sys_dnode_t _node;
4575
4576 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004577 struct _poller *poller;
4578
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004579 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004580 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004581
Benjamin Walshacc68c12017-01-29 18:57:45 -05004582 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004583 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004584
4585 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004586 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004587
4588 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004589 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004590
4591 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004592 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004593
4594 /* per-type data */
4595 union {
4596 void *obj;
4597 struct k_poll_signal *signal;
4598 struct k_sem *sem;
4599 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004600 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004601 };
4602};
4603
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004604#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004605 { \
4606 .poller = NULL, \
4607 .type = event_type, \
4608 .state = K_POLL_STATE_NOT_READY, \
4609 .mode = event_mode, \
4610 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004611 { .obj = event_obj }, \
4612 }
4613
4614#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4615 event_tag) \
4616 { \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004617 .tag = event_tag, \
Markus Fuchsbe21d3f2019-10-09 21:31:25 +02004618 .type = event_type, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004619 .state = K_POLL_STATE_NOT_READY, \
4620 .mode = event_mode, \
4621 .unused = 0, \
4622 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004623 }
4624
4625/**
4626 * @brief Initialize one struct k_poll_event instance
4627 *
4628 * After this routine is called on a poll event, the event it ready to be
4629 * placed in an event array to be passed to k_poll().
4630 *
4631 * @param event The event to initialize.
4632 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4633 * values. Only values that apply to the same object being polled
4634 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4635 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004636 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004637 * @param obj Kernel object or poll signal.
4638 *
4639 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004640 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004641 */
4642
Kumar Galacc334c72017-04-21 10:55:34 -05004643extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004644 int mode, void *obj);
4645
4646/**
4647 * @brief Wait for one or many of multiple poll events to occur
4648 *
4649 * This routine allows a thread to wait concurrently for one or many of
4650 * multiple poll events to have occurred. Such events can be a kernel object
4651 * being available, like a semaphore, or a poll signal event.
4652 *
4653 * When an event notifies that a kernel object is available, the kernel object
4654 * is not "given" to the thread calling k_poll(): it merely signals the fact
4655 * that the object was available when the k_poll() call was in effect. Also,
4656 * all threads trying to acquire an object the regular way, i.e. by pending on
4657 * the object, have precedence over the thread polling on the object. This
4658 * means that the polling thread will never get the poll event on an object
4659 * until the object becomes available and its pend queue is empty. For this
4660 * reason, the k_poll() call is more effective when the objects being polled
4661 * only have one thread, the polling thread, trying to acquire them.
4662 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004663 * When k_poll() returns 0, the caller should loop on all the events that were
4664 * passed to k_poll() and check the state field for the values that were
4665 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004666 *
4667 * Before being reused for another call to k_poll(), the user has to reset the
4668 * state field to K_POLL_STATE_NOT_READY.
4669 *
Andrew Boie3772f772018-05-07 16:52:57 -07004670 * When called from user mode, a temporary memory allocation is required from
4671 * the caller's resource pool.
4672 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004673 * @param events An array of pointers to events to be polled for.
4674 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004675 * @param timeout Non-negative waiting period for an event to be ready (in
4676 * milliseconds), or one of the special values K_NO_WAIT and
4677 * K_FOREVER.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004678 *
4679 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004680 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004681 * @retval -EINTR Polling has been interrupted, e.g. with
4682 * k_queue_cancel_wait(). All output events are still set and valid,
4683 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4684 * words, -EINTR status means that at least one of output events is
4685 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004686 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4687 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004688 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004689 */
4690
Andrew Boie3772f772018-05-07 16:52:57 -07004691__syscall int k_poll(struct k_poll_event *events, int num_events,
4692 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004693
4694/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004695 * @brief Initialize a poll signal object.
4696 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004697 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004698 *
4699 * @param signal A poll signal.
4700 *
4701 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004702 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004703 */
4704
Andrew Boie3772f772018-05-07 16:52:57 -07004705__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4706
4707/*
4708 * @brief Reset a poll signal object's state to unsignaled.
4709 *
4710 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004711 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004712 */
4713__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4714
Patrik Flykt4344e272019-03-08 14:19:05 -07004715static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004716{
Patrik Flykt24d71432019-03-26 19:57:45 -06004717 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004718}
4719
4720/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004721 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004722 *
4723 * @param signal A poll signal object
4724 * @param signaled An integer buffer which will be written nonzero if the
4725 * object was signaled
4726 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004727 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004728 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004729 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004730 */
4731__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4732 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004733
4734/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004735 * @brief Signal a poll signal object.
4736 *
4737 * This routine makes ready a poll signal, which is basically a poll event of
4738 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4739 * made ready to run. A @a result value can be specified.
4740 *
4741 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004742 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004743 * k_poll_signal_reset(). It thus has to be reset by the user before being
4744 * passed again to k_poll() or k_poll() will consider it being signaled, and
4745 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004746 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004747 * @note The result is stored and the 'signaled' field is set even if
4748 * this function returns an error indicating that an expiring poll was
4749 * not notified. The next k_poll() will detect the missed raise.
4750 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004751 * @param signal A poll signal.
4752 * @param result The value to store in the result field of the signal.
4753 *
4754 * @retval 0 The signal was delivered successfully.
4755 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004756 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004757 */
4758
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004759__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004760
Anas Nashif954d5502018-02-25 08:37:28 -06004761/**
4762 * @internal
4763 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004764extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004765
Anas Nashif166f5192018-02-25 08:02:36 -06004766/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004767
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004768/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004769 * @defgroup cpu_idle_apis CPU Idling APIs
4770 * @ingroup kernel_apis
4771 * @{
4772 */
Anas Nashif30c3cff2019-01-22 08:18:13 -05004773/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004774 * @brief Make the CPU idle.
4775 *
4776 * This function makes the CPU idle until an event wakes it up.
4777 *
4778 * In a regular system, the idle thread should be the only thread responsible
4779 * for making the CPU idle and triggering any type of power management.
4780 * However, in some more constrained systems, such as a single-threaded system,
4781 * the only thread would be responsible for this if needed.
4782 *
4783 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004784 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004785 */
Andrew Boie07525a32019-09-21 16:17:23 -07004786static inline void k_cpu_idle(void)
4787{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004788 arch_cpu_idle();
Andrew Boie07525a32019-09-21 16:17:23 -07004789}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004790
4791/**
4792 * @brief Make the CPU idle in an atomic fashion.
4793 *
4794 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4795 * must be done atomically before making the CPU idle.
4796 *
4797 * @param key Interrupt locking key obtained from irq_lock().
4798 *
4799 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004800 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004801 */
Andrew Boie07525a32019-09-21 16:17:23 -07004802static inline void k_cpu_atomic_idle(unsigned int key)
4803{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004804 arch_cpu_atomic_idle(key);
Andrew Boie07525a32019-09-21 16:17:23 -07004805}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004806
Anas Nashif30c3cff2019-01-22 08:18:13 -05004807/**
4808 * @}
4809 */
Anas Nashif954d5502018-02-25 08:37:28 -06004810
4811/**
4812 * @internal
4813 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004814extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004815
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004816#ifdef ARCH_EXCEPT
Ioannis Glaropoulosdf029232019-10-07 11:24:36 +02004817/* This architecture has direct support for triggering a CPU exception */
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004818#define z_except_reason(reason) ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004819#else
4820
Andrew Boiecdb94d62017-04-18 15:22:05 -07004821/* NOTE: This is the implementation for arches that do not implement
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004822 * ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004823 *
4824 * We won't have a real exception frame to determine the PC value when
4825 * the oops occurred, so print file and line number before we jump into
4826 * the fatal error handler.
4827 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004828#define z_except_reason(reason) do { \
Andrew Boiecdb94d62017-04-18 15:22:05 -07004829 printk("@ %s:%d:\n", __FILE__, __LINE__); \
Andrew Boie56236372019-07-15 15:22:29 -07004830 z_fatal_error(reason, NULL); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004831 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004832
4833#endif /* _ARCH__EXCEPT */
4834
4835/**
4836 * @brief Fatally terminate a thread
4837 *
4838 * This should be called when a thread has encountered an unrecoverable
4839 * runtime condition and needs to terminate. What this ultimately
4840 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004841 * will be called will reason code K_ERR_KERNEL_OOPS.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004842 *
4843 * If this is called from ISR context, the default system fatal error handler
4844 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004845 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004846 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004847#define k_oops() z_except_reason(K_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004848
4849/**
4850 * @brief Fatally terminate the system
4851 *
4852 * This should be called when the Zephyr kernel has encountered an
4853 * unrecoverable runtime condition and needs to terminate. What this ultimately
4854 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004855 * will be called will reason code K_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004856 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004857 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004858#define k_panic() z_except_reason(K_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004859
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004860/*
4861 * private APIs that are utilized by one or more public APIs
4862 */
4863
Stephanos Ioannidis2d746042019-10-25 00:08:21 +09004864/**
4865 * @internal
4866 */
4867extern void z_init_thread_base(struct _thread_base *thread_base,
4868 int priority, u32_t initial_state,
4869 unsigned int options);
4870
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004871#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004872/**
4873 * @internal
4874 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004875extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004876#else
Anas Nashif954d5502018-02-25 08:37:28 -06004877/**
4878 * @internal
4879 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004880#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004881#endif
4882
Anas Nashif954d5502018-02-25 08:37:28 -06004883/**
4884 * @internal
4885 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004886extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004887/**
4888 * @internal
4889 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004890extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004891
Andrew Boiedc5d9352017-06-02 12:56:47 -07004892/* arch/cpu.h may declare an architecture or platform-specific macro
4893 * for properly declaring stacks, compatible with MMU/MPU constraints if
4894 * enabled
4895 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004896
4897/**
4898 * @brief Obtain an extern reference to a stack
4899 *
4900 * This macro properly brings the symbol of a thread stack declared
4901 * elsewhere into scope.
4902 *
4903 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004904 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004905 */
4906#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4907
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004908#ifdef ARCH_THREAD_STACK_DEFINE
4909#define K_THREAD_STACK_DEFINE(sym, size) ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004910#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004911 ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4912#define K_THREAD_STACK_LEN(size) ARCH_THREAD_STACK_LEN(size)
4913#define K_THREAD_STACK_MEMBER(sym, size) ARCH_THREAD_STACK_MEMBER(sym, size)
4914#define K_THREAD_STACK_SIZEOF(sym) ARCH_THREAD_STACK_SIZEOF(sym)
4915#define K_THREAD_STACK_RESERVED ARCH_THREAD_STACK_RESERVED
Andrew Boie4e5c0932019-04-04 12:05:28 -07004916static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004917{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004918 return ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07004919}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004920#else
4921/**
4922 * @brief Declare a toplevel thread stack memory region
4923 *
4924 * This declares a region of memory suitable for use as a thread's stack.
4925 *
4926 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4927 * 'noinit' section so that it isn't zeroed at boot
4928 *
Andrew Boie507852a2017-07-25 18:47:07 -07004929 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004930 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07004931 * inside needs to be examined, examine thread->stack_info for the associated
4932 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004933 *
4934 * It is legal to precede this definition with the 'static' keyword.
4935 *
4936 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4937 * parameter of k_thread_create(), it may not be the same as the
4938 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4939 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004940 * Some arches may round the size of the usable stack region up to satisfy
4941 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4942 * size.
4943 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004944 * @param sym Thread stack symbol name
4945 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004946 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004947 */
4948#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004949 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004950
4951/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304952 * @brief Calculate size of stacks to be allocated in a stack array
4953 *
4954 * This macro calculates the size to be allocated for the stacks
4955 * inside a stack array. It accepts the indicated "size" as a parameter
4956 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4957 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4958 *
4959 * @param size Size of the stack memory region
4960 * @req K-TSTACK-001
4961 */
4962#define K_THREAD_STACK_LEN(size) (size)
4963
4964/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004965 * @brief Declare a toplevel array of thread stack memory regions
4966 *
4967 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4968 * definition for additional details and constraints.
4969 *
4970 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4971 * 'noinit' section so that it isn't zeroed at boot
4972 *
4973 * @param sym Thread stack symbol name
4974 * @param nmemb Number of stacks to declare
4975 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004976 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004977 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004978#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004979 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304980 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004981
4982/**
4983 * @brief Declare an embedded stack memory region
4984 *
4985 * Used for stacks embedded within other data structures. Use is highly
4986 * discouraged but in some cases necessary. For memory protection scenarios,
4987 * it is very important that any RAM preceding this member not be writable
4988 * by threads else a stack overflow will lead to silent corruption. In other
4989 * words, the containing data structure should live in RAM owned by the kernel.
4990 *
4991 * @param sym Thread stack symbol name
4992 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004993 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004994 */
4995#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004996 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004997
4998/**
4999 * @brief Return the size in bytes of a stack memory region
5000 *
5001 * Convenience macro for passing the desired stack size to k_thread_create()
5002 * since the underlying implementation may actually create something larger
5003 * (for instance a guard area).
5004 *
Andrew Boiee2d77912018-05-30 09:45:35 -07005005 * The value returned here is not guaranteed to match the 'size' parameter
5006 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005007 *
5008 * @param sym Stack memory symbol
5009 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005010 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005011 */
5012#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
5013
Andrew Boie575abc02019-03-19 10:42:24 -07005014
5015/**
5016 * @brief Indicate how much additional memory is reserved for stack objects
5017 *
5018 * Any given stack declaration may have additional memory in it for guard
5019 * areas or supervisor mode stacks. This macro indicates how much space
5020 * is reserved for this. The memory reserved may not be contiguous within
5021 * the stack object, and does not account for additional space used due to
5022 * enforce alignment.
5023 */
5024#define K_THREAD_STACK_RESERVED 0
5025
Andrew Boiedc5d9352017-06-02 12:56:47 -07005026/**
5027 * @brief Get a pointer to the physical stack buffer
5028 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07005029 * This macro is deprecated. If a stack buffer needs to be examined, the
5030 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005031 *
5032 * @param sym Declared stack symbol name
5033 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005034 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005035 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07005036static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07005037{
5038 return (char *)sym;
5039}
Andrew Boiedc5d9352017-06-02 12:56:47 -07005040
5041#endif /* _ARCH_DECLARE_STACK */
5042
Chunlin Hane9c97022017-07-07 20:29:30 +08005043/**
5044 * @defgroup mem_domain_apis Memory domain APIs
5045 * @ingroup kernel_apis
5046 * @{
5047 */
5048
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005049/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005050 * @def K_MEM_PARTITION_DEFINE
5051 * @brief Used to declare a memory partition
5052 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005053 */
5054#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
5055#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
5056 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08005057 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005058 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005059#else
5060#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08005061 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005062 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005063#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
5064
Chunlin Hane9c97022017-07-07 20:29:30 +08005065/* memory partition */
5066struct k_mem_partition {
5067 /* start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005068 uintptr_t start;
Chunlin Hane9c97022017-07-07 20:29:30 +08005069 /* size of memory partition */
5070 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005071#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08005072 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305073 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005074#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08005075};
5076
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02005077/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05305078 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005079struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305080#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08005081 /* partitions in the domain */
5082 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305083#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08005084 /* domain q */
5085 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08005086 /* number of partitions in the domain */
5087 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08005088};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305089
Chunlin Hane9c97022017-07-07 20:29:30 +08005090
5091/**
5092 * @brief Initialize a memory domain.
5093 *
5094 * Initialize a memory domain with given name and memory partitions.
5095 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005096 * See documentation for k_mem_domain_add_partition() for details about
5097 * partition constraints.
5098 *
Chunlin Hane9c97022017-07-07 20:29:30 +08005099 * @param domain The memory domain to be initialized.
5100 * @param num_parts The number of array items of "parts" parameter.
5101 * @param parts An array of pointers to the memory partitions. Can be NULL
5102 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005103 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005104 */
Leandro Pereira08de6582018-02-28 14:22:57 -08005105extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08005106 struct k_mem_partition *parts[]);
5107/**
5108 * @brief Destroy a memory domain.
5109 *
5110 * Destroy a memory domain.
5111 *
5112 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005113 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005114 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005115extern void k_mem_domain_destroy(struct k_mem_domain *domain);
5116
5117/**
5118 * @brief Add a memory partition into a memory domain.
5119 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005120 * Add a memory partition into a memory domain. Partitions must conform to
5121 * the following constraints:
5122 *
5123 * - Partition bounds must be within system RAM boundaries on MMU-based
5124 * systems.
5125 * - Partitions in the same memory domain may not overlap each other.
5126 * - Partitions must not be defined which expose private kernel
5127 * data structures or kernel objects.
5128 * - The starting address alignment, and the partition size must conform to
5129 * the constraints of the underlying memory management hardware, which
5130 * varies per architecture.
5131 * - Memory domain partitions are only intended to control access to memory
5132 * from user mode threads.
5133 *
5134 * Violating these constraints may lead to CPU exceptions or undefined
5135 * behavior.
Chunlin Hane9c97022017-07-07 20:29:30 +08005136 *
5137 * @param domain The memory domain to be added a memory partition.
5138 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005139 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005140 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005141extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
5142 struct k_mem_partition *part);
5143
5144/**
5145 * @brief Remove a memory partition from a memory domain.
5146 *
5147 * Remove a memory partition from a memory domain.
5148 *
5149 * @param domain The memory domain to be removed a memory partition.
5150 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005151 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005152 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005153extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
5154 struct k_mem_partition *part);
5155
5156/**
5157 * @brief Add a thread into a memory domain.
5158 *
5159 * Add a thread into a memory domain.
5160 *
5161 * @param domain The memory domain that the thread is going to be added into.
5162 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005163 *
5164 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005165 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005166extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
5167 k_tid_t thread);
5168
5169/**
5170 * @brief Remove a thread from its memory domain.
5171 *
5172 * Remove a thread from its memory domain.
5173 *
5174 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005175 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005176 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005177extern void k_mem_domain_remove_thread(k_tid_t thread);
5178
Anas Nashif166f5192018-02-25 08:02:36 -06005179/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08005180
Andrew Boie756f9072017-10-10 16:01:49 -07005181/**
5182 * @brief Emit a character buffer to the console device
5183 *
5184 * @param c String of characters to print
5185 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005186 *
5187 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07005188 */
5189__syscall void k_str_out(char *c, size_t n);
5190
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005191/**
5192 * @brief Disable preservation of floating point context information.
5193 *
5194 * This routine informs the kernel that the specified thread
5195 * will no longer be using the floating point registers.
5196 *
5197 * @warning
5198 * Some architectures apply restrictions on how the disabling of floating
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005199 * point preservation may be requested, see arch_float_disable.
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005200 *
5201 * @warning
5202 * This routine should only be used to disable floating point support for
5203 * a thread that currently has such support enabled.
5204 *
5205 * @param thread ID of thread.
5206 *
5207 * @retval 0 On success.
5208 * @retval -ENOSYS If the floating point disabling is not implemented.
5209 * -EINVAL If the floating point disabling could not be performed.
5210 */
5211__syscall int k_float_disable(struct k_thread *thread);
5212
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005213#ifdef __cplusplus
5214}
5215#endif
5216
Anas Nashif10291a02019-06-25 12:25:12 -04005217#include <debug/tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07005218#include <syscalls/kernel.h>
5219
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05005220#endif /* !_ASMLANGUAGE */
5221
Flavio Ceolin67ca1762018-09-14 10:43:44 -07005222#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */