blob: e5f95c8994804cb484b9bb1240f2b90181b842ce [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
Flavio Ceolin67ca1762018-09-14 10:43:44 -070013#ifndef ZEPHYR_INCLUDE_KERNEL_H_
14#define ZEPHYR_INCLUDE_KERNEL_H_
Benjamin Walsh456c6da2016-09-02 18:55:39 -040015
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Ioannis Glaropoulos92b8a412018-06-20 17:30:48 +020017#include <kernel_includes.h>
Kumar Gala8777ff12018-07-25 20:24:34 -050018#include <errno.h>
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -070019#include <stdbool.h>
Stephanos Ioannidis33fbe002019-09-09 21:26:59 +090020#include <toolchain.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040021
22#ifdef __cplusplus
23extern "C" {
24#endif
25
Anas Nashifbbb157d2017-01-15 08:46:31 -050026/**
27 * @brief Kernel APIs
28 * @defgroup kernel_apis Kernel APIs
29 * @{
30 * @}
31 */
32
Anas Nashif61f4b242016-11-18 10:53:59 -050033#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040034#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
35#else
36#define K_DEBUG(fmt, ...)
37#endif
38
Benjamin Walsh2f280412017-01-14 19:23:46 -050039#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
40#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
41#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
42#elif defined(CONFIG_COOP_ENABLED)
43#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
44#define _NUM_PREEMPT_PRIO (0)
45#elif defined(CONFIG_PREEMPT_ENABLED)
46#define _NUM_COOP_PRIO (0)
47#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
48#else
49#error "invalid configuration"
50#endif
51
52#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040053#define K_PRIO_PREEMPT(x) (x)
54
Benjamin Walsh456c6da2016-09-02 18:55:39 -040055#define K_ANY NULL
56#define K_END NULL
57
Benjamin Walshedb35702017-01-14 18:47:22 -050058#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040059#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050060#elif defined(CONFIG_COOP_ENABLED)
61#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
62#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040063#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050064#else
65#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040066#endif
67
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050068#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040069#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
70#else
71#define K_LOWEST_THREAD_PRIO -1
72#endif
73
Benjamin Walshfab8d922016-11-08 15:36:36 -050074#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
75
Benjamin Walsh456c6da2016-09-02 18:55:39 -040076#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
77#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
78
Andy Ross225c74b2018-06-27 11:20:50 -070079#ifdef CONFIG_WAITQ_SCALABLE
Andy Ross1acd8c22018-05-03 14:51:49 -070080
81typedef struct {
82 struct _priq_rb waitq;
83} _wait_q_t;
84
Patrik Flykt4344e272019-03-08 14:19:05 -070085extern bool z_priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
Andy Ross1acd8c22018-05-03 14:51:49 -070086
Patrik Flykt4344e272019-03-08 14:19:05 -070087#define Z_WAIT_Q_INIT(wait_q) { { { .lessthan_fn = z_priq_rb_lessthan } } }
Andy Ross1acd8c22018-05-03 14:51:49 -070088
89#else
90
Andy Rossccf3bf72018-05-10 11:10:34 -070091typedef struct {
92 sys_dlist_t waitq;
93} _wait_q_t;
94
Patrik Flykt4344e272019-03-08 14:19:05 -070095#define Z_WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096
Andy Ross1acd8c22018-05-03 14:51:49 -070097#endif
98
Anas Nashif2f203c22016-12-18 06:57:45 -050099#ifdef CONFIG_OBJECT_TRACING
Flavio Ceolind1ed3362018-12-07 11:39:13 -0800100#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next;
Anas Nashif2f203c22016-12-18 06:57:45 -0500101#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400102#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500103#define _OBJECT_TRACING_INIT
Flavio Ceolind1ed3362018-12-07 11:39:13 -0800104#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400105#endif
106
Benjamin Walshacc68c12017-01-29 18:57:45 -0500107#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300108#define _POLL_EVENT_OBJ_INIT(obj) \
109 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
110#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500111#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300112#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500113#define _POLL_EVENT
114#endif
115
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500116struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400117struct k_mutex;
118struct k_sem;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400119struct k_msgq;
120struct k_mbox;
121struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200122struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400123struct k_fifo;
124struct k_lifo;
125struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400126struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400127struct k_mem_pool;
128struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500129struct k_poll_event;
130struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800131struct k_mem_domain;
132struct k_mem_partition;
Wentong Wu5611e922019-06-20 23:51:27 +0800133struct k_futex;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400134
Andrew Boie5bd891d2017-09-27 12:59:28 -0700135/* This enumeration needs to be kept in sync with the lists of kernel objects
136 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
137 * function in kernel/userspace.c
138 */
Andrew Boie945af952017-08-22 13:15:23 -0700139enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700140 K_OBJ_ANY,
141
Leandro Pereirac2003672018-04-04 13:50:32 -0700142 /** @cond
143 * Doxygen should ignore this build-time generated include file
144 * when genrating API documentation. Enumeration values are
145 * generated during build by gen_kobject_list.py. It includes
146 * basic kernel objects (e.g. pipes and mutexes) and driver types.
147 */
148#include <kobj-types-enum.h>
149 /** @endcond
150 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700151
Andrew Boie945af952017-08-22 13:15:23 -0700152 K_OBJ_LAST
153};
Anas Nashif4bcb2942019-01-23 23:06:29 -0500154/**
155 * @defgroup usermode_apis User Mode APIs
156 * @ingroup kernel_apis
157 * @{
158 */
Andrew Boie945af952017-08-22 13:15:23 -0700159
160#ifdef CONFIG_USERSPACE
161/* Table generated by gperf, these objects are retrieved via
Patrik Flykt4344e272019-03-08 14:19:05 -0700162 * z_object_find() */
Andrew Boie945af952017-08-22 13:15:23 -0700163struct _k_object {
164 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700165 u8_t perms[CONFIG_MAX_THREAD_BYTES];
166 u8_t type;
167 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700168 u32_t data;
Andrew Boiedf555242018-05-25 07:28:54 -0700169} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700170
Andrew Boie877f82e2017-10-17 11:20:22 -0700171struct _k_object_assignment {
172 struct k_thread *thread;
173 void * const *objects;
174};
175
176/**
177 * @brief Grant a static thread access to a list of kernel objects
178 *
179 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
180 * a set of kernel objects. These objects do not need to be in an initialized
181 * state. The permissions will be granted when the threads are initialized
182 * in the early boot sequence.
183 *
184 * All arguments beyond the first must be pointers to kernel objects.
185 *
186 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
187 */
188#define K_THREAD_ACCESS_GRANT(name_, ...) \
189 static void * const _CONCAT(_object_list_, name_)[] = \
190 { __VA_ARGS__, NULL }; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400191 static const Z_STRUCT_SECTION_ITERABLE(_k_object_assignment, \
192 _CONCAT(_object_access_, name_)) = \
Andrew Boie877f82e2017-10-17 11:20:22 -0700193 { (&_k_thread_obj_ ## name_), \
194 (_CONCAT(_object_list_, name_)) }
195
Andrew Boie945af952017-08-22 13:15:23 -0700196#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700197#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700198#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie78757072019-07-23 13:29:30 -0700199#define K_OBJ_FLAG_DRIVER BIT(3)
Andrew Boie945af952017-08-22 13:15:23 -0700200
201/**
202 * Lookup a kernel object and init its metadata if it exists
203 *
204 * Calling this on an object will make it usable from userspace.
205 * Intended to be called as the last statement in kernel object init
206 * functions.
207 *
Anas Nashif50e3acd2018-12-08 13:26:18 -0500208 * @param obj Address of the kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700209 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700210void z_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700211#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700212/* LCOV_EXCL_START */
Andrew Boie877f82e2017-10-17 11:20:22 -0700213#define K_THREAD_ACCESS_GRANT(thread, ...)
214
Anas Nashif954d5502018-02-25 08:37:28 -0600215/**
216 * @internal
217 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700218static inline void z_object_init(void *obj)
Andrew Boie743e4682017-10-04 12:25:50 -0700219{
220 ARG_UNUSED(obj);
221}
222
Anas Nashif954d5502018-02-25 08:37:28 -0600223/**
224 * @internal
225 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700226static inline void z_impl_k_object_access_grant(void *object,
Andrew Boie743e4682017-10-04 12:25:50 -0700227 struct k_thread *thread)
228{
229 ARG_UNUSED(object);
230 ARG_UNUSED(thread);
231}
232
Anas Nashif954d5502018-02-25 08:37:28 -0600233/**
234 * @internal
235 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700236static inline void k_object_access_revoke(void *object,
237 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700238{
239 ARG_UNUSED(object);
240 ARG_UNUSED(thread);
241}
242
Andrew Boiee9cfc542018-04-13 13:15:28 -0700243/**
244 * @internal
245 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700246static inline void z_impl_k_object_release(void *object)
Andrew Boiee9cfc542018-04-13 13:15:28 -0700247{
248 ARG_UNUSED(object);
249}
250
Andrew Boie41bab6e2017-10-14 14:42:23 -0700251static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700252{
253 ARG_UNUSED(object);
254}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700255/* LCOV_EXCL_STOP */
Andrew Boie743e4682017-10-04 12:25:50 -0700256#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700257
258/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600259 * Grant a thread access to a kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700260 *
261 * The thread will be granted access to the object if the caller is from
262 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700263 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700264 *
265 * @param object Address of kernel object
266 * @param thread Thread to grant access to the object
267 */
Andrew Boie743e4682017-10-04 12:25:50 -0700268__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700269
Andrew Boiea89bf012017-10-09 14:47:55 -0700270/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600271 * Revoke a thread's access to a kernel object
Andrew Boiea89bf012017-10-09 14:47:55 -0700272 *
273 * The thread will lose access to the object if the caller is from
274 * supervisor mode, or the caller is from user mode AND has permissions
275 * on both the object and the thread whose access is being revoked.
276 *
277 * @param object Address of kernel object
278 * @param thread Thread to remove access to the object
279 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700280void k_object_access_revoke(void *object, struct k_thread *thread);
281
282
283__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700284
285/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600286 * Grant all present and future threads access to an object
Andrew Boie3b5ae802017-10-04 12:10:32 -0700287 *
288 * If the caller is from supervisor mode, or the caller is from user mode and
289 * have sufficient permissions on the object, then that object will have
290 * permissions granted to it for *all* current and future threads running in
291 * the system, effectively becoming a public kernel object.
292 *
293 * Use of this API should be avoided on systems that are running untrusted code
294 * as it is possible for such code to derive the addresses of kernel objects
295 * and perform unwanted operations on them.
296 *
Andrew Boie04caa672017-10-13 13:57:07 -0700297 * It is not possible to revoke permissions on public objects; once public,
298 * any thread may use it.
299 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700300 * @param object Address of kernel object
301 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700302void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700303
Andrew Boie31bdfc02017-11-08 16:38:03 -0800304/**
305 * Allocate a kernel object of a designated type
306 *
307 * This will instantiate at runtime a kernel object of the specified type,
308 * returning a pointer to it. The object will be returned in an uninitialized
309 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700310 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800311 *
312 * Currently, allocation of thread stacks is not supported.
313 *
314 * @param otype Requested kernel object type
315 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
316 * available
317 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700318__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800319
Andrew Boie97bf0012018-04-24 17:01:37 -0700320#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800321/**
322 * Free a kernel object previously allocated with k_object_alloc()
323 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700324 * This will return memory for a kernel object back to resource pool it was
325 * allocated from. Care must be exercised that the object will not be used
326 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800327 *
328 * @param obj Pointer to the kernel object memory address.
329 */
330void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700331#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700332/* LCOV_EXCL_START */
Patrik Flykt4344e272019-03-08 14:19:05 -0700333static inline void *z_impl_k_object_alloc(enum k_objects otype)
Andrew Boie97bf0012018-04-24 17:01:37 -0700334{
Kumar Gala85699f72018-05-17 09:26:03 -0500335 ARG_UNUSED(otype);
336
Andrew Boie97bf0012018-04-24 17:01:37 -0700337 return NULL;
338}
339
340static inline void k_obj_free(void *obj)
341{
342 ARG_UNUSED(obj);
343}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700344/* LCOV_EXCL_STOP */
Andrew Boie31bdfc02017-11-08 16:38:03 -0800345#endif /* CONFIG_DYNAMIC_OBJECTS */
346
Anas Nashif4bcb2942019-01-23 23:06:29 -0500347/** @} */
348
Andrew Boiebca15da2017-10-15 14:17:48 -0700349/* Using typedef deliberately here, this is quite intended to be an opaque
Andrew Boie4e5c0932019-04-04 12:05:28 -0700350 * type.
Andrew Boiebca15da2017-10-15 14:17:48 -0700351 *
352 * The purpose of this data type is to clearly distinguish between the
353 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
354 * buffer which composes the stack data actually used by the underlying
Anas Nashiff2cb20c2019-06-18 14:45:40 -0400355 * thread; they cannot be used interchangeably as some arches precede the
Andrew Boiebca15da2017-10-15 14:17:48 -0700356 * stack buffer region with guard areas that trigger a MPU or MMU fault
357 * if written to.
358 *
359 * APIs that want to work with the buffer inside should continue to use
360 * char *.
361 *
362 * Stacks should always be created with K_THREAD_STACK_DEFINE().
363 */
364struct __packed _k_thread_stack_element {
365 char data;
366};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700367typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700368
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700369/**
370 * @typedef k_thread_entry_t
371 * @brief Thread entry point function type.
372 *
373 * A thread's entry point function is invoked when the thread starts executing.
374 * Up to 3 argument values can be passed to the function.
375 *
376 * The thread terminates execution permanently if the entry point function
377 * returns. The thread is responsible for releasing any shared resources
378 * it may own (such as mutexes and dynamically allocated memory), prior to
379 * returning.
380 *
381 * @param p1 First argument.
382 * @param p2 Second argument.
383 * @param p3 Third argument.
384 *
385 * @return N/A
386 */
387typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700388
389#ifdef CONFIG_THREAD_MONITOR
390struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700391 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700392 void *parameter1;
393 void *parameter2;
394 void *parameter3;
395};
396#endif
397
398/* can be used for creating 'dummy' threads, e.g. for pending on objects */
399struct _thread_base {
400
401 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700402 union {
Peter A. Bigot82ad0d22019-01-03 23:49:28 -0600403 sys_dnode_t qnode_dlist;
Andy Ross1acd8c22018-05-03 14:51:49 -0700404 struct rbnode qnode_rb;
405 };
406
Andy Ross1acd8c22018-05-03 14:51:49 -0700407 /* wait queue on which the thread is pended (needed only for
408 * trees, not dumb lists)
409 */
410 _wait_q_t *pended_on;
Andrew Boie73abd322017-04-04 13:19:13 -0700411
412 /* user facing 'thread options'; values defined in include/kernel.h */
413 u8_t user_options;
414
415 /* thread state */
416 u8_t thread_state;
417
418 /*
419 * scheduler lock count and thread priority
420 *
421 * These two fields control the preemptibility of a thread.
422 *
423 * When the scheduler is locked, sched_locked is decremented, which
424 * means that the scheduler is locked for values from 0xff to 0x01. A
425 * thread is coop if its prio is negative, thus 0x80 to 0xff when
426 * looked at the value as unsigned.
427 *
428 * By putting them end-to-end, this means that a thread is
429 * non-preemptible if the bundled value is greater than or equal to
430 * 0x0080.
431 */
432 union {
433 struct {
434#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
435 u8_t sched_locked;
436 s8_t prio;
437#else /* LITTLE and PDP */
438 s8_t prio;
439 u8_t sched_locked;
440#endif
441 };
442 u16_t preempt;
443 };
444
Andy Ross4a2e50f2018-05-15 11:06:25 -0700445#ifdef CONFIG_SCHED_DEADLINE
446 int prio_deadline;
447#endif
448
Andy Ross1acd8c22018-05-03 14:51:49 -0700449 u32_t order_key;
450
Andy Ross2724fd12018-01-29 14:55:20 -0800451#ifdef CONFIG_SMP
452 /* True for the per-CPU idle threads */
453 u8_t is_idle;
454
Andy Ross2724fd12018-01-29 14:55:20 -0800455 /* CPU index on which thread was last run */
456 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700457
458 /* Recursive count of irq_lock() calls */
459 u8_t global_lock_count;
Andy Rossab46b1b2019-01-30 15:00:42 -0800460
461#endif
462
463#ifdef CONFIG_SCHED_CPU_MASK
464 /* "May run on" bits for each CPU */
465 u8_t cpu_mask;
Andy Ross2724fd12018-01-29 14:55:20 -0800466#endif
467
Andrew Boie73abd322017-04-04 13:19:13 -0700468 /* data returned by APIs */
469 void *swap_data;
470
471#ifdef CONFIG_SYS_CLOCK_EXISTS
472 /* this thread's entry in a timeout queue */
473 struct _timeout timeout;
474#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700475};
476
477typedef struct _thread_base _thread_base_t;
478
479#if defined(CONFIG_THREAD_STACK_INFO)
480/* Contains the stack information of a thread */
481struct _thread_stack_info {
Andrew Boie4e5c0932019-04-04 12:05:28 -0700482 /* Stack start - Represents the start address of the thread-writable
483 * stack area.
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700484 */
Nicolas Pitre58d839b2019-05-21 11:32:21 -0400485 uintptr_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700486
487 /* Stack Size - Thread writable stack buffer size. Represents
488 * the size of the actual area, starting from the start member,
489 * that should be writable by the thread
490 */
Andrew Boie73abd322017-04-04 13:19:13 -0700491 u32_t size;
492};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700493
494typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700495#endif /* CONFIG_THREAD_STACK_INFO */
496
Chunlin Hane9c97022017-07-07 20:29:30 +0800497#if defined(CONFIG_USERSPACE)
498struct _mem_domain_info {
499 /* memory domain queue node */
500 sys_dnode_t mem_domain_q_node;
501 /* memory domain of the thread */
502 struct k_mem_domain *mem_domain;
503};
504
505#endif /* CONFIG_USERSPACE */
506
Daniel Leungfc182432018-08-16 15:42:28 -0700507#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
508struct _thread_userspace_local_data {
509 int errno_var;
510};
511#endif
512
Anas Nashifce78d162018-05-24 12:43:11 -0500513/**
514 * @ingroup thread_apis
515 * Thread Structure
516 */
Andrew Boie73abd322017-04-04 13:19:13 -0700517struct k_thread {
518
519 struct _thread_base base;
520
Anas Nashifce78d162018-05-24 12:43:11 -0500521 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700522 struct _callee_saved callee_saved;
523
Anas Nashifce78d162018-05-24 12:43:11 -0500524 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700525 void *init_data;
526
Anas Nashifce78d162018-05-24 12:43:11 -0500527 /**
528 * abort function
529 * @req K-THREAD-002
530 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700531 void (*fn_abort)(void);
532
533#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500534 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700535 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700536
Anas Nashifce78d162018-05-24 12:43:11 -0500537 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700538 struct k_thread *next_thread;
539#endif
540
Anas Nashif57554052018-03-03 02:31:05 -0600541#if defined(CONFIG_THREAD_NAME)
542 /* Thread name */
Andrew Boie38129ce2019-06-25 08:54:37 -0700543 char name[CONFIG_THREAD_MAX_NAME_LEN];
Anas Nashif57554052018-03-03 02:31:05 -0600544#endif
545
Andrew Boie73abd322017-04-04 13:19:13 -0700546#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500547 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700548 void *custom_data;
549#endif
550
Daniel Leungfc182432018-08-16 15:42:28 -0700551#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
552 struct _thread_userspace_local_data *userspace_local_data;
553#endif
554
Andrew Boie73abd322017-04-04 13:19:13 -0700555#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700556#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500557 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700558 int errno_var;
559#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700560#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700561
562#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500563 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700564 struct _thread_stack_info stack_info;
565#endif /* CONFIG_THREAD_STACK_INFO */
566
Chunlin Hane9c97022017-07-07 20:29:30 +0800567#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500568 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800569 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500570 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700571 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800572#endif /* CONFIG_USERSPACE */
573
Andy Ross042d8ec2017-12-09 08:37:20 -0800574#if defined(CONFIG_USE_SWITCH)
575 /* When using __switch() a few previously arch-specific items
576 * become part of the core OS
577 */
578
Patrik Flykt4344e272019-03-08 14:19:05 -0700579 /** z_swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800580 int swap_retval;
581
Patrik Flykt7c0a2452019-03-14 09:20:46 -0600582 /** Context handle returned via z_arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800583 void *switch_handle;
584#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500585 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700586 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800587
Anas Nashifce78d162018-05-24 12:43:11 -0500588 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700589 struct _thread_arch arch;
590};
591
592typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400593typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400594
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400595enum execution_context_types {
596 K_ISR = 0,
597 K_COOP_THREAD,
598 K_PREEMPT_THREAD,
599};
600
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400601/**
Anas Nashif4bcb2942019-01-23 23:06:29 -0500602 * @addtogroup thread_apis
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100603 * @{
604 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530605typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
606 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100607
608/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530609 * @brief Iterate over all the threads in the system.
610 *
611 * This routine iterates over all the threads in the system and
612 * calls the user_cb function for each thread.
613 *
614 * @param user_cb Pointer to the user callback function.
615 * @param user_data Pointer to user data.
616 *
617 * @note CONFIG_THREAD_MONITOR must be set for this function
618 * to be effective. Also this API uses irq_lock to protect the
619 * _kernel.threads list which means creation of new threads and
620 * terminations of existing threads are blocked until this
621 * API returns.
622 *
623 * @return N/A
624 */
625extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
626
Anas Nashif166f5192018-02-25 08:02:36 -0600627/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100628
629/**
Allan Stephensc98da842016-11-11 15:45:03 -0500630 * @defgroup thread_apis Thread APIs
631 * @ingroup kernel_apis
632 * @{
633 */
634
Benjamin Walshed240f22017-01-22 13:05:08 -0500635#endif /* !_ASMLANGUAGE */
636
637
638/*
639 * Thread user options. May be needed by assembly code. Common part uses low
640 * bits, arch-specific use high bits.
641 */
642
Anas Nashifa541e932018-05-24 11:19:16 -0500643/**
644 * @brief system thread that must not abort
645 * @req K-THREAD-000
646 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700647#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500648
649#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500650/**
651 * @brief thread uses floating point registers
652 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700653#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500654#endif
655
Anas Nashifa541e932018-05-24 11:19:16 -0500656/**
657 * @brief user mode thread
658 *
659 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700660 * has additional restrictions
661 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700662#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700663
Anas Nashifa541e932018-05-24 11:19:16 -0500664/**
665 * @brief Inherit Permissions
666 *
667 * @details
668 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700669 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
670 * is not enabled.
671 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700672#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700673
Benjamin Walshed240f22017-01-22 13:05:08 -0500674#ifdef CONFIG_X86
675/* x86 Bitmask definitions for threads user options */
676
677#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
678/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700679#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500680#endif
681#endif
682
683/* end - thread options */
684
685#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400686/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700687 * @brief Create a thread.
688 *
689 * This routine initializes a thread, then schedules it for execution.
690 *
691 * The new thread may be scheduled for immediate execution or a delayed start.
692 * If the newly spawned thread does not have a delayed start the kernel
693 * scheduler may preempt the current thread to allow the new thread to
694 * execute.
695 *
696 * Thread options are architecture-specific, and can include K_ESSENTIAL,
697 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
698 * them using "|" (the logical OR operator).
699 *
700 * Historically, users often would use the beginning of the stack memory region
701 * to store the struct k_thread data, although corruption will occur if the
702 * stack overflows this region and stack protection features may not detect this
703 * situation.
704 *
705 * @param new_thread Pointer to uninitialized struct k_thread
706 * @param stack Pointer to the stack space.
707 * @param stack_size Stack size in bytes.
708 * @param entry Thread entry function.
709 * @param p1 1st entry point parameter.
710 * @param p2 2nd entry point parameter.
711 * @param p3 3rd entry point parameter.
712 * @param prio Thread priority.
713 * @param options Thread options.
714 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
715 *
716 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400717 *
718 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700719 */
Andrew Boie662c3452017-10-02 10:51:18 -0700720__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700721 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700722 size_t stack_size,
723 k_thread_entry_t entry,
724 void *p1, void *p2, void *p3,
725 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700726
Andrew Boie3f091b52017-08-30 14:34:14 -0700727/**
728 * @brief Drop a thread's privileges permanently to user mode
729 *
730 * @param entry Function to start executing from
731 * @param p1 1st entry point parameter
732 * @param p2 2nd entry point parameter
733 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400734 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700735 */
736extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
737 void *p1, void *p2,
738 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700739
Andrew Boied26cf2d2017-03-30 13:07:02 -0700740/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530741 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700742 *
743 * This is a convenience function. For the provided thread, grant access to
744 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700745 *
746 * The thread object must be initialized (i.e. running). The objects don't
747 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530748 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700749 *
750 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530751 * @param ... list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400752 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700753 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530754#define k_thread_access_grant(thread, ...) \
755 FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700756
757/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700758 * @brief Assign a resource memory pool to a thread
759 *
760 * By default, threads have no resource pool assigned unless their parent
761 * thread has a resource pool, in which case it is inherited. Multiple
762 * threads may be assigned to the same memory pool.
763 *
764 * Changing a thread's resource pool will not migrate allocations from the
765 * previous pool.
766 *
767 * @param thread Target thread to assign a memory pool for resource requests,
768 * or NULL if the thread should no longer have a memory pool.
769 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400770 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700771 */
772static inline void k_thread_resource_pool_assign(struct k_thread *thread,
773 struct k_mem_pool *pool)
774{
775 thread->resource_pool = pool;
776}
777
778#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
779/**
780 * @brief Assign the system heap as a thread's resource pool
781 *
782 * Similar to k_thread_resource_pool_assign(), but the thread will use
783 * the kernel heap to draw memory.
784 *
785 * Use with caution, as a malicious thread could perform DoS attacks on the
786 * kernel heap.
787 *
788 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400789 *
790 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700791 */
792void k_thread_system_pool_assign(struct k_thread *thread);
793#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
794
795/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500796 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400797 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700798 * This routine puts the current thread to sleep for @a duration milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400799 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700800 * @param ms Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400801 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200802 * @return Zero if the requested time has elapsed or the number of milliseconds
803 * left to sleep, if thread was woken up by \ref k_wakeup call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400804 */
Charles E. Yousea5678312019-05-09 16:46:46 -0700805__syscall s32_t k_sleep(s32_t ms);
806
807/**
808 * @brief Put the current thread to sleep with microsecond resolution.
809 *
810 * This function is unlikely to work as expected without kernel tuning.
811 * In particular, because the lower bound on the duration of a sleep is
812 * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted
813 * to achieve the resolution desired. The implications of doing this must
814 * be understood before attempting to use k_usleep(). Use with caution.
815 *
816 * @param us Number of microseconds to sleep.
817 *
818 * @return Zero if the requested time has elapsed or the number of microseconds
819 * left to sleep, if thread was woken up by \ref k_wakeup call.
820 */
821__syscall s32_t k_usleep(s32_t us);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400822
823/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500824 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400825 *
826 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500827 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400828 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829 * @return N/A
830 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800831__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832
833/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500834 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400835 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500836 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400837 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500838 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400839 *
840 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400841 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400842 */
Andrew Boie468190a2017-09-29 14:00:48 -0700843__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844
845/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500846 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500848 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500850 * If @a thread is not currently sleeping, the routine has no effect.
851 *
852 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400853 *
854 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400855 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856 */
Andrew Boie468190a2017-09-29 14:00:48 -0700857__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400858
859/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500860 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500862 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400863 *
864 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700866__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400867
868/**
Allan Stephensc98da842016-11-11 15:45:03 -0500869 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500871 * This routine permanently stops execution of @a thread. The thread is taken
872 * off all kernel queues it is part of (i.e. the ready queue, the timeout
873 * queue, or a kernel object wait queue). However, any kernel resources the
874 * thread might currently own (such as mutexes or memory blocks) are not
875 * released. It is the responsibility of the caller of this routine to ensure
876 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500878 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400879 *
880 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400881 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400882 */
Andrew Boie468190a2017-09-29 14:00:48 -0700883__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400884
Andrew Boie7d627c52017-08-30 11:01:56 -0700885
886/**
887 * @brief Start an inactive thread
888 *
889 * If a thread was created with K_FOREVER in the delay parameter, it will
890 * not be added to the scheduling queue until this function is called
891 * on it.
892 *
893 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400894 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700895 */
Andrew Boie468190a2017-09-29 14:00:48 -0700896__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700897
Allan Stephensc98da842016-11-11 15:45:03 -0500898/**
899 * @cond INTERNAL_HIDDEN
900 */
901
Benjamin Walshd211a522016-12-06 11:44:01 -0500902/* timeout has timed out and is not on _timeout_q anymore */
903#define _EXPIRED (-2)
904
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400905struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700906 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700907 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400908 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700909 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500910 void *init_p1;
911 void *init_p2;
912 void *init_p3;
913 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500914 u32_t init_options;
915 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500916 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600917 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400918};
919
Andrew Boied26cf2d2017-03-30 13:07:02 -0700920#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400921 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600922 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500923 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700924 .init_thread = (thread), \
925 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500926 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700927 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400928 .init_p1 = (void *)p1, \
929 .init_p2 = (void *)p2, \
930 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500931 .init_prio = (prio), \
932 .init_options = (options), \
933 .init_delay = (delay), \
934 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600935 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400936 }
937
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400938/**
Allan Stephensc98da842016-11-11 15:45:03 -0500939 * INTERNAL_HIDDEN @endcond
940 */
941
942/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500943 * @brief Statically define and initialize a thread.
944 *
945 * The thread may be scheduled for immediate execution or a delayed start.
946 *
947 * Thread options are architecture-specific, and can include K_ESSENTIAL,
948 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
949 * them using "|" (the logical OR operator).
950 *
951 * The ID of the thread can be accessed using:
952 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500953 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500954 *
955 * @param name Name of the thread.
956 * @param stack_size Stack size in bytes.
957 * @param entry Thread entry function.
958 * @param p1 1st entry point parameter.
959 * @param p2 2nd entry point parameter.
960 * @param p3 3rd entry point parameter.
961 * @param prio Thread priority.
962 * @param options Thread options.
963 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400964 *
Anas Nashif47420d02018-05-24 14:20:56 -0400965 * @req K-THREAD-010
966 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400967 * @internal It has been observed that the x86 compiler by default aligns
968 * these _static_thread_data structures to 32-byte boundaries, thereby
969 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400970 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400971 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500972#define K_THREAD_DEFINE(name, stack_size, \
973 entry, p1, p2, p3, \
974 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700975 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400976 struct k_thread _k_thread_obj_##name; \
977 Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\
Andrew Boied26cf2d2017-03-30 13:07:02 -0700978 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
979 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500980 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -0600981 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700982 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400983
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400984/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500985 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500987 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500989 * @param thread ID of thread whose priority is needed.
990 *
991 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400992 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400993 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700994__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400995
996/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500997 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500999 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001000 *
1001 * Rescheduling can occur immediately depending on the priority @a thread is
1002 * set to:
1003 *
1004 * - If its priority is raised above the priority of the caller of this
1005 * function, and the caller is preemptible, @a thread will be scheduled in.
1006 *
1007 * - If the caller operates on itself, it lowers its priority below that of
1008 * other threads in the system, and the caller is preemptible, the thread of
1009 * highest priority will be scheduled in.
1010 *
1011 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1012 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1013 * highest priority.
1014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001015 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001016 * @param prio New priority.
1017 *
1018 * @warning Changing the priority of a thread currently involved in mutex
1019 * priority inheritance may result in undefined behavior.
1020 *
1021 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001022 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001023 */
Andrew Boie468190a2017-09-29 14:00:48 -07001024__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001025
Andy Ross4a2e50f2018-05-15 11:06:25 -07001026
1027#ifdef CONFIG_SCHED_DEADLINE
1028/**
1029 * @brief Set deadline expiration time for scheduler
1030 *
1031 * This sets the "deadline" expiration as a time delta from the
1032 * current time, in the same units used by k_cycle_get_32(). The
1033 * scheduler (when deadline scheduling is enabled) will choose the
1034 * next expiring thread when selecting between threads at the same
1035 * static priority. Threads at different priorities will be scheduled
1036 * according to their static priority.
1037 *
1038 * @note Deadlines that are negative (i.e. in the past) are still seen
1039 * as higher priority than others, even if the thread has "finished"
1040 * its work. If you don't want it scheduled anymore, you have to
1041 * reset the deadline into the future, block/pend the thread, or
1042 * modify its priority with k_thread_priority_set().
1043 *
1044 * @note Despite the API naming, the scheduler makes no guarantees the
1045 * the thread WILL be scheduled within that deadline, nor does it take
1046 * extra metadata (like e.g. the "runtime" and "period" parameters in
1047 * Linux sched_setattr()) that allows the kernel to validate the
1048 * scheduling for achievability. Such features could be implemented
1049 * above this call, which is simply input to the priority selection
1050 * logic.
1051 *
Anas Nashif240c5162019-06-10 12:25:50 -04001052 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001053 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001054 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1055 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001056 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001057 *
Andy Ross4a2e50f2018-05-15 11:06:25 -07001058 * @param thread A thread on which to set the deadline
1059 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001060 *
1061 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001062 */
1063__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1064#endif
1065
Andy Rossab46b1b2019-01-30 15:00:42 -08001066#ifdef CONFIG_SCHED_CPU_MASK
1067/**
1068 * @brief Sets all CPU enable masks to zero
1069 *
1070 * After this returns, the thread will no longer be schedulable on any
1071 * CPUs. The thread must not be currently runnable.
1072 *
Anas Nashif240c5162019-06-10 12:25:50 -04001073 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001074 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001075 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1076 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001077 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001078 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001079 * @param thread Thread to operate upon
1080 * @return Zero on success, otherwise error code
1081 */
1082int k_thread_cpu_mask_clear(k_tid_t thread);
1083
1084/**
1085 * @brief Sets all CPU enable masks to one
1086 *
1087 * After this returns, the thread will be schedulable on any CPU. The
1088 * thread must not be currently runnable.
1089 *
Anas Nashif240c5162019-06-10 12:25:50 -04001090 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001091 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001092 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1093 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001094 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001095 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001096 * @param thread Thread to operate upon
1097 * @return Zero on success, otherwise error code
1098 */
1099int k_thread_cpu_mask_enable_all(k_tid_t thread);
1100
1101/**
1102 * @brief Enable thread to run on specified CPU
1103 *
1104 * The thread must not be currently runnable.
1105 *
Anas Nashif240c5162019-06-10 12:25:50 -04001106 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001107 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001108 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1109 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001110 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001111 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001112 * @param thread Thread to operate upon
1113 * @param cpu CPU index
1114 * @return Zero on success, otherwise error code
1115 */
1116int k_thread_cpu_mask_enable(k_tid_t thread, int cpu);
1117
1118/**
1119 * @brief Prevent thread to run on specified CPU
1120 *
1121 * The thread must not be currently runnable.
1122 *
Anas Nashif240c5162019-06-10 12:25:50 -04001123 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001124 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001125 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1126 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001127 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001128 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001129 * @param thread Thread to operate upon
1130 * @param cpu CPU index
1131 * @return Zero on success, otherwise error code
1132 */
1133int k_thread_cpu_mask_disable(k_tid_t thread, int cpu);
1134#endif
1135
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001136/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001137 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001139 * This routine prevents the kernel scheduler from making @a thread the
1140 * current thread. All other internal operations on @a thread are still
1141 * performed; for example, any timeout it is waiting on keeps ticking,
1142 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001144 * If @a thread is already suspended, the routine has no effect.
1145 *
1146 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001147 *
1148 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001149 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001150 */
Andrew Boie468190a2017-09-29 14:00:48 -07001151__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001152
1153/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001154 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001155 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001156 * This routine allows the kernel scheduler to make @a thread the current
1157 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001158 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001159 * If @a thread is not currently suspended, the routine has no effect.
1160 *
1161 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001162 *
1163 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001164 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001165 */
Andrew Boie468190a2017-09-29 14:00:48 -07001166__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001167
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001168/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001169 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001170 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001171 * This routine specifies how the scheduler will perform time slicing of
1172 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001174 * To enable time slicing, @a slice must be non-zero. The scheduler
1175 * ensures that no thread runs for more than the specified time limit
1176 * before other threads of that priority are given a chance to execute.
1177 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001178 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001180 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001181 * execute. Once the scheduler selects a thread for execution, there is no
1182 * minimum guaranteed time the thread will execute before threads of greater or
1183 * equal priority are scheduled.
1184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001185 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001186 * for execution, this routine has no effect; the thread is immediately
1187 * rescheduled after the slice period expires.
1188 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001189 * To disable timeslicing, set both @a slice and @a prio to zero.
1190 *
1191 * @param slice Maximum time slice length (in milliseconds).
1192 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001193 *
1194 * @return N/A
1195 */
Kumar Galacc334c72017-04-21 10:55:34 -05001196extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001197
Anas Nashif166f5192018-02-25 08:02:36 -06001198/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001199
1200/**
1201 * @addtogroup isr_apis
1202 * @{
1203 */
1204
1205/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001206 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001207 *
Allan Stephensc98da842016-11-11 15:45:03 -05001208 * This routine allows the caller to customize its actions, depending on
1209 * whether it is a thread or an ISR.
1210 *
1211 * @note Can be called by ISRs.
1212 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001213 * @return false if invoked by a thread.
1214 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001215 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001216extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001217
Benjamin Walsh445830d2016-11-10 15:54:27 -05001218/**
1219 * @brief Determine if code is running in a preemptible thread.
1220 *
Allan Stephensc98da842016-11-11 15:45:03 -05001221 * This routine allows the caller to customize its actions, depending on
1222 * whether it can be preempted by another thread. The routine returns a 'true'
1223 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001224 *
Allan Stephensc98da842016-11-11 15:45:03 -05001225 * - The code is running in a thread, not at ISR.
1226 * - The thread's priority is in the preemptible range.
1227 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001228 *
Allan Stephensc98da842016-11-11 15:45:03 -05001229 * @note Can be called by ISRs.
1230 *
1231 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001232 * @return Non-zero if invoked by a preemptible thread.
1233 */
Andrew Boie468190a2017-09-29 14:00:48 -07001234__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001235
Allan Stephensc98da842016-11-11 15:45:03 -05001236/**
Anas Nashif166f5192018-02-25 08:02:36 -06001237 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001238 */
1239
1240/**
1241 * @addtogroup thread_apis
1242 * @{
1243 */
1244
1245/**
1246 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001247 *
Allan Stephensc98da842016-11-11 15:45:03 -05001248 * This routine prevents the current thread from being preempted by another
1249 * thread by instructing the scheduler to treat it as a cooperative thread.
1250 * If the thread subsequently performs an operation that makes it unready,
1251 * it will be context switched out in the normal manner. When the thread
1252 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001253 *
Allan Stephensc98da842016-11-11 15:45:03 -05001254 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001255 *
Allan Stephensc98da842016-11-11 15:45:03 -05001256 * @note k_sched_lock() and k_sched_unlock() should normally be used
1257 * when the operation being performed can be safely interrupted by ISRs.
1258 * However, if the amount of processing involved is very small, better
1259 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001260 *
1261 * @return N/A
1262 */
1263extern void k_sched_lock(void);
1264
Allan Stephensc98da842016-11-11 15:45:03 -05001265/**
1266 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001267 *
Allan Stephensc98da842016-11-11 15:45:03 -05001268 * This routine reverses the effect of a previous call to k_sched_lock().
1269 * A thread must call the routine once for each time it called k_sched_lock()
1270 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001271 *
1272 * @return N/A
1273 */
1274extern void k_sched_unlock(void);
1275
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001276/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001277 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001279 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001280 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001281 * Custom data is not used by the kernel itself, and is freely available
1282 * for a thread to use as it sees fit. It can be used as a framework
1283 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001285 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001286 *
1287 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001288 *
1289 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001290 */
Andrew Boie468190a2017-09-29 14:00:48 -07001291__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001292
1293/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001294 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001295 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001296 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001297 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001298 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001299 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001300 */
Andrew Boie468190a2017-09-29 14:00:48 -07001301__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001302
1303/**
Anas Nashif57554052018-03-03 02:31:05 -06001304 * @brief Set current thread name
1305 *
1306 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1307 * tracing and debugging.
1308 *
Andrew Boie38129ce2019-06-25 08:54:37 -07001309 * @param thread_id Thread to set name, or NULL to set the current thread
1310 * @param value Name string
1311 * @retval 0 on success
1312 * @retval -EFAULT Memory access error with supplied string
1313 * @retval -ENOSYS Thread name configuration option not enabled
1314 * @retval -EINVAL Thread name too long
Anas Nashif57554052018-03-03 02:31:05 -06001315 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001316__syscall int k_thread_name_set(k_tid_t thread_id, const char *value);
Anas Nashif57554052018-03-03 02:31:05 -06001317
1318/**
1319 * @brief Get thread name
1320 *
1321 * Get the name of a thread
1322 *
1323 * @param thread_id Thread ID
Andrew Boie38129ce2019-06-25 08:54:37 -07001324 * @retval Thread name, or NULL if configuration not enabled
Anas Nashif57554052018-03-03 02:31:05 -06001325 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001326const char *k_thread_name_get(k_tid_t thread_id);
1327
1328/**
1329 * @brief Copy the thread name into a supplied buffer
1330 *
1331 * @param thread_id Thread to obtain name information
1332 * @param buf Destination buffer
1333 * @param size Destinatiomn buffer size
1334 * @retval -ENOSPC Destination buffer too small
1335 * @retval -EFAULT Memory access error
1336 * @retval -ENOSYS Thread name feature not enabled
1337 * @retval 0 Success
1338 */
1339__syscall int k_thread_name_copy(k_tid_t thread_id, char *buf,
1340 size_t size);
Anas Nashif57554052018-03-03 02:31:05 -06001341
1342/**
Pavlo Hamov8076c802019-07-31 12:43:54 +03001343 * @brief Get thread state string
1344 *
1345 * Get the human friendly thread state string
1346 *
1347 * @param thread_id Thread ID
1348 * @retval Thread state string, empty if no state flag is set
1349 */
1350const char *k_thread_state_str(k_tid_t thread_id);
1351
1352/**
Andy Rosscfe62032018-09-29 07:34:55 -07001353 * @}
1354 */
1355
1356/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001357 * @addtogroup clock_apis
1358 * @{
1359 */
1360
1361/**
1362 * @brief Generate null timeout delay.
1363 *
1364 * This macro generates a timeout delay that that instructs a kernel API
1365 * not to wait if the requested operation cannot be performed immediately.
1366 *
1367 * @return Timeout delay value.
1368 */
1369#define K_NO_WAIT 0
1370
1371/**
1372 * @brief Generate timeout delay from milliseconds.
1373 *
1374 * This macro generates a timeout delay that that instructs a kernel API
1375 * to wait up to @a ms milliseconds to perform the requested operation.
1376 *
1377 * @param ms Duration in milliseconds.
1378 *
1379 * @return Timeout delay value.
1380 */
Johan Hedberg14471692016-11-13 10:52:15 +02001381#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001382
1383/**
1384 * @brief Generate timeout delay from seconds.
1385 *
1386 * This macro generates a timeout delay that that instructs a kernel API
1387 * to wait up to @a s seconds to perform the requested operation.
1388 *
1389 * @param s Duration in seconds.
1390 *
1391 * @return Timeout delay value.
1392 */
Johan Hedberg14471692016-11-13 10:52:15 +02001393#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001394
1395/**
1396 * @brief Generate timeout delay from minutes.
1397 *
1398 * This macro generates a timeout delay that that instructs a kernel API
1399 * to wait up to @a m minutes to perform the requested operation.
1400 *
1401 * @param m Duration in minutes.
1402 *
1403 * @return Timeout delay value.
1404 */
Johan Hedberg14471692016-11-13 10:52:15 +02001405#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001406
1407/**
1408 * @brief Generate timeout delay from hours.
1409 *
1410 * This macro generates a timeout delay that that instructs a kernel API
1411 * to wait up to @a h hours to perform the requested operation.
1412 *
1413 * @param h Duration in hours.
1414 *
1415 * @return Timeout delay value.
1416 */
Johan Hedberg14471692016-11-13 10:52:15 +02001417#define K_HOURS(h) K_MINUTES((h) * 60)
1418
Allan Stephensc98da842016-11-11 15:45:03 -05001419/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001420 * @brief Generate infinite timeout delay.
1421 *
1422 * This macro generates a timeout delay that that instructs a kernel API
1423 * to wait as long as necessary to perform the requested operation.
1424 *
1425 * @return Timeout delay value.
1426 */
1427#define K_FOREVER (-1)
1428
1429/**
Anas Nashif166f5192018-02-25 08:02:36 -06001430 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001431 */
1432
1433/**
Allan Stephensc98da842016-11-11 15:45:03 -05001434 * @cond INTERNAL_HIDDEN
1435 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001436
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001437struct k_timer {
1438 /*
1439 * _timeout structure must be first here if we want to use
1440 * dynamic timer allocation. timeout.node is used in the double-linked
1441 * list of free timers
1442 */
1443 struct _timeout timeout;
1444
Allan Stephens45bfa372016-10-12 12:39:42 -05001445 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001446 _wait_q_t wait_q;
1447
1448 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001449 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001450
1451 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001452 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001453
1454 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001455 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001456
Allan Stephens45bfa372016-10-12 12:39:42 -05001457 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001458 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001459
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001460 /* user-specific data, also used to support legacy features */
1461 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001462
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001463 _OBJECT_TRACING_NEXT_PTR(k_timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001464};
1465
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001466#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001467 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001468 .timeout = { \
1469 .node = {},\
1470 .dticks = 0, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001471 .fn = z_timer_expiration_handler \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001472 }, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001473 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001474 .expiry_fn = expiry, \
1475 .stop_fn = stop, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001476 .period = 0, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001477 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001478 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001479 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001480 }
1481
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001482#define K_TIMER_INITIALIZER DEPRECATED_MACRO Z_TIMER_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001483
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001484/**
Allan Stephensc98da842016-11-11 15:45:03 -05001485 * INTERNAL_HIDDEN @endcond
1486 */
1487
1488/**
1489 * @defgroup timer_apis Timer APIs
1490 * @ingroup kernel_apis
1491 * @{
1492 */
1493
1494/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001495 * @typedef k_timer_expiry_t
1496 * @brief Timer expiry function type.
1497 *
1498 * A timer's expiry function is executed by the system clock interrupt handler
1499 * each time the timer expires. The expiry function is optional, and is only
1500 * invoked if the timer has been initialized with one.
1501 *
1502 * @param timer Address of timer.
1503 *
1504 * @return N/A
1505 */
1506typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1507
1508/**
1509 * @typedef k_timer_stop_t
1510 * @brief Timer stop function type.
1511 *
1512 * A timer's stop function is executed if the timer is stopped prematurely.
1513 * The function runs in the context of the thread that stops the timer.
1514 * The stop function is optional, and is only invoked if the timer has been
1515 * initialized with one.
1516 *
1517 * @param timer Address of timer.
1518 *
1519 * @return N/A
1520 */
1521typedef void (*k_timer_stop_t)(struct k_timer *timer);
1522
1523/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001524 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001526 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001527 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001528 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001529 *
1530 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001531 * @param expiry_fn Function to invoke each time the timer expires.
1532 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001533 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001534#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001535 Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001536 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001537
Allan Stephens45bfa372016-10-12 12:39:42 -05001538/**
1539 * @brief Initialize a timer.
1540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001541 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001542 *
1543 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001544 * @param expiry_fn Function to invoke each time the timer expires.
1545 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001546 *
1547 * @return N/A
1548 */
1549extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001550 k_timer_expiry_t expiry_fn,
1551 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001552
Allan Stephens45bfa372016-10-12 12:39:42 -05001553/**
1554 * @brief Start a timer.
1555 *
1556 * This routine starts a timer, and resets its status to zero. The timer
1557 * begins counting down using the specified duration and period values.
1558 *
1559 * Attempting to start a timer that is already running is permitted.
1560 * The timer's status is reset to zero and the timer begins counting down
1561 * using the new duration and period values.
1562 *
1563 * @param timer Address of timer.
1564 * @param duration Initial timer duration (in milliseconds).
1565 * @param period Timer period (in milliseconds).
1566 *
1567 * @return N/A
1568 */
Andrew Boiea354d492017-09-29 16:22:28 -07001569__syscall void k_timer_start(struct k_timer *timer,
1570 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001571
1572/**
1573 * @brief Stop a timer.
1574 *
1575 * This routine stops a running timer prematurely. The timer's stop function,
1576 * if one exists, is invoked by the caller.
1577 *
1578 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001579 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001580 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001581 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1582 * if @a k_timer_stop is to be called from ISRs.
1583 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001584 * @param timer Address of timer.
1585 *
1586 * @return N/A
1587 */
Andrew Boiea354d492017-09-29 16:22:28 -07001588__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001589
1590/**
1591 * @brief Read timer status.
1592 *
1593 * This routine reads the timer's status, which indicates the number of times
1594 * it has expired since its status was last read.
1595 *
1596 * Calling this routine resets the timer's status to zero.
1597 *
1598 * @param timer Address of timer.
1599 *
1600 * @return Timer status.
1601 */
Andrew Boiea354d492017-09-29 16:22:28 -07001602__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001603
1604/**
1605 * @brief Synchronize thread to timer expiration.
1606 *
1607 * This routine blocks the calling thread until the timer's status is non-zero
1608 * (indicating that it has expired at least once since it was last examined)
1609 * or the timer is stopped. If the timer status is already non-zero,
1610 * or the timer is already stopped, the caller continues without waiting.
1611 *
1612 * Calling this routine resets the timer's status to zero.
1613 *
1614 * This routine must not be used by interrupt handlers, since they are not
1615 * allowed to block.
1616 *
1617 * @param timer Address of timer.
1618 *
1619 * @return Timer status.
1620 */
Andrew Boiea354d492017-09-29 16:22:28 -07001621__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001622
Andy Ross52e444b2018-09-28 09:06:37 -07001623extern s32_t z_timeout_remaining(struct _timeout *timeout);
1624
Allan Stephens45bfa372016-10-12 12:39:42 -05001625/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001626 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001627 *
1628 * This routine computes the (approximate) time remaining before a running
1629 * timer next expires. If the timer is not running, it returns zero.
1630 *
1631 * @param timer Address of timer.
1632 *
1633 * @return Remaining time (in milliseconds).
1634 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001635__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001636
Patrik Flykt4344e272019-03-08 14:19:05 -07001637static inline u32_t z_impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001638{
Charles E. Youse0ad40222019-03-01 10:51:04 -08001639 const s32_t ticks = z_timeout_remaining(&timer->timeout);
1640 return (ticks > 0) ? (u32_t)__ticks_to_ms(ticks) : 0U;
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001641}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001642
Allan Stephensc98da842016-11-11 15:45:03 -05001643/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001644 * @brief Associate user-specific data with a timer.
1645 *
1646 * This routine records the @a user_data with the @a timer, to be retrieved
1647 * later.
1648 *
1649 * It can be used e.g. in a timer handler shared across multiple subsystems to
1650 * retrieve data specific to the subsystem this timer is associated with.
1651 *
1652 * @param timer Address of timer.
1653 * @param user_data User data to associate with the timer.
1654 *
1655 * @return N/A
1656 */
Andrew Boiea354d492017-09-29 16:22:28 -07001657__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1658
Anas Nashif954d5502018-02-25 08:37:28 -06001659/**
1660 * @internal
1661 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001662static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
Andrew Boiea354d492017-09-29 16:22:28 -07001663 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001664{
1665 timer->user_data = user_data;
1666}
1667
1668/**
1669 * @brief Retrieve the user-specific data from a timer.
1670 *
1671 * @param timer Address of timer.
1672 *
1673 * @return The user data.
1674 */
Andrew Boiea354d492017-09-29 16:22:28 -07001675__syscall void *k_timer_user_data_get(struct k_timer *timer);
1676
Patrik Flykt4344e272019-03-08 14:19:05 -07001677static inline void *z_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001678{
1679 return timer->user_data;
1680}
1681
Anas Nashif166f5192018-02-25 08:02:36 -06001682/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001683
Allan Stephensc98da842016-11-11 15:45:03 -05001684/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001685 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001686 * @{
1687 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001688
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001689/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001690 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001691 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001692 * This routine returns the elapsed time since the system booted,
1693 * in milliseconds.
1694 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001695 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001696 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001697 * While this function returns time in milliseconds, it does
1698 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001699 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001700 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001701 *
1702 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001703 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001704__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001705
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001706/**
1707 * @brief Enable clock always on in tickless kernel
1708 *
Andy Ross1db9f182019-06-25 10:09:45 -07001709 * Deprecated. This does nothing (it was always just a hint). This
1710 * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE
1711 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001712 *
1713 * @retval prev_status Previous status of always on flag
1714 */
Andy Ross1db9f182019-06-25 10:09:45 -07001715/* LCOV_EXCL_START */
1716__deprecated static inline int k_enable_sys_clock_always_on(void)
1717{
1718 __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1719 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1720
1721 return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE);
1722}
1723/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001724
1725/**
1726 * @brief Disable clock always on in tickless kernel
1727 *
Andy Ross1db9f182019-06-25 10:09:45 -07001728 * Deprecated. This does nothing (it was always just a hint). This
1729 * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE
1730 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001731 */
Andy Ross1db9f182019-06-25 10:09:45 -07001732/* LCOV_EXCL_START */
1733__deprecated static inline void k_disable_sys_clock_always_on(void)
1734{
1735 __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1736 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1737}
1738/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001739
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001740/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001741 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001742 *
Peter Bigota6067a32019-08-28 08:19:26 -05001743 * This routine returns the lower 32 bits of the system uptime in
1744 * milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001745 *
Peter Bigota6067a32019-08-28 08:19:26 -05001746 * Because correct conversion requires full precision of the system
1747 * clock there is no benefit to using this over k_uptime_get() unless
1748 * you know the application will never run long enough for the system
1749 * clock to approach 2^32 ticks. Calls to this function may involve
1750 * interrupt blocking and 64-bit math.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001751 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001752 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001753 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001754 * While this function returns time in milliseconds, it does
1755 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001756 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option
David B. Kinder8de9cc72019-06-25 10:44:55 -07001757 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001758 *
Peter Bigota6067a32019-08-28 08:19:26 -05001759 * @return The low 32 bits of the current uptime, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001760 */
Peter Bigota6067a32019-08-28 08:19:26 -05001761static inline u32_t k_uptime_get_32(void)
1762{
1763 return (u32_t)k_uptime_get();
1764}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001765
1766/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001767 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001768 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001769 * This routine computes the elapsed time between the current system uptime
1770 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001771 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001772 * @param reftime Pointer to a reference time, which is updated to the current
1773 * uptime upon return.
1774 *
1775 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001776 */
Andy Ross987c0e52018-09-27 16:50:00 -07001777static inline s64_t k_uptime_delta(s64_t *reftime)
1778{
1779 s64_t uptime, delta;
1780
1781 uptime = k_uptime_get();
1782 delta = uptime - *reftime;
1783 *reftime = uptime;
1784
1785 return delta;
1786}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001787
1788/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001789 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001791 * This routine computes the elapsed time between the current system uptime
1792 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001793 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001794 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1795 * need for interrupt locking and 64-bit math. However, the 32-bit result
1796 * cannot hold an elapsed time larger than approximately 50 days, so the
1797 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001799 * @param reftime Pointer to a reference time, which is updated to the current
1800 * uptime upon return.
1801 *
1802 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001803 */
Andy Ross987c0e52018-09-27 16:50:00 -07001804static inline u32_t k_uptime_delta_32(s64_t *reftime)
1805{
1806 return (u32_t)k_uptime_delta(reftime);
1807}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001808
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001809/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001810 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001812 * This routine returns the current time, as measured by the system's hardware
1813 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001815 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001816 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001817#define k_cycle_get_32() z_arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001818
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001819/**
Anas Nashif166f5192018-02-25 08:02:36 -06001820 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001821 */
1822
Allan Stephensc98da842016-11-11 15:45:03 -05001823/**
1824 * @cond INTERNAL_HIDDEN
1825 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001826
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001827struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001828 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001829 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001830 union {
1831 _wait_q_t wait_q;
1832
1833 _POLL_EVENT;
1834 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001835
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001836 _OBJECT_TRACING_NEXT_PTR(k_queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001837};
1838
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001839#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001840 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001841 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Patrik Flykt4344e272019-03-08 14:19:05 -07001842 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001843 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001844 _OBJECT_TRACING_INIT \
1845 }
1846
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001847#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1848
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001849extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1850
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001851/**
1852 * INTERNAL_HIDDEN @endcond
1853 */
1854
1855/**
1856 * @defgroup queue_apis Queue APIs
1857 * @ingroup kernel_apis
1858 * @{
1859 */
1860
1861/**
1862 * @brief Initialize a queue.
1863 *
1864 * This routine initializes a queue object, prior to its first use.
1865 *
1866 * @param queue Address of the queue.
1867 *
1868 * @return N/A
1869 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001870__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001871
1872/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001873 * @brief Cancel waiting on a queue.
1874 *
1875 * This routine causes first thread pending on @a queue, if any, to
1876 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001877 * If the queue is being waited on by k_poll(), it will return with
1878 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1879 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001880 *
1881 * @note Can be called by ISRs.
1882 *
1883 * @param queue Address of the queue.
1884 *
1885 * @return N/A
1886 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001887__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001888
1889/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001890 * @brief Append an element to the end of a queue.
1891 *
1892 * This routine appends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001893 * aligned on a word boundary, and the first word of the item is reserved
1894 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001895 *
1896 * @note Can be called by ISRs.
1897 *
1898 * @param queue Address of the queue.
1899 * @param data Address of the data item.
1900 *
1901 * @return N/A
1902 */
1903extern void k_queue_append(struct k_queue *queue, void *data);
1904
1905/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001906 * @brief Append an element to a queue.
1907 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001908 * This routine appends a data item to @a queue. There is an implicit memory
1909 * allocation to create an additional temporary bookkeeping data structure from
1910 * the calling thread's resource pool, which is automatically freed when the
1911 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001912 *
1913 * @note Can be called by ISRs.
1914 *
1915 * @param queue Address of the queue.
1916 * @param data Address of the data item.
1917 *
1918 * @retval 0 on success
1919 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1920 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301921__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001922
1923/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001924 * @brief Prepend an element to a queue.
1925 *
1926 * This routine prepends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001927 * aligned on a word boundary, and the first word of the item is reserved
1928 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001929 *
1930 * @note Can be called by ISRs.
1931 *
1932 * @param queue Address of the queue.
1933 * @param data Address of the data item.
1934 *
1935 * @return N/A
1936 */
1937extern void k_queue_prepend(struct k_queue *queue, void *data);
1938
1939/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001940 * @brief Prepend an element to a queue.
1941 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001942 * This routine prepends a data item to @a queue. There is an implicit memory
1943 * allocation to create an additional temporary bookkeeping data structure from
1944 * the calling thread's resource pool, which is automatically freed when the
1945 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001946 *
1947 * @note Can be called by ISRs.
1948 *
1949 * @param queue Address of the queue.
1950 * @param data Address of the data item.
1951 *
1952 * @retval 0 on success
1953 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1954 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301955__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001956
1957/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001958 * @brief Inserts an element to a queue.
1959 *
1960 * This routine inserts a data item to @a queue after previous item. A queue
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001961 * data item must be aligned on a word boundary, and the first word of
1962 * the item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001963 *
1964 * @note Can be called by ISRs.
1965 *
1966 * @param queue Address of the queue.
1967 * @param prev Address of the previous data item.
1968 * @param data Address of the data item.
1969 *
1970 * @return N/A
1971 */
1972extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1973
1974/**
1975 * @brief Atomically append a list of elements to a queue.
1976 *
1977 * This routine adds a list of data items to @a queue in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001978 * The data items must be in a singly-linked list, with the first word
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001979 * in each data item pointing to the next data item; the list must be
1980 * NULL-terminated.
1981 *
1982 * @note Can be called by ISRs.
1983 *
1984 * @param queue Address of the queue.
1985 * @param head Pointer to first node in singly-linked list.
1986 * @param tail Pointer to last node in singly-linked list.
1987 *
1988 * @return N/A
1989 */
1990extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1991
1992/**
1993 * @brief Atomically add a list of elements to a queue.
1994 *
1995 * This routine adds a list of data items to @a queue in one operation.
1996 * The data items must be in a singly-linked list implemented using a
1997 * sys_slist_t object. Upon completion, the original list is empty.
1998 *
1999 * @note Can be called by ISRs.
2000 *
2001 * @param queue Address of the queue.
2002 * @param list Pointer to sys_slist_t object.
2003 *
2004 * @return N/A
2005 */
2006extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
2007
2008/**
2009 * @brief Get an element from a queue.
2010 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002011 * This routine removes first data item from @a queue. The first word of the
2012 * data item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002013 *
2014 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2015 *
2016 * @param queue Address of the queue.
2017 * @param timeout Waiting period to obtain a data item (in milliseconds),
2018 * or one of the special values K_NO_WAIT and K_FOREVER.
2019 *
2020 * @return Address of the data item if successful; NULL if returned
2021 * without waiting, or waiting period timed out.
2022 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002023__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002024
2025/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002026 * @brief Remove an element from a queue.
2027 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002028 * This routine removes data item from @a queue. The first word of the
2029 * data item is reserved for the kernel's use. Removing elements from k_queue
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002030 * rely on sys_slist_find_and_remove which is not a constant time operation.
2031 *
2032 * @note Can be called by ISRs
2033 *
2034 * @param queue Address of the queue.
2035 * @param data Address of the data item.
2036 *
2037 * @return true if data item was removed
2038 */
2039static inline bool k_queue_remove(struct k_queue *queue, void *data)
2040{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002041 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002042}
2043
2044/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002045 * @brief Append an element to a queue only if it's not present already.
2046 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002047 * This routine appends data item to @a queue. The first word of the data
2048 * item is reserved for the kernel's use. Appending elements to k_queue
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002049 * relies on sys_slist_is_node_in_list which is not a constant time operation.
2050 *
2051 * @note Can be called by ISRs
2052 *
2053 * @param queue Address of the queue.
2054 * @param data Address of the data item.
2055 *
2056 * @return true if data item was added, false if not
2057 */
2058static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
2059{
2060 sys_sfnode_t *test;
2061
2062 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
2063 if (test == (sys_sfnode_t *) data) {
2064 return false;
2065 }
2066 }
2067
2068 k_queue_append(queue, data);
2069 return true;
2070}
2071
2072/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002073 * @brief Query a queue to see if it has data available.
2074 *
2075 * Note that the data might be already gone by the time this function returns
2076 * if other threads are also trying to read from the queue.
2077 *
2078 * @note Can be called by ISRs.
2079 *
2080 * @param queue Address of the queue.
2081 *
2082 * @return Non-zero if the queue is empty.
2083 * @return 0 if data is available.
2084 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002085__syscall int k_queue_is_empty(struct k_queue *queue);
2086
Patrik Flykt4344e272019-03-08 14:19:05 -07002087static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002088{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002089 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002090}
2091
2092/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002093 * @brief Peek element at the head of queue.
2094 *
2095 * Return element from the head of queue without removing it.
2096 *
2097 * @param queue Address of the queue.
2098 *
2099 * @return Head element, or NULL if queue is empty.
2100 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002101__syscall void *k_queue_peek_head(struct k_queue *queue);
2102
Patrik Flykt4344e272019-03-08 14:19:05 -07002103static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002104{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002105 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002106}
2107
2108/**
2109 * @brief Peek element at the tail of queue.
2110 *
2111 * Return element from the tail of queue without removing it.
2112 *
2113 * @param queue Address of the queue.
2114 *
2115 * @return Tail element, or NULL if queue is empty.
2116 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002117__syscall void *k_queue_peek_tail(struct k_queue *queue);
2118
Patrik Flykt4344e272019-03-08 14:19:05 -07002119static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002120{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002121 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002122}
2123
2124/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002125 * @brief Statically define and initialize a queue.
2126 *
2127 * The queue can be accessed outside the module where it is defined using:
2128 *
2129 * @code extern struct k_queue <name>; @endcode
2130 *
2131 * @param name Name of the queue.
2132 */
2133#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002134 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002135 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002136
Anas Nashif166f5192018-02-25 08:02:36 -06002137/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002138
Wentong Wu5611e922019-06-20 23:51:27 +08002139#ifdef CONFIG_USERSPACE
2140/**
2141 * @brief futex structure
2142 *
2143 * A k_futex is a lightweight mutual exclusion primitive designed
2144 * to minimize kernel involvement. Uncontended operation relies
2145 * only on atomic access to shared memory. k_futex are tracked as
2146 * kernel objects and can live in user memory so any access bypass
2147 * the kernel object permission management mechanism.
2148 */
2149struct k_futex {
2150 atomic_t val;
2151};
2152
2153/**
2154 * @brief futex kernel data structure
2155 *
2156 * z_futex_data are the helper data structure for k_futex to complete
2157 * futex contended operation on kernel side, structure z_futex_data
2158 * of every futex object is invisible in user mode.
2159 */
2160struct z_futex_data {
2161 _wait_q_t wait_q;
2162 struct k_spinlock lock;
2163};
2164
2165#define Z_FUTEX_DATA_INITIALIZER(obj) \
2166 { \
2167 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \
2168 }
2169
2170/**
2171 * @defgroup futex_apis FUTEX APIs
2172 * @ingroup kernel_apis
2173 * @{
2174 */
2175
2176/**
Wentong Wu5611e922019-06-20 23:51:27 +08002177 * @brief Pend the current thread on a futex
2178 *
2179 * Tests that the supplied futex contains the expected value, and if so,
2180 * goes to sleep until some other thread calls k_futex_wake() on it.
2181 *
2182 * @param futex Address of the futex.
2183 * @param expected Expected value of the futex, if it is different the caller
2184 * will not wait on it.
2185 * @param timeout Waiting period on the futex, in milliseconds, or one of the
2186 * special values K_NO_WAIT or K_FOREVER.
2187 * @retval -EACCES Caller does not have read access to futex address.
2188 * @retval -EAGAIN If the futex value did not match the expected parameter.
2189 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2190 * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup.
2191 * @retval 0 if the caller went to sleep and was woken up. The caller
2192 * should check the futex's value on wakeup to determine if it needs
2193 * to block again.
2194 */
2195__syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout);
2196
2197/**
2198 * @brief Wake one/all threads pending on a futex
2199 *
2200 * Wake up the highest priority thread pending on the supplied futex, or
2201 * wakeup all the threads pending on the supplied futex, and the behavior
2202 * depends on wake_all.
2203 *
2204 * @param futex Futex to wake up pending threads.
2205 * @param wake_all If true, wake up all pending threads; If false,
2206 * wakeup the highest priority thread.
2207 * @retval -EACCES Caller does not have access to the futex address.
2208 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2209 * @retval Number of threads that were woken up.
2210 */
2211__syscall int k_futex_wake(struct k_futex *futex, bool wake_all);
2212
2213/** @} */
2214#endif
2215
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002216struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002217 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002218};
2219
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002220/**
2221 * @cond INTERNAL_HIDDEN
2222 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002223#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002224 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002225 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002226 }
2227
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002228#define K_FIFO_INITIALIZER DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002229
Allan Stephensc98da842016-11-11 15:45:03 -05002230/**
2231 * INTERNAL_HIDDEN @endcond
2232 */
2233
2234/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002235 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002236 * @ingroup kernel_apis
2237 * @{
2238 */
2239
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002240/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002241 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002242 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002243 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002244 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002245 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002246 *
2247 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002248 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002249 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002250#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002251 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002252
2253/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002254 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002255 *
2256 * This routine causes first thread pending on @a fifo, if any, to
2257 * return from k_fifo_get() call with NULL value (as if timeout
2258 * expired).
2259 *
2260 * @note Can be called by ISRs.
2261 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002262 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002263 *
2264 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002265 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002266 */
2267#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002268 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002269
2270/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002271 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002272 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002273 * This routine adds a data item to @a fifo. A FIFO data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002274 * aligned on a word boundary, and the first word of the item is reserved
2275 * for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002276 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002277 * @note Can be called by ISRs.
2278 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002279 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002280 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002281 *
2282 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002283 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002284 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002285#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002286 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002287
2288/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002289 * @brief Add an element to a FIFO queue.
2290 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002291 * This routine adds a data item to @a fifo. There is an implicit memory
2292 * allocation to create an additional temporary bookkeeping data structure from
2293 * the calling thread's resource pool, which is automatically freed when the
2294 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002295 *
2296 * @note Can be called by ISRs.
2297 *
2298 * @param fifo Address of the FIFO.
2299 * @param data Address of the data item.
2300 *
2301 * @retval 0 on success
2302 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002303 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002304 */
2305#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002306 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002307
2308/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002309 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002310 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002311 * This routine adds a list of data items to @a fifo in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002312 * The data items must be in a singly-linked list, with the first word of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002313 * each data item pointing to the next data item; the list must be
2314 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002315 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002316 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002317 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002318 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002319 * @param head Pointer to first node in singly-linked list.
2320 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002321 *
2322 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002323 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002324 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002325#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002326 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002327
2328/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002329 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002330 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002331 * This routine adds a list of data items to @a fifo in one operation.
2332 * The data items must be in a singly-linked list implemented using a
2333 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002334 * and must be re-initialized via sys_slist_init().
2335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002336 * @note Can be called by ISRs.
2337 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002338 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002339 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002340 *
2341 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002342 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002343 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002344#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002345 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002346
2347/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002348 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002350 * This routine removes a data item from @a fifo in a "first in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002351 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002353 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2354 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002355 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002356 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002357 * or one of the special values K_NO_WAIT and K_FOREVER.
2358 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002359 * @return Address of the data item if successful; NULL if returned
2360 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002361 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002362 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002363#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002364 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002365
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002366/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002367 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002368 *
2369 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002370 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002371 *
2372 * @note Can be called by ISRs.
2373 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002374 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002375 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002376 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002377 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002378 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002379 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002380#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002381 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002382
2383/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002384 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002385 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002386 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302387 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002388 * on each iteration of processing, a head container will be peeked,
2389 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002390 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002391 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002392 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002393 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002394 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002395 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002396 */
2397#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002398 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002399
2400/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002401 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002402 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002403 * Return element from the tail of FIFO queue (without removing it). A usecase
2404 * for this is if elements of the FIFO queue are themselves containers. Then
2405 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002406 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002407 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002408 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002409 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002410 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002411 */
2412#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002413 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002414
2415/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002416 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002417 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002418 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002419 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002420 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002421 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002422 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002423 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002424 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002425#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002426 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002427 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002428
Anas Nashif166f5192018-02-25 08:02:36 -06002429/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002430
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002431struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002432 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002433};
2434
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002435/**
2436 * @cond INTERNAL_HIDDEN
2437 */
2438
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002439#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002440 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002441 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002442 }
2443
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002444#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2445
Allan Stephensc98da842016-11-11 15:45:03 -05002446/**
2447 * INTERNAL_HIDDEN @endcond
2448 */
2449
2450/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002451 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002452 * @ingroup kernel_apis
2453 * @{
2454 */
2455
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002456/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002457 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002458 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002459 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002460 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002461 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002462 *
2463 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002464 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002465 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002466#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002467 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002468
2469/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002470 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002471 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002472 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002473 * aligned on a word boundary, and the first word of the item is
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002474 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002475 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002476 * @note Can be called by ISRs.
2477 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002478 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002479 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002480 *
2481 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002482 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002483 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002484#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002485 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002486
2487/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002488 * @brief Add an element to a LIFO queue.
2489 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002490 * This routine adds a data item to @a lifo. There is an implicit memory
2491 * allocation to create an additional temporary bookkeeping data structure from
2492 * the calling thread's resource pool, which is automatically freed when the
2493 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002494 *
2495 * @note Can be called by ISRs.
2496 *
2497 * @param lifo Address of the LIFO.
2498 * @param data Address of the data item.
2499 *
2500 * @retval 0 on success
2501 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002502 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002503 */
2504#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002505 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002506
2507/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002508 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002510 * This routine removes a data item from @a lifo in a "last in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002511 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002512 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002513 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2514 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002515 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002516 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002517 * or one of the special values K_NO_WAIT and K_FOREVER.
2518 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002519 * @return Address of the data item if successful; NULL if returned
2520 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002521 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002522 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002523#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002524 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002525
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002526/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002527 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002528 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002529 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002530 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002531 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002533 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002534 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002535 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002536#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002537 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002538 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002539
Anas Nashif166f5192018-02-25 08:02:36 -06002540/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002541
2542/**
2543 * @cond INTERNAL_HIDDEN
2544 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302545#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002546
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002547typedef uintptr_t stack_data_t;
2548
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002549struct k_stack {
2550 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002551 struct k_spinlock lock;
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002552 stack_data_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002553
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002554 _OBJECT_TRACING_NEXT_PTR(k_stack)
Andrew Boief3bee952018-05-02 17:44:39 -07002555 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556};
2557
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002558#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002559 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002560 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002561 .base = stack_buffer, \
2562 .next = stack_buffer, \
2563 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002564 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002565 }
2566
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002567#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2568
Allan Stephensc98da842016-11-11 15:45:03 -05002569/**
2570 * INTERNAL_HIDDEN @endcond
2571 */
2572
2573/**
2574 * @defgroup stack_apis Stack APIs
2575 * @ingroup kernel_apis
2576 * @{
2577 */
2578
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002579/**
2580 * @brief Initialize a stack.
2581 *
2582 * This routine initializes a stack object, prior to its first use.
2583 *
2584 * @param stack Address of the stack.
2585 * @param buffer Address of array used to hold stacked values.
2586 * @param num_entries Maximum number of values that can be stacked.
2587 *
2588 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002589 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002590 */
Andrew Boief3bee952018-05-02 17:44:39 -07002591void k_stack_init(struct k_stack *stack,
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002592 stack_data_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002593
2594
2595/**
2596 * @brief Initialize a stack.
2597 *
2598 * This routine initializes a stack object, prior to its first use. Internal
2599 * buffers will be allocated from the calling thread's resource pool.
2600 * This memory will be released if k_stack_cleanup() is called, or
2601 * userspace is enabled and the stack object loses all references to it.
2602 *
2603 * @param stack Address of the stack.
2604 * @param num_entries Maximum number of values that can be stacked.
2605 *
2606 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002607 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002608 */
2609
Adithya Baglody28080d32018-10-15 11:48:51 +05302610__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2611 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002612
2613/**
2614 * @brief Release a stack's allocated buffer
2615 *
2616 * If a stack object was given a dynamically allocated buffer via
2617 * k_stack_alloc_init(), this will free it. This function does nothing
2618 * if the buffer wasn't dynamically allocated.
2619 *
2620 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002621 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002622 */
2623void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002624
2625/**
2626 * @brief Push an element onto a stack.
2627 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002628 * This routine adds a stack_data_t value @a data to @a stack.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002629 *
2630 * @note Can be called by ISRs.
2631 *
2632 * @param stack Address of the stack.
2633 * @param data Value to push onto the stack.
2634 *
2635 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002636 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002637 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002638__syscall void k_stack_push(struct k_stack *stack, stack_data_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002639
2640/**
2641 * @brief Pop an element from a stack.
2642 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002643 * This routine removes a stack_data_t value from @a stack in a "last in,
2644 * first out" manner and stores the value in @a data.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002645 *
2646 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2647 *
2648 * @param stack Address of the stack.
2649 * @param data Address of area to hold the value popped from the stack.
2650 * @param timeout Waiting period to obtain a value (in milliseconds),
2651 * or one of the special values K_NO_WAIT and K_FOREVER.
2652 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002653 * @retval 0 Element popped from stack.
2654 * @retval -EBUSY Returned without waiting.
2655 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002656 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002657 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002658__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002659
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002660/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002661 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002662 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002663 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002664 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002665 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002667 * @param name Name of the stack.
2668 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002669 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002670 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002671#define K_STACK_DEFINE(name, stack_num_entries) \
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002672 stack_data_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002673 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002674 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002675 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002676 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002677
Anas Nashif166f5192018-02-25 08:02:36 -06002678/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002679
Allan Stephens6bba9b02016-11-16 14:56:54 -05002680struct k_work;
2681
Allan Stephensc98da842016-11-11 15:45:03 -05002682/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002683 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002684 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002685 */
2686
Allan Stephens6bba9b02016-11-16 14:56:54 -05002687/**
2688 * @typedef k_work_handler_t
2689 * @brief Work item handler function type.
2690 *
2691 * A work item's handler function is executed by a workqueue's thread
2692 * when the work item is processed by the workqueue.
2693 *
2694 * @param work Address of the work item.
2695 *
2696 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002697 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002698 */
2699typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002700
2701/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002702 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002703 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002704
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002705struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002706 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002707 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002708};
2709
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002710enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002711 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002712};
2713
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002714struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002715 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002716 k_work_handler_t handler;
2717 atomic_t flags[1];
2718};
2719
Allan Stephens6bba9b02016-11-16 14:56:54 -05002720struct k_delayed_work {
2721 struct k_work work;
2722 struct _timeout timeout;
2723 struct k_work_q *work_q;
2724};
2725
2726extern struct k_work_q k_sys_work_q;
2727
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002728/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002729 * INTERNAL_HIDDEN @endcond
2730 */
2731
Patrik Flykt4344e272019-03-08 14:19:05 -07002732#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002733 { \
2734 ._reserved = NULL, \
2735 .handler = work_handler, \
2736 .flags = { 0 } \
2737 }
2738
Patrik Flykt4344e272019-03-08 14:19:05 -07002739#define K_WORK_INITIALIZER DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002740
Allan Stephens6bba9b02016-11-16 14:56:54 -05002741/**
2742 * @brief Initialize a statically-defined work item.
2743 *
2744 * This macro can be used to initialize a statically-defined workqueue work
2745 * item, prior to its first use. For example,
2746 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002747 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002748 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002749 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002750 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002751 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002752 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002753#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002754 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002755
2756/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002757 * @brief Initialize a work item.
2758 *
2759 * This routine initializes a workqueue work item, prior to its first use.
2760 *
2761 * @param work Address of work item.
2762 * @param handler Function to invoke each time work item is processed.
2763 *
2764 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002765 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002766 */
2767static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2768{
Patrik Flykt4344e272019-03-08 14:19:05 -07002769 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002770}
2771
2772/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002773 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002774 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002775 * This routine submits work item @a work to be processed by workqueue
2776 * @a work_q. If the work item is already pending in the workqueue's queue
2777 * as a result of an earlier submission, this routine has no effect on the
2778 * work item. If the work item has already been processed, or is currently
2779 * being processed, its work is considered complete and the work item can be
2780 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002781 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002782 * @warning
2783 * A submitted work item must not be modified until it has been processed
2784 * by the workqueue.
2785 *
2786 * @note Can be called by ISRs.
2787 *
2788 * @param work_q Address of workqueue.
2789 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002790 *
2791 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002792 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002793 */
2794static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2795 struct k_work *work)
2796{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002797 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002798 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002799 }
2800}
2801
2802/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002803 * @brief Submit a work item to a user mode workqueue
2804 *
David B. Kinder06d78352018-12-17 14:32:40 -08002805 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002806 * memory allocation is made from the caller's resource pool which is freed
2807 * once the worker thread consumes the k_work item. The workqueue
2808 * thread must have memory access to the k_work item being submitted. The caller
2809 * must have permission granted on the work_q parameter's queue object.
2810 *
2811 * Otherwise this works the same as k_work_submit_to_queue().
2812 *
2813 * @note Can be called by ISRs.
2814 *
2815 * @param work_q Address of workqueue.
2816 * @param work Address of work item.
2817 *
2818 * @retval -EBUSY if the work item was already in some workqueue
2819 * @retval -ENOMEM if no memory for thread resource pool allocation
2820 * @retval 0 Success
2821 * @req K-WORK-001
2822 */
2823static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2824 struct k_work *work)
2825{
2826 int ret = -EBUSY;
2827
2828 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2829 ret = k_queue_alloc_append(&work_q->queue, work);
2830
2831 /* Couldn't insert into the queue. Clear the pending bit
2832 * so the work item can be submitted again
2833 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002834 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002835 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2836 }
2837 }
2838
2839 return ret;
2840}
2841
2842/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002843 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002844 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002845 * This routine indicates if work item @a work is pending in a workqueue's
2846 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002847 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002848 * @note Can be called by ISRs.
2849 *
2850 * @param work Address of work item.
2851 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002852 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002853 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002854 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002855static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002856{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002857 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002858}
2859
2860/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002861 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002862 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002863 * This routine starts workqueue @a work_q. The workqueue spawns its work
2864 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002865 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002866 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002867 * @param stack Pointer to work queue thread's stack space, as defined by
2868 * K_THREAD_STACK_DEFINE()
2869 * @param stack_size Size of the work queue thread's stack (in bytes), which
2870 * should either be the same constant passed to
2871 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002872 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002873 *
2874 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002875 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002876 */
Andrew Boie507852a2017-07-25 18:47:07 -07002877extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002878 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002879 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002880
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002881/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002882 * @brief Start a workqueue in user mode
2883 *
2884 * This works identically to k_work_q_start() except it is callable from user
2885 * mode, and the worker thread created will run in user mode.
2886 * The caller must have permissions granted on both the work_q parameter's
2887 * thread and queue objects, and the same restrictions on priority apply as
2888 * k_thread_create().
2889 *
2890 * @param work_q Address of workqueue.
2891 * @param stack Pointer to work queue thread's stack space, as defined by
2892 * K_THREAD_STACK_DEFINE()
2893 * @param stack_size Size of the work queue thread's stack (in bytes), which
2894 * should either be the same constant passed to
2895 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2896 * @param prio Priority of the work queue's thread.
2897 *
2898 * @return N/A
2899 * @req K-WORK-001
2900 */
2901extern void k_work_q_user_start(struct k_work_q *work_q,
2902 k_thread_stack_t *stack,
2903 size_t stack_size, int prio);
2904
2905/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002906 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002907 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002908 * This routine initializes a workqueue delayed work item, prior to
2909 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002910 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002911 * @param work Address of delayed work item.
2912 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002913 *
2914 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002915 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002916 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002917extern void k_delayed_work_init(struct k_delayed_work *work,
2918 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002919
2920/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002921 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002922 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002923 * This routine schedules work item @a work to be processed by workqueue
2924 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002925 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002926 * Only when the countdown completes is the work item actually submitted to
2927 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002928 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002929 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002930 * counting down cancels the existing submission and restarts the
2931 * countdown using the new delay. Note that this behavior is
2932 * inherently subject to race conditions with the pre-existing
2933 * timeouts and work queue, so care must be taken to synchronize such
2934 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002935 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002936 * @warning
2937 * A delayed work item must not be modified until it has been processed
2938 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002939 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002940 * @note Can be called by ISRs.
2941 *
2942 * @param work_q Address of workqueue.
2943 * @param work Address of delayed work item.
2944 * @param delay Delay before submitting the work item (in milliseconds).
2945 *
2946 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002947 * @retval -EINVAL Work item is being processed or has completed its work.
2948 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002949 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002950 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002951extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2952 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002953 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002954
2955/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002956 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002957 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002958 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002959 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002960 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002961 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002962 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002963 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08002964 * @note The result of calling this on a k_delayed_work item that has
2965 * not been submitted (i.e. before the return of the
2966 * k_delayed_work_submit_to_queue() call) is undefined.
2967 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002968 * @param work Address of delayed work item.
2969 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002970 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002971 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002972 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002973 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002974extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002975
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002976/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002977 * @brief Submit a work item to the system workqueue.
2978 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002979 * This routine submits work item @a work to be processed by the system
2980 * workqueue. If the work item is already pending in the workqueue's queue
2981 * as a result of an earlier submission, this routine has no effect on the
2982 * work item. If the work item has already been processed, or is currently
2983 * being processed, its work is considered complete and the work item can be
2984 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002985 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002986 * @warning
2987 * Work items submitted to the system workqueue should avoid using handlers
2988 * that block or yield since this may prevent the system workqueue from
2989 * processing other work items in a timely manner.
2990 *
2991 * @note Can be called by ISRs.
2992 *
2993 * @param work Address of work item.
2994 *
2995 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002996 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002997 */
2998static inline void k_work_submit(struct k_work *work)
2999{
3000 k_work_submit_to_queue(&k_sys_work_q, work);
3001}
3002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003003/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003004 * @brief Submit a delayed work item to the system workqueue.
3005 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003006 * This routine schedules work item @a work to be processed by the system
3007 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003008 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003009 * Only when the countdown completes is the work item actually submitted to
3010 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003011 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003012 * Submitting a previously submitted delayed work item that is still
3013 * counting down cancels the existing submission and restarts the countdown
3014 * using the new delay. If the work item is currently pending on the
3015 * workqueue's queue because the countdown has completed it is too late to
3016 * resubmit the item, and resubmission fails without impacting the work item.
3017 * If the work item has already been processed, or is currently being processed,
3018 * its work is considered complete and the work item can be resubmitted.
3019 *
3020 * @warning
3021 * Work items submitted to the system workqueue should avoid using handlers
3022 * that block or yield since this may prevent the system workqueue from
3023 * processing other work items in a timely manner.
3024 *
3025 * @note Can be called by ISRs.
3026 *
3027 * @param work Address of delayed work item.
3028 * @param delay Delay before submitting the work item (in milliseconds).
3029 *
3030 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003031 * @retval -EINVAL Work item is being processed or has completed its work.
3032 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003033 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003034 */
3035static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003036 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003037{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05003038 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003039}
3040
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003041/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02003042 * @brief Get time remaining before a delayed work gets scheduled.
3043 *
3044 * This routine computes the (approximate) time remaining before a
3045 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02003046 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02003047 *
3048 * @param work Delayed work item.
3049 *
3050 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003051 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02003052 */
Kumar Galacc334c72017-04-21 10:55:34 -05003053static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02003054{
Andy Ross52e444b2018-09-28 09:06:37 -07003055 return __ticks_to_ms(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02003056}
3057
Anas Nashif166f5192018-02-25 08:02:36 -06003058/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003059/**
Anas Nashifce78d162018-05-24 12:43:11 -05003060 * @defgroup mutex_apis Mutex APIs
3061 * @ingroup kernel_apis
3062 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003063 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003064
Anas Nashifce78d162018-05-24 12:43:11 -05003065/**
3066 * Mutex Structure
3067 * @ingroup mutex_apis
3068 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003069struct k_mutex {
3070 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05003071 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04003072 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05003073 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003074 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003075
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003076 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003077};
3078
Anas Nashifce78d162018-05-24 12:43:11 -05003079/**
3080 * @cond INTERNAL_HIDDEN
3081 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003082#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003083 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003084 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003085 .owner = NULL, \
3086 .lock_count = 0, \
3087 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003088 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003089 }
3090
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003091#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
3092
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003093/**
Allan Stephensc98da842016-11-11 15:45:03 -05003094 * INTERNAL_HIDDEN @endcond
3095 */
3096
3097/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003098 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003099 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003100 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003101 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003102 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003104 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003105 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003106 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003107#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003108 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003109 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003110
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003111/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003112 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003113 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003114 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003115 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003116 * Upon completion, the mutex is available and does not have an owner.
3117 *
3118 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003119 *
3120 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003121 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003122 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003123__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003124
3125/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003126 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003127 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003128 * This routine locks @a mutex. If the mutex is locked by another thread,
3129 * the calling thread waits until the mutex becomes available or until
3130 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003132 * A thread is permitted to lock a mutex it has already locked. The operation
3133 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003135 * @param mutex Address of the mutex.
3136 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003137 * or one of the special values K_NO_WAIT and K_FOREVER.
3138 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003139 * @retval 0 Mutex locked.
3140 * @retval -EBUSY Returned without waiting.
3141 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003142 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003143 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003144__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003145
3146/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003147 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003148 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003149 * This routine unlocks @a mutex. The mutex must already be locked by the
3150 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003151 *
3152 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003153 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003154 * thread.
3155 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003156 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003157 *
3158 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003159 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003160 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003161__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003162
Allan Stephensc98da842016-11-11 15:45:03 -05003163/**
Anas Nashif166f5192018-02-25 08:02:36 -06003164 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003165 */
3166
3167/**
3168 * @cond INTERNAL_HIDDEN
3169 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003170
3171struct k_sem {
3172 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303173 u32_t count;
3174 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003175 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003176
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003177 _OBJECT_TRACING_NEXT_PTR(k_sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003178};
3179
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003180#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003181 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003182 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003183 .count = initial_count, \
3184 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003185 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003186 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003187 }
3188
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003189#define K_SEM_INITIALIZER DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003190
Allan Stephensc98da842016-11-11 15:45:03 -05003191/**
3192 * INTERNAL_HIDDEN @endcond
3193 */
3194
3195/**
3196 * @defgroup semaphore_apis Semaphore APIs
3197 * @ingroup kernel_apis
3198 * @{
3199 */
3200
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003201/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003202 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003203 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003204 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003205 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003206 * @param sem Address of the semaphore.
3207 * @param initial_count Initial semaphore count.
3208 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003209 *
3210 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003211 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003212 */
Andrew Boie99280232017-09-29 14:17:47 -07003213__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
3214 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003215
3216/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003217 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003218 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003219 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003221 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3222 *
3223 * @param sem Address of the semaphore.
3224 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003225 * or one of the special values K_NO_WAIT and K_FOREVER.
3226 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05003227 * @note When porting code from the nanokernel legacy API to the new API, be
3228 * careful with the return value of this function. The return value is the
3229 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
3230 * non-zero means failure, while the nano_sem_take family returns 1 for success
3231 * and 0 for failure.
3232 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003233 * @retval 0 Semaphore taken.
3234 * @retval -EBUSY Returned without waiting.
3235 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003236 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003237 */
Andrew Boie99280232017-09-29 14:17:47 -07003238__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003239
3240/**
3241 * @brief Give a semaphore.
3242 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003243 * This routine gives @a sem, unless the semaphore is already at its maximum
3244 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003246 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003247 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003248 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003249 *
3250 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003251 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003252 */
Andrew Boie99280232017-09-29 14:17:47 -07003253__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003254
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003255/**
3256 * @brief Reset a semaphore's count to zero.
3257 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003258 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003259 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003260 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003261 *
3262 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003263 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003264 */
Andrew Boie990bf162017-10-03 12:36:49 -07003265__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003266
Anas Nashif954d5502018-02-25 08:37:28 -06003267/**
3268 * @internal
3269 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003270static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003271{
Patrik Flykt24d71432019-03-26 19:57:45 -06003272 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003273}
3274
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003275/**
3276 * @brief Get a semaphore's count.
3277 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003278 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003279 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003280 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003281 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003282 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003283 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003284 */
Andrew Boie990bf162017-10-03 12:36:49 -07003285__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003286
Anas Nashif954d5502018-02-25 08:37:28 -06003287/**
3288 * @internal
3289 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003290static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003291{
3292 return sem->count;
3293}
3294
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003295/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003296 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003297 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003298 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003299 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003300 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003301 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003302 * @param name Name of the semaphore.
3303 * @param initial_count Initial semaphore count.
3304 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003305 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003306 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003307#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003308 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003309 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303310 BUILD_ASSERT(((count_limit) != 0) && \
3311 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003312
Anas Nashif166f5192018-02-25 08:02:36 -06003313/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003314
3315/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003316 * @defgroup msgq_apis Message Queue APIs
3317 * @ingroup kernel_apis
3318 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003319 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003320
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003321/**
3322 * @brief Message Queue Structure
3323 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003324struct k_msgq {
3325 _wait_q_t wait_q;
Andy Rossbe03dbd2018-07-26 10:23:02 -07003326 struct k_spinlock lock;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003327 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003328 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003329 char *buffer_start;
3330 char *buffer_end;
3331 char *read_ptr;
3332 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003333 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003334
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003335 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Andrew Boie0fe789f2018-04-12 18:35:56 -07003336 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003337};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003338/**
3339 * @cond INTERNAL_HIDDEN
3340 */
3341
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003342
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003343#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003344 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003345 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003346 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003347 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003348 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003349 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003350 .read_ptr = q_buffer, \
3351 .write_ptr = q_buffer, \
3352 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003353 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003354 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003355#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003356/**
3357 * INTERNAL_HIDDEN @endcond
3358 */
3359
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003360
Andrew Boie0fe789f2018-04-12 18:35:56 -07003361#define K_MSGQ_FLAG_ALLOC BIT(0)
3362
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003363/**
3364 * @brief Message Queue Attributes
3365 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303366struct k_msgq_attrs {
3367 size_t msg_size;
3368 u32_t max_msgs;
3369 u32_t used_msgs;
3370};
3371
Allan Stephensc98da842016-11-11 15:45:03 -05003372
3373/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003374 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003375 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003376 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3377 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003378 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3379 * message is similarly aligned to this boundary, @a q_msg_size must also be
3380 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003381 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003382 * The message queue can be accessed outside the module where it is defined
3383 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003384 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003385 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003386 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003387 * @param q_name Name of the message queue.
3388 * @param q_msg_size Message size (in bytes).
3389 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003390 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003391 *
3392 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003393 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003394#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3395 static char __noinit __aligned(q_align) \
3396 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3397 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
3398 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003399 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003400
Peter Mitsisd7a37502016-10-13 11:37:40 -04003401/**
3402 * @brief Initialize a message queue.
3403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003404 * This routine initializes a message queue object, prior to its first use.
3405 *
Allan Stephensda827222016-11-09 14:23:58 -06003406 * The message queue's ring buffer must contain space for @a max_msgs messages,
3407 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3408 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3409 * that each message is similarly aligned to this boundary, @a q_msg_size
3410 * must also be a multiple of N.
3411 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003412 * @param q Address of the message queue.
3413 * @param buffer Pointer to ring buffer that holds queued messages.
3414 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003415 * @param max_msgs Maximum number of messages that can be queued.
3416 *
3417 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003418 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003419 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003420void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3421 u32_t max_msgs);
3422
3423/**
3424 * @brief Initialize a message queue.
3425 *
3426 * This routine initializes a message queue object, prior to its first use,
3427 * allocating its internal ring buffer from the calling thread's resource
3428 * pool.
3429 *
3430 * Memory allocated for the ring buffer can be released by calling
3431 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3432 * all of its references.
3433 *
3434 * @param q Address of the message queue.
3435 * @param msg_size Message size (in bytes).
3436 * @param max_msgs Maximum number of messages that can be queued.
3437 *
3438 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3439 * thread's resource pool, or -EINVAL if the size parameters cause
3440 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003441 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003442 */
3443__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3444 u32_t max_msgs);
3445
3446
3447void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003448
3449/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003450 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003451 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003452 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003453 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003454 * @note Can be called by ISRs.
3455 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003456 * @param q Address of the message queue.
3457 * @param data Pointer to the message.
3458 * @param timeout Waiting period to add the message (in milliseconds),
3459 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003460 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003461 * @retval 0 Message sent.
3462 * @retval -ENOMSG Returned without waiting or queue purged.
3463 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003464 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003465 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003466__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003467
3468/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003469 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003470 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003471 * This routine receives a message from message queue @a q in a "first in,
3472 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003473 *
Allan Stephensc98da842016-11-11 15:45:03 -05003474 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003475 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003476 * @param q Address of the message queue.
3477 * @param data Address of area to hold the received message.
3478 * @param timeout Waiting period to receive the message (in milliseconds),
3479 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003480 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003481 * @retval 0 Message received.
3482 * @retval -ENOMSG Returned without waiting.
3483 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003484 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003485 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003486__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003487
3488/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003489 * @brief Peek/read a message from a message queue.
3490 *
3491 * This routine reads a message from message queue @a q in a "first in,
3492 * first out" manner and leaves the message in the queue.
3493 *
3494 * @note Can be called by ISRs.
3495 *
3496 * @param q Address of the message queue.
3497 * @param data Address of area to hold the message read from the queue.
3498 *
3499 * @retval 0 Message read.
3500 * @retval -ENOMSG Returned when the queue has no message.
3501 * @req K-MSGQ-002
3502 */
3503__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3504
3505/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003506 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003507 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003508 * This routine discards all unreceived messages in a message queue's ring
3509 * buffer. Any threads that are blocked waiting to send a message to the
3510 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003511 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003512 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003513 *
3514 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003515 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003516 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003517__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003518
Peter Mitsis67be2492016-10-07 11:44:34 -04003519/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003520 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003521 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003522 * This routine returns the number of unused entries in a message queue's
3523 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003525 * @param q Address of the message queue.
3526 *
3527 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003528 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003529 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003530__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3531
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303532/**
3533 * @brief Get basic attributes of a message queue.
3534 *
3535 * This routine fetches basic attributes of message queue into attr argument.
3536 *
3537 * @param q Address of the message queue.
3538 * @param attrs pointer to message queue attribute structure.
3539 *
3540 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003541 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303542 */
3543__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3544
3545
Patrik Flykt4344e272019-03-08 14:19:05 -07003546static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003547{
3548 return q->max_msgs - q->used_msgs;
3549}
3550
Peter Mitsisd7a37502016-10-13 11:37:40 -04003551/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003552 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003554 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003555 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003556 * @param q Address of the message queue.
3557 *
3558 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003559 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003560 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003561__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3562
Patrik Flykt4344e272019-03-08 14:19:05 -07003563static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003564{
3565 return q->used_msgs;
3566}
3567
Anas Nashif166f5192018-02-25 08:02:36 -06003568/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003569
3570/**
3571 * @defgroup mem_pool_apis Memory Pool APIs
3572 * @ingroup kernel_apis
3573 * @{
3574 */
3575
Andy Ross73cb9582017-05-09 10:42:39 -07003576/* Note on sizing: the use of a 20 bit field for block means that,
3577 * assuming a reasonable minimum block size of 16 bytes, we're limited
3578 * to 16M of memory managed by a single pool. Long term it would be
3579 * good to move to a variable bit size based on configuration.
3580 */
3581struct k_mem_block_id {
3582 u32_t pool : 8;
3583 u32_t level : 4;
3584 u32_t block : 20;
3585};
3586
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003587struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003588 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003589 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003590};
3591
Anas Nashif166f5192018-02-25 08:02:36 -06003592/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003593
3594/**
3595 * @defgroup mailbox_apis Mailbox APIs
3596 * @ingroup kernel_apis
3597 * @{
3598 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003599
3600struct k_mbox_msg {
3601 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003602 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003603 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003604 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003605 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003606 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003607 /** sender's message data buffer */
3608 void *tx_data;
3609 /** internal use only - needed for legacy API support */
3610 void *_rx_data;
3611 /** message data block descriptor */
3612 struct k_mem_block tx_block;
3613 /** source thread id */
3614 k_tid_t rx_source_thread;
3615 /** target thread id */
3616 k_tid_t tx_target_thread;
3617 /** internal use only - thread waiting on send (may be a dummy) */
3618 k_tid_t _syncing_thread;
3619#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3620 /** internal use only - semaphore used during asynchronous send */
3621 struct k_sem *_async_sem;
3622#endif
3623};
3624
3625struct k_mbox {
3626 _wait_q_t tx_msg_queue;
3627 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003628 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003629
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003630 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003631};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003632/**
3633 * @cond INTERNAL_HIDDEN
3634 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003635
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003636#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003637 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003638 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3639 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003640 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003641 }
3642
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003643#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3644
Peter Mitsis12092702016-10-14 12:57:23 -04003645/**
Allan Stephensc98da842016-11-11 15:45:03 -05003646 * INTERNAL_HIDDEN @endcond
3647 */
3648
3649/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003650 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003651 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003652 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003653 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003654 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003656 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003657 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003658 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003659#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003660 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003661 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003662
Peter Mitsis12092702016-10-14 12:57:23 -04003663/**
3664 * @brief Initialize a mailbox.
3665 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003666 * This routine initializes a mailbox object, prior to its first use.
3667 *
3668 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003669 *
3670 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003671 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003672 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003673extern void k_mbox_init(struct k_mbox *mbox);
3674
Peter Mitsis12092702016-10-14 12:57:23 -04003675/**
3676 * @brief Send a mailbox message in a synchronous manner.
3677 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003678 * This routine sends a message to @a mbox and waits for a receiver to both
3679 * receive and process it. The message data may be in a buffer, in a memory
3680 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003681 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003682 * @param mbox Address of the mailbox.
3683 * @param tx_msg Address of the transmit message descriptor.
3684 * @param timeout Waiting period for the message to be received (in
3685 * milliseconds), or one of the special values K_NO_WAIT
3686 * and K_FOREVER. Once the message has been received,
3687 * this routine waits as long as necessary for the message
3688 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003689 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003690 * @retval 0 Message sent.
3691 * @retval -ENOMSG Returned without waiting.
3692 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003693 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003694 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003695extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003696 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003697
Peter Mitsis12092702016-10-14 12:57:23 -04003698/**
3699 * @brief Send a mailbox message in an asynchronous manner.
3700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003701 * This routine sends a message to @a mbox without waiting for a receiver
3702 * to process it. The message data may be in a buffer, in a memory pool block,
3703 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3704 * will be given when the message has been both received and completely
3705 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003706 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003707 * @param mbox Address of the mailbox.
3708 * @param tx_msg Address of the transmit message descriptor.
3709 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003710 *
3711 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003712 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003713 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003714extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003715 struct k_sem *sem);
3716
Peter Mitsis12092702016-10-14 12:57:23 -04003717/**
3718 * @brief Receive a mailbox message.
3719 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003720 * This routine receives a message from @a mbox, then optionally retrieves
3721 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003722 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003723 * @param mbox Address of the mailbox.
3724 * @param rx_msg Address of the receive message descriptor.
3725 * @param buffer Address of the buffer to receive data, or NULL to defer data
3726 * retrieval and message disposal until later.
3727 * @param timeout Waiting period for a message to be received (in
3728 * milliseconds), or one of the special values K_NO_WAIT
3729 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003730 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003731 * @retval 0 Message received.
3732 * @retval -ENOMSG Returned without waiting.
3733 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003734 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003735 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003736extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003737 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003738
3739/**
3740 * @brief Retrieve mailbox message data into a buffer.
3741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003742 * This routine completes the processing of a received message by retrieving
3743 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003744 *
3745 * Alternatively, this routine can be used to dispose of a received message
3746 * without retrieving its data.
3747 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003748 * @param rx_msg Address of the receive message descriptor.
3749 * @param buffer Address of the buffer to receive data, or NULL to discard
3750 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003751 *
3752 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003753 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003754 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003755extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003756
3757/**
3758 * @brief Retrieve mailbox message data into a memory pool block.
3759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003760 * This routine completes the processing of a received message by retrieving
3761 * its data into a memory pool block, then disposing of the message.
3762 * The memory pool block that results from successful retrieval must be
3763 * returned to the pool once the data has been processed, even in cases
3764 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003765 *
3766 * Alternatively, this routine can be used to dispose of a received message
3767 * without retrieving its data. In this case there is no need to return a
3768 * memory pool block to the pool.
3769 *
3770 * This routine allocates a new memory pool block for the data only if the
3771 * data is not already in one. If a new block cannot be allocated, the routine
3772 * returns a failure code and the received message is left unchanged. This
3773 * permits the caller to reattempt data retrieval at a later time or to dispose
3774 * of the received message without retrieving its data.
3775 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003776 * @param rx_msg Address of a receive message descriptor.
3777 * @param pool Address of memory pool, or NULL to discard data.
3778 * @param block Address of the area to hold memory pool block info.
3779 * @param timeout Waiting period to wait for a memory pool block (in
3780 * milliseconds), or one of the special values K_NO_WAIT
3781 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003782 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003783 * @retval 0 Data retrieved.
3784 * @retval -ENOMEM Returned without waiting.
3785 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003786 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003787 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003788extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003789 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003790 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003791
Anas Nashif166f5192018-02-25 08:02:36 -06003792/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003793
3794/**
Anas Nashifce78d162018-05-24 12:43:11 -05003795 * @defgroup pipe_apis Pipe APIs
3796 * @ingroup kernel_apis
3797 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003798 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003799
Anas Nashifce78d162018-05-24 12:43:11 -05003800/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003801struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003802 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3803 size_t size; /**< Buffer size */
3804 size_t bytes_used; /**< # bytes used in buffer */
3805 size_t read_index; /**< Where in buffer to read from */
3806 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08003807 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003808
3809 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003810 _wait_q_t readers; /**< Reader wait queue */
3811 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003812 } wait_q;
3813
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003814 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Anas Nashifce78d162018-05-24 12:43:11 -05003815 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003816};
3817
Anas Nashifce78d162018-05-24 12:43:11 -05003818/**
3819 * @cond INTERNAL_HIDDEN
3820 */
3821#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3822
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003823#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
3824 { \
3825 .buffer = pipe_buffer, \
3826 .size = pipe_buffer_size, \
3827 .bytes_used = 0, \
3828 .read_index = 0, \
3829 .write_index = 0, \
3830 .lock = {}, \
3831 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003832 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
3833 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003834 }, \
3835 _OBJECT_TRACING_INIT \
3836 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003837 }
3838
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003839#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3840
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003841/**
Allan Stephensc98da842016-11-11 15:45:03 -05003842 * INTERNAL_HIDDEN @endcond
3843 */
3844
3845/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003846 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003848 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003849 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003850 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003852 * @param name Name of the pipe.
3853 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3854 * or zero if no ring buffer is used.
3855 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003856 *
3857 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003858 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003859#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08003860 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07003861 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003862 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003863 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003864
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003865/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003866 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003868 * This routine initializes a pipe object, prior to its first use.
3869 *
3870 * @param pipe Address of the pipe.
3871 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3872 * is used.
3873 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3874 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003875 *
3876 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003877 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003878 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003879void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3880
3881/**
3882 * @brief Release a pipe's allocated buffer
3883 *
3884 * If a pipe object was given a dynamically allocated buffer via
3885 * k_pipe_alloc_init(), this will free it. This function does nothing
3886 * if the buffer wasn't dynamically allocated.
3887 *
3888 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003889 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003890 */
3891void k_pipe_cleanup(struct k_pipe *pipe);
3892
3893/**
3894 * @brief Initialize a pipe and allocate a buffer for it
3895 *
3896 * Storage for the buffer region will be allocated from the calling thread's
3897 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3898 * or userspace is enabled and the pipe object loses all references to it.
3899 *
3900 * This function should only be called on uninitialized pipe objects.
3901 *
3902 * @param pipe Address of the pipe.
3903 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3904 * buffer is used.
3905 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003906 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003907 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003908 */
3909__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003910
3911/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003912 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003914 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003915 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003916 * @param pipe Address of the pipe.
3917 * @param data Address of data to write.
3918 * @param bytes_to_write Size of data (in bytes).
3919 * @param bytes_written Address of area to hold the number of bytes written.
3920 * @param min_xfer Minimum number of bytes to write.
3921 * @param timeout Waiting period to wait for the data to be written (in
3922 * milliseconds), or one of the special values K_NO_WAIT
3923 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003924 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003925 * @retval 0 At least @a min_xfer bytes of data were written.
3926 * @retval -EIO Returned without waiting; zero data bytes were written.
3927 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003928 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003929 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003930 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003931__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3932 size_t bytes_to_write, size_t *bytes_written,
3933 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003934
3935/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003936 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003937 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003938 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003939 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003940 * @param pipe Address of the pipe.
3941 * @param data Address to place the data read from pipe.
3942 * @param bytes_to_read Maximum number of data bytes to read.
3943 * @param bytes_read Address of area to hold the number of bytes read.
3944 * @param min_xfer Minimum number of data bytes to read.
3945 * @param timeout Waiting period to wait for the data to be read (in
3946 * milliseconds), or one of the special values K_NO_WAIT
3947 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003948 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003949 * @retval 0 At least @a min_xfer bytes of data were read.
3950 * @retval -EIO Returned without waiting; zero data bytes were read.
3951 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003952 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003953 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003954 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003955__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3956 size_t bytes_to_read, size_t *bytes_read,
3957 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003958
3959/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003960 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003962 * This routine writes the data contained in a memory block to @a pipe.
3963 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003964 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003965 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003966 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003967 * @param block Memory block containing data to send
3968 * @param size Number of data bytes in memory block to send
3969 * @param sem Semaphore to signal upon completion (else NULL)
3970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003971 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003972 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003973 */
3974extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3975 size_t size, struct k_sem *sem);
3976
Anas Nashif166f5192018-02-25 08:02:36 -06003977/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003978
Allan Stephensc98da842016-11-11 15:45:03 -05003979/**
3980 * @cond INTERNAL_HIDDEN
3981 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003982
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003983struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003984 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003985 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003986 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003987 char *buffer;
3988 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003989 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003990
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003991 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003992};
3993
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003994#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003995 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003996 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003997 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003998 .num_blocks = slab_num_blocks, \
3999 .block_size = slab_block_size, \
4000 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004001 .free_list = NULL, \
4002 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05004003 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004004 }
4005
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004006#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
4007
4008
Peter Mitsis578f9112016-10-07 13:50:31 -04004009/**
Allan Stephensc98da842016-11-11 15:45:03 -05004010 * INTERNAL_HIDDEN @endcond
4011 */
4012
4013/**
4014 * @defgroup mem_slab_apis Memory Slab APIs
4015 * @ingroup kernel_apis
4016 * @{
4017 */
4018
4019/**
Allan Stephensda827222016-11-09 14:23:58 -06004020 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04004021 *
Allan Stephensda827222016-11-09 14:23:58 -06004022 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004023 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06004024 * @a slab_align -byte boundary. To ensure that each memory block is similarly
4025 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004026 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04004027 *
Allan Stephensda827222016-11-09 14:23:58 -06004028 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004029 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004030 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004031 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004033 * @param name Name of the memory slab.
4034 * @param slab_block_size Size of each memory block (in bytes).
4035 * @param slab_num_blocks Number memory blocks.
4036 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004037 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04004038 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004039#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004040 char __noinit __aligned(WB_UP(slab_align)) \
4041 _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004042 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004043 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004044 WB_UP(slab_block_size), slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004045
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004046/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004047 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004048 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004049 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004050 *
Allan Stephensda827222016-11-09 14:23:58 -06004051 * The memory slab's buffer contains @a slab_num_blocks memory blocks
4052 * that are @a slab_block_size bytes long. The buffer must be aligned to an
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004053 * N-byte boundary matching a word boundary, where N is a power of 2
4054 * (i.e. 4 on 32-bit systems, 8, 16, ...).
Allan Stephensda827222016-11-09 14:23:58 -06004055 * To ensure that each memory block is similarly aligned to this boundary,
4056 * @a slab_block_size must also be a multiple of N.
4057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004058 * @param slab Address of the memory slab.
4059 * @param buffer Pointer to buffer used for the memory blocks.
4060 * @param block_size Size of each memory block (in bytes).
4061 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004062 *
4063 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004064 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004065 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004066extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05004067 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004068
4069/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004070 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004071 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004072 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004074 * @param slab Address of the memory slab.
4075 * @param mem Pointer to block address area.
4076 * @param timeout Maximum time to wait for operation to complete
4077 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4078 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004079 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004080 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004081 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004082 * @retval -ENOMEM Returned without waiting.
4083 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004084 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004085 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004086extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05004087 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004088
4089/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004090 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004092 * This routine releases a previously allocated memory block back to its
4093 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004095 * @param slab Address of the memory slab.
4096 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004097 *
4098 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004099 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004100 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004101extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004102
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004103/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004104 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004105 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004106 * This routine gets the number of memory blocks that are currently
4107 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004108 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004109 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004110 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004111 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004112 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004113 */
Kumar Galacc334c72017-04-21 10:55:34 -05004114static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004115{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004116 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004117}
4118
Peter Mitsisc001aa82016-10-13 13:53:37 -04004119/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004120 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004121 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004122 * This routine gets the number of memory blocks that are currently
4123 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004125 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004127 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004128 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004129 */
Kumar Galacc334c72017-04-21 10:55:34 -05004130static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004131{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004132 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004133}
4134
Anas Nashif166f5192018-02-25 08:02:36 -06004135/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004136
4137/**
4138 * @cond INTERNAL_HIDDEN
4139 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004140
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004141struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004142 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004143 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004144};
4145
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004146/**
Allan Stephensc98da842016-11-11 15:45:03 -05004147 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004148 */
4149
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004150/**
Allan Stephensc98da842016-11-11 15:45:03 -05004151 * @addtogroup mem_pool_apis
4152 * @{
4153 */
4154
4155/**
4156 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004157 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004158 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4159 * long. The memory pool allows blocks to be repeatedly partitioned into
4160 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004161 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004162 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004163 * If the pool is to be accessed outside the module where it is defined, it
4164 * can be declared via
4165 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004166 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004168 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004169 * @param minsz Size of the smallest blocks in the pool (in bytes).
4170 * @param maxsz Size of the largest blocks in the pool (in bytes).
4171 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004172 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004173 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004174 */
Andy Ross73cb9582017-05-09 10:42:39 -07004175#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004176 char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \
Andy Ross73cb9582017-05-09 10:42:39 -07004177 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004178 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004179 Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004180 .base = { \
4181 .buf = _mpool_buf_##name, \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004182 .max_sz = WB_UP(maxsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004183 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004184 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004185 .levels = _mpool_lvls_##name, \
4186 .flags = SYS_MEM_POOL_KERNEL \
4187 } \
Johann Fischer223a2b92019-07-04 15:55:20 +02004188 }; \
4189 BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK);
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004190
Peter Mitsis937042c2016-10-13 13:18:26 -04004191/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004192 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004193 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004194 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004195 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004196 * @param pool Address of the memory pool.
4197 * @param block Pointer to block descriptor for the allocated memory.
4198 * @param size Amount of memory to allocate (in bytes).
4199 * @param timeout Maximum time to wait for operation to complete
4200 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4201 * or K_FOREVER to wait as long as necessary.
4202 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004203 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004204 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004205 * @retval -ENOMEM Returned without waiting.
4206 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004207 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004208 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004209extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004210 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004211
4212/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004213 * @brief Allocate memory from a memory pool with malloc() semantics
4214 *
4215 * Such memory must be released using k_free().
4216 *
4217 * @param pool Address of the memory pool.
4218 * @param size Amount of memory to allocate (in bytes).
4219 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004220 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004221 */
4222extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4223
4224/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004225 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004226 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004227 * This routine releases a previously allocated memory block back to its
4228 * memory pool.
4229 *
4230 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004231 *
4232 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004233 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004234 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004235extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004236
4237/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004238 * @brief Free memory allocated from a memory pool.
4239 *
4240 * This routine releases a previously allocated memory block back to its
4241 * memory pool
4242 *
4243 * @param id Memory block identifier.
4244 *
4245 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004246 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004247 */
4248extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4249
4250/**
Anas Nashif166f5192018-02-25 08:02:36 -06004251 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004252 */
4253
4254/**
4255 * @defgroup heap_apis Heap Memory Pool APIs
4256 * @ingroup kernel_apis
4257 * @{
4258 */
4259
4260/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004261 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004262 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004263 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004264 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004265 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004266 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004267 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004268 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004269 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004270 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004271extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004272
4273/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004274 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004275 *
4276 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004277 * returned must have been allocated from the heap memory pool or
4278 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004279 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004280 * If @a ptr is NULL, no operation is performed.
4281 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004282 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004283 *
4284 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004285 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004286 */
4287extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004288
Allan Stephensc98da842016-11-11 15:45:03 -05004289/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004290 * @brief Allocate memory from heap, array style
4291 *
4292 * This routine provides traditional calloc() semantics. Memory is
4293 * allocated from the heap memory pool and zeroed.
4294 *
4295 * @param nmemb Number of elements in the requested array
4296 * @param size Size of each array element (in bytes).
4297 *
4298 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004299 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004300 */
4301extern void *k_calloc(size_t nmemb, size_t size);
4302
Anas Nashif166f5192018-02-25 08:02:36 -06004303/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004304
Benjamin Walshacc68c12017-01-29 18:57:45 -05004305/* polling API - PRIVATE */
4306
Benjamin Walshb0179862017-02-02 16:39:57 -05004307#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004308#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004309#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004310#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004311#endif
4312
Benjamin Walshacc68c12017-01-29 18:57:45 -05004313/* private - implementation data created as needed, per-type */
4314struct _poller {
4315 struct k_thread *thread;
Flavio Ceolin76b35182018-12-16 12:48:29 -08004316 volatile bool is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004317};
4318
4319/* private - types bit positions */
4320enum _poll_types_bits {
4321 /* can be used to ignore an event */
4322 _POLL_TYPE_IGNORE,
4323
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004324 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004325 _POLL_TYPE_SIGNAL,
4326
4327 /* semaphore availability */
4328 _POLL_TYPE_SEM_AVAILABLE,
4329
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004330 /* queue/fifo/lifo data availability */
4331 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004332
4333 _POLL_NUM_TYPES
4334};
4335
Patrik Flykt4344e272019-03-08 14:19:05 -07004336#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004337
4338/* private - states bit positions */
4339enum _poll_states_bits {
4340 /* default state when creating event */
4341 _POLL_STATE_NOT_READY,
4342
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004343 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004344 _POLL_STATE_SIGNALED,
4345
4346 /* semaphore is available */
4347 _POLL_STATE_SEM_AVAILABLE,
4348
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004349 /* data is available to read on queue/fifo/lifo */
4350 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004351
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004352 /* queue/fifo/lifo wait was cancelled */
4353 _POLL_STATE_CANCELLED,
4354
Benjamin Walshacc68c12017-01-29 18:57:45 -05004355 _POLL_NUM_STATES
4356};
4357
Patrik Flykt4344e272019-03-08 14:19:05 -07004358#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004359
4360#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004361 (32 - (0 \
4362 + 8 /* tag */ \
4363 + _POLL_NUM_TYPES \
4364 + _POLL_NUM_STATES \
4365 + 1 /* modes */ \
4366 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004367
Benjamin Walshacc68c12017-01-29 18:57:45 -05004368/* end of polling API - PRIVATE */
4369
4370
4371/**
4372 * @defgroup poll_apis Async polling APIs
4373 * @ingroup kernel_apis
4374 * @{
4375 */
4376
4377/* Public polling API */
4378
4379/* public - values for k_poll_event.type bitfield */
4380#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004381#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4382#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4383#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004384#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004385
4386/* public - polling modes */
4387enum k_poll_modes {
4388 /* polling thread does not take ownership of objects when available */
4389 K_POLL_MODE_NOTIFY_ONLY = 0,
4390
4391 K_POLL_NUM_MODES
4392};
4393
4394/* public - values for k_poll_event.state bitfield */
4395#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004396#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4397#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4398#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004399#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004400#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004401
4402/* public - poll signal object */
4403struct k_poll_signal {
4404 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004405 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004406
4407 /*
4408 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4409 * user resets it to 0.
4410 */
4411 unsigned int signaled;
4412
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004413 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004414 int result;
4415};
4416
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004417#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004418 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004419 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004420 .signaled = 0, \
4421 .result = 0, \
4422 }
4423
4424struct k_poll_event {
4425 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004426 sys_dnode_t _node;
4427
4428 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004429 struct _poller *poller;
4430
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004431 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004432 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004433
Benjamin Walshacc68c12017-01-29 18:57:45 -05004434 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004435 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004436
4437 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004438 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004439
4440 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004441 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004442
4443 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004444 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004445
4446 /* per-type data */
4447 union {
4448 void *obj;
4449 struct k_poll_signal *signal;
4450 struct k_sem *sem;
4451 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004452 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004453 };
4454};
4455
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004456#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004457 { \
4458 .poller = NULL, \
4459 .type = event_type, \
4460 .state = K_POLL_STATE_NOT_READY, \
4461 .mode = event_mode, \
4462 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004463 { .obj = event_obj }, \
4464 }
4465
4466#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4467 event_tag) \
4468 { \
4469 .type = event_type, \
4470 .tag = event_tag, \
4471 .state = K_POLL_STATE_NOT_READY, \
4472 .mode = event_mode, \
4473 .unused = 0, \
4474 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004475 }
4476
4477/**
4478 * @brief Initialize one struct k_poll_event instance
4479 *
4480 * After this routine is called on a poll event, the event it ready to be
4481 * placed in an event array to be passed to k_poll().
4482 *
4483 * @param event The event to initialize.
4484 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4485 * values. Only values that apply to the same object being polled
4486 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4487 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004488 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004489 * @param obj Kernel object or poll signal.
4490 *
4491 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004492 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004493 */
4494
Kumar Galacc334c72017-04-21 10:55:34 -05004495extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004496 int mode, void *obj);
4497
4498/**
4499 * @brief Wait for one or many of multiple poll events to occur
4500 *
4501 * This routine allows a thread to wait concurrently for one or many of
4502 * multiple poll events to have occurred. Such events can be a kernel object
4503 * being available, like a semaphore, or a poll signal event.
4504 *
4505 * When an event notifies that a kernel object is available, the kernel object
4506 * is not "given" to the thread calling k_poll(): it merely signals the fact
4507 * that the object was available when the k_poll() call was in effect. Also,
4508 * all threads trying to acquire an object the regular way, i.e. by pending on
4509 * the object, have precedence over the thread polling on the object. This
4510 * means that the polling thread will never get the poll event on an object
4511 * until the object becomes available and its pend queue is empty. For this
4512 * reason, the k_poll() call is more effective when the objects being polled
4513 * only have one thread, the polling thread, trying to acquire them.
4514 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004515 * When k_poll() returns 0, the caller should loop on all the events that were
4516 * passed to k_poll() and check the state field for the values that were
4517 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004518 *
4519 * Before being reused for another call to k_poll(), the user has to reset the
4520 * state field to K_POLL_STATE_NOT_READY.
4521 *
Andrew Boie3772f772018-05-07 16:52:57 -07004522 * When called from user mode, a temporary memory allocation is required from
4523 * the caller's resource pool.
4524 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004525 * @param events An array of pointers to events to be polled for.
4526 * @param num_events The number of events in the array.
4527 * @param timeout Waiting period for an event to be ready (in milliseconds),
4528 * or one of the special values K_NO_WAIT and K_FOREVER.
4529 *
4530 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004531 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004532 * @retval -EINTR Polling has been interrupted, e.g. with
4533 * k_queue_cancel_wait(). All output events are still set and valid,
4534 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4535 * words, -EINTR status means that at least one of output events is
4536 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004537 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4538 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004539 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004540 */
4541
Andrew Boie3772f772018-05-07 16:52:57 -07004542__syscall int k_poll(struct k_poll_event *events, int num_events,
4543 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004544
4545/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004546 * @brief Initialize a poll signal object.
4547 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004548 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004549 *
4550 * @param signal A poll signal.
4551 *
4552 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004553 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004554 */
4555
Andrew Boie3772f772018-05-07 16:52:57 -07004556__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4557
4558/*
4559 * @brief Reset a poll signal object's state to unsignaled.
4560 *
4561 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004562 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004563 */
4564__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4565
Patrik Flykt4344e272019-03-08 14:19:05 -07004566static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004567{
Patrik Flykt24d71432019-03-26 19:57:45 -06004568 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004569}
4570
4571/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004572 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004573 *
4574 * @param signal A poll signal object
4575 * @param signaled An integer buffer which will be written nonzero if the
4576 * object was signaled
4577 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004578 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004579 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004580 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004581 */
4582__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4583 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004584
4585/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004586 * @brief Signal a poll signal object.
4587 *
4588 * This routine makes ready a poll signal, which is basically a poll event of
4589 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4590 * made ready to run. A @a result value can be specified.
4591 *
4592 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004593 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004594 * k_poll_signal_reset(). It thus has to be reset by the user before being
4595 * passed again to k_poll() or k_poll() will consider it being signaled, and
4596 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004597 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004598 * @note The result is stored and the 'signaled' field is set even if
4599 * this function returns an error indicating that an expiring poll was
4600 * not notified. The next k_poll() will detect the missed raise.
4601 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004602 * @param signal A poll signal.
4603 * @param result The value to store in the result field of the signal.
4604 *
4605 * @retval 0 The signal was delivered successfully.
4606 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004607 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004608 */
4609
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004610__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004611
Anas Nashif954d5502018-02-25 08:37:28 -06004612/**
4613 * @internal
4614 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004615extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004616
Anas Nashif166f5192018-02-25 08:02:36 -06004617/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004618
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004619/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004620 * @defgroup cpu_idle_apis CPU Idling APIs
4621 * @ingroup kernel_apis
4622 * @{
4623 */
4624
4625/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004626 * @brief Make the CPU idle.
4627 *
4628 * This function makes the CPU idle until an event wakes it up.
4629 *
4630 * In a regular system, the idle thread should be the only thread responsible
4631 * for making the CPU idle and triggering any type of power management.
4632 * However, in some more constrained systems, such as a single-threaded system,
4633 * the only thread would be responsible for this if needed.
4634 *
4635 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004636 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004637 */
4638extern void k_cpu_idle(void);
4639
4640/**
4641 * @brief Make the CPU idle in an atomic fashion.
4642 *
4643 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4644 * must be done atomically before making the CPU idle.
4645 *
4646 * @param key Interrupt locking key obtained from irq_lock().
4647 *
4648 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004649 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004650 */
4651extern void k_cpu_atomic_idle(unsigned int key);
4652
Anas Nashif30c3cff2019-01-22 08:18:13 -05004653/**
4654 * @}
4655 */
Anas Nashif954d5502018-02-25 08:37:28 -06004656
4657/**
4658 * @internal
4659 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004660extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004661
Patrik Flykt4344e272019-03-08 14:19:05 -07004662#ifdef Z_ARCH_EXCEPT
Andrew Boiecdb94d62017-04-18 15:22:05 -07004663/* This archtecture has direct support for triggering a CPU exception */
Patrik Flykt4344e272019-03-08 14:19:05 -07004664#define z_except_reason(reason) Z_ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004665#else
4666
Andrew Boiecdb94d62017-04-18 15:22:05 -07004667/* NOTE: This is the implementation for arches that do not implement
Patrik Flykt4344e272019-03-08 14:19:05 -07004668 * Z_ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004669 *
4670 * We won't have a real exception frame to determine the PC value when
4671 * the oops occurred, so print file and line number before we jump into
4672 * the fatal error handler.
4673 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004674#define z_except_reason(reason) do { \
Andrew Boiecdb94d62017-04-18 15:22:05 -07004675 printk("@ %s:%d:\n", __FILE__, __LINE__); \
Andrew Boie56236372019-07-15 15:22:29 -07004676 z_fatal_error(reason, NULL); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004677 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004678
4679#endif /* _ARCH__EXCEPT */
4680
4681/**
4682 * @brief Fatally terminate a thread
4683 *
4684 * This should be called when a thread has encountered an unrecoverable
4685 * runtime condition and needs to terminate. What this ultimately
4686 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004687 * will be called will reason code K_ERR_KERNEL_OOPS.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004688 *
4689 * If this is called from ISR context, the default system fatal error handler
4690 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004691 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004692 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004693#define k_oops() z_except_reason(K_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004694
4695/**
4696 * @brief Fatally terminate the system
4697 *
4698 * This should be called when the Zephyr kernel has encountered an
4699 * unrecoverable runtime condition and needs to terminate. What this ultimately
4700 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004701 * will be called will reason code K_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004702 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004703 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004704#define k_panic() z_except_reason(K_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004705
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004706/*
4707 * private APIs that are utilized by one or more public APIs
4708 */
4709
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004710#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004711/**
4712 * @internal
4713 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004714extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004715#else
Anas Nashif954d5502018-02-25 08:37:28 -06004716/**
4717 * @internal
4718 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004719#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004720#endif
4721
Anas Nashif954d5502018-02-25 08:37:28 -06004722/**
4723 * @internal
4724 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004725extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004726/**
4727 * @internal
4728 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004729extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004730
Andrew Boiedc5d9352017-06-02 12:56:47 -07004731/* arch/cpu.h may declare an architecture or platform-specific macro
4732 * for properly declaring stacks, compatible with MMU/MPU constraints if
4733 * enabled
4734 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004735
4736/**
4737 * @brief Obtain an extern reference to a stack
4738 *
4739 * This macro properly brings the symbol of a thread stack declared
4740 * elsewhere into scope.
4741 *
4742 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004743 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004744 */
4745#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4746
Patrik Flykt4344e272019-03-08 14:19:05 -07004747#ifdef Z_ARCH_THREAD_STACK_DEFINE
4748#define K_THREAD_STACK_DEFINE(sym, size) Z_ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004749#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Patrik Flykt4344e272019-03-08 14:19:05 -07004750 Z_ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4751#define K_THREAD_STACK_LEN(size) Z_ARCH_THREAD_STACK_LEN(size)
4752#define K_THREAD_STACK_MEMBER(sym, size) Z_ARCH_THREAD_STACK_MEMBER(sym, size)
4753#define K_THREAD_STACK_SIZEOF(sym) Z_ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie575abc02019-03-19 10:42:24 -07004754#define K_THREAD_STACK_RESERVED Z_ARCH_THREAD_STACK_RESERVED
Andrew Boie4e5c0932019-04-04 12:05:28 -07004755static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004756{
Patrik Flykt4344e272019-03-08 14:19:05 -07004757 return Z_ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07004758}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004759#else
4760/**
4761 * @brief Declare a toplevel thread stack memory region
4762 *
4763 * This declares a region of memory suitable for use as a thread's stack.
4764 *
4765 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4766 * 'noinit' section so that it isn't zeroed at boot
4767 *
Andrew Boie507852a2017-07-25 18:47:07 -07004768 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004769 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07004770 * inside needs to be examined, examine thread->stack_info for the associated
4771 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004772 *
4773 * It is legal to precede this definition with the 'static' keyword.
4774 *
4775 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4776 * parameter of k_thread_create(), it may not be the same as the
4777 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4778 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004779 * Some arches may round the size of the usable stack region up to satisfy
4780 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4781 * size.
4782 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004783 * @param sym Thread stack symbol name
4784 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004785 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004786 */
4787#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004788 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004789
4790/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304791 * @brief Calculate size of stacks to be allocated in a stack array
4792 *
4793 * This macro calculates the size to be allocated for the stacks
4794 * inside a stack array. It accepts the indicated "size" as a parameter
4795 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4796 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4797 *
4798 * @param size Size of the stack memory region
4799 * @req K-TSTACK-001
4800 */
4801#define K_THREAD_STACK_LEN(size) (size)
4802
4803/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004804 * @brief Declare a toplevel array of thread stack memory regions
4805 *
4806 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4807 * definition for additional details and constraints.
4808 *
4809 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4810 * 'noinit' section so that it isn't zeroed at boot
4811 *
4812 * @param sym Thread stack symbol name
4813 * @param nmemb Number of stacks to declare
4814 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004815 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004816 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004817#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004818 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304819 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004820
4821/**
4822 * @brief Declare an embedded stack memory region
4823 *
4824 * Used for stacks embedded within other data structures. Use is highly
4825 * discouraged but in some cases necessary. For memory protection scenarios,
4826 * it is very important that any RAM preceding this member not be writable
4827 * by threads else a stack overflow will lead to silent corruption. In other
4828 * words, the containing data structure should live in RAM owned by the kernel.
4829 *
4830 * @param sym Thread stack symbol name
4831 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004832 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004833 */
4834#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004835 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004836
4837/**
4838 * @brief Return the size in bytes of a stack memory region
4839 *
4840 * Convenience macro for passing the desired stack size to k_thread_create()
4841 * since the underlying implementation may actually create something larger
4842 * (for instance a guard area).
4843 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004844 * The value returned here is not guaranteed to match the 'size' parameter
4845 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004846 *
4847 * @param sym Stack memory symbol
4848 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004849 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004850 */
4851#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4852
Andrew Boie575abc02019-03-19 10:42:24 -07004853
4854/**
4855 * @brief Indicate how much additional memory is reserved for stack objects
4856 *
4857 * Any given stack declaration may have additional memory in it for guard
4858 * areas or supervisor mode stacks. This macro indicates how much space
4859 * is reserved for this. The memory reserved may not be contiguous within
4860 * the stack object, and does not account for additional space used due to
4861 * enforce alignment.
4862 */
4863#define K_THREAD_STACK_RESERVED 0
4864
Andrew Boiedc5d9352017-06-02 12:56:47 -07004865/**
4866 * @brief Get a pointer to the physical stack buffer
4867 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07004868 * This macro is deprecated. If a stack buffer needs to be examined, the
4869 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004870 *
4871 * @param sym Declared stack symbol name
4872 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004873 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004874 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07004875static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004876{
4877 return (char *)sym;
4878}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004879
4880#endif /* _ARCH_DECLARE_STACK */
4881
Chunlin Hane9c97022017-07-07 20:29:30 +08004882/**
4883 * @defgroup mem_domain_apis Memory domain APIs
4884 * @ingroup kernel_apis
4885 * @{
4886 */
4887
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004888/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004889 * @def K_MEM_PARTITION_DEFINE
4890 * @brief Used to declare a memory partition
4891 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004892 */
4893#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4894#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4895 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08004896 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004897 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004898#else
4899#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08004900 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004901 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004902#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4903
Chunlin Hane9c97022017-07-07 20:29:30 +08004904/* memory partition */
4905struct k_mem_partition {
4906 /* start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004907 uintptr_t start;
Chunlin Hane9c97022017-07-07 20:29:30 +08004908 /* size of memory partition */
4909 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004910#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08004911 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304912 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004913#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08004914};
4915
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02004916/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304917 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004918struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304919#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004920 /* partitions in the domain */
4921 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304922#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004923 /* domain q */
4924 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004925 /* number of partitions in the domain */
4926 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004927};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304928
Chunlin Hane9c97022017-07-07 20:29:30 +08004929
4930/**
4931 * @brief Initialize a memory domain.
4932 *
4933 * Initialize a memory domain with given name and memory partitions.
4934 *
Andrew Boiefddd5502019-07-30 18:07:32 -07004935 * See documentation for k_mem_domain_add_partition() for details about
4936 * partition constraints.
4937 *
Chunlin Hane9c97022017-07-07 20:29:30 +08004938 * @param domain The memory domain to be initialized.
4939 * @param num_parts The number of array items of "parts" parameter.
4940 * @param parts An array of pointers to the memory partitions. Can be NULL
4941 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004942 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004943 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004944extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004945 struct k_mem_partition *parts[]);
4946/**
4947 * @brief Destroy a memory domain.
4948 *
4949 * Destroy a memory domain.
4950 *
4951 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004952 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004953 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004954extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4955
4956/**
4957 * @brief Add a memory partition into a memory domain.
4958 *
Andrew Boiefddd5502019-07-30 18:07:32 -07004959 * Add a memory partition into a memory domain. Partitions must conform to
4960 * the following constraints:
4961 *
4962 * - Partition bounds must be within system RAM boundaries on MMU-based
4963 * systems.
4964 * - Partitions in the same memory domain may not overlap each other.
4965 * - Partitions must not be defined which expose private kernel
4966 * data structures or kernel objects.
4967 * - The starting address alignment, and the partition size must conform to
4968 * the constraints of the underlying memory management hardware, which
4969 * varies per architecture.
4970 * - Memory domain partitions are only intended to control access to memory
4971 * from user mode threads.
4972 *
4973 * Violating these constraints may lead to CPU exceptions or undefined
4974 * behavior.
Chunlin Hane9c97022017-07-07 20:29:30 +08004975 *
4976 * @param domain The memory domain to be added a memory partition.
4977 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004978 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004979 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004980extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4981 struct k_mem_partition *part);
4982
4983/**
4984 * @brief Remove a memory partition from a memory domain.
4985 *
4986 * Remove a memory partition from a memory domain.
4987 *
4988 * @param domain The memory domain to be removed a memory partition.
4989 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004990 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004991 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004992extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4993 struct k_mem_partition *part);
4994
4995/**
4996 * @brief Add a thread into a memory domain.
4997 *
4998 * Add a thread into a memory domain.
4999 *
5000 * @param domain The memory domain that the thread is going to be added into.
5001 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005002 *
5003 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005004 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005005extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
5006 k_tid_t thread);
5007
5008/**
5009 * @brief Remove a thread from its memory domain.
5010 *
5011 * Remove a thread from its memory domain.
5012 *
5013 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005014 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005015 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005016extern void k_mem_domain_remove_thread(k_tid_t thread);
5017
Anas Nashif166f5192018-02-25 08:02:36 -06005018/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08005019
Andrew Boie756f9072017-10-10 16:01:49 -07005020/**
5021 * @brief Emit a character buffer to the console device
5022 *
5023 * @param c String of characters to print
5024 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005025 *
5026 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07005027 */
5028__syscall void k_str_out(char *c, size_t n);
5029
Andy Rosse7172672018-01-24 15:48:32 -08005030/**
5031 * @brief Start a numbered CPU on a MP-capable system
5032
5033 * This starts and initializes a specific CPU. The main thread on
5034 * startup is running on CPU zero, other processors are numbered
5035 * sequentially. On return from this function, the CPU is known to
5036 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07005037 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08005038 * with the provided key will work to enable them.
5039 *
5040 * Normally, in SMP mode this function will be called by the kernel
5041 * initialization and should not be used as a user API. But it is
5042 * defined here for special-purpose apps which want Zephyr running on
5043 * one core and to use others for design-specific processing.
5044 *
5045 * @param cpu_num Integer number of the CPU
5046 * @param stack Stack memory for the CPU
5047 * @param sz Stack buffer size, in bytes
5048 * @param fn Function to begin running on the CPU. First argument is
5049 * an irq_unlock() key.
5050 * @param arg Untyped argument to be passed to "fn"
5051 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005052extern void z_arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08005053 void (*fn)(int key, void *data), void *arg);
Andy Rosse7172672018-01-24 15:48:32 -08005054
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005055
5056/**
5057 * @brief Disable preservation of floating point context information.
5058 *
5059 * This routine informs the kernel that the specified thread
5060 * will no longer be using the floating point registers.
5061 *
5062 * @warning
5063 * Some architectures apply restrictions on how the disabling of floating
5064 * point preservation may be requested, see z_arch_float_disable.
5065 *
5066 * @warning
5067 * This routine should only be used to disable floating point support for
5068 * a thread that currently has such support enabled.
5069 *
5070 * @param thread ID of thread.
5071 *
5072 * @retval 0 On success.
5073 * @retval -ENOSYS If the floating point disabling is not implemented.
5074 * -EINVAL If the floating point disabling could not be performed.
5075 */
5076__syscall int k_float_disable(struct k_thread *thread);
5077
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005078#ifdef __cplusplus
5079}
5080#endif
5081
Anas Nashif10291a02019-06-25 12:25:12 -04005082#include <debug/tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07005083#include <syscalls/kernel.h>
5084
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05005085#endif /* !_ASMLANGUAGE */
5086
Flavio Ceolin67ca1762018-09-14 10:43:44 -07005087#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */