blob: 1233b575c3a1900cbb85b1cf9a1c1e816df9b825 [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>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020
21#ifdef __cplusplus
22extern "C" {
23#endif
24
Anas Nashifbbb157d2017-01-15 08:46:31 -050025/**
26 * @brief Kernel APIs
27 * @defgroup kernel_apis Kernel APIs
28 * @{
29 * @}
30 */
31
Anas Nashif61f4b242016-11-18 10:53:59 -050032#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040033#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
34#else
35#define K_DEBUG(fmt, ...)
36#endif
37
Benjamin Walsh2f280412017-01-14 19:23:46 -050038#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
39#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
40#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
41#elif defined(CONFIG_COOP_ENABLED)
42#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
43#define _NUM_PREEMPT_PRIO (0)
44#elif defined(CONFIG_PREEMPT_ENABLED)
45#define _NUM_COOP_PRIO (0)
46#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
47#else
48#error "invalid configuration"
49#endif
50
51#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040052#define K_PRIO_PREEMPT(x) (x)
53
Benjamin Walsh456c6da2016-09-02 18:55:39 -040054#define K_ANY NULL
55#define K_END NULL
56
Benjamin Walshedb35702017-01-14 18:47:22 -050057#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040058#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050059#elif defined(CONFIG_COOP_ENABLED)
60#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
61#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040062#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050063#else
64#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040065#endif
66
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050067#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040068#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
69#else
70#define K_LOWEST_THREAD_PRIO -1
71#endif
72
Benjamin Walshfab8d922016-11-08 15:36:36 -050073#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
74
Benjamin Walsh456c6da2016-09-02 18:55:39 -040075#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
76#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
77
Andy Ross225c74b2018-06-27 11:20:50 -070078#ifdef CONFIG_WAITQ_SCALABLE
Andy Ross1acd8c22018-05-03 14:51:49 -070079
80typedef struct {
81 struct _priq_rb waitq;
82} _wait_q_t;
83
Patrik Flykt4344e272019-03-08 14:19:05 -070084extern bool z_priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
Andy Ross1acd8c22018-05-03 14:51:49 -070085
Patrik Flykt4344e272019-03-08 14:19:05 -070086#define Z_WAIT_Q_INIT(wait_q) { { { .lessthan_fn = z_priq_rb_lessthan } } }
Andy Ross1acd8c22018-05-03 14:51:49 -070087
88#else
89
Andy Rossccf3bf72018-05-10 11:10:34 -070090typedef struct {
91 sys_dlist_t waitq;
92} _wait_q_t;
93
Patrik Flykt4344e272019-03-08 14:19:05 -070094#define Z_WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
Benjamin Walsh456c6da2016-09-02 18:55:39 -040095
Andy Ross1acd8c22018-05-03 14:51:49 -070096#endif
97
Anas Nashif2f203c22016-12-18 06:57:45 -050098#ifdef CONFIG_OBJECT_TRACING
Flavio Ceolind1ed3362018-12-07 11:39:13 -080099#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next;
Anas Nashif2f203c22016-12-18 06:57:45 -0500100#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400101#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500102#define _OBJECT_TRACING_INIT
Flavio Ceolind1ed3362018-12-07 11:39:13 -0800103#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400104#endif
105
Benjamin Walshacc68c12017-01-29 18:57:45 -0500106#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300107#define _POLL_EVENT_OBJ_INIT(obj) \
108 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
109#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500110#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300111#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500112#define _POLL_EVENT
113#endif
114
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500115struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400116struct k_mutex;
117struct k_sem;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400118struct k_msgq;
119struct k_mbox;
120struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200121struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122struct k_fifo;
123struct k_lifo;
124struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400125struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400126struct k_mem_pool;
127struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500128struct k_poll_event;
129struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800130struct k_mem_domain;
131struct k_mem_partition;
Wentong Wu5611e922019-06-20 23:51:27 +0800132struct k_futex;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400133
Andrew Boie5bd891d2017-09-27 12:59:28 -0700134/* This enumeration needs to be kept in sync with the lists of kernel objects
135 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
136 * function in kernel/userspace.c
137 */
Andrew Boie945af952017-08-22 13:15:23 -0700138enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700139 K_OBJ_ANY,
140
Leandro Pereirac2003672018-04-04 13:50:32 -0700141 /** @cond
142 * Doxygen should ignore this build-time generated include file
143 * when genrating API documentation. Enumeration values are
144 * generated during build by gen_kobject_list.py. It includes
145 * basic kernel objects (e.g. pipes and mutexes) and driver types.
146 */
147#include <kobj-types-enum.h>
148 /** @endcond
149 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700150
Andrew Boie945af952017-08-22 13:15:23 -0700151 K_OBJ_LAST
152};
Anas Nashif4bcb2942019-01-23 23:06:29 -0500153/**
154 * @defgroup usermode_apis User Mode APIs
155 * @ingroup kernel_apis
156 * @{
157 */
Andrew Boie945af952017-08-22 13:15:23 -0700158
159#ifdef CONFIG_USERSPACE
160/* Table generated by gperf, these objects are retrieved via
Patrik Flykt4344e272019-03-08 14:19:05 -0700161 * z_object_find() */
Andrew Boie945af952017-08-22 13:15:23 -0700162struct _k_object {
163 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700164 u8_t perms[CONFIG_MAX_THREAD_BYTES];
165 u8_t type;
166 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700167 u32_t data;
Andrew Boiedf555242018-05-25 07:28:54 -0700168} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700169
Andrew Boie877f82e2017-10-17 11:20:22 -0700170struct _k_object_assignment {
171 struct k_thread *thread;
172 void * const *objects;
173};
174
175/**
176 * @brief Grant a static thread access to a list of kernel objects
177 *
178 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
179 * a set of kernel objects. These objects do not need to be in an initialized
180 * state. The permissions will be granted when the threads are initialized
181 * in the early boot sequence.
182 *
183 * All arguments beyond the first must be pointers to kernel objects.
184 *
185 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
186 */
187#define K_THREAD_ACCESS_GRANT(name_, ...) \
188 static void * const _CONCAT(_object_list_, name_)[] = \
189 { __VA_ARGS__, NULL }; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400190 static const Z_STRUCT_SECTION_ITERABLE(_k_object_assignment, \
191 _CONCAT(_object_access_, name_)) = \
Andrew Boie877f82e2017-10-17 11:20:22 -0700192 { (&_k_thread_obj_ ## name_), \
193 (_CONCAT(_object_list_, name_)) }
194
Andrew Boie945af952017-08-22 13:15:23 -0700195#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700196#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700197#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie78757072019-07-23 13:29:30 -0700198#define K_OBJ_FLAG_DRIVER BIT(3)
Andrew Boie945af952017-08-22 13:15:23 -0700199
200/**
201 * Lookup a kernel object and init its metadata if it exists
202 *
203 * Calling this on an object will make it usable from userspace.
204 * Intended to be called as the last statement in kernel object init
205 * functions.
206 *
Anas Nashif50e3acd2018-12-08 13:26:18 -0500207 * @param obj Address of the kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700208 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700209void z_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700210#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700211/* LCOV_EXCL_START */
Andrew Boie877f82e2017-10-17 11:20:22 -0700212#define K_THREAD_ACCESS_GRANT(thread, ...)
213
Anas Nashif954d5502018-02-25 08:37:28 -0600214/**
215 * @internal
216 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700217static inline void z_object_init(void *obj)
Andrew Boie743e4682017-10-04 12:25:50 -0700218{
219 ARG_UNUSED(obj);
220}
221
Anas Nashif954d5502018-02-25 08:37:28 -0600222/**
223 * @internal
224 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700225static inline void z_impl_k_object_access_grant(void *object,
Andrew Boie743e4682017-10-04 12:25:50 -0700226 struct k_thread *thread)
227{
228 ARG_UNUSED(object);
229 ARG_UNUSED(thread);
230}
231
Anas Nashif954d5502018-02-25 08:37:28 -0600232/**
233 * @internal
234 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700235static inline void k_object_access_revoke(void *object,
236 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700237{
238 ARG_UNUSED(object);
239 ARG_UNUSED(thread);
240}
241
Andrew Boiee9cfc542018-04-13 13:15:28 -0700242/**
243 * @internal
244 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700245static inline void z_impl_k_object_release(void *object)
Andrew Boiee9cfc542018-04-13 13:15:28 -0700246{
247 ARG_UNUSED(object);
248}
249
Andrew Boie41bab6e2017-10-14 14:42:23 -0700250static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700251{
252 ARG_UNUSED(object);
253}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700254/* LCOV_EXCL_STOP */
Andrew Boie743e4682017-10-04 12:25:50 -0700255#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700256
257/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600258 * Grant a thread access to a kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700259 *
260 * The thread will be granted access to the object if the caller is from
261 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700262 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700263 *
264 * @param object Address of kernel object
265 * @param thread Thread to grant access to the object
266 */
Andrew Boie743e4682017-10-04 12:25:50 -0700267__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700268
Andrew Boiea89bf012017-10-09 14:47:55 -0700269/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600270 * Revoke a thread's access to a kernel object
Andrew Boiea89bf012017-10-09 14:47:55 -0700271 *
272 * The thread will lose access to the object if the caller is from
273 * supervisor mode, or the caller is from user mode AND has permissions
274 * on both the object and the thread whose access is being revoked.
275 *
276 * @param object Address of kernel object
277 * @param thread Thread to remove access to the object
278 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700279void k_object_access_revoke(void *object, struct k_thread *thread);
280
281
282__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700283
284/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600285 * Grant all present and future threads access to an object
Andrew Boie3b5ae802017-10-04 12:10:32 -0700286 *
287 * If the caller is from supervisor mode, or the caller is from user mode and
288 * have sufficient permissions on the object, then that object will have
289 * permissions granted to it for *all* current and future threads running in
290 * the system, effectively becoming a public kernel object.
291 *
292 * Use of this API should be avoided on systems that are running untrusted code
293 * as it is possible for such code to derive the addresses of kernel objects
294 * and perform unwanted operations on them.
295 *
Andrew Boie04caa672017-10-13 13:57:07 -0700296 * It is not possible to revoke permissions on public objects; once public,
297 * any thread may use it.
298 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700299 * @param object Address of kernel object
300 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700301void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700302
Andrew Boie31bdfc02017-11-08 16:38:03 -0800303/**
304 * Allocate a kernel object of a designated type
305 *
306 * This will instantiate at runtime a kernel object of the specified type,
307 * returning a pointer to it. The object will be returned in an uninitialized
308 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700309 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800310 *
311 * Currently, allocation of thread stacks is not supported.
312 *
313 * @param otype Requested kernel object type
314 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
315 * available
316 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700317__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800318
Andrew Boie97bf0012018-04-24 17:01:37 -0700319#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800320/**
321 * Free a kernel object previously allocated with k_object_alloc()
322 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700323 * This will return memory for a kernel object back to resource pool it was
324 * allocated from. Care must be exercised that the object will not be used
325 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800326 *
327 * @param obj Pointer to the kernel object memory address.
328 */
329void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700330#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700331/* LCOV_EXCL_START */
Patrik Flykt4344e272019-03-08 14:19:05 -0700332static inline void *z_impl_k_object_alloc(enum k_objects otype)
Andrew Boie97bf0012018-04-24 17:01:37 -0700333{
Kumar Gala85699f72018-05-17 09:26:03 -0500334 ARG_UNUSED(otype);
335
Andrew Boie97bf0012018-04-24 17:01:37 -0700336 return NULL;
337}
338
339static inline void k_obj_free(void *obj)
340{
341 ARG_UNUSED(obj);
342}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700343/* LCOV_EXCL_STOP */
Andrew Boie31bdfc02017-11-08 16:38:03 -0800344#endif /* CONFIG_DYNAMIC_OBJECTS */
345
Anas Nashif4bcb2942019-01-23 23:06:29 -0500346/** @} */
347
Andrew Boiebca15da2017-10-15 14:17:48 -0700348/* Using typedef deliberately here, this is quite intended to be an opaque
Andrew Boie4e5c0932019-04-04 12:05:28 -0700349 * type.
Andrew Boiebca15da2017-10-15 14:17:48 -0700350 *
351 * The purpose of this data type is to clearly distinguish between the
352 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
353 * buffer which composes the stack data actually used by the underlying
Anas Nashiff2cb20c2019-06-18 14:45:40 -0400354 * thread; they cannot be used interchangeably as some arches precede the
Andrew Boiebca15da2017-10-15 14:17:48 -0700355 * stack buffer region with guard areas that trigger a MPU or MMU fault
356 * if written to.
357 *
358 * APIs that want to work with the buffer inside should continue to use
359 * char *.
360 *
361 * Stacks should always be created with K_THREAD_STACK_DEFINE().
362 */
363struct __packed _k_thread_stack_element {
364 char data;
365};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700366typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700367
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700368/**
369 * @typedef k_thread_entry_t
370 * @brief Thread entry point function type.
371 *
372 * A thread's entry point function is invoked when the thread starts executing.
373 * Up to 3 argument values can be passed to the function.
374 *
375 * The thread terminates execution permanently if the entry point function
376 * returns. The thread is responsible for releasing any shared resources
377 * it may own (such as mutexes and dynamically allocated memory), prior to
378 * returning.
379 *
380 * @param p1 First argument.
381 * @param p2 Second argument.
382 * @param p3 Third argument.
383 *
384 * @return N/A
385 */
386typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700387
388#ifdef CONFIG_THREAD_MONITOR
389struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700390 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700391 void *parameter1;
392 void *parameter2;
393 void *parameter3;
394};
395#endif
396
397/* can be used for creating 'dummy' threads, e.g. for pending on objects */
398struct _thread_base {
399
400 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700401 union {
Peter A. Bigot82ad0d22019-01-03 23:49:28 -0600402 sys_dnode_t qnode_dlist;
Andy Ross1acd8c22018-05-03 14:51:49 -0700403 struct rbnode qnode_rb;
404 };
405
Andy Ross1acd8c22018-05-03 14:51:49 -0700406 /* wait queue on which the thread is pended (needed only for
407 * trees, not dumb lists)
408 */
409 _wait_q_t *pended_on;
Andrew Boie73abd322017-04-04 13:19:13 -0700410
411 /* user facing 'thread options'; values defined in include/kernel.h */
412 u8_t user_options;
413
414 /* thread state */
415 u8_t thread_state;
416
417 /*
418 * scheduler lock count and thread priority
419 *
420 * These two fields control the preemptibility of a thread.
421 *
422 * When the scheduler is locked, sched_locked is decremented, which
423 * means that the scheduler is locked for values from 0xff to 0x01. A
424 * thread is coop if its prio is negative, thus 0x80 to 0xff when
425 * looked at the value as unsigned.
426 *
427 * By putting them end-to-end, this means that a thread is
428 * non-preemptible if the bundled value is greater than or equal to
429 * 0x0080.
430 */
431 union {
432 struct {
433#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
434 u8_t sched_locked;
435 s8_t prio;
436#else /* LITTLE and PDP */
437 s8_t prio;
438 u8_t sched_locked;
439#endif
440 };
441 u16_t preempt;
442 };
443
Andy Ross4a2e50f2018-05-15 11:06:25 -0700444#ifdef CONFIG_SCHED_DEADLINE
445 int prio_deadline;
446#endif
447
Andy Ross1acd8c22018-05-03 14:51:49 -0700448 u32_t order_key;
449
Andy Ross2724fd12018-01-29 14:55:20 -0800450#ifdef CONFIG_SMP
451 /* True for the per-CPU idle threads */
452 u8_t is_idle;
453
Andy Ross2724fd12018-01-29 14:55:20 -0800454 /* CPU index on which thread was last run */
455 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700456
457 /* Recursive count of irq_lock() calls */
458 u8_t global_lock_count;
Andy Rossab46b1b2019-01-30 15:00:42 -0800459
460#endif
461
462#ifdef CONFIG_SCHED_CPU_MASK
463 /* "May run on" bits for each CPU */
464 u8_t cpu_mask;
Andy Ross2724fd12018-01-29 14:55:20 -0800465#endif
466
Andrew Boie73abd322017-04-04 13:19:13 -0700467 /* data returned by APIs */
468 void *swap_data;
469
470#ifdef CONFIG_SYS_CLOCK_EXISTS
471 /* this thread's entry in a timeout queue */
472 struct _timeout timeout;
473#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700474};
475
476typedef struct _thread_base _thread_base_t;
477
478#if defined(CONFIG_THREAD_STACK_INFO)
479/* Contains the stack information of a thread */
480struct _thread_stack_info {
Andrew Boie4e5c0932019-04-04 12:05:28 -0700481 /* Stack start - Represents the start address of the thread-writable
482 * stack area.
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700483 */
Nicolas Pitre58d839b2019-05-21 11:32:21 -0400484 uintptr_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700485
486 /* Stack Size - Thread writable stack buffer size. Represents
487 * the size of the actual area, starting from the start member,
488 * that should be writable by the thread
489 */
Andrew Boie73abd322017-04-04 13:19:13 -0700490 u32_t size;
491};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700492
493typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700494#endif /* CONFIG_THREAD_STACK_INFO */
495
Chunlin Hane9c97022017-07-07 20:29:30 +0800496#if defined(CONFIG_USERSPACE)
497struct _mem_domain_info {
498 /* memory domain queue node */
499 sys_dnode_t mem_domain_q_node;
500 /* memory domain of the thread */
501 struct k_mem_domain *mem_domain;
502};
503
504#endif /* CONFIG_USERSPACE */
505
Daniel Leungfc182432018-08-16 15:42:28 -0700506#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
507struct _thread_userspace_local_data {
508 int errno_var;
509};
510#endif
511
Anas Nashifce78d162018-05-24 12:43:11 -0500512/**
513 * @ingroup thread_apis
514 * Thread Structure
515 */
Andrew Boie73abd322017-04-04 13:19:13 -0700516struct k_thread {
517
518 struct _thread_base base;
519
Anas Nashifce78d162018-05-24 12:43:11 -0500520 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700521 struct _callee_saved callee_saved;
522
Anas Nashifce78d162018-05-24 12:43:11 -0500523 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700524 void *init_data;
525
Anas Nashifce78d162018-05-24 12:43:11 -0500526 /**
527 * abort function
528 * @req K-THREAD-002
529 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700530 void (*fn_abort)(void);
531
532#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500533 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700534 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700535
Anas Nashifce78d162018-05-24 12:43:11 -0500536 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700537 struct k_thread *next_thread;
538#endif
539
Anas Nashif57554052018-03-03 02:31:05 -0600540#if defined(CONFIG_THREAD_NAME)
541 /* Thread name */
Andrew Boie38129ce2019-06-25 08:54:37 -0700542 char name[CONFIG_THREAD_MAX_NAME_LEN];
Anas Nashif57554052018-03-03 02:31:05 -0600543#endif
544
Andrew Boie73abd322017-04-04 13:19:13 -0700545#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500546 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700547 void *custom_data;
548#endif
549
Daniel Leungfc182432018-08-16 15:42:28 -0700550#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
551 struct _thread_userspace_local_data *userspace_local_data;
552#endif
553
Andrew Boie73abd322017-04-04 13:19:13 -0700554#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700555#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500556 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700557 int errno_var;
558#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700559#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700560
561#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500562 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700563 struct _thread_stack_info stack_info;
564#endif /* CONFIG_THREAD_STACK_INFO */
565
Chunlin Hane9c97022017-07-07 20:29:30 +0800566#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500567 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800568 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500569 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700570 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800571#endif /* CONFIG_USERSPACE */
572
Andy Ross042d8ec2017-12-09 08:37:20 -0800573#if defined(CONFIG_USE_SWITCH)
574 /* When using __switch() a few previously arch-specific items
575 * become part of the core OS
576 */
577
Patrik Flykt4344e272019-03-08 14:19:05 -0700578 /** z_swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800579 int swap_retval;
580
Patrik Flykt7c0a2452019-03-14 09:20:46 -0600581 /** Context handle returned via z_arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800582 void *switch_handle;
583#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500584 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700585 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800586
Anas Nashifce78d162018-05-24 12:43:11 -0500587 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700588 struct _thread_arch arch;
589};
590
591typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400592typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400593
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400594enum execution_context_types {
595 K_ISR = 0,
596 K_COOP_THREAD,
597 K_PREEMPT_THREAD,
598};
599
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400600/**
Anas Nashif4bcb2942019-01-23 23:06:29 -0500601 * @addtogroup thread_apis
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100602 * @{
603 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530604typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
605 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100606
607/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530608 * @brief Iterate over all the threads in the system.
609 *
610 * This routine iterates over all the threads in the system and
611 * calls the user_cb function for each thread.
612 *
613 * @param user_cb Pointer to the user callback function.
614 * @param user_data Pointer to user data.
615 *
616 * @note CONFIG_THREAD_MONITOR must be set for this function
617 * to be effective. Also this API uses irq_lock to protect the
618 * _kernel.threads list which means creation of new threads and
619 * terminations of existing threads are blocked until this
620 * API returns.
621 *
622 * @return N/A
623 */
624extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
625
Anas Nashif166f5192018-02-25 08:02:36 -0600626/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100627
628/**
Allan Stephensc98da842016-11-11 15:45:03 -0500629 * @defgroup thread_apis Thread APIs
630 * @ingroup kernel_apis
631 * @{
632 */
633
Benjamin Walshed240f22017-01-22 13:05:08 -0500634#endif /* !_ASMLANGUAGE */
635
636
637/*
638 * Thread user options. May be needed by assembly code. Common part uses low
639 * bits, arch-specific use high bits.
640 */
641
Anas Nashifa541e932018-05-24 11:19:16 -0500642/**
643 * @brief system thread that must not abort
644 * @req K-THREAD-000
645 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700646#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500647
648#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500649/**
650 * @brief thread uses floating point registers
651 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700652#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500653#endif
654
Anas Nashifa541e932018-05-24 11:19:16 -0500655/**
656 * @brief user mode thread
657 *
658 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700659 * has additional restrictions
660 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700661#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700662
Anas Nashifa541e932018-05-24 11:19:16 -0500663/**
664 * @brief Inherit Permissions
665 *
666 * @details
667 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700668 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
669 * is not enabled.
670 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700671#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700672
Benjamin Walshed240f22017-01-22 13:05:08 -0500673#ifdef CONFIG_X86
674/* x86 Bitmask definitions for threads user options */
675
676#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
677/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700678#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500679#endif
680#endif
681
682/* end - thread options */
683
684#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400685/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700686 * @brief Create a thread.
687 *
688 * This routine initializes a thread, then schedules it for execution.
689 *
690 * The new thread may be scheduled for immediate execution or a delayed start.
691 * If the newly spawned thread does not have a delayed start the kernel
692 * scheduler may preempt the current thread to allow the new thread to
693 * execute.
694 *
695 * Thread options are architecture-specific, and can include K_ESSENTIAL,
696 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
697 * them using "|" (the logical OR operator).
698 *
699 * Historically, users often would use the beginning of the stack memory region
700 * to store the struct k_thread data, although corruption will occur if the
701 * stack overflows this region and stack protection features may not detect this
702 * situation.
703 *
704 * @param new_thread Pointer to uninitialized struct k_thread
705 * @param stack Pointer to the stack space.
706 * @param stack_size Stack size in bytes.
707 * @param entry Thread entry function.
708 * @param p1 1st entry point parameter.
709 * @param p2 2nd entry point parameter.
710 * @param p3 3rd entry point parameter.
711 * @param prio Thread priority.
712 * @param options Thread options.
713 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
714 *
715 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400716 *
717 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700718 */
Andrew Boie662c3452017-10-02 10:51:18 -0700719__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700720 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700721 size_t stack_size,
722 k_thread_entry_t entry,
723 void *p1, void *p2, void *p3,
724 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700725
Andrew Boie3f091b52017-08-30 14:34:14 -0700726/**
727 * @brief Drop a thread's privileges permanently to user mode
728 *
729 * @param entry Function to start executing from
730 * @param p1 1st entry point parameter
731 * @param p2 2nd entry point parameter
732 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400733 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700734 */
735extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
736 void *p1, void *p2,
737 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700738
Andrew Boied26cf2d2017-03-30 13:07:02 -0700739/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530740 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700741 *
742 * This is a convenience function. For the provided thread, grant access to
743 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700744 *
745 * The thread object must be initialized (i.e. running). The objects don't
746 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530747 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700748 *
749 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530750 * @param ... list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400751 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700752 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530753#define k_thread_access_grant(thread, ...) \
754 FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700755
756/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700757 * @brief Assign a resource memory pool to a thread
758 *
759 * By default, threads have no resource pool assigned unless their parent
760 * thread has a resource pool, in which case it is inherited. Multiple
761 * threads may be assigned to the same memory pool.
762 *
763 * Changing a thread's resource pool will not migrate allocations from the
764 * previous pool.
765 *
766 * @param thread Target thread to assign a memory pool for resource requests,
767 * or NULL if the thread should no longer have a memory pool.
768 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400769 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700770 */
771static inline void k_thread_resource_pool_assign(struct k_thread *thread,
772 struct k_mem_pool *pool)
773{
774 thread->resource_pool = pool;
775}
776
777#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
778/**
779 * @brief Assign the system heap as a thread's resource pool
780 *
781 * Similar to k_thread_resource_pool_assign(), but the thread will use
782 * the kernel heap to draw memory.
783 *
784 * Use with caution, as a malicious thread could perform DoS attacks on the
785 * kernel heap.
786 *
787 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400788 *
789 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700790 */
791void k_thread_system_pool_assign(struct k_thread *thread);
792#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
793
794/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500795 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400796 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700797 * This routine puts the current thread to sleep for @a duration milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400798 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700799 * @param ms Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400800 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200801 * @return Zero if the requested time has elapsed or the number of milliseconds
802 * left to sleep, if thread was woken up by \ref k_wakeup call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400803 */
Charles E. Yousea5678312019-05-09 16:46:46 -0700804__syscall s32_t k_sleep(s32_t ms);
805
806/**
807 * @brief Put the current thread to sleep with microsecond resolution.
808 *
809 * This function is unlikely to work as expected without kernel tuning.
810 * In particular, because the lower bound on the duration of a sleep is
811 * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted
812 * to achieve the resolution desired. The implications of doing this must
813 * be understood before attempting to use k_usleep(). Use with caution.
814 *
815 * @param us Number of microseconds to sleep.
816 *
817 * @return Zero if the requested time has elapsed or the number of microseconds
818 * left to sleep, if thread was woken up by \ref k_wakeup call.
819 */
820__syscall s32_t k_usleep(s32_t us);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400821
822/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500823 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400824 *
825 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500826 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400828 * @return N/A
829 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800830__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400831
832/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500833 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400834 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500835 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500837 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838 *
839 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400840 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400841 */
Andrew Boie468190a2017-09-29 14:00:48 -0700842__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400843
844/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500845 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500847 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500849 * If @a thread is not currently sleeping, the routine has no effect.
850 *
851 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400852 *
853 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400854 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400855 */
Andrew Boie468190a2017-09-29 14:00:48 -0700856__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400857
858/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500859 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500861 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400862 *
863 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400864 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700865__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400866
867/**
Allan Stephensc98da842016-11-11 15:45:03 -0500868 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500870 * This routine permanently stops execution of @a thread. The thread is taken
871 * off all kernel queues it is part of (i.e. the ready queue, the timeout
872 * queue, or a kernel object wait queue). However, any kernel resources the
873 * thread might currently own (such as mutexes or memory blocks) are not
874 * released. It is the responsibility of the caller of this routine to ensure
875 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500877 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400878 *
879 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400880 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881 */
Andrew Boie468190a2017-09-29 14:00:48 -0700882__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400883
Andrew Boie7d627c52017-08-30 11:01:56 -0700884
885/**
886 * @brief Start an inactive thread
887 *
888 * If a thread was created with K_FOREVER in the delay parameter, it will
889 * not be added to the scheduling queue until this function is called
890 * on it.
891 *
892 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400893 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700894 */
Andrew Boie468190a2017-09-29 14:00:48 -0700895__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700896
Allan Stephensc98da842016-11-11 15:45:03 -0500897/**
898 * @cond INTERNAL_HIDDEN
899 */
900
Benjamin Walshd211a522016-12-06 11:44:01 -0500901/* timeout has timed out and is not on _timeout_q anymore */
902#define _EXPIRED (-2)
903
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400904struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700905 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700906 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400907 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700908 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500909 void *init_p1;
910 void *init_p2;
911 void *init_p3;
912 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500913 u32_t init_options;
914 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500915 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600916 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400917};
918
Andrew Boied26cf2d2017-03-30 13:07:02 -0700919#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400920 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600921 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500922 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700923 .init_thread = (thread), \
924 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500925 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700926 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400927 .init_p1 = (void *)p1, \
928 .init_p2 = (void *)p2, \
929 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500930 .init_prio = (prio), \
931 .init_options = (options), \
932 .init_delay = (delay), \
933 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600934 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400935 }
936
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400937/**
Allan Stephensc98da842016-11-11 15:45:03 -0500938 * INTERNAL_HIDDEN @endcond
939 */
940
941/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500942 * @brief Statically define and initialize a thread.
943 *
944 * The thread may be scheduled for immediate execution or a delayed start.
945 *
946 * Thread options are architecture-specific, and can include K_ESSENTIAL,
947 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
948 * them using "|" (the logical OR operator).
949 *
950 * The ID of the thread can be accessed using:
951 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500952 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500953 *
954 * @param name Name of the thread.
955 * @param stack_size Stack size in bytes.
956 * @param entry Thread entry function.
957 * @param p1 1st entry point parameter.
958 * @param p2 2nd entry point parameter.
959 * @param p3 3rd entry point parameter.
960 * @param prio Thread priority.
961 * @param options Thread options.
962 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400963 *
Anas Nashif47420d02018-05-24 14:20:56 -0400964 * @req K-THREAD-010
965 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400966 * @internal It has been observed that the x86 compiler by default aligns
967 * these _static_thread_data structures to 32-byte boundaries, thereby
968 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400969 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400970 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500971#define K_THREAD_DEFINE(name, stack_size, \
972 entry, p1, p2, p3, \
973 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700974 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400975 struct k_thread _k_thread_obj_##name; \
976 Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\
Andrew Boied26cf2d2017-03-30 13:07:02 -0700977 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
978 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500979 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -0600980 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700981 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400982
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400983/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500984 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500986 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400987 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500988 * @param thread ID of thread whose priority is needed.
989 *
990 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400991 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400992 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700993__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400994
995/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500996 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400997 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500998 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400999 *
1000 * Rescheduling can occur immediately depending on the priority @a thread is
1001 * set to:
1002 *
1003 * - If its priority is raised above the priority of the caller of this
1004 * function, and the caller is preemptible, @a thread will be scheduled in.
1005 *
1006 * - If the caller operates on itself, it lowers its priority below that of
1007 * other threads in the system, and the caller is preemptible, the thread of
1008 * highest priority will be scheduled in.
1009 *
1010 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1011 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1012 * highest priority.
1013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001014 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001015 * @param prio New priority.
1016 *
1017 * @warning Changing the priority of a thread currently involved in mutex
1018 * priority inheritance may result in undefined behavior.
1019 *
1020 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001021 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001022 */
Andrew Boie468190a2017-09-29 14:00:48 -07001023__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001024
Andy Ross4a2e50f2018-05-15 11:06:25 -07001025
1026#ifdef CONFIG_SCHED_DEADLINE
1027/**
1028 * @brief Set deadline expiration time for scheduler
1029 *
1030 * This sets the "deadline" expiration as a time delta from the
1031 * current time, in the same units used by k_cycle_get_32(). The
1032 * scheduler (when deadline scheduling is enabled) will choose the
1033 * next expiring thread when selecting between threads at the same
1034 * static priority. Threads at different priorities will be scheduled
1035 * according to their static priority.
1036 *
1037 * @note Deadlines that are negative (i.e. in the past) are still seen
1038 * as higher priority than others, even if the thread has "finished"
1039 * its work. If you don't want it scheduled anymore, you have to
1040 * reset the deadline into the future, block/pend the thread, or
1041 * modify its priority with k_thread_priority_set().
1042 *
1043 * @note Despite the API naming, the scheduler makes no guarantees the
1044 * the thread WILL be scheduled within that deadline, nor does it take
1045 * extra metadata (like e.g. the "runtime" and "period" parameters in
1046 * Linux sched_setattr()) that allows the kernel to validate the
1047 * scheduling for achievability. Such features could be implemented
1048 * above this call, which is simply input to the priority selection
1049 * logic.
1050 *
Anas Nashif240c5162019-06-10 12:25:50 -04001051 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001052 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001053 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1054 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001055 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001056 *
Andy Ross4a2e50f2018-05-15 11:06:25 -07001057 * @param thread A thread on which to set the deadline
1058 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001059 *
1060 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001061 */
1062__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1063#endif
1064
Andy Rossab46b1b2019-01-30 15:00:42 -08001065#ifdef CONFIG_SCHED_CPU_MASK
1066/**
1067 * @brief Sets all CPU enable masks to zero
1068 *
1069 * After this returns, the thread will no longer be schedulable on any
1070 * CPUs. The thread must not be currently runnable.
1071 *
Anas Nashif240c5162019-06-10 12:25:50 -04001072 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001073 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001074 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1075 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001076 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001077 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001078 * @param thread Thread to operate upon
1079 * @return Zero on success, otherwise error code
1080 */
1081int k_thread_cpu_mask_clear(k_tid_t thread);
1082
1083/**
1084 * @brief Sets all CPU enable masks to one
1085 *
1086 * After this returns, the thread will be schedulable on any CPU. The
1087 * thread must not be currently runnable.
1088 *
Anas Nashif240c5162019-06-10 12:25:50 -04001089 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001090 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001091 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1092 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001093 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001094 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001095 * @param thread Thread to operate upon
1096 * @return Zero on success, otherwise error code
1097 */
1098int k_thread_cpu_mask_enable_all(k_tid_t thread);
1099
1100/**
1101 * @brief Enable thread to run on specified CPU
1102 *
1103 * The thread must not be currently runnable.
1104 *
Anas Nashif240c5162019-06-10 12:25:50 -04001105 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001106 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001107 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1108 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001109 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001110 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001111 * @param thread Thread to operate upon
1112 * @param cpu CPU index
1113 * @return Zero on success, otherwise error code
1114 */
1115int k_thread_cpu_mask_enable(k_tid_t thread, int cpu);
1116
1117/**
1118 * @brief Prevent thread to run on specified CPU
1119 *
1120 * The thread must not be currently runnable.
1121 *
Anas Nashif240c5162019-06-10 12:25:50 -04001122 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001123 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001124 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1125 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001126 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001127 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001128 * @param thread Thread to operate upon
1129 * @param cpu CPU index
1130 * @return Zero on success, otherwise error code
1131 */
1132int k_thread_cpu_mask_disable(k_tid_t thread, int cpu);
1133#endif
1134
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001135/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001136 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001137 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001138 * This routine prevents the kernel scheduler from making @a thread the
1139 * current thread. All other internal operations on @a thread are still
1140 * performed; for example, any timeout it is waiting on keeps ticking,
1141 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001143 * If @a thread is already suspended, the routine has no effect.
1144 *
1145 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001146 *
1147 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001148 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001149 */
Andrew Boie468190a2017-09-29 14:00:48 -07001150__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001151
1152/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001153 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001155 * This routine allows the kernel scheduler to make @a thread the current
1156 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001157 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001158 * If @a thread is not currently suspended, the routine has no effect.
1159 *
1160 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001161 *
1162 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001163 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001164 */
Andrew Boie468190a2017-09-29 14:00:48 -07001165__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001166
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001167/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001168 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001169 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001170 * This routine specifies how the scheduler will perform time slicing of
1171 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001172 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001173 * To enable time slicing, @a slice must be non-zero. The scheduler
1174 * ensures that no thread runs for more than the specified time limit
1175 * before other threads of that priority are given a chance to execute.
1176 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001177 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001179 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001180 * execute. Once the scheduler selects a thread for execution, there is no
1181 * minimum guaranteed time the thread will execute before threads of greater or
1182 * equal priority are scheduled.
1183 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001184 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001185 * for execution, this routine has no effect; the thread is immediately
1186 * rescheduled after the slice period expires.
1187 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001188 * To disable timeslicing, set both @a slice and @a prio to zero.
1189 *
1190 * @param slice Maximum time slice length (in milliseconds).
1191 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001192 *
1193 * @return N/A
1194 */
Kumar Galacc334c72017-04-21 10:55:34 -05001195extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001196
Anas Nashif166f5192018-02-25 08:02:36 -06001197/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001198
1199/**
1200 * @addtogroup isr_apis
1201 * @{
1202 */
1203
1204/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001205 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001206 *
Allan Stephensc98da842016-11-11 15:45:03 -05001207 * This routine allows the caller to customize its actions, depending on
1208 * whether it is a thread or an ISR.
1209 *
1210 * @note Can be called by ISRs.
1211 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001212 * @return false if invoked by a thread.
1213 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001214 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001215extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001216
Benjamin Walsh445830d2016-11-10 15:54:27 -05001217/**
1218 * @brief Determine if code is running in a preemptible thread.
1219 *
Allan Stephensc98da842016-11-11 15:45:03 -05001220 * This routine allows the caller to customize its actions, depending on
1221 * whether it can be preempted by another thread. The routine returns a 'true'
1222 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001223 *
Allan Stephensc98da842016-11-11 15:45:03 -05001224 * - The code is running in a thread, not at ISR.
1225 * - The thread's priority is in the preemptible range.
1226 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001227 *
Allan Stephensc98da842016-11-11 15:45:03 -05001228 * @note Can be called by ISRs.
1229 *
1230 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001231 * @return Non-zero if invoked by a preemptible thread.
1232 */
Andrew Boie468190a2017-09-29 14:00:48 -07001233__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001234
Allan Stephensc98da842016-11-11 15:45:03 -05001235/**
Anas Nashif166f5192018-02-25 08:02:36 -06001236 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001237 */
1238
1239/**
1240 * @addtogroup thread_apis
1241 * @{
1242 */
1243
1244/**
1245 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001246 *
Allan Stephensc98da842016-11-11 15:45:03 -05001247 * This routine prevents the current thread from being preempted by another
1248 * thread by instructing the scheduler to treat it as a cooperative thread.
1249 * If the thread subsequently performs an operation that makes it unready,
1250 * it will be context switched out in the normal manner. When the thread
1251 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001252 *
Allan Stephensc98da842016-11-11 15:45:03 -05001253 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001254 *
Allan Stephensc98da842016-11-11 15:45:03 -05001255 * @note k_sched_lock() and k_sched_unlock() should normally be used
1256 * when the operation being performed can be safely interrupted by ISRs.
1257 * However, if the amount of processing involved is very small, better
1258 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001259 *
1260 * @return N/A
1261 */
1262extern void k_sched_lock(void);
1263
Allan Stephensc98da842016-11-11 15:45:03 -05001264/**
1265 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001266 *
Allan Stephensc98da842016-11-11 15:45:03 -05001267 * This routine reverses the effect of a previous call to k_sched_lock().
1268 * A thread must call the routine once for each time it called k_sched_lock()
1269 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001270 *
1271 * @return N/A
1272 */
1273extern void k_sched_unlock(void);
1274
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001275/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001276 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001277 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001278 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001279 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001280 * Custom data is not used by the kernel itself, and is freely available
1281 * for a thread to use as it sees fit. It can be used as a framework
1282 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001283 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001284 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001285 *
1286 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001287 *
1288 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001289 */
Andrew Boie468190a2017-09-29 14:00:48 -07001290__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001291
1292/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001293 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001294 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001295 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001297 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001298 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001299 */
Andrew Boie468190a2017-09-29 14:00:48 -07001300__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001301
1302/**
Anas Nashif57554052018-03-03 02:31:05 -06001303 * @brief Set current thread name
1304 *
1305 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1306 * tracing and debugging.
1307 *
Andrew Boie38129ce2019-06-25 08:54:37 -07001308 * @param thread_id Thread to set name, or NULL to set the current thread
1309 * @param value Name string
1310 * @retval 0 on success
1311 * @retval -EFAULT Memory access error with supplied string
1312 * @retval -ENOSYS Thread name configuration option not enabled
1313 * @retval -EINVAL Thread name too long
Anas Nashif57554052018-03-03 02:31:05 -06001314 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001315__syscall int k_thread_name_set(k_tid_t thread_id, const char *value);
Anas Nashif57554052018-03-03 02:31:05 -06001316
1317/**
1318 * @brief Get thread name
1319 *
1320 * Get the name of a thread
1321 *
1322 * @param thread_id Thread ID
Andrew Boie38129ce2019-06-25 08:54:37 -07001323 * @retval Thread name, or NULL if configuration not enabled
Anas Nashif57554052018-03-03 02:31:05 -06001324 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001325const char *k_thread_name_get(k_tid_t thread_id);
1326
1327/**
1328 * @brief Copy the thread name into a supplied buffer
1329 *
1330 * @param thread_id Thread to obtain name information
1331 * @param buf Destination buffer
1332 * @param size Destinatiomn buffer size
1333 * @retval -ENOSPC Destination buffer too small
1334 * @retval -EFAULT Memory access error
1335 * @retval -ENOSYS Thread name feature not enabled
1336 * @retval 0 Success
1337 */
1338__syscall int k_thread_name_copy(k_tid_t thread_id, char *buf,
1339 size_t size);
Anas Nashif57554052018-03-03 02:31:05 -06001340
1341/**
Pavlo Hamov8076c802019-07-31 12:43:54 +03001342 * @brief Get thread state string
1343 *
1344 * Get the human friendly thread state string
1345 *
1346 * @param thread_id Thread ID
1347 * @retval Thread state string, empty if no state flag is set
1348 */
1349const char *k_thread_state_str(k_tid_t thread_id);
1350
1351/**
Andy Rosscfe62032018-09-29 07:34:55 -07001352 * @}
1353 */
1354
1355/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001356 * @addtogroup clock_apis
1357 * @{
1358 */
1359
1360/**
1361 * @brief Generate null timeout delay.
1362 *
1363 * This macro generates a timeout delay that that instructs a kernel API
1364 * not to wait if the requested operation cannot be performed immediately.
1365 *
1366 * @return Timeout delay value.
1367 */
1368#define K_NO_WAIT 0
1369
1370/**
1371 * @brief Generate timeout delay from milliseconds.
1372 *
1373 * This macro generates a timeout delay that that instructs a kernel API
1374 * to wait up to @a ms milliseconds to perform the requested operation.
1375 *
1376 * @param ms Duration in milliseconds.
1377 *
1378 * @return Timeout delay value.
1379 */
Johan Hedberg14471692016-11-13 10:52:15 +02001380#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001381
1382/**
1383 * @brief Generate timeout delay from seconds.
1384 *
1385 * This macro generates a timeout delay that that instructs a kernel API
1386 * to wait up to @a s seconds to perform the requested operation.
1387 *
1388 * @param s Duration in seconds.
1389 *
1390 * @return Timeout delay value.
1391 */
Johan Hedberg14471692016-11-13 10:52:15 +02001392#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001393
1394/**
1395 * @brief Generate timeout delay from minutes.
1396 *
1397 * This macro generates a timeout delay that that instructs a kernel API
1398 * to wait up to @a m minutes to perform the requested operation.
1399 *
1400 * @param m Duration in minutes.
1401 *
1402 * @return Timeout delay value.
1403 */
Johan Hedberg14471692016-11-13 10:52:15 +02001404#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001405
1406/**
1407 * @brief Generate timeout delay from hours.
1408 *
1409 * This macro generates a timeout delay that that instructs a kernel API
1410 * to wait up to @a h hours to perform the requested operation.
1411 *
1412 * @param h Duration in hours.
1413 *
1414 * @return Timeout delay value.
1415 */
Johan Hedberg14471692016-11-13 10:52:15 +02001416#define K_HOURS(h) K_MINUTES((h) * 60)
1417
Allan Stephensc98da842016-11-11 15:45:03 -05001418/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001419 * @brief Generate infinite timeout delay.
1420 *
1421 * This macro generates a timeout delay that that instructs a kernel API
1422 * to wait as long as necessary to perform the requested operation.
1423 *
1424 * @return Timeout delay value.
1425 */
1426#define K_FOREVER (-1)
1427
1428/**
Anas Nashif166f5192018-02-25 08:02:36 -06001429 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001430 */
1431
1432/**
Allan Stephensc98da842016-11-11 15:45:03 -05001433 * @cond INTERNAL_HIDDEN
1434 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001435
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001436struct k_timer {
1437 /*
1438 * _timeout structure must be first here if we want to use
1439 * dynamic timer allocation. timeout.node is used in the double-linked
1440 * list of free timers
1441 */
1442 struct _timeout timeout;
1443
Allan Stephens45bfa372016-10-12 12:39:42 -05001444 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001445 _wait_q_t wait_q;
1446
1447 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001448 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001449
1450 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001451 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001452
1453 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001454 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001455
Allan Stephens45bfa372016-10-12 12:39:42 -05001456 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001457 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001458
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001459 /* user-specific data, also used to support legacy features */
1460 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001461
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001462 _OBJECT_TRACING_NEXT_PTR(k_timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001463};
1464
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001465#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001466 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001467 .timeout = { \
1468 .node = {},\
1469 .dticks = 0, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001470 .fn = z_timer_expiration_handler \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001471 }, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001472 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001473 .expiry_fn = expiry, \
1474 .stop_fn = stop, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001475 .period = 0, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001476 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001477 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001478 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001479 }
1480
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001481#define K_TIMER_INITIALIZER DEPRECATED_MACRO Z_TIMER_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001482
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001483/**
Allan Stephensc98da842016-11-11 15:45:03 -05001484 * INTERNAL_HIDDEN @endcond
1485 */
1486
1487/**
1488 * @defgroup timer_apis Timer APIs
1489 * @ingroup kernel_apis
1490 * @{
1491 */
1492
1493/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001494 * @typedef k_timer_expiry_t
1495 * @brief Timer expiry function type.
1496 *
1497 * A timer's expiry function is executed by the system clock interrupt handler
1498 * each time the timer expires. The expiry function is optional, and is only
1499 * invoked if the timer has been initialized with one.
1500 *
1501 * @param timer Address of timer.
1502 *
1503 * @return N/A
1504 */
1505typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1506
1507/**
1508 * @typedef k_timer_stop_t
1509 * @brief Timer stop function type.
1510 *
1511 * A timer's stop function is executed if the timer is stopped prematurely.
1512 * The function runs in the context of the thread that stops the timer.
1513 * The stop function is optional, and is only invoked if the timer has been
1514 * initialized with one.
1515 *
1516 * @param timer Address of timer.
1517 *
1518 * @return N/A
1519 */
1520typedef void (*k_timer_stop_t)(struct k_timer *timer);
1521
1522/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001523 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001525 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001526 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001527 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001528 *
1529 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001530 * @param expiry_fn Function to invoke each time the timer expires.
1531 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001532 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001533#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001534 Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001535 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001536
Allan Stephens45bfa372016-10-12 12:39:42 -05001537/**
1538 * @brief Initialize a timer.
1539 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001540 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001541 *
1542 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001543 * @param expiry_fn Function to invoke each time the timer expires.
1544 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001545 *
1546 * @return N/A
1547 */
1548extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001549 k_timer_expiry_t expiry_fn,
1550 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001551
Allan Stephens45bfa372016-10-12 12:39:42 -05001552/**
1553 * @brief Start a timer.
1554 *
1555 * This routine starts a timer, and resets its status to zero. The timer
1556 * begins counting down using the specified duration and period values.
1557 *
1558 * Attempting to start a timer that is already running is permitted.
1559 * The timer's status is reset to zero and the timer begins counting down
1560 * using the new duration and period values.
1561 *
1562 * @param timer Address of timer.
1563 * @param duration Initial timer duration (in milliseconds).
1564 * @param period Timer period (in milliseconds).
1565 *
1566 * @return N/A
1567 */
Andrew Boiea354d492017-09-29 16:22:28 -07001568__syscall void k_timer_start(struct k_timer *timer,
1569 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001570
1571/**
1572 * @brief Stop a timer.
1573 *
1574 * This routine stops a running timer prematurely. The timer's stop function,
1575 * if one exists, is invoked by the caller.
1576 *
1577 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001578 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001579 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001580 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1581 * if @a k_timer_stop is to be called from ISRs.
1582 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001583 * @param timer Address of timer.
1584 *
1585 * @return N/A
1586 */
Andrew Boiea354d492017-09-29 16:22:28 -07001587__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001588
1589/**
1590 * @brief Read timer status.
1591 *
1592 * This routine reads the timer's status, which indicates the number of times
1593 * it has expired since its status was last read.
1594 *
1595 * Calling this routine resets the timer's status to zero.
1596 *
1597 * @param timer Address of timer.
1598 *
1599 * @return Timer status.
1600 */
Andrew Boiea354d492017-09-29 16:22:28 -07001601__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001602
1603/**
1604 * @brief Synchronize thread to timer expiration.
1605 *
1606 * This routine blocks the calling thread until the timer's status is non-zero
1607 * (indicating that it has expired at least once since it was last examined)
1608 * or the timer is stopped. If the timer status is already non-zero,
1609 * or the timer is already stopped, the caller continues without waiting.
1610 *
1611 * Calling this routine resets the timer's status to zero.
1612 *
1613 * This routine must not be used by interrupt handlers, since they are not
1614 * allowed to block.
1615 *
1616 * @param timer Address of timer.
1617 *
1618 * @return Timer status.
1619 */
Andrew Boiea354d492017-09-29 16:22:28 -07001620__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001621
Andy Ross52e444b2018-09-28 09:06:37 -07001622extern s32_t z_timeout_remaining(struct _timeout *timeout);
1623
Allan Stephens45bfa372016-10-12 12:39:42 -05001624/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001625 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001626 *
1627 * This routine computes the (approximate) time remaining before a running
1628 * timer next expires. If the timer is not running, it returns zero.
1629 *
1630 * @param timer Address of timer.
1631 *
1632 * @return Remaining time (in milliseconds).
1633 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001634__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001635
Patrik Flykt4344e272019-03-08 14:19:05 -07001636static inline u32_t z_impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001637{
Charles E. Youse0ad40222019-03-01 10:51:04 -08001638 const s32_t ticks = z_timeout_remaining(&timer->timeout);
1639 return (ticks > 0) ? (u32_t)__ticks_to_ms(ticks) : 0U;
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001640}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001641
Allan Stephensc98da842016-11-11 15:45:03 -05001642/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001643 * @brief Associate user-specific data with a timer.
1644 *
1645 * This routine records the @a user_data with the @a timer, to be retrieved
1646 * later.
1647 *
1648 * It can be used e.g. in a timer handler shared across multiple subsystems to
1649 * retrieve data specific to the subsystem this timer is associated with.
1650 *
1651 * @param timer Address of timer.
1652 * @param user_data User data to associate with the timer.
1653 *
1654 * @return N/A
1655 */
Andrew Boiea354d492017-09-29 16:22:28 -07001656__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1657
Anas Nashif954d5502018-02-25 08:37:28 -06001658/**
1659 * @internal
1660 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001661static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
Andrew Boiea354d492017-09-29 16:22:28 -07001662 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001663{
1664 timer->user_data = user_data;
1665}
1666
1667/**
1668 * @brief Retrieve the user-specific data from a timer.
1669 *
1670 * @param timer Address of timer.
1671 *
1672 * @return The user data.
1673 */
Andrew Boiea354d492017-09-29 16:22:28 -07001674__syscall void *k_timer_user_data_get(struct k_timer *timer);
1675
Patrik Flykt4344e272019-03-08 14:19:05 -07001676static inline void *z_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001677{
1678 return timer->user_data;
1679}
1680
Anas Nashif166f5192018-02-25 08:02:36 -06001681/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001682
Allan Stephensc98da842016-11-11 15:45:03 -05001683/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001684 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001685 * @{
1686 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001687
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001688/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001689 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001691 * This routine returns the elapsed time since the system booted,
1692 * in milliseconds.
1693 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001694 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001695 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001696 * While this function returns time in milliseconds, it does
1697 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001698 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001699 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001700 *
1701 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001702 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001703__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001704
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001705/**
1706 * @brief Enable clock always on in tickless kernel
1707 *
Andy Ross1db9f182019-06-25 10:09:45 -07001708 * Deprecated. This does nothing (it was always just a hint). This
1709 * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE
1710 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001711 *
1712 * @retval prev_status Previous status of always on flag
1713 */
Andy Ross1db9f182019-06-25 10:09:45 -07001714/* LCOV_EXCL_START */
1715__deprecated static inline int k_enable_sys_clock_always_on(void)
1716{
1717 __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1718 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1719
1720 return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE);
1721}
1722/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001723
1724/**
1725 * @brief Disable clock always on in tickless kernel
1726 *
Andy Ross1db9f182019-06-25 10:09:45 -07001727 * Deprecated. This does nothing (it was always just a hint). This
1728 * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE
1729 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001730 */
Andy Ross1db9f182019-06-25 10:09:45 -07001731/* LCOV_EXCL_START */
1732__deprecated static inline void k_disable_sys_clock_always_on(void)
1733{
1734 __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1735 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1736}
1737/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001738
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001739/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001740 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001741 *
Peter Bigota6067a32019-08-28 08:19:26 -05001742 * This routine returns the lower 32 bits of the system uptime in
1743 * milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001744 *
Peter Bigota6067a32019-08-28 08:19:26 -05001745 * Because correct conversion requires full precision of the system
1746 * clock there is no benefit to using this over k_uptime_get() unless
1747 * you know the application will never run long enough for the system
1748 * clock to approach 2^32 ticks. Calls to this function may involve
1749 * interrupt blocking and 64-bit math.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001750 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001751 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001752 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001753 * While this function returns time in milliseconds, it does
1754 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001755 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option
David B. Kinder8de9cc72019-06-25 10:44:55 -07001756 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001757 *
Peter Bigota6067a32019-08-28 08:19:26 -05001758 * @return The low 32 bits of the current uptime, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001759 */
Peter Bigota6067a32019-08-28 08:19:26 -05001760static inline u32_t k_uptime_get_32(void)
1761{
1762 return (u32_t)k_uptime_get();
1763}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001764
1765/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001766 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001768 * This routine computes the elapsed time between the current system uptime
1769 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001770 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001771 * @param reftime Pointer to a reference time, which is updated to the current
1772 * uptime upon return.
1773 *
1774 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001775 */
Andy Ross987c0e52018-09-27 16:50:00 -07001776static inline s64_t k_uptime_delta(s64_t *reftime)
1777{
1778 s64_t uptime, delta;
1779
1780 uptime = k_uptime_get();
1781 delta = uptime - *reftime;
1782 *reftime = uptime;
1783
1784 return delta;
1785}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001786
1787/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001788 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001789 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001790 * This routine computes the elapsed time between the current system uptime
1791 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001792 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001793 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1794 * need for interrupt locking and 64-bit math. However, the 32-bit result
1795 * cannot hold an elapsed time larger than approximately 50 days, so the
1796 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001798 * @param reftime Pointer to a reference time, which is updated to the current
1799 * uptime upon return.
1800 *
1801 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001802 */
Andy Ross987c0e52018-09-27 16:50:00 -07001803static inline u32_t k_uptime_delta_32(s64_t *reftime)
1804{
1805 return (u32_t)k_uptime_delta(reftime);
1806}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001807
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001808/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001809 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001810 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001811 * This routine returns the current time, as measured by the system's hardware
1812 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001813 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001814 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001815 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001816#define k_cycle_get_32() z_arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001817
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001818/**
Anas Nashif166f5192018-02-25 08:02:36 -06001819 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001820 */
1821
Allan Stephensc98da842016-11-11 15:45:03 -05001822/**
1823 * @cond INTERNAL_HIDDEN
1824 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001825
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001826struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001827 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001828 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001829 union {
1830 _wait_q_t wait_q;
1831
1832 _POLL_EVENT;
1833 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001834
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001835 _OBJECT_TRACING_NEXT_PTR(k_queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001836};
1837
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001838#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001839 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001840 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Patrik Flykt4344e272019-03-08 14:19:05 -07001841 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001842 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001843 _OBJECT_TRACING_INIT \
1844 }
1845
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001846#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1847
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001848extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1849
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001850/**
1851 * INTERNAL_HIDDEN @endcond
1852 */
1853
1854/**
1855 * @defgroup queue_apis Queue APIs
1856 * @ingroup kernel_apis
1857 * @{
1858 */
1859
1860/**
1861 * @brief Initialize a queue.
1862 *
1863 * This routine initializes a queue object, prior to its first use.
1864 *
1865 * @param queue Address of the queue.
1866 *
1867 * @return N/A
1868 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001869__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001870
1871/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001872 * @brief Cancel waiting on a queue.
1873 *
1874 * This routine causes first thread pending on @a queue, if any, to
1875 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001876 * If the queue is being waited on by k_poll(), it will return with
1877 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1878 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001879 *
1880 * @note Can be called by ISRs.
1881 *
1882 * @param queue Address of the queue.
1883 *
1884 * @return N/A
1885 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001886__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001887
1888/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001889 * @brief Append an element to the end of a queue.
1890 *
1891 * This routine appends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001892 * aligned on a word boundary, and the first word of the item is reserved
1893 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001894 *
1895 * @note Can be called by ISRs.
1896 *
1897 * @param queue Address of the queue.
1898 * @param data Address of the data item.
1899 *
1900 * @return N/A
1901 */
1902extern void k_queue_append(struct k_queue *queue, void *data);
1903
1904/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001905 * @brief Append an element to a queue.
1906 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001907 * This routine appends a data item to @a queue. There is an implicit memory
1908 * allocation to create an additional temporary bookkeeping data structure from
1909 * the calling thread's resource pool, which is automatically freed when the
1910 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001911 *
1912 * @note Can be called by ISRs.
1913 *
1914 * @param queue Address of the queue.
1915 * @param data Address of the data item.
1916 *
1917 * @retval 0 on success
1918 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1919 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301920__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001921
1922/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001923 * @brief Prepend an element to a queue.
1924 *
1925 * This routine prepends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001926 * aligned on a word boundary, and the first word of the item is reserved
1927 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001928 *
1929 * @note Can be called by ISRs.
1930 *
1931 * @param queue Address of the queue.
1932 * @param data Address of the data item.
1933 *
1934 * @return N/A
1935 */
1936extern void k_queue_prepend(struct k_queue *queue, void *data);
1937
1938/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001939 * @brief Prepend an element to a queue.
1940 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001941 * This routine prepends a data item to @a queue. There is an implicit memory
1942 * allocation to create an additional temporary bookkeeping data structure from
1943 * the calling thread's resource pool, which is automatically freed when the
1944 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001945 *
1946 * @note Can be called by ISRs.
1947 *
1948 * @param queue Address of the queue.
1949 * @param data Address of the data item.
1950 *
1951 * @retval 0 on success
1952 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1953 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301954__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001955
1956/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001957 * @brief Inserts an element to a queue.
1958 *
1959 * This routine inserts a data item to @a queue after previous item. A queue
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001960 * data item must be aligned on a word boundary, and the first word of
1961 * the item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001962 *
1963 * @note Can be called by ISRs.
1964 *
1965 * @param queue Address of the queue.
1966 * @param prev Address of the previous data item.
1967 * @param data Address of the data item.
1968 *
1969 * @return N/A
1970 */
1971extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1972
1973/**
1974 * @brief Atomically append a list of elements to a queue.
1975 *
1976 * This routine adds a list of data items to @a queue in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001977 * The data items must be in a singly-linked list, with the first word
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001978 * in each data item pointing to the next data item; the list must be
1979 * NULL-terminated.
1980 *
1981 * @note Can be called by ISRs.
1982 *
1983 * @param queue Address of the queue.
1984 * @param head Pointer to first node in singly-linked list.
1985 * @param tail Pointer to last node in singly-linked list.
1986 *
1987 * @return N/A
1988 */
1989extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1990
1991/**
1992 * @brief Atomically add a list of elements to a queue.
1993 *
1994 * This routine adds a list of data items to @a queue in one operation.
1995 * The data items must be in a singly-linked list implemented using a
1996 * sys_slist_t object. Upon completion, the original list is empty.
1997 *
1998 * @note Can be called by ISRs.
1999 *
2000 * @param queue Address of the queue.
2001 * @param list Pointer to sys_slist_t object.
2002 *
2003 * @return N/A
2004 */
2005extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
2006
2007/**
2008 * @brief Get an element from a queue.
2009 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002010 * This routine removes first data item from @a queue. The first word of the
2011 * data item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002012 *
2013 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2014 *
2015 * @param queue Address of the queue.
2016 * @param timeout Waiting period to obtain a data item (in milliseconds),
2017 * or one of the special values K_NO_WAIT and K_FOREVER.
2018 *
2019 * @return Address of the data item if successful; NULL if returned
2020 * without waiting, or waiting period timed out.
2021 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002022__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002023
2024/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002025 * @brief Remove an element from a queue.
2026 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002027 * This routine removes data item from @a queue. The first word of the
2028 * data item is reserved for the kernel's use. Removing elements from k_queue
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002029 * rely on sys_slist_find_and_remove which is not a constant time operation.
2030 *
2031 * @note Can be called by ISRs
2032 *
2033 * @param queue Address of the queue.
2034 * @param data Address of the data item.
2035 *
2036 * @return true if data item was removed
2037 */
2038static inline bool k_queue_remove(struct k_queue *queue, void *data)
2039{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002040 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002041}
2042
2043/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002044 * @brief Append an element to a queue only if it's not present already.
2045 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002046 * This routine appends data item to @a queue. The first word of the data
2047 * item is reserved for the kernel's use. Appending elements to k_queue
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002048 * relies on sys_slist_is_node_in_list which is not a constant time operation.
2049 *
2050 * @note Can be called by ISRs
2051 *
2052 * @param queue Address of the queue.
2053 * @param data Address of the data item.
2054 *
2055 * @return true if data item was added, false if not
2056 */
2057static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
2058{
2059 sys_sfnode_t *test;
2060
2061 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
2062 if (test == (sys_sfnode_t *) data) {
2063 return false;
2064 }
2065 }
2066
2067 k_queue_append(queue, data);
2068 return true;
2069}
2070
2071/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002072 * @brief Query a queue to see if it has data available.
2073 *
2074 * Note that the data might be already gone by the time this function returns
2075 * if other threads are also trying to read from the queue.
2076 *
2077 * @note Can be called by ISRs.
2078 *
2079 * @param queue Address of the queue.
2080 *
2081 * @return Non-zero if the queue is empty.
2082 * @return 0 if data is available.
2083 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002084__syscall int k_queue_is_empty(struct k_queue *queue);
2085
Patrik Flykt4344e272019-03-08 14:19:05 -07002086static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002087{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002088 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002089}
2090
2091/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002092 * @brief Peek element at the head of queue.
2093 *
2094 * Return element from the head of queue without removing it.
2095 *
2096 * @param queue Address of the queue.
2097 *
2098 * @return Head element, or NULL if queue is empty.
2099 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002100__syscall void *k_queue_peek_head(struct k_queue *queue);
2101
Patrik Flykt4344e272019-03-08 14:19:05 -07002102static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002103{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002104 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002105}
2106
2107/**
2108 * @brief Peek element at the tail of queue.
2109 *
2110 * Return element from the tail of queue without removing it.
2111 *
2112 * @param queue Address of the queue.
2113 *
2114 * @return Tail element, or NULL if queue is empty.
2115 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002116__syscall void *k_queue_peek_tail(struct k_queue *queue);
2117
Patrik Flykt4344e272019-03-08 14:19:05 -07002118static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002119{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002120 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002121}
2122
2123/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002124 * @brief Statically define and initialize a queue.
2125 *
2126 * The queue can be accessed outside the module where it is defined using:
2127 *
2128 * @code extern struct k_queue <name>; @endcode
2129 *
2130 * @param name Name of the queue.
2131 */
2132#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002133 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002134 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002135
Anas Nashif166f5192018-02-25 08:02:36 -06002136/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002137
Wentong Wu5611e922019-06-20 23:51:27 +08002138#ifdef CONFIG_USERSPACE
2139/**
2140 * @brief futex structure
2141 *
2142 * A k_futex is a lightweight mutual exclusion primitive designed
2143 * to minimize kernel involvement. Uncontended operation relies
2144 * only on atomic access to shared memory. k_futex are tracked as
2145 * kernel objects and can live in user memory so any access bypass
2146 * the kernel object permission management mechanism.
2147 */
2148struct k_futex {
2149 atomic_t val;
2150};
2151
2152/**
2153 * @brief futex kernel data structure
2154 *
2155 * z_futex_data are the helper data structure for k_futex to complete
2156 * futex contended operation on kernel side, structure z_futex_data
2157 * of every futex object is invisible in user mode.
2158 */
2159struct z_futex_data {
2160 _wait_q_t wait_q;
2161 struct k_spinlock lock;
2162};
2163
2164#define Z_FUTEX_DATA_INITIALIZER(obj) \
2165 { \
2166 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \
2167 }
2168
2169/**
2170 * @defgroup futex_apis FUTEX APIs
2171 * @ingroup kernel_apis
2172 * @{
2173 */
2174
2175/**
Wentong Wu5611e922019-06-20 23:51:27 +08002176 * @brief Pend the current thread on a futex
2177 *
2178 * Tests that the supplied futex contains the expected value, and if so,
2179 * goes to sleep until some other thread calls k_futex_wake() on it.
2180 *
2181 * @param futex Address of the futex.
2182 * @param expected Expected value of the futex, if it is different the caller
2183 * will not wait on it.
2184 * @param timeout Waiting period on the futex, in milliseconds, or one of the
2185 * special values K_NO_WAIT or K_FOREVER.
2186 * @retval -EACCES Caller does not have read access to futex address.
2187 * @retval -EAGAIN If the futex value did not match the expected parameter.
2188 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2189 * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup.
2190 * @retval 0 if the caller went to sleep and was woken up. The caller
2191 * should check the futex's value on wakeup to determine if it needs
2192 * to block again.
2193 */
2194__syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout);
2195
2196/**
2197 * @brief Wake one/all threads pending on a futex
2198 *
2199 * Wake up the highest priority thread pending on the supplied futex, or
2200 * wakeup all the threads pending on the supplied futex, and the behavior
2201 * depends on wake_all.
2202 *
2203 * @param futex Futex to wake up pending threads.
2204 * @param wake_all If true, wake up all pending threads; If false,
2205 * wakeup the highest priority thread.
2206 * @retval -EACCES Caller does not have access to the futex address.
2207 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2208 * @retval Number of threads that were woken up.
2209 */
2210__syscall int k_futex_wake(struct k_futex *futex, bool wake_all);
2211
2212/** @} */
2213#endif
2214
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002215struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002216 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002217};
2218
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002219/**
2220 * @cond INTERNAL_HIDDEN
2221 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002222#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002223 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002224 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002225 }
2226
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002227#define K_FIFO_INITIALIZER DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002228
Allan Stephensc98da842016-11-11 15:45:03 -05002229/**
2230 * INTERNAL_HIDDEN @endcond
2231 */
2232
2233/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002234 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002235 * @ingroup kernel_apis
2236 * @{
2237 */
2238
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002239/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002240 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002241 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002242 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002243 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002244 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002245 *
2246 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002247 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002248 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002249#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002250 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002251
2252/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002253 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002254 *
2255 * This routine causes first thread pending on @a fifo, if any, to
2256 * return from k_fifo_get() call with NULL value (as if timeout
2257 * expired).
2258 *
2259 * @note Can be called by ISRs.
2260 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002261 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002262 *
2263 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002264 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002265 */
2266#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002267 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002268
2269/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002270 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002271 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002272 * This routine adds a data item to @a fifo. A FIFO data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002273 * aligned on a word boundary, and the first word of the item is reserved
2274 * for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002275 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002276 * @note Can be called by ISRs.
2277 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002278 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002279 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002280 *
2281 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002282 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002283 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002284#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002285 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002286
2287/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002288 * @brief Add an element to a FIFO queue.
2289 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002290 * This routine adds a data item to @a fifo. There is an implicit memory
2291 * allocation to create an additional temporary bookkeeping data structure from
2292 * the calling thread's resource pool, which is automatically freed when the
2293 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002294 *
2295 * @note Can be called by ISRs.
2296 *
2297 * @param fifo Address of the FIFO.
2298 * @param data Address of the data item.
2299 *
2300 * @retval 0 on success
2301 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002302 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002303 */
2304#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002305 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002306
2307/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002308 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002309 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002310 * This routine adds a list of data items to @a fifo in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002311 * The data items must be in a singly-linked list, with the first word of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002312 * each data item pointing to the next data item; the list must be
2313 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002314 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002315 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002316 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002317 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002318 * @param head Pointer to first node in singly-linked list.
2319 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002320 *
2321 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002322 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002323 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002324#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002325 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002326
2327/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002328 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002329 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002330 * This routine adds a list of data items to @a fifo in one operation.
2331 * The data items must be in a singly-linked list implemented using a
2332 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002333 * and must be re-initialized via sys_slist_init().
2334 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002335 * @note Can be called by ISRs.
2336 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002337 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002338 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002339 *
2340 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002341 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002342 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002343#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002344 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002345
2346/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002347 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002349 * This routine removes a data item from @a fifo in a "first in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002350 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002351 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002352 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2353 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002354 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002355 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002356 * or one of the special values K_NO_WAIT and K_FOREVER.
2357 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002358 * @return Address of the data item if successful; NULL if returned
2359 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002360 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002361 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002362#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002363 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002364
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002365/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002366 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002367 *
2368 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002369 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002370 *
2371 * @note Can be called by ISRs.
2372 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002373 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002374 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002375 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002376 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002377 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002378 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002379#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002380 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002381
2382/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002383 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002384 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002385 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302386 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002387 * on each iteration of processing, a head container will be peeked,
2388 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002389 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002390 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002391 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002392 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002393 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002394 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002395 */
2396#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002397 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002398
2399/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002400 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002401 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002402 * Return element from the tail of FIFO queue (without removing it). A usecase
2403 * for this is if elements of the FIFO queue are themselves containers. Then
2404 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002405 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002406 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002407 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002408 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002409 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002410 */
2411#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002412 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002413
2414/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002415 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002416 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002417 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002418 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002419 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002420 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002421 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002422 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002423 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002424#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002425 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002426 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002427
Anas Nashif166f5192018-02-25 08:02:36 -06002428/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002429
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002430struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002431 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002432};
2433
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002434/**
2435 * @cond INTERNAL_HIDDEN
2436 */
2437
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002438#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002439 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002440 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002441 }
2442
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002443#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2444
Allan Stephensc98da842016-11-11 15:45:03 -05002445/**
2446 * INTERNAL_HIDDEN @endcond
2447 */
2448
2449/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002450 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002451 * @ingroup kernel_apis
2452 * @{
2453 */
2454
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002455/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002456 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002457 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002458 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002459 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002460 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002461 *
2462 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002463 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002464 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002465#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002466 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002467
2468/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002469 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002470 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002471 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002472 * aligned on a word boundary, and the first word of the item is
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002473 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002475 * @note Can be called by ISRs.
2476 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002477 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002478 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002479 *
2480 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002481 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002482 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002483#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002484 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002485
2486/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002487 * @brief Add an element to a LIFO queue.
2488 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002489 * This routine adds a data item to @a lifo. There is an implicit memory
2490 * allocation to create an additional temporary bookkeeping data structure from
2491 * the calling thread's resource pool, which is automatically freed when the
2492 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002493 *
2494 * @note Can be called by ISRs.
2495 *
2496 * @param lifo Address of the LIFO.
2497 * @param data Address of the data item.
2498 *
2499 * @retval 0 on success
2500 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002501 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002502 */
2503#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002504 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002505
2506/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002507 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002509 * This routine removes a data item from @a lifo in a "last in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002510 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002511 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002512 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2513 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002514 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002515 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002516 * or one of the special values K_NO_WAIT and K_FOREVER.
2517 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002518 * @return Address of the data item if successful; NULL if returned
2519 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002520 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002521 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002522#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002523 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002524
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002525/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002526 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002527 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002528 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002529 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002530 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002531 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002532 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002533 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002534 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002535#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002536 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002537 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002538
Anas Nashif166f5192018-02-25 08:02:36 -06002539/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002540
2541/**
2542 * @cond INTERNAL_HIDDEN
2543 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302544#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002545
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002546typedef uintptr_t stack_data_t;
2547
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002548struct k_stack {
2549 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002550 struct k_spinlock lock;
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002551 stack_data_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002552
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002553 _OBJECT_TRACING_NEXT_PTR(k_stack)
Andrew Boief3bee952018-05-02 17:44:39 -07002554 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002555};
2556
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002557#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002558 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002559 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002560 .base = stack_buffer, \
2561 .next = stack_buffer, \
2562 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002563 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002564 }
2565
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002566#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2567
Allan Stephensc98da842016-11-11 15:45:03 -05002568/**
2569 * INTERNAL_HIDDEN @endcond
2570 */
2571
2572/**
2573 * @defgroup stack_apis Stack APIs
2574 * @ingroup kernel_apis
2575 * @{
2576 */
2577
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002578/**
2579 * @brief Initialize a stack.
2580 *
2581 * This routine initializes a stack object, prior to its first use.
2582 *
2583 * @param stack Address of the stack.
2584 * @param buffer Address of array used to hold stacked values.
2585 * @param num_entries Maximum number of values that can be stacked.
2586 *
2587 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002588 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002589 */
Andrew Boief3bee952018-05-02 17:44:39 -07002590void k_stack_init(struct k_stack *stack,
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002591 stack_data_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002592
2593
2594/**
2595 * @brief Initialize a stack.
2596 *
2597 * This routine initializes a stack object, prior to its first use. Internal
2598 * buffers will be allocated from the calling thread's resource pool.
2599 * This memory will be released if k_stack_cleanup() is called, or
2600 * userspace is enabled and the stack object loses all references to it.
2601 *
2602 * @param stack Address of the stack.
2603 * @param num_entries Maximum number of values that can be stacked.
2604 *
2605 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002606 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002607 */
2608
Adithya Baglody28080d32018-10-15 11:48:51 +05302609__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2610 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002611
2612/**
2613 * @brief Release a stack's allocated buffer
2614 *
2615 * If a stack object was given a dynamically allocated buffer via
2616 * k_stack_alloc_init(), this will free it. This function does nothing
2617 * if the buffer wasn't dynamically allocated.
2618 *
2619 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002620 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002621 */
2622void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002623
2624/**
2625 * @brief Push an element onto a stack.
2626 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002627 * This routine adds a stack_data_t value @a data to @a stack.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002628 *
2629 * @note Can be called by ISRs.
2630 *
2631 * @param stack Address of the stack.
2632 * @param data Value to push onto the stack.
2633 *
2634 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002635 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002636 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002637__syscall void k_stack_push(struct k_stack *stack, stack_data_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002638
2639/**
2640 * @brief Pop an element from a stack.
2641 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002642 * This routine removes a stack_data_t value from @a stack in a "last in,
2643 * first out" manner and stores the value in @a data.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002644 *
2645 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2646 *
2647 * @param stack Address of the stack.
2648 * @param data Address of area to hold the value popped from the stack.
2649 * @param timeout Waiting period to obtain a value (in milliseconds),
2650 * or one of the special values K_NO_WAIT and K_FOREVER.
2651 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002652 * @retval 0 Element popped from stack.
2653 * @retval -EBUSY Returned without waiting.
2654 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002655 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002656 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002657__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002658
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002659/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002660 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002661 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002662 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002663 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002664 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002665 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002666 * @param name Name of the stack.
2667 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002668 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002669 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002670#define K_STACK_DEFINE(name, stack_num_entries) \
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002671 stack_data_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002672 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002673 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002674 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002675 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002676
Anas Nashif166f5192018-02-25 08:02:36 -06002677/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002678
Allan Stephens6bba9b02016-11-16 14:56:54 -05002679struct k_work;
2680
Allan Stephensc98da842016-11-11 15:45:03 -05002681/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002682 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002683 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002684 */
2685
Allan Stephens6bba9b02016-11-16 14:56:54 -05002686/**
2687 * @typedef k_work_handler_t
2688 * @brief Work item handler function type.
2689 *
2690 * A work item's handler function is executed by a workqueue's thread
2691 * when the work item is processed by the workqueue.
2692 *
2693 * @param work Address of the work item.
2694 *
2695 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002696 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002697 */
2698typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002699
2700/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002701 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002702 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002703
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002704struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002705 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002706 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002707};
2708
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002709enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002710 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002711};
2712
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002713struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002714 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002715 k_work_handler_t handler;
2716 atomic_t flags[1];
2717};
2718
Allan Stephens6bba9b02016-11-16 14:56:54 -05002719struct k_delayed_work {
2720 struct k_work work;
2721 struct _timeout timeout;
2722 struct k_work_q *work_q;
2723};
2724
2725extern struct k_work_q k_sys_work_q;
2726
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002727/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002728 * INTERNAL_HIDDEN @endcond
2729 */
2730
Patrik Flykt4344e272019-03-08 14:19:05 -07002731#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002732 { \
2733 ._reserved = NULL, \
2734 .handler = work_handler, \
2735 .flags = { 0 } \
2736 }
2737
Patrik Flykt4344e272019-03-08 14:19:05 -07002738#define K_WORK_INITIALIZER DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002739
Allan Stephens6bba9b02016-11-16 14:56:54 -05002740/**
2741 * @brief Initialize a statically-defined work item.
2742 *
2743 * This macro can be used to initialize a statically-defined workqueue work
2744 * item, prior to its first use. For example,
2745 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002746 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002747 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002748 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002749 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002750 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002751 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002752#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002753 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002754
2755/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002756 * @brief Initialize a work item.
2757 *
2758 * This routine initializes a workqueue work item, prior to its first use.
2759 *
2760 * @param work Address of work item.
2761 * @param handler Function to invoke each time work item is processed.
2762 *
2763 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002764 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002765 */
2766static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2767{
Patrik Flykt4344e272019-03-08 14:19:05 -07002768 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002769}
2770
2771/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002772 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002773 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002774 * This routine submits work item @a work to be processed by workqueue
2775 * @a work_q. If the work item is already pending in the workqueue's queue
2776 * as a result of an earlier submission, this routine has no effect on the
2777 * work item. If the work item has already been processed, or is currently
2778 * being processed, its work is considered complete and the work item can be
2779 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002780 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002781 * @warning
2782 * A submitted work item must not be modified until it has been processed
2783 * by the workqueue.
2784 *
2785 * @note Can be called by ISRs.
2786 *
2787 * @param work_q Address of workqueue.
2788 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002789 *
2790 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002791 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002792 */
2793static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2794 struct k_work *work)
2795{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002796 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002797 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002798 }
2799}
2800
2801/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002802 * @brief Submit a work item to a user mode workqueue
2803 *
David B. Kinder06d78352018-12-17 14:32:40 -08002804 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002805 * memory allocation is made from the caller's resource pool which is freed
2806 * once the worker thread consumes the k_work item. The workqueue
2807 * thread must have memory access to the k_work item being submitted. The caller
2808 * must have permission granted on the work_q parameter's queue object.
2809 *
2810 * Otherwise this works the same as k_work_submit_to_queue().
2811 *
2812 * @note Can be called by ISRs.
2813 *
2814 * @param work_q Address of workqueue.
2815 * @param work Address of work item.
2816 *
2817 * @retval -EBUSY if the work item was already in some workqueue
2818 * @retval -ENOMEM if no memory for thread resource pool allocation
2819 * @retval 0 Success
2820 * @req K-WORK-001
2821 */
2822static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2823 struct k_work *work)
2824{
2825 int ret = -EBUSY;
2826
2827 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2828 ret = k_queue_alloc_append(&work_q->queue, work);
2829
2830 /* Couldn't insert into the queue. Clear the pending bit
2831 * so the work item can be submitted again
2832 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002833 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002834 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2835 }
2836 }
2837
2838 return ret;
2839}
2840
2841/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002842 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002843 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002844 * This routine indicates if work item @a work is pending in a workqueue's
2845 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002846 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002847 * @note Can be called by ISRs.
2848 *
2849 * @param work Address of work item.
2850 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002851 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002852 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002853 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002854static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002855{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002856 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002857}
2858
2859/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002860 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002861 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002862 * This routine starts workqueue @a work_q. The workqueue spawns its work
2863 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002864 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002865 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002866 * @param stack Pointer to work queue thread's stack space, as defined by
2867 * K_THREAD_STACK_DEFINE()
2868 * @param stack_size Size of the work queue thread's stack (in bytes), which
2869 * should either be the same constant passed to
2870 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002871 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002872 *
2873 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002874 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002875 */
Andrew Boie507852a2017-07-25 18:47:07 -07002876extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002877 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002878 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002879
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002880/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002881 * @brief Start a workqueue in user mode
2882 *
2883 * This works identically to k_work_q_start() except it is callable from user
2884 * mode, and the worker thread created will run in user mode.
2885 * The caller must have permissions granted on both the work_q parameter's
2886 * thread and queue objects, and the same restrictions on priority apply as
2887 * k_thread_create().
2888 *
2889 * @param work_q Address of workqueue.
2890 * @param stack Pointer to work queue thread's stack space, as defined by
2891 * K_THREAD_STACK_DEFINE()
2892 * @param stack_size Size of the work queue thread's stack (in bytes), which
2893 * should either be the same constant passed to
2894 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2895 * @param prio Priority of the work queue's thread.
2896 *
2897 * @return N/A
2898 * @req K-WORK-001
2899 */
2900extern void k_work_q_user_start(struct k_work_q *work_q,
2901 k_thread_stack_t *stack,
2902 size_t stack_size, int prio);
2903
2904/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002905 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002906 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002907 * This routine initializes a workqueue delayed work item, prior to
2908 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002909 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002910 * @param work Address of delayed work item.
2911 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002912 *
2913 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002914 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002915 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002916extern void k_delayed_work_init(struct k_delayed_work *work,
2917 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002918
2919/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002920 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002921 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002922 * This routine schedules work item @a work to be processed by workqueue
2923 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002924 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002925 * Only when the countdown completes is the work item actually submitted to
2926 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002927 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002928 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002929 * counting down cancels the existing submission and restarts the
2930 * countdown using the new delay. Note that this behavior is
2931 * inherently subject to race conditions with the pre-existing
2932 * timeouts and work queue, so care must be taken to synchronize such
2933 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002934 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002935 * @warning
2936 * A delayed work item must not be modified until it has been processed
2937 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002938 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002939 * @note Can be called by ISRs.
2940 *
2941 * @param work_q Address of workqueue.
2942 * @param work Address of delayed work item.
2943 * @param delay Delay before submitting the work item (in milliseconds).
2944 *
2945 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002946 * @retval -EINVAL Work item is being processed or has completed its work.
2947 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002948 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002949 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002950extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2951 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002952 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002953
2954/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002955 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002956 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002957 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002958 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002959 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002960 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002961 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002962 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08002963 * @note The result of calling this on a k_delayed_work item that has
2964 * not been submitted (i.e. before the return of the
2965 * k_delayed_work_submit_to_queue() call) is undefined.
2966 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002967 * @param work Address of delayed work item.
2968 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002969 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002970 * @retval -EINVAL Work item is being processed or has completed its work.
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_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002974
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002975/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002976 * @brief Submit a work item to the system workqueue.
2977 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002978 * This routine submits work item @a work to be processed by the system
2979 * workqueue. If the work item is already pending in the workqueue's queue
2980 * as a result of an earlier submission, this routine has no effect on the
2981 * work item. If the work item has already been processed, or is currently
2982 * being processed, its work is considered complete and the work item can be
2983 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002984 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002985 * @warning
2986 * Work items submitted to the system workqueue should avoid using handlers
2987 * that block or yield since this may prevent the system workqueue from
2988 * processing other work items in a timely manner.
2989 *
2990 * @note Can be called by ISRs.
2991 *
2992 * @param work Address of work item.
2993 *
2994 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002995 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002996 */
2997static inline void k_work_submit(struct k_work *work)
2998{
2999 k_work_submit_to_queue(&k_sys_work_q, work);
3000}
3001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003002/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003003 * @brief Submit a delayed work item to the system workqueue.
3004 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003005 * This routine schedules work item @a work to be processed by the system
3006 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003007 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003008 * Only when the countdown completes is the work item actually submitted to
3009 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003010 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003011 * Submitting a previously submitted delayed work item that is still
3012 * counting down cancels the existing submission and restarts the countdown
3013 * using the new delay. If the work item is currently pending on the
3014 * workqueue's queue because the countdown has completed it is too late to
3015 * resubmit the item, and resubmission fails without impacting the work item.
3016 * If the work item has already been processed, or is currently being processed,
3017 * its work is considered complete and the work item can be resubmitted.
3018 *
3019 * @warning
3020 * Work items submitted to the system workqueue should avoid using handlers
3021 * that block or yield since this may prevent the system workqueue from
3022 * processing other work items in a timely manner.
3023 *
3024 * @note Can be called by ISRs.
3025 *
3026 * @param work Address of delayed work item.
3027 * @param delay Delay before submitting the work item (in milliseconds).
3028 *
3029 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003030 * @retval -EINVAL Work item is being processed or has completed its work.
3031 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003032 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003033 */
3034static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003035 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003036{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05003037 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003038}
3039
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003040/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02003041 * @brief Get time remaining before a delayed work gets scheduled.
3042 *
3043 * This routine computes the (approximate) time remaining before a
3044 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02003045 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02003046 *
3047 * @param work Delayed work item.
3048 *
3049 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003050 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02003051 */
Kumar Galacc334c72017-04-21 10:55:34 -05003052static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02003053{
Andy Ross52e444b2018-09-28 09:06:37 -07003054 return __ticks_to_ms(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02003055}
3056
Anas Nashif166f5192018-02-25 08:02:36 -06003057/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003058/**
Anas Nashifce78d162018-05-24 12:43:11 -05003059 * @defgroup mutex_apis Mutex APIs
3060 * @ingroup kernel_apis
3061 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003062 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003063
Anas Nashifce78d162018-05-24 12:43:11 -05003064/**
3065 * Mutex Structure
3066 * @ingroup mutex_apis
3067 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003068struct k_mutex {
3069 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05003070 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04003071 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05003072 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003073 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003074
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003075 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003076};
3077
Anas Nashifce78d162018-05-24 12:43:11 -05003078/**
3079 * @cond INTERNAL_HIDDEN
3080 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003081#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003082 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003083 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003084 .owner = NULL, \
3085 .lock_count = 0, \
3086 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003087 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003088 }
3089
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003090#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
3091
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003092/**
Allan Stephensc98da842016-11-11 15:45:03 -05003093 * INTERNAL_HIDDEN @endcond
3094 */
3095
3096/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003097 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003098 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003099 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003100 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003101 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003102 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003103 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003104 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003105 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003106#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003107 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003108 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003109
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003110/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003111 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003112 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003113 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003114 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003115 * Upon completion, the mutex is available and does not have an owner.
3116 *
3117 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003118 *
3119 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003120 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003121 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003122__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003123
3124/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003125 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003127 * This routine locks @a mutex. If the mutex is locked by another thread,
3128 * the calling thread waits until the mutex becomes available or until
3129 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003130 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003131 * A thread is permitted to lock a mutex it has already locked. The operation
3132 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003133 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003134 * @param mutex Address of the mutex.
3135 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003136 * or one of the special values K_NO_WAIT and K_FOREVER.
3137 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003138 * @retval 0 Mutex locked.
3139 * @retval -EBUSY Returned without waiting.
3140 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003141 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003142 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003143__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003144
3145/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003146 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003147 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003148 * This routine unlocks @a mutex. The mutex must already be locked by the
3149 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003150 *
3151 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003152 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003153 * thread.
3154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003155 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003156 *
3157 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003158 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003159 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003160__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003161
Allan Stephensc98da842016-11-11 15:45:03 -05003162/**
Anas Nashif166f5192018-02-25 08:02:36 -06003163 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003164 */
3165
3166/**
3167 * @cond INTERNAL_HIDDEN
3168 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003169
3170struct k_sem {
3171 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303172 u32_t count;
3173 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003174 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003175
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003176 _OBJECT_TRACING_NEXT_PTR(k_sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003177};
3178
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003179#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003180 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003181 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003182 .count = initial_count, \
3183 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003184 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003185 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003186 }
3187
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003188#define K_SEM_INITIALIZER DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003189
Allan Stephensc98da842016-11-11 15:45:03 -05003190/**
3191 * INTERNAL_HIDDEN @endcond
3192 */
3193
3194/**
3195 * @defgroup semaphore_apis Semaphore APIs
3196 * @ingroup kernel_apis
3197 * @{
3198 */
3199
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003200/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003201 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003202 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003203 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003204 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003205 * @param sem Address of the semaphore.
3206 * @param initial_count Initial semaphore count.
3207 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003208 *
3209 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003210 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003211 */
Andrew Boie99280232017-09-29 14:17:47 -07003212__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
3213 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003214
3215/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003216 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003218 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003219 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003220 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3221 *
3222 * @param sem Address of the semaphore.
3223 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003224 * or one of the special values K_NO_WAIT and K_FOREVER.
3225 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05003226 * @note When porting code from the nanokernel legacy API to the new API, be
3227 * careful with the return value of this function. The return value is the
3228 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
3229 * non-zero means failure, while the nano_sem_take family returns 1 for success
3230 * and 0 for failure.
3231 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003232 * @retval 0 Semaphore taken.
3233 * @retval -EBUSY Returned without waiting.
3234 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003235 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003236 */
Andrew Boie99280232017-09-29 14:17:47 -07003237__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003238
3239/**
3240 * @brief Give a semaphore.
3241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003242 * This routine gives @a sem, unless the semaphore is already at its maximum
3243 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003244 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003245 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003246 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003247 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003248 *
3249 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003250 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003251 */
Andrew Boie99280232017-09-29 14:17:47 -07003252__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003253
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003254/**
3255 * @brief Reset a semaphore's count to zero.
3256 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003257 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003258 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003259 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003260 *
3261 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003262 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003263 */
Andrew Boie990bf162017-10-03 12:36:49 -07003264__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003265
Anas Nashif954d5502018-02-25 08:37:28 -06003266/**
3267 * @internal
3268 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003269static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003270{
Patrik Flykt24d71432019-03-26 19:57:45 -06003271 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003272}
3273
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003274/**
3275 * @brief Get a semaphore's count.
3276 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003277 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003279 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003280 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003281 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003282 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003283 */
Andrew Boie990bf162017-10-03 12:36:49 -07003284__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003285
Anas Nashif954d5502018-02-25 08:37:28 -06003286/**
3287 * @internal
3288 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003289static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003290{
3291 return sem->count;
3292}
3293
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003294/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003295 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003297 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003298 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003299 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003300 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003301 * @param name Name of the semaphore.
3302 * @param initial_count Initial semaphore count.
3303 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003304 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003305 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003306#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003307 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003308 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303309 BUILD_ASSERT(((count_limit) != 0) && \
3310 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003311
Anas Nashif166f5192018-02-25 08:02:36 -06003312/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003313
3314/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003315 * @defgroup msgq_apis Message Queue APIs
3316 * @ingroup kernel_apis
3317 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003318 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003319
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003320/**
3321 * @brief Message Queue Structure
3322 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003323struct k_msgq {
3324 _wait_q_t wait_q;
Andy Rossbe03dbd2018-07-26 10:23:02 -07003325 struct k_spinlock lock;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003326 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003327 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003328 char *buffer_start;
3329 char *buffer_end;
3330 char *read_ptr;
3331 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003332 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003333
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003334 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Andrew Boie0fe789f2018-04-12 18:35:56 -07003335 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003336};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003337/**
3338 * @cond INTERNAL_HIDDEN
3339 */
3340
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003341
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003342#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003343 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003344 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003345 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003346 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003347 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003348 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003349 .read_ptr = q_buffer, \
3350 .write_ptr = q_buffer, \
3351 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003352 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003353 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003354#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003355/**
3356 * INTERNAL_HIDDEN @endcond
3357 */
3358
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003359
Andrew Boie0fe789f2018-04-12 18:35:56 -07003360#define K_MSGQ_FLAG_ALLOC BIT(0)
3361
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003362/**
3363 * @brief Message Queue Attributes
3364 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303365struct k_msgq_attrs {
3366 size_t msg_size;
3367 u32_t max_msgs;
3368 u32_t used_msgs;
3369};
3370
Allan Stephensc98da842016-11-11 15:45:03 -05003371
3372/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003373 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003374 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003375 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3376 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003377 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3378 * message is similarly aligned to this boundary, @a q_msg_size must also be
3379 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003380 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003381 * The message queue can be accessed outside the module where it is defined
3382 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003383 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003384 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003386 * @param q_name Name of the message queue.
3387 * @param q_msg_size Message size (in bytes).
3388 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003389 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003390 *
3391 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003392 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003393#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3394 static char __noinit __aligned(q_align) \
3395 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3396 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
3397 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003398 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003399
Peter Mitsisd7a37502016-10-13 11:37:40 -04003400/**
3401 * @brief Initialize a message queue.
3402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003403 * This routine initializes a message queue object, prior to its first use.
3404 *
Allan Stephensda827222016-11-09 14:23:58 -06003405 * The message queue's ring buffer must contain space for @a max_msgs messages,
3406 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3407 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3408 * that each message is similarly aligned to this boundary, @a q_msg_size
3409 * must also be a multiple of N.
3410 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003411 * @param q Address of the message queue.
3412 * @param buffer Pointer to ring buffer that holds queued messages.
3413 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003414 * @param max_msgs Maximum number of messages that can be queued.
3415 *
3416 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003417 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003418 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003419void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3420 u32_t max_msgs);
3421
3422/**
3423 * @brief Initialize a message queue.
3424 *
3425 * This routine initializes a message queue object, prior to its first use,
3426 * allocating its internal ring buffer from the calling thread's resource
3427 * pool.
3428 *
3429 * Memory allocated for the ring buffer can be released by calling
3430 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3431 * all of its references.
3432 *
3433 * @param q Address of the message queue.
3434 * @param msg_size Message size (in bytes).
3435 * @param max_msgs Maximum number of messages that can be queued.
3436 *
3437 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3438 * thread's resource pool, or -EINVAL if the size parameters cause
3439 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003440 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003441 */
3442__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3443 u32_t max_msgs);
3444
3445
3446void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003447
3448/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003449 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003450 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003451 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003452 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003453 * @note Can be called by ISRs.
3454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003455 * @param q Address of the message queue.
3456 * @param data Pointer to the message.
3457 * @param timeout Waiting period to add the message (in milliseconds),
3458 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003459 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003460 * @retval 0 Message sent.
3461 * @retval -ENOMSG Returned without waiting or queue purged.
3462 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003463 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003464 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003465__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003466
3467/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003468 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003469 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003470 * This routine receives a message from message queue @a q in a "first in,
3471 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003472 *
Allan Stephensc98da842016-11-11 15:45:03 -05003473 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003475 * @param q Address of the message queue.
3476 * @param data Address of area to hold the received message.
3477 * @param timeout Waiting period to receive the message (in milliseconds),
3478 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003479 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003480 * @retval 0 Message received.
3481 * @retval -ENOMSG Returned without waiting.
3482 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003483 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003484 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003485__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003486
3487/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003488 * @brief Peek/read a message from a message queue.
3489 *
3490 * This routine reads a message from message queue @a q in a "first in,
3491 * first out" manner and leaves the message in the queue.
3492 *
3493 * @note Can be called by ISRs.
3494 *
3495 * @param q Address of the message queue.
3496 * @param data Address of area to hold the message read from the queue.
3497 *
3498 * @retval 0 Message read.
3499 * @retval -ENOMSG Returned when the queue has no message.
3500 * @req K-MSGQ-002
3501 */
3502__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3503
3504/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003505 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003507 * This routine discards all unreceived messages in a message queue's ring
3508 * buffer. Any threads that are blocked waiting to send a message to the
3509 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003510 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003511 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003512 *
3513 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003514 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003515 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003516__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003517
Peter Mitsis67be2492016-10-07 11:44:34 -04003518/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003519 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003521 * This routine returns the number of unused entries in a message queue's
3522 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003523 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003524 * @param q Address of the message queue.
3525 *
3526 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003527 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003528 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003529__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3530
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303531/**
3532 * @brief Get basic attributes of a message queue.
3533 *
3534 * This routine fetches basic attributes of message queue into attr argument.
3535 *
3536 * @param q Address of the message queue.
3537 * @param attrs pointer to message queue attribute structure.
3538 *
3539 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003540 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303541 */
3542__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3543
3544
Patrik Flykt4344e272019-03-08 14:19:05 -07003545static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003546{
3547 return q->max_msgs - q->used_msgs;
3548}
3549
Peter Mitsisd7a37502016-10-13 11:37:40 -04003550/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003551 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003552 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003553 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003554 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003555 * @param q Address of the message queue.
3556 *
3557 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003558 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003559 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003560__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3561
Patrik Flykt4344e272019-03-08 14:19:05 -07003562static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003563{
3564 return q->used_msgs;
3565}
3566
Anas Nashif166f5192018-02-25 08:02:36 -06003567/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003568
3569/**
3570 * @defgroup mem_pool_apis Memory Pool APIs
3571 * @ingroup kernel_apis
3572 * @{
3573 */
3574
Andy Ross73cb9582017-05-09 10:42:39 -07003575/* Note on sizing: the use of a 20 bit field for block means that,
3576 * assuming a reasonable minimum block size of 16 bytes, we're limited
3577 * to 16M of memory managed by a single pool. Long term it would be
3578 * good to move to a variable bit size based on configuration.
3579 */
3580struct k_mem_block_id {
3581 u32_t pool : 8;
3582 u32_t level : 4;
3583 u32_t block : 20;
3584};
3585
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003586struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003587 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003588 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003589};
3590
Anas Nashif166f5192018-02-25 08:02:36 -06003591/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003592
3593/**
3594 * @defgroup mailbox_apis Mailbox APIs
3595 * @ingroup kernel_apis
3596 * @{
3597 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003598
3599struct k_mbox_msg {
3600 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003601 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003602 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003603 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003604 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003605 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003606 /** sender's message data buffer */
3607 void *tx_data;
3608 /** internal use only - needed for legacy API support */
3609 void *_rx_data;
3610 /** message data block descriptor */
3611 struct k_mem_block tx_block;
3612 /** source thread id */
3613 k_tid_t rx_source_thread;
3614 /** target thread id */
3615 k_tid_t tx_target_thread;
3616 /** internal use only - thread waiting on send (may be a dummy) */
3617 k_tid_t _syncing_thread;
3618#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3619 /** internal use only - semaphore used during asynchronous send */
3620 struct k_sem *_async_sem;
3621#endif
3622};
3623
3624struct k_mbox {
3625 _wait_q_t tx_msg_queue;
3626 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003627 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003628
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003629 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003630};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003631/**
3632 * @cond INTERNAL_HIDDEN
3633 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003634
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003635#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003636 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003637 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3638 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003639 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003640 }
3641
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003642#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3643
Peter Mitsis12092702016-10-14 12:57:23 -04003644/**
Allan Stephensc98da842016-11-11 15:45:03 -05003645 * INTERNAL_HIDDEN @endcond
3646 */
3647
3648/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003649 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003651 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003652 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003653 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003654 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003655 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003656 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003657 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003658#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003659 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003660 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003661
Peter Mitsis12092702016-10-14 12:57:23 -04003662/**
3663 * @brief Initialize a mailbox.
3664 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003665 * This routine initializes a mailbox object, prior to its first use.
3666 *
3667 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003668 *
3669 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003670 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003671 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003672extern void k_mbox_init(struct k_mbox *mbox);
3673
Peter Mitsis12092702016-10-14 12:57:23 -04003674/**
3675 * @brief Send a mailbox message in a synchronous manner.
3676 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003677 * This routine sends a message to @a mbox and waits for a receiver to both
3678 * receive and process it. The message data may be in a buffer, in a memory
3679 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003680 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003681 * @param mbox Address of the mailbox.
3682 * @param tx_msg Address of the transmit message descriptor.
3683 * @param timeout Waiting period for the message to be received (in
3684 * milliseconds), or one of the special values K_NO_WAIT
3685 * and K_FOREVER. Once the message has been received,
3686 * this routine waits as long as necessary for the message
3687 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003688 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003689 * @retval 0 Message sent.
3690 * @retval -ENOMSG Returned without waiting.
3691 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003692 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003693 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003694extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003695 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003696
Peter Mitsis12092702016-10-14 12:57:23 -04003697/**
3698 * @brief Send a mailbox message in an asynchronous manner.
3699 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003700 * This routine sends a message to @a mbox without waiting for a receiver
3701 * to process it. The message data may be in a buffer, in a memory pool block,
3702 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3703 * will be given when the message has been both received and completely
3704 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003706 * @param mbox Address of the mailbox.
3707 * @param tx_msg Address of the transmit message descriptor.
3708 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003709 *
3710 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003711 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003712 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003713extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003714 struct k_sem *sem);
3715
Peter Mitsis12092702016-10-14 12:57:23 -04003716/**
3717 * @brief Receive a mailbox message.
3718 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003719 * This routine receives a message from @a mbox, then optionally retrieves
3720 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003722 * @param mbox Address of the mailbox.
3723 * @param rx_msg Address of the receive message descriptor.
3724 * @param buffer Address of the buffer to receive data, or NULL to defer data
3725 * retrieval and message disposal until later.
3726 * @param timeout Waiting period for a message to be received (in
3727 * milliseconds), or one of the special values K_NO_WAIT
3728 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003729 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003730 * @retval 0 Message received.
3731 * @retval -ENOMSG Returned without waiting.
3732 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003733 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003734 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003735extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003736 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003737
3738/**
3739 * @brief Retrieve mailbox message data into a buffer.
3740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003741 * This routine completes the processing of a received message by retrieving
3742 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003743 *
3744 * Alternatively, this routine can be used to dispose of a received message
3745 * without retrieving its data.
3746 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003747 * @param rx_msg Address of the receive message descriptor.
3748 * @param buffer Address of the buffer to receive data, or NULL to discard
3749 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003750 *
3751 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003752 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003753 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003754extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003755
3756/**
3757 * @brief Retrieve mailbox message data into a memory pool block.
3758 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003759 * This routine completes the processing of a received message by retrieving
3760 * its data into a memory pool block, then disposing of the message.
3761 * The memory pool block that results from successful retrieval must be
3762 * returned to the pool once the data has been processed, even in cases
3763 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003764 *
3765 * Alternatively, this routine can be used to dispose of a received message
3766 * without retrieving its data. In this case there is no need to return a
3767 * memory pool block to the pool.
3768 *
3769 * This routine allocates a new memory pool block for the data only if the
3770 * data is not already in one. If a new block cannot be allocated, the routine
3771 * returns a failure code and the received message is left unchanged. This
3772 * permits the caller to reattempt data retrieval at a later time or to dispose
3773 * of the received message without retrieving its data.
3774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003775 * @param rx_msg Address of a receive message descriptor.
3776 * @param pool Address of memory pool, or NULL to discard data.
3777 * @param block Address of the area to hold memory pool block info.
3778 * @param timeout Waiting period to wait for a memory pool block (in
3779 * milliseconds), or one of the special values K_NO_WAIT
3780 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003781 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003782 * @retval 0 Data retrieved.
3783 * @retval -ENOMEM Returned without waiting.
3784 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003785 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003786 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003787extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003788 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003789 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003790
Anas Nashif166f5192018-02-25 08:02:36 -06003791/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003792
3793/**
Anas Nashifce78d162018-05-24 12:43:11 -05003794 * @defgroup pipe_apis Pipe APIs
3795 * @ingroup kernel_apis
3796 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003797 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003798
Anas Nashifce78d162018-05-24 12:43:11 -05003799/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003800struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003801 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3802 size_t size; /**< Buffer size */
3803 size_t bytes_used; /**< # bytes used in buffer */
3804 size_t read_index; /**< Where in buffer to read from */
3805 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08003806 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003807
3808 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003809 _wait_q_t readers; /**< Reader wait queue */
3810 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003811 } wait_q;
3812
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003813 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Anas Nashifce78d162018-05-24 12:43:11 -05003814 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003815};
3816
Anas Nashifce78d162018-05-24 12:43:11 -05003817/**
3818 * @cond INTERNAL_HIDDEN
3819 */
3820#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3821
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003822#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
3823 { \
3824 .buffer = pipe_buffer, \
3825 .size = pipe_buffer_size, \
3826 .bytes_used = 0, \
3827 .read_index = 0, \
3828 .write_index = 0, \
3829 .lock = {}, \
3830 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003831 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
3832 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003833 }, \
3834 _OBJECT_TRACING_INIT \
3835 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003836 }
3837
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003838#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3839
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003840/**
Allan Stephensc98da842016-11-11 15:45:03 -05003841 * INTERNAL_HIDDEN @endcond
3842 */
3843
3844/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003845 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003847 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003848 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003849 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003850 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003851 * @param name Name of the pipe.
3852 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3853 * or zero if no ring buffer is used.
3854 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003855 *
3856 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003857 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003858#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08003859 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07003860 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003861 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003862 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003863
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003864/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003865 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003867 * This routine initializes a pipe object, prior to its first use.
3868 *
3869 * @param pipe Address of the pipe.
3870 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3871 * is used.
3872 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3873 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003874 *
3875 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003876 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003877 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003878void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3879
3880/**
3881 * @brief Release a pipe's allocated buffer
3882 *
3883 * If a pipe object was given a dynamically allocated buffer via
3884 * k_pipe_alloc_init(), this will free it. This function does nothing
3885 * if the buffer wasn't dynamically allocated.
3886 *
3887 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003888 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003889 */
3890void k_pipe_cleanup(struct k_pipe *pipe);
3891
3892/**
3893 * @brief Initialize a pipe and allocate a buffer for it
3894 *
3895 * Storage for the buffer region will be allocated from the calling thread's
3896 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3897 * or userspace is enabled and the pipe object loses all references to it.
3898 *
3899 * This function should only be called on uninitialized pipe objects.
3900 *
3901 * @param pipe Address of the pipe.
3902 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3903 * buffer is used.
3904 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003905 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003906 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003907 */
3908__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003909
3910/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003911 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003912 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003913 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003915 * @param pipe Address of the pipe.
3916 * @param data Address of data to write.
3917 * @param bytes_to_write Size of data (in bytes).
3918 * @param bytes_written Address of area to hold the number of bytes written.
3919 * @param min_xfer Minimum number of bytes to write.
3920 * @param timeout Waiting period to wait for the data to be written (in
3921 * milliseconds), or one of the special values K_NO_WAIT
3922 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003923 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003924 * @retval 0 At least @a min_xfer bytes of data were written.
3925 * @retval -EIO Returned without waiting; zero data bytes were written.
3926 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003927 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003928 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003929 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003930__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3931 size_t bytes_to_write, size_t *bytes_written,
3932 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003933
3934/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003935 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003936 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003937 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003939 * @param pipe Address of the pipe.
3940 * @param data Address to place the data read from pipe.
3941 * @param bytes_to_read Maximum number of data bytes to read.
3942 * @param bytes_read Address of area to hold the number of bytes read.
3943 * @param min_xfer Minimum number of data bytes to read.
3944 * @param timeout Waiting period to wait for the data to be read (in
3945 * milliseconds), or one of the special values K_NO_WAIT
3946 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003947 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003948 * @retval 0 At least @a min_xfer bytes of data were read.
3949 * @retval -EIO Returned without waiting; zero data bytes were read.
3950 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003951 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003952 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003953 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003954__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3955 size_t bytes_to_read, size_t *bytes_read,
3956 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003957
3958/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003959 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003960 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003961 * This routine writes the data contained in a memory block to @a pipe.
3962 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003963 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003964 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003965 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003966 * @param block Memory block containing data to send
3967 * @param size Number of data bytes in memory block to send
3968 * @param sem Semaphore to signal upon completion (else NULL)
3969 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003970 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003971 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003972 */
3973extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3974 size_t size, struct k_sem *sem);
3975
Anas Nashif166f5192018-02-25 08:02:36 -06003976/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003977
Allan Stephensc98da842016-11-11 15:45:03 -05003978/**
3979 * @cond INTERNAL_HIDDEN
3980 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003981
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003982struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003983 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003984 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003985 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003986 char *buffer;
3987 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003988 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003989
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003990 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003991};
3992
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003993#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003994 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003995 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003996 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003997 .num_blocks = slab_num_blocks, \
3998 .block_size = slab_block_size, \
3999 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004000 .free_list = NULL, \
4001 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05004002 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004003 }
4004
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004005#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
4006
4007
Peter Mitsis578f9112016-10-07 13:50:31 -04004008/**
Allan Stephensc98da842016-11-11 15:45:03 -05004009 * INTERNAL_HIDDEN @endcond
4010 */
4011
4012/**
4013 * @defgroup mem_slab_apis Memory Slab APIs
4014 * @ingroup kernel_apis
4015 * @{
4016 */
4017
4018/**
Allan Stephensda827222016-11-09 14:23:58 -06004019 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04004020 *
Allan Stephensda827222016-11-09 14:23:58 -06004021 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004022 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06004023 * @a slab_align -byte boundary. To ensure that each memory block is similarly
4024 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004025 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04004026 *
Allan Stephensda827222016-11-09 14:23:58 -06004027 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004028 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004029 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004030 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004032 * @param name Name of the memory slab.
4033 * @param slab_block_size Size of each memory block (in bytes).
4034 * @param slab_num_blocks Number memory blocks.
4035 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004036 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04004037 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004038#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004039 char __noinit __aligned(WB_UP(slab_align)) \
4040 _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004041 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004042 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004043 WB_UP(slab_block_size), slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004044
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004045/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004046 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004048 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004049 *
Allan Stephensda827222016-11-09 14:23:58 -06004050 * The memory slab's buffer contains @a slab_num_blocks memory blocks
4051 * that are @a slab_block_size bytes long. The buffer must be aligned to an
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004052 * N-byte boundary matching a word boundary, where N is a power of 2
4053 * (i.e. 4 on 32-bit systems, 8, 16, ...).
Allan Stephensda827222016-11-09 14:23:58 -06004054 * To ensure that each memory block is similarly aligned to this boundary,
4055 * @a slab_block_size must also be a multiple of N.
4056 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004057 * @param slab Address of the memory slab.
4058 * @param buffer Pointer to buffer used for the memory blocks.
4059 * @param block_size Size of each memory block (in bytes).
4060 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004061 *
4062 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004063 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004064 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004065extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05004066 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004067
4068/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004069 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004070 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004071 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004073 * @param slab Address of the memory slab.
4074 * @param mem Pointer to block address area.
4075 * @param timeout Maximum time to wait for operation to complete
4076 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4077 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004078 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004079 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004080 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004081 * @retval -ENOMEM Returned without waiting.
4082 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004083 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004084 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004085extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05004086 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004087
4088/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004089 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004090 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004091 * This routine releases a previously allocated memory block back to its
4092 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004094 * @param slab Address of the memory slab.
4095 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004096 *
4097 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004098 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004099 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004100extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004101
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004102/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004103 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004104 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004105 * This routine gets the number of memory blocks that are currently
4106 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004107 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004108 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004110 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004111 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004112 */
Kumar Galacc334c72017-04-21 10:55:34 -05004113static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004114{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004115 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004116}
4117
Peter Mitsisc001aa82016-10-13 13:53:37 -04004118/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004119 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004120 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004121 * This routine gets the number of memory blocks that are currently
4122 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004124 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004125 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004126 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004127 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004128 */
Kumar Galacc334c72017-04-21 10:55:34 -05004129static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004130{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004131 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004132}
4133
Anas Nashif166f5192018-02-25 08:02:36 -06004134/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004135
4136/**
4137 * @cond INTERNAL_HIDDEN
4138 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004139
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004140struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004141 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004142 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004143};
4144
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004145/**
Allan Stephensc98da842016-11-11 15:45:03 -05004146 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004147 */
4148
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004149/**
Allan Stephensc98da842016-11-11 15:45:03 -05004150 * @addtogroup mem_pool_apis
4151 * @{
4152 */
4153
4154/**
4155 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004156 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004157 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4158 * long. The memory pool allows blocks to be repeatedly partitioned into
4159 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004160 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004161 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004162 * If the pool is to be accessed outside the module where it is defined, it
4163 * can be declared via
4164 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004165 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004167 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004168 * @param minsz Size of the smallest blocks in the pool (in bytes).
4169 * @param maxsz Size of the largest blocks in the pool (in bytes).
4170 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004171 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004172 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004173 */
Andy Ross73cb9582017-05-09 10:42:39 -07004174#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004175 char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \
Andy Ross73cb9582017-05-09 10:42:39 -07004176 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004177 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004178 Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004179 .base = { \
4180 .buf = _mpool_buf_##name, \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004181 .max_sz = WB_UP(maxsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004182 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004183 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004184 .levels = _mpool_lvls_##name, \
4185 .flags = SYS_MEM_POOL_KERNEL \
4186 } \
Johann Fischer223a2b92019-07-04 15:55:20 +02004187 }; \
4188 BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK);
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004189
Peter Mitsis937042c2016-10-13 13:18:26 -04004190/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004191 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004192 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004193 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004195 * @param pool Address of the memory pool.
4196 * @param block Pointer to block descriptor for the allocated memory.
4197 * @param size Amount of memory to allocate (in bytes).
4198 * @param timeout Maximum time to wait for operation to complete
4199 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4200 * or K_FOREVER to wait as long as necessary.
4201 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004202 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004203 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004204 * @retval -ENOMEM Returned without waiting.
4205 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004206 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004207 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004208extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004209 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004210
4211/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004212 * @brief Allocate memory from a memory pool with malloc() semantics
4213 *
4214 * Such memory must be released using k_free().
4215 *
4216 * @param pool Address of the memory pool.
4217 * @param size Amount of memory to allocate (in bytes).
4218 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004219 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004220 */
4221extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4222
4223/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004224 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004225 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004226 * This routine releases a previously allocated memory block back to its
4227 * memory pool.
4228 *
4229 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004230 *
4231 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004232 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004233 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004234extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004235
4236/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004237 * @brief Free memory allocated from a memory pool.
4238 *
4239 * This routine releases a previously allocated memory block back to its
4240 * memory pool
4241 *
4242 * @param id Memory block identifier.
4243 *
4244 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004245 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004246 */
4247extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4248
4249/**
Anas Nashif166f5192018-02-25 08:02:36 -06004250 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004251 */
4252
4253/**
4254 * @defgroup heap_apis Heap Memory Pool APIs
4255 * @ingroup kernel_apis
4256 * @{
4257 */
4258
4259/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004260 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004261 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004262 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004263 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004264 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004265 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004267 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004268 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004269 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004270extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004271
4272/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004273 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004274 *
4275 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004276 * returned must have been allocated from the heap memory pool or
4277 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004278 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004279 * If @a ptr is NULL, no operation is performed.
4280 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004281 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004282 *
4283 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004284 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004285 */
4286extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004287
Allan Stephensc98da842016-11-11 15:45:03 -05004288/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004289 * @brief Allocate memory from heap, array style
4290 *
4291 * This routine provides traditional calloc() semantics. Memory is
4292 * allocated from the heap memory pool and zeroed.
4293 *
4294 * @param nmemb Number of elements in the requested array
4295 * @param size Size of each array element (in bytes).
4296 *
4297 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004298 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004299 */
4300extern void *k_calloc(size_t nmemb, size_t size);
4301
Anas Nashif166f5192018-02-25 08:02:36 -06004302/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004303
Benjamin Walshacc68c12017-01-29 18:57:45 -05004304/* polling API - PRIVATE */
4305
Benjamin Walshb0179862017-02-02 16:39:57 -05004306#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004307#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004308#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004309#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004310#endif
4311
Benjamin Walshacc68c12017-01-29 18:57:45 -05004312/* private - implementation data created as needed, per-type */
4313struct _poller {
4314 struct k_thread *thread;
Flavio Ceolin76b35182018-12-16 12:48:29 -08004315 volatile bool is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004316};
4317
4318/* private - types bit positions */
4319enum _poll_types_bits {
4320 /* can be used to ignore an event */
4321 _POLL_TYPE_IGNORE,
4322
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004323 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004324 _POLL_TYPE_SIGNAL,
4325
4326 /* semaphore availability */
4327 _POLL_TYPE_SEM_AVAILABLE,
4328
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004329 /* queue/fifo/lifo data availability */
4330 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004331
4332 _POLL_NUM_TYPES
4333};
4334
Patrik Flykt4344e272019-03-08 14:19:05 -07004335#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004336
4337/* private - states bit positions */
4338enum _poll_states_bits {
4339 /* default state when creating event */
4340 _POLL_STATE_NOT_READY,
4341
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004342 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004343 _POLL_STATE_SIGNALED,
4344
4345 /* semaphore is available */
4346 _POLL_STATE_SEM_AVAILABLE,
4347
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004348 /* data is available to read on queue/fifo/lifo */
4349 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004350
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004351 /* queue/fifo/lifo wait was cancelled */
4352 _POLL_STATE_CANCELLED,
4353
Benjamin Walshacc68c12017-01-29 18:57:45 -05004354 _POLL_NUM_STATES
4355};
4356
Patrik Flykt4344e272019-03-08 14:19:05 -07004357#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004358
4359#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004360 (32 - (0 \
4361 + 8 /* tag */ \
4362 + _POLL_NUM_TYPES \
4363 + _POLL_NUM_STATES \
4364 + 1 /* modes */ \
4365 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004366
Benjamin Walshacc68c12017-01-29 18:57:45 -05004367/* end of polling API - PRIVATE */
4368
4369
4370/**
4371 * @defgroup poll_apis Async polling APIs
4372 * @ingroup kernel_apis
4373 * @{
4374 */
4375
4376/* Public polling API */
4377
4378/* public - values for k_poll_event.type bitfield */
4379#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004380#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4381#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4382#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004383#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004384
4385/* public - polling modes */
4386enum k_poll_modes {
4387 /* polling thread does not take ownership of objects when available */
4388 K_POLL_MODE_NOTIFY_ONLY = 0,
4389
4390 K_POLL_NUM_MODES
4391};
4392
4393/* public - values for k_poll_event.state bitfield */
4394#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004395#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4396#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4397#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004398#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004399#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004400
4401/* public - poll signal object */
4402struct k_poll_signal {
4403 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004404 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004405
4406 /*
4407 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4408 * user resets it to 0.
4409 */
4410 unsigned int signaled;
4411
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004412 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004413 int result;
4414};
4415
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004416#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004417 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004418 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004419 .signaled = 0, \
4420 .result = 0, \
4421 }
4422
4423struct k_poll_event {
4424 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004425 sys_dnode_t _node;
4426
4427 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004428 struct _poller *poller;
4429
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004430 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004431 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004432
Benjamin Walshacc68c12017-01-29 18:57:45 -05004433 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004434 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004435
4436 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004437 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004438
4439 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004440 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004441
4442 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004443 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004444
4445 /* per-type data */
4446 union {
4447 void *obj;
4448 struct k_poll_signal *signal;
4449 struct k_sem *sem;
4450 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004451 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004452 };
4453};
4454
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004455#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004456 { \
4457 .poller = NULL, \
4458 .type = event_type, \
4459 .state = K_POLL_STATE_NOT_READY, \
4460 .mode = event_mode, \
4461 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004462 { .obj = event_obj }, \
4463 }
4464
4465#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4466 event_tag) \
4467 { \
4468 .type = event_type, \
4469 .tag = event_tag, \
4470 .state = K_POLL_STATE_NOT_READY, \
4471 .mode = event_mode, \
4472 .unused = 0, \
4473 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004474 }
4475
4476/**
4477 * @brief Initialize one struct k_poll_event instance
4478 *
4479 * After this routine is called on a poll event, the event it ready to be
4480 * placed in an event array to be passed to k_poll().
4481 *
4482 * @param event The event to initialize.
4483 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4484 * values. Only values that apply to the same object being polled
4485 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4486 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004487 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004488 * @param obj Kernel object or poll signal.
4489 *
4490 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004491 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004492 */
4493
Kumar Galacc334c72017-04-21 10:55:34 -05004494extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004495 int mode, void *obj);
4496
4497/**
4498 * @brief Wait for one or many of multiple poll events to occur
4499 *
4500 * This routine allows a thread to wait concurrently for one or many of
4501 * multiple poll events to have occurred. Such events can be a kernel object
4502 * being available, like a semaphore, or a poll signal event.
4503 *
4504 * When an event notifies that a kernel object is available, the kernel object
4505 * is not "given" to the thread calling k_poll(): it merely signals the fact
4506 * that the object was available when the k_poll() call was in effect. Also,
4507 * all threads trying to acquire an object the regular way, i.e. by pending on
4508 * the object, have precedence over the thread polling on the object. This
4509 * means that the polling thread will never get the poll event on an object
4510 * until the object becomes available and its pend queue is empty. For this
4511 * reason, the k_poll() call is more effective when the objects being polled
4512 * only have one thread, the polling thread, trying to acquire them.
4513 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004514 * When k_poll() returns 0, the caller should loop on all the events that were
4515 * passed to k_poll() and check the state field for the values that were
4516 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004517 *
4518 * Before being reused for another call to k_poll(), the user has to reset the
4519 * state field to K_POLL_STATE_NOT_READY.
4520 *
Andrew Boie3772f772018-05-07 16:52:57 -07004521 * When called from user mode, a temporary memory allocation is required from
4522 * the caller's resource pool.
4523 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004524 * @param events An array of pointers to events to be polled for.
4525 * @param num_events The number of events in the array.
4526 * @param timeout Waiting period for an event to be ready (in milliseconds),
4527 * or one of the special values K_NO_WAIT and K_FOREVER.
4528 *
4529 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004530 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004531 * @retval -EINTR Polling has been interrupted, e.g. with
4532 * k_queue_cancel_wait(). All output events are still set and valid,
4533 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4534 * words, -EINTR status means that at least one of output events is
4535 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004536 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4537 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004538 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004539 */
4540
Andrew Boie3772f772018-05-07 16:52:57 -07004541__syscall int k_poll(struct k_poll_event *events, int num_events,
4542 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004543
4544/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004545 * @brief Initialize a poll signal object.
4546 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004547 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004548 *
4549 * @param signal A poll signal.
4550 *
4551 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004552 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004553 */
4554
Andrew Boie3772f772018-05-07 16:52:57 -07004555__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4556
4557/*
4558 * @brief Reset a poll signal object's state to unsignaled.
4559 *
4560 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004561 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004562 */
4563__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4564
Patrik Flykt4344e272019-03-08 14:19:05 -07004565static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004566{
Patrik Flykt24d71432019-03-26 19:57:45 -06004567 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004568}
4569
4570/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004571 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004572 *
4573 * @param signal A poll signal object
4574 * @param signaled An integer buffer which will be written nonzero if the
4575 * object was signaled
4576 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004577 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004578 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004579 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004580 */
4581__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4582 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004583
4584/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004585 * @brief Signal a poll signal object.
4586 *
4587 * This routine makes ready a poll signal, which is basically a poll event of
4588 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4589 * made ready to run. A @a result value can be specified.
4590 *
4591 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004592 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004593 * k_poll_signal_reset(). It thus has to be reset by the user before being
4594 * passed again to k_poll() or k_poll() will consider it being signaled, and
4595 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004596 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004597 * @note The result is stored and the 'signaled' field is set even if
4598 * this function returns an error indicating that an expiring poll was
4599 * not notified. The next k_poll() will detect the missed raise.
4600 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004601 * @param signal A poll signal.
4602 * @param result The value to store in the result field of the signal.
4603 *
4604 * @retval 0 The signal was delivered successfully.
4605 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004606 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004607 */
4608
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004609__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004610
Anas Nashif954d5502018-02-25 08:37:28 -06004611/**
4612 * @internal
4613 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004614extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004615
Anas Nashif166f5192018-02-25 08:02:36 -06004616/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004617
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004618/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004619 * @defgroup cpu_idle_apis CPU Idling APIs
4620 * @ingroup kernel_apis
4621 * @{
4622 */
4623
4624/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004625 * @brief Make the CPU idle.
4626 *
4627 * This function makes the CPU idle until an event wakes it up.
4628 *
4629 * In a regular system, the idle thread should be the only thread responsible
4630 * for making the CPU idle and triggering any type of power management.
4631 * However, in some more constrained systems, such as a single-threaded system,
4632 * the only thread would be responsible for this if needed.
4633 *
4634 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004635 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004636 */
4637extern void k_cpu_idle(void);
4638
4639/**
4640 * @brief Make the CPU idle in an atomic fashion.
4641 *
4642 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4643 * must be done atomically before making the CPU idle.
4644 *
4645 * @param key Interrupt locking key obtained from irq_lock().
4646 *
4647 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004648 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004649 */
4650extern void k_cpu_atomic_idle(unsigned int key);
4651
Anas Nashif30c3cff2019-01-22 08:18:13 -05004652/**
4653 * @}
4654 */
Anas Nashif954d5502018-02-25 08:37:28 -06004655
4656/**
4657 * @internal
4658 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004659extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004660
Patrik Flykt4344e272019-03-08 14:19:05 -07004661#ifdef Z_ARCH_EXCEPT
Andrew Boiecdb94d62017-04-18 15:22:05 -07004662/* This archtecture has direct support for triggering a CPU exception */
Patrik Flykt4344e272019-03-08 14:19:05 -07004663#define z_except_reason(reason) Z_ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004664#else
4665
Andrew Boiecdb94d62017-04-18 15:22:05 -07004666/* NOTE: This is the implementation for arches that do not implement
Patrik Flykt4344e272019-03-08 14:19:05 -07004667 * Z_ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004668 *
4669 * We won't have a real exception frame to determine the PC value when
4670 * the oops occurred, so print file and line number before we jump into
4671 * the fatal error handler.
4672 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004673#define z_except_reason(reason) do { \
Andrew Boiecdb94d62017-04-18 15:22:05 -07004674 printk("@ %s:%d:\n", __FILE__, __LINE__); \
Andrew Boie56236372019-07-15 15:22:29 -07004675 z_fatal_error(reason, NULL); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004676 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004677
4678#endif /* _ARCH__EXCEPT */
4679
4680/**
4681 * @brief Fatally terminate a thread
4682 *
4683 * This should be called when a thread has encountered an unrecoverable
4684 * runtime condition and needs to terminate. What this ultimately
4685 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004686 * will be called will reason code K_ERR_KERNEL_OOPS.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004687 *
4688 * If this is called from ISR context, the default system fatal error handler
4689 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004690 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004691 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004692#define k_oops() z_except_reason(K_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004693
4694/**
4695 * @brief Fatally terminate the system
4696 *
4697 * This should be called when the Zephyr kernel has encountered an
4698 * unrecoverable runtime condition and needs to terminate. What this ultimately
4699 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004700 * will be called will reason code K_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004701 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004702 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004703#define k_panic() z_except_reason(K_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004704
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004705/*
4706 * private APIs that are utilized by one or more public APIs
4707 */
4708
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004709#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004710/**
4711 * @internal
4712 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004713extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004714#else
Anas Nashif954d5502018-02-25 08:37:28 -06004715/**
4716 * @internal
4717 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004718#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004719#endif
4720
Anas Nashif954d5502018-02-25 08:37:28 -06004721/**
4722 * @internal
4723 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004724extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004725/**
4726 * @internal
4727 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004728extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004729
Andrew Boiedc5d9352017-06-02 12:56:47 -07004730/* arch/cpu.h may declare an architecture or platform-specific macro
4731 * for properly declaring stacks, compatible with MMU/MPU constraints if
4732 * enabled
4733 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004734
4735/**
4736 * @brief Obtain an extern reference to a stack
4737 *
4738 * This macro properly brings the symbol of a thread stack declared
4739 * elsewhere into scope.
4740 *
4741 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004742 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004743 */
4744#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4745
Patrik Flykt4344e272019-03-08 14:19:05 -07004746#ifdef Z_ARCH_THREAD_STACK_DEFINE
4747#define K_THREAD_STACK_DEFINE(sym, size) Z_ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004748#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Patrik Flykt4344e272019-03-08 14:19:05 -07004749 Z_ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4750#define K_THREAD_STACK_LEN(size) Z_ARCH_THREAD_STACK_LEN(size)
4751#define K_THREAD_STACK_MEMBER(sym, size) Z_ARCH_THREAD_STACK_MEMBER(sym, size)
4752#define K_THREAD_STACK_SIZEOF(sym) Z_ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie575abc02019-03-19 10:42:24 -07004753#define K_THREAD_STACK_RESERVED Z_ARCH_THREAD_STACK_RESERVED
Andrew Boie4e5c0932019-04-04 12:05:28 -07004754static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004755{
Patrik Flykt4344e272019-03-08 14:19:05 -07004756 return Z_ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07004757}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004758#else
4759/**
4760 * @brief Declare a toplevel thread stack memory region
4761 *
4762 * This declares a region of memory suitable for use as a thread's stack.
4763 *
4764 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4765 * 'noinit' section so that it isn't zeroed at boot
4766 *
Andrew Boie507852a2017-07-25 18:47:07 -07004767 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004768 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07004769 * inside needs to be examined, examine thread->stack_info for the associated
4770 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004771 *
4772 * It is legal to precede this definition with the 'static' keyword.
4773 *
4774 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4775 * parameter of k_thread_create(), it may not be the same as the
4776 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4777 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004778 * Some arches may round the size of the usable stack region up to satisfy
4779 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4780 * size.
4781 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004782 * @param sym Thread stack symbol name
4783 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004784 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004785 */
4786#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004787 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004788
4789/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304790 * @brief Calculate size of stacks to be allocated in a stack array
4791 *
4792 * This macro calculates the size to be allocated for the stacks
4793 * inside a stack array. It accepts the indicated "size" as a parameter
4794 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4795 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4796 *
4797 * @param size Size of the stack memory region
4798 * @req K-TSTACK-001
4799 */
4800#define K_THREAD_STACK_LEN(size) (size)
4801
4802/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004803 * @brief Declare a toplevel array of thread stack memory regions
4804 *
4805 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4806 * definition for additional details and constraints.
4807 *
4808 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4809 * 'noinit' section so that it isn't zeroed at boot
4810 *
4811 * @param sym Thread stack symbol name
4812 * @param nmemb Number of stacks to declare
4813 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004814 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004815 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004816#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004817 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304818 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004819
4820/**
4821 * @brief Declare an embedded stack memory region
4822 *
4823 * Used for stacks embedded within other data structures. Use is highly
4824 * discouraged but in some cases necessary. For memory protection scenarios,
4825 * it is very important that any RAM preceding this member not be writable
4826 * by threads else a stack overflow will lead to silent corruption. In other
4827 * words, the containing data structure should live in RAM owned by the kernel.
4828 *
4829 * @param sym Thread stack symbol name
4830 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004831 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004832 */
4833#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004834 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004835
4836/**
4837 * @brief Return the size in bytes of a stack memory region
4838 *
4839 * Convenience macro for passing the desired stack size to k_thread_create()
4840 * since the underlying implementation may actually create something larger
4841 * (for instance a guard area).
4842 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004843 * The value returned here is not guaranteed to match the 'size' parameter
4844 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004845 *
4846 * @param sym Stack memory symbol
4847 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004848 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004849 */
4850#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4851
Andrew Boie575abc02019-03-19 10:42:24 -07004852
4853/**
4854 * @brief Indicate how much additional memory is reserved for stack objects
4855 *
4856 * Any given stack declaration may have additional memory in it for guard
4857 * areas or supervisor mode stacks. This macro indicates how much space
4858 * is reserved for this. The memory reserved may not be contiguous within
4859 * the stack object, and does not account for additional space used due to
4860 * enforce alignment.
4861 */
4862#define K_THREAD_STACK_RESERVED 0
4863
Andrew Boiedc5d9352017-06-02 12:56:47 -07004864/**
4865 * @brief Get a pointer to the physical stack buffer
4866 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07004867 * This macro is deprecated. If a stack buffer needs to be examined, the
4868 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004869 *
4870 * @param sym Declared stack symbol name
4871 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004872 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004873 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07004874static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004875{
4876 return (char *)sym;
4877}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004878
4879#endif /* _ARCH_DECLARE_STACK */
4880
Chunlin Hane9c97022017-07-07 20:29:30 +08004881/**
4882 * @defgroup mem_domain_apis Memory domain APIs
4883 * @ingroup kernel_apis
4884 * @{
4885 */
4886
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004887/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004888 * @def K_MEM_PARTITION_DEFINE
4889 * @brief Used to declare a memory partition
4890 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004891 */
4892#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4893#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4894 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08004895 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004896 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004897#else
4898#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08004899 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004900 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004901#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4902
Chunlin Hane9c97022017-07-07 20:29:30 +08004903/* memory partition */
4904struct k_mem_partition {
4905 /* start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004906 uintptr_t start;
Chunlin Hane9c97022017-07-07 20:29:30 +08004907 /* size of memory partition */
4908 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004909#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08004910 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304911 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004912#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08004913};
4914
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02004915/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304916 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004917struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304918#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004919 /* partitions in the domain */
4920 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304921#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004922 /* domain q */
4923 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004924 /* number of partitions in the domain */
4925 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004926};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304927
Chunlin Hane9c97022017-07-07 20:29:30 +08004928
4929/**
4930 * @brief Initialize a memory domain.
4931 *
4932 * Initialize a memory domain with given name and memory partitions.
4933 *
Andrew Boiefddd5502019-07-30 18:07:32 -07004934 * See documentation for k_mem_domain_add_partition() for details about
4935 * partition constraints.
4936 *
Chunlin Hane9c97022017-07-07 20:29:30 +08004937 * @param domain The memory domain to be initialized.
4938 * @param num_parts The number of array items of "parts" parameter.
4939 * @param parts An array of pointers to the memory partitions. Can be NULL
4940 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004941 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004942 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004943extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004944 struct k_mem_partition *parts[]);
4945/**
4946 * @brief Destroy a memory domain.
4947 *
4948 * Destroy a memory domain.
4949 *
4950 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004951 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004952 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004953extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4954
4955/**
4956 * @brief Add a memory partition into a memory domain.
4957 *
Andrew Boiefddd5502019-07-30 18:07:32 -07004958 * Add a memory partition into a memory domain. Partitions must conform to
4959 * the following constraints:
4960 *
4961 * - Partition bounds must be within system RAM boundaries on MMU-based
4962 * systems.
4963 * - Partitions in the same memory domain may not overlap each other.
4964 * - Partitions must not be defined which expose private kernel
4965 * data structures or kernel objects.
4966 * - The starting address alignment, and the partition size must conform to
4967 * the constraints of the underlying memory management hardware, which
4968 * varies per architecture.
4969 * - Memory domain partitions are only intended to control access to memory
4970 * from user mode threads.
4971 *
4972 * Violating these constraints may lead to CPU exceptions or undefined
4973 * behavior.
Chunlin Hane9c97022017-07-07 20:29:30 +08004974 *
4975 * @param domain The memory domain to be added a memory partition.
4976 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004977 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004978 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004979extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4980 struct k_mem_partition *part);
4981
4982/**
4983 * @brief Remove a memory partition from a memory domain.
4984 *
4985 * Remove a memory partition from a memory domain.
4986 *
4987 * @param domain The memory domain to be removed a memory partition.
4988 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004989 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004990 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004991extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4992 struct k_mem_partition *part);
4993
4994/**
4995 * @brief Add a thread into a memory domain.
4996 *
4997 * Add a thread into a memory domain.
4998 *
4999 * @param domain The memory domain that the thread is going to be added into.
5000 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005001 *
5002 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005003 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005004extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
5005 k_tid_t thread);
5006
5007/**
5008 * @brief Remove a thread from its memory domain.
5009 *
5010 * Remove a thread from its memory domain.
5011 *
5012 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005013 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005014 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005015extern void k_mem_domain_remove_thread(k_tid_t thread);
5016
Anas Nashif166f5192018-02-25 08:02:36 -06005017/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08005018
Andrew Boie756f9072017-10-10 16:01:49 -07005019/**
5020 * @brief Emit a character buffer to the console device
5021 *
5022 * @param c String of characters to print
5023 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005024 *
5025 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07005026 */
5027__syscall void k_str_out(char *c, size_t n);
5028
Andy Rosse7172672018-01-24 15:48:32 -08005029/**
5030 * @brief Start a numbered CPU on a MP-capable system
5031
5032 * This starts and initializes a specific CPU. The main thread on
5033 * startup is running on CPU zero, other processors are numbered
5034 * sequentially. On return from this function, the CPU is known to
5035 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07005036 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08005037 * with the provided key will work to enable them.
5038 *
5039 * Normally, in SMP mode this function will be called by the kernel
5040 * initialization and should not be used as a user API. But it is
5041 * defined here for special-purpose apps which want Zephyr running on
5042 * one core and to use others for design-specific processing.
5043 *
5044 * @param cpu_num Integer number of the CPU
5045 * @param stack Stack memory for the CPU
5046 * @param sz Stack buffer size, in bytes
5047 * @param fn Function to begin running on the CPU. First argument is
5048 * an irq_unlock() key.
5049 * @param arg Untyped argument to be passed to "fn"
5050 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005051extern void z_arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08005052 void (*fn)(int key, void *data), void *arg);
Andy Rosse7172672018-01-24 15:48:32 -08005053
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005054
5055/**
5056 * @brief Disable preservation of floating point context information.
5057 *
5058 * This routine informs the kernel that the specified thread
5059 * will no longer be using the floating point registers.
5060 *
5061 * @warning
5062 * Some architectures apply restrictions on how the disabling of floating
5063 * point preservation may be requested, see z_arch_float_disable.
5064 *
5065 * @warning
5066 * This routine should only be used to disable floating point support for
5067 * a thread that currently has such support enabled.
5068 *
5069 * @param thread ID of thread.
5070 *
5071 * @retval 0 On success.
5072 * @retval -ENOSYS If the floating point disabling is not implemented.
5073 * -EINVAL If the floating point disabling could not be performed.
5074 */
5075__syscall int k_float_disable(struct k_thread *thread);
5076
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005077#ifdef __cplusplus
5078}
5079#endif
5080
Anas Nashif10291a02019-06-25 12:25:12 -04005081#include <debug/tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07005082#include <syscalls/kernel.h>
5083
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05005084#endif /* !_ASMLANGUAGE */
5085
Flavio Ceolin67ca1762018-09-14 10:43:44 -07005086#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */