blob: 32fbdbd8ebfe6823b1ab5456b8c7a401cb1ac628 [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
Patrik Flykt7c0a2452019-03-14 09:20:46 -0600582 /** Context handle returned via z_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
1333 * @param size Destinatiomn buffer size
1334 * @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 *
1364 * This macro generates a timeout delay that that instructs a kernel API
1365 * 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 *
1374 * This macro generates a timeout delay that that instructs a kernel API
1375 * 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 *
1386 * This macro generates a timeout delay that that instructs a kernel API
1387 * 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.
1397 *
1398 * This macro generates a timeout delay that that instructs a kernel API
1399 * 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 *
1410 * This macro generates a timeout delay that that instructs a kernel API
1411 * 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 *
1422 * This macro generates a timeout delay that that instructs a kernel API
1423 * 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
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001482#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 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001817#define k_cycle_get_32() z_arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001818
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001819/**
Anas Nashif166f5192018-02-25 08:02:36 -06001820 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001821 */
1822
Allan Stephensc98da842016-11-11 15:45:03 -05001823/**
1824 * @cond INTERNAL_HIDDEN
1825 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001826
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001827struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001828 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001829 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001830 union {
1831 _wait_q_t wait_q;
1832
1833 _POLL_EVENT;
1834 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001835
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001836 _OBJECT_TRACING_NEXT_PTR(k_queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001837};
1838
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001839#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001840 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001841 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Stephanos Ioannidisf628dcd2019-09-11 18:09:49 +09001842 .lock = { }, \
1843 { \
1844 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
1845 _POLL_EVENT_OBJ_INIT(obj) \
1846 }, \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001847 _OBJECT_TRACING_INIT \
1848 }
1849
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001850#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1851
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001852extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1853
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001854/**
1855 * INTERNAL_HIDDEN @endcond
1856 */
1857
1858/**
1859 * @defgroup queue_apis Queue APIs
1860 * @ingroup kernel_apis
1861 * @{
1862 */
1863
1864/**
1865 * @brief Initialize a queue.
1866 *
1867 * This routine initializes a queue object, prior to its first use.
1868 *
1869 * @param queue Address of the queue.
1870 *
1871 * @return N/A
1872 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001873__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001874
1875/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001876 * @brief Cancel waiting on a queue.
1877 *
1878 * This routine causes first thread pending on @a queue, if any, to
1879 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001880 * If the queue is being waited on by k_poll(), it will return with
1881 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1882 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001883 *
1884 * @note Can be called by ISRs.
1885 *
1886 * @param queue Address of the queue.
1887 *
1888 * @return N/A
1889 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001890__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001891
1892/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001893 * @brief Append an element to the end of a queue.
1894 *
1895 * This routine appends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001896 * aligned on a word boundary, and the first word of the item is reserved
1897 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001898 *
1899 * @note Can be called by ISRs.
1900 *
1901 * @param queue Address of the queue.
1902 * @param data Address of the data item.
1903 *
1904 * @return N/A
1905 */
1906extern void k_queue_append(struct k_queue *queue, void *data);
1907
1908/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001909 * @brief Append an element to a queue.
1910 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001911 * This routine appends a data item to @a queue. There is an implicit memory
1912 * allocation to create an additional temporary bookkeeping data structure from
1913 * the calling thread's resource pool, which is automatically freed when the
1914 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001915 *
1916 * @note Can be called by ISRs.
1917 *
1918 * @param queue Address of the queue.
1919 * @param data Address of the data item.
1920 *
1921 * @retval 0 on success
1922 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1923 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301924__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001925
1926/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001927 * @brief Prepend an element to a queue.
1928 *
1929 * This routine prepends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001930 * aligned on a word boundary, and the first word of the item is reserved
1931 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001932 *
1933 * @note Can be called by ISRs.
1934 *
1935 * @param queue Address of the queue.
1936 * @param data Address of the data item.
1937 *
1938 * @return N/A
1939 */
1940extern void k_queue_prepend(struct k_queue *queue, void *data);
1941
1942/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001943 * @brief Prepend an element to a queue.
1944 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001945 * This routine prepends a data item to @a queue. There is an implicit memory
1946 * allocation to create an additional temporary bookkeeping data structure from
1947 * the calling thread's resource pool, which is automatically freed when the
1948 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001949 *
1950 * @note Can be called by ISRs.
1951 *
1952 * @param queue Address of the queue.
1953 * @param data Address of the data item.
1954 *
1955 * @retval 0 on success
1956 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1957 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301958__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001959
1960/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001961 * @brief Inserts an element to a queue.
1962 *
1963 * This routine inserts a data item to @a queue after previous item. A queue
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001964 * data item must be aligned on a word boundary, and the first word of
1965 * the item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001966 *
1967 * @note Can be called by ISRs.
1968 *
1969 * @param queue Address of the queue.
1970 * @param prev Address of the previous data item.
1971 * @param data Address of the data item.
1972 *
1973 * @return N/A
1974 */
1975extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1976
1977/**
1978 * @brief Atomically append a list of elements to a queue.
1979 *
1980 * This routine adds a list of data items to @a queue in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001981 * The data items must be in a singly-linked list, with the first word
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001982 * in each data item pointing to the next data item; the list must be
1983 * NULL-terminated.
1984 *
1985 * @note Can be called by ISRs.
1986 *
1987 * @param queue Address of the queue.
1988 * @param head Pointer to first node in singly-linked list.
1989 * @param tail Pointer to last node in singly-linked list.
1990 *
1991 * @return N/A
1992 */
1993extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1994
1995/**
1996 * @brief Atomically add a list of elements to a queue.
1997 *
1998 * This routine adds a list of data items to @a queue in one operation.
1999 * The data items must be in a singly-linked list implemented using a
2000 * sys_slist_t object. Upon completion, the original list is empty.
2001 *
2002 * @note Can be called by ISRs.
2003 *
2004 * @param queue Address of the queue.
2005 * @param list Pointer to sys_slist_t object.
2006 *
2007 * @return N/A
2008 */
2009extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
2010
2011/**
2012 * @brief Get an element from a queue.
2013 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002014 * This routine removes first data item from @a queue. The first word of the
2015 * data item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002016 *
2017 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2018 *
2019 * @param queue Address of the queue.
2020 * @param timeout Waiting period to obtain a data item (in milliseconds),
2021 * or one of the special values K_NO_WAIT and K_FOREVER.
2022 *
2023 * @return Address of the data item if successful; NULL if returned
2024 * without waiting, or waiting period timed out.
2025 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002026__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002027
2028/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002029 * @brief Remove an element from a queue.
2030 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002031 * This routine removes data item from @a queue. The first word of the
2032 * data item is reserved for the kernel's use. Removing elements from k_queue
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002033 * rely on sys_slist_find_and_remove which is not a constant time operation.
2034 *
2035 * @note Can be called by ISRs
2036 *
2037 * @param queue Address of the queue.
2038 * @param data Address of the data item.
2039 *
2040 * @return true if data item was removed
2041 */
2042static inline bool k_queue_remove(struct k_queue *queue, void *data)
2043{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002044 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002045}
2046
2047/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002048 * @brief Append an element to a queue only if it's not present already.
2049 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002050 * This routine appends data item to @a queue. The first word of the data
2051 * item is reserved for the kernel's use. Appending elements to k_queue
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002052 * relies on sys_slist_is_node_in_list which is not a constant time operation.
2053 *
2054 * @note Can be called by ISRs
2055 *
2056 * @param queue Address of the queue.
2057 * @param data Address of the data item.
2058 *
2059 * @return true if data item was added, false if not
2060 */
2061static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
2062{
2063 sys_sfnode_t *test;
2064
2065 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
2066 if (test == (sys_sfnode_t *) data) {
2067 return false;
2068 }
2069 }
2070
2071 k_queue_append(queue, data);
2072 return true;
2073}
2074
2075/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002076 * @brief Query a queue to see if it has data available.
2077 *
2078 * Note that the data might be already gone by the time this function returns
2079 * if other threads are also trying to read from the queue.
2080 *
2081 * @note Can be called by ISRs.
2082 *
2083 * @param queue Address of the queue.
2084 *
2085 * @return Non-zero if the queue is empty.
2086 * @return 0 if data is available.
2087 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002088__syscall int k_queue_is_empty(struct k_queue *queue);
2089
Patrik Flykt4344e272019-03-08 14:19:05 -07002090static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002091{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002092 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002093}
2094
2095/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002096 * @brief Peek element at the head of queue.
2097 *
2098 * Return element from the head of queue without removing it.
2099 *
2100 * @param queue Address of the queue.
2101 *
2102 * @return Head element, or NULL if queue is empty.
2103 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002104__syscall void *k_queue_peek_head(struct k_queue *queue);
2105
Patrik Flykt4344e272019-03-08 14:19:05 -07002106static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002107{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002108 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002109}
2110
2111/**
2112 * @brief Peek element at the tail of queue.
2113 *
2114 * Return element from the tail of queue without removing it.
2115 *
2116 * @param queue Address of the queue.
2117 *
2118 * @return Tail element, or NULL if queue is empty.
2119 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002120__syscall void *k_queue_peek_tail(struct k_queue *queue);
2121
Patrik Flykt4344e272019-03-08 14:19:05 -07002122static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002123{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002124 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002125}
2126
2127/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002128 * @brief Statically define and initialize a queue.
2129 *
2130 * The queue can be accessed outside the module where it is defined using:
2131 *
2132 * @code extern struct k_queue <name>; @endcode
2133 *
2134 * @param name Name of the queue.
2135 */
2136#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002137 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002138 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002139
Anas Nashif166f5192018-02-25 08:02:36 -06002140/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002141
Wentong Wu5611e922019-06-20 23:51:27 +08002142#ifdef CONFIG_USERSPACE
2143/**
2144 * @brief futex structure
2145 *
2146 * A k_futex is a lightweight mutual exclusion primitive designed
2147 * to minimize kernel involvement. Uncontended operation relies
2148 * only on atomic access to shared memory. k_futex are tracked as
2149 * kernel objects and can live in user memory so any access bypass
2150 * the kernel object permission management mechanism.
2151 */
2152struct k_futex {
2153 atomic_t val;
2154};
2155
2156/**
2157 * @brief futex kernel data structure
2158 *
2159 * z_futex_data are the helper data structure for k_futex to complete
2160 * futex contended operation on kernel side, structure z_futex_data
2161 * of every futex object is invisible in user mode.
2162 */
2163struct z_futex_data {
2164 _wait_q_t wait_q;
2165 struct k_spinlock lock;
2166};
2167
2168#define Z_FUTEX_DATA_INITIALIZER(obj) \
2169 { \
2170 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \
2171 }
2172
2173/**
2174 * @defgroup futex_apis FUTEX APIs
2175 * @ingroup kernel_apis
2176 * @{
2177 */
2178
2179/**
Wentong Wu5611e922019-06-20 23:51:27 +08002180 * @brief Pend the current thread on a futex
2181 *
2182 * Tests that the supplied futex contains the expected value, and if so,
2183 * goes to sleep until some other thread calls k_futex_wake() on it.
2184 *
2185 * @param futex Address of the futex.
2186 * @param expected Expected value of the futex, if it is different the caller
2187 * will not wait on it.
2188 * @param timeout Waiting period on the futex, in milliseconds, or one of the
2189 * special values K_NO_WAIT or K_FOREVER.
2190 * @retval -EACCES Caller does not have read access to futex address.
2191 * @retval -EAGAIN If the futex value did not match the expected parameter.
2192 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2193 * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup.
2194 * @retval 0 if the caller went to sleep and was woken up. The caller
2195 * should check the futex's value on wakeup to determine if it needs
2196 * to block again.
2197 */
2198__syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout);
2199
2200/**
2201 * @brief Wake one/all threads pending on a futex
2202 *
2203 * Wake up the highest priority thread pending on the supplied futex, or
2204 * wakeup all the threads pending on the supplied futex, and the behavior
2205 * depends on wake_all.
2206 *
2207 * @param futex Futex to wake up pending threads.
2208 * @param wake_all If true, wake up all pending threads; If false,
2209 * wakeup the highest priority thread.
2210 * @retval -EACCES Caller does not have access to the futex address.
2211 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2212 * @retval Number of threads that were woken up.
2213 */
2214__syscall int k_futex_wake(struct k_futex *futex, bool wake_all);
2215
2216/** @} */
2217#endif
2218
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002219struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002220 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002221};
2222
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002223/**
2224 * @cond INTERNAL_HIDDEN
2225 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002226#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002227 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002228 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002229 }
2230
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002231#define K_FIFO_INITIALIZER DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002232
Allan Stephensc98da842016-11-11 15:45:03 -05002233/**
2234 * INTERNAL_HIDDEN @endcond
2235 */
2236
2237/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002238 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002239 * @ingroup kernel_apis
2240 * @{
2241 */
2242
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002243/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002244 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002245 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002246 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002247 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002248 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002249 *
2250 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002251 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002252 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002253#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002254 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002255
2256/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002257 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002258 *
2259 * This routine causes first thread pending on @a fifo, if any, to
2260 * return from k_fifo_get() call with NULL value (as if timeout
2261 * expired).
2262 *
2263 * @note Can be called by ISRs.
2264 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002265 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002266 *
2267 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002268 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002269 */
2270#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002271 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002272
2273/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002274 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002275 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002276 * This routine adds a data item to @a fifo. A FIFO data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002277 * aligned on a word boundary, and the first word of the item is reserved
2278 * for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002279 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002280 * @note Can be called by ISRs.
2281 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002282 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002283 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002284 *
2285 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002286 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002287 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002288#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002289 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002290
2291/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002292 * @brief Add an element to a FIFO queue.
2293 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002294 * This routine adds a data item to @a fifo. There is an implicit memory
2295 * allocation to create an additional temporary bookkeeping data structure from
2296 * the calling thread's resource pool, which is automatically freed when the
2297 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002298 *
2299 * @note Can be called by ISRs.
2300 *
2301 * @param fifo Address of the FIFO.
2302 * @param data Address of the data item.
2303 *
2304 * @retval 0 on success
2305 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002306 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002307 */
2308#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002309 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002310
2311/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002312 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002313 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002314 * This routine adds a list of data items to @a fifo in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002315 * The data items must be in a singly-linked list, with the first word of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002316 * each data item pointing to the next data item; the list must be
2317 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002318 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002319 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002320 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002321 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002322 * @param head Pointer to first node in singly-linked list.
2323 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002324 *
2325 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002326 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002327 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002328#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002329 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002330
2331/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002332 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002333 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002334 * This routine adds a list of data items to @a fifo in one operation.
2335 * The data items must be in a singly-linked list implemented using a
2336 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002337 * and must be re-initialized via sys_slist_init().
2338 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002339 * @note Can be called by ISRs.
2340 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002341 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002342 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002343 *
2344 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002345 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002346 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002347#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002348 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002349
2350/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002351 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002353 * This routine removes a data item from @a fifo in a "first in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002354 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002355 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002356 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2357 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002358 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002359 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002360 * or one of the special values K_NO_WAIT and K_FOREVER.
2361 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002362 * @return Address of the data item if successful; NULL if returned
2363 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002364 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002365 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002366#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002367 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002368
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002369/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002370 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002371 *
2372 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002373 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002374 *
2375 * @note Can be called by ISRs.
2376 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002377 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002378 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002379 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002380 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002381 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002382 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002383#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002384 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002385
2386/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002387 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002388 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002389 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302390 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002391 * on each iteration of processing, a head container will be peeked,
2392 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002393 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002394 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002395 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002396 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002397 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002398 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002399 */
2400#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002401 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002402
2403/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002404 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002405 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002406 * Return element from the tail of FIFO queue (without removing it). A usecase
2407 * for this is if elements of the FIFO queue are themselves containers. Then
2408 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002409 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002410 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002411 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002412 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002413 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002414 */
2415#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002416 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002417
2418/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002419 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002420 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002421 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002422 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002423 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002424 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002425 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002426 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002427 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002428#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002429 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002430 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002431
Anas Nashif166f5192018-02-25 08:02:36 -06002432/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002433
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002434struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002435 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002436};
2437
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002438/**
2439 * @cond INTERNAL_HIDDEN
2440 */
2441
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002442#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002443 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002444 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002445 }
2446
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002447#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2448
Allan Stephensc98da842016-11-11 15:45:03 -05002449/**
2450 * INTERNAL_HIDDEN @endcond
2451 */
2452
2453/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002454 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002455 * @ingroup kernel_apis
2456 * @{
2457 */
2458
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002459/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002460 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002461 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002462 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002463 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002464 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002465 *
2466 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002467 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002468 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002469#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002470 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002471
2472/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002473 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002474 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002475 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002476 * aligned on a word boundary, and the first word of the item is
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002477 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002479 * @note Can be called by ISRs.
2480 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002481 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002482 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002483 *
2484 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002485 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002486 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002487#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002488 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002489
2490/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002491 * @brief Add an element to a LIFO queue.
2492 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002493 * This routine adds a data item to @a lifo. There is an implicit memory
2494 * allocation to create an additional temporary bookkeeping data structure from
2495 * the calling thread's resource pool, which is automatically freed when the
2496 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002497 *
2498 * @note Can be called by ISRs.
2499 *
2500 * @param lifo Address of the LIFO.
2501 * @param data Address of the data item.
2502 *
2503 * @retval 0 on success
2504 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002505 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002506 */
2507#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002508 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002509
2510/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002511 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002512 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002513 * This routine removes a data item from @a lifo in a "last in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002514 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002515 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002516 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2517 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002518 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002519 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002520 * or one of the special values K_NO_WAIT and K_FOREVER.
2521 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002522 * @return Address of the data item if successful; NULL if returned
2523 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002524 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002525 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002526#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002527 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002528
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002529/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002530 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002531 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002532 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002533 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002534 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002536 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002537 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002538 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002539#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002540 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002541 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002542
Anas Nashif166f5192018-02-25 08:02:36 -06002543/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002544
2545/**
2546 * @cond INTERNAL_HIDDEN
2547 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302548#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002549
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002550typedef uintptr_t stack_data_t;
2551
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002552struct k_stack {
2553 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002554 struct k_spinlock lock;
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002555 stack_data_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002557 _OBJECT_TRACING_NEXT_PTR(k_stack)
Andrew Boief3bee952018-05-02 17:44:39 -07002558 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002559};
2560
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002561#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002562 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002563 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002564 .base = stack_buffer, \
2565 .next = stack_buffer, \
2566 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002567 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002568 }
2569
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002570#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2571
Allan Stephensc98da842016-11-11 15:45:03 -05002572/**
2573 * INTERNAL_HIDDEN @endcond
2574 */
2575
2576/**
2577 * @defgroup stack_apis Stack APIs
2578 * @ingroup kernel_apis
2579 * @{
2580 */
2581
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002582/**
2583 * @brief Initialize a stack.
2584 *
2585 * This routine initializes a stack object, prior to its first use.
2586 *
2587 * @param stack Address of the stack.
2588 * @param buffer Address of array used to hold stacked values.
2589 * @param num_entries Maximum number of values that can be stacked.
2590 *
2591 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002592 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002593 */
Andrew Boief3bee952018-05-02 17:44:39 -07002594void k_stack_init(struct k_stack *stack,
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002595 stack_data_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002596
2597
2598/**
2599 * @brief Initialize a stack.
2600 *
2601 * This routine initializes a stack object, prior to its first use. Internal
2602 * buffers will be allocated from the calling thread's resource pool.
2603 * This memory will be released if k_stack_cleanup() is called, or
2604 * userspace is enabled and the stack object loses all references to it.
2605 *
2606 * @param stack Address of the stack.
2607 * @param num_entries Maximum number of values that can be stacked.
2608 *
2609 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002610 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002611 */
2612
Adithya Baglody28080d32018-10-15 11:48:51 +05302613__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2614 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002615
2616/**
2617 * @brief Release a stack's allocated buffer
2618 *
2619 * If a stack object was given a dynamically allocated buffer via
2620 * k_stack_alloc_init(), this will free it. This function does nothing
2621 * if the buffer wasn't dynamically allocated.
2622 *
2623 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002624 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002625 */
2626void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002627
2628/**
2629 * @brief Push an element onto a stack.
2630 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002631 * This routine adds a stack_data_t value @a data to @a stack.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002632 *
2633 * @note Can be called by ISRs.
2634 *
2635 * @param stack Address of the stack.
2636 * @param data Value to push onto the stack.
2637 *
2638 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002639 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002640 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002641__syscall void k_stack_push(struct k_stack *stack, stack_data_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002642
2643/**
2644 * @brief Pop an element from a stack.
2645 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002646 * This routine removes a stack_data_t value from @a stack in a "last in,
2647 * first out" manner and stores the value in @a data.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002648 *
2649 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2650 *
2651 * @param stack Address of the stack.
2652 * @param data Address of area to hold the value popped from the stack.
2653 * @param timeout Waiting period to obtain a value (in milliseconds),
2654 * or one of the special values K_NO_WAIT and K_FOREVER.
2655 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002656 * @retval 0 Element popped from stack.
2657 * @retval -EBUSY Returned without waiting.
2658 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002659 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002660 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002661__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002662
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002663/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002664 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002665 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002666 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002667 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002668 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002670 * @param name Name of the stack.
2671 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002672 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002673 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002674#define K_STACK_DEFINE(name, stack_num_entries) \
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002675 stack_data_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002676 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002677 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002678 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002679 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002680
Anas Nashif166f5192018-02-25 08:02:36 -06002681/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002682
Allan Stephens6bba9b02016-11-16 14:56:54 -05002683struct k_work;
Piotr Zięcik19d83492019-09-27 09:16:25 +02002684struct k_work_poll;
Allan Stephens6bba9b02016-11-16 14:56:54 -05002685
Piotr Zięcik19d83492019-09-27 09:16:25 +02002686/* private, used by k_poll and k_work_poll */
Piotr Zięcik1c4177d2019-08-27 12:19:26 +02002687typedef int (*_poller_cb_t)(struct k_poll_event *event, u32_t state);
2688struct _poller {
2689 volatile bool is_polling;
2690 struct k_thread *thread;
2691 _poller_cb_t cb;
2692};
2693
Allan Stephensc98da842016-11-11 15:45:03 -05002694/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002695 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002696 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002697 */
2698
Allan Stephens6bba9b02016-11-16 14:56:54 -05002699/**
2700 * @typedef k_work_handler_t
2701 * @brief Work item handler function type.
2702 *
2703 * A work item's handler function is executed by a workqueue's thread
2704 * when the work item is processed by the workqueue.
2705 *
2706 * @param work Address of the work item.
2707 *
2708 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002709 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002710 */
2711typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002712
2713/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002714 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002715 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002716
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002717struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002718 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002719 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002720};
2721
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002722enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002723 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002724};
2725
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002726struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002727 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002728 k_work_handler_t handler;
2729 atomic_t flags[1];
2730};
2731
Allan Stephens6bba9b02016-11-16 14:56:54 -05002732struct k_delayed_work {
2733 struct k_work work;
2734 struct _timeout timeout;
2735 struct k_work_q *work_q;
2736};
2737
Piotr Zięcik19d83492019-09-27 09:16:25 +02002738struct k_work_poll {
2739 struct k_work work;
2740 struct _poller poller;
2741 struct k_poll_event *events;
2742 int num_events;
2743 k_work_handler_t real_handler;
2744 struct _timeout timeout;
2745 int poll_result;
2746};
2747
Allan Stephens6bba9b02016-11-16 14:56:54 -05002748extern struct k_work_q k_sys_work_q;
2749
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002750/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002751 * INTERNAL_HIDDEN @endcond
2752 */
2753
Patrik Flykt4344e272019-03-08 14:19:05 -07002754#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002755 { \
2756 ._reserved = NULL, \
2757 .handler = work_handler, \
2758 .flags = { 0 } \
2759 }
2760
Patrik Flykt4344e272019-03-08 14:19:05 -07002761#define K_WORK_INITIALIZER DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002762
Allan Stephens6bba9b02016-11-16 14:56:54 -05002763/**
2764 * @brief Initialize a statically-defined work item.
2765 *
2766 * This macro can be used to initialize a statically-defined workqueue work
2767 * item, prior to its first use. For example,
2768 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002769 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002770 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002771 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002772 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002773 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002774 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002775#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002776 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002777
2778/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002779 * @brief Initialize a work item.
2780 *
2781 * This routine initializes a workqueue work item, prior to its first use.
2782 *
2783 * @param work Address of work item.
2784 * @param handler Function to invoke each time work item is processed.
2785 *
2786 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002787 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002788 */
2789static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2790{
Patrik Flykt4344e272019-03-08 14:19:05 -07002791 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002792}
2793
2794/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002795 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002796 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002797 * This routine submits work item @a work to be processed by workqueue
2798 * @a work_q. If the work item is already pending in the workqueue's queue
2799 * as a result of an earlier submission, this routine has no effect on the
2800 * work item. If the work item has already been processed, or is currently
2801 * being processed, its work is considered complete and the work item can be
2802 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002803 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002804 * @warning
2805 * A submitted work item must not be modified until it has been processed
2806 * by the workqueue.
2807 *
2808 * @note Can be called by ISRs.
2809 *
2810 * @param work_q Address of workqueue.
2811 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002812 *
2813 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002814 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002815 */
2816static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2817 struct k_work *work)
2818{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002819 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002820 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002821 }
2822}
2823
2824/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002825 * @brief Submit a work item to a user mode workqueue
2826 *
David B. Kinder06d78352018-12-17 14:32:40 -08002827 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002828 * memory allocation is made from the caller's resource pool which is freed
2829 * once the worker thread consumes the k_work item. The workqueue
2830 * thread must have memory access to the k_work item being submitted. The caller
2831 * must have permission granted on the work_q parameter's queue object.
2832 *
2833 * Otherwise this works the same as k_work_submit_to_queue().
2834 *
2835 * @note Can be called by ISRs.
2836 *
2837 * @param work_q Address of workqueue.
2838 * @param work Address of work item.
2839 *
2840 * @retval -EBUSY if the work item was already in some workqueue
2841 * @retval -ENOMEM if no memory for thread resource pool allocation
2842 * @retval 0 Success
2843 * @req K-WORK-001
2844 */
2845static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2846 struct k_work *work)
2847{
2848 int ret = -EBUSY;
2849
2850 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2851 ret = k_queue_alloc_append(&work_q->queue, work);
2852
2853 /* Couldn't insert into the queue. Clear the pending bit
2854 * so the work item can be submitted again
2855 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002856 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002857 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2858 }
2859 }
2860
2861 return ret;
2862}
2863
2864/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002865 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002866 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002867 * This routine indicates if work item @a work is pending in a workqueue's
2868 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002869 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002870 * @note Can be called by ISRs.
2871 *
2872 * @param work Address of work item.
2873 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002874 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002875 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002876 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002877static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002878{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002879 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002880}
2881
2882/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002883 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002884 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002885 * This routine starts workqueue @a work_q. The workqueue spawns its work
2886 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002887 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002888 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002889 * @param stack Pointer to work queue thread's stack space, as defined by
2890 * K_THREAD_STACK_DEFINE()
2891 * @param stack_size Size of the work queue thread's stack (in bytes), which
2892 * should either be the same constant passed to
2893 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002894 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002895 *
2896 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002897 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002898 */
Andrew Boie507852a2017-07-25 18:47:07 -07002899extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002900 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002901 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002902
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002903/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002904 * @brief Start a workqueue in user mode
2905 *
2906 * This works identically to k_work_q_start() except it is callable from user
2907 * mode, and the worker thread created will run in user mode.
2908 * The caller must have permissions granted on both the work_q parameter's
2909 * thread and queue objects, and the same restrictions on priority apply as
2910 * k_thread_create().
2911 *
2912 * @param work_q Address of workqueue.
2913 * @param stack Pointer to work queue thread's stack space, as defined by
2914 * K_THREAD_STACK_DEFINE()
2915 * @param stack_size Size of the work queue thread's stack (in bytes), which
2916 * should either be the same constant passed to
2917 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2918 * @param prio Priority of the work queue's thread.
2919 *
2920 * @return N/A
2921 * @req K-WORK-001
2922 */
2923extern void k_work_q_user_start(struct k_work_q *work_q,
2924 k_thread_stack_t *stack,
2925 size_t stack_size, int prio);
2926
2927/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002928 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002929 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002930 * This routine initializes a workqueue delayed work item, prior to
2931 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002932 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002933 * @param work Address of delayed work item.
2934 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002935 *
2936 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002937 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002938 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002939extern void k_delayed_work_init(struct k_delayed_work *work,
2940 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002941
2942/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002943 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002944 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002945 * This routine schedules work item @a work to be processed by workqueue
2946 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002947 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002948 * Only when the countdown completes is the work item actually submitted to
2949 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002950 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002951 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002952 * counting down cancels the existing submission and restarts the
2953 * countdown using the new delay. Note that this behavior is
2954 * inherently subject to race conditions with the pre-existing
2955 * timeouts and work queue, so care must be taken to synchronize such
2956 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002957 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002958 * @warning
2959 * A delayed work item must not be modified until it has been processed
2960 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002961 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002962 * @note Can be called by ISRs.
2963 *
2964 * @param work_q Address of workqueue.
2965 * @param work Address of delayed work item.
2966 * @param delay Delay before submitting the work item (in milliseconds).
2967 *
2968 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002969 * @retval -EINVAL Work item is being processed or has completed its work.
2970 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002971 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002972 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002973extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2974 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002975 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002976
2977/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002978 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002979 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002980 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002981 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002982 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002983 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002984 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002985 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08002986 * @note The result of calling this on a k_delayed_work item that has
2987 * not been submitted (i.e. before the return of the
2988 * k_delayed_work_submit_to_queue() call) is undefined.
2989 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002990 * @param work Address of delayed work item.
2991 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002992 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002993 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002994 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002995 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002996extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002997
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002998/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002999 * @brief Submit a work item to the system workqueue.
3000 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003001 * This routine submits work item @a work to be processed by the system
3002 * workqueue. If the work item is already pending in the workqueue's queue
3003 * as a result of an earlier submission, this routine has no effect on the
3004 * work item. If the work item has already been processed, or is currently
3005 * being processed, its work is considered complete and the work item can be
3006 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003007 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003008 * @warning
3009 * Work items submitted to the system workqueue should avoid using handlers
3010 * that block or yield since this may prevent the system workqueue from
3011 * processing other work items in a timely manner.
3012 *
3013 * @note Can be called by ISRs.
3014 *
3015 * @param work Address of work item.
3016 *
3017 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003018 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003019 */
3020static inline void k_work_submit(struct k_work *work)
3021{
3022 k_work_submit_to_queue(&k_sys_work_q, work);
3023}
3024
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003025/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003026 * @brief Submit a delayed work item to the system workqueue.
3027 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003028 * This routine schedules work item @a work to be processed by the system
3029 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003030 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003031 * Only when the countdown completes is the work item actually submitted to
3032 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003033 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003034 * Submitting a previously submitted delayed work item that is still
3035 * counting down cancels the existing submission and restarts the countdown
3036 * using the new delay. If the work item is currently pending on the
3037 * workqueue's queue because the countdown has completed it is too late to
3038 * resubmit the item, and resubmission fails without impacting the work item.
3039 * If the work item has already been processed, or is currently being processed,
3040 * its work is considered complete and the work item can be resubmitted.
3041 *
3042 * @warning
3043 * Work items submitted to the system workqueue should avoid using handlers
3044 * that block or yield since this may prevent the system workqueue from
3045 * processing other work items in a timely manner.
3046 *
3047 * @note Can be called by ISRs.
3048 *
3049 * @param work Address of delayed work item.
3050 * @param delay Delay before submitting the work item (in milliseconds).
3051 *
3052 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003053 * @retval -EINVAL Work item is being processed or has completed its work.
3054 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003055 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003056 */
3057static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003058 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003059{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05003060 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003061}
3062
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003063/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02003064 * @brief Get time remaining before a delayed work gets scheduled.
3065 *
3066 * This routine computes the (approximate) time remaining before a
3067 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02003068 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02003069 *
3070 * @param work Delayed work item.
3071 *
3072 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003073 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02003074 */
Kumar Galacc334c72017-04-21 10:55:34 -05003075static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02003076{
Andy Ross52e444b2018-09-28 09:06:37 -07003077 return __ticks_to_ms(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02003078}
3079
Piotr Zięcik19d83492019-09-27 09:16:25 +02003080/**
3081 * @brief Initialize a triggered work item.
3082 *
3083 * This routine initializes a workqueue triggered work item, prior to
3084 * its first use.
3085 *
3086 * @param work Address of triggered work item.
3087 * @param handler Function to invoke each time work item is processed.
3088 *
3089 * @return N/A
3090 */
3091extern void k_work_poll_init(struct k_work_poll *work,
3092 k_work_handler_t handler);
3093
3094/**
3095 * @brief Submit a triggered work item.
3096 *
3097 * This routine schedules work item @a work to be processed by workqueue
3098 * @a work_q when one of the given @a events is signaled. The routine
3099 * initiates internal poller for the work item and then returns to the caller.
3100 * Only when one of the watched events happen the work item is actually
3101 * submitted to the workqueue and becomes pending.
3102 *
3103 * Submitting a previously submitted triggered work item that is still
3104 * waiting for the event cancels the existing submission and reschedules it
3105 * the using the new event list. Note that this behavior is inherently subject
3106 * to race conditions with the pre-existig triggered work item and work queue,
3107 * so care must be taken to synchronize such resubmissions externally.
3108 *
3109 * @note Can be called by ISRs.
3110 *
3111 * @warning
3112 * Provided array of events as well as a triggered work item must be placed
3113 * in persistent memory (valid until work handler execution or work
3114 * cancellation) and cannot be modified after submission.
3115 *
3116 * @param work_q Address of workqueue.
3117 * @param work Address of delayed work item.
3118 * @param events An array of pointers to events which trigger the work.
3119 * @param num_events The number of events in the array.
3120 * @param timeout Timeout after which the work will be scheduled for
3121 * execution even if not triggered.
3122 *
3123 *
3124 * @retval 0 Work item started watching for events.
3125 * @retval -EINVAL Work item is being processed or has completed its work.
3126 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3127 */
3128extern int k_work_poll_submit_to_queue(struct k_work_q *work_q,
3129 struct k_work_poll *work,
3130 struct k_poll_event *events,
3131 int num_events,
3132 s32_t timeout);
3133
3134/**
3135 * @brief Submit a triggered work item to the system workqueue.
3136 *
3137 * This routine schedules work item @a work to be processed by system
3138 * workqueue when one of the given @a events is signaled. The routine
3139 * initiates internal poller for the work item and then returns to the caller.
3140 * Only when one of the watched events happen the work item is actually
3141 * submitted to the workqueue and becomes pending.
3142 *
3143 * Submitting a previously submitted triggered work item that is still
3144 * waiting for the event cancels the existing submission and reschedules it
3145 * the using the new event list. Note that this behavior is inherently subject
3146 * to race conditions with the pre-existig triggered work item and work queue,
3147 * so care must be taken to synchronize such resubmissions externally.
3148 *
3149 * @note Can be called by ISRs.
3150 *
3151 * @warning
3152 * Provided array of events as well as a triggered work item must not be
3153 * modified until the item has been processed by the workqueue.
3154 *
3155 * @param work Address of delayed work item.
3156 * @param events An array of pointers to events which trigger the work.
3157 * @param num_events The number of events in the array.
3158 * @param timeout Timeout after which the work will be scheduled for
3159 * execution even if not triggered.
3160 *
3161 * @retval 0 Work item started watching for events.
3162 * @retval -EINVAL Work item is being processed or has completed its work.
3163 * @retval -EADDRINUSE Work item is pending on a different workqueue.
3164 */
3165static inline int k_work_poll_submit(struct k_work_poll *work,
3166 struct k_poll_event *events,
3167 int num_events,
3168 s32_t timeout)
3169{
3170 return k_work_poll_submit_to_queue(&k_sys_work_q, work,
3171 events, num_events, timeout);
3172}
3173
3174/**
3175 * @brief Cancel a triggered work item.
3176 *
3177 * This routine cancels the submission of triggered work item @a work.
3178 * A triggered work item can only be canceled if no event triggered work
3179 * submission.
3180 *
3181 * @note Can be called by ISRs.
3182 *
3183 * @param work Address of delayed work item.
3184 *
3185 * @retval 0 Work tiem canceled.
3186 * @retval -EINVAL Work item is being processed or has completed its work.
3187 */
3188extern int k_work_poll_cancel(struct k_work_poll *work);
3189
Anas Nashif166f5192018-02-25 08:02:36 -06003190/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003191/**
Anas Nashifce78d162018-05-24 12:43:11 -05003192 * @defgroup mutex_apis Mutex APIs
3193 * @ingroup kernel_apis
3194 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003195 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003196
Anas Nashifce78d162018-05-24 12:43:11 -05003197/**
3198 * Mutex Structure
3199 * @ingroup mutex_apis
3200 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003201struct k_mutex {
3202 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05003203 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04003204 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05003205 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003206 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003207
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003208 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003209};
3210
Anas Nashifce78d162018-05-24 12:43:11 -05003211/**
3212 * @cond INTERNAL_HIDDEN
3213 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003214#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003215 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003216 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003217 .owner = NULL, \
3218 .lock_count = 0, \
3219 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003220 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003221 }
3222
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003223#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
3224
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003225/**
Allan Stephensc98da842016-11-11 15:45:03 -05003226 * INTERNAL_HIDDEN @endcond
3227 */
3228
3229/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003230 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003231 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003232 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003233 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003234 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003236 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003237 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003238 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003239#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003240 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003241 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003242
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003243/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003244 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003246 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003247 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003248 * Upon completion, the mutex is available and does not have an owner.
3249 *
3250 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003251 *
3252 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003253 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003254 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003255__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003256
3257/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003258 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003259 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003260 * This routine locks @a mutex. If the mutex is locked by another thread,
3261 * the calling thread waits until the mutex becomes available or until
3262 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003264 * A thread is permitted to lock a mutex it has already locked. The operation
3265 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003267 * @param mutex Address of the mutex.
3268 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003269 * or one of the special values K_NO_WAIT and K_FOREVER.
3270 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003271 * @retval 0 Mutex locked.
3272 * @retval -EBUSY Returned without waiting.
3273 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003274 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003275 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003276__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003277
3278/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003279 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003280 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003281 * This routine unlocks @a mutex. The mutex must already be locked by the
3282 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003283 *
3284 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003285 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003286 * thread.
3287 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003288 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003289 *
3290 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003291 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003292 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003293__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003294
Allan Stephensc98da842016-11-11 15:45:03 -05003295/**
Anas Nashif166f5192018-02-25 08:02:36 -06003296 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003297 */
3298
3299/**
3300 * @cond INTERNAL_HIDDEN
3301 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003302
3303struct k_sem {
3304 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303305 u32_t count;
3306 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003307 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003308
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003309 _OBJECT_TRACING_NEXT_PTR(k_sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003310};
3311
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003312#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003313 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003314 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003315 .count = initial_count, \
3316 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003317 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003318 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003319 }
3320
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003321#define K_SEM_INITIALIZER DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003322
Allan Stephensc98da842016-11-11 15:45:03 -05003323/**
3324 * INTERNAL_HIDDEN @endcond
3325 */
3326
3327/**
3328 * @defgroup semaphore_apis Semaphore APIs
3329 * @ingroup kernel_apis
3330 * @{
3331 */
3332
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003333/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003334 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003336 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003337 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003338 * @param sem Address of the semaphore.
3339 * @param initial_count Initial semaphore count.
3340 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003341 *
3342 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003343 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003344 */
Andrew Boie99280232017-09-29 14:17:47 -07003345__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
3346 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003347
3348/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003349 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003351 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003353 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3354 *
3355 * @param sem Address of the semaphore.
3356 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003357 * or one of the special values K_NO_WAIT and K_FOREVER.
3358 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05003359 * @note When porting code from the nanokernel legacy API to the new API, be
3360 * careful with the return value of this function. The return value is the
3361 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
3362 * non-zero means failure, while the nano_sem_take family returns 1 for success
3363 * and 0 for failure.
3364 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003365 * @retval 0 Semaphore taken.
3366 * @retval -EBUSY Returned without waiting.
3367 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003368 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003369 */
Andrew Boie99280232017-09-29 14:17:47 -07003370__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003371
3372/**
3373 * @brief Give a semaphore.
3374 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003375 * This routine gives @a sem, unless the semaphore is already at its maximum
3376 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003377 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003378 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003379 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003380 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003381 *
3382 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003383 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003384 */
Andrew Boie99280232017-09-29 14:17:47 -07003385__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003386
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003387/**
3388 * @brief Reset a semaphore's count to zero.
3389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003390 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003391 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003392 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003393 *
3394 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003395 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003396 */
Andrew Boie990bf162017-10-03 12:36:49 -07003397__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003398
Anas Nashif954d5502018-02-25 08:37:28 -06003399/**
3400 * @internal
3401 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003402static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003403{
Patrik Flykt24d71432019-03-26 19:57:45 -06003404 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003405}
3406
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003407/**
3408 * @brief Get a semaphore's count.
3409 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003410 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003411 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003412 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003413 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003414 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003415 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003416 */
Andrew Boie990bf162017-10-03 12:36:49 -07003417__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003418
Anas Nashif954d5502018-02-25 08:37:28 -06003419/**
3420 * @internal
3421 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003422static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003423{
3424 return sem->count;
3425}
3426
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003427/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003428 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003430 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003431 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003432 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003433 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003434 * @param name Name of the semaphore.
3435 * @param initial_count Initial semaphore count.
3436 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003437 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003438 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003439#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003440 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003441 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303442 BUILD_ASSERT(((count_limit) != 0) && \
3443 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003444
Anas Nashif166f5192018-02-25 08:02:36 -06003445/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003446
3447/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003448 * @defgroup msgq_apis Message Queue APIs
3449 * @ingroup kernel_apis
3450 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003451 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003452
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003453/**
3454 * @brief Message Queue Structure
3455 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003456struct k_msgq {
3457 _wait_q_t wait_q;
Andy Rossbe03dbd2018-07-26 10:23:02 -07003458 struct k_spinlock lock;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003459 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003460 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003461 char *buffer_start;
3462 char *buffer_end;
3463 char *read_ptr;
3464 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003465 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003466
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003467 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Andrew Boie0fe789f2018-04-12 18:35:56 -07003468 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003469};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003470/**
3471 * @cond INTERNAL_HIDDEN
3472 */
3473
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003474
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003475#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003476 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003477 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003478 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003479 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003480 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003481 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003482 .read_ptr = q_buffer, \
3483 .write_ptr = q_buffer, \
3484 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003485 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003486 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003487#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003488/**
3489 * INTERNAL_HIDDEN @endcond
3490 */
3491
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003492
Andrew Boie0fe789f2018-04-12 18:35:56 -07003493#define K_MSGQ_FLAG_ALLOC BIT(0)
3494
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003495/**
3496 * @brief Message Queue Attributes
3497 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303498struct k_msgq_attrs {
3499 size_t msg_size;
3500 u32_t max_msgs;
3501 u32_t used_msgs;
3502};
3503
Allan Stephensc98da842016-11-11 15:45:03 -05003504
3505/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003506 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003507 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003508 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3509 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003510 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3511 * message is similarly aligned to this boundary, @a q_msg_size must also be
3512 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003513 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003514 * The message queue can be accessed outside the module where it is defined
3515 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003516 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003517 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003519 * @param q_name Name of the message queue.
3520 * @param q_msg_size Message size (in bytes).
3521 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003522 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003523 *
3524 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003525 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003526#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3527 static char __noinit __aligned(q_align) \
3528 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3529 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
3530 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003531 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003532
Peter Mitsisd7a37502016-10-13 11:37:40 -04003533/**
3534 * @brief Initialize a message queue.
3535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003536 * This routine initializes a message queue object, prior to its first use.
3537 *
Allan Stephensda827222016-11-09 14:23:58 -06003538 * The message queue's ring buffer must contain space for @a max_msgs messages,
3539 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3540 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3541 * that each message is similarly aligned to this boundary, @a q_msg_size
3542 * must also be a multiple of N.
3543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003544 * @param q Address of the message queue.
3545 * @param buffer Pointer to ring buffer that holds queued messages.
3546 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003547 * @param max_msgs Maximum number of messages that can be queued.
3548 *
3549 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003550 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003551 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003552void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3553 u32_t max_msgs);
3554
3555/**
3556 * @brief Initialize a message queue.
3557 *
3558 * This routine initializes a message queue object, prior to its first use,
3559 * allocating its internal ring buffer from the calling thread's resource
3560 * pool.
3561 *
3562 * Memory allocated for the ring buffer can be released by calling
3563 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3564 * all of its references.
3565 *
3566 * @param q Address of the message queue.
3567 * @param msg_size Message size (in bytes).
3568 * @param max_msgs Maximum number of messages that can be queued.
3569 *
3570 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3571 * thread's resource pool, or -EINVAL if the size parameters cause
3572 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003573 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003574 */
3575__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3576 u32_t max_msgs);
3577
3578
3579void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003580
3581/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003582 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003584 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003585 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003586 * @note Can be called by ISRs.
3587 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003588 * @param q Address of the message queue.
3589 * @param data Pointer to the message.
3590 * @param timeout Waiting period to add the message (in milliseconds),
3591 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003592 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003593 * @retval 0 Message sent.
3594 * @retval -ENOMSG Returned without waiting or queue purged.
3595 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003596 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003597 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003598__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003599
3600/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003601 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003603 * This routine receives a message from message queue @a q in a "first in,
3604 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003605 *
Allan Stephensc98da842016-11-11 15:45:03 -05003606 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003608 * @param q Address of the message queue.
3609 * @param data Address of area to hold the received message.
3610 * @param timeout Waiting period to receive the message (in milliseconds),
3611 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003612 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003613 * @retval 0 Message received.
3614 * @retval -ENOMSG Returned without waiting.
3615 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003616 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003617 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003618__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003619
3620/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003621 * @brief Peek/read a message from a message queue.
3622 *
3623 * This routine reads a message from message queue @a q in a "first in,
3624 * first out" manner and leaves the message in the queue.
3625 *
3626 * @note Can be called by ISRs.
3627 *
3628 * @param q Address of the message queue.
3629 * @param data Address of area to hold the message read from the queue.
3630 *
3631 * @retval 0 Message read.
3632 * @retval -ENOMSG Returned when the queue has no message.
3633 * @req K-MSGQ-002
3634 */
3635__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3636
3637/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003638 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003639 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003640 * This routine discards all unreceived messages in a message queue's ring
3641 * buffer. Any threads that are blocked waiting to send a message to the
3642 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003644 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003645 *
3646 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003647 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003648 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003649__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003650
Peter Mitsis67be2492016-10-07 11:44:34 -04003651/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003652 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003653 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003654 * This routine returns the number of unused entries in a message queue's
3655 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003657 * @param q Address of the message queue.
3658 *
3659 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003660 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003661 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003662__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3663
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303664/**
3665 * @brief Get basic attributes of a message queue.
3666 *
3667 * This routine fetches basic attributes of message queue into attr argument.
3668 *
3669 * @param q Address of the message queue.
3670 * @param attrs pointer to message queue attribute structure.
3671 *
3672 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003673 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303674 */
3675__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3676
3677
Patrik Flykt4344e272019-03-08 14:19:05 -07003678static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003679{
3680 return q->max_msgs - q->used_msgs;
3681}
3682
Peter Mitsisd7a37502016-10-13 11:37:40 -04003683/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003684 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003686 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003688 * @param q Address of the message queue.
3689 *
3690 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003691 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003692 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003693__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3694
Patrik Flykt4344e272019-03-08 14:19:05 -07003695static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003696{
3697 return q->used_msgs;
3698}
3699
Anas Nashif166f5192018-02-25 08:02:36 -06003700/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003701
3702/**
3703 * @defgroup mem_pool_apis Memory Pool APIs
3704 * @ingroup kernel_apis
3705 * @{
3706 */
3707
Andy Ross73cb9582017-05-09 10:42:39 -07003708/* Note on sizing: the use of a 20 bit field for block means that,
3709 * assuming a reasonable minimum block size of 16 bytes, we're limited
3710 * to 16M of memory managed by a single pool. Long term it would be
3711 * good to move to a variable bit size based on configuration.
3712 */
3713struct k_mem_block_id {
3714 u32_t pool : 8;
3715 u32_t level : 4;
3716 u32_t block : 20;
3717};
3718
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003719struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003720 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003721 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003722};
3723
Anas Nashif166f5192018-02-25 08:02:36 -06003724/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003725
3726/**
3727 * @defgroup mailbox_apis Mailbox APIs
3728 * @ingroup kernel_apis
3729 * @{
3730 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003731
3732struct k_mbox_msg {
3733 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003734 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003735 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003736 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003737 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003738 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003739 /** sender's message data buffer */
3740 void *tx_data;
3741 /** internal use only - needed for legacy API support */
3742 void *_rx_data;
3743 /** message data block descriptor */
3744 struct k_mem_block tx_block;
3745 /** source thread id */
3746 k_tid_t rx_source_thread;
3747 /** target thread id */
3748 k_tid_t tx_target_thread;
3749 /** internal use only - thread waiting on send (may be a dummy) */
3750 k_tid_t _syncing_thread;
3751#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3752 /** internal use only - semaphore used during asynchronous send */
3753 struct k_sem *_async_sem;
3754#endif
3755};
3756
3757struct k_mbox {
3758 _wait_q_t tx_msg_queue;
3759 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003760 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003761
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003762 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003763};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003764/**
3765 * @cond INTERNAL_HIDDEN
3766 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003767
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003768#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003769 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003770 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3771 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003772 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003773 }
3774
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003775#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3776
Peter Mitsis12092702016-10-14 12:57:23 -04003777/**
Allan Stephensc98da842016-11-11 15:45:03 -05003778 * INTERNAL_HIDDEN @endcond
3779 */
3780
3781/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003782 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003783 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003784 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003785 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003786 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003788 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003789 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003790 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003791#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003792 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003793 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003794
Peter Mitsis12092702016-10-14 12:57:23 -04003795/**
3796 * @brief Initialize a mailbox.
3797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003798 * This routine initializes a mailbox object, prior to its first use.
3799 *
3800 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003801 *
3802 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003803 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003804 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003805extern void k_mbox_init(struct k_mbox *mbox);
3806
Peter Mitsis12092702016-10-14 12:57:23 -04003807/**
3808 * @brief Send a mailbox message in a synchronous manner.
3809 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003810 * This routine sends a message to @a mbox and waits for a receiver to both
3811 * receive and process it. The message data may be in a buffer, in a memory
3812 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003813 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003814 * @param mbox Address of the mailbox.
3815 * @param tx_msg Address of the transmit message descriptor.
3816 * @param timeout Waiting period for the message to be received (in
3817 * milliseconds), or one of the special values K_NO_WAIT
3818 * and K_FOREVER. Once the message has been received,
3819 * this routine waits as long as necessary for the message
3820 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003821 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003822 * @retval 0 Message sent.
3823 * @retval -ENOMSG Returned without waiting.
3824 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003825 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003826 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003827extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003828 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003829
Peter Mitsis12092702016-10-14 12:57:23 -04003830/**
3831 * @brief Send a mailbox message in an asynchronous manner.
3832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003833 * This routine sends a message to @a mbox without waiting for a receiver
3834 * to process it. The message data may be in a buffer, in a memory pool block,
3835 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3836 * will be given when the message has been both received and completely
3837 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003838 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003839 * @param mbox Address of the mailbox.
3840 * @param tx_msg Address of the transmit message descriptor.
3841 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003842 *
3843 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003844 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003845 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003846extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003847 struct k_sem *sem);
3848
Peter Mitsis12092702016-10-14 12:57:23 -04003849/**
3850 * @brief Receive a mailbox message.
3851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003852 * This routine receives a message from @a mbox, then optionally retrieves
3853 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003855 * @param mbox Address of the mailbox.
3856 * @param rx_msg Address of the receive message descriptor.
3857 * @param buffer Address of the buffer to receive data, or NULL to defer data
3858 * retrieval and message disposal until later.
3859 * @param timeout Waiting period for a message to be received (in
3860 * milliseconds), or one of the special values K_NO_WAIT
3861 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003862 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003863 * @retval 0 Message received.
3864 * @retval -ENOMSG Returned without waiting.
3865 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003866 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003867 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003868extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003869 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003870
3871/**
3872 * @brief Retrieve mailbox message data into a buffer.
3873 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003874 * This routine completes the processing of a received message by retrieving
3875 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003876 *
3877 * Alternatively, this routine can be used to dispose of a received message
3878 * without retrieving its data.
3879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003880 * @param rx_msg Address of the receive message descriptor.
3881 * @param buffer Address of the buffer to receive data, or NULL to discard
3882 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003883 *
3884 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003885 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003886 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003887extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003888
3889/**
3890 * @brief Retrieve mailbox message data into a memory pool block.
3891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003892 * This routine completes the processing of a received message by retrieving
3893 * its data into a memory pool block, then disposing of the message.
3894 * The memory pool block that results from successful retrieval must be
3895 * returned to the pool once the data has been processed, even in cases
3896 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003897 *
3898 * Alternatively, this routine can be used to dispose of a received message
3899 * without retrieving its data. In this case there is no need to return a
3900 * memory pool block to the pool.
3901 *
3902 * This routine allocates a new memory pool block for the data only if the
3903 * data is not already in one. If a new block cannot be allocated, the routine
3904 * returns a failure code and the received message is left unchanged. This
3905 * permits the caller to reattempt data retrieval at a later time or to dispose
3906 * of the received message without retrieving its data.
3907 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003908 * @param rx_msg Address of a receive message descriptor.
3909 * @param pool Address of memory pool, or NULL to discard data.
3910 * @param block Address of the area to hold memory pool block info.
3911 * @param timeout Waiting period to wait for a memory pool block (in
3912 * milliseconds), or one of the special values K_NO_WAIT
3913 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003914 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003915 * @retval 0 Data retrieved.
3916 * @retval -ENOMEM Returned without waiting.
3917 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003918 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003919 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003920extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003921 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003922 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003923
Anas Nashif166f5192018-02-25 08:02:36 -06003924/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003925
3926/**
Anas Nashifce78d162018-05-24 12:43:11 -05003927 * @defgroup pipe_apis Pipe APIs
3928 * @ingroup kernel_apis
3929 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003930 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003931
Anas Nashifce78d162018-05-24 12:43:11 -05003932/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003933struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003934 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3935 size_t size; /**< Buffer size */
3936 size_t bytes_used; /**< # bytes used in buffer */
3937 size_t read_index; /**< Where in buffer to read from */
3938 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08003939 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003940
3941 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003942 _wait_q_t readers; /**< Reader wait queue */
3943 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003944 } wait_q;
3945
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003946 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Anas Nashifce78d162018-05-24 12:43:11 -05003947 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003948};
3949
Anas Nashifce78d162018-05-24 12:43:11 -05003950/**
3951 * @cond INTERNAL_HIDDEN
3952 */
3953#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3954
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003955#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
3956 { \
3957 .buffer = pipe_buffer, \
3958 .size = pipe_buffer_size, \
3959 .bytes_used = 0, \
3960 .read_index = 0, \
3961 .write_index = 0, \
3962 .lock = {}, \
3963 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003964 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
3965 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003966 }, \
3967 _OBJECT_TRACING_INIT \
3968 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003969 }
3970
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003971#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3972
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003973/**
Allan Stephensc98da842016-11-11 15:45:03 -05003974 * INTERNAL_HIDDEN @endcond
3975 */
3976
3977/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003978 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003979 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003980 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003981 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003982 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003983 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003984 * @param name Name of the pipe.
3985 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3986 * or zero if no ring buffer is used.
3987 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003988 *
3989 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003990 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003991#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08003992 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07003993 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003994 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003995 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003996
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003997/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003998 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003999 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004000 * This routine initializes a pipe object, prior to its first use.
4001 *
4002 * @param pipe Address of the pipe.
4003 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
4004 * is used.
4005 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4006 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004007 *
4008 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004009 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004010 */
Andrew Boie44fe8122018-04-12 17:38:12 -07004011void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
4012
4013/**
4014 * @brief Release a pipe's allocated buffer
4015 *
4016 * If a pipe object was given a dynamically allocated buffer via
4017 * k_pipe_alloc_init(), this will free it. This function does nothing
4018 * if the buffer wasn't dynamically allocated.
4019 *
4020 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004021 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004022 */
4023void k_pipe_cleanup(struct k_pipe *pipe);
4024
4025/**
4026 * @brief Initialize a pipe and allocate a buffer for it
4027 *
4028 * Storage for the buffer region will be allocated from the calling thread's
4029 * resource pool. This memory will be released if k_pipe_cleanup() is called,
4030 * or userspace is enabled and the pipe object loses all references to it.
4031 *
4032 * This function should only be called on uninitialized pipe objects.
4033 *
4034 * @param pipe Address of the pipe.
4035 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
4036 * buffer is used.
4037 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004038 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004039 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07004040 */
4041__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004042
4043/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004044 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004046 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004048 * @param pipe Address of the pipe.
4049 * @param data Address of data to write.
4050 * @param bytes_to_write Size of data (in bytes).
4051 * @param bytes_written Address of area to hold the number of bytes written.
4052 * @param min_xfer Minimum number of bytes to write.
4053 * @param timeout Waiting period to wait for the data to be written (in
4054 * milliseconds), or one of the special values K_NO_WAIT
4055 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004056 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004057 * @retval 0 At least @a min_xfer bytes of data were written.
4058 * @retval -EIO Returned without waiting; zero data bytes were written.
4059 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004060 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004061 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004062 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004063__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
4064 size_t bytes_to_write, size_t *bytes_written,
4065 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004066
4067/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004068 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004069 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004070 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004071 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004072 * @param pipe Address of the pipe.
4073 * @param data Address to place the data read from pipe.
4074 * @param bytes_to_read Maximum number of data bytes to read.
4075 * @param bytes_read Address of area to hold the number of bytes read.
4076 * @param min_xfer Minimum number of data bytes to read.
4077 * @param timeout Waiting period to wait for the data to be read (in
4078 * milliseconds), or one of the special values K_NO_WAIT
4079 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004080 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004081 * @retval 0 At least @a min_xfer bytes of data were read.
4082 * @retval -EIO Returned without waiting; zero data bytes were read.
4083 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004084 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004085 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004086 */
Andrew Boieb9a05782017-09-29 16:05:32 -07004087__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
4088 size_t bytes_to_read, size_t *bytes_read,
4089 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004090
4091/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004092 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004094 * This routine writes the data contained in a memory block to @a pipe.
4095 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004096 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004097 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004098 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004099 * @param block Memory block containing data to send
4100 * @param size Number of data bytes in memory block to send
4101 * @param sem Semaphore to signal upon completion (else NULL)
4102 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004103 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004104 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004105 */
4106extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
4107 size_t size, struct k_sem *sem);
4108
Anas Nashif166f5192018-02-25 08:02:36 -06004109/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004110
Allan Stephensc98da842016-11-11 15:45:03 -05004111/**
4112 * @cond INTERNAL_HIDDEN
4113 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004114
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004115struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004116 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05004117 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04004118 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004119 char *buffer;
4120 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05004121 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004122
Flavio Ceolind1ed3362018-12-07 11:39:13 -08004123 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004124};
4125
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004126#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004127 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004128 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004129 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004130 .num_blocks = slab_num_blocks, \
4131 .block_size = slab_block_size, \
4132 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004133 .free_list = NULL, \
4134 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05004135 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004136 }
4137
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004138#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
4139
4140
Peter Mitsis578f9112016-10-07 13:50:31 -04004141/**
Allan Stephensc98da842016-11-11 15:45:03 -05004142 * INTERNAL_HIDDEN @endcond
4143 */
4144
4145/**
4146 * @defgroup mem_slab_apis Memory Slab APIs
4147 * @ingroup kernel_apis
4148 * @{
4149 */
4150
4151/**
Allan Stephensda827222016-11-09 14:23:58 -06004152 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04004153 *
Allan Stephensda827222016-11-09 14:23:58 -06004154 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004155 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06004156 * @a slab_align -byte boundary. To ensure that each memory block is similarly
4157 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004158 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04004159 *
Allan Stephensda827222016-11-09 14:23:58 -06004160 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004161 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004162 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004163 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004165 * @param name Name of the memory slab.
4166 * @param slab_block_size Size of each memory block (in bytes).
4167 * @param slab_num_blocks Number memory blocks.
4168 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004169 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04004170 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004171#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004172 char __noinit __aligned(WB_UP(slab_align)) \
4173 _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004174 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004175 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004176 WB_UP(slab_block_size), slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004177
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004178/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004179 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004181 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004182 *
Allan Stephensda827222016-11-09 14:23:58 -06004183 * The memory slab's buffer contains @a slab_num_blocks memory blocks
4184 * that are @a slab_block_size bytes long. The buffer must be aligned to an
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004185 * N-byte boundary matching a word boundary, where N is a power of 2
4186 * (i.e. 4 on 32-bit systems, 8, 16, ...).
Allan Stephensda827222016-11-09 14:23:58 -06004187 * To ensure that each memory block is similarly aligned to this boundary,
4188 * @a slab_block_size must also be a multiple of N.
4189 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004190 * @param slab Address of the memory slab.
4191 * @param buffer Pointer to buffer used for the memory blocks.
4192 * @param block_size Size of each memory block (in bytes).
4193 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004194 *
4195 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004196 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004197 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004198extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05004199 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004200
4201/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004202 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004203 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004204 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004205 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004206 * @param slab Address of the memory slab.
4207 * @param mem Pointer to block address area.
4208 * @param timeout Maximum time to wait for operation to complete
4209 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4210 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004211 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004212 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004213 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004214 * @retval -ENOMEM Returned without waiting.
4215 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004216 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004217 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004218extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05004219 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004220
4221/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004222 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004223 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004224 * This routine releases a previously allocated memory block back to its
4225 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004226 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004227 * @param slab Address of the memory slab.
4228 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004229 *
4230 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004231 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004232 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004233extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004234
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004235/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004236 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004237 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004238 * This routine gets the number of memory blocks that are currently
4239 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004240 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004241 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004242 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004243 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004244 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004245 */
Kumar Galacc334c72017-04-21 10:55:34 -05004246static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004247{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004248 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004249}
4250
Peter Mitsisc001aa82016-10-13 13:53:37 -04004251/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004252 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004253 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004254 * This routine gets the number of memory blocks that are currently
4255 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004256 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004257 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004258 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004259 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004260 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004261 */
Kumar Galacc334c72017-04-21 10:55:34 -05004262static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004263{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004264 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004265}
4266
Anas Nashif166f5192018-02-25 08:02:36 -06004267/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004268
4269/**
4270 * @cond INTERNAL_HIDDEN
4271 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004272
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004273struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004274 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004275 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004276};
4277
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004278/**
Allan Stephensc98da842016-11-11 15:45:03 -05004279 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004280 */
4281
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004282/**
Allan Stephensc98da842016-11-11 15:45:03 -05004283 * @addtogroup mem_pool_apis
4284 * @{
4285 */
4286
4287/**
4288 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004290 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4291 * long. The memory pool allows blocks to be repeatedly partitioned into
4292 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004293 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004294 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004295 * If the pool is to be accessed outside the module where it is defined, it
4296 * can be declared via
4297 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004298 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004299 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004300 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004301 * @param minsz Size of the smallest blocks in the pool (in bytes).
4302 * @param maxsz Size of the largest blocks in the pool (in bytes).
4303 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004304 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004305 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004306 */
Andy Ross73cb9582017-05-09 10:42:39 -07004307#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004308 char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \
Andy Ross73cb9582017-05-09 10:42:39 -07004309 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004310 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004311 Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004312 .base = { \
4313 .buf = _mpool_buf_##name, \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004314 .max_sz = WB_UP(maxsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004315 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004316 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004317 .levels = _mpool_lvls_##name, \
4318 .flags = SYS_MEM_POOL_KERNEL \
4319 } \
Johann Fischer223a2b92019-07-04 15:55:20 +02004320 }; \
Nicolas Pitreb2a022b2019-09-26 16:36:40 -04004321 BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004322
Peter Mitsis937042c2016-10-13 13:18:26 -04004323/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004324 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004325 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004326 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004327 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004328 * @param pool Address of the memory pool.
4329 * @param block Pointer to block descriptor for the allocated memory.
4330 * @param size Amount of memory to allocate (in bytes).
4331 * @param timeout Maximum time to wait for operation to complete
4332 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4333 * or K_FOREVER to wait as long as necessary.
4334 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004335 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004336 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004337 * @retval -ENOMEM Returned without waiting.
4338 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004339 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004340 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004341extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004342 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004343
4344/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004345 * @brief Allocate memory from a memory pool with malloc() semantics
4346 *
4347 * Such memory must be released using k_free().
4348 *
4349 * @param pool Address of the memory pool.
4350 * @param size Amount of memory to allocate (in bytes).
4351 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004352 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004353 */
4354extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4355
4356/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004357 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004359 * This routine releases a previously allocated memory block back to its
4360 * memory pool.
4361 *
4362 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004363 *
4364 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004365 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004366 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004367extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004368
4369/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004370 * @brief Free memory allocated from a memory pool.
4371 *
4372 * This routine releases a previously allocated memory block back to its
4373 * memory pool
4374 *
4375 * @param id Memory block identifier.
4376 *
4377 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004378 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004379 */
4380extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4381
4382/**
Anas Nashif166f5192018-02-25 08:02:36 -06004383 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004384 */
4385
4386/**
4387 * @defgroup heap_apis Heap Memory Pool APIs
4388 * @ingroup kernel_apis
4389 * @{
4390 */
4391
4392/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004393 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004394 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004395 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004396 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004397 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004398 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004399 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004400 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004401 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004402 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004403extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004404
4405/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004406 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004407 *
4408 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004409 * returned must have been allocated from the heap memory pool or
4410 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004411 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004412 * If @a ptr is NULL, no operation is performed.
4413 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004414 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004415 *
4416 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004417 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004418 */
4419extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004420
Allan Stephensc98da842016-11-11 15:45:03 -05004421/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004422 * @brief Allocate memory from heap, array style
4423 *
4424 * This routine provides traditional calloc() semantics. Memory is
4425 * allocated from the heap memory pool and zeroed.
4426 *
4427 * @param nmemb Number of elements in the requested array
4428 * @param size Size of each array element (in bytes).
4429 *
4430 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004431 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004432 */
4433extern void *k_calloc(size_t nmemb, size_t size);
4434
Anas Nashif166f5192018-02-25 08:02:36 -06004435/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004436
Benjamin Walshacc68c12017-01-29 18:57:45 -05004437/* polling API - PRIVATE */
4438
Benjamin Walshb0179862017-02-02 16:39:57 -05004439#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004440#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004441#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004442#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004443#endif
4444
Benjamin Walshacc68c12017-01-29 18:57:45 -05004445/* private - types bit positions */
4446enum _poll_types_bits {
4447 /* can be used to ignore an event */
4448 _POLL_TYPE_IGNORE,
4449
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004450 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004451 _POLL_TYPE_SIGNAL,
4452
4453 /* semaphore availability */
4454 _POLL_TYPE_SEM_AVAILABLE,
4455
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004456 /* queue/fifo/lifo data availability */
4457 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004458
4459 _POLL_NUM_TYPES
4460};
4461
Patrik Flykt4344e272019-03-08 14:19:05 -07004462#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004463
4464/* private - states bit positions */
4465enum _poll_states_bits {
4466 /* default state when creating event */
4467 _POLL_STATE_NOT_READY,
4468
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004469 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004470 _POLL_STATE_SIGNALED,
4471
4472 /* semaphore is available */
4473 _POLL_STATE_SEM_AVAILABLE,
4474
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004475 /* data is available to read on queue/fifo/lifo */
4476 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004477
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004478 /* queue/fifo/lifo wait was cancelled */
4479 _POLL_STATE_CANCELLED,
4480
Benjamin Walshacc68c12017-01-29 18:57:45 -05004481 _POLL_NUM_STATES
4482};
4483
Patrik Flykt4344e272019-03-08 14:19:05 -07004484#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004485
4486#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004487 (32 - (0 \
4488 + 8 /* tag */ \
4489 + _POLL_NUM_TYPES \
4490 + _POLL_NUM_STATES \
4491 + 1 /* modes */ \
4492 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004493
Benjamin Walshacc68c12017-01-29 18:57:45 -05004494/* end of polling API - PRIVATE */
4495
4496
4497/**
4498 * @defgroup poll_apis Async polling APIs
4499 * @ingroup kernel_apis
4500 * @{
4501 */
4502
4503/* Public polling API */
4504
4505/* public - values for k_poll_event.type bitfield */
4506#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004507#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4508#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4509#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004510#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004511
4512/* public - polling modes */
4513enum k_poll_modes {
4514 /* polling thread does not take ownership of objects when available */
4515 K_POLL_MODE_NOTIFY_ONLY = 0,
4516
4517 K_POLL_NUM_MODES
4518};
4519
4520/* public - values for k_poll_event.state bitfield */
4521#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004522#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4523#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4524#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004525#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004526#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004527
4528/* public - poll signal object */
4529struct k_poll_signal {
4530 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004531 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004532
4533 /*
4534 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4535 * user resets it to 0.
4536 */
4537 unsigned int signaled;
4538
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004539 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004540 int result;
4541};
4542
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004543#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004544 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004545 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004546 .signaled = 0, \
4547 .result = 0, \
4548 }
4549
4550struct k_poll_event {
4551 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004552 sys_dnode_t _node;
4553
4554 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004555 struct _poller *poller;
4556
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004557 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004558 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004559
Benjamin Walshacc68c12017-01-29 18:57:45 -05004560 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004561 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004562
4563 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004564 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004565
4566 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004567 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004568
4569 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004570 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004571
4572 /* per-type data */
4573 union {
4574 void *obj;
4575 struct k_poll_signal *signal;
4576 struct k_sem *sem;
4577 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004578 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004579 };
4580};
4581
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004582#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004583 { \
4584 .poller = NULL, \
4585 .type = event_type, \
4586 .state = K_POLL_STATE_NOT_READY, \
4587 .mode = event_mode, \
4588 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004589 { .obj = event_obj }, \
4590 }
4591
4592#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4593 event_tag) \
4594 { \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004595 .tag = event_tag, \
Markus Fuchsbe21d3f2019-10-09 21:31:25 +02004596 .type = event_type, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004597 .state = K_POLL_STATE_NOT_READY, \
4598 .mode = event_mode, \
4599 .unused = 0, \
4600 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004601 }
4602
4603/**
4604 * @brief Initialize one struct k_poll_event instance
4605 *
4606 * After this routine is called on a poll event, the event it ready to be
4607 * placed in an event array to be passed to k_poll().
4608 *
4609 * @param event The event to initialize.
4610 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4611 * values. Only values that apply to the same object being polled
4612 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4613 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004614 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004615 * @param obj Kernel object or poll signal.
4616 *
4617 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004618 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004619 */
4620
Kumar Galacc334c72017-04-21 10:55:34 -05004621extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004622 int mode, void *obj);
4623
4624/**
4625 * @brief Wait for one or many of multiple poll events to occur
4626 *
4627 * This routine allows a thread to wait concurrently for one or many of
4628 * multiple poll events to have occurred. Such events can be a kernel object
4629 * being available, like a semaphore, or a poll signal event.
4630 *
4631 * When an event notifies that a kernel object is available, the kernel object
4632 * is not "given" to the thread calling k_poll(): it merely signals the fact
4633 * that the object was available when the k_poll() call was in effect. Also,
4634 * all threads trying to acquire an object the regular way, i.e. by pending on
4635 * the object, have precedence over the thread polling on the object. This
4636 * means that the polling thread will never get the poll event on an object
4637 * until the object becomes available and its pend queue is empty. For this
4638 * reason, the k_poll() call is more effective when the objects being polled
4639 * only have one thread, the polling thread, trying to acquire them.
4640 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004641 * When k_poll() returns 0, the caller should loop on all the events that were
4642 * passed to k_poll() and check the state field for the values that were
4643 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004644 *
4645 * Before being reused for another call to k_poll(), the user has to reset the
4646 * state field to K_POLL_STATE_NOT_READY.
4647 *
Andrew Boie3772f772018-05-07 16:52:57 -07004648 * When called from user mode, a temporary memory allocation is required from
4649 * the caller's resource pool.
4650 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004651 * @param events An array of pointers to events to be polled for.
4652 * @param num_events The number of events in the array.
4653 * @param timeout Waiting period for an event to be ready (in milliseconds),
4654 * or one of the special values K_NO_WAIT and K_FOREVER.
4655 *
4656 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004657 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004658 * @retval -EINTR Polling has been interrupted, e.g. with
4659 * k_queue_cancel_wait(). All output events are still set and valid,
4660 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4661 * words, -EINTR status means that at least one of output events is
4662 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004663 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4664 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004665 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004666 */
4667
Andrew Boie3772f772018-05-07 16:52:57 -07004668__syscall int k_poll(struct k_poll_event *events, int num_events,
4669 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004670
4671/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004672 * @brief Initialize a poll signal object.
4673 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004674 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004675 *
4676 * @param signal A poll signal.
4677 *
4678 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004679 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004680 */
4681
Andrew Boie3772f772018-05-07 16:52:57 -07004682__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4683
4684/*
4685 * @brief Reset a poll signal object's state to unsignaled.
4686 *
4687 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004688 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004689 */
4690__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4691
Patrik Flykt4344e272019-03-08 14:19:05 -07004692static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004693{
Patrik Flykt24d71432019-03-26 19:57:45 -06004694 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004695}
4696
4697/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004698 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004699 *
4700 * @param signal A poll signal object
4701 * @param signaled An integer buffer which will be written nonzero if the
4702 * object was signaled
4703 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004704 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004705 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004706 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004707 */
4708__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4709 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004710
4711/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004712 * @brief Signal a poll signal object.
4713 *
4714 * This routine makes ready a poll signal, which is basically a poll event of
4715 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4716 * made ready to run. A @a result value can be specified.
4717 *
4718 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004719 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004720 * k_poll_signal_reset(). It thus has to be reset by the user before being
4721 * passed again to k_poll() or k_poll() will consider it being signaled, and
4722 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004723 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004724 * @note The result is stored and the 'signaled' field is set even if
4725 * this function returns an error indicating that an expiring poll was
4726 * not notified. The next k_poll() will detect the missed raise.
4727 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004728 * @param signal A poll signal.
4729 * @param result The value to store in the result field of the signal.
4730 *
4731 * @retval 0 The signal was delivered successfully.
4732 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004733 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004734 */
4735
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004736__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004737
Anas Nashif954d5502018-02-25 08:37:28 -06004738/**
4739 * @internal
4740 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004741extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004742
Anas Nashif166f5192018-02-25 08:02:36 -06004743/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004744
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004745/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004746 * @defgroup cpu_idle_apis CPU Idling APIs
4747 * @ingroup kernel_apis
4748 * @{
4749 */
4750
Andrew Boie07525a32019-09-21 16:17:23 -07004751extern void z_arch_cpu_idle(void);
4752extern void z_arch_cpu_atomic_idle(unsigned int key);
4753
Anas Nashif30c3cff2019-01-22 08:18:13 -05004754/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004755 * @brief Make the CPU idle.
4756 *
4757 * This function makes the CPU idle until an event wakes it up.
4758 *
4759 * In a regular system, the idle thread should be the only thread responsible
4760 * for making the CPU idle and triggering any type of power management.
4761 * However, in some more constrained systems, such as a single-threaded system,
4762 * the only thread would be responsible for this if needed.
4763 *
4764 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004765 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004766 */
Andrew Boie07525a32019-09-21 16:17:23 -07004767static inline void k_cpu_idle(void)
4768{
4769 z_arch_cpu_idle();
4770}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004771
4772/**
4773 * @brief Make the CPU idle in an atomic fashion.
4774 *
4775 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4776 * must be done atomically before making the CPU idle.
4777 *
4778 * @param key Interrupt locking key obtained from irq_lock().
4779 *
4780 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004781 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004782 */
Andrew Boie07525a32019-09-21 16:17:23 -07004783static inline void k_cpu_atomic_idle(unsigned int key)
4784{
4785 z_arch_cpu_atomic_idle(key);
4786}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004787
Anas Nashif30c3cff2019-01-22 08:18:13 -05004788/**
4789 * @}
4790 */
Anas Nashif954d5502018-02-25 08:37:28 -06004791
4792/**
4793 * @internal
4794 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004795extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004796
Patrik Flykt4344e272019-03-08 14:19:05 -07004797#ifdef Z_ARCH_EXCEPT
Andrew Boiecdb94d62017-04-18 15:22:05 -07004798/* This archtecture has direct support for triggering a CPU exception */
Patrik Flykt4344e272019-03-08 14:19:05 -07004799#define z_except_reason(reason) Z_ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004800#else
4801
Andrew Boiecdb94d62017-04-18 15:22:05 -07004802/* NOTE: This is the implementation for arches that do not implement
Patrik Flykt4344e272019-03-08 14:19:05 -07004803 * Z_ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004804 *
4805 * We won't have a real exception frame to determine the PC value when
4806 * the oops occurred, so print file and line number before we jump into
4807 * the fatal error handler.
4808 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004809#define z_except_reason(reason) do { \
Andrew Boiecdb94d62017-04-18 15:22:05 -07004810 printk("@ %s:%d:\n", __FILE__, __LINE__); \
Andrew Boie56236372019-07-15 15:22:29 -07004811 z_fatal_error(reason, NULL); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004812 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004813
4814#endif /* _ARCH__EXCEPT */
4815
4816/**
4817 * @brief Fatally terminate a thread
4818 *
4819 * This should be called when a thread has encountered an unrecoverable
4820 * runtime condition and needs to terminate. What this ultimately
4821 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004822 * will be called will reason code K_ERR_KERNEL_OOPS.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004823 *
4824 * If this is called from ISR context, the default system fatal error handler
4825 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004826 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004827 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004828#define k_oops() z_except_reason(K_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004829
4830/**
4831 * @brief Fatally terminate the system
4832 *
4833 * This should be called when the Zephyr kernel has encountered an
4834 * unrecoverable runtime condition and needs to terminate. What this ultimately
4835 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004836 * will be called will reason code K_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004837 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004838 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004839#define k_panic() z_except_reason(K_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004840
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004841/*
4842 * private APIs that are utilized by one or more public APIs
4843 */
4844
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004845#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004846/**
4847 * @internal
4848 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004849extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004850#else
Anas Nashif954d5502018-02-25 08:37:28 -06004851/**
4852 * @internal
4853 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004854#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004855#endif
4856
Anas Nashif954d5502018-02-25 08:37:28 -06004857/**
4858 * @internal
4859 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004860extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004861/**
4862 * @internal
4863 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004864extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004865
Andrew Boiedc5d9352017-06-02 12:56:47 -07004866/* arch/cpu.h may declare an architecture or platform-specific macro
4867 * for properly declaring stacks, compatible with MMU/MPU constraints if
4868 * enabled
4869 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004870
4871/**
4872 * @brief Obtain an extern reference to a stack
4873 *
4874 * This macro properly brings the symbol of a thread stack declared
4875 * elsewhere into scope.
4876 *
4877 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004878 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004879 */
4880#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4881
Patrik Flykt4344e272019-03-08 14:19:05 -07004882#ifdef Z_ARCH_THREAD_STACK_DEFINE
4883#define K_THREAD_STACK_DEFINE(sym, size) Z_ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004884#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Patrik Flykt4344e272019-03-08 14:19:05 -07004885 Z_ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4886#define K_THREAD_STACK_LEN(size) Z_ARCH_THREAD_STACK_LEN(size)
4887#define K_THREAD_STACK_MEMBER(sym, size) Z_ARCH_THREAD_STACK_MEMBER(sym, size)
4888#define K_THREAD_STACK_SIZEOF(sym) Z_ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie575abc02019-03-19 10:42:24 -07004889#define K_THREAD_STACK_RESERVED Z_ARCH_THREAD_STACK_RESERVED
Andrew Boie4e5c0932019-04-04 12:05:28 -07004890static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004891{
Patrik Flykt4344e272019-03-08 14:19:05 -07004892 return Z_ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07004893}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004894#else
4895/**
4896 * @brief Declare a toplevel thread stack memory region
4897 *
4898 * This declares a region of memory suitable for use as a thread's stack.
4899 *
4900 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4901 * 'noinit' section so that it isn't zeroed at boot
4902 *
Andrew Boie507852a2017-07-25 18:47:07 -07004903 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004904 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07004905 * inside needs to be examined, examine thread->stack_info for the associated
4906 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004907 *
4908 * It is legal to precede this definition with the 'static' keyword.
4909 *
4910 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4911 * parameter of k_thread_create(), it may not be the same as the
4912 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4913 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004914 * Some arches may round the size of the usable stack region up to satisfy
4915 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4916 * size.
4917 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004918 * @param sym Thread stack symbol name
4919 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004920 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004921 */
4922#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004923 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004924
4925/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304926 * @brief Calculate size of stacks to be allocated in a stack array
4927 *
4928 * This macro calculates the size to be allocated for the stacks
4929 * inside a stack array. It accepts the indicated "size" as a parameter
4930 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4931 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4932 *
4933 * @param size Size of the stack memory region
4934 * @req K-TSTACK-001
4935 */
4936#define K_THREAD_STACK_LEN(size) (size)
4937
4938/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004939 * @brief Declare a toplevel array of thread stack memory regions
4940 *
4941 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4942 * definition for additional details and constraints.
4943 *
4944 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4945 * 'noinit' section so that it isn't zeroed at boot
4946 *
4947 * @param sym Thread stack symbol name
4948 * @param nmemb Number of stacks to declare
4949 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004950 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004951 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004952#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004953 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304954 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004955
4956/**
4957 * @brief Declare an embedded stack memory region
4958 *
4959 * Used for stacks embedded within other data structures. Use is highly
4960 * discouraged but in some cases necessary. For memory protection scenarios,
4961 * it is very important that any RAM preceding this member not be writable
4962 * by threads else a stack overflow will lead to silent corruption. In other
4963 * words, the containing data structure should live in RAM owned by the kernel.
4964 *
4965 * @param sym Thread stack symbol name
4966 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004967 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004968 */
4969#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004970 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004971
4972/**
4973 * @brief Return the size in bytes of a stack memory region
4974 *
4975 * Convenience macro for passing the desired stack size to k_thread_create()
4976 * since the underlying implementation may actually create something larger
4977 * (for instance a guard area).
4978 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004979 * The value returned here is not guaranteed to match the 'size' parameter
4980 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004981 *
4982 * @param sym Stack memory symbol
4983 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004984 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004985 */
4986#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4987
Andrew Boie575abc02019-03-19 10:42:24 -07004988
4989/**
4990 * @brief Indicate how much additional memory is reserved for stack objects
4991 *
4992 * Any given stack declaration may have additional memory in it for guard
4993 * areas or supervisor mode stacks. This macro indicates how much space
4994 * is reserved for this. The memory reserved may not be contiguous within
4995 * the stack object, and does not account for additional space used due to
4996 * enforce alignment.
4997 */
4998#define K_THREAD_STACK_RESERVED 0
4999
Andrew Boiedc5d9352017-06-02 12:56:47 -07005000/**
5001 * @brief Get a pointer to the physical stack buffer
5002 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07005003 * This macro is deprecated. If a stack buffer needs to be examined, the
5004 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07005005 *
5006 * @param sym Declared stack symbol name
5007 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005008 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07005009 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07005010static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07005011{
5012 return (char *)sym;
5013}
Andrew Boiedc5d9352017-06-02 12:56:47 -07005014
5015#endif /* _ARCH_DECLARE_STACK */
5016
Chunlin Hane9c97022017-07-07 20:29:30 +08005017/**
5018 * @defgroup mem_domain_apis Memory domain APIs
5019 * @ingroup kernel_apis
5020 * @{
5021 */
5022
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005023/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005024 * @def K_MEM_PARTITION_DEFINE
5025 * @brief Used to declare a memory partition
5026 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005027 */
5028#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
5029#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
5030 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08005031 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005032 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005033#else
5034#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08005035 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005036 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08005037#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
5038
Chunlin Hane9c97022017-07-07 20:29:30 +08005039/* memory partition */
5040struct k_mem_partition {
5041 /* start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04005042 uintptr_t start;
Chunlin Hane9c97022017-07-07 20:29:30 +08005043 /* size of memory partition */
5044 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005045#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08005046 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305047 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01005048#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08005049};
5050
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02005051/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05305052 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005053struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305054#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08005055 /* partitions in the domain */
5056 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305057#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08005058 /* domain q */
5059 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08005060 /* number of partitions in the domain */
5061 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08005062};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05305063
Chunlin Hane9c97022017-07-07 20:29:30 +08005064
5065/**
5066 * @brief Initialize a memory domain.
5067 *
5068 * Initialize a memory domain with given name and memory partitions.
5069 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005070 * See documentation for k_mem_domain_add_partition() for details about
5071 * partition constraints.
5072 *
Chunlin Hane9c97022017-07-07 20:29:30 +08005073 * @param domain The memory domain to be initialized.
5074 * @param num_parts The number of array items of "parts" parameter.
5075 * @param parts An array of pointers to the memory partitions. Can be NULL
5076 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005077 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005078 */
Leandro Pereira08de6582018-02-28 14:22:57 -08005079extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08005080 struct k_mem_partition *parts[]);
5081/**
5082 * @brief Destroy a memory domain.
5083 *
5084 * Destroy a memory domain.
5085 *
5086 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005087 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005088 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005089extern void k_mem_domain_destroy(struct k_mem_domain *domain);
5090
5091/**
5092 * @brief Add a memory partition into a memory domain.
5093 *
Andrew Boiefddd5502019-07-30 18:07:32 -07005094 * Add a memory partition into a memory domain. Partitions must conform to
5095 * the following constraints:
5096 *
5097 * - Partition bounds must be within system RAM boundaries on MMU-based
5098 * systems.
5099 * - Partitions in the same memory domain may not overlap each other.
5100 * - Partitions must not be defined which expose private kernel
5101 * data structures or kernel objects.
5102 * - The starting address alignment, and the partition size must conform to
5103 * the constraints of the underlying memory management hardware, which
5104 * varies per architecture.
5105 * - Memory domain partitions are only intended to control access to memory
5106 * from user mode threads.
5107 *
5108 * Violating these constraints may lead to CPU exceptions or undefined
5109 * behavior.
Chunlin Hane9c97022017-07-07 20:29:30 +08005110 *
5111 * @param domain The memory domain to be added a memory partition.
5112 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005113 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005114 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005115extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
5116 struct k_mem_partition *part);
5117
5118/**
5119 * @brief Remove a memory partition from a memory domain.
5120 *
5121 * Remove a memory partition from a memory domain.
5122 *
5123 * @param domain The memory domain to be removed a memory partition.
5124 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005125 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005126 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005127extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
5128 struct k_mem_partition *part);
5129
5130/**
5131 * @brief Add a thread into a memory domain.
5132 *
5133 * Add a thread into a memory domain.
5134 *
5135 * @param domain The memory domain that the thread is going to be added into.
5136 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005137 *
5138 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005139 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005140extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
5141 k_tid_t thread);
5142
5143/**
5144 * @brief Remove a thread from its memory domain.
5145 *
5146 * Remove a thread from its memory domain.
5147 *
5148 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005149 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005150 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005151extern void k_mem_domain_remove_thread(k_tid_t thread);
5152
Anas Nashif166f5192018-02-25 08:02:36 -06005153/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08005154
Andrew Boie756f9072017-10-10 16:01:49 -07005155/**
5156 * @brief Emit a character buffer to the console device
5157 *
5158 * @param c String of characters to print
5159 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005160 *
5161 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07005162 */
5163__syscall void k_str_out(char *c, size_t n);
5164
Andy Rosse7172672018-01-24 15:48:32 -08005165/**
5166 * @brief Start a numbered CPU on a MP-capable system
5167
5168 * This starts and initializes a specific CPU. The main thread on
5169 * startup is running on CPU zero, other processors are numbered
5170 * sequentially. On return from this function, the CPU is known to
5171 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07005172 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08005173 * with the provided key will work to enable them.
5174 *
5175 * Normally, in SMP mode this function will be called by the kernel
5176 * initialization and should not be used as a user API. But it is
5177 * defined here for special-purpose apps which want Zephyr running on
5178 * one core and to use others for design-specific processing.
5179 *
5180 * @param cpu_num Integer number of the CPU
5181 * @param stack Stack memory for the CPU
5182 * @param sz Stack buffer size, in bytes
5183 * @param fn Function to begin running on the CPU. First argument is
5184 * an irq_unlock() key.
5185 * @param arg Untyped argument to be passed to "fn"
5186 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005187extern void z_arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08005188 void (*fn)(int key, void *data), void *arg);
Andy Rosse7172672018-01-24 15:48:32 -08005189
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005190
5191/**
5192 * @brief Disable preservation of floating point context information.
5193 *
5194 * This routine informs the kernel that the specified thread
5195 * will no longer be using the floating point registers.
5196 *
5197 * @warning
5198 * Some architectures apply restrictions on how the disabling of floating
5199 * point preservation may be requested, see z_arch_float_disable.
5200 *
5201 * @warning
5202 * This routine should only be used to disable floating point support for
5203 * a thread that currently has such support enabled.
5204 *
5205 * @param thread ID of thread.
5206 *
5207 * @retval 0 On success.
5208 * @retval -ENOSYS If the floating point disabling is not implemented.
5209 * -EINVAL If the floating point disabling could not be performed.
5210 */
5211__syscall int k_float_disable(struct k_thread *thread);
5212
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005213#ifdef __cplusplus
5214}
5215#endif
5216
Anas Nashif10291a02019-06-25 12:25:12 -04005217#include <debug/tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07005218#include <syscalls/kernel.h>
5219
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05005220#endif /* !_ASMLANGUAGE */
5221
Flavio Ceolin67ca1762018-09-14 10:43:44 -07005222#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */