blob: e1e785731a155105862386719be4a29acc1a37b7 [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;
Anas Nashif2f203c22016-12-18 06:57:45 -0500101#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400102#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500103#define _OBJECT_TRACING_INIT
Flavio Ceolind1ed3362018-12-07 11:39:13 -0800104#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400105#endif
106
Benjamin Walshacc68c12017-01-29 18:57:45 -0500107#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300108#define _POLL_EVENT_OBJ_INIT(obj) \
109 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
110#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500111#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300112#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500113#define _POLL_EVENT
114#endif
115
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500116struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400117struct k_mutex;
118struct k_sem;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400119struct k_msgq;
120struct k_mbox;
121struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200122struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400123struct k_fifo;
124struct k_lifo;
125struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400126struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400127struct k_mem_pool;
128struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500129struct k_poll_event;
130struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800131struct k_mem_domain;
132struct k_mem_partition;
Wentong Wu5611e922019-06-20 23:51:27 +0800133struct k_futex;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400134
Andrew Boie5bd891d2017-09-27 12:59:28 -0700135/* This enumeration needs to be kept in sync with the lists of kernel objects
136 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
137 * function in kernel/userspace.c
138 */
Andrew Boie945af952017-08-22 13:15:23 -0700139enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700140 K_OBJ_ANY,
141
Leandro Pereirac2003672018-04-04 13:50:32 -0700142 /** @cond
143 * Doxygen should ignore this build-time generated include file
144 * when genrating API documentation. Enumeration values are
145 * generated during build by gen_kobject_list.py. It includes
146 * basic kernel objects (e.g. pipes and mutexes) and driver types.
147 */
148#include <kobj-types-enum.h>
149 /** @endcond
150 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700151
Andrew Boie945af952017-08-22 13:15:23 -0700152 K_OBJ_LAST
153};
Anas Nashif4bcb2942019-01-23 23:06:29 -0500154/**
155 * @defgroup usermode_apis User Mode APIs
156 * @ingroup kernel_apis
157 * @{
158 */
Andrew Boie945af952017-08-22 13:15:23 -0700159
160#ifdef CONFIG_USERSPACE
161/* Table generated by gperf, these objects are retrieved via
Patrik Flykt4344e272019-03-08 14:19:05 -0700162 * z_object_find() */
Andrew Boie945af952017-08-22 13:15:23 -0700163struct _k_object {
164 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700165 u8_t perms[CONFIG_MAX_THREAD_BYTES];
166 u8_t type;
167 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700168 u32_t data;
Andrew Boiedf555242018-05-25 07:28:54 -0700169} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700170
Andrew Boie877f82e2017-10-17 11:20:22 -0700171struct _k_object_assignment {
172 struct k_thread *thread;
173 void * const *objects;
174};
175
176/**
177 * @brief Grant a static thread access to a list of kernel objects
178 *
179 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
180 * a set of kernel objects. These objects do not need to be in an initialized
181 * state. The permissions will be granted when the threads are initialized
182 * in the early boot sequence.
183 *
184 * All arguments beyond the first must be pointers to kernel objects.
185 *
186 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
187 */
188#define K_THREAD_ACCESS_GRANT(name_, ...) \
189 static void * const _CONCAT(_object_list_, name_)[] = \
190 { __VA_ARGS__, NULL }; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400191 static const Z_STRUCT_SECTION_ITERABLE(_k_object_assignment, \
192 _CONCAT(_object_access_, name_)) = \
Andrew Boie877f82e2017-10-17 11:20:22 -0700193 { (&_k_thread_obj_ ## name_), \
194 (_CONCAT(_object_list_, name_)) }
195
Andrew Boie945af952017-08-22 13:15:23 -0700196#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700197#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700198#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie78757072019-07-23 13:29:30 -0700199#define K_OBJ_FLAG_DRIVER BIT(3)
Andrew Boie945af952017-08-22 13:15:23 -0700200
201/**
202 * Lookup a kernel object and init its metadata if it exists
203 *
204 * Calling this on an object will make it usable from userspace.
205 * Intended to be called as the last statement in kernel object init
206 * functions.
207 *
Anas Nashif50e3acd2018-12-08 13:26:18 -0500208 * @param obj Address of the kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700209 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700210void z_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700211#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700212/* LCOV_EXCL_START */
Andrew Boie877f82e2017-10-17 11:20:22 -0700213#define K_THREAD_ACCESS_GRANT(thread, ...)
214
Anas Nashif954d5502018-02-25 08:37:28 -0600215/**
216 * @internal
217 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700218static inline void z_object_init(void *obj)
Andrew Boie743e4682017-10-04 12:25:50 -0700219{
220 ARG_UNUSED(obj);
221}
222
Anas Nashif954d5502018-02-25 08:37:28 -0600223/**
224 * @internal
225 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700226static inline void z_impl_k_object_access_grant(void *object,
Andrew Boie743e4682017-10-04 12:25:50 -0700227 struct k_thread *thread)
228{
229 ARG_UNUSED(object);
230 ARG_UNUSED(thread);
231}
232
Anas Nashif954d5502018-02-25 08:37:28 -0600233/**
234 * @internal
235 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700236static inline void k_object_access_revoke(void *object,
237 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700238{
239 ARG_UNUSED(object);
240 ARG_UNUSED(thread);
241}
242
Andrew Boiee9cfc542018-04-13 13:15:28 -0700243/**
244 * @internal
245 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700246static inline void z_impl_k_object_release(void *object)
Andrew Boiee9cfc542018-04-13 13:15:28 -0700247{
248 ARG_UNUSED(object);
249}
250
Andrew Boie41bab6e2017-10-14 14:42:23 -0700251static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700252{
253 ARG_UNUSED(object);
254}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700255/* LCOV_EXCL_STOP */
Andrew Boie743e4682017-10-04 12:25:50 -0700256#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700257
258/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600259 * Grant a thread access to a kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700260 *
261 * The thread will be granted access to the object if the caller is from
262 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700263 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700264 *
265 * @param object Address of kernel object
266 * @param thread Thread to grant access to the object
267 */
Andrew Boie743e4682017-10-04 12:25:50 -0700268__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700269
Andrew Boiea89bf012017-10-09 14:47:55 -0700270/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600271 * Revoke a thread's access to a kernel object
Andrew Boiea89bf012017-10-09 14:47:55 -0700272 *
273 * The thread will lose access to the object if the caller is from
274 * supervisor mode, or the caller is from user mode AND has permissions
275 * on both the object and the thread whose access is being revoked.
276 *
277 * @param object Address of kernel object
278 * @param thread Thread to remove access to the object
279 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700280void k_object_access_revoke(void *object, struct k_thread *thread);
281
282
283__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700284
285/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600286 * Grant all present and future threads access to an object
Andrew Boie3b5ae802017-10-04 12:10:32 -0700287 *
288 * If the caller is from supervisor mode, or the caller is from user mode and
289 * have sufficient permissions on the object, then that object will have
290 * permissions granted to it for *all* current and future threads running in
291 * the system, effectively becoming a public kernel object.
292 *
293 * Use of this API should be avoided on systems that are running untrusted code
294 * as it is possible for such code to derive the addresses of kernel objects
295 * and perform unwanted operations on them.
296 *
Andrew Boie04caa672017-10-13 13:57:07 -0700297 * It is not possible to revoke permissions on public objects; once public,
298 * any thread may use it.
299 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700300 * @param object Address of kernel object
301 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700302void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700303
Andrew Boie31bdfc02017-11-08 16:38:03 -0800304/**
305 * Allocate a kernel object of a designated type
306 *
307 * This will instantiate at runtime a kernel object of the specified type,
308 * returning a pointer to it. The object will be returned in an uninitialized
309 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700310 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800311 *
312 * Currently, allocation of thread stacks is not supported.
313 *
314 * @param otype Requested kernel object type
315 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
316 * available
317 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700318__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800319
Andrew Boie97bf0012018-04-24 17:01:37 -0700320#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800321/**
322 * Free a kernel object previously allocated with k_object_alloc()
323 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700324 * This will return memory for a kernel object back to resource pool it was
325 * allocated from. Care must be exercised that the object will not be used
326 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800327 *
328 * @param obj Pointer to the kernel object memory address.
329 */
330void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700331#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700332/* LCOV_EXCL_START */
Patrik Flykt4344e272019-03-08 14:19:05 -0700333static inline void *z_impl_k_object_alloc(enum k_objects otype)
Andrew Boie97bf0012018-04-24 17:01:37 -0700334{
Kumar Gala85699f72018-05-17 09:26:03 -0500335 ARG_UNUSED(otype);
336
Andrew Boie97bf0012018-04-24 17:01:37 -0700337 return NULL;
338}
339
340static inline void k_obj_free(void *obj)
341{
342 ARG_UNUSED(obj);
343}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700344/* LCOV_EXCL_STOP */
Andrew Boie31bdfc02017-11-08 16:38:03 -0800345#endif /* CONFIG_DYNAMIC_OBJECTS */
346
Anas Nashif4bcb2942019-01-23 23:06:29 -0500347/** @} */
348
Andrew Boiebca15da2017-10-15 14:17:48 -0700349/* Using typedef deliberately here, this is quite intended to be an opaque
Andrew Boie4e5c0932019-04-04 12:05:28 -0700350 * type.
Andrew Boiebca15da2017-10-15 14:17:48 -0700351 *
352 * The purpose of this data type is to clearly distinguish between the
353 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
354 * buffer which composes the stack data actually used by the underlying
Anas Nashiff2cb20c2019-06-18 14:45:40 -0400355 * thread; they cannot be used interchangeably as some arches precede the
Andrew Boiebca15da2017-10-15 14:17:48 -0700356 * stack buffer region with guard areas that trigger a MPU or MMU fault
357 * if written to.
358 *
359 * APIs that want to work with the buffer inside should continue to use
360 * char *.
361 *
362 * Stacks should always be created with K_THREAD_STACK_DEFINE().
363 */
364struct __packed _k_thread_stack_element {
365 char data;
366};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700367typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700368
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700369/**
370 * @typedef k_thread_entry_t
371 * @brief Thread entry point function type.
372 *
373 * A thread's entry point function is invoked when the thread starts executing.
374 * Up to 3 argument values can be passed to the function.
375 *
376 * The thread terminates execution permanently if the entry point function
377 * returns. The thread is responsible for releasing any shared resources
378 * it may own (such as mutexes and dynamically allocated memory), prior to
379 * returning.
380 *
381 * @param p1 First argument.
382 * @param p2 Second argument.
383 * @param p3 Third argument.
384 *
385 * @return N/A
386 */
387typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700388
389#ifdef CONFIG_THREAD_MONITOR
390struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700391 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700392 void *parameter1;
393 void *parameter2;
394 void *parameter3;
395};
396#endif
397
398/* can be used for creating 'dummy' threads, e.g. for pending on objects */
399struct _thread_base {
400
401 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700402 union {
Peter A. Bigot82ad0d22019-01-03 23:49:28 -0600403 sys_dnode_t qnode_dlist;
Andy Ross1acd8c22018-05-03 14:51:49 -0700404 struct rbnode qnode_rb;
405 };
406
Andy Ross1acd8c22018-05-03 14:51:49 -0700407 /* wait queue on which the thread is pended (needed only for
408 * trees, not dumb lists)
409 */
410 _wait_q_t *pended_on;
Andrew Boie73abd322017-04-04 13:19:13 -0700411
412 /* user facing 'thread options'; values defined in include/kernel.h */
413 u8_t user_options;
414
415 /* thread state */
416 u8_t thread_state;
417
418 /*
419 * scheduler lock count and thread priority
420 *
421 * These two fields control the preemptibility of a thread.
422 *
423 * When the scheduler is locked, sched_locked is decremented, which
424 * means that the scheduler is locked for values from 0xff to 0x01. A
425 * thread is coop if its prio is negative, thus 0x80 to 0xff when
426 * looked at the value as unsigned.
427 *
428 * By putting them end-to-end, this means that a thread is
429 * non-preemptible if the bundled value is greater than or equal to
430 * 0x0080.
431 */
432 union {
433 struct {
434#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
435 u8_t sched_locked;
436 s8_t prio;
437#else /* LITTLE and PDP */
438 s8_t prio;
439 u8_t sched_locked;
440#endif
441 };
442 u16_t preempt;
443 };
444
Andy Ross4a2e50f2018-05-15 11:06:25 -0700445#ifdef CONFIG_SCHED_DEADLINE
446 int prio_deadline;
447#endif
448
Andy Ross1acd8c22018-05-03 14:51:49 -0700449 u32_t order_key;
450
Andy Ross2724fd12018-01-29 14:55:20 -0800451#ifdef CONFIG_SMP
452 /* True for the per-CPU idle threads */
453 u8_t is_idle;
454
Andy Ross2724fd12018-01-29 14:55:20 -0800455 /* CPU index on which thread was last run */
456 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700457
458 /* Recursive count of irq_lock() calls */
459 u8_t global_lock_count;
Andy Rossab46b1b2019-01-30 15:00:42 -0800460
461#endif
462
463#ifdef CONFIG_SCHED_CPU_MASK
464 /* "May run on" bits for each CPU */
465 u8_t cpu_mask;
Andy Ross2724fd12018-01-29 14:55:20 -0800466#endif
467
Andrew Boie73abd322017-04-04 13:19:13 -0700468 /* data returned by APIs */
469 void *swap_data;
470
471#ifdef CONFIG_SYS_CLOCK_EXISTS
472 /* this thread's entry in a timeout queue */
473 struct _timeout timeout;
474#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700475};
476
477typedef struct _thread_base _thread_base_t;
478
479#if defined(CONFIG_THREAD_STACK_INFO)
480/* Contains the stack information of a thread */
481struct _thread_stack_info {
Andrew Boie4e5c0932019-04-04 12:05:28 -0700482 /* Stack start - Represents the start address of the thread-writable
483 * stack area.
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700484 */
Nicolas Pitre58d839b2019-05-21 11:32:21 -0400485 uintptr_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700486
487 /* Stack Size - Thread writable stack buffer size. Represents
488 * the size of the actual area, starting from the start member,
489 * that should be writable by the thread
490 */
Andrew Boie73abd322017-04-04 13:19:13 -0700491 u32_t size;
492};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700493
494typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700495#endif /* CONFIG_THREAD_STACK_INFO */
496
Chunlin Hane9c97022017-07-07 20:29:30 +0800497#if defined(CONFIG_USERSPACE)
498struct _mem_domain_info {
499 /* memory domain queue node */
500 sys_dnode_t mem_domain_q_node;
501 /* memory domain of the thread */
502 struct k_mem_domain *mem_domain;
503};
504
505#endif /* CONFIG_USERSPACE */
506
Daniel Leungfc182432018-08-16 15:42:28 -0700507#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
508struct _thread_userspace_local_data {
509 int errno_var;
510};
511#endif
512
Anas Nashifce78d162018-05-24 12:43:11 -0500513/**
514 * @ingroup thread_apis
515 * Thread Structure
516 */
Andrew Boie73abd322017-04-04 13:19:13 -0700517struct k_thread {
518
519 struct _thread_base base;
520
Anas Nashifce78d162018-05-24 12:43:11 -0500521 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700522 struct _callee_saved callee_saved;
523
Anas Nashifce78d162018-05-24 12:43:11 -0500524 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700525 void *init_data;
526
Anas Nashifce78d162018-05-24 12:43:11 -0500527 /**
528 * abort function
529 * @req K-THREAD-002
530 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700531 void (*fn_abort)(void);
532
533#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500534 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700535 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700536
Anas Nashifce78d162018-05-24 12:43:11 -0500537 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700538 struct k_thread *next_thread;
539#endif
540
Anas Nashif57554052018-03-03 02:31:05 -0600541#if defined(CONFIG_THREAD_NAME)
542 /* Thread name */
Andrew Boie38129ce2019-06-25 08:54:37 -0700543 char name[CONFIG_THREAD_MAX_NAME_LEN];
Anas Nashif57554052018-03-03 02:31:05 -0600544#endif
545
Andrew Boie73abd322017-04-04 13:19:13 -0700546#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500547 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700548 void *custom_data;
549#endif
550
Daniel Leungfc182432018-08-16 15:42:28 -0700551#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
552 struct _thread_userspace_local_data *userspace_local_data;
553#endif
554
Andrew Boie73abd322017-04-04 13:19:13 -0700555#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700556#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500557 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700558 int errno_var;
559#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700560#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700561
562#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500563 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700564 struct _thread_stack_info stack_info;
565#endif /* CONFIG_THREAD_STACK_INFO */
566
Chunlin Hane9c97022017-07-07 20:29:30 +0800567#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500568 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800569 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500570 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700571 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800572#endif /* CONFIG_USERSPACE */
573
Andy Ross042d8ec2017-12-09 08:37:20 -0800574#if defined(CONFIG_USE_SWITCH)
575 /* When using __switch() a few previously arch-specific items
576 * become part of the core OS
577 */
578
Patrik Flykt4344e272019-03-08 14:19:05 -0700579 /** z_swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800580 int swap_retval;
581
Andrew Boie4f77c2a2019-11-07 12:43:29 -0800582 /** Context handle returned via arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800583 void *switch_handle;
584#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500585 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700586 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800587
Anas Nashifce78d162018-05-24 12:43:11 -0500588 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700589 struct _thread_arch arch;
590};
591
592typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400593typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400594
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400595enum execution_context_types {
596 K_ISR = 0,
597 K_COOP_THREAD,
598 K_PREEMPT_THREAD,
599};
600
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400601/**
Anas Nashif4bcb2942019-01-23 23:06:29 -0500602 * @addtogroup thread_apis
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100603 * @{
604 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530605typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
606 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100607
608/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530609 * @brief Iterate over all the threads in the system.
610 *
611 * This routine iterates over all the threads in the system and
612 * calls the user_cb function for each thread.
613 *
614 * @param user_cb Pointer to the user callback function.
615 * @param user_data Pointer to user data.
616 *
617 * @note CONFIG_THREAD_MONITOR must be set for this function
618 * to be effective. Also this API uses irq_lock to protect the
619 * _kernel.threads list which means creation of new threads and
620 * terminations of existing threads are blocked until this
621 * API returns.
622 *
623 * @return N/A
624 */
625extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
626
Anas Nashif166f5192018-02-25 08:02:36 -0600627/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100628
629/**
Allan Stephensc98da842016-11-11 15:45:03 -0500630 * @defgroup thread_apis Thread APIs
631 * @ingroup kernel_apis
632 * @{
633 */
634
Benjamin Walshed240f22017-01-22 13:05:08 -0500635#endif /* !_ASMLANGUAGE */
636
637
638/*
639 * Thread user options. May be needed by assembly code. Common part uses low
640 * bits, arch-specific use high bits.
641 */
642
Anas Nashifa541e932018-05-24 11:19:16 -0500643/**
644 * @brief system thread that must not abort
645 * @req K-THREAD-000
646 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700647#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500648
649#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500650/**
651 * @brief thread uses floating point registers
652 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700653#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500654#endif
655
Anas Nashifa541e932018-05-24 11:19:16 -0500656/**
657 * @brief user mode thread
658 *
659 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700660 * has additional restrictions
661 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700662#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700663
Anas Nashifa541e932018-05-24 11:19:16 -0500664/**
665 * @brief Inherit Permissions
666 *
667 * @details
668 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700669 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
670 * is not enabled.
671 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700672#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700673
Benjamin Walshed240f22017-01-22 13:05:08 -0500674#ifdef CONFIG_X86
675/* x86 Bitmask definitions for threads user options */
676
677#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
678/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700679#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500680#endif
681#endif
682
683/* end - thread options */
684
685#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400686/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700687 * @brief Create a thread.
688 *
689 * This routine initializes a thread, then schedules it for execution.
690 *
691 * The new thread may be scheduled for immediate execution or a delayed start.
692 * If the newly spawned thread does not have a delayed start the kernel
693 * scheduler may preempt the current thread to allow the new thread to
694 * execute.
695 *
696 * Thread options are architecture-specific, and can include K_ESSENTIAL,
697 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
698 * them using "|" (the logical OR operator).
699 *
700 * Historically, users often would use the beginning of the stack memory region
701 * to store the struct k_thread data, although corruption will occur if the
702 * stack overflows this region and stack protection features may not detect this
703 * situation.
704 *
705 * @param new_thread Pointer to uninitialized struct k_thread
706 * @param stack Pointer to the stack space.
707 * @param stack_size Stack size in bytes.
708 * @param entry Thread entry function.
709 * @param p1 1st entry point parameter.
710 * @param p2 2nd entry point parameter.
711 * @param p3 3rd entry point parameter.
712 * @param prio Thread priority.
713 * @param options Thread options.
714 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
715 *
716 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400717 *
718 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700719 */
Andrew Boie662c3452017-10-02 10:51:18 -0700720__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700721 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700722 size_t stack_size,
723 k_thread_entry_t entry,
724 void *p1, void *p2, void *p3,
725 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700726
Andrew Boie3f091b52017-08-30 14:34:14 -0700727/**
728 * @brief Drop a thread's privileges permanently to user mode
729 *
730 * @param entry Function to start executing from
731 * @param p1 1st entry point parameter
732 * @param p2 2nd entry point parameter
733 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400734 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700735 */
736extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
737 void *p1, void *p2,
738 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700739
Andrew Boied26cf2d2017-03-30 13:07:02 -0700740/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530741 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700742 *
743 * This is a convenience function. For the provided thread, grant access to
744 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700745 *
746 * The thread object must be initialized (i.e. running). The objects don't
747 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530748 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700749 *
750 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530751 * @param ... list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400752 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700753 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530754#define k_thread_access_grant(thread, ...) \
755 FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700756
757/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700758 * @brief Assign a resource memory pool to a thread
759 *
760 * By default, threads have no resource pool assigned unless their parent
761 * thread has a resource pool, in which case it is inherited. Multiple
762 * threads may be assigned to the same memory pool.
763 *
764 * Changing a thread's resource pool will not migrate allocations from the
765 * previous pool.
766 *
767 * @param thread Target thread to assign a memory pool for resource requests,
768 * or NULL if the thread should no longer have a memory pool.
769 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400770 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700771 */
772static inline void k_thread_resource_pool_assign(struct k_thread *thread,
773 struct k_mem_pool *pool)
774{
775 thread->resource_pool = pool;
776}
777
778#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
779/**
780 * @brief Assign the system heap as a thread's resource pool
781 *
782 * Similar to k_thread_resource_pool_assign(), but the thread will use
783 * the kernel heap to draw memory.
784 *
785 * Use with caution, as a malicious thread could perform DoS attacks on the
786 * kernel heap.
787 *
788 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400789 *
790 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700791 */
792void k_thread_system_pool_assign(struct k_thread *thread);
793#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
794
795/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500796 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400797 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700798 * This routine puts the current thread to sleep for @a duration milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400799 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700800 * @param ms Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400801 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200802 * @return Zero if the requested time has elapsed or the number of milliseconds
803 * left to sleep, if thread was woken up by \ref k_wakeup call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400804 */
Charles E. Yousea5678312019-05-09 16:46:46 -0700805__syscall s32_t k_sleep(s32_t ms);
806
807/**
808 * @brief Put the current thread to sleep with microsecond resolution.
809 *
810 * This function is unlikely to work as expected without kernel tuning.
811 * In particular, because the lower bound on the duration of a sleep is
812 * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted
813 * to achieve the resolution desired. The implications of doing this must
814 * be understood before attempting to use k_usleep(). Use with caution.
815 *
816 * @param us Number of microseconds to sleep.
817 *
818 * @return Zero if the requested time has elapsed or the number of microseconds
819 * left to sleep, if thread was woken up by \ref k_wakeup call.
820 */
821__syscall s32_t k_usleep(s32_t us);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400822
823/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500824 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400825 *
826 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500827 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400828 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829 * @return N/A
830 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800831__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832
833/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500834 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400835 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500836 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400837 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500838 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400839 *
840 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400841 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400842 */
Andrew Boie468190a2017-09-29 14:00:48 -0700843__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844
845/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500846 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500848 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500850 * If @a thread is not currently sleeping, the routine has no effect.
851 *
852 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400853 *
854 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400855 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856 */
Andrew Boie468190a2017-09-29 14:00:48 -0700857__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400858
859/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500860 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500862 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400863 *
864 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700866__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400867
868/**
Allan Stephensc98da842016-11-11 15:45:03 -0500869 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500871 * This routine permanently stops execution of @a thread. The thread is taken
872 * off all kernel queues it is part of (i.e. the ready queue, the timeout
873 * queue, or a kernel object wait queue). However, any kernel resources the
874 * thread might currently own (such as mutexes or memory blocks) are not
875 * released. It is the responsibility of the caller of this routine to ensure
876 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500878 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400879 *
880 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400881 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400882 */
Andrew Boie468190a2017-09-29 14:00:48 -0700883__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400884
Andrew Boie7d627c52017-08-30 11:01:56 -0700885
886/**
887 * @brief Start an inactive thread
888 *
889 * If a thread was created with K_FOREVER in the delay parameter, it will
890 * not be added to the scheduling queue until this function is called
891 * on it.
892 *
893 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400894 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700895 */
Andrew Boie468190a2017-09-29 14:00:48 -0700896__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700897
Allan Stephensc98da842016-11-11 15:45:03 -0500898/**
899 * @cond INTERNAL_HIDDEN
900 */
901
Benjamin Walshd211a522016-12-06 11:44:01 -0500902/* timeout has timed out and is not on _timeout_q anymore */
903#define _EXPIRED (-2)
904
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400905struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700906 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700907 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400908 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700909 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500910 void *init_p1;
911 void *init_p2;
912 void *init_p3;
913 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500914 u32_t init_options;
915 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500916 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600917 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400918};
919
Andrew Boied26cf2d2017-03-30 13:07:02 -0700920#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400921 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600922 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500923 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700924 .init_thread = (thread), \
925 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500926 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700927 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400928 .init_p1 = (void *)p1, \
929 .init_p2 = (void *)p2, \
930 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500931 .init_prio = (prio), \
932 .init_options = (options), \
933 .init_delay = (delay), \
934 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600935 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400936 }
937
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400938/**
Allan Stephensc98da842016-11-11 15:45:03 -0500939 * INTERNAL_HIDDEN @endcond
940 */
941
942/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500943 * @brief Statically define and initialize a thread.
944 *
945 * The thread may be scheduled for immediate execution or a delayed start.
946 *
947 * Thread options are architecture-specific, and can include K_ESSENTIAL,
948 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
949 * them using "|" (the logical OR operator).
950 *
951 * The ID of the thread can be accessed using:
952 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500953 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500954 *
955 * @param name Name of the thread.
956 * @param stack_size Stack size in bytes.
957 * @param entry Thread entry function.
958 * @param p1 1st entry point parameter.
959 * @param p2 2nd entry point parameter.
960 * @param p3 3rd entry point parameter.
961 * @param prio Thread priority.
962 * @param options Thread options.
963 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400964 *
Anas Nashif47420d02018-05-24 14:20:56 -0400965 * @req K-THREAD-010
966 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400967 * @internal It has been observed that the x86 compiler by default aligns
968 * these _static_thread_data structures to 32-byte boundaries, thereby
969 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400970 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400971 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500972#define K_THREAD_DEFINE(name, stack_size, \
973 entry, p1, p2, p3, \
974 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700975 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400976 struct k_thread _k_thread_obj_##name; \
977 Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\
Andrew Boied26cf2d2017-03-30 13:07:02 -0700978 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
979 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500980 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -0600981 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700982 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400983
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400984/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500985 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500987 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500989 * @param thread ID of thread whose priority is needed.
990 *
991 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400992 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400993 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700994__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400995
996/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500997 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500999 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001000 *
1001 * Rescheduling can occur immediately depending on the priority @a thread is
1002 * set to:
1003 *
1004 * - If its priority is raised above the priority of the caller of this
1005 * function, and the caller is preemptible, @a thread will be scheduled in.
1006 *
1007 * - If the caller operates on itself, it lowers its priority below that of
1008 * other threads in the system, and the caller is preemptible, the thread of
1009 * highest priority will be scheduled in.
1010 *
1011 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1012 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1013 * highest priority.
1014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001015 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001016 * @param prio New priority.
1017 *
1018 * @warning Changing the priority of a thread currently involved in mutex
1019 * priority inheritance may result in undefined behavior.
1020 *
1021 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001022 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001023 */
Andrew Boie468190a2017-09-29 14:00:48 -07001024__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001025
Andy Ross4a2e50f2018-05-15 11:06:25 -07001026
1027#ifdef CONFIG_SCHED_DEADLINE
1028/**
1029 * @brief Set deadline expiration time for scheduler
1030 *
1031 * This sets the "deadline" expiration as a time delta from the
1032 * current time, in the same units used by k_cycle_get_32(). The
1033 * scheduler (when deadline scheduling is enabled) will choose the
1034 * next expiring thread when selecting between threads at the same
1035 * static priority. Threads at different priorities will be scheduled
1036 * according to their static priority.
1037 *
1038 * @note Deadlines that are negative (i.e. in the past) are still seen
1039 * as higher priority than others, even if the thread has "finished"
1040 * its work. If you don't want it scheduled anymore, you have to
1041 * reset the deadline into the future, block/pend the thread, or
1042 * modify its priority with k_thread_priority_set().
1043 *
1044 * @note Despite the API naming, the scheduler makes no guarantees the
1045 * the thread WILL be scheduled within that deadline, nor does it take
1046 * extra metadata (like e.g. the "runtime" and "period" parameters in
1047 * Linux sched_setattr()) that allows the kernel to validate the
1048 * scheduling for achievability. Such features could be implemented
1049 * above this call, which is simply input to the priority selection
1050 * logic.
1051 *
Anas Nashif240c5162019-06-10 12:25:50 -04001052 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001053 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001054 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1055 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001056 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001057 *
Andy Ross4a2e50f2018-05-15 11:06:25 -07001058 * @param thread A thread on which to set the deadline
1059 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001060 *
1061 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001062 */
1063__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1064#endif
1065
Andy Rossab46b1b2019-01-30 15:00:42 -08001066#ifdef CONFIG_SCHED_CPU_MASK
1067/**
1068 * @brief Sets all CPU enable masks to zero
1069 *
1070 * After this returns, the thread will no longer be schedulable on any
1071 * CPUs. The thread must not be currently runnable.
1072 *
Anas Nashif240c5162019-06-10 12:25:50 -04001073 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001074 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001075 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1076 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001077 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001078 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001079 * @param thread Thread to operate upon
1080 * @return Zero on success, otherwise error code
1081 */
1082int k_thread_cpu_mask_clear(k_tid_t thread);
1083
1084/**
1085 * @brief Sets all CPU enable masks to one
1086 *
1087 * After this returns, the thread will be schedulable on any CPU. The
1088 * thread must not be currently runnable.
1089 *
Anas Nashif240c5162019-06-10 12:25:50 -04001090 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001091 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001092 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1093 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001094 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001095 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001096 * @param thread Thread to operate upon
1097 * @return Zero on success, otherwise error code
1098 */
1099int k_thread_cpu_mask_enable_all(k_tid_t thread);
1100
1101/**
1102 * @brief Enable thread to run on specified CPU
1103 *
1104 * The thread must not be currently runnable.
1105 *
Anas Nashif240c5162019-06-10 12:25:50 -04001106 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001107 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001108 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1109 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001110 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001111 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001112 * @param thread Thread to operate upon
1113 * @param cpu CPU index
1114 * @return Zero on success, otherwise error code
1115 */
1116int k_thread_cpu_mask_enable(k_tid_t thread, int cpu);
1117
1118/**
1119 * @brief Prevent thread to run on specified CPU
1120 *
1121 * The thread must not be currently runnable.
1122 *
Anas Nashif240c5162019-06-10 12:25:50 -04001123 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001124 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001125 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1126 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001127 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001128 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001129 * @param thread Thread to operate upon
1130 * @param cpu CPU index
1131 * @return Zero on success, otherwise error code
1132 */
1133int k_thread_cpu_mask_disable(k_tid_t thread, int cpu);
1134#endif
1135
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001136/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001137 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001139 * This routine prevents the kernel scheduler from making @a thread the
1140 * current thread. All other internal operations on @a thread are still
1141 * performed; for example, any timeout it is waiting on keeps ticking,
1142 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001144 * If @a thread is already suspended, the routine has no effect.
1145 *
1146 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001147 *
1148 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001149 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001150 */
Andrew Boie468190a2017-09-29 14:00:48 -07001151__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001152
1153/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001154 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001155 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001156 * This routine allows the kernel scheduler to make @a thread the current
1157 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001158 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001159 * If @a thread is not currently suspended, the routine has no effect.
1160 *
1161 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001162 *
1163 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001164 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001165 */
Andrew Boie468190a2017-09-29 14:00:48 -07001166__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001167
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001168/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001169 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001170 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001171 * This routine specifies how the scheduler will perform time slicing of
1172 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001174 * To enable time slicing, @a slice must be non-zero. The scheduler
1175 * ensures that no thread runs for more than the specified time limit
1176 * before other threads of that priority are given a chance to execute.
1177 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001178 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001180 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001181 * execute. Once the scheduler selects a thread for execution, there is no
1182 * minimum guaranteed time the thread will execute before threads of greater or
1183 * equal priority are scheduled.
1184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001185 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001186 * for execution, this routine has no effect; the thread is immediately
1187 * rescheduled after the slice period expires.
1188 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001189 * To disable timeslicing, set both @a slice and @a prio to zero.
1190 *
1191 * @param slice Maximum time slice length (in milliseconds).
1192 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001193 *
1194 * @return N/A
1195 */
Kumar Galacc334c72017-04-21 10:55:34 -05001196extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001197
Anas Nashif166f5192018-02-25 08:02:36 -06001198/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001199
1200/**
1201 * @addtogroup isr_apis
1202 * @{
1203 */
1204
1205/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001206 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001207 *
Allan Stephensc98da842016-11-11 15:45:03 -05001208 * This routine allows the caller to customize its actions, depending on
1209 * whether it is a thread or an ISR.
1210 *
1211 * @note Can be called by ISRs.
1212 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001213 * @return false if invoked by a thread.
1214 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001215 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001216extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001217
Benjamin Walsh445830d2016-11-10 15:54:27 -05001218/**
1219 * @brief Determine if code is running in a preemptible thread.
1220 *
Allan Stephensc98da842016-11-11 15:45:03 -05001221 * This routine allows the caller to customize its actions, depending on
1222 * whether it can be preempted by another thread. The routine returns a 'true'
1223 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001224 *
Allan Stephensc98da842016-11-11 15:45:03 -05001225 * - The code is running in a thread, not at ISR.
1226 * - The thread's priority is in the preemptible range.
1227 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001228 *
Allan Stephensc98da842016-11-11 15:45:03 -05001229 * @note Can be called by ISRs.
1230 *
1231 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001232 * @return Non-zero if invoked by a preemptible thread.
1233 */
Andrew Boie468190a2017-09-29 14:00:48 -07001234__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001235
Allan Stephensc98da842016-11-11 15:45:03 -05001236/**
Anas Nashif166f5192018-02-25 08:02:36 -06001237 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001238 */
1239
1240/**
1241 * @addtogroup thread_apis
1242 * @{
1243 */
1244
1245/**
1246 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001247 *
Allan Stephensc98da842016-11-11 15:45:03 -05001248 * This routine prevents the current thread from being preempted by another
1249 * thread by instructing the scheduler to treat it as a cooperative thread.
1250 * If the thread subsequently performs an operation that makes it unready,
1251 * it will be context switched out in the normal manner. When the thread
1252 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001253 *
Allan Stephensc98da842016-11-11 15:45:03 -05001254 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001255 *
Allan Stephensc98da842016-11-11 15:45:03 -05001256 * @note k_sched_lock() and k_sched_unlock() should normally be used
1257 * when the operation being performed can be safely interrupted by ISRs.
1258 * However, if the amount of processing involved is very small, better
1259 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001260 *
1261 * @return N/A
1262 */
1263extern void k_sched_lock(void);
1264
Allan Stephensc98da842016-11-11 15:45:03 -05001265/**
1266 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001267 *
Allan Stephensc98da842016-11-11 15:45:03 -05001268 * This routine reverses the effect of a previous call to k_sched_lock().
1269 * A thread must call the routine once for each time it called k_sched_lock()
1270 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001271 *
1272 * @return N/A
1273 */
1274extern void k_sched_unlock(void);
1275
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001276/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001277 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001279 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001280 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001281 * Custom data is not used by the kernel itself, and is freely available
1282 * for a thread to use as it sees fit. It can be used as a framework
1283 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001285 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001286 *
1287 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001288 *
1289 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001290 */
Andrew Boie468190a2017-09-29 14:00:48 -07001291__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001292
1293/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001294 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001295 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001296 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001297 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001298 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001299 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001300 */
Andrew Boie468190a2017-09-29 14:00:48 -07001301__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001302
1303/**
Anas Nashif57554052018-03-03 02:31:05 -06001304 * @brief Set current thread name
1305 *
1306 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1307 * tracing and debugging.
1308 *
Andrew Boie38129ce2019-06-25 08:54:37 -07001309 * @param thread_id Thread to set name, or NULL to set the current thread
1310 * @param value Name string
1311 * @retval 0 on success
1312 * @retval -EFAULT Memory access error with supplied string
1313 * @retval -ENOSYS Thread name configuration option not enabled
1314 * @retval -EINVAL Thread name too long
Anas Nashif57554052018-03-03 02:31:05 -06001315 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001316__syscall int k_thread_name_set(k_tid_t thread_id, const char *value);
Anas Nashif57554052018-03-03 02:31:05 -06001317
1318/**
1319 * @brief Get thread name
1320 *
1321 * Get the name of a thread
1322 *
1323 * @param thread_id Thread ID
Andrew Boie38129ce2019-06-25 08:54:37 -07001324 * @retval Thread name, or NULL if configuration not enabled
Anas Nashif57554052018-03-03 02:31:05 -06001325 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001326const char *k_thread_name_get(k_tid_t thread_id);
1327
1328/**
1329 * @brief Copy the thread name into a supplied buffer
1330 *
1331 * @param thread_id Thread to obtain name information
1332 * @param buf Destination buffer
David B. Kinder73896c02019-10-28 16:27:57 -07001333 * @param size Destination buffer size
Andrew Boie38129ce2019-06-25 08:54:37 -07001334 * @retval -ENOSPC Destination buffer too small
1335 * @retval -EFAULT Memory access error
1336 * @retval -ENOSYS Thread name feature not enabled
1337 * @retval 0 Success
1338 */
1339__syscall int k_thread_name_copy(k_tid_t thread_id, char *buf,
1340 size_t size);
Anas Nashif57554052018-03-03 02:31:05 -06001341
1342/**
Pavlo Hamov8076c802019-07-31 12:43:54 +03001343 * @brief Get thread state string
1344 *
1345 * Get the human friendly thread state string
1346 *
1347 * @param thread_id Thread ID
1348 * @retval Thread state string, empty if no state flag is set
1349 */
1350const char *k_thread_state_str(k_tid_t thread_id);
1351
1352/**
Andy Rosscfe62032018-09-29 07:34:55 -07001353 * @}
1354 */
1355
1356/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001357 * @addtogroup clock_apis
1358 * @{
1359 */
1360
1361/**
1362 * @brief Generate null timeout delay.
1363 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001364 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001365 * not to wait if the requested operation cannot be performed immediately.
1366 *
1367 * @return Timeout delay value.
1368 */
1369#define K_NO_WAIT 0
1370
1371/**
1372 * @brief Generate timeout delay from milliseconds.
1373 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001374 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001375 * to wait up to @a ms milliseconds to perform the requested operation.
1376 *
1377 * @param ms Duration in milliseconds.
1378 *
1379 * @return Timeout delay value.
1380 */
Johan Hedberg14471692016-11-13 10:52:15 +02001381#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001382
1383/**
1384 * @brief Generate timeout delay from seconds.
1385 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001386 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001387 * to wait up to @a s seconds to perform the requested operation.
1388 *
1389 * @param s Duration in seconds.
1390 *
1391 * @return Timeout delay value.
1392 */
Johan Hedberg14471692016-11-13 10:52:15 +02001393#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001394
1395/**
1396 * @brief Generate timeout delay from minutes.
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001397
1398 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001399 * to wait up to @a m minutes to perform the requested operation.
1400 *
1401 * @param m Duration in minutes.
1402 *
1403 * @return Timeout delay value.
1404 */
Johan Hedberg14471692016-11-13 10:52:15 +02001405#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001406
1407/**
1408 * @brief Generate timeout delay from hours.
1409 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001410 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001411 * to wait up to @a h hours to perform the requested operation.
1412 *
1413 * @param h Duration in hours.
1414 *
1415 * @return Timeout delay value.
1416 */
Johan Hedberg14471692016-11-13 10:52:15 +02001417#define K_HOURS(h) K_MINUTES((h) * 60)
1418
Allan Stephensc98da842016-11-11 15:45:03 -05001419/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001420 * @brief Generate infinite timeout delay.
1421 *
Maksim Masalskife1ff2f2019-10-29 16:50:44 +08001422 * This macro generates a timeout delay that instructs a kernel API
Allan Stephensc2f15a42016-11-17 12:24:22 -05001423 * to wait as long as necessary to perform the requested operation.
1424 *
1425 * @return Timeout delay value.
1426 */
1427#define K_FOREVER (-1)
1428
1429/**
Anas Nashif166f5192018-02-25 08:02:36 -06001430 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001431 */
1432
1433/**
Allan Stephensc98da842016-11-11 15:45:03 -05001434 * @cond INTERNAL_HIDDEN
1435 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001436
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001437struct k_timer {
1438 /*
1439 * _timeout structure must be first here if we want to use
1440 * dynamic timer allocation. timeout.node is used in the double-linked
1441 * list of free timers
1442 */
1443 struct _timeout timeout;
1444
Allan Stephens45bfa372016-10-12 12:39:42 -05001445 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001446 _wait_q_t wait_q;
1447
1448 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001449 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001450
1451 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001452 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001453
1454 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001455 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001456
Allan Stephens45bfa372016-10-12 12:39:42 -05001457 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001458 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001459
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001460 /* user-specific data, also used to support legacy features */
1461 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001462
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001463 _OBJECT_TRACING_NEXT_PTR(k_timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001464};
1465
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001466#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001467 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001468 .timeout = { \
1469 .node = {},\
1470 .dticks = 0, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001471 .fn = z_timer_expiration_handler \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001472 }, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001473 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001474 .expiry_fn = expiry, \
1475 .stop_fn = stop, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001476 .period = 0, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001477 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001478 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001479 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001480 }
1481
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05001482#define K_TIMER_INITIALIZER __DEPRECATED_MACRO Z_TIMER_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001483
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001484/**
Allan Stephensc98da842016-11-11 15:45:03 -05001485 * INTERNAL_HIDDEN @endcond
1486 */
1487
1488/**
1489 * @defgroup timer_apis Timer APIs
1490 * @ingroup kernel_apis
1491 * @{
1492 */
1493
1494/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001495 * @typedef k_timer_expiry_t
1496 * @brief Timer expiry function type.
1497 *
1498 * A timer's expiry function is executed by the system clock interrupt handler
1499 * each time the timer expires. The expiry function is optional, and is only
1500 * invoked if the timer has been initialized with one.
1501 *
1502 * @param timer Address of timer.
1503 *
1504 * @return N/A
1505 */
1506typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1507
1508/**
1509 * @typedef k_timer_stop_t
1510 * @brief Timer stop function type.
1511 *
1512 * A timer's stop function is executed if the timer is stopped prematurely.
1513 * The function runs in the context of the thread that stops the timer.
1514 * The stop function is optional, and is only invoked if the timer has been
1515 * initialized with one.
1516 *
1517 * @param timer Address of timer.
1518 *
1519 * @return N/A
1520 */
1521typedef void (*k_timer_stop_t)(struct k_timer *timer);
1522
1523/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001524 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001526 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001527 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001528 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001529 *
1530 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001531 * @param expiry_fn Function to invoke each time the timer expires.
1532 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001533 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001534#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001535 Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001536 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001537
Allan Stephens45bfa372016-10-12 12:39:42 -05001538/**
1539 * @brief Initialize a timer.
1540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001541 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001542 *
1543 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001544 * @param expiry_fn Function to invoke each time the timer expires.
1545 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001546 *
1547 * @return N/A
1548 */
1549extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001550 k_timer_expiry_t expiry_fn,
1551 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001552
Allan Stephens45bfa372016-10-12 12:39:42 -05001553/**
1554 * @brief Start a timer.
1555 *
1556 * This routine starts a timer, and resets its status to zero. The timer
1557 * begins counting down using the specified duration and period values.
1558 *
1559 * Attempting to start a timer that is already running is permitted.
1560 * The timer's status is reset to zero and the timer begins counting down
1561 * using the new duration and period values.
1562 *
1563 * @param timer Address of timer.
1564 * @param duration Initial timer duration (in milliseconds).
1565 * @param period Timer period (in milliseconds).
1566 *
1567 * @return N/A
1568 */
Andrew Boiea354d492017-09-29 16:22:28 -07001569__syscall void k_timer_start(struct k_timer *timer,
1570 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001571
1572/**
1573 * @brief Stop a timer.
1574 *
1575 * This routine stops a running timer prematurely. The timer's stop function,
1576 * if one exists, is invoked by the caller.
1577 *
1578 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001579 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001580 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001581 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1582 * if @a k_timer_stop is to be called from ISRs.
1583 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001584 * @param timer Address of timer.
1585 *
1586 * @return N/A
1587 */
Andrew Boiea354d492017-09-29 16:22:28 -07001588__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001589
1590/**
1591 * @brief Read timer status.
1592 *
1593 * This routine reads the timer's status, which indicates the number of times
1594 * it has expired since its status was last read.
1595 *
1596 * Calling this routine resets the timer's status to zero.
1597 *
1598 * @param timer Address of timer.
1599 *
1600 * @return Timer status.
1601 */
Andrew Boiea354d492017-09-29 16:22:28 -07001602__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001603
1604/**
1605 * @brief Synchronize thread to timer expiration.
1606 *
1607 * This routine blocks the calling thread until the timer's status is non-zero
1608 * (indicating that it has expired at least once since it was last examined)
1609 * or the timer is stopped. If the timer status is already non-zero,
1610 * or the timer is already stopped, the caller continues without waiting.
1611 *
1612 * Calling this routine resets the timer's status to zero.
1613 *
1614 * This routine must not be used by interrupt handlers, since they are not
1615 * allowed to block.
1616 *
1617 * @param timer Address of timer.
1618 *
1619 * @return Timer status.
1620 */
Andrew Boiea354d492017-09-29 16:22:28 -07001621__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001622
Andy Ross52e444b2018-09-28 09:06:37 -07001623extern s32_t z_timeout_remaining(struct _timeout *timeout);
1624
Allan Stephens45bfa372016-10-12 12:39:42 -05001625/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001626 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001627 *
1628 * This routine computes the (approximate) time remaining before a running
1629 * timer next expires. If the timer is not running, it returns zero.
1630 *
1631 * @param timer Address of timer.
1632 *
1633 * @return Remaining time (in milliseconds).
1634 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001635__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001636
Patrik Flykt4344e272019-03-08 14:19:05 -07001637static inline u32_t z_impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001638{
Charles E. Youse0ad40222019-03-01 10:51:04 -08001639 const s32_t ticks = z_timeout_remaining(&timer->timeout);
Andy Ross88924062019-10-03 11:43:10 -07001640 return (ticks > 0) ? (u32_t)k_ticks_to_ms_floor64(ticks) : 0U;
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001641}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001642
Allan Stephensc98da842016-11-11 15:45:03 -05001643/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001644 * @brief Associate user-specific data with a timer.
1645 *
1646 * This routine records the @a user_data with the @a timer, to be retrieved
1647 * later.
1648 *
1649 * It can be used e.g. in a timer handler shared across multiple subsystems to
1650 * retrieve data specific to the subsystem this timer is associated with.
1651 *
1652 * @param timer Address of timer.
1653 * @param user_data User data to associate with the timer.
1654 *
1655 * @return N/A
1656 */
Andrew Boiea354d492017-09-29 16:22:28 -07001657__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1658
Anas Nashif954d5502018-02-25 08:37:28 -06001659/**
1660 * @internal
1661 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001662static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
Andrew Boiea354d492017-09-29 16:22:28 -07001663 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001664{
1665 timer->user_data = user_data;
1666}
1667
1668/**
1669 * @brief Retrieve the user-specific data from a timer.
1670 *
1671 * @param timer Address of timer.
1672 *
1673 * @return The user data.
1674 */
Andrew Boiea354d492017-09-29 16:22:28 -07001675__syscall void *k_timer_user_data_get(struct k_timer *timer);
1676
Patrik Flykt4344e272019-03-08 14:19:05 -07001677static inline void *z_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001678{
1679 return timer->user_data;
1680}
1681
Anas Nashif166f5192018-02-25 08:02:36 -06001682/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001683
Allan Stephensc98da842016-11-11 15:45:03 -05001684/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001685 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001686 * @{
1687 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001688
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001689/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001690 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001691 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001692 * This routine returns the elapsed time since the system booted,
1693 * in milliseconds.
1694 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001695 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001696 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001697 * While this function returns time in milliseconds, it does
1698 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001699 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001700 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001701 *
1702 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001703 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001704__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001705
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001706/**
1707 * @brief Enable clock always on in tickless kernel
1708 *
Andy Ross1db9f182019-06-25 10:09:45 -07001709 * Deprecated. This does nothing (it was always just a hint). This
1710 * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE
1711 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001712 *
1713 * @retval prev_status Previous status of always on flag
1714 */
Andy Ross1db9f182019-06-25 10:09:45 -07001715/* LCOV_EXCL_START */
1716__deprecated static inline int k_enable_sys_clock_always_on(void)
1717{
1718 __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1719 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1720
1721 return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE);
1722}
1723/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001724
1725/**
1726 * @brief Disable clock always on in tickless kernel
1727 *
Andy Ross1db9f182019-06-25 10:09:45 -07001728 * Deprecated. This does nothing (it was always just a hint). This
1729 * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE
1730 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001731 */
Andy Ross1db9f182019-06-25 10:09:45 -07001732/* LCOV_EXCL_START */
1733__deprecated static inline void k_disable_sys_clock_always_on(void)
1734{
1735 __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1736 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1737}
1738/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001739
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001740/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001741 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001742 *
Peter Bigota6067a32019-08-28 08:19:26 -05001743 * This routine returns the lower 32 bits of the system uptime in
1744 * milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001745 *
Peter Bigota6067a32019-08-28 08:19:26 -05001746 * Because correct conversion requires full precision of the system
1747 * clock there is no benefit to using this over k_uptime_get() unless
1748 * you know the application will never run long enough for the system
1749 * clock to approach 2^32 ticks. Calls to this function may involve
1750 * interrupt blocking and 64-bit math.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001751 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001752 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001753 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001754 * While this function returns time in milliseconds, it does
1755 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001756 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option
David B. Kinder8de9cc72019-06-25 10:44:55 -07001757 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001758 *
Peter Bigota6067a32019-08-28 08:19:26 -05001759 * @return The low 32 bits of the current uptime, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001760 */
Peter Bigota6067a32019-08-28 08:19:26 -05001761static inline u32_t k_uptime_get_32(void)
1762{
1763 return (u32_t)k_uptime_get();
1764}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001765
1766/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001767 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001768 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001769 * This routine computes the elapsed time between the current system uptime
1770 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001771 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001772 * @param reftime Pointer to a reference time, which is updated to the current
1773 * uptime upon return.
1774 *
1775 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001776 */
Andy Ross987c0e52018-09-27 16:50:00 -07001777static inline s64_t k_uptime_delta(s64_t *reftime)
1778{
1779 s64_t uptime, delta;
1780
1781 uptime = k_uptime_get();
1782 delta = uptime - *reftime;
1783 *reftime = uptime;
1784
1785 return delta;
1786}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001787
1788/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001789 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001791 * This routine computes the elapsed time between the current system uptime
1792 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001793 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001794 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1795 * need for interrupt locking and 64-bit math. However, the 32-bit result
1796 * cannot hold an elapsed time larger than approximately 50 days, so the
1797 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001799 * @param reftime Pointer to a reference time, which is updated to the current
1800 * uptime upon return.
1801 *
1802 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001803 */
Andy Ross987c0e52018-09-27 16:50:00 -07001804static inline u32_t k_uptime_delta_32(s64_t *reftime)
1805{
1806 return (u32_t)k_uptime_delta(reftime);
1807}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001808
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001809/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001810 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001812 * This routine returns the current time, as measured by the system's hardware
1813 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001815 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001816 */
Andrew Boie979b17f2019-10-03 15:20:41 -07001817static inline u32_t k_cycle_get_32(void)
1818{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08001819 return arch_k_cycle_get_32();
Andrew Boie979b17f2019-10-03 15:20:41 -07001820}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001821
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001822/**
Anas Nashif166f5192018-02-25 08:02:36 -06001823 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001824 */
1825
Allan Stephensc98da842016-11-11 15:45:03 -05001826/**
1827 * @cond INTERNAL_HIDDEN
1828 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001829
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001830struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001831 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001832 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001833 union {
1834 _wait_q_t wait_q;
1835
1836 _POLL_EVENT;
1837 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001838
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001839 _OBJECT_TRACING_NEXT_PTR(k_queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001840};
1841
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001842#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001843 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001844 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Stephanos Ioannidisf628dcd2019-09-11 18:09:49 +09001845 .lock = { }, \
1846 { \
1847 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
1848 _POLL_EVENT_OBJ_INIT(obj) \
1849 }, \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001850 _OBJECT_TRACING_INIT \
1851 }
1852
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05001853#define K_QUEUE_INITIALIZER __DEPRECATED_MACRO _K_QUEUE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001854
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001855extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1856
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001857/**
1858 * INTERNAL_HIDDEN @endcond
1859 */
1860
1861/**
1862 * @defgroup queue_apis Queue APIs
1863 * @ingroup kernel_apis
1864 * @{
1865 */
1866
1867/**
1868 * @brief Initialize a queue.
1869 *
1870 * This routine initializes a queue object, prior to its first use.
1871 *
1872 * @param queue Address of the queue.
1873 *
1874 * @return N/A
1875 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001876__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001877
1878/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001879 * @brief Cancel waiting on a queue.
1880 *
1881 * This routine causes first thread pending on @a queue, if any, to
1882 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001883 * If the queue is being waited on by k_poll(), it will return with
1884 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1885 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001886 *
1887 * @note Can be called by ISRs.
1888 *
1889 * @param queue Address of the queue.
1890 *
1891 * @return N/A
1892 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001893__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001894
1895/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001896 * @brief Append an element to the end of a queue.
1897 *
1898 * This routine appends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001899 * aligned on a word boundary, and the first word of the item is reserved
1900 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001901 *
1902 * @note Can be called by ISRs.
1903 *
1904 * @param queue Address of the queue.
1905 * @param data Address of the data item.
1906 *
1907 * @return N/A
1908 */
1909extern void k_queue_append(struct k_queue *queue, void *data);
1910
1911/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001912 * @brief Append an element to a queue.
1913 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001914 * This routine appends a data item to @a queue. There is an implicit memory
1915 * allocation to create an additional temporary bookkeeping data structure from
1916 * the calling thread's resource pool, which is automatically freed when the
1917 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001918 *
1919 * @note Can be called by ISRs.
1920 *
1921 * @param queue Address of the queue.
1922 * @param data Address of the data item.
1923 *
1924 * @retval 0 on success
1925 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1926 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301927__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001928
1929/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001930 * @brief Prepend an element to a queue.
1931 *
1932 * This routine prepends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001933 * aligned on a word boundary, and the first word of the item is reserved
1934 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001935 *
1936 * @note Can be called by ISRs.
1937 *
1938 * @param queue Address of the queue.
1939 * @param data Address of the data item.
1940 *
1941 * @return N/A
1942 */
1943extern void k_queue_prepend(struct k_queue *queue, void *data);
1944
1945/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001946 * @brief Prepend an element to a queue.
1947 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001948 * This routine prepends a data item to @a queue. There is an implicit memory
1949 * allocation to create an additional temporary bookkeeping data structure from
1950 * the calling thread's resource pool, which is automatically freed when the
1951 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001952 *
1953 * @note Can be called by ISRs.
1954 *
1955 * @param queue Address of the queue.
1956 * @param data Address of the data item.
1957 *
1958 * @retval 0 on success
1959 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1960 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301961__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001962
1963/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001964 * @brief Inserts an element to a queue.
1965 *
1966 * This routine inserts a data item to @a queue after previous item. A queue
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001967 * data item must be aligned on a word boundary, and the first word of
1968 * the item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001969 *
1970 * @note Can be called by ISRs.
1971 *
1972 * @param queue Address of the queue.
1973 * @param prev Address of the previous data item.
1974 * @param data Address of the data item.
1975 *
1976 * @return N/A
1977 */
1978extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1979
1980/**
1981 * @brief Atomically append a list of elements to a queue.
1982 *
1983 * This routine adds a list of data items to @a queue in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001984 * The data items must be in a singly-linked list, with the first word
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001985 * in each data item pointing to the next data item; the list must be
1986 * NULL-terminated.
1987 *
1988 * @note Can be called by ISRs.
1989 *
1990 * @param queue Address of the queue.
1991 * @param head Pointer to first node in singly-linked list.
1992 * @param tail Pointer to last node in singly-linked list.
1993 *
1994 * @return N/A
1995 */
1996extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1997
1998/**
1999 * @brief Atomically add a list of elements to a queue.
2000 *
2001 * This routine adds a list of data items to @a queue in one operation.
2002 * The data items must be in a singly-linked list implemented using a
2003 * sys_slist_t object. Upon completion, the original list is empty.
2004 *
2005 * @note Can be called by ISRs.
2006 *
2007 * @param queue Address of the queue.
2008 * @param list Pointer to sys_slist_t object.
2009 *
2010 * @return N/A
2011 */
2012extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
2013
2014/**
2015 * @brief Get an element from a queue.
2016 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002017 * This routine removes first data item from @a queue. The first word of the
2018 * data item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002019 *
2020 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2021 *
2022 * @param queue Address of the queue.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002023 * @param timeout Non-negative waiting period to obtain a data item (in
2024 * milliseconds), or one of the special values K_NO_WAIT and
2025 * K_FOREVER.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002026 *
2027 * @return Address of the data item if successful; NULL if returned
2028 * without waiting, or waiting period timed out.
2029 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002030__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002031
2032/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002033 * @brief Remove an element from a queue.
2034 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002035 * This routine removes data item from @a queue. The first word of the
2036 * data item is reserved for the kernel's use. Removing elements from k_queue
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002037 * rely on sys_slist_find_and_remove which is not a constant time operation.
2038 *
2039 * @note Can be called by ISRs
2040 *
2041 * @param queue Address of the queue.
2042 * @param data Address of the data item.
2043 *
2044 * @return true if data item was removed
2045 */
2046static inline bool k_queue_remove(struct k_queue *queue, void *data)
2047{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002048 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002049}
2050
2051/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002052 * @brief Append an element to a queue only if it's not present already.
2053 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002054 * This routine appends data item to @a queue. The first word of the data
2055 * item is reserved for the kernel's use. Appending elements to k_queue
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002056 * relies on sys_slist_is_node_in_list which is not a constant time operation.
2057 *
2058 * @note Can be called by ISRs
2059 *
2060 * @param queue Address of the queue.
2061 * @param data Address of the data item.
2062 *
2063 * @return true if data item was added, false if not
2064 */
2065static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
2066{
2067 sys_sfnode_t *test;
2068
2069 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
2070 if (test == (sys_sfnode_t *) data) {
2071 return false;
2072 }
2073 }
2074
2075 k_queue_append(queue, data);
2076 return true;
2077}
2078
2079/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002080 * @brief Query a queue to see if it has data available.
2081 *
2082 * Note that the data might be already gone by the time this function returns
2083 * if other threads are also trying to read from the queue.
2084 *
2085 * @note Can be called by ISRs.
2086 *
2087 * @param queue Address of the queue.
2088 *
2089 * @return Non-zero if the queue is empty.
2090 * @return 0 if data is available.
2091 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002092__syscall int k_queue_is_empty(struct k_queue *queue);
2093
Patrik Flykt4344e272019-03-08 14:19:05 -07002094static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002095{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002096 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002097}
2098
2099/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002100 * @brief Peek element at the head of queue.
2101 *
2102 * Return element from the head of queue without removing it.
2103 *
2104 * @param queue Address of the queue.
2105 *
2106 * @return Head element, or NULL if queue is empty.
2107 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002108__syscall void *k_queue_peek_head(struct k_queue *queue);
2109
Patrik Flykt4344e272019-03-08 14:19:05 -07002110static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002111{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002112 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002113}
2114
2115/**
2116 * @brief Peek element at the tail of queue.
2117 *
2118 * Return element from the tail of queue without removing it.
2119 *
2120 * @param queue Address of the queue.
2121 *
2122 * @return Tail element, or NULL if queue is empty.
2123 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002124__syscall void *k_queue_peek_tail(struct k_queue *queue);
2125
Patrik Flykt4344e272019-03-08 14:19:05 -07002126static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002127{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002128 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002129}
2130
2131/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002132 * @brief Statically define and initialize a queue.
2133 *
2134 * The queue can be accessed outside the module where it is defined using:
2135 *
2136 * @code extern struct k_queue <name>; @endcode
2137 *
2138 * @param name Name of the queue.
2139 */
2140#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002141 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002142 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002143
Anas Nashif166f5192018-02-25 08:02:36 -06002144/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002145
Wentong Wu5611e922019-06-20 23:51:27 +08002146#ifdef CONFIG_USERSPACE
2147/**
2148 * @brief futex structure
2149 *
2150 * A k_futex is a lightweight mutual exclusion primitive designed
2151 * to minimize kernel involvement. Uncontended operation relies
2152 * only on atomic access to shared memory. k_futex are tracked as
2153 * kernel objects and can live in user memory so any access bypass
2154 * the kernel object permission management mechanism.
2155 */
2156struct k_futex {
2157 atomic_t val;
2158};
2159
2160/**
2161 * @brief futex kernel data structure
2162 *
2163 * z_futex_data are the helper data structure for k_futex to complete
2164 * futex contended operation on kernel side, structure z_futex_data
2165 * of every futex object is invisible in user mode.
2166 */
2167struct z_futex_data {
2168 _wait_q_t wait_q;
2169 struct k_spinlock lock;
2170};
2171
2172#define Z_FUTEX_DATA_INITIALIZER(obj) \
2173 { \
2174 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \
2175 }
2176
2177/**
2178 * @defgroup futex_apis FUTEX APIs
2179 * @ingroup kernel_apis
2180 * @{
2181 */
2182
2183/**
Wentong Wu5611e922019-06-20 23:51:27 +08002184 * @brief Pend the current thread on a futex
2185 *
2186 * Tests that the supplied futex contains the expected value, and if so,
2187 * goes to sleep until some other thread calls k_futex_wake() on it.
2188 *
2189 * @param futex Address of the futex.
2190 * @param expected Expected value of the futex, if it is different the caller
2191 * will not wait on it.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002192 * @param timeout Non-negative waiting period on the futex, in milliseconds, or
2193 * one of the special values K_NO_WAIT or K_FOREVER.
Wentong Wu5611e922019-06-20 23:51:27 +08002194 * @retval -EACCES Caller does not have read access to futex address.
2195 * @retval -EAGAIN If the futex value did not match the expected parameter.
2196 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2197 * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup.
2198 * @retval 0 if the caller went to sleep and was woken up. The caller
2199 * should check the futex's value on wakeup to determine if it needs
2200 * to block again.
2201 */
2202__syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout);
2203
2204/**
2205 * @brief Wake one/all threads pending on a futex
2206 *
2207 * Wake up the highest priority thread pending on the supplied futex, or
2208 * wakeup all the threads pending on the supplied futex, and the behavior
2209 * depends on wake_all.
2210 *
2211 * @param futex Futex to wake up pending threads.
2212 * @param wake_all If true, wake up all pending threads; If false,
2213 * wakeup the highest priority thread.
2214 * @retval -EACCES Caller does not have access to the futex address.
2215 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2216 * @retval Number of threads that were woken up.
2217 */
2218__syscall int k_futex_wake(struct k_futex *futex, bool wake_all);
2219
2220/** @} */
2221#endif
2222
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002223struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002224 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002225};
2226
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002227/**
2228 * @cond INTERNAL_HIDDEN
2229 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002230#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002231 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002232 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002233 }
2234
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002235#define K_FIFO_INITIALIZER __DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002236
Allan Stephensc98da842016-11-11 15:45:03 -05002237/**
2238 * INTERNAL_HIDDEN @endcond
2239 */
2240
2241/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002242 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002243 * @ingroup kernel_apis
2244 * @{
2245 */
2246
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002247/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002248 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002249 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002250 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002251 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002252 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002253 *
2254 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002255 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002256 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002257#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002258 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002259
2260/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002261 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002262 *
2263 * This routine causes first thread pending on @a fifo, if any, to
2264 * return from k_fifo_get() call with NULL value (as if timeout
2265 * expired).
2266 *
2267 * @note Can be called by ISRs.
2268 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002269 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002270 *
2271 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002272 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002273 */
2274#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002275 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002276
2277/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002278 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002279 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002280 * This routine adds a data item to @a fifo. A FIFO data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002281 * aligned on a word boundary, and the first word of the item is reserved
2282 * for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002283 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002284 * @note Can be called by ISRs.
2285 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002286 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002287 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002288 *
2289 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002290 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002291 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002292#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002293 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002294
2295/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002296 * @brief Add an element to a FIFO queue.
2297 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002298 * This routine adds a data item to @a fifo. There is an implicit memory
2299 * allocation to create an additional temporary bookkeeping data structure from
2300 * the calling thread's resource pool, which is automatically freed when the
2301 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002302 *
2303 * @note Can be called by ISRs.
2304 *
2305 * @param fifo Address of the FIFO.
2306 * @param data Address of the data item.
2307 *
2308 * @retval 0 on success
2309 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002310 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002311 */
2312#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002313 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002314
2315/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002316 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002318 * This routine adds a list of data items to @a fifo in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002319 * The data items must be in a singly-linked list, with the first word of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002320 * each data item pointing to the next data item; the list must be
2321 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002322 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002323 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002324 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002325 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002326 * @param head Pointer to first node in singly-linked list.
2327 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002328 *
2329 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002330 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002331 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002332#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002333 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002334
2335/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002336 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002337 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002338 * This routine adds a list of data items to @a fifo in one operation.
2339 * The data items must be in a singly-linked list implemented using a
2340 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002341 * and must be re-initialized via sys_slist_init().
2342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002343 * @note Can be called by ISRs.
2344 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002345 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002346 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002347 *
2348 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002349 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002350 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002351#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002352 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002353
2354/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002355 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002356 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002357 * This routine removes a data item from @a fifo in a "first in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002358 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002359 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002360 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2361 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002362 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002363 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002364 * or one of the special values K_NO_WAIT and K_FOREVER.
2365 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002366 * @return Address of the data item if successful; NULL if returned
2367 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002368 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002369 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002370#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002371 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002372
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002373/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002374 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002375 *
2376 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002377 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002378 *
2379 * @note Can be called by ISRs.
2380 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002381 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002382 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002383 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002384 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002385 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002386 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002387#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002388 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002389
2390/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002391 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002392 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002393 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302394 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002395 * on each iteration of processing, a head container will be peeked,
2396 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002397 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002398 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002399 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002400 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002401 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002402 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002403 */
2404#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002405 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002406
2407/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002408 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002409 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002410 * Return element from the tail of FIFO queue (without removing it). A usecase
2411 * for this is if elements of the FIFO queue are themselves containers. Then
2412 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002413 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002414 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002415 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002416 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002417 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002418 */
2419#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002420 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002421
2422/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002423 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002424 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002425 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002426 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002427 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002428 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002429 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002430 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002431 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002432#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002433 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002434 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002435
Anas Nashif166f5192018-02-25 08:02:36 -06002436/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002437
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002439 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002440};
2441
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002442/**
2443 * @cond INTERNAL_HIDDEN
2444 */
2445
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002446#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002447 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002448 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002449 }
2450
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002451#define K_LIFO_INITIALIZER __DEPRECATED_MACRO _K_LIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002452
Allan Stephensc98da842016-11-11 15:45:03 -05002453/**
2454 * INTERNAL_HIDDEN @endcond
2455 */
2456
2457/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002458 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002459 * @ingroup kernel_apis
2460 * @{
2461 */
2462
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002463/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002464 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002465 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002466 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002467 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002468 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002469 *
2470 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002471 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002472 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002473#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002474 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002475
2476/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002477 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002478 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002479 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002480 * aligned on a word boundary, and the first word of the item is
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002481 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002483 * @note Can be called by ISRs.
2484 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002485 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002486 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002487 *
2488 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002489 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002490 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002491#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002492 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002493
2494/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002495 * @brief Add an element to a LIFO queue.
2496 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002497 * This routine adds a data item to @a lifo. There is an implicit memory
2498 * allocation to create an additional temporary bookkeeping data structure from
2499 * the calling thread's resource pool, which is automatically freed when the
2500 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002501 *
2502 * @note Can be called by ISRs.
2503 *
2504 * @param lifo Address of the LIFO.
2505 * @param data Address of the data item.
2506 *
2507 * @retval 0 on success
2508 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002509 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002510 */
2511#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002512 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002513
2514/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002515 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002516 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002517 * This routine removes a data item from @a lifo in a "last in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002518 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002519 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002520 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2521 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002522 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002523 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002524 * or one of the special values K_NO_WAIT and K_FOREVER.
2525 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002526 * @return Address of the data item if successful; NULL if returned
2527 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002528 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002529 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002530#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002531 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002532
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002533/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002534 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002535 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002536 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002537 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002538 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002540 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002541 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002542 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002543#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002544 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002545 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002546
Anas Nashif166f5192018-02-25 08:02:36 -06002547/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002548
2549/**
2550 * @cond INTERNAL_HIDDEN
2551 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302552#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002553
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002554typedef uintptr_t stack_data_t;
2555
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556struct k_stack {
2557 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002558 struct k_spinlock lock;
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002559 stack_data_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002560
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002561 _OBJECT_TRACING_NEXT_PTR(k_stack)
Andrew Boief3bee952018-05-02 17:44:39 -07002562 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002563};
2564
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002565#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002566 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002567 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002568 .base = stack_buffer, \
2569 .next = stack_buffer, \
2570 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002571 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002572 }
2573
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002574#define K_STACK_INITIALIZER __DEPRECATED_MACRO _K_STACK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002575
Allan Stephensc98da842016-11-11 15:45:03 -05002576/**
2577 * INTERNAL_HIDDEN @endcond
2578 */
2579
2580/**
2581 * @defgroup stack_apis Stack APIs
2582 * @ingroup kernel_apis
2583 * @{
2584 */
2585
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002586/**
2587 * @brief Initialize a stack.
2588 *
2589 * This routine initializes a stack object, prior to its first use.
2590 *
2591 * @param stack Address of the stack.
2592 * @param buffer Address of array used to hold stacked values.
2593 * @param num_entries Maximum number of values that can be stacked.
2594 *
2595 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002596 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002597 */
Andrew Boief3bee952018-05-02 17:44:39 -07002598void k_stack_init(struct k_stack *stack,
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002599 stack_data_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002600
2601
2602/**
2603 * @brief Initialize a stack.
2604 *
2605 * This routine initializes a stack object, prior to its first use. Internal
2606 * buffers will be allocated from the calling thread's resource pool.
2607 * This memory will be released if k_stack_cleanup() is called, or
2608 * userspace is enabled and the stack object loses all references to it.
2609 *
2610 * @param stack Address of the stack.
2611 * @param num_entries Maximum number of values that can be stacked.
2612 *
2613 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002614 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002615 */
2616
Adithya Baglody28080d32018-10-15 11:48:51 +05302617__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2618 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002619
2620/**
2621 * @brief Release a stack's allocated buffer
2622 *
2623 * If a stack object was given a dynamically allocated buffer via
2624 * k_stack_alloc_init(), this will free it. This function does nothing
2625 * if the buffer wasn't dynamically allocated.
2626 *
2627 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002628 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002629 */
2630void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002631
2632/**
2633 * @brief Push an element onto a stack.
2634 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002635 * This routine adds a stack_data_t value @a data to @a stack.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002636 *
2637 * @note Can be called by ISRs.
2638 *
2639 * @param stack Address of the stack.
2640 * @param data Value to push onto the stack.
2641 *
2642 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002643 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002644 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002645__syscall void k_stack_push(struct k_stack *stack, stack_data_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002646
2647/**
2648 * @brief Pop an element from a stack.
2649 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002650 * This routine removes a stack_data_t value from @a stack in a "last in,
2651 * first out" manner and stores the value in @a data.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002652 *
2653 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2654 *
2655 * @param stack Address of the stack.
2656 * @param data Address of area to hold the value popped from the stack.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002657 * @param timeout Non-negative waiting period to obtain a value (in
2658 * milliseconds), or one of the special values K_NO_WAIT and
2659 * K_FOREVER.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002660 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002661 * @retval 0 Element popped from stack.
2662 * @retval -EBUSY Returned without waiting.
2663 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002664 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002665 */
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002666__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data,
2667 s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002668
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002669/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002670 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002672 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002673 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002674 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002675 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002676 * @param name Name of the stack.
2677 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002678 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002679 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002680#define K_STACK_DEFINE(name, stack_num_entries) \
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002681 stack_data_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002682 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002683 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002684 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002685 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002686
Anas Nashif166f5192018-02-25 08:02:36 -06002687/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002688
Allan Stephens6bba9b02016-11-16 14:56:54 -05002689struct k_work;
Piotr Zięcik19d83492019-09-27 09:16:25 +02002690struct k_work_poll;
Allan Stephens6bba9b02016-11-16 14:56:54 -05002691
Piotr Zięcik19d83492019-09-27 09:16:25 +02002692/* private, used by k_poll and k_work_poll */
Piotr Zięcik1c4177d2019-08-27 12:19:26 +02002693typedef int (*_poller_cb_t)(struct k_poll_event *event, u32_t state);
2694struct _poller {
2695 volatile bool is_polling;
2696 struct k_thread *thread;
2697 _poller_cb_t cb;
2698};
2699
Allan Stephensc98da842016-11-11 15:45:03 -05002700/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002701 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002702 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002703 */
2704
Allan Stephens6bba9b02016-11-16 14:56:54 -05002705/**
2706 * @typedef k_work_handler_t
2707 * @brief Work item handler function type.
2708 *
2709 * A work item's handler function is executed by a workqueue's thread
2710 * when the work item is processed by the workqueue.
2711 *
2712 * @param work Address of the work item.
2713 *
2714 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002715 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002716 */
2717typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002718
2719/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002720 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002721 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002722
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002723struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002724 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002725 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002726};
2727
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002728enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002729 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002730};
2731
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002732struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002733 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002734 k_work_handler_t handler;
2735 atomic_t flags[1];
2736};
2737
Allan Stephens6bba9b02016-11-16 14:56:54 -05002738struct k_delayed_work {
2739 struct k_work work;
2740 struct _timeout timeout;
2741 struct k_work_q *work_q;
2742};
2743
Piotr Zięcik19d83492019-09-27 09:16:25 +02002744struct k_work_poll {
2745 struct k_work work;
2746 struct _poller poller;
2747 struct k_poll_event *events;
2748 int num_events;
2749 k_work_handler_t real_handler;
2750 struct _timeout timeout;
2751 int poll_result;
2752};
2753
Allan Stephens6bba9b02016-11-16 14:56:54 -05002754extern struct k_work_q k_sys_work_q;
2755
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002756/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002757 * INTERNAL_HIDDEN @endcond
2758 */
2759
Patrik Flykt4344e272019-03-08 14:19:05 -07002760#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002761 { \
2762 ._reserved = NULL, \
2763 .handler = work_handler, \
2764 .flags = { 0 } \
2765 }
2766
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002767#define K_WORK_INITIALIZER __DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002768
Allan Stephens6bba9b02016-11-16 14:56:54 -05002769/**
2770 * @brief Initialize a statically-defined work item.
2771 *
2772 * This macro can be used to initialize a statically-defined workqueue work
2773 * item, prior to its first use. For example,
2774 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002775 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002776 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002777 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002778 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002779 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002780 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002781#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002782 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002783
2784/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002785 * @brief Initialize a work item.
2786 *
2787 * This routine initializes a workqueue work item, prior to its first use.
2788 *
2789 * @param work Address of work item.
2790 * @param handler Function to invoke each time work item is processed.
2791 *
2792 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002793 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002794 */
2795static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2796{
Patrik Flykt4344e272019-03-08 14:19:05 -07002797 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002798}
2799
2800/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002801 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002802 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002803 * This routine submits work item @a work to be processed by workqueue
2804 * @a work_q. If the work item is already pending in the workqueue's queue
2805 * as a result of an earlier submission, this routine has no effect on the
2806 * work item. If the work item has already been processed, or is currently
2807 * being processed, its work is considered complete and the work item can be
2808 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002809 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002810 * @warning
2811 * A submitted work item must not be modified until it has been processed
2812 * by the workqueue.
2813 *
2814 * @note Can be called by ISRs.
2815 *
2816 * @param work_q Address of workqueue.
2817 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002818 *
2819 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002820 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002821 */
2822static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2823 struct k_work *work)
2824{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002825 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002826 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002827 }
2828}
2829
2830/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002831 * @brief Submit a work item to a user mode workqueue
2832 *
David B. Kinder06d78352018-12-17 14:32:40 -08002833 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002834 * memory allocation is made from the caller's resource pool which is freed
2835 * once the worker thread consumes the k_work item. The workqueue
2836 * thread must have memory access to the k_work item being submitted. The caller
2837 * must have permission granted on the work_q parameter's queue object.
2838 *
2839 * Otherwise this works the same as k_work_submit_to_queue().
2840 *
2841 * @note Can be called by ISRs.
2842 *
2843 * @param work_q Address of workqueue.
2844 * @param work Address of work item.
2845 *
2846 * @retval -EBUSY if the work item was already in some workqueue
2847 * @retval -ENOMEM if no memory for thread resource pool allocation
2848 * @retval 0 Success
2849 * @req K-WORK-001
2850 */
2851static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2852 struct k_work *work)
2853{
2854 int ret = -EBUSY;
2855
2856 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2857 ret = k_queue_alloc_append(&work_q->queue, work);
2858
2859 /* Couldn't insert into the queue. Clear the pending bit
2860 * so the work item can be submitted again
2861 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002862 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002863 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2864 }
2865 }
2866
2867 return ret;
2868}
2869
2870/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002871 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002872 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002873 * This routine indicates if work item @a work is pending in a workqueue's
2874 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002875 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002876 * @note Can be called by ISRs.
2877 *
2878 * @param work Address of work item.
2879 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002880 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002881 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002882 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002883static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002884{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002885 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002886}
2887
2888/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002889 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002890 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002891 * This routine starts workqueue @a work_q. The workqueue spawns its work
2892 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002893 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002894 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002895 * @param stack Pointer to work queue thread's stack space, as defined by
2896 * K_THREAD_STACK_DEFINE()
2897 * @param stack_size Size of the work queue thread's stack (in bytes), which
2898 * should either be the same constant passed to
2899 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002900 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002901 *
2902 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002903 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002904 */
Andrew Boie507852a2017-07-25 18:47:07 -07002905extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002906 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002907 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002908
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002909/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002910 * @brief Start a workqueue in user mode
2911 *
2912 * This works identically to k_work_q_start() except it is callable from user
2913 * mode, and the worker thread created will run in user mode.
2914 * The caller must have permissions granted on both the work_q parameter's
2915 * thread and queue objects, and the same restrictions on priority apply as
2916 * k_thread_create().
2917 *
2918 * @param work_q Address of workqueue.
2919 * @param stack Pointer to work queue thread's stack space, as defined by
2920 * K_THREAD_STACK_DEFINE()
2921 * @param stack_size Size of the work queue thread's stack (in bytes), which
2922 * should either be the same constant passed to
2923 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2924 * @param prio Priority of the work queue's thread.
2925 *
2926 * @return N/A
2927 * @req K-WORK-001
2928 */
2929extern void k_work_q_user_start(struct k_work_q *work_q,
2930 k_thread_stack_t *stack,
2931 size_t stack_size, int prio);
2932
2933/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002934 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002935 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002936 * This routine initializes a workqueue delayed work item, prior to
2937 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002938 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002939 * @param work Address of delayed work item.
2940 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002941 *
2942 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002943 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002944 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002945extern void k_delayed_work_init(struct k_delayed_work *work,
2946 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002947
2948/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002949 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002950 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002951 * This routine schedules work item @a work to be processed by workqueue
2952 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002953 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002954 * Only when the countdown completes is the work item actually submitted to
2955 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002956 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002957 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002958 * counting down cancels the existing submission and restarts the
2959 * countdown using the new delay. Note that this behavior is
2960 * inherently subject to race conditions with the pre-existing
2961 * timeouts and work queue, so care must be taken to synchronize such
2962 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002963 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002964 * @warning
2965 * A delayed work item must not be modified until it has been processed
2966 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002967 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002968 * @note Can be called by ISRs.
2969 *
2970 * @param work_q Address of workqueue.
2971 * @param work Address of delayed work item.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01002972 * @param delay Non-negative delay before submitting the work item (in
2973 * milliseconds).
Allan Stephens6bba9b02016-11-16 14:56:54 -05002974 *
2975 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002976 * @retval -EINVAL Work item is being processed or has completed its work.
2977 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002978 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002979 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002980extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2981 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002982 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002983
2984/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002985 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002986 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002987 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002988 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002989 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002990 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002991 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002992 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08002993 * @note The result of calling this on a k_delayed_work item that has
2994 * not been submitted (i.e. before the return of the
2995 * k_delayed_work_submit_to_queue() call) is undefined.
2996 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002997 * @param work Address of delayed work item.
2998 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002999 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003000 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003001 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003002 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04003003extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003004
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003005/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003006 * @brief Submit a work item to the system workqueue.
3007 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003008 * This routine submits work item @a work to be processed by the system
3009 * workqueue. If the work item is already pending in the workqueue's queue
3010 * as a result of an earlier submission, this routine has no effect on the
3011 * work item. If the work item has already been processed, or is currently
3012 * being processed, its work is considered complete and the work item can be
3013 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003014 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003015 * @warning
3016 * Work items submitted to the system workqueue should avoid using handlers
3017 * that block or yield since this may prevent the system workqueue from
3018 * processing other work items in a timely manner.
3019 *
3020 * @note Can be called by ISRs.
3021 *
3022 * @param work Address of work item.
3023 *
3024 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003025 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003026 */
3027static inline void k_work_submit(struct k_work *work)
3028{
3029 k_work_submit_to_queue(&k_sys_work_q, work);
3030}
3031
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003032/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003033 * @brief Submit a delayed work item to the system workqueue.
3034 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003035 * This routine schedules work item @a work to be processed by the system
3036 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003037 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003038 * Only when the countdown completes is the work item actually submitted to
3039 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003040 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003041 * Submitting a previously submitted delayed work item that is still
3042 * counting down cancels the existing submission and restarts the countdown
3043 * using the new delay. If the work item is currently pending on the
3044 * workqueue's queue because the countdown has completed it is too late to
3045 * resubmit the item, and resubmission fails without impacting the work item.
3046 * If the work item has already been processed, or is currently being processed,
3047 * its work is considered complete and the work item can be resubmitted.
3048 *
3049 * @warning
3050 * Work items submitted to the system workqueue should avoid using handlers
3051 * that block or yield since this may prevent the system workqueue from
3052 * processing other work items in a timely manner.
3053 *
3054 * @note Can be called by ISRs.
3055 *
3056 * @param work Address of delayed work item.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003057 * @param delay Non-negative delay before submitting the work item (in
3058 * milliseconds).
Allan Stephens6bba9b02016-11-16 14:56:54 -05003059 *
3060 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003061 * @retval -EINVAL Work item is being processed or has completed its work.
3062 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003063 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003064 */
3065static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003066 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003067{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05003068 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003069}
3070
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003071/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02003072 * @brief Get time remaining before a delayed work gets scheduled.
3073 *
3074 * This routine computes the (approximate) time remaining before a
3075 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02003076 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02003077 *
3078 * @param work Delayed work item.
3079 *
3080 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003081 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02003082 */
Kumar Galacc334c72017-04-21 10:55:34 -05003083static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02003084{
Andy Ross88924062019-10-03 11:43:10 -07003085 return k_ticks_to_ms_floor64(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02003086}
3087
Piotr Zięcik19d83492019-09-27 09:16:25 +02003088/**
3089 * @brief Initialize a triggered work item.
3090 *
3091 * This routine initializes a workqueue triggered work item, prior to
3092 * its first use.
3093 *
3094 * @param work Address of triggered work item.
3095 * @param handler Function to invoke each time work item is processed.
3096 *
3097 * @return N/A
3098 */
3099extern void k_work_poll_init(struct k_work_poll *work,
3100 k_work_handler_t handler);
3101
3102/**
3103 * @brief Submit a triggered work item.
3104 *
3105 * This routine schedules work item @a work to be processed by workqueue
3106 * @a work_q when one of the given @a events is signaled. The routine
3107 * initiates internal poller for the work item and then returns to the caller.
3108 * Only when one of the watched events happen the work item is actually
3109 * submitted to the workqueue and becomes pending.
3110 *
3111 * Submitting a previously submitted triggered work item that is still
3112 * waiting for the event cancels the existing submission and reschedules it
3113 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003114 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003115 * so care must be taken to synchronize such resubmissions externally.
3116 *
3117 * @note Can be called by ISRs.
3118 *
3119 * @warning
3120 * Provided array of events as well as a triggered work item must be placed
3121 * in persistent memory (valid until work handler execution or work
3122 * cancellation) and cannot be modified after submission.
3123 *
3124 * @param work_q Address of workqueue.
3125 * @param work Address of delayed work item.
3126 * @param events An array of pointers to events which trigger the work.
3127 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003128 * @param timeout Non-negative timeout after which the work will be scheduled
3129 * for execution even if not triggered.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003130 *
3131 *
3132 * @retval 0 Work item started watching for events.
3133 * @retval -EINVAL Work item is being processed or has completed its work.
3134 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3135 */
3136extern int k_work_poll_submit_to_queue(struct k_work_q *work_q,
3137 struct k_work_poll *work,
3138 struct k_poll_event *events,
3139 int num_events,
3140 s32_t timeout);
3141
3142/**
3143 * @brief Submit a triggered work item to the system workqueue.
3144 *
3145 * This routine schedules work item @a work to be processed by system
3146 * workqueue when one of the given @a events is signaled. The routine
3147 * initiates internal poller for the work item and then returns to the caller.
3148 * Only when one of the watched events happen the work item is actually
3149 * submitted to the workqueue and becomes pending.
3150 *
3151 * Submitting a previously submitted triggered work item that is still
3152 * waiting for the event cancels the existing submission and reschedules it
3153 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003154 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003155 * so care must be taken to synchronize such resubmissions externally.
3156 *
3157 * @note Can be called by ISRs.
3158 *
3159 * @warning
3160 * Provided array of events as well as a triggered work item must not be
3161 * modified until the item has been processed by the workqueue.
3162 *
3163 * @param work Address of delayed work item.
3164 * @param events An array of pointers to events which trigger the work.
3165 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003166 * @param timeout Non-negative timeout after which the work will be scheduled
3167 * for execution even if not triggered.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003168 *
3169 * @retval 0 Work item started watching for events.
3170 * @retval -EINVAL Work item is being processed or has completed its work.
3171 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3172 */
3173static inline int k_work_poll_submit(struct k_work_poll *work,
3174 struct k_poll_event *events,
3175 int num_events,
3176 s32_t timeout)
3177{
3178 return k_work_poll_submit_to_queue(&k_sys_work_q, work,
3179 events, num_events, timeout);
3180}
3181
3182/**
3183 * @brief Cancel a triggered work item.
3184 *
3185 * This routine cancels the submission of triggered work item @a work.
3186 * A triggered work item can only be canceled if no event triggered work
3187 * submission.
3188 *
3189 * @note Can be called by ISRs.
3190 *
3191 * @param work Address of delayed work item.
3192 *
David B. Kinder73896c02019-10-28 16:27:57 -07003193 * @retval 0 Work item canceled.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003194 * @retval -EINVAL Work item is being processed or has completed its work.
3195 */
3196extern int k_work_poll_cancel(struct k_work_poll *work);
3197
Anas Nashif166f5192018-02-25 08:02:36 -06003198/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003199/**
Anas Nashifce78d162018-05-24 12:43:11 -05003200 * @defgroup mutex_apis Mutex APIs
3201 * @ingroup kernel_apis
3202 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003203 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003204
Anas Nashifce78d162018-05-24 12:43:11 -05003205/**
3206 * Mutex Structure
3207 * @ingroup mutex_apis
3208 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003209struct k_mutex {
3210 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05003211 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04003212 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05003213 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003214 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003215
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003216 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003217};
3218
Anas Nashifce78d162018-05-24 12:43:11 -05003219/**
3220 * @cond INTERNAL_HIDDEN
3221 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003222#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003223 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003224 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003225 .owner = NULL, \
3226 .lock_count = 0, \
3227 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003228 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003229 }
3230
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003231#define K_MUTEX_INITIALIZER __DEPRECATED_MACRO _K_MUTEX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003232
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003233/**
Allan Stephensc98da842016-11-11 15:45:03 -05003234 * INTERNAL_HIDDEN @endcond
3235 */
3236
3237/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003238 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003239 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003240 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003241 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003242 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003243 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003244 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003245 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003246 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003247#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003248 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003249 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003250
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003251/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003252 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003253 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003254 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003255 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003256 * Upon completion, the mutex is available and does not have an owner.
3257 *
3258 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003259 *
3260 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003261 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003262 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003263__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003264
3265/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003266 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003267 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003268 * This routine locks @a mutex. If the mutex is locked by another thread,
3269 * the calling thread waits until the mutex becomes available or until
3270 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003271 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003272 * A thread is permitted to lock a mutex it has already locked. The operation
3273 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003274 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003275 * @param mutex Address of the mutex.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003276 * @param timeout Non-negative waiting period to lock the mutex (in
3277 * milliseconds), or one of the special values K_NO_WAIT and
3278 * K_FOREVER.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003279 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003280 * @retval 0 Mutex locked.
3281 * @retval -EBUSY Returned without waiting.
3282 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003283 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003284 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003285__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003286
3287/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003288 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003290 * This routine unlocks @a mutex. The mutex must already be locked by the
3291 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003292 *
3293 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003294 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003295 * thread.
3296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003297 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003298 *
3299 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003300 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003301 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003302__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003303
Allan Stephensc98da842016-11-11 15:45:03 -05003304/**
Anas Nashif166f5192018-02-25 08:02:36 -06003305 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003306 */
3307
3308/**
3309 * @cond INTERNAL_HIDDEN
3310 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003311
3312struct k_sem {
3313 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303314 u32_t count;
3315 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003316 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003317
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003318 _OBJECT_TRACING_NEXT_PTR(k_sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003319};
3320
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003321#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003322 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003323 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003324 .count = initial_count, \
3325 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003326 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003327 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003328 }
3329
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003330#define K_SEM_INITIALIZER __DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003331
Allan Stephensc98da842016-11-11 15:45:03 -05003332/**
3333 * INTERNAL_HIDDEN @endcond
3334 */
3335
3336/**
3337 * @defgroup semaphore_apis Semaphore APIs
3338 * @ingroup kernel_apis
3339 * @{
3340 */
3341
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003342/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003343 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003345 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003346 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003347 * @param sem Address of the semaphore.
3348 * @param initial_count Initial semaphore count.
3349 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003350 *
3351 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003352 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003353 */
Andrew Boie99280232017-09-29 14:17:47 -07003354__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
3355 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003356
3357/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003358 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003359 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003360 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003362 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3363 *
3364 * @param sem Address of the semaphore.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003365 * @param timeout Non-negative waiting period to take the semaphore (in
3366 * milliseconds), or one of the special values K_NO_WAIT and
3367 * K_FOREVER.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003368 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05003369 * @note When porting code from the nanokernel legacy API to the new API, be
3370 * careful with the return value of this function. The return value is the
3371 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
3372 * non-zero means failure, while the nano_sem_take family returns 1 for success
3373 * and 0 for failure.
3374 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003375 * @retval 0 Semaphore taken.
3376 * @retval -EBUSY Returned without waiting.
3377 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003378 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003379 */
Andrew Boie99280232017-09-29 14:17:47 -07003380__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003381
3382/**
3383 * @brief Give a semaphore.
3384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003385 * This routine gives @a sem, unless the semaphore is already at its maximum
3386 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003388 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003390 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003391 *
3392 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003393 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003394 */
Andrew Boie99280232017-09-29 14:17:47 -07003395__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003396
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003397/**
3398 * @brief Reset a semaphore's count to zero.
3399 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003400 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003401 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003402 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003403 *
3404 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003405 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003406 */
Andrew Boie990bf162017-10-03 12:36:49 -07003407__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003408
Anas Nashif954d5502018-02-25 08:37:28 -06003409/**
3410 * @internal
3411 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003412static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003413{
Patrik Flykt24d71432019-03-26 19:57:45 -06003414 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003415}
3416
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003417/**
3418 * @brief Get a semaphore's count.
3419 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003420 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003422 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003423 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003424 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003425 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003426 */
Andrew Boie990bf162017-10-03 12:36:49 -07003427__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003428
Anas Nashif954d5502018-02-25 08:37:28 -06003429/**
3430 * @internal
3431 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003432static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003433{
3434 return sem->count;
3435}
3436
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003437/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003438 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003439 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003440 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003441 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003442 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003443 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003444 * @param name Name of the semaphore.
3445 * @param initial_count Initial semaphore count.
3446 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003447 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003448 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003449#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003450 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003451 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303452 BUILD_ASSERT(((count_limit) != 0) && \
3453 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003454
Anas Nashif166f5192018-02-25 08:02:36 -06003455/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003456
3457/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003458 * @defgroup msgq_apis Message Queue APIs
3459 * @ingroup kernel_apis
3460 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003461 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003462
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003463/**
3464 * @brief Message Queue Structure
3465 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003466struct k_msgq {
3467 _wait_q_t wait_q;
Andy Rossbe03dbd2018-07-26 10:23:02 -07003468 struct k_spinlock lock;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003469 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003470 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003471 char *buffer_start;
3472 char *buffer_end;
3473 char *read_ptr;
3474 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003475 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003476
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003477 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Andrew Boie0fe789f2018-04-12 18:35:56 -07003478 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003479};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003480/**
3481 * @cond INTERNAL_HIDDEN
3482 */
3483
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003484
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003485#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003486 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003487 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003488 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003489 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003490 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003491 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003492 .read_ptr = q_buffer, \
3493 .write_ptr = q_buffer, \
3494 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003495 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003496 }
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003497#define K_MSGQ_INITIALIZER __DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003498/**
3499 * INTERNAL_HIDDEN @endcond
3500 */
3501
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003502
Andrew Boie0fe789f2018-04-12 18:35:56 -07003503#define K_MSGQ_FLAG_ALLOC BIT(0)
3504
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003505/**
3506 * @brief Message Queue Attributes
3507 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303508struct k_msgq_attrs {
3509 size_t msg_size;
3510 u32_t max_msgs;
3511 u32_t used_msgs;
3512};
3513
Allan Stephensc98da842016-11-11 15:45:03 -05003514
3515/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003516 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003518 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3519 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003520 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3521 * message is similarly aligned to this boundary, @a q_msg_size must also be
3522 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003523 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003524 * The message queue can be accessed outside the module where it is defined
3525 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003526 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003527 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003529 * @param q_name Name of the message queue.
3530 * @param q_msg_size Message size (in bytes).
3531 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003532 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003533 *
3534 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003535 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003536#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3537 static char __noinit __aligned(q_align) \
3538 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3539 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
3540 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003541 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003542
Peter Mitsisd7a37502016-10-13 11:37:40 -04003543/**
3544 * @brief Initialize a message queue.
3545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003546 * This routine initializes a message queue object, prior to its first use.
3547 *
Allan Stephensda827222016-11-09 14:23:58 -06003548 * The message queue's ring buffer must contain space for @a max_msgs messages,
3549 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3550 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3551 * that each message is similarly aligned to this boundary, @a q_msg_size
3552 * must also be a multiple of N.
3553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003554 * @param q Address of the message queue.
3555 * @param buffer Pointer to ring buffer that holds queued messages.
3556 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003557 * @param max_msgs Maximum number of messages that can be queued.
3558 *
3559 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003560 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003561 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003562void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3563 u32_t max_msgs);
3564
3565/**
3566 * @brief Initialize a message queue.
3567 *
3568 * This routine initializes a message queue object, prior to its first use,
3569 * allocating its internal ring buffer from the calling thread's resource
3570 * pool.
3571 *
3572 * Memory allocated for the ring buffer can be released by calling
3573 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3574 * all of its references.
3575 *
3576 * @param q Address of the message queue.
3577 * @param msg_size Message size (in bytes).
3578 * @param max_msgs Maximum number of messages that can be queued.
3579 *
3580 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3581 * thread's resource pool, or -EINVAL if the size parameters cause
3582 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003583 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003584 */
3585__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3586 u32_t max_msgs);
3587
3588
3589void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003590
3591/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003592 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003593 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003594 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003595 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003596 * @note Can be called by ISRs.
3597 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003598 * @param q Address of the message queue.
3599 * @param data Pointer to the message.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003600 * @param timeout Non-negative waiting period to add the message (in
3601 * milliseconds), or one of the special values K_NO_WAIT and
3602 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003603 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003604 * @retval 0 Message sent.
3605 * @retval -ENOMSG Returned without waiting or queue purged.
3606 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003607 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003608 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003609__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003610
3611/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003612 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003613 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003614 * This routine receives a message from message queue @a q in a "first in,
3615 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003616 *
Allan Stephensc98da842016-11-11 15:45:03 -05003617 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003619 * @param q Address of the message queue.
3620 * @param data Address of area to hold the received message.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003621 * @param timeout Non-negative waiting period to receive the message (in
3622 * milliseconds), or one of the special values K_NO_WAIT and
3623 * K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003624 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003625 * @retval 0 Message received.
3626 * @retval -ENOMSG Returned without waiting.
3627 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003628 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003629 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003630__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003631
3632/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003633 * @brief Peek/read a message from a message queue.
3634 *
3635 * This routine reads a message from message queue @a q in a "first in,
3636 * first out" manner and leaves the message in the queue.
3637 *
3638 * @note Can be called by ISRs.
3639 *
3640 * @param q Address of the message queue.
3641 * @param data Address of area to hold the message read from the queue.
3642 *
3643 * @retval 0 Message read.
3644 * @retval -ENOMSG Returned when the queue has no message.
3645 * @req K-MSGQ-002
3646 */
3647__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3648
3649/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003650 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003651 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003652 * This routine discards all unreceived messages in a message queue's ring
3653 * buffer. Any threads that are blocked waiting to send a message to the
3654 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003656 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003657 *
3658 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003659 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003660 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003661__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003662
Peter Mitsis67be2492016-10-07 11:44:34 -04003663/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003664 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003665 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003666 * This routine returns the number of unused entries in a message queue's
3667 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003668 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003669 * @param q Address of the message queue.
3670 *
3671 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003672 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003673 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003674__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3675
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303676/**
3677 * @brief Get basic attributes of a message queue.
3678 *
3679 * This routine fetches basic attributes of message queue into attr argument.
3680 *
3681 * @param q Address of the message queue.
3682 * @param attrs pointer to message queue attribute structure.
3683 *
3684 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003685 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303686 */
3687__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3688
3689
Patrik Flykt4344e272019-03-08 14:19:05 -07003690static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003691{
3692 return q->max_msgs - q->used_msgs;
3693}
3694
Peter Mitsisd7a37502016-10-13 11:37:40 -04003695/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003696 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003697 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003698 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003699 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003700 * @param q Address of the message queue.
3701 *
3702 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003703 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003704 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003705__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3706
Patrik Flykt4344e272019-03-08 14:19:05 -07003707static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003708{
3709 return q->used_msgs;
3710}
3711
Anas Nashif166f5192018-02-25 08:02:36 -06003712/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003713
3714/**
3715 * @defgroup mem_pool_apis Memory Pool APIs
3716 * @ingroup kernel_apis
3717 * @{
3718 */
3719
Andy Ross73cb9582017-05-09 10:42:39 -07003720/* Note on sizing: the use of a 20 bit field for block means that,
3721 * assuming a reasonable minimum block size of 16 bytes, we're limited
3722 * to 16M of memory managed by a single pool. Long term it would be
3723 * good to move to a variable bit size based on configuration.
3724 */
3725struct k_mem_block_id {
3726 u32_t pool : 8;
3727 u32_t level : 4;
3728 u32_t block : 20;
3729};
3730
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003731struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003732 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003733 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003734};
3735
Anas Nashif166f5192018-02-25 08:02:36 -06003736/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003737
3738/**
3739 * @defgroup mailbox_apis Mailbox APIs
3740 * @ingroup kernel_apis
3741 * @{
3742 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003743
3744struct k_mbox_msg {
3745 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003746 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003747 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003748 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003749 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003750 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003751 /** sender's message data buffer */
3752 void *tx_data;
3753 /** internal use only - needed for legacy API support */
3754 void *_rx_data;
3755 /** message data block descriptor */
3756 struct k_mem_block tx_block;
3757 /** source thread id */
3758 k_tid_t rx_source_thread;
3759 /** target thread id */
3760 k_tid_t tx_target_thread;
3761 /** internal use only - thread waiting on send (may be a dummy) */
3762 k_tid_t _syncing_thread;
3763#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3764 /** internal use only - semaphore used during asynchronous send */
3765 struct k_sem *_async_sem;
3766#endif
3767};
3768
3769struct k_mbox {
3770 _wait_q_t tx_msg_queue;
3771 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003772 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003773
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003774 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003775};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003776/**
3777 * @cond INTERNAL_HIDDEN
3778 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003779
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003780#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003781 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003782 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3783 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003784 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003785 }
3786
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003787#define K_MBOX_INITIALIZER __DEPRECATED_MACRO _K_MBOX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003788
Peter Mitsis12092702016-10-14 12:57:23 -04003789/**
Allan Stephensc98da842016-11-11 15:45:03 -05003790 * INTERNAL_HIDDEN @endcond
3791 */
3792
3793/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003794 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003796 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003797 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003798 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003800 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003801 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003802 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003803#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003804 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003805 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003806
Peter Mitsis12092702016-10-14 12:57:23 -04003807/**
3808 * @brief Initialize a mailbox.
3809 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003810 * This routine initializes a mailbox object, prior to its first use.
3811 *
3812 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003813 *
3814 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003815 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003816 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003817extern void k_mbox_init(struct k_mbox *mbox);
3818
Peter Mitsis12092702016-10-14 12:57:23 -04003819/**
3820 * @brief Send a mailbox message in a synchronous manner.
3821 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003822 * This routine sends a message to @a mbox and waits for a receiver to both
3823 * receive and process it. The message data may be in a buffer, in a memory
3824 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003825 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003826 * @param mbox Address of the mailbox.
3827 * @param tx_msg Address of the transmit message descriptor.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003828 * @param timeout Non-negative waiting period for the message to be received (in
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003829 * milliseconds), or one of the special values K_NO_WAIT
3830 * and K_FOREVER. Once the message has been received,
3831 * this routine waits as long as necessary for the message
3832 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003833 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003834 * @retval 0 Message sent.
3835 * @retval -ENOMSG Returned without waiting.
3836 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003837 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003838 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003839extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003840 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003841
Peter Mitsis12092702016-10-14 12:57:23 -04003842/**
3843 * @brief Send a mailbox message in an asynchronous manner.
3844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003845 * This routine sends a message to @a mbox without waiting for a receiver
3846 * to process it. The message data may be in a buffer, in a memory pool block,
3847 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3848 * will be given when the message has been both received and completely
3849 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003850 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003851 * @param mbox Address of the mailbox.
3852 * @param tx_msg Address of the transmit message descriptor.
3853 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003854 *
3855 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003856 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003857 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003858extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003859 struct k_sem *sem);
3860
Peter Mitsis12092702016-10-14 12:57:23 -04003861/**
3862 * @brief Receive a mailbox message.
3863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003864 * This routine receives a message from @a mbox, then optionally retrieves
3865 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003867 * @param mbox Address of the mailbox.
3868 * @param rx_msg Address of the receive message descriptor.
3869 * @param buffer Address of the buffer to receive data, or NULL to defer data
3870 * retrieval and message disposal until later.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003871 * @param timeout Non-negative waiting period for a message to be received (in
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003872 * milliseconds), or one of the special values K_NO_WAIT
3873 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003874 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003875 * @retval 0 Message received.
3876 * @retval -ENOMSG Returned without waiting.
3877 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003878 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003879 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003880extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003881 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003882
3883/**
3884 * @brief Retrieve mailbox message data into a buffer.
3885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003886 * This routine completes the processing of a received message by retrieving
3887 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003888 *
3889 * Alternatively, this routine can be used to dispose of a received message
3890 * without retrieving its data.
3891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003892 * @param rx_msg Address of the receive message descriptor.
3893 * @param buffer Address of the buffer to receive data, or NULL to discard
3894 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003895 *
3896 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003897 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003898 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003899extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003900
3901/**
3902 * @brief Retrieve mailbox message data into a memory pool block.
3903 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003904 * This routine completes the processing of a received message by retrieving
3905 * its data into a memory pool block, then disposing of the message.
3906 * The memory pool block that results from successful retrieval must be
3907 * returned to the pool once the data has been processed, even in cases
3908 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003909 *
3910 * Alternatively, this routine can be used to dispose of a received message
3911 * without retrieving its data. In this case there is no need to return a
3912 * memory pool block to the pool.
3913 *
3914 * This routine allocates a new memory pool block for the data only if the
3915 * data is not already in one. If a new block cannot be allocated, the routine
3916 * returns a failure code and the received message is left unchanged. This
3917 * permits the caller to reattempt data retrieval at a later time or to dispose
3918 * of the received message without retrieving its data.
3919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003920 * @param rx_msg Address of a receive message descriptor.
3921 * @param pool Address of memory pool, or NULL to discard data.
3922 * @param block Address of the area to hold memory pool block info.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01003923 * @param timeout Non-negative waiting period to wait for a memory pool block
3924 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003925 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003926 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003927 * @retval 0 Data retrieved.
3928 * @retval -ENOMEM Returned without waiting.
3929 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003930 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003931 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003932extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003933 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003934 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003935
Anas Nashif166f5192018-02-25 08:02:36 -06003936/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003937
3938/**
Anas Nashifce78d162018-05-24 12:43:11 -05003939 * @defgroup pipe_apis Pipe APIs
3940 * @ingroup kernel_apis
3941 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003942 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003943
Anas Nashifce78d162018-05-24 12:43:11 -05003944/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003945struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003946 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3947 size_t size; /**< Buffer size */
3948 size_t bytes_used; /**< # bytes used in buffer */
3949 size_t read_index; /**< Where in buffer to read from */
3950 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08003951 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003952
3953 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003954 _wait_q_t readers; /**< Reader wait queue */
3955 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003956 } wait_q;
3957
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003958 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Anas Nashifce78d162018-05-24 12:43:11 -05003959 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003960};
3961
Anas Nashifce78d162018-05-24 12:43:11 -05003962/**
3963 * @cond INTERNAL_HIDDEN
3964 */
3965#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3966
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003967#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
3968 { \
3969 .buffer = pipe_buffer, \
3970 .size = pipe_buffer_size, \
3971 .bytes_used = 0, \
3972 .read_index = 0, \
3973 .write_index = 0, \
3974 .lock = {}, \
3975 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003976 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
3977 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003978 }, \
3979 _OBJECT_TRACING_INIT \
3980 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003981 }
3982
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003983#define K_PIPE_INITIALIZER __DEPRECATED_MACRO _K_PIPE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003984
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003985/**
Allan Stephensc98da842016-11-11 15:45:03 -05003986 * INTERNAL_HIDDEN @endcond
3987 */
3988
3989/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003990 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003991 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003992 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003993 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003994 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003996 * @param name Name of the pipe.
3997 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3998 * or zero if no ring buffer is used.
3999 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004000 *
4001 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004002 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004003#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08004004 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07004005 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004006 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004007 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004008
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004009/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004010 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004011 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004012 * This routine initializes a pipe object, prior to its first use.
4013 *
4014 * @param pipe Address of the pipe.
4015 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
4016 * is used.
4017 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4018 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004019 *
4020 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004021 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004022 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004023void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
4024
4025/**
4026 * @brief Release a pipe's allocated buffer
4027 *
4028 * If a pipe object was given a dynamically allocated buffer via
4029 * k_pipe_alloc_init(), this will free it. This function does nothing
4030 * if the buffer wasn't dynamically allocated.
4031 *
4032 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004033 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004034 */
4035void k_pipe_cleanup(struct k_pipe *pipe);
4036
4037/**
4038 * @brief Initialize a pipe and allocate a buffer for it
4039 *
4040 * Storage for the buffer region will be allocated from the calling thread's
4041 * resource pool. This memory will be released if k_pipe_cleanup() is called,
4042 * or userspace is enabled and the pipe object loses all references to it.
4043 *
4044 * This function should only be called on uninitialized pipe objects.
4045 *
4046 * @param pipe Address of the pipe.
4047 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4048 * buffer is used.
4049 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004050 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004051 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004052 */
4053__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004054
4055/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004056 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004058 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004059 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004060 * @param pipe Address of the pipe.
4061 * @param data Address of data to write.
4062 * @param bytes_to_write Size of data (in bytes).
4063 * @param bytes_written Address of area to hold the number of bytes written.
4064 * @param min_xfer Minimum number of bytes to write.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004065 * @param timeout Non-negative waiting period to wait for the data to be written
4066 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004067 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004068 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004069 * @retval 0 At least @a min_xfer bytes of data were written.
4070 * @retval -EIO Returned without waiting; zero data bytes were written.
4071 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004072 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004073 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004074 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004075__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
4076 size_t bytes_to_write, size_t *bytes_written,
4077 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004078
4079/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004080 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004081 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004082 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004083 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004084 * @param pipe Address of the pipe.
4085 * @param data Address to place the data read from pipe.
4086 * @param bytes_to_read Maximum number of data bytes to read.
4087 * @param bytes_read Address of area to hold the number of bytes read.
4088 * @param min_xfer Minimum number of data bytes to read.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004089 * @param timeout Non-negative waiting period to wait for the data to be read
4090 * (in milliseconds), or one of the special values K_NO_WAIT
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004091 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004092 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004093 * @retval 0 At least @a min_xfer bytes of data were read.
4094 * @retval -EIO Returned without waiting; zero data bytes were read.
4095 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004096 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004097 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004098 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004099__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
4100 size_t bytes_to_read, size_t *bytes_read,
4101 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004102
4103/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004104 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004105 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004106 * This routine writes the data contained in a memory block to @a pipe.
4107 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004108 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004110 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004111 * @param block Memory block containing data to send
4112 * @param size Number of data bytes in memory block to send
4113 * @param sem Semaphore to signal upon completion (else NULL)
4114 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004115 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004116 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004117 */
4118extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
4119 size_t size, struct k_sem *sem);
4120
Anas Nashif166f5192018-02-25 08:02:36 -06004121/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004122
Allan Stephensc98da842016-11-11 15:45:03 -05004123/**
4124 * @cond INTERNAL_HIDDEN
4125 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004126
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004127struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004128 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05004129 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04004130 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004131 char *buffer;
4132 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05004133 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004134
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004135 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004136};
4137
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004138#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004139 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004140 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004141 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004142 .num_blocks = slab_num_blocks, \
4143 .block_size = slab_block_size, \
4144 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004145 .free_list = NULL, \
4146 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05004147 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004148 }
4149
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05004150#define K_MEM_SLAB_INITIALIZER __DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004151
4152
Peter Mitsis578f9112016-10-07 13:50:31 -04004153/**
Allan Stephensc98da842016-11-11 15:45:03 -05004154 * INTERNAL_HIDDEN @endcond
4155 */
4156
4157/**
4158 * @defgroup mem_slab_apis Memory Slab APIs
4159 * @ingroup kernel_apis
4160 * @{
4161 */
4162
4163/**
Allan Stephensda827222016-11-09 14:23:58 -06004164 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04004165 *
Allan Stephensda827222016-11-09 14:23:58 -06004166 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004167 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06004168 * @a slab_align -byte boundary. To ensure that each memory block is similarly
4169 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004170 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04004171 *
Allan Stephensda827222016-11-09 14:23:58 -06004172 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004173 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004174 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004175 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004176 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004177 * @param name Name of the memory slab.
4178 * @param slab_block_size Size of each memory block (in bytes).
4179 * @param slab_num_blocks Number memory blocks.
4180 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004181 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04004182 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004183#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004184 char __noinit __aligned(WB_UP(slab_align)) \
4185 _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004186 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004187 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004188 WB_UP(slab_block_size), slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004189
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004190/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004191 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004192 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004193 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004194 *
Allan Stephensda827222016-11-09 14:23:58 -06004195 * The memory slab's buffer contains @a slab_num_blocks memory blocks
4196 * that are @a slab_block_size bytes long. The buffer must be aligned to an
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004197 * N-byte boundary matching a word boundary, where N is a power of 2
4198 * (i.e. 4 on 32-bit systems, 8, 16, ...).
Allan Stephensda827222016-11-09 14:23:58 -06004199 * To ensure that each memory block is similarly aligned to this boundary,
4200 * @a slab_block_size must also be a multiple of N.
4201 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004202 * @param slab Address of the memory slab.
4203 * @param buffer Pointer to buffer used for the memory blocks.
4204 * @param block_size Size of each memory block (in bytes).
4205 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004206 *
4207 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004208 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004209 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004210extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05004211 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004212
4213/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004214 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004215 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004216 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004218 * @param slab Address of the memory slab.
4219 * @param mem Pointer to block address area.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004220 * @param timeout Non-negative waiting period to wait for operation to complete
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004221 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4222 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004223 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004224 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004225 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004226 * @retval -ENOMEM Returned without waiting.
4227 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004228 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004229 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004230extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05004231 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004232
4233/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004234 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004236 * This routine releases a previously allocated memory block back to its
4237 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004239 * @param slab Address of the memory slab.
4240 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004241 *
4242 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004243 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004244 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004245extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004246
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004247/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004248 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004249 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004250 * This routine gets the number of memory blocks that are currently
4251 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004252 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004253 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004255 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004256 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004257 */
Kumar Galacc334c72017-04-21 10:55:34 -05004258static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004259{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004260 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004261}
4262
Peter Mitsisc001aa82016-10-13 13:53:37 -04004263/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004264 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004265 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004266 * This routine gets the number of memory blocks that are currently
4267 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004268 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004269 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004270 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004271 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004272 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004273 */
Kumar Galacc334c72017-04-21 10:55:34 -05004274static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004275{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004276 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004277}
4278
Anas Nashif166f5192018-02-25 08:02:36 -06004279/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004280
4281/**
4282 * @cond INTERNAL_HIDDEN
4283 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004284
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004285struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004286 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004287 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004288};
4289
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004290/**
Allan Stephensc98da842016-11-11 15:45:03 -05004291 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004292 */
4293
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004294/**
Allan Stephensc98da842016-11-11 15:45:03 -05004295 * @addtogroup mem_pool_apis
4296 * @{
4297 */
4298
4299/**
4300 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004301 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004302 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4303 * long. The memory pool allows blocks to be repeatedly partitioned into
4304 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004305 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004306 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004307 * If the pool is to be accessed outside the module where it is defined, it
4308 * can be declared via
4309 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004310 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004311 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004312 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004313 * @param minsz Size of the smallest blocks in the pool (in bytes).
4314 * @param maxsz Size of the largest blocks in the pool (in bytes).
4315 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004316 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004317 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004318 */
Andy Ross73cb9582017-05-09 10:42:39 -07004319#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004320 char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \
Andy Ross73cb9582017-05-09 10:42:39 -07004321 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004322 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004323 Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004324 .base = { \
4325 .buf = _mpool_buf_##name, \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004326 .max_sz = WB_UP(maxsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004327 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004328 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004329 .levels = _mpool_lvls_##name, \
4330 .flags = SYS_MEM_POOL_KERNEL \
4331 } \
Johann Fischer223a2b92019-07-04 15:55:20 +02004332 }; \
Nicolas Pitreb2a022b2019-09-26 16:36:40 -04004333 BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004334
Peter Mitsis937042c2016-10-13 13:18:26 -04004335/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004336 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004337 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004338 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004339 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004340 * @param pool Address of the memory pool.
4341 * @param block Pointer to block descriptor for the allocated memory.
4342 * @param size Amount of memory to allocate (in bytes).
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004343 * @param timeout Non-negative waiting period to wait for operation to complete
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004344 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4345 * or K_FOREVER to wait as long as necessary.
4346 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004347 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004348 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004349 * @retval -ENOMEM Returned without waiting.
4350 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004351 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004352 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004353extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004354 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004355
4356/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004357 * @brief Allocate memory from a memory pool with malloc() semantics
4358 *
4359 * Such memory must be released using k_free().
4360 *
4361 * @param pool Address of the memory pool.
4362 * @param size Amount of memory to allocate (in bytes).
4363 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004364 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004365 */
4366extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4367
4368/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004369 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004370 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004371 * This routine releases a previously allocated memory block back to its
4372 * memory pool.
4373 *
4374 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004375 *
4376 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004377 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004378 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004379extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004380
4381/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004382 * @brief Free memory allocated from a memory pool.
4383 *
4384 * This routine releases a previously allocated memory block back to its
4385 * memory pool
4386 *
4387 * @param id Memory block identifier.
4388 *
4389 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004390 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004391 */
4392extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4393
4394/**
Anas Nashif166f5192018-02-25 08:02:36 -06004395 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004396 */
4397
4398/**
4399 * @defgroup heap_apis Heap Memory Pool APIs
4400 * @ingroup kernel_apis
4401 * @{
4402 */
4403
4404/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004405 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004407 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004408 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004409 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004410 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004411 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004412 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004413 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004414 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004415extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004416
4417/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004418 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004419 *
4420 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004421 * returned must have been allocated from the heap memory pool or
4422 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004423 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004424 * If @a ptr is NULL, no operation is performed.
4425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004426 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004427 *
4428 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004429 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004430 */
4431extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004432
Allan Stephensc98da842016-11-11 15:45:03 -05004433/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004434 * @brief Allocate memory from heap, array style
4435 *
4436 * This routine provides traditional calloc() semantics. Memory is
4437 * allocated from the heap memory pool and zeroed.
4438 *
4439 * @param nmemb Number of elements in the requested array
4440 * @param size Size of each array element (in bytes).
4441 *
4442 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004443 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004444 */
4445extern void *k_calloc(size_t nmemb, size_t size);
4446
Anas Nashif166f5192018-02-25 08:02:36 -06004447/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004448
Benjamin Walshacc68c12017-01-29 18:57:45 -05004449/* polling API - PRIVATE */
4450
Benjamin Walshb0179862017-02-02 16:39:57 -05004451#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004452#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004453#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004454#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004455#endif
4456
Benjamin Walshacc68c12017-01-29 18:57:45 -05004457/* private - types bit positions */
4458enum _poll_types_bits {
4459 /* can be used to ignore an event */
4460 _POLL_TYPE_IGNORE,
4461
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004462 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004463 _POLL_TYPE_SIGNAL,
4464
4465 /* semaphore availability */
4466 _POLL_TYPE_SEM_AVAILABLE,
4467
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004468 /* queue/fifo/lifo data availability */
4469 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004470
4471 _POLL_NUM_TYPES
4472};
4473
Patrik Flykt4344e272019-03-08 14:19:05 -07004474#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004475
4476/* private - states bit positions */
4477enum _poll_states_bits {
4478 /* default state when creating event */
4479 _POLL_STATE_NOT_READY,
4480
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004481 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004482 _POLL_STATE_SIGNALED,
4483
4484 /* semaphore is available */
4485 _POLL_STATE_SEM_AVAILABLE,
4486
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004487 /* data is available to read on queue/fifo/lifo */
4488 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004489
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004490 /* queue/fifo/lifo wait was cancelled */
4491 _POLL_STATE_CANCELLED,
4492
Benjamin Walshacc68c12017-01-29 18:57:45 -05004493 _POLL_NUM_STATES
4494};
4495
Patrik Flykt4344e272019-03-08 14:19:05 -07004496#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004497
4498#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004499 (32 - (0 \
4500 + 8 /* tag */ \
4501 + _POLL_NUM_TYPES \
4502 + _POLL_NUM_STATES \
4503 + 1 /* modes */ \
4504 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004505
Benjamin Walshacc68c12017-01-29 18:57:45 -05004506/* end of polling API - PRIVATE */
4507
4508
4509/**
4510 * @defgroup poll_apis Async polling APIs
4511 * @ingroup kernel_apis
4512 * @{
4513 */
4514
4515/* Public polling API */
4516
4517/* public - values for k_poll_event.type bitfield */
4518#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004519#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4520#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4521#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004522#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004523
4524/* public - polling modes */
4525enum k_poll_modes {
4526 /* polling thread does not take ownership of objects when available */
4527 K_POLL_MODE_NOTIFY_ONLY = 0,
4528
4529 K_POLL_NUM_MODES
4530};
4531
4532/* public - values for k_poll_event.state bitfield */
4533#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004534#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4535#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4536#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004537#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004538#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004539
4540/* public - poll signal object */
4541struct k_poll_signal {
4542 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004543 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004544
4545 /*
4546 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4547 * user resets it to 0.
4548 */
4549 unsigned int signaled;
4550
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004551 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004552 int result;
4553};
4554
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004555#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004556 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004557 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004558 .signaled = 0, \
4559 .result = 0, \
4560 }
4561
4562struct k_poll_event {
4563 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004564 sys_dnode_t _node;
4565
4566 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004567 struct _poller *poller;
4568
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004569 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004570 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004571
Benjamin Walshacc68c12017-01-29 18:57:45 -05004572 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004573 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004574
4575 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004576 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004577
4578 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004579 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004580
4581 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004582 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004583
4584 /* per-type data */
4585 union {
4586 void *obj;
4587 struct k_poll_signal *signal;
4588 struct k_sem *sem;
4589 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004590 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004591 };
4592};
4593
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004594#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004595 { \
4596 .poller = NULL, \
4597 .type = event_type, \
4598 .state = K_POLL_STATE_NOT_READY, \
4599 .mode = event_mode, \
4600 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004601 { .obj = event_obj }, \
4602 }
4603
4604#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4605 event_tag) \
4606 { \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004607 .tag = event_tag, \
Markus Fuchsbe21d3f2019-10-09 21:31:25 +02004608 .type = event_type, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004609 .state = K_POLL_STATE_NOT_READY, \
4610 .mode = event_mode, \
4611 .unused = 0, \
4612 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004613 }
4614
4615/**
4616 * @brief Initialize one struct k_poll_event instance
4617 *
4618 * After this routine is called on a poll event, the event it ready to be
4619 * placed in an event array to be passed to k_poll().
4620 *
4621 * @param event The event to initialize.
4622 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4623 * values. Only values that apply to the same object being polled
4624 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4625 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004626 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004627 * @param obj Kernel object or poll signal.
4628 *
4629 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004630 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004631 */
4632
Kumar Galacc334c72017-04-21 10:55:34 -05004633extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004634 int mode, void *obj);
4635
4636/**
4637 * @brief Wait for one or many of multiple poll events to occur
4638 *
4639 * This routine allows a thread to wait concurrently for one or many of
4640 * multiple poll events to have occurred. Such events can be a kernel object
4641 * being available, like a semaphore, or a poll signal event.
4642 *
4643 * When an event notifies that a kernel object is available, the kernel object
4644 * is not "given" to the thread calling k_poll(): it merely signals the fact
4645 * that the object was available when the k_poll() call was in effect. Also,
4646 * all threads trying to acquire an object the regular way, i.e. by pending on
4647 * the object, have precedence over the thread polling on the object. This
4648 * means that the polling thread will never get the poll event on an object
4649 * until the object becomes available and its pend queue is empty. For this
4650 * reason, the k_poll() call is more effective when the objects being polled
4651 * only have one thread, the polling thread, trying to acquire them.
4652 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004653 * When k_poll() returns 0, the caller should loop on all the events that were
4654 * passed to k_poll() and check the state field for the values that were
4655 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004656 *
4657 * Before being reused for another call to k_poll(), the user has to reset the
4658 * state field to K_POLL_STATE_NOT_READY.
4659 *
Andrew Boie3772f772018-05-07 16:52:57 -07004660 * When called from user mode, a temporary memory allocation is required from
4661 * the caller's resource pool.
4662 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004663 * @param events An array of pointers to events to be polled for.
4664 * @param num_events The number of events in the array.
Krzysztof Chruscinski94f742e2019-11-07 19:28:00 +01004665 * @param timeout Non-negative waiting period for an event to be ready (in
4666 * milliseconds), or one of the special values K_NO_WAIT and
4667 * K_FOREVER.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004668 *
4669 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004670 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004671 * @retval -EINTR Polling has been interrupted, e.g. with
4672 * k_queue_cancel_wait(). All output events are still set and valid,
4673 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4674 * words, -EINTR status means that at least one of output events is
4675 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004676 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4677 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004678 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004679 */
4680
Andrew Boie3772f772018-05-07 16:52:57 -07004681__syscall int k_poll(struct k_poll_event *events, int num_events,
4682 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004683
4684/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004685 * @brief Initialize a poll signal object.
4686 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004687 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004688 *
4689 * @param signal A poll signal.
4690 *
4691 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004692 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004693 */
4694
Andrew Boie3772f772018-05-07 16:52:57 -07004695__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4696
4697/*
4698 * @brief Reset a poll signal object's state to unsignaled.
4699 *
4700 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004701 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004702 */
4703__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4704
Patrik Flykt4344e272019-03-08 14:19:05 -07004705static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004706{
Patrik Flykt24d71432019-03-26 19:57:45 -06004707 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004708}
4709
4710/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004711 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004712 *
4713 * @param signal A poll signal object
4714 * @param signaled An integer buffer which will be written nonzero if the
4715 * object was signaled
4716 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004717 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004718 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004719 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004720 */
4721__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4722 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004723
4724/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004725 * @brief Signal a poll signal object.
4726 *
4727 * This routine makes ready a poll signal, which is basically a poll event of
4728 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4729 * made ready to run. A @a result value can be specified.
4730 *
4731 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004732 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004733 * k_poll_signal_reset(). It thus has to be reset by the user before being
4734 * passed again to k_poll() or k_poll() will consider it being signaled, and
4735 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004736 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004737 * @note The result is stored and the 'signaled' field is set even if
4738 * this function returns an error indicating that an expiring poll was
4739 * not notified. The next k_poll() will detect the missed raise.
4740 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004741 * @param signal A poll signal.
4742 * @param result The value to store in the result field of the signal.
4743 *
4744 * @retval 0 The signal was delivered successfully.
4745 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004746 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004747 */
4748
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004749__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004750
Anas Nashif954d5502018-02-25 08:37:28 -06004751/**
4752 * @internal
4753 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004754extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004755
Anas Nashif166f5192018-02-25 08:02:36 -06004756/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004757
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004758/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004759 * @defgroup cpu_idle_apis CPU Idling APIs
4760 * @ingroup kernel_apis
4761 * @{
4762 */
Anas Nashif30c3cff2019-01-22 08:18:13 -05004763/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004764 * @brief Make the CPU idle.
4765 *
4766 * This function makes the CPU idle until an event wakes it up.
4767 *
4768 * In a regular system, the idle thread should be the only thread responsible
4769 * for making the CPU idle and triggering any type of power management.
4770 * However, in some more constrained systems, such as a single-threaded system,
4771 * the only thread would be responsible for this if needed.
4772 *
4773 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004774 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004775 */
Andrew Boie07525a32019-09-21 16:17:23 -07004776static inline void k_cpu_idle(void)
4777{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004778 arch_cpu_idle();
Andrew Boie07525a32019-09-21 16:17:23 -07004779}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004780
4781/**
4782 * @brief Make the CPU idle in an atomic fashion.
4783 *
4784 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4785 * must be done atomically before making the CPU idle.
4786 *
4787 * @param key Interrupt locking key obtained from irq_lock().
4788 *
4789 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004790 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004791 */
Andrew Boie07525a32019-09-21 16:17:23 -07004792static inline void k_cpu_atomic_idle(unsigned int key)
4793{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004794 arch_cpu_atomic_idle(key);
Andrew Boie07525a32019-09-21 16:17:23 -07004795}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004796
Anas Nashif30c3cff2019-01-22 08:18:13 -05004797/**
4798 * @}
4799 */
Anas Nashif954d5502018-02-25 08:37:28 -06004800
4801/**
4802 * @internal
4803 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004804extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004805
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004806#ifdef ARCH_EXCEPT
Ioannis Glaropoulosdf029232019-10-07 11:24:36 +02004807/* This architecture has direct support for triggering a CPU exception */
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004808#define z_except_reason(reason) ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004809#else
4810
Andrew Boiecdb94d62017-04-18 15:22:05 -07004811/* NOTE: This is the implementation for arches that do not implement
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004812 * ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004813 *
4814 * We won't have a real exception frame to determine the PC value when
4815 * the oops occurred, so print file and line number before we jump into
4816 * the fatal error handler.
4817 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004818#define z_except_reason(reason) do { \
Andrew Boiecdb94d62017-04-18 15:22:05 -07004819 printk("@ %s:%d:\n", __FILE__, __LINE__); \
Andrew Boie56236372019-07-15 15:22:29 -07004820 z_fatal_error(reason, NULL); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004821 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004822
4823#endif /* _ARCH__EXCEPT */
4824
4825/**
4826 * @brief Fatally terminate a thread
4827 *
4828 * This should be called when a thread has encountered an unrecoverable
4829 * runtime condition and needs to terminate. What this ultimately
4830 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004831 * will be called will reason code K_ERR_KERNEL_OOPS.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004832 *
4833 * If this is called from ISR context, the default system fatal error handler
4834 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004835 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004836 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004837#define k_oops() z_except_reason(K_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004838
4839/**
4840 * @brief Fatally terminate the system
4841 *
4842 * This should be called when the Zephyr kernel has encountered an
4843 * unrecoverable runtime condition and needs to terminate. What this ultimately
4844 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004845 * will be called will reason code K_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004846 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004847 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004848#define k_panic() z_except_reason(K_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004849
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004850/*
4851 * private APIs that are utilized by one or more public APIs
4852 */
4853
Stephanos Ioannidis2d746042019-10-25 00:08:21 +09004854/**
4855 * @internal
4856 */
4857extern void z_init_thread_base(struct _thread_base *thread_base,
4858 int priority, u32_t initial_state,
4859 unsigned int options);
4860
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004861#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004862/**
4863 * @internal
4864 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004865extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004866#else
Anas Nashif954d5502018-02-25 08:37:28 -06004867/**
4868 * @internal
4869 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004870#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004871#endif
4872
Anas Nashif954d5502018-02-25 08:37:28 -06004873/**
4874 * @internal
4875 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004876extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004877/**
4878 * @internal
4879 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004880extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004881
Andrew Boiedc5d9352017-06-02 12:56:47 -07004882/* arch/cpu.h may declare an architecture or platform-specific macro
4883 * for properly declaring stacks, compatible with MMU/MPU constraints if
4884 * enabled
4885 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004886
4887/**
4888 * @brief Obtain an extern reference to a stack
4889 *
4890 * This macro properly brings the symbol of a thread stack declared
4891 * elsewhere into scope.
4892 *
4893 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004894 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004895 */
4896#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4897
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004898#ifdef ARCH_THREAD_STACK_DEFINE
4899#define K_THREAD_STACK_DEFINE(sym, size) ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004900#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004901 ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4902#define K_THREAD_STACK_LEN(size) ARCH_THREAD_STACK_LEN(size)
4903#define K_THREAD_STACK_MEMBER(sym, size) ARCH_THREAD_STACK_MEMBER(sym, size)
4904#define K_THREAD_STACK_SIZEOF(sym) ARCH_THREAD_STACK_SIZEOF(sym)
4905#define K_THREAD_STACK_RESERVED ARCH_THREAD_STACK_RESERVED
Andrew Boie4e5c0932019-04-04 12:05:28 -07004906static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004907{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004908 return ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07004909}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004910#else
4911/**
4912 * @brief Declare a toplevel thread stack memory region
4913 *
4914 * This declares a region of memory suitable for use as a thread's stack.
4915 *
4916 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4917 * 'noinit' section so that it isn't zeroed at boot
4918 *
Andrew Boie507852a2017-07-25 18:47:07 -07004919 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004920 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07004921 * inside needs to be examined, examine thread->stack_info for the associated
4922 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004923 *
4924 * It is legal to precede this definition with the 'static' keyword.
4925 *
4926 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4927 * parameter of k_thread_create(), it may not be the same as the
4928 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4929 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004930 * Some arches may round the size of the usable stack region up to satisfy
4931 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4932 * size.
4933 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004934 * @param sym Thread stack symbol name
4935 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004936 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004937 */
4938#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004939 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004940
4941/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304942 * @brief Calculate size of stacks to be allocated in a stack array
4943 *
4944 * This macro calculates the size to be allocated for the stacks
4945 * inside a stack array. It accepts the indicated "size" as a parameter
4946 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4947 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4948 *
4949 * @param size Size of the stack memory region
4950 * @req K-TSTACK-001
4951 */
4952#define K_THREAD_STACK_LEN(size) (size)
4953
4954/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004955 * @brief Declare a toplevel array of thread stack memory regions
4956 *
4957 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4958 * definition for additional details and constraints.
4959 *
4960 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4961 * 'noinit' section so that it isn't zeroed at boot
4962 *
4963 * @param sym Thread stack symbol name
4964 * @param nmemb Number of stacks to declare
4965 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004966 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004967 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004968#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004969 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304970 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004971
4972/**
4973 * @brief Declare an embedded stack memory region
4974 *
4975 * Used for stacks embedded within other data structures. Use is highly
4976 * discouraged but in some cases necessary. For memory protection scenarios,
4977 * it is very important that any RAM preceding this member not be writable
4978 * by threads else a stack overflow will lead to silent corruption. In other
4979 * words, the containing data structure should live in RAM owned by the kernel.
4980 *
4981 * @param sym Thread stack symbol name
4982 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004983 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004984 */
4985#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004986 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004987
4988/**
4989 * @brief Return the size in bytes of a stack memory region
4990 *
4991 * Convenience macro for passing the desired stack size to k_thread_create()
4992 * since the underlying implementation may actually create something larger
4993 * (for instance a guard area).
4994 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004995 * The value returned here is not guaranteed to match the 'size' parameter
4996 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004997 *
4998 * @param sym Stack memory symbol
4999 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005000 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005001 */
5002#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
5003
Andrew Boie575abc02019-03-19 10:42:24 -07005004
5005/**
5006 * @brief Indicate how much additional memory is reserved for stack objects
5007 *
5008 * Any given stack declaration may have additional memory in it for guard
5009 * areas or supervisor mode stacks. This macro indicates how much space
5010 * is reserved for this. The memory reserved may not be contiguous within
5011 * the stack object, and does not account for additional space used due to
5012 * enforce alignment.
5013 */
5014#define K_THREAD_STACK_RESERVED 0
5015
Andrew Boiedc5d9352017-06-02 12:56:47 -07005016/**
5017 * @brief Get a pointer to the physical stack buffer
5018 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07005019 * This macro is deprecated. If a stack buffer needs to be examined, the
5020 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005021 *
5022 * @param sym Declared stack symbol name
5023 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005024 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005025 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07005026static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07005027{
5028 return (char *)sym;
5029}
Andrew Boiedc5d9352017-06-02 12:56:47 -07005030
5031#endif /* _ARCH_DECLARE_STACK */
5032
Chunlin Hane9c97022017-07-07 20:29:30 +08005033/**
5034 * @defgroup mem_domain_apis Memory domain APIs
5035 * @ingroup kernel_apis
5036 * @{
5037 */
5038
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005039/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005040 * @def K_MEM_PARTITION_DEFINE
5041 * @brief Used to declare a memory partition
5042 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005043 */
5044#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
5045#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
5046 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08005047 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005048 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005049#else
5050#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08005051 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005052 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005053#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
5054
Chunlin Hane9c97022017-07-07 20:29:30 +08005055/* memory partition */
5056struct k_mem_partition {
5057 /* start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005058 uintptr_t start;
Chunlin Hane9c97022017-07-07 20:29:30 +08005059 /* size of memory partition */
5060 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005061#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08005062 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305063 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005064#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08005065};
5066
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02005067/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05305068 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005069struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305070#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08005071 /* partitions in the domain */
5072 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305073#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08005074 /* domain q */
5075 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08005076 /* number of partitions in the domain */
5077 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08005078};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305079
Chunlin Hane9c97022017-07-07 20:29:30 +08005080
5081/**
5082 * @brief Initialize a memory domain.
5083 *
5084 * Initialize a memory domain with given name and memory partitions.
5085 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005086 * See documentation for k_mem_domain_add_partition() for details about
5087 * partition constraints.
5088 *
Chunlin Hane9c97022017-07-07 20:29:30 +08005089 * @param domain The memory domain to be initialized.
5090 * @param num_parts The number of array items of "parts" parameter.
5091 * @param parts An array of pointers to the memory partitions. Can be NULL
5092 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005093 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005094 */
Leandro Pereira08de6582018-02-28 14:22:57 -08005095extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08005096 struct k_mem_partition *parts[]);
5097/**
5098 * @brief Destroy a memory domain.
5099 *
5100 * Destroy a memory domain.
5101 *
5102 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005103 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005104 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005105extern void k_mem_domain_destroy(struct k_mem_domain *domain);
5106
5107/**
5108 * @brief Add a memory partition into a memory domain.
5109 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005110 * Add a memory partition into a memory domain. Partitions must conform to
5111 * the following constraints:
5112 *
5113 * - Partition bounds must be within system RAM boundaries on MMU-based
5114 * systems.
5115 * - Partitions in the same memory domain may not overlap each other.
5116 * - Partitions must not be defined which expose private kernel
5117 * data structures or kernel objects.
5118 * - The starting address alignment, and the partition size must conform to
5119 * the constraints of the underlying memory management hardware, which
5120 * varies per architecture.
5121 * - Memory domain partitions are only intended to control access to memory
5122 * from user mode threads.
5123 *
5124 * Violating these constraints may lead to CPU exceptions or undefined
5125 * behavior.
Chunlin Hane9c97022017-07-07 20:29:30 +08005126 *
5127 * @param domain The memory domain to be added a memory partition.
5128 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005129 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005130 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005131extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
5132 struct k_mem_partition *part);
5133
5134/**
5135 * @brief Remove a memory partition from a memory domain.
5136 *
5137 * Remove a memory partition from a memory domain.
5138 *
5139 * @param domain The memory domain to be removed a memory partition.
5140 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005141 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005142 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005143extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
5144 struct k_mem_partition *part);
5145
5146/**
5147 * @brief Add a thread into a memory domain.
5148 *
5149 * Add a thread into a memory domain.
5150 *
5151 * @param domain The memory domain that the thread is going to be added into.
5152 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005153 *
5154 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005155 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005156extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
5157 k_tid_t thread);
5158
5159/**
5160 * @brief Remove a thread from its memory domain.
5161 *
5162 * Remove a thread from its memory domain.
5163 *
5164 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005165 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005166 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005167extern void k_mem_domain_remove_thread(k_tid_t thread);
5168
Anas Nashif166f5192018-02-25 08:02:36 -06005169/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08005170
Andrew Boie756f9072017-10-10 16:01:49 -07005171/**
5172 * @brief Emit a character buffer to the console device
5173 *
5174 * @param c String of characters to print
5175 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005176 *
5177 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07005178 */
5179__syscall void k_str_out(char *c, size_t n);
5180
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005181/**
5182 * @brief Disable preservation of floating point context information.
5183 *
5184 * This routine informs the kernel that the specified thread
5185 * will no longer be using the floating point registers.
5186 *
5187 * @warning
5188 * Some architectures apply restrictions on how the disabling of floating
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005189 * point preservation may be requested, see arch_float_disable.
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005190 *
5191 * @warning
5192 * This routine should only be used to disable floating point support for
5193 * a thread that currently has such support enabled.
5194 *
5195 * @param thread ID of thread.
5196 *
5197 * @retval 0 On success.
5198 * @retval -ENOSYS If the floating point disabling is not implemented.
5199 * -EINVAL If the floating point disabling could not be performed.
5200 */
5201__syscall int k_float_disable(struct k_thread *thread);
5202
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005203#ifdef __cplusplus
5204}
5205#endif
5206
Anas Nashif10291a02019-06-25 12:25:12 -04005207#include <debug/tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07005208#include <syscalls/kernel.h>
5209
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05005210#endif /* !_ASMLANGUAGE */
5211
Flavio Ceolin67ca1762018-09-14 10:43:44 -07005212#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */