blob: 8ff4cd61b00ef644be0c79d43ba2210ab7d2357c [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
Flavio Ceolin67ca1762018-09-14 10:43:44 -070013#ifndef ZEPHYR_INCLUDE_KERNEL_H_
14#define ZEPHYR_INCLUDE_KERNEL_H_
Benjamin Walsh456c6da2016-09-02 18:55:39 -040015
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Ioannis Glaropoulos92b8a412018-06-20 17:30:48 +020017#include <kernel_includes.h>
Kumar Gala8777ff12018-07-25 20:24:34 -050018#include <errno.h>
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -070019#include <stdbool.h>
Stephanos Ioannidis33fbe002019-09-09 21:26:59 +090020#include <toolchain.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040021
22#ifdef __cplusplus
23extern "C" {
24#endif
25
Anas Nashifbbb157d2017-01-15 08:46:31 -050026/**
27 * @brief Kernel APIs
28 * @defgroup kernel_apis Kernel APIs
29 * @{
30 * @}
31 */
32
Anas Nashif61f4b242016-11-18 10:53:59 -050033#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040034#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
35#else
36#define K_DEBUG(fmt, ...)
37#endif
38
Benjamin Walsh2f280412017-01-14 19:23:46 -050039#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
40#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
41#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
42#elif defined(CONFIG_COOP_ENABLED)
43#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
44#define _NUM_PREEMPT_PRIO (0)
45#elif defined(CONFIG_PREEMPT_ENABLED)
46#define _NUM_COOP_PRIO (0)
47#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
48#else
49#error "invalid configuration"
50#endif
51
52#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040053#define K_PRIO_PREEMPT(x) (x)
54
Benjamin Walsh456c6da2016-09-02 18:55:39 -040055#define K_ANY NULL
56#define K_END NULL
57
Benjamin Walshedb35702017-01-14 18:47:22 -050058#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040059#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050060#elif defined(CONFIG_COOP_ENABLED)
61#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
62#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040063#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050064#else
65#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040066#endif
67
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050068#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040069#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
70#else
71#define K_LOWEST_THREAD_PRIO -1
72#endif
73
Benjamin Walshfab8d922016-11-08 15:36:36 -050074#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
75
Benjamin Walsh456c6da2016-09-02 18:55:39 -040076#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
77#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
78
Andy Ross225c74b2018-06-27 11:20:50 -070079#ifdef CONFIG_WAITQ_SCALABLE
Andy Ross1acd8c22018-05-03 14:51:49 -070080
81typedef struct {
82 struct _priq_rb waitq;
83} _wait_q_t;
84
Patrik Flykt4344e272019-03-08 14:19:05 -070085extern bool z_priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
Andy Ross1acd8c22018-05-03 14:51:49 -070086
Patrik Flykt4344e272019-03-08 14:19:05 -070087#define Z_WAIT_Q_INIT(wait_q) { { { .lessthan_fn = z_priq_rb_lessthan } } }
Andy Ross1acd8c22018-05-03 14:51:49 -070088
89#else
90
Andy Rossccf3bf72018-05-10 11:10:34 -070091typedef struct {
92 sys_dlist_t waitq;
93} _wait_q_t;
94
Patrik Flykt4344e272019-03-08 14:19:05 -070095#define Z_WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096
Andy Ross1acd8c22018-05-03 14:51:49 -070097#endif
98
Anas Nashif2f203c22016-12-18 06:57:45 -050099#ifdef CONFIG_OBJECT_TRACING
Flavio Ceolind1ed3362018-12-07 11:39:13 -0800100#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next;
Anas Nashif2f203c22016-12-18 06:57:45 -0500101#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400102#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500103#define _OBJECT_TRACING_INIT
Flavio Ceolind1ed3362018-12-07 11:39:13 -0800104#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400105#endif
106
Benjamin Walshacc68c12017-01-29 18:57:45 -0500107#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300108#define _POLL_EVENT_OBJ_INIT(obj) \
109 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
110#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500111#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300112#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500113#define _POLL_EVENT
114#endif
115
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500116struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400117struct k_mutex;
118struct k_sem;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400119struct k_msgq;
120struct k_mbox;
121struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200122struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400123struct k_fifo;
124struct k_lifo;
125struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400126struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400127struct k_mem_pool;
128struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500129struct k_poll_event;
130struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800131struct k_mem_domain;
132struct k_mem_partition;
Wentong Wu5611e922019-06-20 23:51:27 +0800133struct k_futex;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400134
Andrew Boie5bd891d2017-09-27 12:59:28 -0700135/* This enumeration needs to be kept in sync with the lists of kernel objects
136 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
137 * function in kernel/userspace.c
138 */
Andrew Boie945af952017-08-22 13:15:23 -0700139enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700140 K_OBJ_ANY,
141
Leandro Pereirac2003672018-04-04 13:50:32 -0700142 /** @cond
143 * Doxygen should ignore this build-time generated include file
144 * when genrating API documentation. Enumeration values are
145 * generated during build by gen_kobject_list.py. It includes
146 * basic kernel objects (e.g. pipes and mutexes) and driver types.
147 */
148#include <kobj-types-enum.h>
149 /** @endcond
150 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700151
Andrew Boie945af952017-08-22 13:15:23 -0700152 K_OBJ_LAST
153};
Anas Nashif4bcb2942019-01-23 23:06:29 -0500154/**
155 * @defgroup usermode_apis User Mode APIs
156 * @ingroup kernel_apis
157 * @{
158 */
Andrew Boie945af952017-08-22 13:15:23 -0700159
160#ifdef CONFIG_USERSPACE
161/* Table generated by gperf, these objects are retrieved via
Patrik Flykt4344e272019-03-08 14:19:05 -0700162 * z_object_find() */
Andrew Boie945af952017-08-22 13:15:23 -0700163struct _k_object {
164 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700165 u8_t perms[CONFIG_MAX_THREAD_BYTES];
166 u8_t type;
167 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700168 u32_t data;
Andrew Boiedf555242018-05-25 07:28:54 -0700169} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700170
Andrew Boie877f82e2017-10-17 11:20:22 -0700171struct _k_object_assignment {
172 struct k_thread *thread;
173 void * const *objects;
174};
175
176/**
177 * @brief Grant a static thread access to a list of kernel objects
178 *
179 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
180 * a set of kernel objects. These objects do not need to be in an initialized
181 * state. The permissions will be granted when the threads are initialized
182 * in the early boot sequence.
183 *
184 * All arguments beyond the first must be pointers to kernel objects.
185 *
186 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
187 */
188#define K_THREAD_ACCESS_GRANT(name_, ...) \
189 static void * const _CONCAT(_object_list_, name_)[] = \
190 { __VA_ARGS__, NULL }; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400191 static const Z_STRUCT_SECTION_ITERABLE(_k_object_assignment, \
192 _CONCAT(_object_access_, name_)) = \
Andrew Boie877f82e2017-10-17 11:20:22 -0700193 { (&_k_thread_obj_ ## name_), \
194 (_CONCAT(_object_list_, name_)) }
195
Andrew Boie945af952017-08-22 13:15:23 -0700196#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700197#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700198#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie78757072019-07-23 13:29:30 -0700199#define K_OBJ_FLAG_DRIVER BIT(3)
Andrew Boie945af952017-08-22 13:15:23 -0700200
201/**
202 * Lookup a kernel object and init its metadata if it exists
203 *
204 * Calling this on an object will make it usable from userspace.
205 * Intended to be called as the last statement in kernel object init
206 * functions.
207 *
Anas Nashif50e3acd2018-12-08 13:26:18 -0500208 * @param obj Address of the kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700209 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700210void z_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700211#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700212/* LCOV_EXCL_START */
Andrew Boie877f82e2017-10-17 11:20:22 -0700213#define K_THREAD_ACCESS_GRANT(thread, ...)
214
Anas Nashif954d5502018-02-25 08:37:28 -0600215/**
216 * @internal
217 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700218static inline void z_object_init(void *obj)
Andrew Boie743e4682017-10-04 12:25:50 -0700219{
220 ARG_UNUSED(obj);
221}
222
Anas Nashif954d5502018-02-25 08:37:28 -0600223/**
224 * @internal
225 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700226static inline void z_impl_k_object_access_grant(void *object,
Andrew Boie743e4682017-10-04 12:25:50 -0700227 struct k_thread *thread)
228{
229 ARG_UNUSED(object);
230 ARG_UNUSED(thread);
231}
232
Anas Nashif954d5502018-02-25 08:37:28 -0600233/**
234 * @internal
235 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700236static inline void k_object_access_revoke(void *object,
237 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700238{
239 ARG_UNUSED(object);
240 ARG_UNUSED(thread);
241}
242
Andrew Boiee9cfc542018-04-13 13:15:28 -0700243/**
244 * @internal
245 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700246static inline void z_impl_k_object_release(void *object)
Andrew Boiee9cfc542018-04-13 13:15:28 -0700247{
248 ARG_UNUSED(object);
249}
250
Andrew Boie41bab6e2017-10-14 14:42:23 -0700251static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700252{
253 ARG_UNUSED(object);
254}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700255/* LCOV_EXCL_STOP */
Andrew Boie743e4682017-10-04 12:25:50 -0700256#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700257
258/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600259 * Grant a thread access to a kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700260 *
261 * The thread will be granted access to the object if the caller is from
262 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700263 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700264 *
265 * @param object Address of kernel object
266 * @param thread Thread to grant access to the object
267 */
Andrew Boie743e4682017-10-04 12:25:50 -0700268__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700269
Andrew Boiea89bf012017-10-09 14:47:55 -0700270/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600271 * Revoke a thread's access to a kernel object
Andrew Boiea89bf012017-10-09 14:47:55 -0700272 *
273 * The thread will lose access to the object if the caller is from
274 * supervisor mode, or the caller is from user mode AND has permissions
275 * on both the object and the thread whose access is being revoked.
276 *
277 * @param object Address of kernel object
278 * @param thread Thread to remove access to the object
279 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700280void k_object_access_revoke(void *object, struct k_thread *thread);
281
282
283__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700284
285/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600286 * Grant all present and future threads access to an object
Andrew Boie3b5ae802017-10-04 12:10:32 -0700287 *
288 * If the caller is from supervisor mode, or the caller is from user mode and
289 * have sufficient permissions on the object, then that object will have
290 * permissions granted to it for *all* current and future threads running in
291 * the system, effectively becoming a public kernel object.
292 *
293 * Use of this API should be avoided on systems that are running untrusted code
294 * as it is possible for such code to derive the addresses of kernel objects
295 * and perform unwanted operations on them.
296 *
Andrew Boie04caa672017-10-13 13:57:07 -0700297 * It is not possible to revoke permissions on public objects; once public,
298 * any thread may use it.
299 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700300 * @param object Address of kernel object
301 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700302void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700303
Andrew Boie31bdfc02017-11-08 16:38:03 -0800304/**
305 * Allocate a kernel object of a designated type
306 *
307 * This will instantiate at runtime a kernel object of the specified type,
308 * returning a pointer to it. The object will be returned in an uninitialized
309 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700310 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800311 *
312 * Currently, allocation of thread stacks is not supported.
313 *
314 * @param otype Requested kernel object type
315 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
316 * available
317 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700318__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800319
Andrew Boie97bf0012018-04-24 17:01:37 -0700320#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800321/**
322 * Free a kernel object previously allocated with k_object_alloc()
323 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700324 * This will return memory for a kernel object back to resource pool it was
325 * allocated from. Care must be exercised that the object will not be used
326 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800327 *
328 * @param obj Pointer to the kernel object memory address.
329 */
330void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700331#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700332/* LCOV_EXCL_START */
Patrik Flykt4344e272019-03-08 14:19:05 -0700333static inline void *z_impl_k_object_alloc(enum k_objects otype)
Andrew Boie97bf0012018-04-24 17:01:37 -0700334{
Kumar Gala85699f72018-05-17 09:26:03 -0500335 ARG_UNUSED(otype);
336
Andrew Boie97bf0012018-04-24 17:01:37 -0700337 return NULL;
338}
339
340static inline void k_obj_free(void *obj)
341{
342 ARG_UNUSED(obj);
343}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700344/* LCOV_EXCL_STOP */
Andrew Boie31bdfc02017-11-08 16:38:03 -0800345#endif /* CONFIG_DYNAMIC_OBJECTS */
346
Anas Nashif4bcb2942019-01-23 23:06:29 -0500347/** @} */
348
Andrew Boiebca15da2017-10-15 14:17:48 -0700349/* Using typedef deliberately here, this is quite intended to be an opaque
Andrew Boie4e5c0932019-04-04 12:05:28 -0700350 * type.
Andrew Boiebca15da2017-10-15 14:17:48 -0700351 *
352 * The purpose of this data type is to clearly distinguish between the
353 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
354 * buffer which composes the stack data actually used by the underlying
Anas Nashiff2cb20c2019-06-18 14:45:40 -0400355 * thread; they cannot be used interchangeably as some arches precede the
Andrew Boiebca15da2017-10-15 14:17:48 -0700356 * stack buffer region with guard areas that trigger a MPU or MMU fault
357 * if written to.
358 *
359 * APIs that want to work with the buffer inside should continue to use
360 * char *.
361 *
362 * Stacks should always be created with K_THREAD_STACK_DEFINE().
363 */
364struct __packed _k_thread_stack_element {
365 char data;
366};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700367typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700368
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700369/**
370 * @typedef k_thread_entry_t
371 * @brief Thread entry point function type.
372 *
373 * A thread's entry point function is invoked when the thread starts executing.
374 * Up to 3 argument values can be passed to the function.
375 *
376 * The thread terminates execution permanently if the entry point function
377 * returns. The thread is responsible for releasing any shared resources
378 * it may own (such as mutexes and dynamically allocated memory), prior to
379 * returning.
380 *
381 * @param p1 First argument.
382 * @param p2 Second argument.
383 * @param p3 Third argument.
384 *
385 * @return N/A
386 */
387typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700388
389#ifdef CONFIG_THREAD_MONITOR
390struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700391 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700392 void *parameter1;
393 void *parameter2;
394 void *parameter3;
395};
396#endif
397
398/* can be used for creating 'dummy' threads, e.g. for pending on objects */
399struct _thread_base {
400
401 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700402 union {
Peter A. Bigot82ad0d22019-01-03 23:49:28 -0600403 sys_dnode_t qnode_dlist;
Andy Ross1acd8c22018-05-03 14:51:49 -0700404 struct rbnode qnode_rb;
405 };
406
Andy Ross1acd8c22018-05-03 14:51:49 -0700407 /* wait queue on which the thread is pended (needed only for
408 * trees, not dumb lists)
409 */
410 _wait_q_t *pended_on;
Andrew Boie73abd322017-04-04 13:19:13 -0700411
412 /* user facing 'thread options'; values defined in include/kernel.h */
413 u8_t user_options;
414
415 /* thread state */
416 u8_t thread_state;
417
418 /*
419 * scheduler lock count and thread priority
420 *
421 * These two fields control the preemptibility of a thread.
422 *
423 * When the scheduler is locked, sched_locked is decremented, which
424 * means that the scheduler is locked for values from 0xff to 0x01. A
425 * thread is coop if its prio is negative, thus 0x80 to 0xff when
426 * looked at the value as unsigned.
427 *
428 * By putting them end-to-end, this means that a thread is
429 * non-preemptible if the bundled value is greater than or equal to
430 * 0x0080.
431 */
432 union {
433 struct {
434#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
435 u8_t sched_locked;
436 s8_t prio;
437#else /* LITTLE and PDP */
438 s8_t prio;
439 u8_t sched_locked;
440#endif
441 };
442 u16_t preempt;
443 };
444
Andy Ross4a2e50f2018-05-15 11:06:25 -0700445#ifdef CONFIG_SCHED_DEADLINE
446 int prio_deadline;
447#endif
448
Andy Ross1acd8c22018-05-03 14:51:49 -0700449 u32_t order_key;
450
Andy Ross2724fd12018-01-29 14:55:20 -0800451#ifdef CONFIG_SMP
452 /* True for the per-CPU idle threads */
453 u8_t is_idle;
454
Andy Ross2724fd12018-01-29 14:55:20 -0800455 /* CPU index on which thread was last run */
456 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700457
458 /* Recursive count of irq_lock() calls */
459 u8_t global_lock_count;
Andy Rossab46b1b2019-01-30 15:00:42 -0800460
461#endif
462
463#ifdef CONFIG_SCHED_CPU_MASK
464 /* "May run on" bits for each CPU */
465 u8_t cpu_mask;
Andy Ross2724fd12018-01-29 14:55:20 -0800466#endif
467
Andrew Boie73abd322017-04-04 13:19:13 -0700468 /* data returned by APIs */
469 void *swap_data;
470
471#ifdef CONFIG_SYS_CLOCK_EXISTS
472 /* this thread's entry in a timeout queue */
473 struct _timeout timeout;
474#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700475};
476
477typedef struct _thread_base _thread_base_t;
478
479#if defined(CONFIG_THREAD_STACK_INFO)
480/* Contains the stack information of a thread */
481struct _thread_stack_info {
Andrew Boie4e5c0932019-04-04 12:05:28 -0700482 /* Stack start - Represents the start address of the thread-writable
483 * stack area.
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700484 */
Nicolas Pitre58d839b2019-05-21 11:32:21 -0400485 uintptr_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700486
487 /* Stack Size - Thread writable stack buffer size. Represents
488 * the size of the actual area, starting from the start member,
489 * that should be writable by the thread
490 */
Andrew Boie73abd322017-04-04 13:19:13 -0700491 u32_t size;
492};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700493
494typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700495#endif /* CONFIG_THREAD_STACK_INFO */
496
Chunlin Hane9c97022017-07-07 20:29:30 +0800497#if defined(CONFIG_USERSPACE)
498struct _mem_domain_info {
499 /* memory domain queue node */
500 sys_dnode_t mem_domain_q_node;
501 /* memory domain of the thread */
502 struct k_mem_domain *mem_domain;
503};
504
505#endif /* CONFIG_USERSPACE */
506
Daniel Leungfc182432018-08-16 15:42:28 -0700507#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
508struct _thread_userspace_local_data {
509 int errno_var;
510};
511#endif
512
Anas Nashifce78d162018-05-24 12:43:11 -0500513/**
514 * @ingroup thread_apis
515 * Thread Structure
516 */
Andrew Boie73abd322017-04-04 13:19:13 -0700517struct k_thread {
518
519 struct _thread_base base;
520
Anas Nashifce78d162018-05-24 12:43:11 -0500521 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700522 struct _callee_saved callee_saved;
523
Anas Nashifce78d162018-05-24 12:43:11 -0500524 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700525 void *init_data;
526
Anas Nashifce78d162018-05-24 12:43:11 -0500527 /**
528 * abort function
529 * @req K-THREAD-002
530 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700531 void (*fn_abort)(void);
532
533#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500534 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700535 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700536
Anas Nashifce78d162018-05-24 12:43:11 -0500537 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700538 struct k_thread *next_thread;
539#endif
540
Anas Nashif57554052018-03-03 02:31:05 -0600541#if defined(CONFIG_THREAD_NAME)
542 /* Thread name */
Andrew Boie38129ce2019-06-25 08:54:37 -0700543 char name[CONFIG_THREAD_MAX_NAME_LEN];
Anas Nashif57554052018-03-03 02:31:05 -0600544#endif
545
Andrew Boie73abd322017-04-04 13:19:13 -0700546#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500547 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700548 void *custom_data;
549#endif
550
Daniel Leungfc182432018-08-16 15:42:28 -0700551#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
552 struct _thread_userspace_local_data *userspace_local_data;
553#endif
554
Andrew Boie73abd322017-04-04 13:19:13 -0700555#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700556#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500557 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700558 int errno_var;
559#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700560#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700561
562#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500563 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700564 struct _thread_stack_info stack_info;
565#endif /* CONFIG_THREAD_STACK_INFO */
566
Chunlin Hane9c97022017-07-07 20:29:30 +0800567#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500568 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800569 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500570 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700571 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800572#endif /* CONFIG_USERSPACE */
573
Andy Ross042d8ec2017-12-09 08:37:20 -0800574#if defined(CONFIG_USE_SWITCH)
575 /* When using __switch() a few previously arch-specific items
576 * become part of the core OS
577 */
578
Patrik Flykt4344e272019-03-08 14:19:05 -0700579 /** z_swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800580 int swap_retval;
581
Patrik Flykt7c0a2452019-03-14 09:20:46 -0600582 /** Context handle returned via z_arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800583 void *switch_handle;
584#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500585 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700586 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800587
Anas Nashifce78d162018-05-24 12:43:11 -0500588 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700589 struct _thread_arch arch;
590};
591
592typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400593typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400594
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400595enum execution_context_types {
596 K_ISR = 0,
597 K_COOP_THREAD,
598 K_PREEMPT_THREAD,
599};
600
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400601/**
Anas Nashif4bcb2942019-01-23 23:06:29 -0500602 * @addtogroup thread_apis
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100603 * @{
604 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530605typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
606 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100607
608/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530609 * @brief Iterate over all the threads in the system.
610 *
611 * This routine iterates over all the threads in the system and
612 * calls the user_cb function for each thread.
613 *
614 * @param user_cb Pointer to the user callback function.
615 * @param user_data Pointer to user data.
616 *
617 * @note CONFIG_THREAD_MONITOR must be set for this function
618 * to be effective. Also this API uses irq_lock to protect the
619 * _kernel.threads list which means creation of new threads and
620 * terminations of existing threads are blocked until this
621 * API returns.
622 *
623 * @return N/A
624 */
625extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
626
Anas Nashif166f5192018-02-25 08:02:36 -0600627/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100628
629/**
Allan Stephensc98da842016-11-11 15:45:03 -0500630 * @defgroup thread_apis Thread APIs
631 * @ingroup kernel_apis
632 * @{
633 */
634
Benjamin Walshed240f22017-01-22 13:05:08 -0500635#endif /* !_ASMLANGUAGE */
636
637
638/*
639 * Thread user options. May be needed by assembly code. Common part uses low
640 * bits, arch-specific use high bits.
641 */
642
Anas Nashifa541e932018-05-24 11:19:16 -0500643/**
644 * @brief system thread that must not abort
645 * @req K-THREAD-000
646 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700647#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500648
649#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500650/**
651 * @brief thread uses floating point registers
652 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700653#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500654#endif
655
Anas Nashifa541e932018-05-24 11:19:16 -0500656/**
657 * @brief user mode thread
658 *
659 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700660 * has additional restrictions
661 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700662#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700663
Anas Nashifa541e932018-05-24 11:19:16 -0500664/**
665 * @brief Inherit Permissions
666 *
667 * @details
668 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700669 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
670 * is not enabled.
671 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700672#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700673
Benjamin Walshed240f22017-01-22 13:05:08 -0500674#ifdef CONFIG_X86
675/* x86 Bitmask definitions for threads user options */
676
677#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
678/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700679#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500680#endif
681#endif
682
683/* end - thread options */
684
685#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400686/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700687 * @brief Create a thread.
688 *
689 * This routine initializes a thread, then schedules it for execution.
690 *
691 * The new thread may be scheduled for immediate execution or a delayed start.
692 * If the newly spawned thread does not have a delayed start the kernel
693 * scheduler may preempt the current thread to allow the new thread to
694 * execute.
695 *
696 * Thread options are architecture-specific, and can include K_ESSENTIAL,
697 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
698 * them using "|" (the logical OR operator).
699 *
700 * Historically, users often would use the beginning of the stack memory region
701 * to store the struct k_thread data, although corruption will occur if the
702 * stack overflows this region and stack protection features may not detect this
703 * situation.
704 *
705 * @param new_thread Pointer to uninitialized struct k_thread
706 * @param stack Pointer to the stack space.
707 * @param stack_size Stack size in bytes.
708 * @param entry Thread entry function.
709 * @param p1 1st entry point parameter.
710 * @param p2 2nd entry point parameter.
711 * @param p3 3rd entry point parameter.
712 * @param prio Thread priority.
713 * @param options Thread options.
714 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
715 *
716 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400717 *
718 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700719 */
Andrew Boie662c3452017-10-02 10:51:18 -0700720__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700721 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700722 size_t stack_size,
723 k_thread_entry_t entry,
724 void *p1, void *p2, void *p3,
725 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700726
Andrew Boie3f091b52017-08-30 14:34:14 -0700727/**
728 * @brief Drop a thread's privileges permanently to user mode
729 *
730 * @param entry Function to start executing from
731 * @param p1 1st entry point parameter
732 * @param p2 2nd entry point parameter
733 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400734 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700735 */
736extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
737 void *p1, void *p2,
738 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700739
Andrew Boied26cf2d2017-03-30 13:07:02 -0700740/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530741 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700742 *
743 * This is a convenience function. For the provided thread, grant access to
744 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700745 *
746 * The thread object must be initialized (i.e. running). The objects don't
747 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530748 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700749 *
750 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530751 * @param ... list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400752 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700753 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530754#define k_thread_access_grant(thread, ...) \
755 FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700756
757/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700758 * @brief Assign a resource memory pool to a thread
759 *
760 * By default, threads have no resource pool assigned unless their parent
761 * thread has a resource pool, in which case it is inherited. Multiple
762 * threads may be assigned to the same memory pool.
763 *
764 * Changing a thread's resource pool will not migrate allocations from the
765 * previous pool.
766 *
767 * @param thread Target thread to assign a memory pool for resource requests,
768 * or NULL if the thread should no longer have a memory pool.
769 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400770 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700771 */
772static inline void k_thread_resource_pool_assign(struct k_thread *thread,
773 struct k_mem_pool *pool)
774{
775 thread->resource_pool = pool;
776}
777
778#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
779/**
780 * @brief Assign the system heap as a thread's resource pool
781 *
782 * Similar to k_thread_resource_pool_assign(), but the thread will use
783 * the kernel heap to draw memory.
784 *
785 * Use with caution, as a malicious thread could perform DoS attacks on the
786 * kernel heap.
787 *
788 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400789 *
790 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700791 */
792void k_thread_system_pool_assign(struct k_thread *thread);
793#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
794
795/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500796 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400797 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700798 * This routine puts the current thread to sleep for @a duration milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400799 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700800 * @param ms Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400801 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200802 * @return Zero if the requested time has elapsed or the number of milliseconds
803 * left to sleep, if thread was woken up by \ref k_wakeup call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400804 */
Charles E. Yousea5678312019-05-09 16:46:46 -0700805__syscall s32_t k_sleep(s32_t ms);
806
807/**
808 * @brief Put the current thread to sleep with microsecond resolution.
809 *
810 * This function is unlikely to work as expected without kernel tuning.
811 * In particular, because the lower bound on the duration of a sleep is
812 * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted
813 * to achieve the resolution desired. The implications of doing this must
814 * be understood before attempting to use k_usleep(). Use with caution.
815 *
816 * @param us Number of microseconds to sleep.
817 *
818 * @return Zero if the requested time has elapsed or the number of microseconds
819 * left to sleep, if thread was woken up by \ref k_wakeup call.
820 */
821__syscall s32_t k_usleep(s32_t us);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400822
823/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500824 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400825 *
826 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500827 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400828 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829 * @return N/A
830 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800831__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832
833/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500834 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400835 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500836 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400837 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500838 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400839 *
840 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400841 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400842 */
Andrew Boie468190a2017-09-29 14:00:48 -0700843__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844
845/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500846 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500848 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500850 * If @a thread is not currently sleeping, the routine has no effect.
851 *
852 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400853 *
854 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400855 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856 */
Andrew Boie468190a2017-09-29 14:00:48 -0700857__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400858
859/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500860 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500862 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400863 *
864 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700866__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400867
868/**
Allan Stephensc98da842016-11-11 15:45:03 -0500869 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500871 * This routine permanently stops execution of @a thread. The thread is taken
872 * off all kernel queues it is part of (i.e. the ready queue, the timeout
873 * queue, or a kernel object wait queue). However, any kernel resources the
874 * thread might currently own (such as mutexes or memory blocks) are not
875 * released. It is the responsibility of the caller of this routine to ensure
876 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500878 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400879 *
880 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400881 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400882 */
Andrew Boie468190a2017-09-29 14:00:48 -0700883__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400884
Andrew Boie7d627c52017-08-30 11:01:56 -0700885
886/**
887 * @brief Start an inactive thread
888 *
889 * If a thread was created with K_FOREVER in the delay parameter, it will
890 * not be added to the scheduling queue until this function is called
891 * on it.
892 *
893 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400894 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700895 */
Andrew Boie468190a2017-09-29 14:00:48 -0700896__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700897
Allan Stephensc98da842016-11-11 15:45:03 -0500898/**
899 * @cond INTERNAL_HIDDEN
900 */
901
Benjamin Walshd211a522016-12-06 11:44:01 -0500902/* timeout has timed out and is not on _timeout_q anymore */
903#define _EXPIRED (-2)
904
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400905struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700906 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700907 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400908 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700909 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500910 void *init_p1;
911 void *init_p2;
912 void *init_p3;
913 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500914 u32_t init_options;
915 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500916 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600917 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400918};
919
Andrew Boied26cf2d2017-03-30 13:07:02 -0700920#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400921 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600922 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500923 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700924 .init_thread = (thread), \
925 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500926 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700927 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400928 .init_p1 = (void *)p1, \
929 .init_p2 = (void *)p2, \
930 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500931 .init_prio = (prio), \
932 .init_options = (options), \
933 .init_delay = (delay), \
934 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600935 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400936 }
937
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400938/**
Allan Stephensc98da842016-11-11 15:45:03 -0500939 * INTERNAL_HIDDEN @endcond
940 */
941
942/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500943 * @brief Statically define and initialize a thread.
944 *
945 * The thread may be scheduled for immediate execution or a delayed start.
946 *
947 * Thread options are architecture-specific, and can include K_ESSENTIAL,
948 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
949 * them using "|" (the logical OR operator).
950 *
951 * The ID of the thread can be accessed using:
952 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500953 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500954 *
955 * @param name Name of the thread.
956 * @param stack_size Stack size in bytes.
957 * @param entry Thread entry function.
958 * @param p1 1st entry point parameter.
959 * @param p2 2nd entry point parameter.
960 * @param p3 3rd entry point parameter.
961 * @param prio Thread priority.
962 * @param options Thread options.
963 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400964 *
Anas Nashif47420d02018-05-24 14:20:56 -0400965 * @req K-THREAD-010
966 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400967 * @internal It has been observed that the x86 compiler by default aligns
968 * these _static_thread_data structures to 32-byte boundaries, thereby
969 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400970 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400971 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500972#define K_THREAD_DEFINE(name, stack_size, \
973 entry, p1, p2, p3, \
974 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700975 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400976 struct k_thread _k_thread_obj_##name; \
977 Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\
Andrew Boied26cf2d2017-03-30 13:07:02 -0700978 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
979 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500980 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -0600981 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700982 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400983
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400984/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500985 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500987 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500989 * @param thread ID of thread whose priority is needed.
990 *
991 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400992 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400993 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700994__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400995
996/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500997 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500999 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001000 *
1001 * Rescheduling can occur immediately depending on the priority @a thread is
1002 * set to:
1003 *
1004 * - If its priority is raised above the priority of the caller of this
1005 * function, and the caller is preemptible, @a thread will be scheduled in.
1006 *
1007 * - If the caller operates on itself, it lowers its priority below that of
1008 * other threads in the system, and the caller is preemptible, the thread of
1009 * highest priority will be scheduled in.
1010 *
1011 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1012 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1013 * highest priority.
1014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001015 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001016 * @param prio New priority.
1017 *
1018 * @warning Changing the priority of a thread currently involved in mutex
1019 * priority inheritance may result in undefined behavior.
1020 *
1021 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001022 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001023 */
Andrew Boie468190a2017-09-29 14:00:48 -07001024__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001025
Andy Ross4a2e50f2018-05-15 11:06:25 -07001026
1027#ifdef CONFIG_SCHED_DEADLINE
1028/**
1029 * @brief Set deadline expiration time for scheduler
1030 *
1031 * This sets the "deadline" expiration as a time delta from the
1032 * current time, in the same units used by k_cycle_get_32(). The
1033 * scheduler (when deadline scheduling is enabled) will choose the
1034 * next expiring thread when selecting between threads at the same
1035 * static priority. Threads at different priorities will be scheduled
1036 * according to their static priority.
1037 *
1038 * @note Deadlines that are negative (i.e. in the past) are still seen
1039 * as higher priority than others, even if the thread has "finished"
1040 * its work. If you don't want it scheduled anymore, you have to
1041 * reset the deadline into the future, block/pend the thread, or
1042 * modify its priority with k_thread_priority_set().
1043 *
1044 * @note Despite the API naming, the scheduler makes no guarantees the
1045 * the thread WILL be scheduled within that deadline, nor does it take
1046 * extra metadata (like e.g. the "runtime" and "period" parameters in
1047 * Linux sched_setattr()) that allows the kernel to validate the
1048 * scheduling for achievability. Such features could be implemented
1049 * above this call, which is simply input to the priority selection
1050 * logic.
1051 *
Anas Nashif240c5162019-06-10 12:25:50 -04001052 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001053 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001054 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1055 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001056 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001057 *
Andy Ross4a2e50f2018-05-15 11:06:25 -07001058 * @param thread A thread on which to set the deadline
1059 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001060 *
1061 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001062 */
1063__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1064#endif
1065
Andy Rossab46b1b2019-01-30 15:00:42 -08001066#ifdef CONFIG_SCHED_CPU_MASK
1067/**
1068 * @brief Sets all CPU enable masks to zero
1069 *
1070 * After this returns, the thread will no longer be schedulable on any
1071 * CPUs. The thread must not be currently runnable.
1072 *
Anas Nashif240c5162019-06-10 12:25:50 -04001073 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001074 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001075 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1076 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001077 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001078 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001079 * @param thread Thread to operate upon
1080 * @return Zero on success, otherwise error code
1081 */
1082int k_thread_cpu_mask_clear(k_tid_t thread);
1083
1084/**
1085 * @brief Sets all CPU enable masks to one
1086 *
1087 * After this returns, the thread will be schedulable on any CPU. The
1088 * thread must not be currently runnable.
1089 *
Anas Nashif240c5162019-06-10 12:25:50 -04001090 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001091 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001092 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1093 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001094 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001095 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001096 * @param thread Thread to operate upon
1097 * @return Zero on success, otherwise error code
1098 */
1099int k_thread_cpu_mask_enable_all(k_tid_t thread);
1100
1101/**
1102 * @brief Enable thread to run on specified CPU
1103 *
1104 * The thread must not be currently runnable.
1105 *
Anas Nashif240c5162019-06-10 12:25:50 -04001106 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001107 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001108 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1109 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001110 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001111 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001112 * @param thread Thread to operate upon
1113 * @param cpu CPU index
1114 * @return Zero on success, otherwise error code
1115 */
1116int k_thread_cpu_mask_enable(k_tid_t thread, int cpu);
1117
1118/**
1119 * @brief Prevent thread to run on specified CPU
1120 *
1121 * The thread must not be currently runnable.
1122 *
Anas Nashif240c5162019-06-10 12:25:50 -04001123 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001124 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001125 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1126 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001127 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001128 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001129 * @param thread Thread to operate upon
1130 * @param cpu CPU index
1131 * @return Zero on success, otherwise error code
1132 */
1133int k_thread_cpu_mask_disable(k_tid_t thread, int cpu);
1134#endif
1135
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001136/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001137 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001139 * This routine prevents the kernel scheduler from making @a thread the
1140 * current thread. All other internal operations on @a thread are still
1141 * performed; for example, any timeout it is waiting on keeps ticking,
1142 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001144 * If @a thread is already suspended, the routine has no effect.
1145 *
1146 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001147 *
1148 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001149 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001150 */
Andrew Boie468190a2017-09-29 14:00:48 -07001151__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001152
1153/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001154 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001155 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001156 * This routine allows the kernel scheduler to make @a thread the current
1157 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001158 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001159 * If @a thread is not currently suspended, the routine has no effect.
1160 *
1161 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001162 *
1163 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001164 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001165 */
Andrew Boie468190a2017-09-29 14:00:48 -07001166__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001167
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001168/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001169 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001170 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001171 * This routine specifies how the scheduler will perform time slicing of
1172 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001174 * To enable time slicing, @a slice must be non-zero. The scheduler
1175 * ensures that no thread runs for more than the specified time limit
1176 * before other threads of that priority are given a chance to execute.
1177 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001178 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001180 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001181 * execute. Once the scheduler selects a thread for execution, there is no
1182 * minimum guaranteed time the thread will execute before threads of greater or
1183 * equal priority are scheduled.
1184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001185 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001186 * for execution, this routine has no effect; the thread is immediately
1187 * rescheduled after the slice period expires.
1188 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001189 * To disable timeslicing, set both @a slice and @a prio to zero.
1190 *
1191 * @param slice Maximum time slice length (in milliseconds).
1192 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001193 *
1194 * @return N/A
1195 */
Kumar Galacc334c72017-04-21 10:55:34 -05001196extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001197
Anas Nashif166f5192018-02-25 08:02:36 -06001198/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001199
1200/**
1201 * @addtogroup isr_apis
1202 * @{
1203 */
1204
1205/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001206 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001207 *
Allan Stephensc98da842016-11-11 15:45:03 -05001208 * This routine allows the caller to customize its actions, depending on
1209 * whether it is a thread or an ISR.
1210 *
1211 * @note Can be called by ISRs.
1212 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001213 * @return false if invoked by a thread.
1214 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001215 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001216extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001217
Benjamin Walsh445830d2016-11-10 15:54:27 -05001218/**
1219 * @brief Determine if code is running in a preemptible thread.
1220 *
Allan Stephensc98da842016-11-11 15:45:03 -05001221 * This routine allows the caller to customize its actions, depending on
1222 * whether it can be preempted by another thread. The routine returns a 'true'
1223 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001224 *
Allan Stephensc98da842016-11-11 15:45:03 -05001225 * - The code is running in a thread, not at ISR.
1226 * - The thread's priority is in the preemptible range.
1227 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001228 *
Allan Stephensc98da842016-11-11 15:45:03 -05001229 * @note Can be called by ISRs.
1230 *
1231 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001232 * @return Non-zero if invoked by a preemptible thread.
1233 */
Andrew Boie468190a2017-09-29 14:00:48 -07001234__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001235
Allan Stephensc98da842016-11-11 15:45:03 -05001236/**
Anas Nashif166f5192018-02-25 08:02:36 -06001237 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001238 */
1239
1240/**
1241 * @addtogroup thread_apis
1242 * @{
1243 */
1244
1245/**
1246 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001247 *
Allan Stephensc98da842016-11-11 15:45:03 -05001248 * This routine prevents the current thread from being preempted by another
1249 * thread by instructing the scheduler to treat it as a cooperative thread.
1250 * If the thread subsequently performs an operation that makes it unready,
1251 * it will be context switched out in the normal manner. When the thread
1252 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001253 *
Allan Stephensc98da842016-11-11 15:45:03 -05001254 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001255 *
Allan Stephensc98da842016-11-11 15:45:03 -05001256 * @note k_sched_lock() and k_sched_unlock() should normally be used
1257 * when the operation being performed can be safely interrupted by ISRs.
1258 * However, if the amount of processing involved is very small, better
1259 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001260 *
1261 * @return N/A
1262 */
1263extern void k_sched_lock(void);
1264
Allan Stephensc98da842016-11-11 15:45:03 -05001265/**
1266 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001267 *
Allan Stephensc98da842016-11-11 15:45:03 -05001268 * This routine reverses the effect of a previous call to k_sched_lock().
1269 * A thread must call the routine once for each time it called k_sched_lock()
1270 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001271 *
1272 * @return N/A
1273 */
1274extern void k_sched_unlock(void);
1275
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001276/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001277 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001279 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001280 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001281 * Custom data is not used by the kernel itself, and is freely available
1282 * for a thread to use as it sees fit. It can be used as a framework
1283 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001285 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001286 *
1287 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001288 *
1289 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001290 */
Andrew Boie468190a2017-09-29 14:00:48 -07001291__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001292
1293/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001294 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001295 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001296 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001297 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001298 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001299 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001300 */
Andrew Boie468190a2017-09-29 14:00:48 -07001301__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001302
1303/**
Anas Nashif57554052018-03-03 02:31:05 -06001304 * @brief Set current thread name
1305 *
1306 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1307 * tracing and debugging.
1308 *
Andrew Boie38129ce2019-06-25 08:54:37 -07001309 * @param thread_id Thread to set name, or NULL to set the current thread
1310 * @param value Name string
1311 * @retval 0 on success
1312 * @retval -EFAULT Memory access error with supplied string
1313 * @retval -ENOSYS Thread name configuration option not enabled
1314 * @retval -EINVAL Thread name too long
Anas Nashif57554052018-03-03 02:31:05 -06001315 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001316__syscall int k_thread_name_set(k_tid_t thread_id, const char *value);
Anas Nashif57554052018-03-03 02:31:05 -06001317
1318/**
1319 * @brief Get thread name
1320 *
1321 * Get the name of a thread
1322 *
1323 * @param thread_id Thread ID
Andrew Boie38129ce2019-06-25 08:54:37 -07001324 * @retval Thread name, or NULL if configuration not enabled
Anas Nashif57554052018-03-03 02:31:05 -06001325 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001326const char *k_thread_name_get(k_tid_t thread_id);
1327
1328/**
1329 * @brief Copy the thread name into a supplied buffer
1330 *
1331 * @param thread_id Thread to obtain name information
1332 * @param buf Destination buffer
1333 * @param size Destinatiomn buffer size
1334 * @retval -ENOSPC Destination buffer too small
1335 * @retval -EFAULT Memory access error
1336 * @retval -ENOSYS Thread name feature not enabled
1337 * @retval 0 Success
1338 */
1339__syscall int k_thread_name_copy(k_tid_t thread_id, char *buf,
1340 size_t size);
Anas Nashif57554052018-03-03 02:31:05 -06001341
1342/**
Pavlo Hamov8076c802019-07-31 12:43:54 +03001343 * @brief Get thread state string
1344 *
1345 * Get the human friendly thread state string
1346 *
1347 * @param thread_id Thread ID
1348 * @retval Thread state string, empty if no state flag is set
1349 */
1350const char *k_thread_state_str(k_tid_t thread_id);
1351
1352/**
Andy Rosscfe62032018-09-29 07:34:55 -07001353 * @}
1354 */
1355
1356/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001357 * @addtogroup clock_apis
1358 * @{
1359 */
1360
1361/**
1362 * @brief Generate null timeout delay.
1363 *
1364 * This macro generates a timeout delay that that instructs a kernel API
1365 * not to wait if the requested operation cannot be performed immediately.
1366 *
1367 * @return Timeout delay value.
1368 */
1369#define K_NO_WAIT 0
1370
1371/**
1372 * @brief Generate timeout delay from milliseconds.
1373 *
1374 * This macro generates a timeout delay that that instructs a kernel API
1375 * to wait up to @a ms milliseconds to perform the requested operation.
1376 *
1377 * @param ms Duration in milliseconds.
1378 *
1379 * @return Timeout delay value.
1380 */
Johan Hedberg14471692016-11-13 10:52:15 +02001381#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001382
1383/**
1384 * @brief Generate timeout delay from seconds.
1385 *
1386 * This macro generates a timeout delay that that instructs a kernel API
1387 * to wait up to @a s seconds to perform the requested operation.
1388 *
1389 * @param s Duration in seconds.
1390 *
1391 * @return Timeout delay value.
1392 */
Johan Hedberg14471692016-11-13 10:52:15 +02001393#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001394
1395/**
1396 * @brief Generate timeout delay from minutes.
1397 *
1398 * This macro generates a timeout delay that that instructs a kernel API
1399 * to wait up to @a m minutes to perform the requested operation.
1400 *
1401 * @param m Duration in minutes.
1402 *
1403 * @return Timeout delay value.
1404 */
Johan Hedberg14471692016-11-13 10:52:15 +02001405#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001406
1407/**
1408 * @brief Generate timeout delay from hours.
1409 *
1410 * This macro generates a timeout delay that that instructs a kernel API
1411 * to wait up to @a h hours to perform the requested operation.
1412 *
1413 * @param h Duration in hours.
1414 *
1415 * @return Timeout delay value.
1416 */
Johan Hedberg14471692016-11-13 10:52:15 +02001417#define K_HOURS(h) K_MINUTES((h) * 60)
1418
Allan Stephensc98da842016-11-11 15:45:03 -05001419/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001420 * @brief Generate infinite timeout delay.
1421 *
1422 * This macro generates a timeout delay that that instructs a kernel API
1423 * to wait as long as necessary to perform the requested operation.
1424 *
1425 * @return Timeout delay value.
1426 */
1427#define K_FOREVER (-1)
1428
1429/**
Anas Nashif166f5192018-02-25 08:02:36 -06001430 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001431 */
1432
1433/**
Allan Stephensc98da842016-11-11 15:45:03 -05001434 * @cond INTERNAL_HIDDEN
1435 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001436
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001437struct k_timer {
1438 /*
1439 * _timeout structure must be first here if we want to use
1440 * dynamic timer allocation. timeout.node is used in the double-linked
1441 * list of free timers
1442 */
1443 struct _timeout timeout;
1444
Allan Stephens45bfa372016-10-12 12:39:42 -05001445 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001446 _wait_q_t wait_q;
1447
1448 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001449 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001450
1451 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001452 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001453
1454 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001455 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001456
Allan Stephens45bfa372016-10-12 12:39:42 -05001457 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001458 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001459
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001460 /* user-specific data, also used to support legacy features */
1461 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001462
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001463 _OBJECT_TRACING_NEXT_PTR(k_timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001464};
1465
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001466#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001467 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001468 .timeout = { \
1469 .node = {},\
1470 .dticks = 0, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001471 .fn = z_timer_expiration_handler \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001472 }, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001473 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001474 .expiry_fn = expiry, \
1475 .stop_fn = stop, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001476 .period = 0, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001477 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001478 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001479 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001480 }
1481
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001482#define K_TIMER_INITIALIZER DEPRECATED_MACRO Z_TIMER_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001483
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001484/**
Allan Stephensc98da842016-11-11 15:45:03 -05001485 * INTERNAL_HIDDEN @endcond
1486 */
1487
1488/**
1489 * @defgroup timer_apis Timer APIs
1490 * @ingroup kernel_apis
1491 * @{
1492 */
1493
1494/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001495 * @typedef k_timer_expiry_t
1496 * @brief Timer expiry function type.
1497 *
1498 * A timer's expiry function is executed by the system clock interrupt handler
1499 * each time the timer expires. The expiry function is optional, and is only
1500 * invoked if the timer has been initialized with one.
1501 *
1502 * @param timer Address of timer.
1503 *
1504 * @return N/A
1505 */
1506typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1507
1508/**
1509 * @typedef k_timer_stop_t
1510 * @brief Timer stop function type.
1511 *
1512 * A timer's stop function is executed if the timer is stopped prematurely.
1513 * The function runs in the context of the thread that stops the timer.
1514 * The stop function is optional, and is only invoked if the timer has been
1515 * initialized with one.
1516 *
1517 * @param timer Address of timer.
1518 *
1519 * @return N/A
1520 */
1521typedef void (*k_timer_stop_t)(struct k_timer *timer);
1522
1523/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001524 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001526 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001527 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001528 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001529 *
1530 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001531 * @param expiry_fn Function to invoke each time the timer expires.
1532 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001533 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001534#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001535 Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001536 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001537
Allan Stephens45bfa372016-10-12 12:39:42 -05001538/**
1539 * @brief Initialize a timer.
1540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001541 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001542 *
1543 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001544 * @param expiry_fn Function to invoke each time the timer expires.
1545 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001546 *
1547 * @return N/A
1548 */
1549extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001550 k_timer_expiry_t expiry_fn,
1551 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001552
Allan Stephens45bfa372016-10-12 12:39:42 -05001553/**
1554 * @brief Start a timer.
1555 *
1556 * This routine starts a timer, and resets its status to zero. The timer
1557 * begins counting down using the specified duration and period values.
1558 *
1559 * Attempting to start a timer that is already running is permitted.
1560 * The timer's status is reset to zero and the timer begins counting down
1561 * using the new duration and period values.
1562 *
1563 * @param timer Address of timer.
1564 * @param duration Initial timer duration (in milliseconds).
1565 * @param period Timer period (in milliseconds).
1566 *
1567 * @return N/A
1568 */
Andrew Boiea354d492017-09-29 16:22:28 -07001569__syscall void k_timer_start(struct k_timer *timer,
1570 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001571
1572/**
1573 * @brief Stop a timer.
1574 *
1575 * This routine stops a running timer prematurely. The timer's stop function,
1576 * if one exists, is invoked by the caller.
1577 *
1578 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001579 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001580 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001581 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1582 * if @a k_timer_stop is to be called from ISRs.
1583 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001584 * @param timer Address of timer.
1585 *
1586 * @return N/A
1587 */
Andrew Boiea354d492017-09-29 16:22:28 -07001588__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001589
1590/**
1591 * @brief Read timer status.
1592 *
1593 * This routine reads the timer's status, which indicates the number of times
1594 * it has expired since its status was last read.
1595 *
1596 * Calling this routine resets the timer's status to zero.
1597 *
1598 * @param timer Address of timer.
1599 *
1600 * @return Timer status.
1601 */
Andrew Boiea354d492017-09-29 16:22:28 -07001602__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001603
1604/**
1605 * @brief Synchronize thread to timer expiration.
1606 *
1607 * This routine blocks the calling thread until the timer's status is non-zero
1608 * (indicating that it has expired at least once since it was last examined)
1609 * or the timer is stopped. If the timer status is already non-zero,
1610 * or the timer is already stopped, the caller continues without waiting.
1611 *
1612 * Calling this routine resets the timer's status to zero.
1613 *
1614 * This routine must not be used by interrupt handlers, since they are not
1615 * allowed to block.
1616 *
1617 * @param timer Address of timer.
1618 *
1619 * @return Timer status.
1620 */
Andrew Boiea354d492017-09-29 16:22:28 -07001621__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001622
Andy Ross52e444b2018-09-28 09:06:37 -07001623extern s32_t z_timeout_remaining(struct _timeout *timeout);
1624
Allan Stephens45bfa372016-10-12 12:39:42 -05001625/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001626 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001627 *
1628 * This routine computes the (approximate) time remaining before a running
1629 * timer next expires. If the timer is not running, it returns zero.
1630 *
1631 * @param timer Address of timer.
1632 *
1633 * @return Remaining time (in milliseconds).
1634 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001635__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001636
Patrik Flykt4344e272019-03-08 14:19:05 -07001637static inline u32_t z_impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001638{
Charles E. Youse0ad40222019-03-01 10:51:04 -08001639 const s32_t ticks = z_timeout_remaining(&timer->timeout);
1640 return (ticks > 0) ? (u32_t)__ticks_to_ms(ticks) : 0U;
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001641}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001642
Allan Stephensc98da842016-11-11 15:45:03 -05001643/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001644 * @brief Associate user-specific data with a timer.
1645 *
1646 * This routine records the @a user_data with the @a timer, to be retrieved
1647 * later.
1648 *
1649 * It can be used e.g. in a timer handler shared across multiple subsystems to
1650 * retrieve data specific to the subsystem this timer is associated with.
1651 *
1652 * @param timer Address of timer.
1653 * @param user_data User data to associate with the timer.
1654 *
1655 * @return N/A
1656 */
Andrew Boiea354d492017-09-29 16:22:28 -07001657__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1658
Anas Nashif954d5502018-02-25 08:37:28 -06001659/**
1660 * @internal
1661 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001662static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
Andrew Boiea354d492017-09-29 16:22:28 -07001663 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001664{
1665 timer->user_data = user_data;
1666}
1667
1668/**
1669 * @brief Retrieve the user-specific data from a timer.
1670 *
1671 * @param timer Address of timer.
1672 *
1673 * @return The user data.
1674 */
Andrew Boiea354d492017-09-29 16:22:28 -07001675__syscall void *k_timer_user_data_get(struct k_timer *timer);
1676
Patrik Flykt4344e272019-03-08 14:19:05 -07001677static inline void *z_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001678{
1679 return timer->user_data;
1680}
1681
Anas Nashif166f5192018-02-25 08:02:36 -06001682/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001683
Allan Stephensc98da842016-11-11 15:45:03 -05001684/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001685 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001686 * @{
1687 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001688
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001689/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001690 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001691 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001692 * This routine returns the elapsed time since the system booted,
1693 * in milliseconds.
1694 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001695 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001696 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001697 * While this function returns time in milliseconds, it does
1698 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001699 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001700 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001701 *
1702 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001703 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001704__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001705
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001706/**
1707 * @brief Enable clock always on in tickless kernel
1708 *
Andy Ross1db9f182019-06-25 10:09:45 -07001709 * Deprecated. This does nothing (it was always just a hint). This
1710 * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE
1711 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001712 *
1713 * @retval prev_status Previous status of always on flag
1714 */
Andy Ross1db9f182019-06-25 10:09:45 -07001715/* LCOV_EXCL_START */
1716__deprecated static inline int k_enable_sys_clock_always_on(void)
1717{
1718 __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1719 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1720
1721 return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE);
1722}
1723/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001724
1725/**
1726 * @brief Disable clock always on in tickless kernel
1727 *
Andy Ross1db9f182019-06-25 10:09:45 -07001728 * Deprecated. This does nothing (it was always just a hint). This
1729 * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE
1730 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001731 */
Andy Ross1db9f182019-06-25 10:09:45 -07001732/* LCOV_EXCL_START */
1733__deprecated static inline void k_disable_sys_clock_always_on(void)
1734{
1735 __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1736 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1737}
1738/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001739
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001740/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001741 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001742 *
Peter Bigota6067a32019-08-28 08:19:26 -05001743 * This routine returns the lower 32 bits of the system uptime in
1744 * milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001745 *
Peter Bigota6067a32019-08-28 08:19:26 -05001746 * Because correct conversion requires full precision of the system
1747 * clock there is no benefit to using this over k_uptime_get() unless
1748 * you know the application will never run long enough for the system
1749 * clock to approach 2^32 ticks. Calls to this function may involve
1750 * interrupt blocking and 64-bit math.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001751 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001752 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001753 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001754 * While this function returns time in milliseconds, it does
1755 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001756 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option
David B. Kinder8de9cc72019-06-25 10:44:55 -07001757 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001758 *
Peter Bigota6067a32019-08-28 08:19:26 -05001759 * @return The low 32 bits of the current uptime, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001760 */
Peter Bigota6067a32019-08-28 08:19:26 -05001761static inline u32_t k_uptime_get_32(void)
1762{
1763 return (u32_t)k_uptime_get();
1764}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001765
1766/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001767 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001768 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001769 * This routine computes the elapsed time between the current system uptime
1770 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001771 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001772 * @param reftime Pointer to a reference time, which is updated to the current
1773 * uptime upon return.
1774 *
1775 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001776 */
Andy Ross987c0e52018-09-27 16:50:00 -07001777static inline s64_t k_uptime_delta(s64_t *reftime)
1778{
1779 s64_t uptime, delta;
1780
1781 uptime = k_uptime_get();
1782 delta = uptime - *reftime;
1783 *reftime = uptime;
1784
1785 return delta;
1786}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001787
1788/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001789 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001791 * This routine computes the elapsed time between the current system uptime
1792 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001793 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001794 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1795 * need for interrupt locking and 64-bit math. However, the 32-bit result
1796 * cannot hold an elapsed time larger than approximately 50 days, so the
1797 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001799 * @param reftime Pointer to a reference time, which is updated to the current
1800 * uptime upon return.
1801 *
1802 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001803 */
Andy Ross987c0e52018-09-27 16:50:00 -07001804static inline u32_t k_uptime_delta_32(s64_t *reftime)
1805{
1806 return (u32_t)k_uptime_delta(reftime);
1807}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001808
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001809/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001810 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001812 * This routine returns the current time, as measured by the system's hardware
1813 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001815 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001816 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001817#define k_cycle_get_32() z_arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001818
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001819/**
Anas Nashif166f5192018-02-25 08:02:36 -06001820 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001821 */
1822
Allan Stephensc98da842016-11-11 15:45:03 -05001823/**
1824 * @cond INTERNAL_HIDDEN
1825 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001826
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001827struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001828 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001829 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001830 union {
1831 _wait_q_t wait_q;
1832
1833 _POLL_EVENT;
1834 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001835
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001836 _OBJECT_TRACING_NEXT_PTR(k_queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001837};
1838
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001839#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001840 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001841 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Stephanos Ioannidisf628dcd2019-09-11 18:09:49 +09001842 .lock = { }, \
1843 { \
1844 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
1845 _POLL_EVENT_OBJ_INIT(obj) \
1846 }, \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001847 _OBJECT_TRACING_INIT \
1848 }
1849
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001850#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1851
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001852extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1853
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001854/**
1855 * INTERNAL_HIDDEN @endcond
1856 */
1857
1858/**
1859 * @defgroup queue_apis Queue APIs
1860 * @ingroup kernel_apis
1861 * @{
1862 */
1863
1864/**
1865 * @brief Initialize a queue.
1866 *
1867 * This routine initializes a queue object, prior to its first use.
1868 *
1869 * @param queue Address of the queue.
1870 *
1871 * @return N/A
1872 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001873__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001874
1875/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001876 * @brief Cancel waiting on a queue.
1877 *
1878 * This routine causes first thread pending on @a queue, if any, to
1879 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001880 * If the queue is being waited on by k_poll(), it will return with
1881 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1882 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001883 *
1884 * @note Can be called by ISRs.
1885 *
1886 * @param queue Address of the queue.
1887 *
1888 * @return N/A
1889 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001890__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001891
1892/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001893 * @brief Append an element to the end of a queue.
1894 *
1895 * This routine appends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001896 * aligned on a word boundary, and the first word of the item is reserved
1897 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001898 *
1899 * @note Can be called by ISRs.
1900 *
1901 * @param queue Address of the queue.
1902 * @param data Address of the data item.
1903 *
1904 * @return N/A
1905 */
1906extern void k_queue_append(struct k_queue *queue, void *data);
1907
1908/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001909 * @brief Append an element to a queue.
1910 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001911 * This routine appends a data item to @a queue. There is an implicit memory
1912 * allocation to create an additional temporary bookkeeping data structure from
1913 * the calling thread's resource pool, which is automatically freed when the
1914 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001915 *
1916 * @note Can be called by ISRs.
1917 *
1918 * @param queue Address of the queue.
1919 * @param data Address of the data item.
1920 *
1921 * @retval 0 on success
1922 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1923 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301924__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001925
1926/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001927 * @brief Prepend an element to a queue.
1928 *
1929 * This routine prepends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001930 * aligned on a word boundary, and the first word of the item is reserved
1931 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001932 *
1933 * @note Can be called by ISRs.
1934 *
1935 * @param queue Address of the queue.
1936 * @param data Address of the data item.
1937 *
1938 * @return N/A
1939 */
1940extern void k_queue_prepend(struct k_queue *queue, void *data);
1941
1942/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001943 * @brief Prepend an element to a queue.
1944 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001945 * This routine prepends a data item to @a queue. There is an implicit memory
1946 * allocation to create an additional temporary bookkeeping data structure from
1947 * the calling thread's resource pool, which is automatically freed when the
1948 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001949 *
1950 * @note Can be called by ISRs.
1951 *
1952 * @param queue Address of the queue.
1953 * @param data Address of the data item.
1954 *
1955 * @retval 0 on success
1956 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1957 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301958__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001959
1960/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001961 * @brief Inserts an element to a queue.
1962 *
1963 * This routine inserts a data item to @a queue after previous item. A queue
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001964 * data item must be aligned on a word boundary, and the first word of
1965 * the item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001966 *
1967 * @note Can be called by ISRs.
1968 *
1969 * @param queue Address of the queue.
1970 * @param prev Address of the previous data item.
1971 * @param data Address of the data item.
1972 *
1973 * @return N/A
1974 */
1975extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1976
1977/**
1978 * @brief Atomically append a list of elements to a queue.
1979 *
1980 * This routine adds a list of data items to @a queue in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001981 * The data items must be in a singly-linked list, with the first word
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001982 * in each data item pointing to the next data item; the list must be
1983 * NULL-terminated.
1984 *
1985 * @note Can be called by ISRs.
1986 *
1987 * @param queue Address of the queue.
1988 * @param head Pointer to first node in singly-linked list.
1989 * @param tail Pointer to last node in singly-linked list.
1990 *
1991 * @return N/A
1992 */
1993extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1994
1995/**
1996 * @brief Atomically add a list of elements to a queue.
1997 *
1998 * This routine adds a list of data items to @a queue in one operation.
1999 * The data items must be in a singly-linked list implemented using a
2000 * sys_slist_t object. Upon completion, the original list is empty.
2001 *
2002 * @note Can be called by ISRs.
2003 *
2004 * @param queue Address of the queue.
2005 * @param list Pointer to sys_slist_t object.
2006 *
2007 * @return N/A
2008 */
2009extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
2010
2011/**
2012 * @brief Get an element from a queue.
2013 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002014 * This routine removes first data item from @a queue. The first word of the
2015 * data item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002016 *
2017 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2018 *
2019 * @param queue Address of the queue.
2020 * @param timeout Waiting period to obtain a data item (in milliseconds),
2021 * or one of the special values K_NO_WAIT and K_FOREVER.
2022 *
2023 * @return Address of the data item if successful; NULL if returned
2024 * without waiting, or waiting period timed out.
2025 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002026__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002027
2028/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002029 * @brief Remove an element from a queue.
2030 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002031 * This routine removes data item from @a queue. The first word of the
2032 * data item is reserved for the kernel's use. Removing elements from k_queue
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002033 * rely on sys_slist_find_and_remove which is not a constant time operation.
2034 *
2035 * @note Can be called by ISRs
2036 *
2037 * @param queue Address of the queue.
2038 * @param data Address of the data item.
2039 *
2040 * @return true if data item was removed
2041 */
2042static inline bool k_queue_remove(struct k_queue *queue, void *data)
2043{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002044 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002045}
2046
2047/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002048 * @brief Append an element to a queue only if it's not present already.
2049 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002050 * This routine appends data item to @a queue. The first word of the data
2051 * item is reserved for the kernel's use. Appending elements to k_queue
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002052 * relies on sys_slist_is_node_in_list which is not a constant time operation.
2053 *
2054 * @note Can be called by ISRs
2055 *
2056 * @param queue Address of the queue.
2057 * @param data Address of the data item.
2058 *
2059 * @return true if data item was added, false if not
2060 */
2061static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
2062{
2063 sys_sfnode_t *test;
2064
2065 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
2066 if (test == (sys_sfnode_t *) data) {
2067 return false;
2068 }
2069 }
2070
2071 k_queue_append(queue, data);
2072 return true;
2073}
2074
2075/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002076 * @brief Query a queue to see if it has data available.
2077 *
2078 * Note that the data might be already gone by the time this function returns
2079 * if other threads are also trying to read from the queue.
2080 *
2081 * @note Can be called by ISRs.
2082 *
2083 * @param queue Address of the queue.
2084 *
2085 * @return Non-zero if the queue is empty.
2086 * @return 0 if data is available.
2087 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002088__syscall int k_queue_is_empty(struct k_queue *queue);
2089
Patrik Flykt4344e272019-03-08 14:19:05 -07002090static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002091{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002092 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002093}
2094
2095/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002096 * @brief Peek element at the head of queue.
2097 *
2098 * Return element from the head of queue without removing it.
2099 *
2100 * @param queue Address of the queue.
2101 *
2102 * @return Head element, or NULL if queue is empty.
2103 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002104__syscall void *k_queue_peek_head(struct k_queue *queue);
2105
Patrik Flykt4344e272019-03-08 14:19:05 -07002106static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002107{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002108 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002109}
2110
2111/**
2112 * @brief Peek element at the tail of queue.
2113 *
2114 * Return element from the tail of queue without removing it.
2115 *
2116 * @param queue Address of the queue.
2117 *
2118 * @return Tail element, or NULL if queue is empty.
2119 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002120__syscall void *k_queue_peek_tail(struct k_queue *queue);
2121
Patrik Flykt4344e272019-03-08 14:19:05 -07002122static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002123{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002124 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002125}
2126
2127/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002128 * @brief Statically define and initialize a queue.
2129 *
2130 * The queue can be accessed outside the module where it is defined using:
2131 *
2132 * @code extern struct k_queue <name>; @endcode
2133 *
2134 * @param name Name of the queue.
2135 */
2136#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002137 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002138 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002139
Anas Nashif166f5192018-02-25 08:02:36 -06002140/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002141
Wentong Wu5611e922019-06-20 23:51:27 +08002142#ifdef CONFIG_USERSPACE
2143/**
2144 * @brief futex structure
2145 *
2146 * A k_futex is a lightweight mutual exclusion primitive designed
2147 * to minimize kernel involvement. Uncontended operation relies
2148 * only on atomic access to shared memory. k_futex are tracked as
2149 * kernel objects and can live in user memory so any access bypass
2150 * the kernel object permission management mechanism.
2151 */
2152struct k_futex {
2153 atomic_t val;
2154};
2155
2156/**
2157 * @brief futex kernel data structure
2158 *
2159 * z_futex_data are the helper data structure for k_futex to complete
2160 * futex contended operation on kernel side, structure z_futex_data
2161 * of every futex object is invisible in user mode.
2162 */
2163struct z_futex_data {
2164 _wait_q_t wait_q;
2165 struct k_spinlock lock;
2166};
2167
2168#define Z_FUTEX_DATA_INITIALIZER(obj) \
2169 { \
2170 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \
2171 }
2172
2173/**
2174 * @defgroup futex_apis FUTEX APIs
2175 * @ingroup kernel_apis
2176 * @{
2177 */
2178
2179/**
Wentong Wu5611e922019-06-20 23:51:27 +08002180 * @brief Pend the current thread on a futex
2181 *
2182 * Tests that the supplied futex contains the expected value, and if so,
2183 * goes to sleep until some other thread calls k_futex_wake() on it.
2184 *
2185 * @param futex Address of the futex.
2186 * @param expected Expected value of the futex, if it is different the caller
2187 * will not wait on it.
2188 * @param timeout Waiting period on the futex, in milliseconds, or one of the
2189 * special values K_NO_WAIT or K_FOREVER.
2190 * @retval -EACCES Caller does not have read access to futex address.
2191 * @retval -EAGAIN If the futex value did not match the expected parameter.
2192 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2193 * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup.
2194 * @retval 0 if the caller went to sleep and was woken up. The caller
2195 * should check the futex's value on wakeup to determine if it needs
2196 * to block again.
2197 */
2198__syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout);
2199
2200/**
2201 * @brief Wake one/all threads pending on a futex
2202 *
2203 * Wake up the highest priority thread pending on the supplied futex, or
2204 * wakeup all the threads pending on the supplied futex, and the behavior
2205 * depends on wake_all.
2206 *
2207 * @param futex Futex to wake up pending threads.
2208 * @param wake_all If true, wake up all pending threads; If false,
2209 * wakeup the highest priority thread.
2210 * @retval -EACCES Caller does not have access to the futex address.
2211 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2212 * @retval Number of threads that were woken up.
2213 */
2214__syscall int k_futex_wake(struct k_futex *futex, bool wake_all);
2215
2216/** @} */
2217#endif
2218
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002219struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002220 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002221};
2222
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002223/**
2224 * @cond INTERNAL_HIDDEN
2225 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002226#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002227 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002228 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002229 }
2230
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002231#define K_FIFO_INITIALIZER DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002232
Allan Stephensc98da842016-11-11 15:45:03 -05002233/**
2234 * INTERNAL_HIDDEN @endcond
2235 */
2236
2237/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002238 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002239 * @ingroup kernel_apis
2240 * @{
2241 */
2242
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002243/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002244 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002245 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002246 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002247 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002248 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002249 *
2250 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002251 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002252 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002253#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002254 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002255
2256/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002257 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002258 *
2259 * This routine causes first thread pending on @a fifo, if any, to
2260 * return from k_fifo_get() call with NULL value (as if timeout
2261 * expired).
2262 *
2263 * @note Can be called by ISRs.
2264 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002265 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002266 *
2267 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002268 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002269 */
2270#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002271 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002272
2273/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002274 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002275 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002276 * This routine adds a data item to @a fifo. A FIFO data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002277 * aligned on a word boundary, and the first word of the item is reserved
2278 * for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002279 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002280 * @note Can be called by ISRs.
2281 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002282 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002283 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002284 *
2285 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002286 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002287 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002288#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002289 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002290
2291/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002292 * @brief Add an element to a FIFO queue.
2293 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002294 * This routine adds a data item to @a fifo. There is an implicit memory
2295 * allocation to create an additional temporary bookkeeping data structure from
2296 * the calling thread's resource pool, which is automatically freed when the
2297 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002298 *
2299 * @note Can be called by ISRs.
2300 *
2301 * @param fifo Address of the FIFO.
2302 * @param data Address of the data item.
2303 *
2304 * @retval 0 on success
2305 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002306 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002307 */
2308#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002309 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002310
2311/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002312 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002313 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002314 * This routine adds a list of data items to @a fifo in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002315 * The data items must be in a singly-linked list, with the first word of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002316 * each data item pointing to the next data item; the list must be
2317 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002318 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002319 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002320 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002321 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002322 * @param head Pointer to first node in singly-linked list.
2323 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002324 *
2325 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002326 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002327 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002328#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002329 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002330
2331/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002332 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002333 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002334 * This routine adds a list of data items to @a fifo in one operation.
2335 * The data items must be in a singly-linked list implemented using a
2336 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002337 * and must be re-initialized via sys_slist_init().
2338 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002339 * @note Can be called by ISRs.
2340 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002341 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002342 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002343 *
2344 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002345 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002346 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002347#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002348 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002349
2350/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002351 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002353 * This routine removes a data item from @a fifo in a "first in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002354 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002355 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002356 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2357 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002358 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002359 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002360 * or one of the special values K_NO_WAIT and K_FOREVER.
2361 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002362 * @return Address of the data item if successful; NULL if returned
2363 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002364 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002365 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002366#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002367 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002368
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002369/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002370 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002371 *
2372 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002373 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002374 *
2375 * @note Can be called by ISRs.
2376 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002377 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002378 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002379 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002380 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002381 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002382 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002383#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002384 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002385
2386/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002387 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002388 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002389 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302390 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002391 * on each iteration of processing, a head container will be peeked,
2392 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002393 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002394 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002395 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002396 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002397 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002398 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002399 */
2400#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002401 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002402
2403/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002404 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002405 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002406 * Return element from the tail of FIFO queue (without removing it). A usecase
2407 * for this is if elements of the FIFO queue are themselves containers. Then
2408 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002409 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002410 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002411 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002412 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002413 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002414 */
2415#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002416 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002417
2418/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002419 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002420 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002421 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002422 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002423 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002424 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002425 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002426 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002427 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002428#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002429 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002430 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002431
Anas Nashif166f5192018-02-25 08:02:36 -06002432/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002433
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002434struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002435 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002436};
2437
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002438/**
2439 * @cond INTERNAL_HIDDEN
2440 */
2441
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002442#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002443 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002444 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002445 }
2446
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002447#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2448
Allan Stephensc98da842016-11-11 15:45:03 -05002449/**
2450 * INTERNAL_HIDDEN @endcond
2451 */
2452
2453/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002454 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002455 * @ingroup kernel_apis
2456 * @{
2457 */
2458
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002459/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002460 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002461 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002462 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002463 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002464 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002465 *
2466 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002467 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002468 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002469#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002470 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002471
2472/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002473 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002474 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002475 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002476 * aligned on a word boundary, and the first word of the item is
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002477 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002479 * @note Can be called by ISRs.
2480 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002481 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002482 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002483 *
2484 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002485 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002486 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002487#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002488 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002489
2490/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002491 * @brief Add an element to a LIFO queue.
2492 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002493 * This routine adds a data item to @a lifo. There is an implicit memory
2494 * allocation to create an additional temporary bookkeeping data structure from
2495 * the calling thread's resource pool, which is automatically freed when the
2496 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002497 *
2498 * @note Can be called by ISRs.
2499 *
2500 * @param lifo Address of the LIFO.
2501 * @param data Address of the data item.
2502 *
2503 * @retval 0 on success
2504 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002505 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002506 */
2507#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002508 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002509
2510/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002511 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002512 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002513 * This routine removes a data item from @a lifo in a "last in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002514 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002515 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002516 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2517 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002518 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002519 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002520 * or one of the special values K_NO_WAIT and K_FOREVER.
2521 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002522 * @return Address of the data item if successful; NULL if returned
2523 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002524 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002525 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002526#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002527 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002528
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002529/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002530 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002531 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002532 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002533 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002534 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002536 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002537 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002538 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002539#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002540 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002541 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002542
Anas Nashif166f5192018-02-25 08:02:36 -06002543/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002544
2545/**
2546 * @cond INTERNAL_HIDDEN
2547 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302548#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002549
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002550typedef uintptr_t stack_data_t;
2551
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002552struct k_stack {
2553 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002554 struct k_spinlock lock;
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002555 stack_data_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002557 _OBJECT_TRACING_NEXT_PTR(k_stack)
Andrew Boief3bee952018-05-02 17:44:39 -07002558 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002559};
2560
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002561#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002562 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002563 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002564 .base = stack_buffer, \
2565 .next = stack_buffer, \
2566 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002567 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002568 }
2569
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002570#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2571
Allan Stephensc98da842016-11-11 15:45:03 -05002572/**
2573 * INTERNAL_HIDDEN @endcond
2574 */
2575
2576/**
2577 * @defgroup stack_apis Stack APIs
2578 * @ingroup kernel_apis
2579 * @{
2580 */
2581
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002582/**
2583 * @brief Initialize a stack.
2584 *
2585 * This routine initializes a stack object, prior to its first use.
2586 *
2587 * @param stack Address of the stack.
2588 * @param buffer Address of array used to hold stacked values.
2589 * @param num_entries Maximum number of values that can be stacked.
2590 *
2591 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002592 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002593 */
Andrew Boief3bee952018-05-02 17:44:39 -07002594void k_stack_init(struct k_stack *stack,
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002595 stack_data_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002596
2597
2598/**
2599 * @brief Initialize a stack.
2600 *
2601 * This routine initializes a stack object, prior to its first use. Internal
2602 * buffers will be allocated from the calling thread's resource pool.
2603 * This memory will be released if k_stack_cleanup() is called, or
2604 * userspace is enabled and the stack object loses all references to it.
2605 *
2606 * @param stack Address of the stack.
2607 * @param num_entries Maximum number of values that can be stacked.
2608 *
2609 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002610 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002611 */
2612
Adithya Baglody28080d32018-10-15 11:48:51 +05302613__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2614 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002615
2616/**
2617 * @brief Release a stack's allocated buffer
2618 *
2619 * If a stack object was given a dynamically allocated buffer via
2620 * k_stack_alloc_init(), this will free it. This function does nothing
2621 * if the buffer wasn't dynamically allocated.
2622 *
2623 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002624 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002625 */
2626void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002627
2628/**
2629 * @brief Push an element onto a stack.
2630 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002631 * This routine adds a stack_data_t value @a data to @a stack.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002632 *
2633 * @note Can be called by ISRs.
2634 *
2635 * @param stack Address of the stack.
2636 * @param data Value to push onto the stack.
2637 *
2638 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002639 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002640 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002641__syscall void k_stack_push(struct k_stack *stack, stack_data_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002642
2643/**
2644 * @brief Pop an element from a stack.
2645 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002646 * This routine removes a stack_data_t value from @a stack in a "last in,
2647 * first out" manner and stores the value in @a data.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002648 *
2649 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2650 *
2651 * @param stack Address of the stack.
2652 * @param data Address of area to hold the value popped from the stack.
2653 * @param timeout Waiting period to obtain a value (in milliseconds),
2654 * or one of the special values K_NO_WAIT and K_FOREVER.
2655 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002656 * @retval 0 Element popped from stack.
2657 * @retval -EBUSY Returned without waiting.
2658 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002659 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002660 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002661__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002662
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002663/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002664 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002665 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002666 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002667 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002668 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002670 * @param name Name of the stack.
2671 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002672 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002673 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002674#define K_STACK_DEFINE(name, stack_num_entries) \
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002675 stack_data_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002676 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002677 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002678 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002679 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002680
Anas Nashif166f5192018-02-25 08:02:36 -06002681/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002682
Allan Stephens6bba9b02016-11-16 14:56:54 -05002683struct k_work;
2684
Allan Stephensc98da842016-11-11 15:45:03 -05002685/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002686 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002687 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002688 */
2689
Allan Stephens6bba9b02016-11-16 14:56:54 -05002690/**
2691 * @typedef k_work_handler_t
2692 * @brief Work item handler function type.
2693 *
2694 * A work item's handler function is executed by a workqueue's thread
2695 * when the work item is processed by the workqueue.
2696 *
2697 * @param work Address of the work item.
2698 *
2699 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002700 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002701 */
2702typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002703
2704/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002705 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002706 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002707
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002708struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002709 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002710 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002711};
2712
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002713enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002714 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002715};
2716
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002717struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002718 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002719 k_work_handler_t handler;
2720 atomic_t flags[1];
2721};
2722
Allan Stephens6bba9b02016-11-16 14:56:54 -05002723struct k_delayed_work {
2724 struct k_work work;
2725 struct _timeout timeout;
2726 struct k_work_q *work_q;
2727};
2728
2729extern struct k_work_q k_sys_work_q;
2730
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002731/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002732 * INTERNAL_HIDDEN @endcond
2733 */
2734
Patrik Flykt4344e272019-03-08 14:19:05 -07002735#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002736 { \
2737 ._reserved = NULL, \
2738 .handler = work_handler, \
2739 .flags = { 0 } \
2740 }
2741
Patrik Flykt4344e272019-03-08 14:19:05 -07002742#define K_WORK_INITIALIZER DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002743
Allan Stephens6bba9b02016-11-16 14:56:54 -05002744/**
2745 * @brief Initialize a statically-defined work item.
2746 *
2747 * This macro can be used to initialize a statically-defined workqueue work
2748 * item, prior to its first use. For example,
2749 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002750 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002751 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002752 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002753 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002754 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002755 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002756#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002757 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002758
2759/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002760 * @brief Initialize a work item.
2761 *
2762 * This routine initializes a workqueue work item, prior to its first use.
2763 *
2764 * @param work Address of work item.
2765 * @param handler Function to invoke each time work item is processed.
2766 *
2767 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002768 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002769 */
2770static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2771{
Patrik Flykt4344e272019-03-08 14:19:05 -07002772 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002773}
2774
2775/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002776 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002777 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002778 * This routine submits work item @a work to be processed by workqueue
2779 * @a work_q. If the work item is already pending in the workqueue's queue
2780 * as a result of an earlier submission, this routine has no effect on the
2781 * work item. If the work item has already been processed, or is currently
2782 * being processed, its work is considered complete and the work item can be
2783 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002784 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002785 * @warning
2786 * A submitted work item must not be modified until it has been processed
2787 * by the workqueue.
2788 *
2789 * @note Can be called by ISRs.
2790 *
2791 * @param work_q Address of workqueue.
2792 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002793 *
2794 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002795 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002796 */
2797static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2798 struct k_work *work)
2799{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002800 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002801 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002802 }
2803}
2804
2805/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002806 * @brief Submit a work item to a user mode workqueue
2807 *
David B. Kinder06d78352018-12-17 14:32:40 -08002808 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002809 * memory allocation is made from the caller's resource pool which is freed
2810 * once the worker thread consumes the k_work item. The workqueue
2811 * thread must have memory access to the k_work item being submitted. The caller
2812 * must have permission granted on the work_q parameter's queue object.
2813 *
2814 * Otherwise this works the same as k_work_submit_to_queue().
2815 *
2816 * @note Can be called by ISRs.
2817 *
2818 * @param work_q Address of workqueue.
2819 * @param work Address of work item.
2820 *
2821 * @retval -EBUSY if the work item was already in some workqueue
2822 * @retval -ENOMEM if no memory for thread resource pool allocation
2823 * @retval 0 Success
2824 * @req K-WORK-001
2825 */
2826static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2827 struct k_work *work)
2828{
2829 int ret = -EBUSY;
2830
2831 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2832 ret = k_queue_alloc_append(&work_q->queue, work);
2833
2834 /* Couldn't insert into the queue. Clear the pending bit
2835 * so the work item can be submitted again
2836 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002837 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002838 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2839 }
2840 }
2841
2842 return ret;
2843}
2844
2845/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002846 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002847 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002848 * This routine indicates if work item @a work is pending in a workqueue's
2849 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002850 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002851 * @note Can be called by ISRs.
2852 *
2853 * @param work Address of work item.
2854 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002855 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002856 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002857 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002858static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002859{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002860 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002861}
2862
2863/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002864 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002865 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002866 * This routine starts workqueue @a work_q. The workqueue spawns its work
2867 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002868 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002869 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002870 * @param stack Pointer to work queue thread's stack space, as defined by
2871 * K_THREAD_STACK_DEFINE()
2872 * @param stack_size Size of the work queue thread's stack (in bytes), which
2873 * should either be the same constant passed to
2874 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002875 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002876 *
2877 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002878 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002879 */
Andrew Boie507852a2017-07-25 18:47:07 -07002880extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002881 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002882 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002883
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002884/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002885 * @brief Start a workqueue in user mode
2886 *
2887 * This works identically to k_work_q_start() except it is callable from user
2888 * mode, and the worker thread created will run in user mode.
2889 * The caller must have permissions granted on both the work_q parameter's
2890 * thread and queue objects, and the same restrictions on priority apply as
2891 * k_thread_create().
2892 *
2893 * @param work_q Address of workqueue.
2894 * @param stack Pointer to work queue thread's stack space, as defined by
2895 * K_THREAD_STACK_DEFINE()
2896 * @param stack_size Size of the work queue thread's stack (in bytes), which
2897 * should either be the same constant passed to
2898 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2899 * @param prio Priority of the work queue's thread.
2900 *
2901 * @return N/A
2902 * @req K-WORK-001
2903 */
2904extern void k_work_q_user_start(struct k_work_q *work_q,
2905 k_thread_stack_t *stack,
2906 size_t stack_size, int prio);
2907
2908/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002909 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002910 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002911 * This routine initializes a workqueue delayed work item, prior to
2912 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002913 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002914 * @param work Address of delayed work item.
2915 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002916 *
2917 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002918 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002919 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002920extern void k_delayed_work_init(struct k_delayed_work *work,
2921 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002922
2923/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002924 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002925 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002926 * This routine schedules work item @a work to be processed by workqueue
2927 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002928 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002929 * Only when the countdown completes is the work item actually submitted to
2930 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002931 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002932 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002933 * counting down cancels the existing submission and restarts the
2934 * countdown using the new delay. Note that this behavior is
2935 * inherently subject to race conditions with the pre-existing
2936 * timeouts and work queue, so care must be taken to synchronize such
2937 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002938 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002939 * @warning
2940 * A delayed work item must not be modified until it has been processed
2941 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002942 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002943 * @note Can be called by ISRs.
2944 *
2945 * @param work_q Address of workqueue.
2946 * @param work Address of delayed work item.
2947 * @param delay Delay before submitting the work item (in milliseconds).
2948 *
2949 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002950 * @retval -EINVAL Work item is being processed or has completed its work.
2951 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002952 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002953 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002954extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2955 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002956 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002957
2958/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002959 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002960 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002961 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002962 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002963 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002964 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002965 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002966 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08002967 * @note The result of calling this on a k_delayed_work item that has
2968 * not been submitted (i.e. before the return of the
2969 * k_delayed_work_submit_to_queue() call) is undefined.
2970 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002971 * @param work Address of delayed work item.
2972 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002973 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002974 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002975 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002976 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002977extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002978
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002979/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002980 * @brief Submit a work item to the system workqueue.
2981 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002982 * This routine submits work item @a work to be processed by the system
2983 * workqueue. If the work item is already pending in the workqueue's queue
2984 * as a result of an earlier submission, this routine has no effect on the
2985 * work item. If the work item has already been processed, or is currently
2986 * being processed, its work is considered complete and the work item can be
2987 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002988 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002989 * @warning
2990 * Work items submitted to the system workqueue should avoid using handlers
2991 * that block or yield since this may prevent the system workqueue from
2992 * processing other work items in a timely manner.
2993 *
2994 * @note Can be called by ISRs.
2995 *
2996 * @param work Address of work item.
2997 *
2998 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002999 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003000 */
3001static inline void k_work_submit(struct k_work *work)
3002{
3003 k_work_submit_to_queue(&k_sys_work_q, work);
3004}
3005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003006/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003007 * @brief Submit a delayed work item to the system workqueue.
3008 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003009 * This routine schedules work item @a work to be processed by the system
3010 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003011 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003012 * Only when the countdown completes is the work item actually submitted to
3013 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003014 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003015 * Submitting a previously submitted delayed work item that is still
3016 * counting down cancels the existing submission and restarts the countdown
3017 * using the new delay. If the work item is currently pending on the
3018 * workqueue's queue because the countdown has completed it is too late to
3019 * resubmit the item, and resubmission fails without impacting the work item.
3020 * If the work item has already been processed, or is currently being processed,
3021 * its work is considered complete and the work item can be resubmitted.
3022 *
3023 * @warning
3024 * Work items submitted to the system workqueue should avoid using handlers
3025 * that block or yield since this may prevent the system workqueue from
3026 * processing other work items in a timely manner.
3027 *
3028 * @note Can be called by ISRs.
3029 *
3030 * @param work Address of delayed work item.
3031 * @param delay Delay before submitting the work item (in milliseconds).
3032 *
3033 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003034 * @retval -EINVAL Work item is being processed or has completed its work.
3035 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003036 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003037 */
3038static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003039 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003040{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05003041 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003042}
3043
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003044/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02003045 * @brief Get time remaining before a delayed work gets scheduled.
3046 *
3047 * This routine computes the (approximate) time remaining before a
3048 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02003049 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02003050 *
3051 * @param work Delayed work item.
3052 *
3053 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003054 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02003055 */
Kumar Galacc334c72017-04-21 10:55:34 -05003056static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02003057{
Andy Ross52e444b2018-09-28 09:06:37 -07003058 return __ticks_to_ms(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02003059}
3060
Anas Nashif166f5192018-02-25 08:02:36 -06003061/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003062/**
Anas Nashifce78d162018-05-24 12:43:11 -05003063 * @defgroup mutex_apis Mutex APIs
3064 * @ingroup kernel_apis
3065 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003066 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003067
Anas Nashifce78d162018-05-24 12:43:11 -05003068/**
3069 * Mutex Structure
3070 * @ingroup mutex_apis
3071 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003072struct k_mutex {
3073 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05003074 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04003075 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05003076 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003077 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003078
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003079 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003080};
3081
Anas Nashifce78d162018-05-24 12:43:11 -05003082/**
3083 * @cond INTERNAL_HIDDEN
3084 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003085#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003086 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003087 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003088 .owner = NULL, \
3089 .lock_count = 0, \
3090 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003091 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003092 }
3093
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003094#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
3095
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003096/**
Allan Stephensc98da842016-11-11 15:45:03 -05003097 * INTERNAL_HIDDEN @endcond
3098 */
3099
3100/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003101 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003102 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003103 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003104 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003105 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003107 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003108 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003109 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003110#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003111 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003112 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003113
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003114/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003115 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003117 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003119 * Upon completion, the mutex is available and does not have an owner.
3120 *
3121 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003122 *
3123 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003124 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003125 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003126__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003127
3128/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003129 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003130 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003131 * This routine locks @a mutex. If the mutex is locked by another thread,
3132 * the calling thread waits until the mutex becomes available or until
3133 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003135 * A thread is permitted to lock a mutex it has already locked. The operation
3136 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003137 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003138 * @param mutex Address of the mutex.
3139 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003140 * or one of the special values K_NO_WAIT and K_FOREVER.
3141 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003142 * @retval 0 Mutex locked.
3143 * @retval -EBUSY Returned without waiting.
3144 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003145 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003146 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003147__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003148
3149/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003150 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003151 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003152 * This routine unlocks @a mutex. The mutex must already be locked by the
3153 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003154 *
3155 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003156 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003157 * thread.
3158 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003159 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003160 *
3161 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003162 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003163 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003164__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003165
Allan Stephensc98da842016-11-11 15:45:03 -05003166/**
Anas Nashif166f5192018-02-25 08:02:36 -06003167 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003168 */
3169
3170/**
3171 * @cond INTERNAL_HIDDEN
3172 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003173
3174struct k_sem {
3175 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303176 u32_t count;
3177 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003178 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003179
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003180 _OBJECT_TRACING_NEXT_PTR(k_sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003181};
3182
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003183#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003184 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003185 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003186 .count = initial_count, \
3187 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003188 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003189 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003190 }
3191
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003192#define K_SEM_INITIALIZER DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003193
Allan Stephensc98da842016-11-11 15:45:03 -05003194/**
3195 * INTERNAL_HIDDEN @endcond
3196 */
3197
3198/**
3199 * @defgroup semaphore_apis Semaphore APIs
3200 * @ingroup kernel_apis
3201 * @{
3202 */
3203
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003204/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003205 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003206 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003207 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003208 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003209 * @param sem Address of the semaphore.
3210 * @param initial_count Initial semaphore count.
3211 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003212 *
3213 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003214 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003215 */
Andrew Boie99280232017-09-29 14:17:47 -07003216__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
3217 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003218
3219/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003220 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003221 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003222 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003223 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003224 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3225 *
3226 * @param sem Address of the semaphore.
3227 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003228 * or one of the special values K_NO_WAIT and K_FOREVER.
3229 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05003230 * @note When porting code from the nanokernel legacy API to the new API, be
3231 * careful with the return value of this function. The return value is the
3232 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
3233 * non-zero means failure, while the nano_sem_take family returns 1 for success
3234 * and 0 for failure.
3235 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003236 * @retval 0 Semaphore taken.
3237 * @retval -EBUSY Returned without waiting.
3238 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003239 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003240 */
Andrew Boie99280232017-09-29 14:17:47 -07003241__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003242
3243/**
3244 * @brief Give a semaphore.
3245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003246 * This routine gives @a sem, unless the semaphore is already at its maximum
3247 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003248 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003249 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003251 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003252 *
3253 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003254 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003255 */
Andrew Boie99280232017-09-29 14:17:47 -07003256__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003257
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003258/**
3259 * @brief Reset a semaphore's count to zero.
3260 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003261 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003262 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003263 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003264 *
3265 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003266 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003267 */
Andrew Boie990bf162017-10-03 12:36:49 -07003268__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003269
Anas Nashif954d5502018-02-25 08:37:28 -06003270/**
3271 * @internal
3272 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003273static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003274{
Patrik Flykt24d71432019-03-26 19:57:45 -06003275 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003276}
3277
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003278/**
3279 * @brief Get a semaphore's count.
3280 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003281 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003282 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003283 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003285 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003286 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003287 */
Andrew Boie990bf162017-10-03 12:36:49 -07003288__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003289
Anas Nashif954d5502018-02-25 08:37:28 -06003290/**
3291 * @internal
3292 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003293static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003294{
3295 return sem->count;
3296}
3297
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003298/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003299 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003300 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003301 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003302 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003303 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003304 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003305 * @param name Name of the semaphore.
3306 * @param initial_count Initial semaphore count.
3307 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003308 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003309 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003310#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003311 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003312 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303313 BUILD_ASSERT(((count_limit) != 0) && \
3314 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003315
Anas Nashif166f5192018-02-25 08:02:36 -06003316/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003317
3318/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003319 * @defgroup msgq_apis Message Queue APIs
3320 * @ingroup kernel_apis
3321 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003322 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003323
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003324/**
3325 * @brief Message Queue Structure
3326 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003327struct k_msgq {
3328 _wait_q_t wait_q;
Andy Rossbe03dbd2018-07-26 10:23:02 -07003329 struct k_spinlock lock;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003330 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003331 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003332 char *buffer_start;
3333 char *buffer_end;
3334 char *read_ptr;
3335 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003336 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003337
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003338 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Andrew Boie0fe789f2018-04-12 18:35:56 -07003339 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003340};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003341/**
3342 * @cond INTERNAL_HIDDEN
3343 */
3344
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003345
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003346#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003347 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003348 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003349 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003350 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003351 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003352 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003353 .read_ptr = q_buffer, \
3354 .write_ptr = q_buffer, \
3355 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003356 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003357 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003358#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003359/**
3360 * INTERNAL_HIDDEN @endcond
3361 */
3362
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003363
Andrew Boie0fe789f2018-04-12 18:35:56 -07003364#define K_MSGQ_FLAG_ALLOC BIT(0)
3365
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003366/**
3367 * @brief Message Queue Attributes
3368 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303369struct k_msgq_attrs {
3370 size_t msg_size;
3371 u32_t max_msgs;
3372 u32_t used_msgs;
3373};
3374
Allan Stephensc98da842016-11-11 15:45:03 -05003375
3376/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003377 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003379 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3380 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003381 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3382 * message is similarly aligned to this boundary, @a q_msg_size must also be
3383 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003385 * The message queue can be accessed outside the module where it is defined
3386 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003387 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003388 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003390 * @param q_name Name of the message queue.
3391 * @param q_msg_size Message size (in bytes).
3392 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003393 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003394 *
3395 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003396 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003397#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3398 static char __noinit __aligned(q_align) \
3399 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3400 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
3401 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003402 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003403
Peter Mitsisd7a37502016-10-13 11:37:40 -04003404/**
3405 * @brief Initialize a message queue.
3406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003407 * This routine initializes a message queue object, prior to its first use.
3408 *
Allan Stephensda827222016-11-09 14:23:58 -06003409 * The message queue's ring buffer must contain space for @a max_msgs messages,
3410 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3411 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3412 * that each message is similarly aligned to this boundary, @a q_msg_size
3413 * must also be a multiple of N.
3414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003415 * @param q Address of the message queue.
3416 * @param buffer Pointer to ring buffer that holds queued messages.
3417 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003418 * @param max_msgs Maximum number of messages that can be queued.
3419 *
3420 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003421 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003422 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003423void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3424 u32_t max_msgs);
3425
3426/**
3427 * @brief Initialize a message queue.
3428 *
3429 * This routine initializes a message queue object, prior to its first use,
3430 * allocating its internal ring buffer from the calling thread's resource
3431 * pool.
3432 *
3433 * Memory allocated for the ring buffer can be released by calling
3434 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3435 * all of its references.
3436 *
3437 * @param q Address of the message queue.
3438 * @param msg_size Message size (in bytes).
3439 * @param max_msgs Maximum number of messages that can be queued.
3440 *
3441 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3442 * thread's resource pool, or -EINVAL if the size parameters cause
3443 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003444 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003445 */
3446__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3447 u32_t max_msgs);
3448
3449
3450void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003451
3452/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003453 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003455 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003456 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003457 * @note Can be called by ISRs.
3458 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003459 * @param q Address of the message queue.
3460 * @param data Pointer to the message.
3461 * @param timeout Waiting period to add the message (in milliseconds),
3462 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003463 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003464 * @retval 0 Message sent.
3465 * @retval -ENOMSG Returned without waiting or queue purged.
3466 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003467 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003468 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003469__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003470
3471/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003472 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003474 * This routine receives a message from message queue @a q in a "first in,
3475 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003476 *
Allan Stephensc98da842016-11-11 15:45:03 -05003477 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003479 * @param q Address of the message queue.
3480 * @param data Address of area to hold the received message.
3481 * @param timeout Waiting period to receive the message (in milliseconds),
3482 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003483 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003484 * @retval 0 Message received.
3485 * @retval -ENOMSG Returned without waiting.
3486 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003487 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003488 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003489__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003490
3491/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003492 * @brief Peek/read a message from a message queue.
3493 *
3494 * This routine reads a message from message queue @a q in a "first in,
3495 * first out" manner and leaves the message in the queue.
3496 *
3497 * @note Can be called by ISRs.
3498 *
3499 * @param q Address of the message queue.
3500 * @param data Address of area to hold the message read from the queue.
3501 *
3502 * @retval 0 Message read.
3503 * @retval -ENOMSG Returned when the queue has no message.
3504 * @req K-MSGQ-002
3505 */
3506__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3507
3508/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003509 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003510 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003511 * This routine discards all unreceived messages in a message queue's ring
3512 * buffer. Any threads that are blocked waiting to send a message to the
3513 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003514 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003515 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003516 *
3517 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003518 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003519 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003520__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003521
Peter Mitsis67be2492016-10-07 11:44:34 -04003522/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003523 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003525 * This routine returns the number of unused entries in a message queue's
3526 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003527 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003528 * @param q Address of the message queue.
3529 *
3530 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003531 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003532 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003533__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3534
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303535/**
3536 * @brief Get basic attributes of a message queue.
3537 *
3538 * This routine fetches basic attributes of message queue into attr argument.
3539 *
3540 * @param q Address of the message queue.
3541 * @param attrs pointer to message queue attribute structure.
3542 *
3543 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003544 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303545 */
3546__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3547
3548
Patrik Flykt4344e272019-03-08 14:19:05 -07003549static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003550{
3551 return q->max_msgs - q->used_msgs;
3552}
3553
Peter Mitsisd7a37502016-10-13 11:37:40 -04003554/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003555 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003557 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003558 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003559 * @param q Address of the message queue.
3560 *
3561 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003562 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003563 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003564__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3565
Patrik Flykt4344e272019-03-08 14:19:05 -07003566static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003567{
3568 return q->used_msgs;
3569}
3570
Anas Nashif166f5192018-02-25 08:02:36 -06003571/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003572
3573/**
3574 * @defgroup mem_pool_apis Memory Pool APIs
3575 * @ingroup kernel_apis
3576 * @{
3577 */
3578
Andy Ross73cb9582017-05-09 10:42:39 -07003579/* Note on sizing: the use of a 20 bit field for block means that,
3580 * assuming a reasonable minimum block size of 16 bytes, we're limited
3581 * to 16M of memory managed by a single pool. Long term it would be
3582 * good to move to a variable bit size based on configuration.
3583 */
3584struct k_mem_block_id {
3585 u32_t pool : 8;
3586 u32_t level : 4;
3587 u32_t block : 20;
3588};
3589
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003590struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003591 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003592 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003593};
3594
Anas Nashif166f5192018-02-25 08:02:36 -06003595/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003596
3597/**
3598 * @defgroup mailbox_apis Mailbox APIs
3599 * @ingroup kernel_apis
3600 * @{
3601 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003602
3603struct k_mbox_msg {
3604 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003605 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003606 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003607 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003608 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003609 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003610 /** sender's message data buffer */
3611 void *tx_data;
3612 /** internal use only - needed for legacy API support */
3613 void *_rx_data;
3614 /** message data block descriptor */
3615 struct k_mem_block tx_block;
3616 /** source thread id */
3617 k_tid_t rx_source_thread;
3618 /** target thread id */
3619 k_tid_t tx_target_thread;
3620 /** internal use only - thread waiting on send (may be a dummy) */
3621 k_tid_t _syncing_thread;
3622#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3623 /** internal use only - semaphore used during asynchronous send */
3624 struct k_sem *_async_sem;
3625#endif
3626};
3627
3628struct k_mbox {
3629 _wait_q_t tx_msg_queue;
3630 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003631 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003632
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003633 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003634};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003635/**
3636 * @cond INTERNAL_HIDDEN
3637 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003638
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003639#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003640 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003641 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3642 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003643 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003644 }
3645
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003646#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3647
Peter Mitsis12092702016-10-14 12:57:23 -04003648/**
Allan Stephensc98da842016-11-11 15:45:03 -05003649 * INTERNAL_HIDDEN @endcond
3650 */
3651
3652/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003653 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003654 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003655 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003656 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003657 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003658 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003659 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003660 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003661 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003662#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003663 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003664 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003665
Peter Mitsis12092702016-10-14 12:57:23 -04003666/**
3667 * @brief Initialize a mailbox.
3668 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003669 * This routine initializes a mailbox object, prior to its first use.
3670 *
3671 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003672 *
3673 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003674 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003675 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003676extern void k_mbox_init(struct k_mbox *mbox);
3677
Peter Mitsis12092702016-10-14 12:57:23 -04003678/**
3679 * @brief Send a mailbox message in a synchronous manner.
3680 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003681 * This routine sends a message to @a mbox and waits for a receiver to both
3682 * receive and process it. The message data may be in a buffer, in a memory
3683 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003685 * @param mbox Address of the mailbox.
3686 * @param tx_msg Address of the transmit message descriptor.
3687 * @param timeout Waiting period for the message to be received (in
3688 * milliseconds), or one of the special values K_NO_WAIT
3689 * and K_FOREVER. Once the message has been received,
3690 * this routine waits as long as necessary for the message
3691 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003692 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003693 * @retval 0 Message sent.
3694 * @retval -ENOMSG Returned without waiting.
3695 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003696 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003697 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003698extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003699 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003700
Peter Mitsis12092702016-10-14 12:57:23 -04003701/**
3702 * @brief Send a mailbox message in an asynchronous manner.
3703 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003704 * This routine sends a message to @a mbox without waiting for a receiver
3705 * to process it. The message data may be in a buffer, in a memory pool block,
3706 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3707 * will be given when the message has been both received and completely
3708 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003709 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003710 * @param mbox Address of the mailbox.
3711 * @param tx_msg Address of the transmit message descriptor.
3712 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003713 *
3714 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003715 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003716 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003717extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003718 struct k_sem *sem);
3719
Peter Mitsis12092702016-10-14 12:57:23 -04003720/**
3721 * @brief Receive a mailbox message.
3722 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003723 * This routine receives a message from @a mbox, then optionally retrieves
3724 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003725 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003726 * @param mbox Address of the mailbox.
3727 * @param rx_msg Address of the receive message descriptor.
3728 * @param buffer Address of the buffer to receive data, or NULL to defer data
3729 * retrieval and message disposal until later.
3730 * @param timeout Waiting period for a message to be received (in
3731 * milliseconds), or one of the special values K_NO_WAIT
3732 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003733 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003734 * @retval 0 Message received.
3735 * @retval -ENOMSG Returned without waiting.
3736 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003737 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003738 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003739extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003740 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003741
3742/**
3743 * @brief Retrieve mailbox message data into a buffer.
3744 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003745 * This routine completes the processing of a received message by retrieving
3746 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003747 *
3748 * Alternatively, this routine can be used to dispose of a received message
3749 * without retrieving its data.
3750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003751 * @param rx_msg Address of the receive message descriptor.
3752 * @param buffer Address of the buffer to receive data, or NULL to discard
3753 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003754 *
3755 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003756 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003757 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003758extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003759
3760/**
3761 * @brief Retrieve mailbox message data into a memory pool block.
3762 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003763 * This routine completes the processing of a received message by retrieving
3764 * its data into a memory pool block, then disposing of the message.
3765 * The memory pool block that results from successful retrieval must be
3766 * returned to the pool once the data has been processed, even in cases
3767 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003768 *
3769 * Alternatively, this routine can be used to dispose of a received message
3770 * without retrieving its data. In this case there is no need to return a
3771 * memory pool block to the pool.
3772 *
3773 * This routine allocates a new memory pool block for the data only if the
3774 * data is not already in one. If a new block cannot be allocated, the routine
3775 * returns a failure code and the received message is left unchanged. This
3776 * permits the caller to reattempt data retrieval at a later time or to dispose
3777 * of the received message without retrieving its data.
3778 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003779 * @param rx_msg Address of a receive message descriptor.
3780 * @param pool Address of memory pool, or NULL to discard data.
3781 * @param block Address of the area to hold memory pool block info.
3782 * @param timeout Waiting period to wait for a memory pool block (in
3783 * milliseconds), or one of the special values K_NO_WAIT
3784 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003785 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003786 * @retval 0 Data retrieved.
3787 * @retval -ENOMEM Returned without waiting.
3788 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003789 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003790 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003791extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003792 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003793 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003794
Anas Nashif166f5192018-02-25 08:02:36 -06003795/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003796
3797/**
Anas Nashifce78d162018-05-24 12:43:11 -05003798 * @defgroup pipe_apis Pipe APIs
3799 * @ingroup kernel_apis
3800 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003801 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003802
Anas Nashifce78d162018-05-24 12:43:11 -05003803/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003804struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003805 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3806 size_t size; /**< Buffer size */
3807 size_t bytes_used; /**< # bytes used in buffer */
3808 size_t read_index; /**< Where in buffer to read from */
3809 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08003810 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003811
3812 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003813 _wait_q_t readers; /**< Reader wait queue */
3814 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003815 } wait_q;
3816
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003817 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Anas Nashifce78d162018-05-24 12:43:11 -05003818 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003819};
3820
Anas Nashifce78d162018-05-24 12:43:11 -05003821/**
3822 * @cond INTERNAL_HIDDEN
3823 */
3824#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3825
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003826#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
3827 { \
3828 .buffer = pipe_buffer, \
3829 .size = pipe_buffer_size, \
3830 .bytes_used = 0, \
3831 .read_index = 0, \
3832 .write_index = 0, \
3833 .lock = {}, \
3834 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003835 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
3836 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003837 }, \
3838 _OBJECT_TRACING_INIT \
3839 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003840 }
3841
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003842#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3843
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003844/**
Allan Stephensc98da842016-11-11 15:45:03 -05003845 * INTERNAL_HIDDEN @endcond
3846 */
3847
3848/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003849 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003850 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003851 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003852 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003853 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003855 * @param name Name of the pipe.
3856 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3857 * or zero if no ring buffer is used.
3858 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003859 *
3860 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003861 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003862#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08003863 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07003864 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003865 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003866 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003867
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003868/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003869 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003871 * This routine initializes a pipe object, prior to its first use.
3872 *
3873 * @param pipe Address of the pipe.
3874 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3875 * is used.
3876 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3877 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003878 *
3879 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003880 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003881 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003882void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3883
3884/**
3885 * @brief Release a pipe's allocated buffer
3886 *
3887 * If a pipe object was given a dynamically allocated buffer via
3888 * k_pipe_alloc_init(), this will free it. This function does nothing
3889 * if the buffer wasn't dynamically allocated.
3890 *
3891 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003892 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003893 */
3894void k_pipe_cleanup(struct k_pipe *pipe);
3895
3896/**
3897 * @brief Initialize a pipe and allocate a buffer for it
3898 *
3899 * Storage for the buffer region will be allocated from the calling thread's
3900 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3901 * or userspace is enabled and the pipe object loses all references to it.
3902 *
3903 * This function should only be called on uninitialized pipe objects.
3904 *
3905 * @param pipe Address of the pipe.
3906 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3907 * buffer is used.
3908 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003909 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003910 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003911 */
3912__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003913
3914/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003915 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003917 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003918 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003919 * @param pipe Address of the pipe.
3920 * @param data Address of data to write.
3921 * @param bytes_to_write Size of data (in bytes).
3922 * @param bytes_written Address of area to hold the number of bytes written.
3923 * @param min_xfer Minimum number of bytes to write.
3924 * @param timeout Waiting period to wait for the data to be written (in
3925 * milliseconds), or one of the special values K_NO_WAIT
3926 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003927 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003928 * @retval 0 At least @a min_xfer bytes of data were written.
3929 * @retval -EIO Returned without waiting; zero data bytes were written.
3930 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003931 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003932 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003933 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003934__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3935 size_t bytes_to_write, size_t *bytes_written,
3936 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003937
3938/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003939 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003940 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003941 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003943 * @param pipe Address of the pipe.
3944 * @param data Address to place the data read from pipe.
3945 * @param bytes_to_read Maximum number of data bytes to read.
3946 * @param bytes_read Address of area to hold the number of bytes read.
3947 * @param min_xfer Minimum number of data bytes to read.
3948 * @param timeout Waiting period to wait for the data to be read (in
3949 * milliseconds), or one of the special values K_NO_WAIT
3950 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003951 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003952 * @retval 0 At least @a min_xfer bytes of data were read.
3953 * @retval -EIO Returned without waiting; zero data bytes were read.
3954 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003955 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003956 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003957 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003958__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3959 size_t bytes_to_read, size_t *bytes_read,
3960 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003961
3962/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003963 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003964 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003965 * This routine writes the data contained in a memory block to @a pipe.
3966 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003967 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003969 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003970 * @param block Memory block containing data to send
3971 * @param size Number of data bytes in memory block to send
3972 * @param sem Semaphore to signal upon completion (else NULL)
3973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003974 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003975 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003976 */
3977extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3978 size_t size, struct k_sem *sem);
3979
Anas Nashif166f5192018-02-25 08:02:36 -06003980/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003981
Allan Stephensc98da842016-11-11 15:45:03 -05003982/**
3983 * @cond INTERNAL_HIDDEN
3984 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003985
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003986struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003987 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003988 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003989 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003990 char *buffer;
3991 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003992 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003993
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003994 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003995};
3996
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003997#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003998 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003999 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004000 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004001 .num_blocks = slab_num_blocks, \
4002 .block_size = slab_block_size, \
4003 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004004 .free_list = NULL, \
4005 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05004006 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004007 }
4008
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004009#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
4010
4011
Peter Mitsis578f9112016-10-07 13:50:31 -04004012/**
Allan Stephensc98da842016-11-11 15:45:03 -05004013 * INTERNAL_HIDDEN @endcond
4014 */
4015
4016/**
4017 * @defgroup mem_slab_apis Memory Slab APIs
4018 * @ingroup kernel_apis
4019 * @{
4020 */
4021
4022/**
Allan Stephensda827222016-11-09 14:23:58 -06004023 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04004024 *
Allan Stephensda827222016-11-09 14:23:58 -06004025 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004026 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06004027 * @a slab_align -byte boundary. To ensure that each memory block is similarly
4028 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004029 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04004030 *
Allan Stephensda827222016-11-09 14:23:58 -06004031 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004032 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004033 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004034 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004035 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004036 * @param name Name of the memory slab.
4037 * @param slab_block_size Size of each memory block (in bytes).
4038 * @param slab_num_blocks Number memory blocks.
4039 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004040 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04004041 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004042#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004043 char __noinit __aligned(WB_UP(slab_align)) \
4044 _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004045 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004046 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004047 WB_UP(slab_block_size), slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004048
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004049/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004050 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004051 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004052 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004053 *
Allan Stephensda827222016-11-09 14:23:58 -06004054 * The memory slab's buffer contains @a slab_num_blocks memory blocks
4055 * that are @a slab_block_size bytes long. The buffer must be aligned to an
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004056 * N-byte boundary matching a word boundary, where N is a power of 2
4057 * (i.e. 4 on 32-bit systems, 8, 16, ...).
Allan Stephensda827222016-11-09 14:23:58 -06004058 * To ensure that each memory block is similarly aligned to this boundary,
4059 * @a slab_block_size must also be a multiple of N.
4060 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004061 * @param slab Address of the memory slab.
4062 * @param buffer Pointer to buffer used for the memory blocks.
4063 * @param block_size Size of each memory block (in bytes).
4064 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004065 *
4066 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004067 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004068 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004069extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05004070 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004071
4072/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004073 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004075 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004076 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004077 * @param slab Address of the memory slab.
4078 * @param mem Pointer to block address area.
4079 * @param timeout Maximum time to wait for operation to complete
4080 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4081 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004082 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004083 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004084 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004085 * @retval -ENOMEM Returned without waiting.
4086 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004087 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004088 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004089extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05004090 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004091
4092/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004093 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004095 * This routine releases a previously allocated memory block back to its
4096 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004097 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004098 * @param slab Address of the memory slab.
4099 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004100 *
4101 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004102 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004103 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004104extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004105
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004106/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004107 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004108 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004109 * This routine gets the number of memory blocks that are currently
4110 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004112 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004113 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004114 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004115 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004116 */
Kumar Galacc334c72017-04-21 10:55:34 -05004117static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004118{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004119 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004120}
4121
Peter Mitsisc001aa82016-10-13 13:53:37 -04004122/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004123 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004125 * This routine gets the number of memory blocks that are currently
4126 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004127 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004128 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004130 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004131 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004132 */
Kumar Galacc334c72017-04-21 10:55:34 -05004133static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004134{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004135 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004136}
4137
Anas Nashif166f5192018-02-25 08:02:36 -06004138/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004139
4140/**
4141 * @cond INTERNAL_HIDDEN
4142 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004143
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004144struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004145 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004146 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004147};
4148
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004149/**
Allan Stephensc98da842016-11-11 15:45:03 -05004150 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004151 */
4152
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004153/**
Allan Stephensc98da842016-11-11 15:45:03 -05004154 * @addtogroup mem_pool_apis
4155 * @{
4156 */
4157
4158/**
4159 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004160 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004161 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4162 * long. The memory pool allows blocks to be repeatedly partitioned into
4163 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004164 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004165 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004166 * If the pool is to be accessed outside the module where it is defined, it
4167 * can be declared via
4168 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004169 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004170 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004171 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004172 * @param minsz Size of the smallest blocks in the pool (in bytes).
4173 * @param maxsz Size of the largest blocks in the pool (in bytes).
4174 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004175 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004176 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004177 */
Andy Ross73cb9582017-05-09 10:42:39 -07004178#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004179 char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \
Andy Ross73cb9582017-05-09 10:42:39 -07004180 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004181 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004182 Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004183 .base = { \
4184 .buf = _mpool_buf_##name, \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004185 .max_sz = WB_UP(maxsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004186 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004187 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004188 .levels = _mpool_lvls_##name, \
4189 .flags = SYS_MEM_POOL_KERNEL \
4190 } \
Johann Fischer223a2b92019-07-04 15:55:20 +02004191 }; \
Nicolas Pitreb2a022b2019-09-26 16:36:40 -04004192 BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004193
Peter Mitsis937042c2016-10-13 13:18:26 -04004194/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004195 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004197 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004199 * @param pool Address of the memory pool.
4200 * @param block Pointer to block descriptor for the allocated memory.
4201 * @param size Amount of memory to allocate (in bytes).
4202 * @param timeout Maximum time to wait for operation to complete
4203 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4204 * or K_FOREVER to wait as long as necessary.
4205 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004206 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004207 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004208 * @retval -ENOMEM Returned without waiting.
4209 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004210 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004211 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004212extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004213 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004214
4215/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004216 * @brief Allocate memory from a memory pool with malloc() semantics
4217 *
4218 * Such memory must be released using k_free().
4219 *
4220 * @param pool Address of the memory pool.
4221 * @param size Amount of memory to allocate (in bytes).
4222 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004223 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004224 */
4225extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4226
4227/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004228 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004229 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004230 * This routine releases a previously allocated memory block back to its
4231 * memory pool.
4232 *
4233 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004234 *
4235 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004236 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004237 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004238extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004239
4240/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004241 * @brief Free memory allocated from a memory pool.
4242 *
4243 * This routine releases a previously allocated memory block back to its
4244 * memory pool
4245 *
4246 * @param id Memory block identifier.
4247 *
4248 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004249 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004250 */
4251extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4252
4253/**
Anas Nashif166f5192018-02-25 08:02:36 -06004254 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004255 */
4256
4257/**
4258 * @defgroup heap_apis Heap Memory Pool APIs
4259 * @ingroup kernel_apis
4260 * @{
4261 */
4262
4263/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004264 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004265 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004266 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004267 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004268 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004269 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004270 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004271 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004272 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004273 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004274extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004275
4276/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004277 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004278 *
4279 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004280 * returned must have been allocated from the heap memory pool or
4281 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004282 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004283 * If @a ptr is NULL, no operation is performed.
4284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004285 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004286 *
4287 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004288 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004289 */
4290extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004291
Allan Stephensc98da842016-11-11 15:45:03 -05004292/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004293 * @brief Allocate memory from heap, array style
4294 *
4295 * This routine provides traditional calloc() semantics. Memory is
4296 * allocated from the heap memory pool and zeroed.
4297 *
4298 * @param nmemb Number of elements in the requested array
4299 * @param size Size of each array element (in bytes).
4300 *
4301 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004302 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004303 */
4304extern void *k_calloc(size_t nmemb, size_t size);
4305
Anas Nashif166f5192018-02-25 08:02:36 -06004306/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004307
Benjamin Walshacc68c12017-01-29 18:57:45 -05004308/* polling API - PRIVATE */
4309
Benjamin Walshb0179862017-02-02 16:39:57 -05004310#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004311#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004312#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004313#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004314#endif
4315
Benjamin Walshacc68c12017-01-29 18:57:45 -05004316/* private - implementation data created as needed, per-type */
4317struct _poller {
4318 struct k_thread *thread;
Flavio Ceolin76b35182018-12-16 12:48:29 -08004319 volatile bool is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004320};
4321
4322/* private - types bit positions */
4323enum _poll_types_bits {
4324 /* can be used to ignore an event */
4325 _POLL_TYPE_IGNORE,
4326
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004327 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004328 _POLL_TYPE_SIGNAL,
4329
4330 /* semaphore availability */
4331 _POLL_TYPE_SEM_AVAILABLE,
4332
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004333 /* queue/fifo/lifo data availability */
4334 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004335
4336 _POLL_NUM_TYPES
4337};
4338
Patrik Flykt4344e272019-03-08 14:19:05 -07004339#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004340
4341/* private - states bit positions */
4342enum _poll_states_bits {
4343 /* default state when creating event */
4344 _POLL_STATE_NOT_READY,
4345
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004346 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004347 _POLL_STATE_SIGNALED,
4348
4349 /* semaphore is available */
4350 _POLL_STATE_SEM_AVAILABLE,
4351
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004352 /* data is available to read on queue/fifo/lifo */
4353 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004354
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004355 /* queue/fifo/lifo wait was cancelled */
4356 _POLL_STATE_CANCELLED,
4357
Benjamin Walshacc68c12017-01-29 18:57:45 -05004358 _POLL_NUM_STATES
4359};
4360
Patrik Flykt4344e272019-03-08 14:19:05 -07004361#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004362
4363#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004364 (32 - (0 \
4365 + 8 /* tag */ \
4366 + _POLL_NUM_TYPES \
4367 + _POLL_NUM_STATES \
4368 + 1 /* modes */ \
4369 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004370
Benjamin Walshacc68c12017-01-29 18:57:45 -05004371/* end of polling API - PRIVATE */
4372
4373
4374/**
4375 * @defgroup poll_apis Async polling APIs
4376 * @ingroup kernel_apis
4377 * @{
4378 */
4379
4380/* Public polling API */
4381
4382/* public - values for k_poll_event.type bitfield */
4383#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004384#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4385#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4386#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004387#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004388
4389/* public - polling modes */
4390enum k_poll_modes {
4391 /* polling thread does not take ownership of objects when available */
4392 K_POLL_MODE_NOTIFY_ONLY = 0,
4393
4394 K_POLL_NUM_MODES
4395};
4396
4397/* public - values for k_poll_event.state bitfield */
4398#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004399#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4400#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4401#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004402#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004403#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004404
4405/* public - poll signal object */
4406struct k_poll_signal {
4407 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004408 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004409
4410 /*
4411 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4412 * user resets it to 0.
4413 */
4414 unsigned int signaled;
4415
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004416 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004417 int result;
4418};
4419
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004420#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004421 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004422 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004423 .signaled = 0, \
4424 .result = 0, \
4425 }
4426
4427struct k_poll_event {
4428 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004429 sys_dnode_t _node;
4430
4431 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004432 struct _poller *poller;
4433
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004434 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004435 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004436
Benjamin Walshacc68c12017-01-29 18:57:45 -05004437 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004438 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004439
4440 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004441 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004442
4443 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004444 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004445
4446 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004447 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004448
4449 /* per-type data */
4450 union {
4451 void *obj;
4452 struct k_poll_signal *signal;
4453 struct k_sem *sem;
4454 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004455 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004456 };
4457};
4458
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004459#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004460 { \
4461 .poller = NULL, \
4462 .type = event_type, \
4463 .state = K_POLL_STATE_NOT_READY, \
4464 .mode = event_mode, \
4465 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004466 { .obj = event_obj }, \
4467 }
4468
4469#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4470 event_tag) \
4471 { \
4472 .type = event_type, \
4473 .tag = event_tag, \
4474 .state = K_POLL_STATE_NOT_READY, \
4475 .mode = event_mode, \
4476 .unused = 0, \
4477 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004478 }
4479
4480/**
4481 * @brief Initialize one struct k_poll_event instance
4482 *
4483 * After this routine is called on a poll event, the event it ready to be
4484 * placed in an event array to be passed to k_poll().
4485 *
4486 * @param event The event to initialize.
4487 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4488 * values. Only values that apply to the same object being polled
4489 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4490 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004491 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004492 * @param obj Kernel object or poll signal.
4493 *
4494 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004495 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004496 */
4497
Kumar Galacc334c72017-04-21 10:55:34 -05004498extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004499 int mode, void *obj);
4500
4501/**
4502 * @brief Wait for one or many of multiple poll events to occur
4503 *
4504 * This routine allows a thread to wait concurrently for one or many of
4505 * multiple poll events to have occurred. Such events can be a kernel object
4506 * being available, like a semaphore, or a poll signal event.
4507 *
4508 * When an event notifies that a kernel object is available, the kernel object
4509 * is not "given" to the thread calling k_poll(): it merely signals the fact
4510 * that the object was available when the k_poll() call was in effect. Also,
4511 * all threads trying to acquire an object the regular way, i.e. by pending on
4512 * the object, have precedence over the thread polling on the object. This
4513 * means that the polling thread will never get the poll event on an object
4514 * until the object becomes available and its pend queue is empty. For this
4515 * reason, the k_poll() call is more effective when the objects being polled
4516 * only have one thread, the polling thread, trying to acquire them.
4517 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004518 * When k_poll() returns 0, the caller should loop on all the events that were
4519 * passed to k_poll() and check the state field for the values that were
4520 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004521 *
4522 * Before being reused for another call to k_poll(), the user has to reset the
4523 * state field to K_POLL_STATE_NOT_READY.
4524 *
Andrew Boie3772f772018-05-07 16:52:57 -07004525 * When called from user mode, a temporary memory allocation is required from
4526 * the caller's resource pool.
4527 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004528 * @param events An array of pointers to events to be polled for.
4529 * @param num_events The number of events in the array.
4530 * @param timeout Waiting period for an event to be ready (in milliseconds),
4531 * or one of the special values K_NO_WAIT and K_FOREVER.
4532 *
4533 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004534 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004535 * @retval -EINTR Polling has been interrupted, e.g. with
4536 * k_queue_cancel_wait(). All output events are still set and valid,
4537 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4538 * words, -EINTR status means that at least one of output events is
4539 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004540 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4541 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004542 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004543 */
4544
Andrew Boie3772f772018-05-07 16:52:57 -07004545__syscall int k_poll(struct k_poll_event *events, int num_events,
4546 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004547
4548/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004549 * @brief Initialize a poll signal object.
4550 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004551 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004552 *
4553 * @param signal A poll signal.
4554 *
4555 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004556 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004557 */
4558
Andrew Boie3772f772018-05-07 16:52:57 -07004559__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4560
4561/*
4562 * @brief Reset a poll signal object's state to unsignaled.
4563 *
4564 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004565 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004566 */
4567__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4568
Patrik Flykt4344e272019-03-08 14:19:05 -07004569static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004570{
Patrik Flykt24d71432019-03-26 19:57:45 -06004571 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004572}
4573
4574/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004575 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004576 *
4577 * @param signal A poll signal object
4578 * @param signaled An integer buffer which will be written nonzero if the
4579 * object was signaled
4580 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004581 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004582 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004583 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004584 */
4585__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4586 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004587
4588/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004589 * @brief Signal a poll signal object.
4590 *
4591 * This routine makes ready a poll signal, which is basically a poll event of
4592 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4593 * made ready to run. A @a result value can be specified.
4594 *
4595 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004596 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004597 * k_poll_signal_reset(). It thus has to be reset by the user before being
4598 * passed again to k_poll() or k_poll() will consider it being signaled, and
4599 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004600 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004601 * @note The result is stored and the 'signaled' field is set even if
4602 * this function returns an error indicating that an expiring poll was
4603 * not notified. The next k_poll() will detect the missed raise.
4604 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004605 * @param signal A poll signal.
4606 * @param result The value to store in the result field of the signal.
4607 *
4608 * @retval 0 The signal was delivered successfully.
4609 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004610 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004611 */
4612
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004613__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004614
Anas Nashif954d5502018-02-25 08:37:28 -06004615/**
4616 * @internal
4617 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004618extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004619
Anas Nashif166f5192018-02-25 08:02:36 -06004620/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004621
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004622/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004623 * @defgroup cpu_idle_apis CPU Idling APIs
4624 * @ingroup kernel_apis
4625 * @{
4626 */
4627
Andrew Boie07525a32019-09-21 16:17:23 -07004628extern void z_arch_cpu_idle(void);
4629extern void z_arch_cpu_atomic_idle(unsigned int key);
4630
Anas Nashif30c3cff2019-01-22 08:18:13 -05004631/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004632 * @brief Make the CPU idle.
4633 *
4634 * This function makes the CPU idle until an event wakes it up.
4635 *
4636 * In a regular system, the idle thread should be the only thread responsible
4637 * for making the CPU idle and triggering any type of power management.
4638 * However, in some more constrained systems, such as a single-threaded system,
4639 * the only thread would be responsible for this if needed.
4640 *
4641 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004642 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004643 */
Andrew Boie07525a32019-09-21 16:17:23 -07004644static inline void k_cpu_idle(void)
4645{
4646 z_arch_cpu_idle();
4647}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004648
4649/**
4650 * @brief Make the CPU idle in an atomic fashion.
4651 *
4652 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4653 * must be done atomically before making the CPU idle.
4654 *
4655 * @param key Interrupt locking key obtained from irq_lock().
4656 *
4657 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004658 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004659 */
Andrew Boie07525a32019-09-21 16:17:23 -07004660static inline void k_cpu_atomic_idle(unsigned int key)
4661{
4662 z_arch_cpu_atomic_idle(key);
4663}
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004664
Anas Nashif30c3cff2019-01-22 08:18:13 -05004665/**
4666 * @}
4667 */
Anas Nashif954d5502018-02-25 08:37:28 -06004668
4669/**
4670 * @internal
4671 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004672extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004673
Patrik Flykt4344e272019-03-08 14:19:05 -07004674#ifdef Z_ARCH_EXCEPT
Andrew Boiecdb94d62017-04-18 15:22:05 -07004675/* This archtecture has direct support for triggering a CPU exception */
Patrik Flykt4344e272019-03-08 14:19:05 -07004676#define z_except_reason(reason) Z_ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004677#else
4678
Andrew Boiecdb94d62017-04-18 15:22:05 -07004679/* NOTE: This is the implementation for arches that do not implement
Patrik Flykt4344e272019-03-08 14:19:05 -07004680 * Z_ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004681 *
4682 * We won't have a real exception frame to determine the PC value when
4683 * the oops occurred, so print file and line number before we jump into
4684 * the fatal error handler.
4685 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004686#define z_except_reason(reason) do { \
Andrew Boiecdb94d62017-04-18 15:22:05 -07004687 printk("@ %s:%d:\n", __FILE__, __LINE__); \
Andrew Boie56236372019-07-15 15:22:29 -07004688 z_fatal_error(reason, NULL); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004689 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004690
4691#endif /* _ARCH__EXCEPT */
4692
4693/**
4694 * @brief Fatally terminate a thread
4695 *
4696 * This should be called when a thread has encountered an unrecoverable
4697 * runtime condition and needs to terminate. What this ultimately
4698 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004699 * will be called will reason code K_ERR_KERNEL_OOPS.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004700 *
4701 * If this is called from ISR context, the default system fatal error handler
4702 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004703 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004704 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004705#define k_oops() z_except_reason(K_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004706
4707/**
4708 * @brief Fatally terminate the system
4709 *
4710 * This should be called when the Zephyr kernel has encountered an
4711 * unrecoverable runtime condition and needs to terminate. What this ultimately
4712 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004713 * will be called will reason code K_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004714 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004715 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004716#define k_panic() z_except_reason(K_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004717
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004718/*
4719 * private APIs that are utilized by one or more public APIs
4720 */
4721
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004722#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004723/**
4724 * @internal
4725 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004726extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004727#else
Anas Nashif954d5502018-02-25 08:37:28 -06004728/**
4729 * @internal
4730 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004731#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004732#endif
4733
Anas Nashif954d5502018-02-25 08:37:28 -06004734/**
4735 * @internal
4736 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004737extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004738/**
4739 * @internal
4740 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004741extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004742
Andrew Boiedc5d9352017-06-02 12:56:47 -07004743/* arch/cpu.h may declare an architecture or platform-specific macro
4744 * for properly declaring stacks, compatible with MMU/MPU constraints if
4745 * enabled
4746 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004747
4748/**
4749 * @brief Obtain an extern reference to a stack
4750 *
4751 * This macro properly brings the symbol of a thread stack declared
4752 * elsewhere into scope.
4753 *
4754 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004755 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004756 */
4757#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4758
Patrik Flykt4344e272019-03-08 14:19:05 -07004759#ifdef Z_ARCH_THREAD_STACK_DEFINE
4760#define K_THREAD_STACK_DEFINE(sym, size) Z_ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004761#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Patrik Flykt4344e272019-03-08 14:19:05 -07004762 Z_ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4763#define K_THREAD_STACK_LEN(size) Z_ARCH_THREAD_STACK_LEN(size)
4764#define K_THREAD_STACK_MEMBER(sym, size) Z_ARCH_THREAD_STACK_MEMBER(sym, size)
4765#define K_THREAD_STACK_SIZEOF(sym) Z_ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie575abc02019-03-19 10:42:24 -07004766#define K_THREAD_STACK_RESERVED Z_ARCH_THREAD_STACK_RESERVED
Andrew Boie4e5c0932019-04-04 12:05:28 -07004767static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004768{
Patrik Flykt4344e272019-03-08 14:19:05 -07004769 return Z_ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07004770}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004771#else
4772/**
4773 * @brief Declare a toplevel thread stack memory region
4774 *
4775 * This declares a region of memory suitable for use as a thread's stack.
4776 *
4777 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4778 * 'noinit' section so that it isn't zeroed at boot
4779 *
Andrew Boie507852a2017-07-25 18:47:07 -07004780 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004781 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07004782 * inside needs to be examined, examine thread->stack_info for the associated
4783 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004784 *
4785 * It is legal to precede this definition with the 'static' keyword.
4786 *
4787 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4788 * parameter of k_thread_create(), it may not be the same as the
4789 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4790 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004791 * Some arches may round the size of the usable stack region up to satisfy
4792 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4793 * size.
4794 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004795 * @param sym Thread stack symbol name
4796 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004797 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004798 */
4799#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004800 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004801
4802/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304803 * @brief Calculate size of stacks to be allocated in a stack array
4804 *
4805 * This macro calculates the size to be allocated for the stacks
4806 * inside a stack array. It accepts the indicated "size" as a parameter
4807 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4808 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4809 *
4810 * @param size Size of the stack memory region
4811 * @req K-TSTACK-001
4812 */
4813#define K_THREAD_STACK_LEN(size) (size)
4814
4815/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004816 * @brief Declare a toplevel array of thread stack memory regions
4817 *
4818 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4819 * definition for additional details and constraints.
4820 *
4821 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4822 * 'noinit' section so that it isn't zeroed at boot
4823 *
4824 * @param sym Thread stack symbol name
4825 * @param nmemb Number of stacks to declare
4826 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004827 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004828 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004829#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004830 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304831 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004832
4833/**
4834 * @brief Declare an embedded stack memory region
4835 *
4836 * Used for stacks embedded within other data structures. Use is highly
4837 * discouraged but in some cases necessary. For memory protection scenarios,
4838 * it is very important that any RAM preceding this member not be writable
4839 * by threads else a stack overflow will lead to silent corruption. In other
4840 * words, the containing data structure should live in RAM owned by the kernel.
4841 *
4842 * @param sym Thread stack symbol name
4843 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004844 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004845 */
4846#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004847 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004848
4849/**
4850 * @brief Return the size in bytes of a stack memory region
4851 *
4852 * Convenience macro for passing the desired stack size to k_thread_create()
4853 * since the underlying implementation may actually create something larger
4854 * (for instance a guard area).
4855 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004856 * The value returned here is not guaranteed to match the 'size' parameter
4857 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004858 *
4859 * @param sym Stack memory symbol
4860 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004861 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004862 */
4863#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4864
Andrew Boie575abc02019-03-19 10:42:24 -07004865
4866/**
4867 * @brief Indicate how much additional memory is reserved for stack objects
4868 *
4869 * Any given stack declaration may have additional memory in it for guard
4870 * areas or supervisor mode stacks. This macro indicates how much space
4871 * is reserved for this. The memory reserved may not be contiguous within
4872 * the stack object, and does not account for additional space used due to
4873 * enforce alignment.
4874 */
4875#define K_THREAD_STACK_RESERVED 0
4876
Andrew Boiedc5d9352017-06-02 12:56:47 -07004877/**
4878 * @brief Get a pointer to the physical stack buffer
4879 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07004880 * This macro is deprecated. If a stack buffer needs to be examined, the
4881 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004882 *
4883 * @param sym Declared stack symbol name
4884 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004885 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004886 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07004887static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004888{
4889 return (char *)sym;
4890}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004891
4892#endif /* _ARCH_DECLARE_STACK */
4893
Chunlin Hane9c97022017-07-07 20:29:30 +08004894/**
4895 * @defgroup mem_domain_apis Memory domain APIs
4896 * @ingroup kernel_apis
4897 * @{
4898 */
4899
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004900/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004901 * @def K_MEM_PARTITION_DEFINE
4902 * @brief Used to declare a memory partition
4903 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004904 */
4905#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4906#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4907 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08004908 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004909 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004910#else
4911#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08004912 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004913 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004914#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4915
Chunlin Hane9c97022017-07-07 20:29:30 +08004916/* memory partition */
4917struct k_mem_partition {
4918 /* start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004919 uintptr_t start;
Chunlin Hane9c97022017-07-07 20:29:30 +08004920 /* size of memory partition */
4921 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004922#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08004923 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304924 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004925#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08004926};
4927
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02004928/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304929 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004930struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304931#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004932 /* partitions in the domain */
4933 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304934#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004935 /* domain q */
4936 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004937 /* number of partitions in the domain */
4938 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004939};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304940
Chunlin Hane9c97022017-07-07 20:29:30 +08004941
4942/**
4943 * @brief Initialize a memory domain.
4944 *
4945 * Initialize a memory domain with given name and memory partitions.
4946 *
Andrew Boiefddd5502019-07-30 18:07:32 -07004947 * See documentation for k_mem_domain_add_partition() for details about
4948 * partition constraints.
4949 *
Chunlin Hane9c97022017-07-07 20:29:30 +08004950 * @param domain The memory domain to be initialized.
4951 * @param num_parts The number of array items of "parts" parameter.
4952 * @param parts An array of pointers to the memory partitions. Can be NULL
4953 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004954 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004955 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004956extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004957 struct k_mem_partition *parts[]);
4958/**
4959 * @brief Destroy a memory domain.
4960 *
4961 * Destroy a memory domain.
4962 *
4963 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004964 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004965 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004966extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4967
4968/**
4969 * @brief Add a memory partition into a memory domain.
4970 *
Andrew Boiefddd5502019-07-30 18:07:32 -07004971 * Add a memory partition into a memory domain. Partitions must conform to
4972 * the following constraints:
4973 *
4974 * - Partition bounds must be within system RAM boundaries on MMU-based
4975 * systems.
4976 * - Partitions in the same memory domain may not overlap each other.
4977 * - Partitions must not be defined which expose private kernel
4978 * data structures or kernel objects.
4979 * - The starting address alignment, and the partition size must conform to
4980 * the constraints of the underlying memory management hardware, which
4981 * varies per architecture.
4982 * - Memory domain partitions are only intended to control access to memory
4983 * from user mode threads.
4984 *
4985 * Violating these constraints may lead to CPU exceptions or undefined
4986 * behavior.
Chunlin Hane9c97022017-07-07 20:29:30 +08004987 *
4988 * @param domain The memory domain to be added a memory partition.
4989 * @param part The memory partition to be added
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_add_partition(struct k_mem_domain *domain,
4993 struct k_mem_partition *part);
4994
4995/**
4996 * @brief Remove a memory partition from a memory domain.
4997 *
4998 * Remove a memory partition from a memory domain.
4999 *
5000 * @param domain The memory domain to be removed a memory partition.
5001 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005002 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005003 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005004extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
5005 struct k_mem_partition *part);
5006
5007/**
5008 * @brief Add a thread into a memory domain.
5009 *
5010 * Add a thread into a memory domain.
5011 *
5012 * @param domain The memory domain that the thread is going to be added into.
5013 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005014 *
5015 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005016 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005017extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
5018 k_tid_t thread);
5019
5020/**
5021 * @brief Remove a thread from its memory domain.
5022 *
5023 * Remove a thread from its memory domain.
5024 *
5025 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005026 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005027 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005028extern void k_mem_domain_remove_thread(k_tid_t thread);
5029
Anas Nashif166f5192018-02-25 08:02:36 -06005030/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08005031
Andrew Boie756f9072017-10-10 16:01:49 -07005032/**
5033 * @brief Emit a character buffer to the console device
5034 *
5035 * @param c String of characters to print
5036 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005037 *
5038 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07005039 */
5040__syscall void k_str_out(char *c, size_t n);
5041
Andy Rosse7172672018-01-24 15:48:32 -08005042/**
5043 * @brief Start a numbered CPU on a MP-capable system
5044
5045 * This starts and initializes a specific CPU. The main thread on
5046 * startup is running on CPU zero, other processors are numbered
5047 * sequentially. On return from this function, the CPU is known to
5048 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07005049 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08005050 * with the provided key will work to enable them.
5051 *
5052 * Normally, in SMP mode this function will be called by the kernel
5053 * initialization and should not be used as a user API. But it is
5054 * defined here for special-purpose apps which want Zephyr running on
5055 * one core and to use others for design-specific processing.
5056 *
5057 * @param cpu_num Integer number of the CPU
5058 * @param stack Stack memory for the CPU
5059 * @param sz Stack buffer size, in bytes
5060 * @param fn Function to begin running on the CPU. First argument is
5061 * an irq_unlock() key.
5062 * @param arg Untyped argument to be passed to "fn"
5063 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005064extern void z_arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08005065 void (*fn)(int key, void *data), void *arg);
Andy Rosse7172672018-01-24 15:48:32 -08005066
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005067
5068/**
5069 * @brief Disable preservation of floating point context information.
5070 *
5071 * This routine informs the kernel that the specified thread
5072 * will no longer be using the floating point registers.
5073 *
5074 * @warning
5075 * Some architectures apply restrictions on how the disabling of floating
5076 * point preservation may be requested, see z_arch_float_disable.
5077 *
5078 * @warning
5079 * This routine should only be used to disable floating point support for
5080 * a thread that currently has such support enabled.
5081 *
5082 * @param thread ID of thread.
5083 *
5084 * @retval 0 On success.
5085 * @retval -ENOSYS If the floating point disabling is not implemented.
5086 * -EINVAL If the floating point disabling could not be performed.
5087 */
5088__syscall int k_float_disable(struct k_thread *thread);
5089
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005090#ifdef __cplusplus
5091}
5092#endif
5093
Anas Nashif10291a02019-06-25 12:25:12 -04005094#include <debug/tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07005095#include <syscalls/kernel.h>
5096
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05005097#endif /* !_ASMLANGUAGE */
5098
Flavio Ceolin67ca1762018-09-14 10:43:44 -07005099#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */