blob: d2258306e76a6fe9bb012823b2f2da6c56fbecc5 [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);
1640 return (ticks > 0) ? (u32_t)__ticks_to_ms(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.
2023 * @param timeout Waiting period to obtain a data item (in milliseconds),
2024 * or one of the special values K_NO_WAIT and K_FOREVER.
2025 *
2026 * @return Address of the data item if successful; NULL if returned
2027 * without waiting, or waiting period timed out.
2028 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002029__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002030
2031/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002032 * @brief Remove an element from a queue.
2033 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002034 * This routine removes data item from @a queue. The first word of the
2035 * data item is reserved for the kernel's use. Removing elements from k_queue
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002036 * rely on sys_slist_find_and_remove which is not a constant time operation.
2037 *
2038 * @note Can be called by ISRs
2039 *
2040 * @param queue Address of the queue.
2041 * @param data Address of the data item.
2042 *
2043 * @return true if data item was removed
2044 */
2045static inline bool k_queue_remove(struct k_queue *queue, void *data)
2046{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002047 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002048}
2049
2050/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002051 * @brief Append an element to a queue only if it's not present already.
2052 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002053 * This routine appends data item to @a queue. The first word of the data
2054 * item is reserved for the kernel's use. Appending elements to k_queue
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002055 * relies on sys_slist_is_node_in_list which is not a constant time operation.
2056 *
2057 * @note Can be called by ISRs
2058 *
2059 * @param queue Address of the queue.
2060 * @param data Address of the data item.
2061 *
2062 * @return true if data item was added, false if not
2063 */
2064static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
2065{
2066 sys_sfnode_t *test;
2067
2068 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
2069 if (test == (sys_sfnode_t *) data) {
2070 return false;
2071 }
2072 }
2073
2074 k_queue_append(queue, data);
2075 return true;
2076}
2077
2078/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002079 * @brief Query a queue to see if it has data available.
2080 *
2081 * Note that the data might be already gone by the time this function returns
2082 * if other threads are also trying to read from the queue.
2083 *
2084 * @note Can be called by ISRs.
2085 *
2086 * @param queue Address of the queue.
2087 *
2088 * @return Non-zero if the queue is empty.
2089 * @return 0 if data is available.
2090 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002091__syscall int k_queue_is_empty(struct k_queue *queue);
2092
Patrik Flykt4344e272019-03-08 14:19:05 -07002093static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002094{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002095 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002096}
2097
2098/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002099 * @brief Peek element at the head of queue.
2100 *
2101 * Return element from the head of queue without removing it.
2102 *
2103 * @param queue Address of the queue.
2104 *
2105 * @return Head element, or NULL if queue is empty.
2106 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002107__syscall void *k_queue_peek_head(struct k_queue *queue);
2108
Patrik Flykt4344e272019-03-08 14:19:05 -07002109static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002110{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002111 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002112}
2113
2114/**
2115 * @brief Peek element at the tail of queue.
2116 *
2117 * Return element from the tail of queue without removing it.
2118 *
2119 * @param queue Address of the queue.
2120 *
2121 * @return Tail element, or NULL if queue is empty.
2122 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002123__syscall void *k_queue_peek_tail(struct k_queue *queue);
2124
Patrik Flykt4344e272019-03-08 14:19:05 -07002125static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002126{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002127 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002128}
2129
2130/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002131 * @brief Statically define and initialize a queue.
2132 *
2133 * The queue can be accessed outside the module where it is defined using:
2134 *
2135 * @code extern struct k_queue <name>; @endcode
2136 *
2137 * @param name Name of the queue.
2138 */
2139#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002140 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002141 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002142
Anas Nashif166f5192018-02-25 08:02:36 -06002143/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002144
Wentong Wu5611e922019-06-20 23:51:27 +08002145#ifdef CONFIG_USERSPACE
2146/**
2147 * @brief futex structure
2148 *
2149 * A k_futex is a lightweight mutual exclusion primitive designed
2150 * to minimize kernel involvement. Uncontended operation relies
2151 * only on atomic access to shared memory. k_futex are tracked as
2152 * kernel objects and can live in user memory so any access bypass
2153 * the kernel object permission management mechanism.
2154 */
2155struct k_futex {
2156 atomic_t val;
2157};
2158
2159/**
2160 * @brief futex kernel data structure
2161 *
2162 * z_futex_data are the helper data structure for k_futex to complete
2163 * futex contended operation on kernel side, structure z_futex_data
2164 * of every futex object is invisible in user mode.
2165 */
2166struct z_futex_data {
2167 _wait_q_t wait_q;
2168 struct k_spinlock lock;
2169};
2170
2171#define Z_FUTEX_DATA_INITIALIZER(obj) \
2172 { \
2173 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \
2174 }
2175
2176/**
2177 * @defgroup futex_apis FUTEX APIs
2178 * @ingroup kernel_apis
2179 * @{
2180 */
2181
2182/**
Wentong Wu5611e922019-06-20 23:51:27 +08002183 * @brief Pend the current thread on a futex
2184 *
2185 * Tests that the supplied futex contains the expected value, and if so,
2186 * goes to sleep until some other thread calls k_futex_wake() on it.
2187 *
2188 * @param futex Address of the futex.
2189 * @param expected Expected value of the futex, if it is different the caller
2190 * will not wait on it.
2191 * @param timeout Waiting period on the futex, in milliseconds, or one of the
2192 * special values K_NO_WAIT or K_FOREVER.
2193 * @retval -EACCES Caller does not have read access to futex address.
2194 * @retval -EAGAIN If the futex value did not match the expected parameter.
2195 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2196 * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup.
2197 * @retval 0 if the caller went to sleep and was woken up. The caller
2198 * should check the futex's value on wakeup to determine if it needs
2199 * to block again.
2200 */
2201__syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout);
2202
2203/**
2204 * @brief Wake one/all threads pending on a futex
2205 *
2206 * Wake up the highest priority thread pending on the supplied futex, or
2207 * wakeup all the threads pending on the supplied futex, and the behavior
2208 * depends on wake_all.
2209 *
2210 * @param futex Futex to wake up pending threads.
2211 * @param wake_all If true, wake up all pending threads; If false,
2212 * wakeup the highest priority thread.
2213 * @retval -EACCES Caller does not have access to the futex address.
2214 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2215 * @retval Number of threads that were woken up.
2216 */
2217__syscall int k_futex_wake(struct k_futex *futex, bool wake_all);
2218
2219/** @} */
2220#endif
2221
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002222struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002223 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002224};
2225
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002226/**
2227 * @cond INTERNAL_HIDDEN
2228 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002229#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002230 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002231 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002232 }
2233
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002234#define K_FIFO_INITIALIZER __DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002235
Allan Stephensc98da842016-11-11 15:45:03 -05002236/**
2237 * INTERNAL_HIDDEN @endcond
2238 */
2239
2240/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002241 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002242 * @ingroup kernel_apis
2243 * @{
2244 */
2245
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002246/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002247 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002248 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002249 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002250 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002251 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002252 *
2253 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002254 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002255 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002256#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002257 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002258
2259/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002260 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002261 *
2262 * This routine causes first thread pending on @a fifo, if any, to
2263 * return from k_fifo_get() call with NULL value (as if timeout
2264 * expired).
2265 *
2266 * @note Can be called by ISRs.
2267 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002268 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002269 *
2270 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002271 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002272 */
2273#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002274 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002275
2276/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002277 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002278 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002279 * This routine adds a data item to @a fifo. A FIFO data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002280 * aligned on a word boundary, and the first word of the item is reserved
2281 * for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002282 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002283 * @note Can be called by ISRs.
2284 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002285 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002286 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002287 *
2288 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002289 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002290 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002291#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002292 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002293
2294/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002295 * @brief Add an element to a FIFO queue.
2296 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002297 * This routine adds a data item to @a fifo. There is an implicit memory
2298 * allocation to create an additional temporary bookkeeping data structure from
2299 * the calling thread's resource pool, which is automatically freed when the
2300 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002301 *
2302 * @note Can be called by ISRs.
2303 *
2304 * @param fifo Address of the FIFO.
2305 * @param data Address of the data item.
2306 *
2307 * @retval 0 on success
2308 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002309 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002310 */
2311#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002312 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002313
2314/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002315 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002316 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002317 * This routine adds a list of data items to @a fifo in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002318 * The data items must be in a singly-linked list, with the first word of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002319 * each data item pointing to the next data item; the list must be
2320 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002321 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002322 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002323 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002324 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002325 * @param head Pointer to first node in singly-linked list.
2326 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002327 *
2328 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002329 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002330 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002331#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002332 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002333
2334/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002335 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002337 * This routine adds a list of data items to @a fifo in one operation.
2338 * The data items must be in a singly-linked list implemented using a
2339 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002340 * and must be re-initialized via sys_slist_init().
2341 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002342 * @note Can be called by ISRs.
2343 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002344 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002345 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002346 *
2347 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002348 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002349 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002350#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002351 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002352
2353/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002354 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002355 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002356 * This routine removes a data item from @a fifo in a "first in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002357 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002359 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2360 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002361 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002362 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002363 * or one of the special values K_NO_WAIT and K_FOREVER.
2364 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002365 * @return Address of the data item if successful; NULL if returned
2366 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002367 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002368 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002369#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002370 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002371
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002372/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002373 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002374 *
2375 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002376 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002377 *
2378 * @note Can be called by ISRs.
2379 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002380 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002381 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002382 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002383 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002384 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002385 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002386#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002387 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002388
2389/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002390 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002391 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002392 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302393 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002394 * on each iteration of processing, a head container will be peeked,
2395 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002396 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002397 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002398 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002399 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002400 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002401 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002402 */
2403#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002404 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002405
2406/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002407 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002408 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002409 * Return element from the tail of FIFO queue (without removing it). A usecase
2410 * for this is if elements of the FIFO queue are themselves containers. Then
2411 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002412 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002413 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002414 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002415 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002416 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002417 */
2418#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002419 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002420
2421/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002422 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002423 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002424 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002425 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002426 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002427 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002428 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002429 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002430 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002431#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002432 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002433 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002434
Anas Nashif166f5192018-02-25 08:02:36 -06002435/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002436
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002437struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002438 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002439};
2440
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002441/**
2442 * @cond INTERNAL_HIDDEN
2443 */
2444
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002445#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002446 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002447 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002448 }
2449
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002450#define K_LIFO_INITIALIZER __DEPRECATED_MACRO _K_LIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002451
Allan Stephensc98da842016-11-11 15:45:03 -05002452/**
2453 * INTERNAL_HIDDEN @endcond
2454 */
2455
2456/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002457 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002458 * @ingroup kernel_apis
2459 * @{
2460 */
2461
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002462/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002463 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002464 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002465 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002466 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002467 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002468 *
2469 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002470 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002471 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002472#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002473 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002474
2475/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002476 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002477 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002478 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002479 * aligned on a word boundary, and the first word of the item is
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002480 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002482 * @note Can be called by ISRs.
2483 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002484 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002485 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002486 *
2487 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002488 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002489 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002490#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002491 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002492
2493/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002494 * @brief Add an element to a LIFO queue.
2495 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002496 * This routine adds a data item to @a lifo. There is an implicit memory
2497 * allocation to create an additional temporary bookkeeping data structure from
2498 * the calling thread's resource pool, which is automatically freed when the
2499 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002500 *
2501 * @note Can be called by ISRs.
2502 *
2503 * @param lifo Address of the LIFO.
2504 * @param data Address of the data item.
2505 *
2506 * @retval 0 on success
2507 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002508 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002509 */
2510#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002511 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002512
2513/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002514 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002515 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002516 * This routine removes a data item from @a lifo in a "last in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002517 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002519 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2520 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002521 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002522 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002523 * or one of the special values K_NO_WAIT and K_FOREVER.
2524 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002525 * @return Address of the data item if successful; NULL if returned
2526 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002527 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002528 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002529#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002530 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002531
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002532/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002533 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002534 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002535 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002536 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002537 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002539 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002540 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002541 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002542#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002543 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002544 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002545
Anas Nashif166f5192018-02-25 08:02:36 -06002546/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002547
2548/**
2549 * @cond INTERNAL_HIDDEN
2550 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302551#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002552
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002553typedef uintptr_t stack_data_t;
2554
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002555struct k_stack {
2556 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002557 struct k_spinlock lock;
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002558 stack_data_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002559
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002560 _OBJECT_TRACING_NEXT_PTR(k_stack)
Andrew Boief3bee952018-05-02 17:44:39 -07002561 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002562};
2563
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002564#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002565 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002566 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002567 .base = stack_buffer, \
2568 .next = stack_buffer, \
2569 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002570 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002571 }
2572
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002573#define K_STACK_INITIALIZER __DEPRECATED_MACRO _K_STACK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002574
Allan Stephensc98da842016-11-11 15:45:03 -05002575/**
2576 * INTERNAL_HIDDEN @endcond
2577 */
2578
2579/**
2580 * @defgroup stack_apis Stack APIs
2581 * @ingroup kernel_apis
2582 * @{
2583 */
2584
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002585/**
2586 * @brief Initialize a stack.
2587 *
2588 * This routine initializes a stack object, prior to its first use.
2589 *
2590 * @param stack Address of the stack.
2591 * @param buffer Address of array used to hold stacked values.
2592 * @param num_entries Maximum number of values that can be stacked.
2593 *
2594 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002595 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002596 */
Andrew Boief3bee952018-05-02 17:44:39 -07002597void k_stack_init(struct k_stack *stack,
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002598 stack_data_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002599
2600
2601/**
2602 * @brief Initialize a stack.
2603 *
2604 * This routine initializes a stack object, prior to its first use. Internal
2605 * buffers will be allocated from the calling thread's resource pool.
2606 * This memory will be released if k_stack_cleanup() is called, or
2607 * userspace is enabled and the stack object loses all references to it.
2608 *
2609 * @param stack Address of the stack.
2610 * @param num_entries Maximum number of values that can be stacked.
2611 *
2612 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002613 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002614 */
2615
Adithya Baglody28080d32018-10-15 11:48:51 +05302616__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2617 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002618
2619/**
2620 * @brief Release a stack's allocated buffer
2621 *
2622 * If a stack object was given a dynamically allocated buffer via
2623 * k_stack_alloc_init(), this will free it. This function does nothing
2624 * if the buffer wasn't dynamically allocated.
2625 *
2626 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002627 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002628 */
2629void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002630
2631/**
2632 * @brief Push an element onto a stack.
2633 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002634 * This routine adds a stack_data_t value @a data to @a stack.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002635 *
2636 * @note Can be called by ISRs.
2637 *
2638 * @param stack Address of the stack.
2639 * @param data Value to push onto the stack.
2640 *
2641 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002642 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002643 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002644__syscall void k_stack_push(struct k_stack *stack, stack_data_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002645
2646/**
2647 * @brief Pop an element from a stack.
2648 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002649 * This routine removes a stack_data_t value from @a stack in a "last in,
2650 * first out" manner and stores the value in @a data.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002651 *
2652 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2653 *
2654 * @param stack Address of the stack.
2655 * @param data Address of area to hold the value popped from the stack.
2656 * @param timeout Waiting period to obtain a value (in milliseconds),
2657 * or one of the special values K_NO_WAIT and K_FOREVER.
2658 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002659 * @retval 0 Element popped from stack.
2660 * @retval -EBUSY Returned without waiting.
2661 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002662 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002663 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002664__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002665
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002666/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002667 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002668 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002669 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002670 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002671 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002672 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002673 * @param name Name of the stack.
2674 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002675 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002676 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002677#define K_STACK_DEFINE(name, stack_num_entries) \
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002678 stack_data_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002679 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002680 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002681 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002682 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002683
Anas Nashif166f5192018-02-25 08:02:36 -06002684/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002685
Allan Stephens6bba9b02016-11-16 14:56:54 -05002686struct k_work;
Piotr Zięcik19d83492019-09-27 09:16:25 +02002687struct k_work_poll;
Allan Stephens6bba9b02016-11-16 14:56:54 -05002688
Piotr Zięcik19d83492019-09-27 09:16:25 +02002689/* private, used by k_poll and k_work_poll */
Piotr Zięcik1c4177d2019-08-27 12:19:26 +02002690typedef int (*_poller_cb_t)(struct k_poll_event *event, u32_t state);
2691struct _poller {
2692 volatile bool is_polling;
2693 struct k_thread *thread;
2694 _poller_cb_t cb;
2695};
2696
Allan Stephensc98da842016-11-11 15:45:03 -05002697/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002698 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002699 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002700 */
2701
Allan Stephens6bba9b02016-11-16 14:56:54 -05002702/**
2703 * @typedef k_work_handler_t
2704 * @brief Work item handler function type.
2705 *
2706 * A work item's handler function is executed by a workqueue's thread
2707 * when the work item is processed by the workqueue.
2708 *
2709 * @param work Address of the work item.
2710 *
2711 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002712 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002713 */
2714typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002715
2716/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002717 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002718 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002719
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002720struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002721 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002722 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002723};
2724
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002725enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002726 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002727};
2728
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002729struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002730 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002731 k_work_handler_t handler;
2732 atomic_t flags[1];
2733};
2734
Allan Stephens6bba9b02016-11-16 14:56:54 -05002735struct k_delayed_work {
2736 struct k_work work;
2737 struct _timeout timeout;
2738 struct k_work_q *work_q;
2739};
2740
Piotr Zięcik19d83492019-09-27 09:16:25 +02002741struct k_work_poll {
2742 struct k_work work;
2743 struct _poller poller;
2744 struct k_poll_event *events;
2745 int num_events;
2746 k_work_handler_t real_handler;
2747 struct _timeout timeout;
2748 int poll_result;
2749};
2750
Allan Stephens6bba9b02016-11-16 14:56:54 -05002751extern struct k_work_q k_sys_work_q;
2752
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002753/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002754 * INTERNAL_HIDDEN @endcond
2755 */
2756
Patrik Flykt4344e272019-03-08 14:19:05 -07002757#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002758 { \
2759 ._reserved = NULL, \
2760 .handler = work_handler, \
2761 .flags = { 0 } \
2762 }
2763
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05002764#define K_WORK_INITIALIZER __DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002765
Allan Stephens6bba9b02016-11-16 14:56:54 -05002766/**
2767 * @brief Initialize a statically-defined work item.
2768 *
2769 * This macro can be used to initialize a statically-defined workqueue work
2770 * item, prior to its first use. For example,
2771 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002772 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002773 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002774 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002775 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002776 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002777 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002778#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002779 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002780
2781/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002782 * @brief Initialize a work item.
2783 *
2784 * This routine initializes a workqueue work item, prior to its first use.
2785 *
2786 * @param work Address of work item.
2787 * @param handler Function to invoke each time work item is processed.
2788 *
2789 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002790 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002791 */
2792static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2793{
Patrik Flykt4344e272019-03-08 14:19:05 -07002794 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002795}
2796
2797/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002798 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002799 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002800 * This routine submits work item @a work to be processed by workqueue
2801 * @a work_q. If the work item is already pending in the workqueue's queue
2802 * as a result of an earlier submission, this routine has no effect on the
2803 * work item. If the work item has already been processed, or is currently
2804 * being processed, its work is considered complete and the work item can be
2805 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002806 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002807 * @warning
2808 * A submitted work item must not be modified until it has been processed
2809 * by the workqueue.
2810 *
2811 * @note Can be called by ISRs.
2812 *
2813 * @param work_q Address of workqueue.
2814 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002815 *
2816 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002817 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002818 */
2819static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2820 struct k_work *work)
2821{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002822 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002823 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002824 }
2825}
2826
2827/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002828 * @brief Submit a work item to a user mode workqueue
2829 *
David B. Kinder06d78352018-12-17 14:32:40 -08002830 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002831 * memory allocation is made from the caller's resource pool which is freed
2832 * once the worker thread consumes the k_work item. The workqueue
2833 * thread must have memory access to the k_work item being submitted. The caller
2834 * must have permission granted on the work_q parameter's queue object.
2835 *
2836 * Otherwise this works the same as k_work_submit_to_queue().
2837 *
2838 * @note Can be called by ISRs.
2839 *
2840 * @param work_q Address of workqueue.
2841 * @param work Address of work item.
2842 *
2843 * @retval -EBUSY if the work item was already in some workqueue
2844 * @retval -ENOMEM if no memory for thread resource pool allocation
2845 * @retval 0 Success
2846 * @req K-WORK-001
2847 */
2848static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2849 struct k_work *work)
2850{
2851 int ret = -EBUSY;
2852
2853 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2854 ret = k_queue_alloc_append(&work_q->queue, work);
2855
2856 /* Couldn't insert into the queue. Clear the pending bit
2857 * so the work item can be submitted again
2858 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002859 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002860 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2861 }
2862 }
2863
2864 return ret;
2865}
2866
2867/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002868 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002869 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002870 * This routine indicates if work item @a work is pending in a workqueue's
2871 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002872 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002873 * @note Can be called by ISRs.
2874 *
2875 * @param work Address of work item.
2876 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002877 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002878 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002879 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002880static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002881{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002882 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002883}
2884
2885/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002886 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002887 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002888 * This routine starts workqueue @a work_q. The workqueue spawns its work
2889 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002890 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002891 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002892 * @param stack Pointer to work queue thread's stack space, as defined by
2893 * K_THREAD_STACK_DEFINE()
2894 * @param stack_size Size of the work queue thread's stack (in bytes), which
2895 * should either be the same constant passed to
2896 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002897 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002898 *
2899 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002900 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002901 */
Andrew Boie507852a2017-07-25 18:47:07 -07002902extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002903 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002904 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002905
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002906/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002907 * @brief Start a workqueue in user mode
2908 *
2909 * This works identically to k_work_q_start() except it is callable from user
2910 * mode, and the worker thread created will run in user mode.
2911 * The caller must have permissions granted on both the work_q parameter's
2912 * thread and queue objects, and the same restrictions on priority apply as
2913 * k_thread_create().
2914 *
2915 * @param work_q Address of workqueue.
2916 * @param stack Pointer to work queue thread's stack space, as defined by
2917 * K_THREAD_STACK_DEFINE()
2918 * @param stack_size Size of the work queue thread's stack (in bytes), which
2919 * should either be the same constant passed to
2920 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2921 * @param prio Priority of the work queue's thread.
2922 *
2923 * @return N/A
2924 * @req K-WORK-001
2925 */
2926extern void k_work_q_user_start(struct k_work_q *work_q,
2927 k_thread_stack_t *stack,
2928 size_t stack_size, int prio);
2929
2930/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002931 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002932 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002933 * This routine initializes a workqueue delayed work item, prior to
2934 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002935 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002936 * @param work Address of delayed work item.
2937 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002938 *
2939 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002940 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002941 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002942extern void k_delayed_work_init(struct k_delayed_work *work,
2943 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002944
2945/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002946 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002947 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002948 * This routine schedules work item @a work to be processed by workqueue
2949 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002950 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002951 * Only when the countdown completes is the work item actually submitted to
2952 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002953 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002954 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002955 * counting down cancels the existing submission and restarts the
2956 * countdown using the new delay. Note that this behavior is
2957 * inherently subject to race conditions with the pre-existing
2958 * timeouts and work queue, so care must be taken to synchronize such
2959 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002960 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002961 * @warning
2962 * A delayed work item must not be modified until it has been processed
2963 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002964 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002965 * @note Can be called by ISRs.
2966 *
2967 * @param work_q Address of workqueue.
2968 * @param work Address of delayed work item.
2969 * @param delay Delay before submitting the work item (in milliseconds).
2970 *
2971 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002972 * @retval -EINVAL Work item is being processed or has completed its work.
2973 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002974 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002975 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002976extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2977 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002978 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002979
2980/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002981 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002982 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002983 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002984 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002985 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002986 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002987 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002988 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08002989 * @note The result of calling this on a k_delayed_work item that has
2990 * not been submitted (i.e. before the return of the
2991 * k_delayed_work_submit_to_queue() call) is undefined.
2992 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002993 * @param work Address of delayed work item.
2994 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002995 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002996 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002997 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002998 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002999extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003000
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003001/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003002 * @brief Submit a work item to the system workqueue.
3003 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003004 * This routine submits work item @a work to be processed by the system
3005 * workqueue. If the work item is already pending in the workqueue's queue
3006 * as a result of an earlier submission, this routine has no effect on the
3007 * work item. If the work item has already been processed, or is currently
3008 * being processed, its work is considered complete and the work item can be
3009 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003010 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003011 * @warning
3012 * Work items submitted to the system workqueue should avoid using handlers
3013 * that block or yield since this may prevent the system workqueue from
3014 * processing other work items in a timely manner.
3015 *
3016 * @note Can be called by ISRs.
3017 *
3018 * @param work Address of work item.
3019 *
3020 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003021 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003022 */
3023static inline void k_work_submit(struct k_work *work)
3024{
3025 k_work_submit_to_queue(&k_sys_work_q, work);
3026}
3027
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003028/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003029 * @brief Submit a delayed work item to the system workqueue.
3030 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003031 * This routine schedules work item @a work to be processed by the system
3032 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003033 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003034 * Only when the countdown completes is the work item actually submitted to
3035 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003036 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003037 * Submitting a previously submitted delayed work item that is still
3038 * counting down cancels the existing submission and restarts the countdown
3039 * using the new delay. If the work item is currently pending on the
3040 * workqueue's queue because the countdown has completed it is too late to
3041 * resubmit the item, and resubmission fails without impacting the work item.
3042 * If the work item has already been processed, or is currently being processed,
3043 * its work is considered complete and the work item can be resubmitted.
3044 *
3045 * @warning
3046 * Work items submitted to the system workqueue should avoid using handlers
3047 * that block or yield since this may prevent the system workqueue from
3048 * processing other work items in a timely manner.
3049 *
3050 * @note Can be called by ISRs.
3051 *
3052 * @param work Address of delayed work item.
3053 * @param delay Delay before submitting the work item (in milliseconds).
3054 *
3055 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003056 * @retval -EINVAL Work item is being processed or has completed its work.
3057 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003058 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003059 */
3060static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003061 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003062{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05003063 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003064}
3065
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003066/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02003067 * @brief Get time remaining before a delayed work gets scheduled.
3068 *
3069 * This routine computes the (approximate) time remaining before a
3070 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02003071 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02003072 *
3073 * @param work Delayed work item.
3074 *
3075 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003076 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02003077 */
Kumar Galacc334c72017-04-21 10:55:34 -05003078static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02003079{
Andy Ross52e444b2018-09-28 09:06:37 -07003080 return __ticks_to_ms(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02003081}
3082
Piotr Zięcik19d83492019-09-27 09:16:25 +02003083/**
3084 * @brief Initialize a triggered work item.
3085 *
3086 * This routine initializes a workqueue triggered work item, prior to
3087 * its first use.
3088 *
3089 * @param work Address of triggered work item.
3090 * @param handler Function to invoke each time work item is processed.
3091 *
3092 * @return N/A
3093 */
3094extern void k_work_poll_init(struct k_work_poll *work,
3095 k_work_handler_t handler);
3096
3097/**
3098 * @brief Submit a triggered work item.
3099 *
3100 * This routine schedules work item @a work to be processed by workqueue
3101 * @a work_q when one of the given @a events is signaled. The routine
3102 * initiates internal poller for the work item and then returns to the caller.
3103 * Only when one of the watched events happen the work item is actually
3104 * submitted to the workqueue and becomes pending.
3105 *
3106 * Submitting a previously submitted triggered work item that is still
3107 * waiting for the event cancels the existing submission and reschedules it
3108 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003109 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003110 * so care must be taken to synchronize such resubmissions externally.
3111 *
3112 * @note Can be called by ISRs.
3113 *
3114 * @warning
3115 * Provided array of events as well as a triggered work item must be placed
3116 * in persistent memory (valid until work handler execution or work
3117 * cancellation) and cannot be modified after submission.
3118 *
3119 * @param work_q Address of workqueue.
3120 * @param work Address of delayed work item.
3121 * @param events An array of pointers to events which trigger the work.
3122 * @param num_events The number of events in the array.
3123 * @param timeout Timeout after which the work will be scheduled for
3124 * execution even if not triggered.
3125 *
3126 *
3127 * @retval 0 Work item started watching for events.
3128 * @retval -EINVAL Work item is being processed or has completed its work.
3129 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3130 */
3131extern int k_work_poll_submit_to_queue(struct k_work_q *work_q,
3132 struct k_work_poll *work,
3133 struct k_poll_event *events,
3134 int num_events,
3135 s32_t timeout);
3136
3137/**
3138 * @brief Submit a triggered work item to the system workqueue.
3139 *
3140 * This routine schedules work item @a work to be processed by system
3141 * workqueue when one of the given @a events is signaled. The routine
3142 * initiates internal poller for the work item and then returns to the caller.
3143 * Only when one of the watched events happen the work item is actually
3144 * submitted to the workqueue and becomes pending.
3145 *
3146 * Submitting a previously submitted triggered work item that is still
3147 * waiting for the event cancels the existing submission and reschedules it
3148 * the using the new event list. Note that this behavior is inherently subject
David B. Kinder73896c02019-10-28 16:27:57 -07003149 * to race conditions with the pre-existing triggered work item and work queue,
Piotr Zięcik19d83492019-09-27 09:16:25 +02003150 * so care must be taken to synchronize such resubmissions externally.
3151 *
3152 * @note Can be called by ISRs.
3153 *
3154 * @warning
3155 * Provided array of events as well as a triggered work item must not be
3156 * modified until the item has been processed by the workqueue.
3157 *
3158 * @param work Address of delayed work item.
3159 * @param events An array of pointers to events which trigger the work.
3160 * @param num_events The number of events in the array.
3161 * @param timeout Timeout after which the work will be scheduled for
3162 * execution even if not triggered.
3163 *
3164 * @retval 0 Work item started watching for events.
3165 * @retval -EINVAL Work item is being processed or has completed its work.
3166 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3167 */
3168static inline int k_work_poll_submit(struct k_work_poll *work,
3169 struct k_poll_event *events,
3170 int num_events,
3171 s32_t timeout)
3172{
3173 return k_work_poll_submit_to_queue(&k_sys_work_q, work,
3174 events, num_events, timeout);
3175}
3176
3177/**
3178 * @brief Cancel a triggered work item.
3179 *
3180 * This routine cancels the submission of triggered work item @a work.
3181 * A triggered work item can only be canceled if no event triggered work
3182 * submission.
3183 *
3184 * @note Can be called by ISRs.
3185 *
3186 * @param work Address of delayed work item.
3187 *
David B. Kinder73896c02019-10-28 16:27:57 -07003188 * @retval 0 Work item canceled.
Piotr Zięcik19d83492019-09-27 09:16:25 +02003189 * @retval -EINVAL Work item is being processed or has completed its work.
3190 */
3191extern int k_work_poll_cancel(struct k_work_poll *work);
3192
Anas Nashif166f5192018-02-25 08:02:36 -06003193/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003194/**
Anas Nashifce78d162018-05-24 12:43:11 -05003195 * @defgroup mutex_apis Mutex APIs
3196 * @ingroup kernel_apis
3197 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003198 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003199
Anas Nashifce78d162018-05-24 12:43:11 -05003200/**
3201 * Mutex Structure
3202 * @ingroup mutex_apis
3203 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003204struct k_mutex {
3205 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05003206 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04003207 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05003208 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003209 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003210
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003211 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003212};
3213
Anas Nashifce78d162018-05-24 12:43:11 -05003214/**
3215 * @cond INTERNAL_HIDDEN
3216 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003217#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003218 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003219 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003220 .owner = NULL, \
3221 .lock_count = 0, \
3222 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003223 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003224 }
3225
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003226#define K_MUTEX_INITIALIZER __DEPRECATED_MACRO _K_MUTEX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003227
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003228/**
Allan Stephensc98da842016-11-11 15:45:03 -05003229 * INTERNAL_HIDDEN @endcond
3230 */
3231
3232/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003233 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003235 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003236 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003237 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003239 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003240 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003241 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003242#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003243 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003244 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003245
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003246/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003247 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003248 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003249 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003251 * Upon completion, the mutex is available and does not have an owner.
3252 *
3253 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003254 *
3255 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003256 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003257 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003258__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003259
3260/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003261 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003262 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003263 * This routine locks @a mutex. If the mutex is locked by another thread,
3264 * the calling thread waits until the mutex becomes available or until
3265 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003267 * A thread is permitted to lock a mutex it has already locked. The operation
3268 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003269 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003270 * @param mutex Address of the mutex.
3271 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003272 * or one of the special values K_NO_WAIT and K_FOREVER.
3273 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003274 * @retval 0 Mutex locked.
3275 * @retval -EBUSY Returned without waiting.
3276 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003277 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003278 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003279__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003280
3281/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003282 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003283 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003284 * This routine unlocks @a mutex. The mutex must already be locked by the
3285 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003286 *
3287 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003288 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003289 * thread.
3290 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003291 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003292 *
3293 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003294 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003295 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003296__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003297
Allan Stephensc98da842016-11-11 15:45:03 -05003298/**
Anas Nashif166f5192018-02-25 08:02:36 -06003299 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003300 */
3301
3302/**
3303 * @cond INTERNAL_HIDDEN
3304 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003305
3306struct k_sem {
3307 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303308 u32_t count;
3309 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003310 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003311
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003312 _OBJECT_TRACING_NEXT_PTR(k_sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003313};
3314
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003315#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003316 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003317 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003318 .count = initial_count, \
3319 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003320 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003321 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003322 }
3323
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003324#define K_SEM_INITIALIZER __DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003325
Allan Stephensc98da842016-11-11 15:45:03 -05003326/**
3327 * INTERNAL_HIDDEN @endcond
3328 */
3329
3330/**
3331 * @defgroup semaphore_apis Semaphore APIs
3332 * @ingroup kernel_apis
3333 * @{
3334 */
3335
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003336/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003337 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003338 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003339 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003340 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003341 * @param sem Address of the semaphore.
3342 * @param initial_count Initial semaphore count.
3343 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003344 *
3345 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003346 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003347 */
Andrew Boie99280232017-09-29 14:17:47 -07003348__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
3349 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003350
3351/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003352 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003353 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003354 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003355 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003356 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3357 *
3358 * @param sem Address of the semaphore.
3359 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003360 * or one of the special values K_NO_WAIT and K_FOREVER.
3361 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05003362 * @note When porting code from the nanokernel legacy API to the new API, be
3363 * careful with the return value of this function. The return value is the
3364 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
3365 * non-zero means failure, while the nano_sem_take family returns 1 for success
3366 * and 0 for failure.
3367 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003368 * @retval 0 Semaphore taken.
3369 * @retval -EBUSY Returned without waiting.
3370 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003371 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003372 */
Andrew Boie99280232017-09-29 14:17:47 -07003373__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003374
3375/**
3376 * @brief Give a semaphore.
3377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003378 * This routine gives @a sem, unless the semaphore is already at its maximum
3379 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003380 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003381 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003383 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003384 *
3385 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003386 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003387 */
Andrew Boie99280232017-09-29 14:17:47 -07003388__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003389
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003390/**
3391 * @brief Reset a semaphore's count to zero.
3392 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003393 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003394 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003395 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003396 *
3397 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003398 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003399 */
Andrew Boie990bf162017-10-03 12:36:49 -07003400__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003401
Anas Nashif954d5502018-02-25 08:37:28 -06003402/**
3403 * @internal
3404 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003405static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003406{
Patrik Flykt24d71432019-03-26 19:57:45 -06003407 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003408}
3409
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003410/**
3411 * @brief Get a semaphore's count.
3412 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003413 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003415 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003416 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003417 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003418 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003419 */
Andrew Boie990bf162017-10-03 12:36:49 -07003420__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003421
Anas Nashif954d5502018-02-25 08:37:28 -06003422/**
3423 * @internal
3424 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003425static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003426{
3427 return sem->count;
3428}
3429
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003430/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003431 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003432 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003433 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003434 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003435 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003436 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003437 * @param name Name of the semaphore.
3438 * @param initial_count Initial semaphore count.
3439 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003440 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003441 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003442#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003443 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003444 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303445 BUILD_ASSERT(((count_limit) != 0) && \
3446 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003447
Anas Nashif166f5192018-02-25 08:02:36 -06003448/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003449
3450/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003451 * @defgroup msgq_apis Message Queue APIs
3452 * @ingroup kernel_apis
3453 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003454 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003455
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003456/**
3457 * @brief Message Queue Structure
3458 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003459struct k_msgq {
3460 _wait_q_t wait_q;
Andy Rossbe03dbd2018-07-26 10:23:02 -07003461 struct k_spinlock lock;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003462 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003463 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003464 char *buffer_start;
3465 char *buffer_end;
3466 char *read_ptr;
3467 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003468 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003469
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003470 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Andrew Boie0fe789f2018-04-12 18:35:56 -07003471 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003472};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003473/**
3474 * @cond INTERNAL_HIDDEN
3475 */
3476
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003477
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003478#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003479 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003480 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003481 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003482 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003483 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003484 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003485 .read_ptr = q_buffer, \
3486 .write_ptr = q_buffer, \
3487 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003488 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003489 }
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003490#define K_MSGQ_INITIALIZER __DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003491/**
3492 * INTERNAL_HIDDEN @endcond
3493 */
3494
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003495
Andrew Boie0fe789f2018-04-12 18:35:56 -07003496#define K_MSGQ_FLAG_ALLOC BIT(0)
3497
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003498/**
3499 * @brief Message Queue Attributes
3500 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303501struct k_msgq_attrs {
3502 size_t msg_size;
3503 u32_t max_msgs;
3504 u32_t used_msgs;
3505};
3506
Allan Stephensc98da842016-11-11 15:45:03 -05003507
3508/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003509 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003510 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003511 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3512 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003513 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3514 * message is similarly aligned to this boundary, @a q_msg_size must also be
3515 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003516 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003517 * The message queue can be accessed outside the module where it is defined
3518 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003519 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003520 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003521 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003522 * @param q_name Name of the message queue.
3523 * @param q_msg_size Message size (in bytes).
3524 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003525 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003526 *
3527 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003528 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003529#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3530 static char __noinit __aligned(q_align) \
3531 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3532 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
3533 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003534 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003535
Peter Mitsisd7a37502016-10-13 11:37:40 -04003536/**
3537 * @brief Initialize a message queue.
3538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003539 * This routine initializes a message queue object, prior to its first use.
3540 *
Allan Stephensda827222016-11-09 14:23:58 -06003541 * The message queue's ring buffer must contain space for @a max_msgs messages,
3542 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3543 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3544 * that each message is similarly aligned to this boundary, @a q_msg_size
3545 * must also be a multiple of N.
3546 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003547 * @param q Address of the message queue.
3548 * @param buffer Pointer to ring buffer that holds queued messages.
3549 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003550 * @param max_msgs Maximum number of messages that can be queued.
3551 *
3552 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003553 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003554 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003555void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3556 u32_t max_msgs);
3557
3558/**
3559 * @brief Initialize a message queue.
3560 *
3561 * This routine initializes a message queue object, prior to its first use,
3562 * allocating its internal ring buffer from the calling thread's resource
3563 * pool.
3564 *
3565 * Memory allocated for the ring buffer can be released by calling
3566 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3567 * all of its references.
3568 *
3569 * @param q Address of the message queue.
3570 * @param msg_size Message size (in bytes).
3571 * @param max_msgs Maximum number of messages that can be queued.
3572 *
3573 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3574 * thread's resource pool, or -EINVAL if the size parameters cause
3575 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003576 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003577 */
3578__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3579 u32_t max_msgs);
3580
3581
3582void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003583
3584/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003585 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003586 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003587 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003588 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003589 * @note Can be called by ISRs.
3590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003591 * @param q Address of the message queue.
3592 * @param data Pointer to the message.
3593 * @param timeout Waiting period to add the message (in milliseconds),
3594 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003595 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003596 * @retval 0 Message sent.
3597 * @retval -ENOMSG Returned without waiting or queue purged.
3598 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003599 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003600 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003601__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003602
3603/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003604 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003606 * This routine receives a message from message queue @a q in a "first in,
3607 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003608 *
Allan Stephensc98da842016-11-11 15:45:03 -05003609 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003610 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003611 * @param q Address of the message queue.
3612 * @param data Address of area to hold the received message.
3613 * @param timeout Waiting period to receive the message (in milliseconds),
3614 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003615 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003616 * @retval 0 Message received.
3617 * @retval -ENOMSG Returned without waiting.
3618 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003619 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003620 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003621__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003622
3623/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003624 * @brief Peek/read a message from a message queue.
3625 *
3626 * This routine reads a message from message queue @a q in a "first in,
3627 * first out" manner and leaves the message in the queue.
3628 *
3629 * @note Can be called by ISRs.
3630 *
3631 * @param q Address of the message queue.
3632 * @param data Address of area to hold the message read from the queue.
3633 *
3634 * @retval 0 Message read.
3635 * @retval -ENOMSG Returned when the queue has no message.
3636 * @req K-MSGQ-002
3637 */
3638__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3639
3640/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003641 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003642 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003643 * This routine discards all unreceived messages in a message queue's ring
3644 * buffer. Any threads that are blocked waiting to send a message to the
3645 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003647 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003648 *
3649 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003650 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003651 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003652__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003653
Peter Mitsis67be2492016-10-07 11:44:34 -04003654/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003655 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003657 * This routine returns the number of unused entries in a message queue's
3658 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003659 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003660 * @param q Address of the message queue.
3661 *
3662 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003663 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003664 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003665__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3666
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303667/**
3668 * @brief Get basic attributes of a message queue.
3669 *
3670 * This routine fetches basic attributes of message queue into attr argument.
3671 *
3672 * @param q Address of the message queue.
3673 * @param attrs pointer to message queue attribute structure.
3674 *
3675 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003676 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303677 */
3678__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3679
3680
Patrik Flykt4344e272019-03-08 14:19:05 -07003681static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003682{
3683 return q->max_msgs - q->used_msgs;
3684}
3685
Peter Mitsisd7a37502016-10-13 11:37:40 -04003686/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003687 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003688 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003689 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003691 * @param q Address of the message queue.
3692 *
3693 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003694 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003695 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003696__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3697
Patrik Flykt4344e272019-03-08 14:19:05 -07003698static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003699{
3700 return q->used_msgs;
3701}
3702
Anas Nashif166f5192018-02-25 08:02:36 -06003703/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003704
3705/**
3706 * @defgroup mem_pool_apis Memory Pool APIs
3707 * @ingroup kernel_apis
3708 * @{
3709 */
3710
Andy Ross73cb9582017-05-09 10:42:39 -07003711/* Note on sizing: the use of a 20 bit field for block means that,
3712 * assuming a reasonable minimum block size of 16 bytes, we're limited
3713 * to 16M of memory managed by a single pool. Long term it would be
3714 * good to move to a variable bit size based on configuration.
3715 */
3716struct k_mem_block_id {
3717 u32_t pool : 8;
3718 u32_t level : 4;
3719 u32_t block : 20;
3720};
3721
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003722struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003723 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003724 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003725};
3726
Anas Nashif166f5192018-02-25 08:02:36 -06003727/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003728
3729/**
3730 * @defgroup mailbox_apis Mailbox APIs
3731 * @ingroup kernel_apis
3732 * @{
3733 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003734
3735struct k_mbox_msg {
3736 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003737 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003738 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003739 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003740 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003741 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003742 /** sender's message data buffer */
3743 void *tx_data;
3744 /** internal use only - needed for legacy API support */
3745 void *_rx_data;
3746 /** message data block descriptor */
3747 struct k_mem_block tx_block;
3748 /** source thread id */
3749 k_tid_t rx_source_thread;
3750 /** target thread id */
3751 k_tid_t tx_target_thread;
3752 /** internal use only - thread waiting on send (may be a dummy) */
3753 k_tid_t _syncing_thread;
3754#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3755 /** internal use only - semaphore used during asynchronous send */
3756 struct k_sem *_async_sem;
3757#endif
3758};
3759
3760struct k_mbox {
3761 _wait_q_t tx_msg_queue;
3762 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003763 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003764
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003765 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003766};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003767/**
3768 * @cond INTERNAL_HIDDEN
3769 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003770
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003771#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003772 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003773 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3774 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003775 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003776 }
3777
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003778#define K_MBOX_INITIALIZER __DEPRECATED_MACRO _K_MBOX_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003779
Peter Mitsis12092702016-10-14 12:57:23 -04003780/**
Allan Stephensc98da842016-11-11 15:45:03 -05003781 * INTERNAL_HIDDEN @endcond
3782 */
3783
3784/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003785 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003786 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003787 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003788 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003789 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003791 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003792 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003793 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003794#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003795 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003796 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003797
Peter Mitsis12092702016-10-14 12:57:23 -04003798/**
3799 * @brief Initialize a mailbox.
3800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003801 * This routine initializes a mailbox object, prior to its first use.
3802 *
3803 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003804 *
3805 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003806 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003807 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003808extern void k_mbox_init(struct k_mbox *mbox);
3809
Peter Mitsis12092702016-10-14 12:57:23 -04003810/**
3811 * @brief Send a mailbox message in a synchronous manner.
3812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003813 * This routine sends a message to @a mbox and waits for a receiver to both
3814 * receive and process it. The message data may be in a buffer, in a memory
3815 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003817 * @param mbox Address of the mailbox.
3818 * @param tx_msg Address of the transmit message descriptor.
3819 * @param timeout Waiting period for the message to be received (in
3820 * milliseconds), or one of the special values K_NO_WAIT
3821 * and K_FOREVER. Once the message has been received,
3822 * this routine waits as long as necessary for the message
3823 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003824 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003825 * @retval 0 Message sent.
3826 * @retval -ENOMSG Returned without waiting.
3827 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003828 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003829 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003830extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003831 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003832
Peter Mitsis12092702016-10-14 12:57:23 -04003833/**
3834 * @brief Send a mailbox message in an asynchronous manner.
3835 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003836 * This routine sends a message to @a mbox without waiting for a receiver
3837 * to process it. The message data may be in a buffer, in a memory pool block,
3838 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3839 * will be given when the message has been both received and completely
3840 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003842 * @param mbox Address of the mailbox.
3843 * @param tx_msg Address of the transmit message descriptor.
3844 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003845 *
3846 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003847 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003848 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003849extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003850 struct k_sem *sem);
3851
Peter Mitsis12092702016-10-14 12:57:23 -04003852/**
3853 * @brief Receive a mailbox message.
3854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003855 * This routine receives a message from @a mbox, then optionally retrieves
3856 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003858 * @param mbox Address of the mailbox.
3859 * @param rx_msg Address of the receive message descriptor.
3860 * @param buffer Address of the buffer to receive data, or NULL to defer data
3861 * retrieval and message disposal until later.
3862 * @param timeout Waiting period for a message to be received (in
3863 * milliseconds), or one of the special values K_NO_WAIT
3864 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003865 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003866 * @retval 0 Message received.
3867 * @retval -ENOMSG Returned without waiting.
3868 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003869 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003870 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003871extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003872 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003873
3874/**
3875 * @brief Retrieve mailbox message data into a buffer.
3876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003877 * This routine completes the processing of a received message by retrieving
3878 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003879 *
3880 * Alternatively, this routine can be used to dispose of a received message
3881 * without retrieving its data.
3882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003883 * @param rx_msg Address of the receive message descriptor.
3884 * @param buffer Address of the buffer to receive data, or NULL to discard
3885 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003886 *
3887 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003888 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003889 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003890extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003891
3892/**
3893 * @brief Retrieve mailbox message data into a memory pool block.
3894 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003895 * This routine completes the processing of a received message by retrieving
3896 * its data into a memory pool block, then disposing of the message.
3897 * The memory pool block that results from successful retrieval must be
3898 * returned to the pool once the data has been processed, even in cases
3899 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003900 *
3901 * Alternatively, this routine can be used to dispose of a received message
3902 * without retrieving its data. In this case there is no need to return a
3903 * memory pool block to the pool.
3904 *
3905 * This routine allocates a new memory pool block for the data only if the
3906 * data is not already in one. If a new block cannot be allocated, the routine
3907 * returns a failure code and the received message is left unchanged. This
3908 * permits the caller to reattempt data retrieval at a later time or to dispose
3909 * of the received message without retrieving its data.
3910 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003911 * @param rx_msg Address of a receive message descriptor.
3912 * @param pool Address of memory pool, or NULL to discard data.
3913 * @param block Address of the area to hold memory pool block info.
3914 * @param timeout Waiting period to wait for a memory pool block (in
3915 * milliseconds), or one of the special values K_NO_WAIT
3916 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003917 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003918 * @retval 0 Data retrieved.
3919 * @retval -ENOMEM Returned without waiting.
3920 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003921 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003922 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003923extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003924 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003925 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003926
Anas Nashif166f5192018-02-25 08:02:36 -06003927/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003928
3929/**
Anas Nashifce78d162018-05-24 12:43:11 -05003930 * @defgroup pipe_apis Pipe APIs
3931 * @ingroup kernel_apis
3932 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003933 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003934
Anas Nashifce78d162018-05-24 12:43:11 -05003935/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003936struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003937 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3938 size_t size; /**< Buffer size */
3939 size_t bytes_used; /**< # bytes used in buffer */
3940 size_t read_index; /**< Where in buffer to read from */
3941 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08003942 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003943
3944 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003945 _wait_q_t readers; /**< Reader wait queue */
3946 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003947 } wait_q;
3948
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003949 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Anas Nashifce78d162018-05-24 12:43:11 -05003950 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003951};
3952
Anas Nashifce78d162018-05-24 12:43:11 -05003953/**
3954 * @cond INTERNAL_HIDDEN
3955 */
3956#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3957
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003958#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
3959 { \
3960 .buffer = pipe_buffer, \
3961 .size = pipe_buffer_size, \
3962 .bytes_used = 0, \
3963 .read_index = 0, \
3964 .write_index = 0, \
3965 .lock = {}, \
3966 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003967 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
3968 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003969 }, \
3970 _OBJECT_TRACING_INIT \
3971 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003972 }
3973
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05003974#define K_PIPE_INITIALIZER __DEPRECATED_MACRO _K_PIPE_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003975
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003976/**
Allan Stephensc98da842016-11-11 15:45:03 -05003977 * INTERNAL_HIDDEN @endcond
3978 */
3979
3980/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003981 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003983 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003984 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003985 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003987 * @param name Name of the pipe.
3988 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3989 * or zero if no ring buffer is used.
3990 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003991 *
3992 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003993 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003994#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08003995 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07003996 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003997 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003998 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003999
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004000/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004001 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004002 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004003 * This routine initializes a pipe object, prior to its first use.
4004 *
4005 * @param pipe Address of the pipe.
4006 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
4007 * is used.
4008 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4009 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004010 *
4011 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004012 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004013 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004014void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
4015
4016/**
4017 * @brief Release a pipe's allocated buffer
4018 *
4019 * If a pipe object was given a dynamically allocated buffer via
4020 * k_pipe_alloc_init(), this will free it. This function does nothing
4021 * if the buffer wasn't dynamically allocated.
4022 *
4023 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004024 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004025 */
4026void k_pipe_cleanup(struct k_pipe *pipe);
4027
4028/**
4029 * @brief Initialize a pipe and allocate a buffer for it
4030 *
4031 * Storage for the buffer region will be allocated from the calling thread's
4032 * resource pool. This memory will be released if k_pipe_cleanup() is called,
4033 * or userspace is enabled and the pipe object loses all references to it.
4034 *
4035 * This function should only be called on uninitialized pipe objects.
4036 *
4037 * @param pipe Address of the pipe.
4038 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4039 * buffer is used.
4040 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004041 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004042 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004043 */
4044__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004045
4046/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004047 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004048 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004049 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004050 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004051 * @param pipe Address of the pipe.
4052 * @param data Address of data to write.
4053 * @param bytes_to_write Size of data (in bytes).
4054 * @param bytes_written Address of area to hold the number of bytes written.
4055 * @param min_xfer Minimum number of bytes to write.
4056 * @param timeout Waiting period to wait for the data to be written (in
4057 * milliseconds), or one of the special values K_NO_WAIT
4058 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004059 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004060 * @retval 0 At least @a min_xfer bytes of data were written.
4061 * @retval -EIO Returned without waiting; zero data bytes were written.
4062 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004063 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004064 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004065 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004066__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
4067 size_t bytes_to_write, size_t *bytes_written,
4068 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004069
4070/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004071 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004073 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004075 * @param pipe Address of the pipe.
4076 * @param data Address to place the data read from pipe.
4077 * @param bytes_to_read Maximum number of data bytes to read.
4078 * @param bytes_read Address of area to hold the number of bytes read.
4079 * @param min_xfer Minimum number of data bytes to read.
4080 * @param timeout Waiting period to wait for the data to be read (in
4081 * milliseconds), or one of the special values K_NO_WAIT
4082 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004083 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004084 * @retval 0 At least @a min_xfer bytes of data were read.
4085 * @retval -EIO Returned without waiting; zero data bytes were read.
4086 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004087 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004088 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004089 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004090__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
4091 size_t bytes_to_read, size_t *bytes_read,
4092 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004093
4094/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004095 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004096 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004097 * This routine writes the data contained in a memory block to @a pipe.
4098 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004099 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004101 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004102 * @param block Memory block containing data to send
4103 * @param size Number of data bytes in memory block to send
4104 * @param sem Semaphore to signal upon completion (else NULL)
4105 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004106 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004107 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004108 */
4109extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
4110 size_t size, struct k_sem *sem);
4111
Anas Nashif166f5192018-02-25 08:02:36 -06004112/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004113
Allan Stephensc98da842016-11-11 15:45:03 -05004114/**
4115 * @cond INTERNAL_HIDDEN
4116 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004117
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004118struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004119 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05004120 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04004121 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004122 char *buffer;
4123 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05004124 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004125
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004126 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004127};
4128
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004129#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004130 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004131 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004132 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004133 .num_blocks = slab_num_blocks, \
4134 .block_size = slab_block_size, \
4135 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004136 .free_list = NULL, \
4137 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05004138 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004139 }
4140
Peter A. Bigotb4d5e672019-11-02 10:35:08 -05004141#define K_MEM_SLAB_INITIALIZER __DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004142
4143
Peter Mitsis578f9112016-10-07 13:50:31 -04004144/**
Allan Stephensc98da842016-11-11 15:45:03 -05004145 * INTERNAL_HIDDEN @endcond
4146 */
4147
4148/**
4149 * @defgroup mem_slab_apis Memory Slab APIs
4150 * @ingroup kernel_apis
4151 * @{
4152 */
4153
4154/**
Allan Stephensda827222016-11-09 14:23:58 -06004155 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04004156 *
Allan Stephensda827222016-11-09 14:23:58 -06004157 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004158 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06004159 * @a slab_align -byte boundary. To ensure that each memory block is similarly
4160 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004161 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04004162 *
Allan Stephensda827222016-11-09 14:23:58 -06004163 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004164 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004165 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004166 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004168 * @param name Name of the memory slab.
4169 * @param slab_block_size Size of each memory block (in bytes).
4170 * @param slab_num_blocks Number memory blocks.
4171 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004172 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04004173 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004174#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004175 char __noinit __aligned(WB_UP(slab_align)) \
4176 _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004177 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004178 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004179 WB_UP(slab_block_size), slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004180
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004181/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004182 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004183 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004184 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004185 *
Allan Stephensda827222016-11-09 14:23:58 -06004186 * The memory slab's buffer contains @a slab_num_blocks memory blocks
4187 * that are @a slab_block_size bytes long. The buffer must be aligned to an
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004188 * N-byte boundary matching a word boundary, where N is a power of 2
4189 * (i.e. 4 on 32-bit systems, 8, 16, ...).
Allan Stephensda827222016-11-09 14:23:58 -06004190 * To ensure that each memory block is similarly aligned to this boundary,
4191 * @a slab_block_size must also be a multiple of N.
4192 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004193 * @param slab Address of the memory slab.
4194 * @param buffer Pointer to buffer used for the memory blocks.
4195 * @param block_size Size of each memory block (in bytes).
4196 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004197 *
4198 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004199 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004200 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004201extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05004202 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004203
4204/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004205 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004206 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004207 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004208 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004209 * @param slab Address of the memory slab.
4210 * @param mem Pointer to block address area.
4211 * @param timeout Maximum time to wait for operation to complete
4212 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4213 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004214 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004215 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004216 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004217 * @retval -ENOMEM Returned without waiting.
4218 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004219 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004220 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004221extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05004222 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004223
4224/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004225 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004226 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004227 * This routine releases a previously allocated memory block back to its
4228 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004229 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004230 * @param slab Address of the memory slab.
4231 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004232 *
4233 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004234 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004235 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004236extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004237
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004238/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004239 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004240 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004241 * This routine gets the number of memory blocks that are currently
4242 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004243 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004244 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004246 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004247 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004248 */
Kumar Galacc334c72017-04-21 10:55:34 -05004249static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004250{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004251 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004252}
4253
Peter Mitsisc001aa82016-10-13 13:53:37 -04004254/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004255 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004256 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004257 * This routine gets the number of memory blocks that are currently
4258 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004259 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004260 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004261 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004262 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004263 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004264 */
Kumar Galacc334c72017-04-21 10:55:34 -05004265static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004266{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004267 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004268}
4269
Anas Nashif166f5192018-02-25 08:02:36 -06004270/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004271
4272/**
4273 * @cond INTERNAL_HIDDEN
4274 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004275
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004276struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004277 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004278 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004279};
4280
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004281/**
Allan Stephensc98da842016-11-11 15:45:03 -05004282 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004283 */
4284
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004285/**
Allan Stephensc98da842016-11-11 15:45:03 -05004286 * @addtogroup mem_pool_apis
4287 * @{
4288 */
4289
4290/**
4291 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004293 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4294 * long. The memory pool allows blocks to be repeatedly partitioned into
4295 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004296 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004297 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004298 * If the pool is to be accessed outside the module where it is defined, it
4299 * can be declared via
4300 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004301 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004302 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004303 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004304 * @param minsz Size of the smallest blocks in the pool (in bytes).
4305 * @param maxsz Size of the largest blocks in the pool (in bytes).
4306 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004307 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004308 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004309 */
Andy Ross73cb9582017-05-09 10:42:39 -07004310#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004311 char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \
Andy Ross73cb9582017-05-09 10:42:39 -07004312 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004313 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004314 Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004315 .base = { \
4316 .buf = _mpool_buf_##name, \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004317 .max_sz = WB_UP(maxsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004318 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004319 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004320 .levels = _mpool_lvls_##name, \
4321 .flags = SYS_MEM_POOL_KERNEL \
4322 } \
Johann Fischer223a2b92019-07-04 15:55:20 +02004323 }; \
Nicolas Pitreb2a022b2019-09-26 16:36:40 -04004324 BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004325
Peter Mitsis937042c2016-10-13 13:18:26 -04004326/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004327 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004328 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004329 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004330 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004331 * @param pool Address of the memory pool.
4332 * @param block Pointer to block descriptor for the allocated memory.
4333 * @param size Amount of memory to allocate (in bytes).
4334 * @param timeout Maximum time to wait for operation to complete
4335 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4336 * or K_FOREVER to wait as long as necessary.
4337 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004338 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004339 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004340 * @retval -ENOMEM Returned without waiting.
4341 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004342 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004343 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004344extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004345 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004346
4347/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004348 * @brief Allocate memory from a memory pool with malloc() semantics
4349 *
4350 * Such memory must be released using k_free().
4351 *
4352 * @param pool Address of the memory pool.
4353 * @param size Amount of memory to allocate (in bytes).
4354 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004355 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004356 */
4357extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4358
4359/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004360 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004362 * This routine releases a previously allocated memory block back to its
4363 * memory pool.
4364 *
4365 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004366 *
4367 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004368 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004369 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004370extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004371
4372/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004373 * @brief Free memory allocated from a memory pool.
4374 *
4375 * This routine releases a previously allocated memory block back to its
4376 * memory pool
4377 *
4378 * @param id Memory block identifier.
4379 *
4380 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004381 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004382 */
4383extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4384
4385/**
Anas Nashif166f5192018-02-25 08:02:36 -06004386 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004387 */
4388
4389/**
4390 * @defgroup heap_apis Heap Memory Pool APIs
4391 * @ingroup kernel_apis
4392 * @{
4393 */
4394
4395/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004396 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004397 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004398 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004399 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004400 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004401 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004403 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004404 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004405 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004406extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004407
4408/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004409 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004410 *
4411 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004412 * returned must have been allocated from the heap memory pool or
4413 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004414 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004415 * If @a ptr is NULL, no operation is performed.
4416 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004417 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004418 *
4419 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004420 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004421 */
4422extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004423
Allan Stephensc98da842016-11-11 15:45:03 -05004424/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004425 * @brief Allocate memory from heap, array style
4426 *
4427 * This routine provides traditional calloc() semantics. Memory is
4428 * allocated from the heap memory pool and zeroed.
4429 *
4430 * @param nmemb Number of elements in the requested array
4431 * @param size Size of each array element (in bytes).
4432 *
4433 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004434 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004435 */
4436extern void *k_calloc(size_t nmemb, size_t size);
4437
Anas Nashif166f5192018-02-25 08:02:36 -06004438/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004439
Benjamin Walshacc68c12017-01-29 18:57:45 -05004440/* polling API - PRIVATE */
4441
Benjamin Walshb0179862017-02-02 16:39:57 -05004442#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004443#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004444#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004445#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004446#endif
4447
Benjamin Walshacc68c12017-01-29 18:57:45 -05004448/* private - types bit positions */
4449enum _poll_types_bits {
4450 /* can be used to ignore an event */
4451 _POLL_TYPE_IGNORE,
4452
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004453 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004454 _POLL_TYPE_SIGNAL,
4455
4456 /* semaphore availability */
4457 _POLL_TYPE_SEM_AVAILABLE,
4458
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004459 /* queue/fifo/lifo data availability */
4460 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004461
4462 _POLL_NUM_TYPES
4463};
4464
Patrik Flykt4344e272019-03-08 14:19:05 -07004465#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004466
4467/* private - states bit positions */
4468enum _poll_states_bits {
4469 /* default state when creating event */
4470 _POLL_STATE_NOT_READY,
4471
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004472 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004473 _POLL_STATE_SIGNALED,
4474
4475 /* semaphore is available */
4476 _POLL_STATE_SEM_AVAILABLE,
4477
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004478 /* data is available to read on queue/fifo/lifo */
4479 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004480
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004481 /* queue/fifo/lifo wait was cancelled */
4482 _POLL_STATE_CANCELLED,
4483
Benjamin Walshacc68c12017-01-29 18:57:45 -05004484 _POLL_NUM_STATES
4485};
4486
Patrik Flykt4344e272019-03-08 14:19:05 -07004487#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004488
4489#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004490 (32 - (0 \
4491 + 8 /* tag */ \
4492 + _POLL_NUM_TYPES \
4493 + _POLL_NUM_STATES \
4494 + 1 /* modes */ \
4495 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004496
Benjamin Walshacc68c12017-01-29 18:57:45 -05004497/* end of polling API - PRIVATE */
4498
4499
4500/**
4501 * @defgroup poll_apis Async polling APIs
4502 * @ingroup kernel_apis
4503 * @{
4504 */
4505
4506/* Public polling API */
4507
4508/* public - values for k_poll_event.type bitfield */
4509#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004510#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4511#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4512#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004513#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004514
4515/* public - polling modes */
4516enum k_poll_modes {
4517 /* polling thread does not take ownership of objects when available */
4518 K_POLL_MODE_NOTIFY_ONLY = 0,
4519
4520 K_POLL_NUM_MODES
4521};
4522
4523/* public - values for k_poll_event.state bitfield */
4524#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004525#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4526#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4527#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004528#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004529#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004530
4531/* public - poll signal object */
4532struct k_poll_signal {
4533 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004534 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004535
4536 /*
4537 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4538 * user resets it to 0.
4539 */
4540 unsigned int signaled;
4541
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004542 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004543 int result;
4544};
4545
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004546#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004547 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004548 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004549 .signaled = 0, \
4550 .result = 0, \
4551 }
4552
4553struct k_poll_event {
4554 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004555 sys_dnode_t _node;
4556
4557 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004558 struct _poller *poller;
4559
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004560 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004561 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004562
Benjamin Walshacc68c12017-01-29 18:57:45 -05004563 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004564 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004565
4566 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004567 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004568
4569 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004570 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004571
4572 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004573 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004574
4575 /* per-type data */
4576 union {
4577 void *obj;
4578 struct k_poll_signal *signal;
4579 struct k_sem *sem;
4580 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004581 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004582 };
4583};
4584
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004585#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004586 { \
4587 .poller = NULL, \
4588 .type = event_type, \
4589 .state = K_POLL_STATE_NOT_READY, \
4590 .mode = event_mode, \
4591 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004592 { .obj = event_obj }, \
4593 }
4594
4595#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4596 event_tag) \
4597 { \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004598 .tag = event_tag, \
Markus Fuchsbe21d3f2019-10-09 21:31:25 +02004599 .type = event_type, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004600 .state = K_POLL_STATE_NOT_READY, \
4601 .mode = event_mode, \
4602 .unused = 0, \
4603 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004604 }
4605
4606/**
4607 * @brief Initialize one struct k_poll_event instance
4608 *
4609 * After this routine is called on a poll event, the event it ready to be
4610 * placed in an event array to be passed to k_poll().
4611 *
4612 * @param event The event to initialize.
4613 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4614 * values. Only values that apply to the same object being polled
4615 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4616 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004617 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004618 * @param obj Kernel object or poll signal.
4619 *
4620 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004621 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004622 */
4623
Kumar Galacc334c72017-04-21 10:55:34 -05004624extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004625 int mode, void *obj);
4626
4627/**
4628 * @brief Wait for one or many of multiple poll events to occur
4629 *
4630 * This routine allows a thread to wait concurrently for one or many of
4631 * multiple poll events to have occurred. Such events can be a kernel object
4632 * being available, like a semaphore, or a poll signal event.
4633 *
4634 * When an event notifies that a kernel object is available, the kernel object
4635 * is not "given" to the thread calling k_poll(): it merely signals the fact
4636 * that the object was available when the k_poll() call was in effect. Also,
4637 * all threads trying to acquire an object the regular way, i.e. by pending on
4638 * the object, have precedence over the thread polling on the object. This
4639 * means that the polling thread will never get the poll event on an object
4640 * until the object becomes available and its pend queue is empty. For this
4641 * reason, the k_poll() call is more effective when the objects being polled
4642 * only have one thread, the polling thread, trying to acquire them.
4643 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004644 * When k_poll() returns 0, the caller should loop on all the events that were
4645 * passed to k_poll() and check the state field for the values that were
4646 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004647 *
4648 * Before being reused for another call to k_poll(), the user has to reset the
4649 * state field to K_POLL_STATE_NOT_READY.
4650 *
Andrew Boie3772f772018-05-07 16:52:57 -07004651 * When called from user mode, a temporary memory allocation is required from
4652 * the caller's resource pool.
4653 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004654 * @param events An array of pointers to events to be polled for.
4655 * @param num_events The number of events in the array.
4656 * @param timeout Waiting period for an event to be ready (in milliseconds),
4657 * or one of the special values K_NO_WAIT and K_FOREVER.
4658 *
4659 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004660 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004661 * @retval -EINTR Polling has been interrupted, e.g. with
4662 * k_queue_cancel_wait(). All output events are still set and valid,
4663 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4664 * words, -EINTR status means that at least one of output events is
4665 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004666 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4667 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004668 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004669 */
4670
Andrew Boie3772f772018-05-07 16:52:57 -07004671__syscall int k_poll(struct k_poll_event *events, int num_events,
4672 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004673
4674/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004675 * @brief Initialize a poll signal object.
4676 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004677 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004678 *
4679 * @param signal A poll signal.
4680 *
4681 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004682 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004683 */
4684
Andrew Boie3772f772018-05-07 16:52:57 -07004685__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4686
4687/*
4688 * @brief Reset a poll signal object's state to unsignaled.
4689 *
4690 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004691 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004692 */
4693__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4694
Patrik Flykt4344e272019-03-08 14:19:05 -07004695static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004696{
Patrik Flykt24d71432019-03-26 19:57:45 -06004697 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004698}
4699
4700/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004701 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004702 *
4703 * @param signal A poll signal object
4704 * @param signaled An integer buffer which will be written nonzero if the
4705 * object was signaled
4706 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004707 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004708 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004709 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004710 */
4711__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4712 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004713
4714/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004715 * @brief Signal a poll signal object.
4716 *
4717 * This routine makes ready a poll signal, which is basically a poll event of
4718 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4719 * made ready to run. A @a result value can be specified.
4720 *
4721 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004722 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004723 * k_poll_signal_reset(). It thus has to be reset by the user before being
4724 * passed again to k_poll() or k_poll() will consider it being signaled, and
4725 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004726 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004727 * @note The result is stored and the 'signaled' field is set even if
4728 * this function returns an error indicating that an expiring poll was
4729 * not notified. The next k_poll() will detect the missed raise.
4730 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004731 * @param signal A poll signal.
4732 * @param result The value to store in the result field of the signal.
4733 *
4734 * @retval 0 The signal was delivered successfully.
4735 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004736 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004737 */
4738
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004739__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004740
Anas Nashif954d5502018-02-25 08:37:28 -06004741/**
4742 * @internal
4743 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004744extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004745
Anas Nashif166f5192018-02-25 08:02:36 -06004746/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004747
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004748/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004749 * @defgroup cpu_idle_apis CPU Idling APIs
4750 * @ingroup kernel_apis
4751 * @{
4752 */
Anas Nashif30c3cff2019-01-22 08:18:13 -05004753/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004754 * @brief Make the CPU idle.
4755 *
4756 * This function makes the CPU idle until an event wakes it up.
4757 *
4758 * In a regular system, the idle thread should be the only thread responsible
4759 * for making the CPU idle and triggering any type of power management.
4760 * However, in some more constrained systems, such as a single-threaded system,
4761 * the only thread would be responsible for this if needed.
4762 *
4763 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004764 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004765 */
Andrew Boie07525a32019-09-21 16:17:23 -07004766static inline void k_cpu_idle(void)
4767{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004768 arch_cpu_idle();
Andrew Boie07525a32019-09-21 16:17:23 -07004769}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004770
4771/**
4772 * @brief Make the CPU idle in an atomic fashion.
4773 *
4774 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4775 * must be done atomically before making the CPU idle.
4776 *
4777 * @param key Interrupt locking key obtained from irq_lock().
4778 *
4779 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004780 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004781 */
Andrew Boie07525a32019-09-21 16:17:23 -07004782static inline void k_cpu_atomic_idle(unsigned int key)
4783{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004784 arch_cpu_atomic_idle(key);
Andrew Boie07525a32019-09-21 16:17:23 -07004785}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004786
Anas Nashif30c3cff2019-01-22 08:18:13 -05004787/**
4788 * @}
4789 */
Anas Nashif954d5502018-02-25 08:37:28 -06004790
4791/**
4792 * @internal
4793 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004794extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004795
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004796#ifdef ARCH_EXCEPT
Ioannis Glaropoulosdf029232019-10-07 11:24:36 +02004797/* This architecture has direct support for triggering a CPU exception */
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004798#define z_except_reason(reason) ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004799#else
4800
Andrew Boiecdb94d62017-04-18 15:22:05 -07004801/* NOTE: This is the implementation for arches that do not implement
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004802 * ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004803 *
4804 * We won't have a real exception frame to determine the PC value when
4805 * the oops occurred, so print file and line number before we jump into
4806 * the fatal error handler.
4807 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004808#define z_except_reason(reason) do { \
Andrew Boiecdb94d62017-04-18 15:22:05 -07004809 printk("@ %s:%d:\n", __FILE__, __LINE__); \
Andrew Boie56236372019-07-15 15:22:29 -07004810 z_fatal_error(reason, NULL); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004811 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004812
4813#endif /* _ARCH__EXCEPT */
4814
4815/**
4816 * @brief Fatally terminate a thread
4817 *
4818 * This should be called when a thread has encountered an unrecoverable
4819 * runtime condition and needs to terminate. What this ultimately
4820 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004821 * will be called will reason code K_ERR_KERNEL_OOPS.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004822 *
4823 * If this is called from ISR context, the default system fatal error handler
4824 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004825 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004826 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004827#define k_oops() z_except_reason(K_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004828
4829/**
4830 * @brief Fatally terminate the system
4831 *
4832 * This should be called when the Zephyr kernel has encountered an
4833 * unrecoverable runtime condition and needs to terminate. What this ultimately
4834 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004835 * will be called will reason code K_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004836 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004837 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004838#define k_panic() z_except_reason(K_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004839
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004840/*
4841 * private APIs that are utilized by one or more public APIs
4842 */
4843
Stephanos Ioannidis2d746042019-10-25 00:08:21 +09004844/**
4845 * @internal
4846 */
4847extern void z_init_thread_base(struct _thread_base *thread_base,
4848 int priority, u32_t initial_state,
4849 unsigned int options);
4850
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004851#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004852/**
4853 * @internal
4854 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004855extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004856#else
Anas Nashif954d5502018-02-25 08:37:28 -06004857/**
4858 * @internal
4859 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004860#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004861#endif
4862
Anas Nashif954d5502018-02-25 08:37:28 -06004863/**
4864 * @internal
4865 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004866extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004867/**
4868 * @internal
4869 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004870extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004871
Andrew Boiedc5d9352017-06-02 12:56:47 -07004872/* arch/cpu.h may declare an architecture or platform-specific macro
4873 * for properly declaring stacks, compatible with MMU/MPU constraints if
4874 * enabled
4875 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004876
4877/**
4878 * @brief Obtain an extern reference to a stack
4879 *
4880 * This macro properly brings the symbol of a thread stack declared
4881 * elsewhere into scope.
4882 *
4883 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004884 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004885 */
4886#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4887
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004888#ifdef ARCH_THREAD_STACK_DEFINE
4889#define K_THREAD_STACK_DEFINE(sym, size) ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004890#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004891 ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4892#define K_THREAD_STACK_LEN(size) ARCH_THREAD_STACK_LEN(size)
4893#define K_THREAD_STACK_MEMBER(sym, size) ARCH_THREAD_STACK_MEMBER(sym, size)
4894#define K_THREAD_STACK_SIZEOF(sym) ARCH_THREAD_STACK_SIZEOF(sym)
4895#define K_THREAD_STACK_RESERVED ARCH_THREAD_STACK_RESERVED
Andrew Boie4e5c0932019-04-04 12:05:28 -07004896static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004897{
Andrew Boie4f77c2a2019-11-07 12:43:29 -08004898 return ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07004899}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004900#else
4901/**
4902 * @brief Declare a toplevel thread stack memory region
4903 *
4904 * This declares a region of memory suitable for use as a thread's stack.
4905 *
4906 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4907 * 'noinit' section so that it isn't zeroed at boot
4908 *
Andrew Boie507852a2017-07-25 18:47:07 -07004909 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004910 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07004911 * inside needs to be examined, examine thread->stack_info for the associated
4912 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004913 *
4914 * It is legal to precede this definition with the 'static' keyword.
4915 *
4916 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4917 * parameter of k_thread_create(), it may not be the same as the
4918 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4919 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004920 * Some arches may round the size of the usable stack region up to satisfy
4921 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4922 * size.
4923 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004924 * @param sym Thread stack symbol name
4925 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004926 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004927 */
4928#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004929 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004930
4931/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304932 * @brief Calculate size of stacks to be allocated in a stack array
4933 *
4934 * This macro calculates the size to be allocated for the stacks
4935 * inside a stack array. It accepts the indicated "size" as a parameter
4936 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4937 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4938 *
4939 * @param size Size of the stack memory region
4940 * @req K-TSTACK-001
4941 */
4942#define K_THREAD_STACK_LEN(size) (size)
4943
4944/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004945 * @brief Declare a toplevel array of thread stack memory regions
4946 *
4947 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4948 * definition for additional details and constraints.
4949 *
4950 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4951 * 'noinit' section so that it isn't zeroed at boot
4952 *
4953 * @param sym Thread stack symbol name
4954 * @param nmemb Number of stacks to declare
4955 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004956 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004957 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004958#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004959 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304960 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004961
4962/**
4963 * @brief Declare an embedded stack memory region
4964 *
4965 * Used for stacks embedded within other data structures. Use is highly
4966 * discouraged but in some cases necessary. For memory protection scenarios,
4967 * it is very important that any RAM preceding this member not be writable
4968 * by threads else a stack overflow will lead to silent corruption. In other
4969 * words, the containing data structure should live in RAM owned by the kernel.
4970 *
4971 * @param sym Thread stack symbol name
4972 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004973 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004974 */
4975#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004976 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004977
4978/**
4979 * @brief Return the size in bytes of a stack memory region
4980 *
4981 * Convenience macro for passing the desired stack size to k_thread_create()
4982 * since the underlying implementation may actually create something larger
4983 * (for instance a guard area).
4984 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004985 * The value returned here is not guaranteed to match the 'size' parameter
4986 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004987 *
4988 * @param sym Stack memory symbol
4989 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004990 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004991 */
4992#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4993
Andrew Boie575abc02019-03-19 10:42:24 -07004994
4995/**
4996 * @brief Indicate how much additional memory is reserved for stack objects
4997 *
4998 * Any given stack declaration may have additional memory in it for guard
4999 * areas or supervisor mode stacks. This macro indicates how much space
5000 * is reserved for this. The memory reserved may not be contiguous within
5001 * the stack object, and does not account for additional space used due to
5002 * enforce alignment.
5003 */
5004#define K_THREAD_STACK_RESERVED 0
5005
Andrew Boiedc5d9352017-06-02 12:56:47 -07005006/**
5007 * @brief Get a pointer to the physical stack buffer
5008 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07005009 * This macro is deprecated. If a stack buffer needs to be examined, the
5010 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005011 *
5012 * @param sym Declared stack symbol name
5013 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005014 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005015 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07005016static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07005017{
5018 return (char *)sym;
5019}
Andrew Boiedc5d9352017-06-02 12:56:47 -07005020
5021#endif /* _ARCH_DECLARE_STACK */
5022
Chunlin Hane9c97022017-07-07 20:29:30 +08005023/**
5024 * @defgroup mem_domain_apis Memory domain APIs
5025 * @ingroup kernel_apis
5026 * @{
5027 */
5028
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005029/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005030 * @def K_MEM_PARTITION_DEFINE
5031 * @brief Used to declare a memory partition
5032 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005033 */
5034#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
5035#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
5036 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08005037 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005038 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005039#else
5040#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08005041 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005042 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005043#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
5044
Chunlin Hane9c97022017-07-07 20:29:30 +08005045/* memory partition */
5046struct k_mem_partition {
5047 /* start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005048 uintptr_t start;
Chunlin Hane9c97022017-07-07 20:29:30 +08005049 /* size of memory partition */
5050 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005051#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08005052 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305053 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005054#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08005055};
5056
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02005057/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05305058 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005059struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305060#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08005061 /* partitions in the domain */
5062 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305063#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08005064 /* domain q */
5065 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08005066 /* number of partitions in the domain */
5067 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08005068};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305069
Chunlin Hane9c97022017-07-07 20:29:30 +08005070
5071/**
5072 * @brief Initialize a memory domain.
5073 *
5074 * Initialize a memory domain with given name and memory partitions.
5075 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005076 * See documentation for k_mem_domain_add_partition() for details about
5077 * partition constraints.
5078 *
Chunlin Hane9c97022017-07-07 20:29:30 +08005079 * @param domain The memory domain to be initialized.
5080 * @param num_parts The number of array items of "parts" parameter.
5081 * @param parts An array of pointers to the memory partitions. Can be NULL
5082 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005083 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005084 */
Leandro Pereira08de6582018-02-28 14:22:57 -08005085extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08005086 struct k_mem_partition *parts[]);
5087/**
5088 * @brief Destroy a memory domain.
5089 *
5090 * Destroy a memory domain.
5091 *
5092 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005093 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005094 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005095extern void k_mem_domain_destroy(struct k_mem_domain *domain);
5096
5097/**
5098 * @brief Add a memory partition into a memory domain.
5099 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005100 * Add a memory partition into a memory domain. Partitions must conform to
5101 * the following constraints:
5102 *
5103 * - Partition bounds must be within system RAM boundaries on MMU-based
5104 * systems.
5105 * - Partitions in the same memory domain may not overlap each other.
5106 * - Partitions must not be defined which expose private kernel
5107 * data structures or kernel objects.
5108 * - The starting address alignment, and the partition size must conform to
5109 * the constraints of the underlying memory management hardware, which
5110 * varies per architecture.
5111 * - Memory domain partitions are only intended to control access to memory
5112 * from user mode threads.
5113 *
5114 * Violating these constraints may lead to CPU exceptions or undefined
5115 * behavior.
Chunlin Hane9c97022017-07-07 20:29:30 +08005116 *
5117 * @param domain The memory domain to be added a memory partition.
5118 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005119 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005120 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005121extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
5122 struct k_mem_partition *part);
5123
5124/**
5125 * @brief Remove a memory partition from a memory domain.
5126 *
5127 * Remove a memory partition from a memory domain.
5128 *
5129 * @param domain The memory domain to be removed a memory partition.
5130 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005131 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005132 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005133extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
5134 struct k_mem_partition *part);
5135
5136/**
5137 * @brief Add a thread into a memory domain.
5138 *
5139 * Add a thread into a memory domain.
5140 *
5141 * @param domain The memory domain that the thread is going to be added into.
5142 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005143 *
5144 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005145 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005146extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
5147 k_tid_t thread);
5148
5149/**
5150 * @brief Remove a thread from its memory domain.
5151 *
5152 * Remove a thread from its memory domain.
5153 *
5154 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005155 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005156 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005157extern void k_mem_domain_remove_thread(k_tid_t thread);
5158
Anas Nashif166f5192018-02-25 08:02:36 -06005159/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08005160
Andrew Boie756f9072017-10-10 16:01:49 -07005161/**
5162 * @brief Emit a character buffer to the console device
5163 *
5164 * @param c String of characters to print
5165 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005166 *
5167 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07005168 */
5169__syscall void k_str_out(char *c, size_t n);
5170
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005171/**
5172 * @brief Disable preservation of floating point context information.
5173 *
5174 * This routine informs the kernel that the specified thread
5175 * will no longer be using the floating point registers.
5176 *
5177 * @warning
5178 * Some architectures apply restrictions on how the disabling of floating
Andrew Boie4f77c2a2019-11-07 12:43:29 -08005179 * point preservation may be requested, see arch_float_disable.
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005180 *
5181 * @warning
5182 * This routine should only be used to disable floating point support for
5183 * a thread that currently has such support enabled.
5184 *
5185 * @param thread ID of thread.
5186 *
5187 * @retval 0 On success.
5188 * @retval -ENOSYS If the floating point disabling is not implemented.
5189 * -EINVAL If the floating point disabling could not be performed.
5190 */
5191__syscall int k_float_disable(struct k_thread *thread);
5192
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005193#ifdef __cplusplus
5194}
5195#endif
5196
Anas Nashif10291a02019-06-25 12:25:12 -04005197#include <debug/tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07005198#include <syscalls/kernel.h>
5199
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05005200#endif /* !_ASMLANGUAGE */
5201
Flavio Ceolin67ca1762018-09-14 10:43:44 -07005202#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */