blob: de4dd44fe22e58b01f62080417ec2fdb77367fa3 [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
Charles E. Youse3036faf2019-09-16 11:13:12 -040026#ifdef CONFIG_BOOT_TIME_MEASUREMENT
27extern u32_t __main_time_stamp; /* timestamp when main task starts */
28extern u32_t __idle_time_stamp; /* timestamp when CPU goes idle */
29#endif
30
Anas Nashifbbb157d2017-01-15 08:46:31 -050031/**
32 * @brief Kernel APIs
33 * @defgroup kernel_apis Kernel APIs
34 * @{
35 * @}
36 */
37
Anas Nashif61f4b242016-11-18 10:53:59 -050038#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040039#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
40#else
41#define K_DEBUG(fmt, ...)
42#endif
43
Benjamin Walsh2f280412017-01-14 19:23:46 -050044#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
45#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
46#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
47#elif defined(CONFIG_COOP_ENABLED)
48#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
49#define _NUM_PREEMPT_PRIO (0)
50#elif defined(CONFIG_PREEMPT_ENABLED)
51#define _NUM_COOP_PRIO (0)
52#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
53#else
54#error "invalid configuration"
55#endif
56
57#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040058#define K_PRIO_PREEMPT(x) (x)
59
Benjamin Walsh456c6da2016-09-02 18:55:39 -040060#define K_ANY NULL
61#define K_END NULL
62
Benjamin Walshedb35702017-01-14 18:47:22 -050063#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040064#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050065#elif defined(CONFIG_COOP_ENABLED)
66#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
67#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040068#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050069#else
70#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040071#endif
72
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050073#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040074#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
75#else
76#define K_LOWEST_THREAD_PRIO -1
77#endif
78
Benjamin Walshfab8d922016-11-08 15:36:36 -050079#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
80
Benjamin Walsh456c6da2016-09-02 18:55:39 -040081#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
82#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
83
Andy Ross225c74b2018-06-27 11:20:50 -070084#ifdef CONFIG_WAITQ_SCALABLE
Andy Ross1acd8c22018-05-03 14:51:49 -070085
86typedef struct {
87 struct _priq_rb waitq;
88} _wait_q_t;
89
Patrik Flykt4344e272019-03-08 14:19:05 -070090extern bool z_priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
Andy Ross1acd8c22018-05-03 14:51:49 -070091
Patrik Flykt4344e272019-03-08 14:19:05 -070092#define Z_WAIT_Q_INIT(wait_q) { { { .lessthan_fn = z_priq_rb_lessthan } } }
Andy Ross1acd8c22018-05-03 14:51:49 -070093
94#else
95
Andy Rossccf3bf72018-05-10 11:10:34 -070096typedef struct {
97 sys_dlist_t waitq;
98} _wait_q_t;
99
Patrik Flykt4344e272019-03-08 14:19:05 -0700100#define Z_WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400101
Andy Ross1acd8c22018-05-03 14:51:49 -0700102#endif
103
Anas Nashif2f203c22016-12-18 06:57:45 -0500104#ifdef CONFIG_OBJECT_TRACING
Flavio Ceolind1ed3362018-12-07 11:39:13 -0800105#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next;
Anas Nashif2f203c22016-12-18 06:57:45 -0500106#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400107#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500108#define _OBJECT_TRACING_INIT
Flavio Ceolind1ed3362018-12-07 11:39:13 -0800109#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400110#endif
111
Benjamin Walshacc68c12017-01-29 18:57:45 -0500112#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300113#define _POLL_EVENT_OBJ_INIT(obj) \
114 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
115#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500116#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300117#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500118#define _POLL_EVENT
119#endif
120
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500121struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122struct k_mutex;
123struct k_sem;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400124struct k_msgq;
125struct k_mbox;
126struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200127struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400128struct k_fifo;
129struct k_lifo;
130struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400131struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400132struct k_mem_pool;
133struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500134struct k_poll_event;
135struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800136struct k_mem_domain;
137struct k_mem_partition;
Wentong Wu5611e922019-06-20 23:51:27 +0800138struct k_futex;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400139
Andrew Boie5bd891d2017-09-27 12:59:28 -0700140/* This enumeration needs to be kept in sync with the lists of kernel objects
141 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
142 * function in kernel/userspace.c
143 */
Andrew Boie945af952017-08-22 13:15:23 -0700144enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700145 K_OBJ_ANY,
146
Leandro Pereirac2003672018-04-04 13:50:32 -0700147 /** @cond
148 * Doxygen should ignore this build-time generated include file
149 * when genrating API documentation. Enumeration values are
150 * generated during build by gen_kobject_list.py. It includes
151 * basic kernel objects (e.g. pipes and mutexes) and driver types.
152 */
153#include <kobj-types-enum.h>
154 /** @endcond
155 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700156
Andrew Boie945af952017-08-22 13:15:23 -0700157 K_OBJ_LAST
158};
Anas Nashif4bcb2942019-01-23 23:06:29 -0500159/**
160 * @defgroup usermode_apis User Mode APIs
161 * @ingroup kernel_apis
162 * @{
163 */
Andrew Boie945af952017-08-22 13:15:23 -0700164
165#ifdef CONFIG_USERSPACE
166/* Table generated by gperf, these objects are retrieved via
Patrik Flykt4344e272019-03-08 14:19:05 -0700167 * z_object_find() */
Andrew Boie945af952017-08-22 13:15:23 -0700168struct _k_object {
169 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700170 u8_t perms[CONFIG_MAX_THREAD_BYTES];
171 u8_t type;
172 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700173 u32_t data;
Andrew Boiedf555242018-05-25 07:28:54 -0700174} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700175
Andrew Boie877f82e2017-10-17 11:20:22 -0700176struct _k_object_assignment {
177 struct k_thread *thread;
178 void * const *objects;
179};
180
181/**
182 * @brief Grant a static thread access to a list of kernel objects
183 *
184 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
185 * a set of kernel objects. These objects do not need to be in an initialized
186 * state. The permissions will be granted when the threads are initialized
187 * in the early boot sequence.
188 *
189 * All arguments beyond the first must be pointers to kernel objects.
190 *
191 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
192 */
193#define K_THREAD_ACCESS_GRANT(name_, ...) \
194 static void * const _CONCAT(_object_list_, name_)[] = \
195 { __VA_ARGS__, NULL }; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400196 static const Z_STRUCT_SECTION_ITERABLE(_k_object_assignment, \
197 _CONCAT(_object_access_, name_)) = \
Andrew Boie877f82e2017-10-17 11:20:22 -0700198 { (&_k_thread_obj_ ## name_), \
199 (_CONCAT(_object_list_, name_)) }
200
Andrew Boie945af952017-08-22 13:15:23 -0700201#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700202#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700203#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie78757072019-07-23 13:29:30 -0700204#define K_OBJ_FLAG_DRIVER BIT(3)
Andrew Boie945af952017-08-22 13:15:23 -0700205
206/**
207 * Lookup a kernel object and init its metadata if it exists
208 *
209 * Calling this on an object will make it usable from userspace.
210 * Intended to be called as the last statement in kernel object init
211 * functions.
212 *
Anas Nashif50e3acd2018-12-08 13:26:18 -0500213 * @param obj Address of the kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700214 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700215void z_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700216#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700217/* LCOV_EXCL_START */
Andrew Boie877f82e2017-10-17 11:20:22 -0700218#define K_THREAD_ACCESS_GRANT(thread, ...)
219
Anas Nashif954d5502018-02-25 08:37:28 -0600220/**
221 * @internal
222 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700223static inline void z_object_init(void *obj)
Andrew Boie743e4682017-10-04 12:25:50 -0700224{
225 ARG_UNUSED(obj);
226}
227
Anas Nashif954d5502018-02-25 08:37:28 -0600228/**
229 * @internal
230 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700231static inline void z_impl_k_object_access_grant(void *object,
Andrew Boie743e4682017-10-04 12:25:50 -0700232 struct k_thread *thread)
233{
234 ARG_UNUSED(object);
235 ARG_UNUSED(thread);
236}
237
Anas Nashif954d5502018-02-25 08:37:28 -0600238/**
239 * @internal
240 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700241static inline void k_object_access_revoke(void *object,
242 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700243{
244 ARG_UNUSED(object);
245 ARG_UNUSED(thread);
246}
247
Andrew Boiee9cfc542018-04-13 13:15:28 -0700248/**
249 * @internal
250 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700251static inline void z_impl_k_object_release(void *object)
Andrew Boiee9cfc542018-04-13 13:15:28 -0700252{
253 ARG_UNUSED(object);
254}
255
Andrew Boie41bab6e2017-10-14 14:42:23 -0700256static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700257{
258 ARG_UNUSED(object);
259}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700260/* LCOV_EXCL_STOP */
Andrew Boie743e4682017-10-04 12:25:50 -0700261#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700262
263/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600264 * Grant a thread access to a kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700265 *
266 * The thread will be granted access to the object if the caller is from
267 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700268 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700269 *
270 * @param object Address of kernel object
271 * @param thread Thread to grant access to the object
272 */
Andrew Boie743e4682017-10-04 12:25:50 -0700273__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700274
Andrew Boiea89bf012017-10-09 14:47:55 -0700275/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600276 * Revoke a thread's access to a kernel object
Andrew Boiea89bf012017-10-09 14:47:55 -0700277 *
278 * The thread will lose access to the object if the caller is from
279 * supervisor mode, or the caller is from user mode AND has permissions
280 * on both the object and the thread whose access is being revoked.
281 *
282 * @param object Address of kernel object
283 * @param thread Thread to remove access to the object
284 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700285void k_object_access_revoke(void *object, struct k_thread *thread);
286
287
288__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700289
290/**
Marti Bolivar67db6162019-08-27 19:12:51 -0600291 * Grant all present and future threads access to an object
Andrew Boie3b5ae802017-10-04 12:10:32 -0700292 *
293 * If the caller is from supervisor mode, or the caller is from user mode and
294 * have sufficient permissions on the object, then that object will have
295 * permissions granted to it for *all* current and future threads running in
296 * the system, effectively becoming a public kernel object.
297 *
298 * Use of this API should be avoided on systems that are running untrusted code
299 * as it is possible for such code to derive the addresses of kernel objects
300 * and perform unwanted operations on them.
301 *
Andrew Boie04caa672017-10-13 13:57:07 -0700302 * It is not possible to revoke permissions on public objects; once public,
303 * any thread may use it.
304 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700305 * @param object Address of kernel object
306 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700307void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700308
Andrew Boie31bdfc02017-11-08 16:38:03 -0800309/**
310 * Allocate a kernel object of a designated type
311 *
312 * This will instantiate at runtime a kernel object of the specified type,
313 * returning a pointer to it. The object will be returned in an uninitialized
314 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700315 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800316 *
317 * Currently, allocation of thread stacks is not supported.
318 *
319 * @param otype Requested kernel object type
320 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
321 * available
322 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700323__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800324
Andrew Boie97bf0012018-04-24 17:01:37 -0700325#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800326/**
327 * Free a kernel object previously allocated with k_object_alloc()
328 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700329 * This will return memory for a kernel object back to resource pool it was
330 * allocated from. Care must be exercised that the object will not be used
331 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800332 *
333 * @param obj Pointer to the kernel object memory address.
334 */
335void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700336#else
Andrew Boiec3d4e652019-06-28 14:19:16 -0700337/* LCOV_EXCL_START */
Patrik Flykt4344e272019-03-08 14:19:05 -0700338static inline void *z_impl_k_object_alloc(enum k_objects otype)
Andrew Boie97bf0012018-04-24 17:01:37 -0700339{
Kumar Gala85699f72018-05-17 09:26:03 -0500340 ARG_UNUSED(otype);
341
Andrew Boie97bf0012018-04-24 17:01:37 -0700342 return NULL;
343}
344
345static inline void k_obj_free(void *obj)
346{
347 ARG_UNUSED(obj);
348}
Andrew Boiec3d4e652019-06-28 14:19:16 -0700349/* LCOV_EXCL_STOP */
Andrew Boie31bdfc02017-11-08 16:38:03 -0800350#endif /* CONFIG_DYNAMIC_OBJECTS */
351
Anas Nashif4bcb2942019-01-23 23:06:29 -0500352/** @} */
353
Andrew Boiebca15da2017-10-15 14:17:48 -0700354/* Using typedef deliberately here, this is quite intended to be an opaque
Andrew Boie4e5c0932019-04-04 12:05:28 -0700355 * type.
Andrew Boiebca15da2017-10-15 14:17:48 -0700356 *
357 * The purpose of this data type is to clearly distinguish between the
358 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
359 * buffer which composes the stack data actually used by the underlying
Anas Nashiff2cb20c2019-06-18 14:45:40 -0400360 * thread; they cannot be used interchangeably as some arches precede the
Andrew Boiebca15da2017-10-15 14:17:48 -0700361 * stack buffer region with guard areas that trigger a MPU or MMU fault
362 * if written to.
363 *
364 * APIs that want to work with the buffer inside should continue to use
365 * char *.
366 *
367 * Stacks should always be created with K_THREAD_STACK_DEFINE().
368 */
369struct __packed _k_thread_stack_element {
370 char data;
371};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700372typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700373
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700374/**
375 * @typedef k_thread_entry_t
376 * @brief Thread entry point function type.
377 *
378 * A thread's entry point function is invoked when the thread starts executing.
379 * Up to 3 argument values can be passed to the function.
380 *
381 * The thread terminates execution permanently if the entry point function
382 * returns. The thread is responsible for releasing any shared resources
383 * it may own (such as mutexes and dynamically allocated memory), prior to
384 * returning.
385 *
386 * @param p1 First argument.
387 * @param p2 Second argument.
388 * @param p3 Third argument.
389 *
390 * @return N/A
391 */
392typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700393
394#ifdef CONFIG_THREAD_MONITOR
395struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700396 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700397 void *parameter1;
398 void *parameter2;
399 void *parameter3;
400};
401#endif
402
403/* can be used for creating 'dummy' threads, e.g. for pending on objects */
404struct _thread_base {
405
406 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700407 union {
Peter A. Bigot82ad0d22019-01-03 23:49:28 -0600408 sys_dnode_t qnode_dlist;
Andy Ross1acd8c22018-05-03 14:51:49 -0700409 struct rbnode qnode_rb;
410 };
411
Andy Ross1acd8c22018-05-03 14:51:49 -0700412 /* wait queue on which the thread is pended (needed only for
413 * trees, not dumb lists)
414 */
415 _wait_q_t *pended_on;
Andrew Boie73abd322017-04-04 13:19:13 -0700416
417 /* user facing 'thread options'; values defined in include/kernel.h */
418 u8_t user_options;
419
420 /* thread state */
421 u8_t thread_state;
422
423 /*
424 * scheduler lock count and thread priority
425 *
426 * These two fields control the preemptibility of a thread.
427 *
428 * When the scheduler is locked, sched_locked is decremented, which
429 * means that the scheduler is locked for values from 0xff to 0x01. A
430 * thread is coop if its prio is negative, thus 0x80 to 0xff when
431 * looked at the value as unsigned.
432 *
433 * By putting them end-to-end, this means that a thread is
434 * non-preemptible if the bundled value is greater than or equal to
435 * 0x0080.
436 */
437 union {
438 struct {
439#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
440 u8_t sched_locked;
441 s8_t prio;
442#else /* LITTLE and PDP */
443 s8_t prio;
444 u8_t sched_locked;
445#endif
446 };
447 u16_t preempt;
448 };
449
Andy Ross4a2e50f2018-05-15 11:06:25 -0700450#ifdef CONFIG_SCHED_DEADLINE
451 int prio_deadline;
452#endif
453
Andy Ross1acd8c22018-05-03 14:51:49 -0700454 u32_t order_key;
455
Andy Ross2724fd12018-01-29 14:55:20 -0800456#ifdef CONFIG_SMP
457 /* True for the per-CPU idle threads */
458 u8_t is_idle;
459
Andy Ross2724fd12018-01-29 14:55:20 -0800460 /* CPU index on which thread was last run */
461 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700462
463 /* Recursive count of irq_lock() calls */
464 u8_t global_lock_count;
Andy Rossab46b1b2019-01-30 15:00:42 -0800465
466#endif
467
468#ifdef CONFIG_SCHED_CPU_MASK
469 /* "May run on" bits for each CPU */
470 u8_t cpu_mask;
Andy Ross2724fd12018-01-29 14:55:20 -0800471#endif
472
Andrew Boie73abd322017-04-04 13:19:13 -0700473 /* data returned by APIs */
474 void *swap_data;
475
476#ifdef CONFIG_SYS_CLOCK_EXISTS
477 /* this thread's entry in a timeout queue */
478 struct _timeout timeout;
479#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700480};
481
482typedef struct _thread_base _thread_base_t;
483
484#if defined(CONFIG_THREAD_STACK_INFO)
485/* Contains the stack information of a thread */
486struct _thread_stack_info {
Andrew Boie4e5c0932019-04-04 12:05:28 -0700487 /* Stack start - Represents the start address of the thread-writable
488 * stack area.
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700489 */
Nicolas Pitre58d839b2019-05-21 11:32:21 -0400490 uintptr_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700491
492 /* Stack Size - Thread writable stack buffer size. Represents
493 * the size of the actual area, starting from the start member,
494 * that should be writable by the thread
495 */
Andrew Boie73abd322017-04-04 13:19:13 -0700496 u32_t size;
497};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700498
499typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700500#endif /* CONFIG_THREAD_STACK_INFO */
501
Chunlin Hane9c97022017-07-07 20:29:30 +0800502#if defined(CONFIG_USERSPACE)
503struct _mem_domain_info {
504 /* memory domain queue node */
505 sys_dnode_t mem_domain_q_node;
506 /* memory domain of the thread */
507 struct k_mem_domain *mem_domain;
508};
509
510#endif /* CONFIG_USERSPACE */
511
Daniel Leungfc182432018-08-16 15:42:28 -0700512#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
513struct _thread_userspace_local_data {
514 int errno_var;
515};
516#endif
517
Anas Nashifce78d162018-05-24 12:43:11 -0500518/**
519 * @ingroup thread_apis
520 * Thread Structure
521 */
Andrew Boie73abd322017-04-04 13:19:13 -0700522struct k_thread {
523
524 struct _thread_base base;
525
Anas Nashifce78d162018-05-24 12:43:11 -0500526 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700527 struct _callee_saved callee_saved;
528
Anas Nashifce78d162018-05-24 12:43:11 -0500529 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700530 void *init_data;
531
Anas Nashifce78d162018-05-24 12:43:11 -0500532 /**
533 * abort function
534 * @req K-THREAD-002
535 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700536 void (*fn_abort)(void);
537
538#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500539 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700540 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700541
Anas Nashifce78d162018-05-24 12:43:11 -0500542 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700543 struct k_thread *next_thread;
544#endif
545
Anas Nashif57554052018-03-03 02:31:05 -0600546#if defined(CONFIG_THREAD_NAME)
547 /* Thread name */
Andrew Boie38129ce2019-06-25 08:54:37 -0700548 char name[CONFIG_THREAD_MAX_NAME_LEN];
Anas Nashif57554052018-03-03 02:31:05 -0600549#endif
550
Andrew Boie73abd322017-04-04 13:19:13 -0700551#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500552 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700553 void *custom_data;
554#endif
555
Daniel Leungfc182432018-08-16 15:42:28 -0700556#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
557 struct _thread_userspace_local_data *userspace_local_data;
558#endif
559
Andrew Boie73abd322017-04-04 13:19:13 -0700560#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700561#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500562 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700563 int errno_var;
564#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700565#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700566
567#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500568 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700569 struct _thread_stack_info stack_info;
570#endif /* CONFIG_THREAD_STACK_INFO */
571
Chunlin Hane9c97022017-07-07 20:29:30 +0800572#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500573 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800574 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500575 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700576 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800577#endif /* CONFIG_USERSPACE */
578
Andy Ross042d8ec2017-12-09 08:37:20 -0800579#if defined(CONFIG_USE_SWITCH)
580 /* When using __switch() a few previously arch-specific items
581 * become part of the core OS
582 */
583
Patrik Flykt4344e272019-03-08 14:19:05 -0700584 /** z_swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800585 int swap_retval;
586
Patrik Flykt7c0a2452019-03-14 09:20:46 -0600587 /** Context handle returned via z_arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800588 void *switch_handle;
589#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500590 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700591 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800592
Anas Nashifce78d162018-05-24 12:43:11 -0500593 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700594 struct _thread_arch arch;
595};
596
597typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400598typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400599
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400600enum execution_context_types {
601 K_ISR = 0,
602 K_COOP_THREAD,
603 K_PREEMPT_THREAD,
604};
605
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400606/**
Anas Nashif4bcb2942019-01-23 23:06:29 -0500607 * @addtogroup thread_apis
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100608 * @{
609 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530610typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
611 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100612
613/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530614 * @brief Iterate over all the threads in the system.
615 *
616 * This routine iterates over all the threads in the system and
617 * calls the user_cb function for each thread.
618 *
619 * @param user_cb Pointer to the user callback function.
620 * @param user_data Pointer to user data.
621 *
622 * @note CONFIG_THREAD_MONITOR must be set for this function
623 * to be effective. Also this API uses irq_lock to protect the
624 * _kernel.threads list which means creation of new threads and
625 * terminations of existing threads are blocked until this
626 * API returns.
627 *
628 * @return N/A
629 */
630extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
631
Anas Nashif166f5192018-02-25 08:02:36 -0600632/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100633
634/**
Allan Stephensc98da842016-11-11 15:45:03 -0500635 * @defgroup thread_apis Thread APIs
636 * @ingroup kernel_apis
637 * @{
638 */
639
Benjamin Walshed240f22017-01-22 13:05:08 -0500640#endif /* !_ASMLANGUAGE */
641
642
643/*
644 * Thread user options. May be needed by assembly code. Common part uses low
645 * bits, arch-specific use high bits.
646 */
647
Anas Nashifa541e932018-05-24 11:19:16 -0500648/**
649 * @brief system thread that must not abort
650 * @req K-THREAD-000
651 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700652#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500653
654#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500655/**
656 * @brief thread uses floating point registers
657 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700658#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500659#endif
660
Anas Nashifa541e932018-05-24 11:19:16 -0500661/**
662 * @brief user mode thread
663 *
664 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700665 * has additional restrictions
666 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700667#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700668
Anas Nashifa541e932018-05-24 11:19:16 -0500669/**
670 * @brief Inherit Permissions
671 *
672 * @details
673 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700674 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
675 * is not enabled.
676 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700677#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700678
Benjamin Walshed240f22017-01-22 13:05:08 -0500679#ifdef CONFIG_X86
680/* x86 Bitmask definitions for threads user options */
681
682#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
683/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700684#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500685#endif
686#endif
687
688/* end - thread options */
689
690#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400691/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700692 * @brief Create a thread.
693 *
694 * This routine initializes a thread, then schedules it for execution.
695 *
696 * The new thread may be scheduled for immediate execution or a delayed start.
697 * If the newly spawned thread does not have a delayed start the kernel
698 * scheduler may preempt the current thread to allow the new thread to
699 * execute.
700 *
701 * Thread options are architecture-specific, and can include K_ESSENTIAL,
702 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
703 * them using "|" (the logical OR operator).
704 *
705 * Historically, users often would use the beginning of the stack memory region
706 * to store the struct k_thread data, although corruption will occur if the
707 * stack overflows this region and stack protection features may not detect this
708 * situation.
709 *
710 * @param new_thread Pointer to uninitialized struct k_thread
711 * @param stack Pointer to the stack space.
712 * @param stack_size Stack size in bytes.
713 * @param entry Thread entry function.
714 * @param p1 1st entry point parameter.
715 * @param p2 2nd entry point parameter.
716 * @param p3 3rd entry point parameter.
717 * @param prio Thread priority.
718 * @param options Thread options.
719 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
720 *
721 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400722 *
723 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700724 */
Andrew Boie662c3452017-10-02 10:51:18 -0700725__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700726 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700727 size_t stack_size,
728 k_thread_entry_t entry,
729 void *p1, void *p2, void *p3,
730 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700731
Andrew Boie3f091b52017-08-30 14:34:14 -0700732/**
733 * @brief Drop a thread's privileges permanently to user mode
734 *
735 * @param entry Function to start executing from
736 * @param p1 1st entry point parameter
737 * @param p2 2nd entry point parameter
738 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400739 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700740 */
741extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
742 void *p1, void *p2,
743 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700744
Andrew Boied26cf2d2017-03-30 13:07:02 -0700745/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530746 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700747 *
748 * This is a convenience function. For the provided thread, grant access to
749 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700750 *
751 * The thread object must be initialized (i.e. running). The objects don't
752 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530753 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700754 *
755 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530756 * @param ... list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400757 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700758 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530759#define k_thread_access_grant(thread, ...) \
760 FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700761
762/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700763 * @brief Assign a resource memory pool to a thread
764 *
765 * By default, threads have no resource pool assigned unless their parent
766 * thread has a resource pool, in which case it is inherited. Multiple
767 * threads may be assigned to the same memory pool.
768 *
769 * Changing a thread's resource pool will not migrate allocations from the
770 * previous pool.
771 *
772 * @param thread Target thread to assign a memory pool for resource requests,
773 * or NULL if the thread should no longer have a memory pool.
774 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400775 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700776 */
777static inline void k_thread_resource_pool_assign(struct k_thread *thread,
778 struct k_mem_pool *pool)
779{
780 thread->resource_pool = pool;
781}
782
783#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
784/**
785 * @brief Assign the system heap as a thread's resource pool
786 *
787 * Similar to k_thread_resource_pool_assign(), but the thread will use
788 * the kernel heap to draw memory.
789 *
790 * Use with caution, as a malicious thread could perform DoS attacks on the
791 * kernel heap.
792 *
793 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400794 *
795 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700796 */
797void k_thread_system_pool_assign(struct k_thread *thread);
798#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
799
800/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500801 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400802 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700803 * This routine puts the current thread to sleep for @a duration milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400804 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700805 * @param ms Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400806 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200807 * @return Zero if the requested time has elapsed or the number of milliseconds
808 * left to sleep, if thread was woken up by \ref k_wakeup call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400809 */
Charles E. Yousea5678312019-05-09 16:46:46 -0700810__syscall s32_t k_sleep(s32_t ms);
811
812/**
813 * @brief Put the current thread to sleep with microsecond resolution.
814 *
815 * This function is unlikely to work as expected without kernel tuning.
816 * In particular, because the lower bound on the duration of a sleep is
817 * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted
818 * to achieve the resolution desired. The implications of doing this must
819 * be understood before attempting to use k_usleep(). Use with caution.
820 *
821 * @param us Number of microseconds to sleep.
822 *
823 * @return Zero if the requested time has elapsed or the number of microseconds
824 * left to sleep, if thread was woken up by \ref k_wakeup call.
825 */
826__syscall s32_t k_usleep(s32_t us);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827
828/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500829 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830 *
831 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500832 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400833 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400834 * @return N/A
835 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800836__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400837
838/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500839 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500841 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400842 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500843 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844 *
845 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400846 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400847 */
Andrew Boie468190a2017-09-29 14:00:48 -0700848__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400849
850/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500851 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500853 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500855 * If @a thread is not currently sleeping, the routine has no effect.
856 *
857 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400858 *
859 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400860 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400861 */
Andrew Boie468190a2017-09-29 14:00:48 -0700862__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400863
864/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500865 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500867 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400868 *
869 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400870 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700871__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400872
873/**
Allan Stephensc98da842016-11-11 15:45:03 -0500874 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500876 * This routine permanently stops execution of @a thread. The thread is taken
877 * off all kernel queues it is part of (i.e. the ready queue, the timeout
878 * queue, or a kernel object wait queue). However, any kernel resources the
879 * thread might currently own (such as mutexes or memory blocks) are not
880 * released. It is the responsibility of the caller of this routine to ensure
881 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500883 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400884 *
885 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400886 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400887 */
Andrew Boie468190a2017-09-29 14:00:48 -0700888__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400889
Andrew Boie7d627c52017-08-30 11:01:56 -0700890
891/**
892 * @brief Start an inactive thread
893 *
894 * If a thread was created with K_FOREVER in the delay parameter, it will
895 * not be added to the scheduling queue until this function is called
896 * on it.
897 *
898 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400899 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700900 */
Andrew Boie468190a2017-09-29 14:00:48 -0700901__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700902
Allan Stephensc98da842016-11-11 15:45:03 -0500903/**
904 * @cond INTERNAL_HIDDEN
905 */
906
Benjamin Walshd211a522016-12-06 11:44:01 -0500907/* timeout has timed out and is not on _timeout_q anymore */
908#define _EXPIRED (-2)
909
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400910struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700911 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700912 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400913 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700914 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500915 void *init_p1;
916 void *init_p2;
917 void *init_p3;
918 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500919 u32_t init_options;
920 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500921 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600922 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400923};
924
Andrew Boied26cf2d2017-03-30 13:07:02 -0700925#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400926 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600927 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500928 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700929 .init_thread = (thread), \
930 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500931 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700932 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400933 .init_p1 = (void *)p1, \
934 .init_p2 = (void *)p2, \
935 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500936 .init_prio = (prio), \
937 .init_options = (options), \
938 .init_delay = (delay), \
939 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600940 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400941 }
942
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400943/**
Allan Stephensc98da842016-11-11 15:45:03 -0500944 * INTERNAL_HIDDEN @endcond
945 */
946
947/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500948 * @brief Statically define and initialize a thread.
949 *
950 * The thread may be scheduled for immediate execution or a delayed start.
951 *
952 * Thread options are architecture-specific, and can include K_ESSENTIAL,
953 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
954 * them using "|" (the logical OR operator).
955 *
956 * The ID of the thread can be accessed using:
957 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500958 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500959 *
960 * @param name Name of the thread.
961 * @param stack_size Stack size in bytes.
962 * @param entry Thread entry function.
963 * @param p1 1st entry point parameter.
964 * @param p2 2nd entry point parameter.
965 * @param p3 3rd entry point parameter.
966 * @param prio Thread priority.
967 * @param options Thread options.
968 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400969 *
Anas Nashif47420d02018-05-24 14:20:56 -0400970 * @req K-THREAD-010
971 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400972 * @internal It has been observed that the x86 compiler by default aligns
973 * these _static_thread_data structures to 32-byte boundaries, thereby
974 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400975 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400976 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500977#define K_THREAD_DEFINE(name, stack_size, \
978 entry, p1, p2, p3, \
979 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700980 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Nicolas Pitreb1d37422019-06-03 10:51:32 -0400981 struct k_thread _k_thread_obj_##name; \
982 Z_STRUCT_SECTION_ITERABLE(_static_thread_data, _k_thread_data_##name) =\
Andrew Boied26cf2d2017-03-30 13:07:02 -0700983 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
984 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500985 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -0600986 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700987 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400988
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400989/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500990 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400991 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500992 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500994 * @param thread ID of thread whose priority is needed.
995 *
996 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400997 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400998 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700999__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001000
1001/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001002 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001004 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001005 *
1006 * Rescheduling can occur immediately depending on the priority @a thread is
1007 * set to:
1008 *
1009 * - If its priority is raised above the priority of the caller of this
1010 * function, and the caller is preemptible, @a thread will be scheduled in.
1011 *
1012 * - If the caller operates on itself, it lowers its priority below that of
1013 * other threads in the system, and the caller is preemptible, the thread of
1014 * highest priority will be scheduled in.
1015 *
1016 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1017 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1018 * highest priority.
1019 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001020 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001021 * @param prio New priority.
1022 *
1023 * @warning Changing the priority of a thread currently involved in mutex
1024 * priority inheritance may result in undefined behavior.
1025 *
1026 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001027 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001028 */
Andrew Boie468190a2017-09-29 14:00:48 -07001029__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001030
Andy Ross4a2e50f2018-05-15 11:06:25 -07001031
1032#ifdef CONFIG_SCHED_DEADLINE
1033/**
1034 * @brief Set deadline expiration time for scheduler
1035 *
1036 * This sets the "deadline" expiration as a time delta from the
1037 * current time, in the same units used by k_cycle_get_32(). The
1038 * scheduler (when deadline scheduling is enabled) will choose the
1039 * next expiring thread when selecting between threads at the same
1040 * static priority. Threads at different priorities will be scheduled
1041 * according to their static priority.
1042 *
1043 * @note Deadlines that are negative (i.e. in the past) are still seen
1044 * as higher priority than others, even if the thread has "finished"
1045 * its work. If you don't want it scheduled anymore, you have to
1046 * reset the deadline into the future, block/pend the thread, or
1047 * modify its priority with k_thread_priority_set().
1048 *
1049 * @note Despite the API naming, the scheduler makes no guarantees the
1050 * the thread WILL be scheduled within that deadline, nor does it take
1051 * extra metadata (like e.g. the "runtime" and "period" parameters in
1052 * Linux sched_setattr()) that allows the kernel to validate the
1053 * scheduling for achievability. Such features could be implemented
1054 * above this call, which is simply input to the priority selection
1055 * logic.
1056 *
Anas Nashif240c5162019-06-10 12:25:50 -04001057 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001058 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001059 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1060 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001061 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001062 *
Andy Ross4a2e50f2018-05-15 11:06:25 -07001063 * @param thread A thread on which to set the deadline
1064 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001065 *
1066 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001067 */
1068__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1069#endif
1070
Andy Rossab46b1b2019-01-30 15:00:42 -08001071#ifdef CONFIG_SCHED_CPU_MASK
1072/**
1073 * @brief Sets all CPU enable masks to zero
1074 *
1075 * After this returns, the thread will no longer be schedulable on any
1076 * CPUs. The thread must not be currently runnable.
1077 *
Anas Nashif240c5162019-06-10 12:25:50 -04001078 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001079 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001080 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1081 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001082 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001083 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001084 * @param thread Thread to operate upon
1085 * @return Zero on success, otherwise error code
1086 */
1087int k_thread_cpu_mask_clear(k_tid_t thread);
1088
1089/**
1090 * @brief Sets all CPU enable masks to one
1091 *
1092 * After this returns, the thread will be schedulable on any CPU. The
1093 * thread must not be currently runnable.
1094 *
Anas Nashif240c5162019-06-10 12:25:50 -04001095 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001096 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001097 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1098 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001099 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001100 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001101 * @param thread Thread to operate upon
1102 * @return Zero on success, otherwise error code
1103 */
1104int k_thread_cpu_mask_enable_all(k_tid_t thread);
1105
1106/**
1107 * @brief Enable thread to run on specified CPU
1108 *
1109 * The thread must not be currently runnable.
1110 *
Anas Nashif240c5162019-06-10 12:25:50 -04001111 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001112 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001113 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1114 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001115 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001116 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001117 * @param thread Thread to operate upon
1118 * @param cpu CPU index
1119 * @return Zero on success, otherwise error code
1120 */
1121int k_thread_cpu_mask_enable(k_tid_t thread, int cpu);
1122
1123/**
1124 * @brief Prevent thread to run on specified CPU
1125 *
1126 * The thread must not be currently runnable.
1127 *
Anas Nashif240c5162019-06-10 12:25:50 -04001128 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001129 * @rst
Anas Nashif240c5162019-06-10 12:25:50 -04001130 * You should enable :option:`CONFIG_SCHED_DEADLINE` in your project
1131 * configuration.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001132 * @endrst
Anas Nashif240c5162019-06-10 12:25:50 -04001133 *
Andy Rossab46b1b2019-01-30 15:00:42 -08001134 * @param thread Thread to operate upon
1135 * @param cpu CPU index
1136 * @return Zero on success, otherwise error code
1137 */
1138int k_thread_cpu_mask_disable(k_tid_t thread, int cpu);
1139#endif
1140
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001141/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001142 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001144 * This routine prevents the kernel scheduler from making @a thread the
1145 * current thread. All other internal operations on @a thread are still
1146 * performed; for example, any timeout it is waiting on keeps ticking,
1147 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001148 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001149 * If @a thread is already suspended, the routine has no effect.
1150 *
1151 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001152 *
1153 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001154 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001155 */
Andrew Boie468190a2017-09-29 14:00:48 -07001156__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001157
1158/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001159 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001160 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001161 * This routine allows the kernel scheduler to make @a thread the current
1162 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001164 * If @a thread is not currently suspended, the routine has no effect.
1165 *
1166 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001167 *
1168 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001169 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001170 */
Andrew Boie468190a2017-09-29 14:00:48 -07001171__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001172
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001173/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001174 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001175 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001176 * This routine specifies how the scheduler will perform time slicing of
1177 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001179 * To enable time slicing, @a slice must be non-zero. The scheduler
1180 * ensures that no thread runs for more than the specified time limit
1181 * before other threads of that priority are given a chance to execute.
1182 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001183 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001184 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001185 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001186 * execute. Once the scheduler selects a thread for execution, there is no
1187 * minimum guaranteed time the thread will execute before threads of greater or
1188 * equal priority are scheduled.
1189 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001190 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001191 * for execution, this routine has no effect; the thread is immediately
1192 * rescheduled after the slice period expires.
1193 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001194 * To disable timeslicing, set both @a slice and @a prio to zero.
1195 *
1196 * @param slice Maximum time slice length (in milliseconds).
1197 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001198 *
1199 * @return N/A
1200 */
Kumar Galacc334c72017-04-21 10:55:34 -05001201extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001202
Anas Nashif166f5192018-02-25 08:02:36 -06001203/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001204
1205/**
1206 * @addtogroup isr_apis
1207 * @{
1208 */
1209
1210/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001211 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001212 *
Allan Stephensc98da842016-11-11 15:45:03 -05001213 * This routine allows the caller to customize its actions, depending on
1214 * whether it is a thread or an ISR.
1215 *
1216 * @note Can be called by ISRs.
1217 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001218 * @return false if invoked by a thread.
1219 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001220 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001221extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001222
Benjamin Walsh445830d2016-11-10 15:54:27 -05001223/**
1224 * @brief Determine if code is running in a preemptible thread.
1225 *
Allan Stephensc98da842016-11-11 15:45:03 -05001226 * This routine allows the caller to customize its actions, depending on
1227 * whether it can be preempted by another thread. The routine returns a 'true'
1228 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001229 *
Allan Stephensc98da842016-11-11 15:45:03 -05001230 * - The code is running in a thread, not at ISR.
1231 * - The thread's priority is in the preemptible range.
1232 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001233 *
Allan Stephensc98da842016-11-11 15:45:03 -05001234 * @note Can be called by ISRs.
1235 *
1236 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001237 * @return Non-zero if invoked by a preemptible thread.
1238 */
Andrew Boie468190a2017-09-29 14:00:48 -07001239__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001240
Allan Stephensc98da842016-11-11 15:45:03 -05001241/**
Anas Nashif166f5192018-02-25 08:02:36 -06001242 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001243 */
1244
1245/**
1246 * @addtogroup thread_apis
1247 * @{
1248 */
1249
1250/**
1251 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001252 *
Allan Stephensc98da842016-11-11 15:45:03 -05001253 * This routine prevents the current thread from being preempted by another
1254 * thread by instructing the scheduler to treat it as a cooperative thread.
1255 * If the thread subsequently performs an operation that makes it unready,
1256 * it will be context switched out in the normal manner. When the thread
1257 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001258 *
Allan Stephensc98da842016-11-11 15:45:03 -05001259 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001260 *
Allan Stephensc98da842016-11-11 15:45:03 -05001261 * @note k_sched_lock() and k_sched_unlock() should normally be used
1262 * when the operation being performed can be safely interrupted by ISRs.
1263 * However, if the amount of processing involved is very small, better
1264 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001265 *
1266 * @return N/A
1267 */
1268extern void k_sched_lock(void);
1269
Allan Stephensc98da842016-11-11 15:45:03 -05001270/**
1271 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001272 *
Allan Stephensc98da842016-11-11 15:45:03 -05001273 * This routine reverses the effect of a previous call to k_sched_lock().
1274 * A thread must call the routine once for each time it called k_sched_lock()
1275 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001276 *
1277 * @return N/A
1278 */
1279extern void k_sched_unlock(void);
1280
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001281/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001282 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001283 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001284 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001285 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001286 * Custom data is not used by the kernel itself, and is freely available
1287 * for a thread to use as it sees fit. It can be used as a framework
1288 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001290 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001291 *
1292 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001293 *
1294 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001295 */
Andrew Boie468190a2017-09-29 14:00:48 -07001296__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001297
1298/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001299 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001300 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001301 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001302 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001303 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001304 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001305 */
Andrew Boie468190a2017-09-29 14:00:48 -07001306__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001307
1308/**
Anas Nashif57554052018-03-03 02:31:05 -06001309 * @brief Set current thread name
1310 *
1311 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1312 * tracing and debugging.
1313 *
Andrew Boie38129ce2019-06-25 08:54:37 -07001314 * @param thread_id Thread to set name, or NULL to set the current thread
1315 * @param value Name string
1316 * @retval 0 on success
1317 * @retval -EFAULT Memory access error with supplied string
1318 * @retval -ENOSYS Thread name configuration option not enabled
1319 * @retval -EINVAL Thread name too long
Anas Nashif57554052018-03-03 02:31:05 -06001320 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001321__syscall int k_thread_name_set(k_tid_t thread_id, const char *value);
Anas Nashif57554052018-03-03 02:31:05 -06001322
1323/**
1324 * @brief Get thread name
1325 *
1326 * Get the name of a thread
1327 *
1328 * @param thread_id Thread ID
Andrew Boie38129ce2019-06-25 08:54:37 -07001329 * @retval Thread name, or NULL if configuration not enabled
Anas Nashif57554052018-03-03 02:31:05 -06001330 */
Andrew Boie38129ce2019-06-25 08:54:37 -07001331const char *k_thread_name_get(k_tid_t thread_id);
1332
1333/**
1334 * @brief Copy the thread name into a supplied buffer
1335 *
1336 * @param thread_id Thread to obtain name information
1337 * @param buf Destination buffer
1338 * @param size Destinatiomn buffer size
1339 * @retval -ENOSPC Destination buffer too small
1340 * @retval -EFAULT Memory access error
1341 * @retval -ENOSYS Thread name feature not enabled
1342 * @retval 0 Success
1343 */
1344__syscall int k_thread_name_copy(k_tid_t thread_id, char *buf,
1345 size_t size);
Anas Nashif57554052018-03-03 02:31:05 -06001346
1347/**
Pavlo Hamov8076c802019-07-31 12:43:54 +03001348 * @brief Get thread state string
1349 *
1350 * Get the human friendly thread state string
1351 *
1352 * @param thread_id Thread ID
1353 * @retval Thread state string, empty if no state flag is set
1354 */
1355const char *k_thread_state_str(k_tid_t thread_id);
1356
1357/**
Andy Rosscfe62032018-09-29 07:34:55 -07001358 * @}
1359 */
1360
1361/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001362 * @addtogroup clock_apis
1363 * @{
1364 */
1365
1366/**
1367 * @brief Generate null timeout delay.
1368 *
1369 * This macro generates a timeout delay that that instructs a kernel API
1370 * not to wait if the requested operation cannot be performed immediately.
1371 *
1372 * @return Timeout delay value.
1373 */
1374#define K_NO_WAIT 0
1375
1376/**
1377 * @brief Generate timeout delay from milliseconds.
1378 *
1379 * This macro generates a timeout delay that that instructs a kernel API
1380 * to wait up to @a ms milliseconds to perform the requested operation.
1381 *
1382 * @param ms Duration in milliseconds.
1383 *
1384 * @return Timeout delay value.
1385 */
Johan Hedberg14471692016-11-13 10:52:15 +02001386#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001387
1388/**
1389 * @brief Generate timeout delay from seconds.
1390 *
1391 * This macro generates a timeout delay that that instructs a kernel API
1392 * to wait up to @a s seconds to perform the requested operation.
1393 *
1394 * @param s Duration in seconds.
1395 *
1396 * @return Timeout delay value.
1397 */
Johan Hedberg14471692016-11-13 10:52:15 +02001398#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001399
1400/**
1401 * @brief Generate timeout delay from minutes.
1402 *
1403 * This macro generates a timeout delay that that instructs a kernel API
1404 * to wait up to @a m minutes to perform the requested operation.
1405 *
1406 * @param m Duration in minutes.
1407 *
1408 * @return Timeout delay value.
1409 */
Johan Hedberg14471692016-11-13 10:52:15 +02001410#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001411
1412/**
1413 * @brief Generate timeout delay from hours.
1414 *
1415 * This macro generates a timeout delay that that instructs a kernel API
1416 * to wait up to @a h hours to perform the requested operation.
1417 *
1418 * @param h Duration in hours.
1419 *
1420 * @return Timeout delay value.
1421 */
Johan Hedberg14471692016-11-13 10:52:15 +02001422#define K_HOURS(h) K_MINUTES((h) * 60)
1423
Allan Stephensc98da842016-11-11 15:45:03 -05001424/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001425 * @brief Generate infinite timeout delay.
1426 *
1427 * This macro generates a timeout delay that that instructs a kernel API
1428 * to wait as long as necessary to perform the requested operation.
1429 *
1430 * @return Timeout delay value.
1431 */
1432#define K_FOREVER (-1)
1433
1434/**
Anas Nashif166f5192018-02-25 08:02:36 -06001435 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001436 */
1437
1438/**
Allan Stephensc98da842016-11-11 15:45:03 -05001439 * @cond INTERNAL_HIDDEN
1440 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001441
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001442struct k_timer {
1443 /*
1444 * _timeout structure must be first here if we want to use
1445 * dynamic timer allocation. timeout.node is used in the double-linked
1446 * list of free timers
1447 */
1448 struct _timeout timeout;
1449
Allan Stephens45bfa372016-10-12 12:39:42 -05001450 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001451 _wait_q_t wait_q;
1452
1453 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001454 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001455
1456 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001457 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001458
1459 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001460 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001461
Allan Stephens45bfa372016-10-12 12:39:42 -05001462 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001463 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001464
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001465 /* user-specific data, also used to support legacy features */
1466 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001467
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001468 _OBJECT_TRACING_NEXT_PTR(k_timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001469};
1470
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001471#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001472 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001473 .timeout = { \
1474 .node = {},\
1475 .dticks = 0, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001476 .fn = z_timer_expiration_handler \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001477 }, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001478 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001479 .expiry_fn = expiry, \
1480 .stop_fn = stop, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001481 .period = 0, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001482 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001483 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001484 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001485 }
1486
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001487#define K_TIMER_INITIALIZER DEPRECATED_MACRO Z_TIMER_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001488
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001489/**
Allan Stephensc98da842016-11-11 15:45:03 -05001490 * INTERNAL_HIDDEN @endcond
1491 */
1492
1493/**
1494 * @defgroup timer_apis Timer APIs
1495 * @ingroup kernel_apis
1496 * @{
1497 */
1498
1499/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001500 * @typedef k_timer_expiry_t
1501 * @brief Timer expiry function type.
1502 *
1503 * A timer's expiry function is executed by the system clock interrupt handler
1504 * each time the timer expires. The expiry function is optional, and is only
1505 * invoked if the timer has been initialized with one.
1506 *
1507 * @param timer Address of timer.
1508 *
1509 * @return N/A
1510 */
1511typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1512
1513/**
1514 * @typedef k_timer_stop_t
1515 * @brief Timer stop function type.
1516 *
1517 * A timer's stop function is executed if the timer is stopped prematurely.
1518 * The function runs in the context of the thread that stops the timer.
1519 * The stop function is optional, and is only invoked if the timer has been
1520 * initialized with one.
1521 *
1522 * @param timer Address of timer.
1523 *
1524 * @return N/A
1525 */
1526typedef void (*k_timer_stop_t)(struct k_timer *timer);
1527
1528/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001529 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001531 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001532 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001533 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001534 *
1535 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001536 * @param expiry_fn Function to invoke each time the timer expires.
1537 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001538 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001539#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04001540 Z_STRUCT_SECTION_ITERABLE(k_timer, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001541 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001542
Allan Stephens45bfa372016-10-12 12:39:42 -05001543/**
1544 * @brief Initialize a timer.
1545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001546 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001547 *
1548 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001549 * @param expiry_fn Function to invoke each time the timer expires.
1550 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001551 *
1552 * @return N/A
1553 */
1554extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001555 k_timer_expiry_t expiry_fn,
1556 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001557
Allan Stephens45bfa372016-10-12 12:39:42 -05001558/**
1559 * @brief Start a timer.
1560 *
1561 * This routine starts a timer, and resets its status to zero. The timer
1562 * begins counting down using the specified duration and period values.
1563 *
1564 * Attempting to start a timer that is already running is permitted.
1565 * The timer's status is reset to zero and the timer begins counting down
1566 * using the new duration and period values.
1567 *
1568 * @param timer Address of timer.
1569 * @param duration Initial timer duration (in milliseconds).
1570 * @param period Timer period (in milliseconds).
1571 *
1572 * @return N/A
1573 */
Andrew Boiea354d492017-09-29 16:22:28 -07001574__syscall void k_timer_start(struct k_timer *timer,
1575 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001576
1577/**
1578 * @brief Stop a timer.
1579 *
1580 * This routine stops a running timer prematurely. The timer's stop function,
1581 * if one exists, is invoked by the caller.
1582 *
1583 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001584 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001585 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001586 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1587 * if @a k_timer_stop is to be called from ISRs.
1588 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001589 * @param timer Address of timer.
1590 *
1591 * @return N/A
1592 */
Andrew Boiea354d492017-09-29 16:22:28 -07001593__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001594
1595/**
1596 * @brief Read timer status.
1597 *
1598 * This routine reads the timer's status, which indicates the number of times
1599 * it has expired since its status was last read.
1600 *
1601 * Calling this routine resets the timer's status to zero.
1602 *
1603 * @param timer Address of timer.
1604 *
1605 * @return Timer status.
1606 */
Andrew Boiea354d492017-09-29 16:22:28 -07001607__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001608
1609/**
1610 * @brief Synchronize thread to timer expiration.
1611 *
1612 * This routine blocks the calling thread until the timer's status is non-zero
1613 * (indicating that it has expired at least once since it was last examined)
1614 * or the timer is stopped. If the timer status is already non-zero,
1615 * or the timer is already stopped, the caller continues without waiting.
1616 *
1617 * Calling this routine resets the timer's status to zero.
1618 *
1619 * This routine must not be used by interrupt handlers, since they are not
1620 * allowed to block.
1621 *
1622 * @param timer Address of timer.
1623 *
1624 * @return Timer status.
1625 */
Andrew Boiea354d492017-09-29 16:22:28 -07001626__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001627
Andy Ross52e444b2018-09-28 09:06:37 -07001628extern s32_t z_timeout_remaining(struct _timeout *timeout);
1629
Allan Stephens45bfa372016-10-12 12:39:42 -05001630/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001631 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001632 *
1633 * This routine computes the (approximate) time remaining before a running
1634 * timer next expires. If the timer is not running, it returns zero.
1635 *
1636 * @param timer Address of timer.
1637 *
1638 * @return Remaining time (in milliseconds).
1639 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001640__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001641
Patrik Flykt4344e272019-03-08 14:19:05 -07001642static inline u32_t z_impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001643{
Charles E. Youse0ad40222019-03-01 10:51:04 -08001644 const s32_t ticks = z_timeout_remaining(&timer->timeout);
1645 return (ticks > 0) ? (u32_t)__ticks_to_ms(ticks) : 0U;
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001646}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001647
Allan Stephensc98da842016-11-11 15:45:03 -05001648/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001649 * @brief Associate user-specific data with a timer.
1650 *
1651 * This routine records the @a user_data with the @a timer, to be retrieved
1652 * later.
1653 *
1654 * It can be used e.g. in a timer handler shared across multiple subsystems to
1655 * retrieve data specific to the subsystem this timer is associated with.
1656 *
1657 * @param timer Address of timer.
1658 * @param user_data User data to associate with the timer.
1659 *
1660 * @return N/A
1661 */
Andrew Boiea354d492017-09-29 16:22:28 -07001662__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1663
Anas Nashif954d5502018-02-25 08:37:28 -06001664/**
1665 * @internal
1666 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001667static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
Andrew Boiea354d492017-09-29 16:22:28 -07001668 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001669{
1670 timer->user_data = user_data;
1671}
1672
1673/**
1674 * @brief Retrieve the user-specific data from a timer.
1675 *
1676 * @param timer Address of timer.
1677 *
1678 * @return The user data.
1679 */
Andrew Boiea354d492017-09-29 16:22:28 -07001680__syscall void *k_timer_user_data_get(struct k_timer *timer);
1681
Patrik Flykt4344e272019-03-08 14:19:05 -07001682static inline void *z_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001683{
1684 return timer->user_data;
1685}
1686
Anas Nashif166f5192018-02-25 08:02:36 -06001687/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001688
Allan Stephensc98da842016-11-11 15:45:03 -05001689/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001690 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001691 * @{
1692 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001693
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001694/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001695 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001696 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001697 * This routine returns the elapsed time since the system booted,
1698 * in milliseconds.
1699 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001700 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001701 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001702 * While this function returns time in milliseconds, it does
1703 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001704 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option.
David B. Kinder8de9cc72019-06-25 10:44:55 -07001705 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001706 *
1707 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001708 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001709__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001710
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001711/**
1712 * @brief Enable clock always on in tickless kernel
1713 *
Andy Ross1db9f182019-06-25 10:09:45 -07001714 * Deprecated. This does nothing (it was always just a hint). This
1715 * functionality has been migrated to the SYSTEM_CLOCK_SLOPPY_IDLE
1716 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001717 *
1718 * @retval prev_status Previous status of always on flag
1719 */
Andy Ross1db9f182019-06-25 10:09:45 -07001720/* LCOV_EXCL_START */
1721__deprecated static inline int k_enable_sys_clock_always_on(void)
1722{
1723 __ASSERT(IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1724 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1725
1726 return !IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE);
1727}
1728/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001729
1730/**
1731 * @brief Disable clock always on in tickless kernel
1732 *
Andy Ross1db9f182019-06-25 10:09:45 -07001733 * Deprecated. This does nothing (it was always just a hint). This
1734 * functionality has been migrated to the SYS_CLOCK_SLOPPY_IDLE
1735 * kconfig.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001736 */
Andy Ross1db9f182019-06-25 10:09:45 -07001737/* LCOV_EXCL_START */
1738__deprecated static inline void k_disable_sys_clock_always_on(void)
1739{
1740 __ASSERT(!IS_ENABLED(CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE),
1741 "Please use CONFIG_SYSTEM_CLOCK_SLOPPY_IDLE instead");
1742}
1743/* LCOV_EXCL_STOP */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001744
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001745/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001746 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001747 *
Peter Bigota6067a32019-08-28 08:19:26 -05001748 * This routine returns the lower 32 bits of the system uptime in
1749 * milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001750 *
Peter Bigota6067a32019-08-28 08:19:26 -05001751 * Because correct conversion requires full precision of the system
1752 * clock there is no benefit to using this over k_uptime_get() unless
1753 * you know the application will never run long enough for the system
1754 * clock to approach 2^32 ticks. Calls to this function may involve
1755 * interrupt blocking and 64-bit math.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001756 *
David B. Kinder00c41ea2019-06-10 11:13:33 -07001757 * @note
David B. Kinder8de9cc72019-06-25 10:44:55 -07001758 * @rst
David B. Kinder00c41ea2019-06-10 11:13:33 -07001759 * While this function returns time in milliseconds, it does
1760 * not mean it has millisecond resolution. The actual resolution depends on
Andy Ross669730f2019-06-11 11:18:20 -07001761 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option
David B. Kinder8de9cc72019-06-25 10:44:55 -07001762 * @endrst
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001763 *
Peter Bigota6067a32019-08-28 08:19:26 -05001764 * @return The low 32 bits of the current uptime, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001765 */
Peter Bigota6067a32019-08-28 08:19:26 -05001766static inline u32_t k_uptime_get_32(void)
1767{
1768 return (u32_t)k_uptime_get();
1769}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001770
1771/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001772 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001773 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001774 * This routine computes the elapsed time between the current system uptime
1775 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001776 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001777 * @param reftime Pointer to a reference time, which is updated to the current
1778 * uptime upon return.
1779 *
1780 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001781 */
Andy Ross987c0e52018-09-27 16:50:00 -07001782static inline s64_t k_uptime_delta(s64_t *reftime)
1783{
1784 s64_t uptime, delta;
1785
1786 uptime = k_uptime_get();
1787 delta = uptime - *reftime;
1788 *reftime = uptime;
1789
1790 return delta;
1791}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001792
1793/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001794 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001796 * This routine computes the elapsed time between the current system uptime
1797 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001799 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1800 * need for interrupt locking and 64-bit math. However, the 32-bit result
1801 * cannot hold an elapsed time larger than approximately 50 days, so the
1802 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001804 * @param reftime Pointer to a reference time, which is updated to the current
1805 * uptime upon return.
1806 *
1807 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001808 */
Andy Ross987c0e52018-09-27 16:50:00 -07001809static inline u32_t k_uptime_delta_32(s64_t *reftime)
1810{
1811 return (u32_t)k_uptime_delta(reftime);
1812}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001813
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001814/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001815 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001817 * This routine returns the current time, as measured by the system's hardware
1818 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001820 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001821 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001822#define k_cycle_get_32() z_arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001823
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001824/**
Anas Nashif166f5192018-02-25 08:02:36 -06001825 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001826 */
1827
Allan Stephensc98da842016-11-11 15:45:03 -05001828/**
1829 * @cond INTERNAL_HIDDEN
1830 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001831
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001832struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001833 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001834 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001835 union {
1836 _wait_q_t wait_q;
1837
1838 _POLL_EVENT;
1839 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001840
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001841 _OBJECT_TRACING_NEXT_PTR(k_queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001842};
1843
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001844#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001845 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001846 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Stephanos Ioannidisf628dcd2019-09-11 18:09:49 +09001847 .lock = { }, \
1848 { \
1849 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
1850 _POLL_EVENT_OBJ_INIT(obj) \
1851 }, \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001852 _OBJECT_TRACING_INIT \
1853 }
1854
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001855#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1856
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001857extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1858
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001859/**
1860 * INTERNAL_HIDDEN @endcond
1861 */
1862
1863/**
1864 * @defgroup queue_apis Queue APIs
1865 * @ingroup kernel_apis
1866 * @{
1867 */
1868
1869/**
1870 * @brief Initialize a queue.
1871 *
1872 * This routine initializes a queue object, prior to its first use.
1873 *
1874 * @param queue Address of the queue.
1875 *
1876 * @return N/A
1877 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001878__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001879
1880/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001881 * @brief Cancel waiting on a queue.
1882 *
1883 * This routine causes first thread pending on @a queue, if any, to
1884 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001885 * If the queue is being waited on by k_poll(), it will return with
1886 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1887 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001888 *
1889 * @note Can be called by ISRs.
1890 *
1891 * @param queue Address of the queue.
1892 *
1893 * @return N/A
1894 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001895__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001896
1897/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001898 * @brief Append an element to the end of a queue.
1899 *
1900 * This routine appends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001901 * aligned on a word boundary, and the first word of the item is reserved
1902 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001903 *
1904 * @note Can be called by ISRs.
1905 *
1906 * @param queue Address of the queue.
1907 * @param data Address of the data item.
1908 *
1909 * @return N/A
1910 */
1911extern void k_queue_append(struct k_queue *queue, void *data);
1912
1913/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001914 * @brief Append an element to a queue.
1915 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001916 * This routine appends a data item to @a queue. There is an implicit memory
1917 * allocation to create an additional temporary bookkeeping data structure from
1918 * the calling thread's resource pool, which is automatically freed when the
1919 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001920 *
1921 * @note Can be called by ISRs.
1922 *
1923 * @param queue Address of the queue.
1924 * @param data Address of the data item.
1925 *
1926 * @retval 0 on success
1927 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1928 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301929__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001930
1931/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001932 * @brief Prepend an element to a queue.
1933 *
1934 * This routine prepends a data item to @a queue. A queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001935 * aligned on a word boundary, and the first word of the item is reserved
1936 * for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001937 *
1938 * @note Can be called by ISRs.
1939 *
1940 * @param queue Address of the queue.
1941 * @param data Address of the data item.
1942 *
1943 * @return N/A
1944 */
1945extern void k_queue_prepend(struct k_queue *queue, void *data);
1946
1947/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001948 * @brief Prepend an element to a queue.
1949 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001950 * This routine prepends a data item to @a queue. There is an implicit memory
1951 * allocation to create an additional temporary bookkeeping data structure from
1952 * the calling thread's resource pool, which is automatically freed when the
1953 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001954 *
1955 * @note Can be called by ISRs.
1956 *
1957 * @param queue Address of the queue.
1958 * @param data Address of the data item.
1959 *
1960 * @retval 0 on success
1961 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1962 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301963__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001964
1965/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001966 * @brief Inserts an element to a queue.
1967 *
1968 * This routine inserts a data item to @a queue after previous item. A queue
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001969 * data item must be aligned on a word boundary, and the first word of
1970 * the item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001971 *
1972 * @note Can be called by ISRs.
1973 *
1974 * @param queue Address of the queue.
1975 * @param prev Address of the previous data item.
1976 * @param data Address of the data item.
1977 *
1978 * @return N/A
1979 */
1980extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1981
1982/**
1983 * @brief Atomically append a list of elements to a queue.
1984 *
1985 * This routine adds a list of data items to @a queue in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04001986 * The data items must be in a singly-linked list, with the first word
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001987 * in each data item pointing to the next data item; the list must be
1988 * NULL-terminated.
1989 *
1990 * @note Can be called by ISRs.
1991 *
1992 * @param queue Address of the queue.
1993 * @param head Pointer to first node in singly-linked list.
1994 * @param tail Pointer to last node in singly-linked list.
1995 *
1996 * @return N/A
1997 */
1998extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1999
2000/**
2001 * @brief Atomically add a list of elements to a queue.
2002 *
2003 * This routine adds a list of data items to @a queue in one operation.
2004 * The data items must be in a singly-linked list implemented using a
2005 * sys_slist_t object. Upon completion, the original list is empty.
2006 *
2007 * @note Can be called by ISRs.
2008 *
2009 * @param queue Address of the queue.
2010 * @param list Pointer to sys_slist_t object.
2011 *
2012 * @return N/A
2013 */
2014extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
2015
2016/**
2017 * @brief Get an element from a queue.
2018 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002019 * This routine removes first data item from @a queue. The first word of the
2020 * data item is reserved for the kernel's use.
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002021 *
2022 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2023 *
2024 * @param queue Address of the queue.
2025 * @param timeout Waiting period to obtain a data item (in milliseconds),
2026 * or one of the special values K_NO_WAIT and K_FOREVER.
2027 *
2028 * @return Address of the data item if successful; NULL if returned
2029 * without waiting, or waiting period timed out.
2030 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002031__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002032
2033/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002034 * @brief Remove an element from a queue.
2035 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002036 * This routine removes data item from @a queue. The first word of the
2037 * data item is reserved for the kernel's use. Removing elements from k_queue
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002038 * rely on sys_slist_find_and_remove which is not a constant time operation.
2039 *
2040 * @note Can be called by ISRs
2041 *
2042 * @param queue Address of the queue.
2043 * @param data Address of the data item.
2044 *
2045 * @return true if data item was removed
2046 */
2047static inline bool k_queue_remove(struct k_queue *queue, void *data)
2048{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002049 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03002050}
2051
2052/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002053 * @brief Append an element to a queue only if it's not present already.
2054 *
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002055 * This routine appends data item to @a queue. The first word of the data
2056 * item is reserved for the kernel's use. Appending elements to k_queue
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02002057 * relies on sys_slist_is_node_in_list which is not a constant time operation.
2058 *
2059 * @note Can be called by ISRs
2060 *
2061 * @param queue Address of the queue.
2062 * @param data Address of the data item.
2063 *
2064 * @return true if data item was added, false if not
2065 */
2066static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
2067{
2068 sys_sfnode_t *test;
2069
2070 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
2071 if (test == (sys_sfnode_t *) data) {
2072 return false;
2073 }
2074 }
2075
2076 k_queue_append(queue, data);
2077 return true;
2078}
2079
2080/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002081 * @brief Query a queue to see if it has data available.
2082 *
2083 * Note that the data might be already gone by the time this function returns
2084 * if other threads are also trying to read from the queue.
2085 *
2086 * @note Can be called by ISRs.
2087 *
2088 * @param queue Address of the queue.
2089 *
2090 * @return Non-zero if the queue is empty.
2091 * @return 0 if data is available.
2092 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002093__syscall int k_queue_is_empty(struct k_queue *queue);
2094
Patrik Flykt4344e272019-03-08 14:19:05 -07002095static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002096{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002097 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002098}
2099
2100/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002101 * @brief Peek element at the head of queue.
2102 *
2103 * Return element from the head of queue without removing it.
2104 *
2105 * @param queue Address of the queue.
2106 *
2107 * @return Head element, or NULL if queue is empty.
2108 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002109__syscall void *k_queue_peek_head(struct k_queue *queue);
2110
Patrik Flykt4344e272019-03-08 14:19:05 -07002111static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002112{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002113 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002114}
2115
2116/**
2117 * @brief Peek element at the tail of queue.
2118 *
2119 * Return element from the tail of queue without removing it.
2120 *
2121 * @param queue Address of the queue.
2122 *
2123 * @return Tail element, or NULL if queue is empty.
2124 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002125__syscall void *k_queue_peek_tail(struct k_queue *queue);
2126
Patrik Flykt4344e272019-03-08 14:19:05 -07002127static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002128{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002129 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002130}
2131
2132/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002133 * @brief Statically define and initialize a queue.
2134 *
2135 * The queue can be accessed outside the module where it is defined using:
2136 *
2137 * @code extern struct k_queue <name>; @endcode
2138 *
2139 * @param name Name of the queue.
2140 */
2141#define K_QUEUE_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002142 Z_STRUCT_SECTION_ITERABLE(k_queue, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002143 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002144
Anas Nashif166f5192018-02-25 08:02:36 -06002145/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002146
Wentong Wu5611e922019-06-20 23:51:27 +08002147#ifdef CONFIG_USERSPACE
2148/**
2149 * @brief futex structure
2150 *
2151 * A k_futex is a lightweight mutual exclusion primitive designed
2152 * to minimize kernel involvement. Uncontended operation relies
2153 * only on atomic access to shared memory. k_futex are tracked as
2154 * kernel objects and can live in user memory so any access bypass
2155 * the kernel object permission management mechanism.
2156 */
2157struct k_futex {
2158 atomic_t val;
2159};
2160
2161/**
2162 * @brief futex kernel data structure
2163 *
2164 * z_futex_data are the helper data structure for k_futex to complete
2165 * futex contended operation on kernel side, structure z_futex_data
2166 * of every futex object is invisible in user mode.
2167 */
2168struct z_futex_data {
2169 _wait_q_t wait_q;
2170 struct k_spinlock lock;
2171};
2172
2173#define Z_FUTEX_DATA_INITIALIZER(obj) \
2174 { \
2175 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q) \
2176 }
2177
2178/**
2179 * @defgroup futex_apis FUTEX APIs
2180 * @ingroup kernel_apis
2181 * @{
2182 */
2183
2184/**
Wentong Wu5611e922019-06-20 23:51:27 +08002185 * @brief Pend the current thread on a futex
2186 *
2187 * Tests that the supplied futex contains the expected value, and if so,
2188 * goes to sleep until some other thread calls k_futex_wake() on it.
2189 *
2190 * @param futex Address of the futex.
2191 * @param expected Expected value of the futex, if it is different the caller
2192 * will not wait on it.
2193 * @param timeout Waiting period on the futex, in milliseconds, or one of the
2194 * special values K_NO_WAIT or K_FOREVER.
2195 * @retval -EACCES Caller does not have read access to futex address.
2196 * @retval -EAGAIN If the futex value did not match the expected parameter.
2197 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2198 * @retval -ETIMEDOUT Thread woke up due to timeout and not a futex wakeup.
2199 * @retval 0 if the caller went to sleep and was woken up. The caller
2200 * should check the futex's value on wakeup to determine if it needs
2201 * to block again.
2202 */
2203__syscall int k_futex_wait(struct k_futex *futex, int expected, s32_t timeout);
2204
2205/**
2206 * @brief Wake one/all threads pending on a futex
2207 *
2208 * Wake up the highest priority thread pending on the supplied futex, or
2209 * wakeup all the threads pending on the supplied futex, and the behavior
2210 * depends on wake_all.
2211 *
2212 * @param futex Futex to wake up pending threads.
2213 * @param wake_all If true, wake up all pending threads; If false,
2214 * wakeup the highest priority thread.
2215 * @retval -EACCES Caller does not have access to the futex address.
2216 * @retval -EINVAL Futex parameter address not recognized by the kernel.
2217 * @retval Number of threads that were woken up.
2218 */
2219__syscall int k_futex_wake(struct k_futex *futex, bool wake_all);
2220
2221/** @} */
2222#endif
2223
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002224struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002225 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002226};
2227
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002228/**
2229 * @cond INTERNAL_HIDDEN
2230 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002231#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002232 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002233 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002234 }
2235
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002236#define K_FIFO_INITIALIZER DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002237
Allan Stephensc98da842016-11-11 15:45:03 -05002238/**
2239 * INTERNAL_HIDDEN @endcond
2240 */
2241
2242/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002243 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002244 * @ingroup kernel_apis
2245 * @{
2246 */
2247
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002248/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002249 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002250 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002251 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002252 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002253 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002254 *
2255 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002256 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002257 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002258#define k_fifo_init(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002259 k_queue_init(&(fifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002260
2261/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002262 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002263 *
2264 * This routine causes first thread pending on @a fifo, if any, to
2265 * return from k_fifo_get() call with NULL value (as if timeout
2266 * expired).
2267 *
2268 * @note Can be called by ISRs.
2269 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002270 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002271 *
2272 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002273 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002274 */
2275#define k_fifo_cancel_wait(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002276 k_queue_cancel_wait(&(fifo)->_queue)
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002277
2278/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002279 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002280 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002281 * This routine adds a data item to @a fifo. A FIFO data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002282 * aligned on a word boundary, and the first word of the item is reserved
2283 * for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002285 * @note Can be called by ISRs.
2286 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002287 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002288 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002289 *
2290 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002291 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002292 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002293#define k_fifo_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002294 k_queue_append(&(fifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002295
2296/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002297 * @brief Add an element to a FIFO queue.
2298 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002299 * This routine adds a data item to @a fifo. There is an implicit memory
2300 * allocation to create an additional temporary bookkeeping data structure from
2301 * the calling thread's resource pool, which is automatically freed when the
2302 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002303 *
2304 * @note Can be called by ISRs.
2305 *
2306 * @param fifo Address of the FIFO.
2307 * @param data Address of the data item.
2308 *
2309 * @retval 0 on success
2310 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002311 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002312 */
2313#define k_fifo_alloc_put(fifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002314 k_queue_alloc_append(&(fifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002315
2316/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002317 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002318 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002319 * This routine adds a list of data items to @a fifo in one operation.
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002320 * The data items must be in a singly-linked list, with the first word of
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002321 * each data item pointing to the next data item; the list must be
2322 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002323 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002324 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002325 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002326 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002327 * @param head Pointer to first node in singly-linked list.
2328 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002329 *
2330 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002331 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002332 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002333#define k_fifo_put_list(fifo, head, tail) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002334 k_queue_append_list(&(fifo)->_queue, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002335
2336/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002337 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002338 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002339 * This routine adds a list of data items to @a fifo in one operation.
2340 * The data items must be in a singly-linked list implemented using a
2341 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002342 * and must be re-initialized via sys_slist_init().
2343 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002344 * @note Can be called by ISRs.
2345 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002346 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002347 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002348 *
2349 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002350 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002351 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002352#define k_fifo_put_slist(fifo, list) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002353 k_queue_merge_slist(&(fifo)->_queue, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002354
2355/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002356 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002358 * This routine removes a data item from @a fifo in a "first in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002359 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002360 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002361 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2362 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002363 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002364 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002365 * or one of the special values K_NO_WAIT and K_FOREVER.
2366 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002367 * @return Address of the data item if successful; NULL if returned
2368 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002369 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002370 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002371#define k_fifo_get(fifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002372 k_queue_get(&(fifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002373
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002374/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002375 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002376 *
2377 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002378 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002379 *
2380 * @note Can be called by ISRs.
2381 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002382 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002383 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002384 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002385 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002386 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002387 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002388#define k_fifo_is_empty(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002389 k_queue_is_empty(&(fifo)->_queue)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002390
2391/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002392 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002393 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002394 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302395 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002396 * on each iteration of processing, a head container will be peeked,
2397 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002398 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002399 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002400 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002401 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002402 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002403 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002404 */
2405#define k_fifo_peek_head(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002406 k_queue_peek_head(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002407
2408/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002409 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002410 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002411 * Return element from the tail of FIFO queue (without removing it). A usecase
2412 * for this is if elements of the FIFO queue are themselves containers. Then
2413 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002414 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002415 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002416 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002417 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002418 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002419 */
2420#define k_fifo_peek_tail(fifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002421 k_queue_peek_tail(&(fifo)->_queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002422
2423/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002424 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002425 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002426 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002427 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002428 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002429 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002430 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002431 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002432 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002433#define K_FIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002434 Z_STRUCT_SECTION_ITERABLE(k_fifo, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002435 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002436
Anas Nashif166f5192018-02-25 08:02:36 -06002437/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002438
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002439struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002440 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002441};
2442
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002443/**
2444 * @cond INTERNAL_HIDDEN
2445 */
2446
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002447#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002448 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002449 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002450 }
2451
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002452#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2453
Allan Stephensc98da842016-11-11 15:45:03 -05002454/**
2455 * INTERNAL_HIDDEN @endcond
2456 */
2457
2458/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002459 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002460 * @ingroup kernel_apis
2461 * @{
2462 */
2463
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002464/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002465 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002466 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002467 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002468 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002469 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002470 *
2471 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002472 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002473 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002474#define k_lifo_init(lifo) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002475 k_queue_init(&(lifo)->_queue)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002476
2477/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002478 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002479 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002480 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002481 * aligned on a word boundary, and the first word of the item is
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002482 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002483 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002484 * @note Can be called by ISRs.
2485 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002486 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002487 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002488 *
2489 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002490 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002491 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002492#define k_lifo_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002493 k_queue_prepend(&(lifo)->_queue, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002494
2495/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002496 * @brief Add an element to a LIFO queue.
2497 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002498 * This routine adds a data item to @a lifo. There is an implicit memory
2499 * allocation to create an additional temporary bookkeeping data structure from
2500 * the calling thread's resource pool, which is automatically freed when the
2501 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002502 *
2503 * @note Can be called by ISRs.
2504 *
2505 * @param lifo Address of the LIFO.
2506 * @param data Address of the data item.
2507 *
2508 * @retval 0 on success
2509 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002510 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002511 */
2512#define k_lifo_alloc_put(lifo, data) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002513 k_queue_alloc_prepend(&(lifo)->_queue, data)
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002514
2515/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002516 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002518 * This routine removes a data item from @a lifo in a "last in, first out"
Nicolas Pitre659fa0d2019-05-21 22:13:01 -04002519 * manner. The first word of the data item is reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002521 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2522 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002523 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002524 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002525 * or one of the special values K_NO_WAIT and K_FOREVER.
2526 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002527 * @return Address of the data item if successful; NULL if returned
2528 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002529 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002530 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002531#define k_lifo_get(lifo, timeout) \
Nicolas Pitrea04a2ca2019-05-20 23:02:39 -04002532 k_queue_get(&(lifo)->_queue, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002533
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002534/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002535 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002536 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002537 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002538 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002539 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002541 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002542 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002543 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002544#define K_LIFO_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002545 Z_STRUCT_SECTION_ITERABLE(k_lifo, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002546 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002547
Anas Nashif166f5192018-02-25 08:02:36 -06002548/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002549
2550/**
2551 * @cond INTERNAL_HIDDEN
2552 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302553#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002554
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002555typedef uintptr_t stack_data_t;
2556
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002557struct k_stack {
2558 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002559 struct k_spinlock lock;
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002560 stack_data_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002561
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002562 _OBJECT_TRACING_NEXT_PTR(k_stack)
Andrew Boief3bee952018-05-02 17:44:39 -07002563 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002564};
2565
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002566#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002567 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002568 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002569 .base = stack_buffer, \
2570 .next = stack_buffer, \
2571 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002572 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002573 }
2574
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002575#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2576
Allan Stephensc98da842016-11-11 15:45:03 -05002577/**
2578 * INTERNAL_HIDDEN @endcond
2579 */
2580
2581/**
2582 * @defgroup stack_apis Stack APIs
2583 * @ingroup kernel_apis
2584 * @{
2585 */
2586
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002587/**
2588 * @brief Initialize a stack.
2589 *
2590 * This routine initializes a stack object, prior to its first use.
2591 *
2592 * @param stack Address of the stack.
2593 * @param buffer Address of array used to hold stacked values.
2594 * @param num_entries Maximum number of values that can be stacked.
2595 *
2596 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002597 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002598 */
Andrew Boief3bee952018-05-02 17:44:39 -07002599void k_stack_init(struct k_stack *stack,
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002600 stack_data_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002601
2602
2603/**
2604 * @brief Initialize a stack.
2605 *
2606 * This routine initializes a stack object, prior to its first use. Internal
2607 * buffers will be allocated from the calling thread's resource pool.
2608 * This memory will be released if k_stack_cleanup() is called, or
2609 * userspace is enabled and the stack object loses all references to it.
2610 *
2611 * @param stack Address of the stack.
2612 * @param num_entries Maximum number of values that can be stacked.
2613 *
2614 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002615 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002616 */
2617
Adithya Baglody28080d32018-10-15 11:48:51 +05302618__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2619 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002620
2621/**
2622 * @brief Release a stack's allocated buffer
2623 *
2624 * If a stack object was given a dynamically allocated buffer via
2625 * k_stack_alloc_init(), this will free it. This function does nothing
2626 * if the buffer wasn't dynamically allocated.
2627 *
2628 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002629 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002630 */
2631void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002632
2633/**
2634 * @brief Push an element onto a stack.
2635 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002636 * This routine adds a stack_data_t value @a data to @a stack.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002637 *
2638 * @note Can be called by ISRs.
2639 *
2640 * @param stack Address of the stack.
2641 * @param data Value to push onto the stack.
2642 *
2643 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002644 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002645 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002646__syscall void k_stack_push(struct k_stack *stack, stack_data_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002647
2648/**
2649 * @brief Pop an element from a stack.
2650 *
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002651 * This routine removes a stack_data_t value from @a stack in a "last in,
2652 * first out" manner and stores the value in @a data.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002653 *
2654 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2655 *
2656 * @param stack Address of the stack.
2657 * @param data Address of area to hold the value popped from the stack.
2658 * @param timeout Waiting period to obtain a value (in milliseconds),
2659 * or one of the special values K_NO_WAIT and K_FOREVER.
2660 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002661 * @retval 0 Element popped from stack.
2662 * @retval -EBUSY Returned without waiting.
2663 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002664 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002665 */
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002666__syscall int k_stack_pop(struct k_stack *stack, stack_data_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002667
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002668/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002669 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002670 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002671 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002672 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002673 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002674 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002675 * @param name Name of the stack.
2676 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002677 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002678 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002679#define K_STACK_DEFINE(name, stack_num_entries) \
Nicolas Pitre3d51f7c2019-05-17 22:48:26 -04002680 stack_data_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002681 _k_stack_buf_##name[stack_num_entries]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04002682 Z_STRUCT_SECTION_ITERABLE(k_stack, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002683 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002684 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002685
Anas Nashif166f5192018-02-25 08:02:36 -06002686/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002687
Allan Stephens6bba9b02016-11-16 14:56:54 -05002688struct k_work;
2689
Allan Stephensc98da842016-11-11 15:45:03 -05002690/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002691 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002692 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002693 */
2694
Allan Stephens6bba9b02016-11-16 14:56:54 -05002695/**
2696 * @typedef k_work_handler_t
2697 * @brief Work item handler function type.
2698 *
2699 * A work item's handler function is executed by a workqueue's thread
2700 * when the work item is processed by the workqueue.
2701 *
2702 * @param work Address of the work item.
2703 *
2704 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002705 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002706 */
2707typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002708
2709/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002710 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002711 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002712
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002713struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002714 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002715 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002716};
2717
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002718enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002719 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002720};
2721
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002722struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002723 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002724 k_work_handler_t handler;
2725 atomic_t flags[1];
2726};
2727
Allan Stephens6bba9b02016-11-16 14:56:54 -05002728struct k_delayed_work {
2729 struct k_work work;
2730 struct _timeout timeout;
2731 struct k_work_q *work_q;
2732};
2733
2734extern struct k_work_q k_sys_work_q;
2735
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002736/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002737 * INTERNAL_HIDDEN @endcond
2738 */
2739
Patrik Flykt4344e272019-03-08 14:19:05 -07002740#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002741 { \
2742 ._reserved = NULL, \
2743 .handler = work_handler, \
2744 .flags = { 0 } \
2745 }
2746
Patrik Flykt4344e272019-03-08 14:19:05 -07002747#define K_WORK_INITIALIZER DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002748
Allan Stephens6bba9b02016-11-16 14:56:54 -05002749/**
2750 * @brief Initialize a statically-defined work item.
2751 *
2752 * This macro can be used to initialize a statically-defined workqueue work
2753 * item, prior to its first use. For example,
2754 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002755 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002756 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002757 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002758 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002759 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002760 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002761#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002762 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002763
2764/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002765 * @brief Initialize a work item.
2766 *
2767 * This routine initializes a workqueue work item, prior to its first use.
2768 *
2769 * @param work Address of work item.
2770 * @param handler Function to invoke each time work item is processed.
2771 *
2772 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002773 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002774 */
2775static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2776{
Patrik Flykt4344e272019-03-08 14:19:05 -07002777 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002778}
2779
2780/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002781 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002782 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002783 * This routine submits work item @a work to be processed by workqueue
2784 * @a work_q. If the work item is already pending in the workqueue's queue
2785 * as a result of an earlier submission, this routine has no effect on the
2786 * work item. If the work item has already been processed, or is currently
2787 * being processed, its work is considered complete and the work item can be
2788 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002789 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002790 * @warning
2791 * A submitted work item must not be modified until it has been processed
2792 * by the workqueue.
2793 *
2794 * @note Can be called by ISRs.
2795 *
2796 * @param work_q Address of workqueue.
2797 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002798 *
2799 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002800 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002801 */
2802static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2803 struct k_work *work)
2804{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002805 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002806 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002807 }
2808}
2809
2810/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002811 * @brief Submit a work item to a user mode workqueue
2812 *
David B. Kinder06d78352018-12-17 14:32:40 -08002813 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002814 * memory allocation is made from the caller's resource pool which is freed
2815 * once the worker thread consumes the k_work item. The workqueue
2816 * thread must have memory access to the k_work item being submitted. The caller
2817 * must have permission granted on the work_q parameter's queue object.
2818 *
2819 * Otherwise this works the same as k_work_submit_to_queue().
2820 *
2821 * @note Can be called by ISRs.
2822 *
2823 * @param work_q Address of workqueue.
2824 * @param work Address of work item.
2825 *
2826 * @retval -EBUSY if the work item was already in some workqueue
2827 * @retval -ENOMEM if no memory for thread resource pool allocation
2828 * @retval 0 Success
2829 * @req K-WORK-001
2830 */
2831static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2832 struct k_work *work)
2833{
2834 int ret = -EBUSY;
2835
2836 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2837 ret = k_queue_alloc_append(&work_q->queue, work);
2838
2839 /* Couldn't insert into the queue. Clear the pending bit
2840 * so the work item can be submitted again
2841 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002842 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002843 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2844 }
2845 }
2846
2847 return ret;
2848}
2849
2850/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002851 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002852 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002853 * This routine indicates if work item @a work is pending in a workqueue's
2854 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002855 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002856 * @note Can be called by ISRs.
2857 *
2858 * @param work Address of work item.
2859 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002860 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002861 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002862 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002863static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002864{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002865 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002866}
2867
2868/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002869 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002870 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002871 * This routine starts workqueue @a work_q. The workqueue spawns its work
2872 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002873 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002874 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002875 * @param stack Pointer to work queue thread's stack space, as defined by
2876 * K_THREAD_STACK_DEFINE()
2877 * @param stack_size Size of the work queue thread's stack (in bytes), which
2878 * should either be the same constant passed to
2879 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002880 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002881 *
2882 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002883 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002884 */
Andrew Boie507852a2017-07-25 18:47:07 -07002885extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002886 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002887 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002888
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002889/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002890 * @brief Start a workqueue in user mode
2891 *
2892 * This works identically to k_work_q_start() except it is callable from user
2893 * mode, and the worker thread created will run in user mode.
2894 * The caller must have permissions granted on both the work_q parameter's
2895 * thread and queue objects, and the same restrictions on priority apply as
2896 * k_thread_create().
2897 *
2898 * @param work_q Address of workqueue.
2899 * @param stack Pointer to work queue thread's stack space, as defined by
2900 * K_THREAD_STACK_DEFINE()
2901 * @param stack_size Size of the work queue thread's stack (in bytes), which
2902 * should either be the same constant passed to
2903 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2904 * @param prio Priority of the work queue's thread.
2905 *
2906 * @return N/A
2907 * @req K-WORK-001
2908 */
2909extern void k_work_q_user_start(struct k_work_q *work_q,
2910 k_thread_stack_t *stack,
2911 size_t stack_size, int prio);
2912
2913/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002914 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002915 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002916 * This routine initializes a workqueue delayed work item, prior to
2917 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002918 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002919 * @param work Address of delayed work item.
2920 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002921 *
2922 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002923 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002924 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002925extern void k_delayed_work_init(struct k_delayed_work *work,
2926 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002927
2928/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002929 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002930 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002931 * This routine schedules work item @a work to be processed by workqueue
2932 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002933 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002934 * Only when the countdown completes is the work item actually submitted to
2935 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002936 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002937 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002938 * counting down cancels the existing submission and restarts the
2939 * countdown using the new delay. Note that this behavior is
2940 * inherently subject to race conditions with the pre-existing
2941 * timeouts and work queue, so care must be taken to synchronize such
2942 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002943 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002944 * @warning
2945 * A delayed work item must not be modified until it has been processed
2946 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002947 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002948 * @note Can be called by ISRs.
2949 *
2950 * @param work_q Address of workqueue.
2951 * @param work Address of delayed work item.
2952 * @param delay Delay before submitting the work item (in milliseconds).
2953 *
2954 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002955 * @retval -EINVAL Work item is being processed or has completed its work.
2956 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002957 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002958 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002959extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2960 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002961 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002962
2963/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002964 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002965 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002966 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002967 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002968 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002969 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002970 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002971 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08002972 * @note The result of calling this on a k_delayed_work item that has
2973 * not been submitted (i.e. before the return of the
2974 * k_delayed_work_submit_to_queue() call) is undefined.
2975 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002976 * @param work Address of delayed work item.
2977 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002978 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002979 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002980 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002981 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002982extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002983
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002984/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002985 * @brief Submit a work item to the system workqueue.
2986 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002987 * This routine submits work item @a work to be processed by the system
2988 * workqueue. If the work item is already pending in the workqueue's queue
2989 * as a result of an earlier submission, this routine has no effect on the
2990 * work item. If the work item has already been processed, or is currently
2991 * being processed, its work is considered complete and the work item can be
2992 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002993 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002994 * @warning
2995 * Work items submitted to the system workqueue should avoid using handlers
2996 * that block or yield since this may prevent the system workqueue from
2997 * processing other work items in a timely manner.
2998 *
2999 * @note Can be called by ISRs.
3000 *
3001 * @param work Address of work item.
3002 *
3003 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003004 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003005 */
3006static inline void k_work_submit(struct k_work *work)
3007{
3008 k_work_submit_to_queue(&k_sys_work_q, work);
3009}
3010
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003011/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003012 * @brief Submit a delayed work item to the system workqueue.
3013 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003014 * This routine schedules work item @a work to be processed by the system
3015 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07003016 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003017 * Only when the countdown completes is the work item actually submitted to
3018 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003019 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05003020 * Submitting a previously submitted delayed work item that is still
3021 * counting down cancels the existing submission and restarts the countdown
3022 * using the new delay. If the work item is currently pending on the
3023 * workqueue's queue because the countdown has completed it is too late to
3024 * resubmit the item, and resubmission fails without impacting the work item.
3025 * If the work item has already been processed, or is currently being processed,
3026 * its work is considered complete and the work item can be resubmitted.
3027 *
3028 * @warning
3029 * Work items submitted to the system workqueue should avoid using handlers
3030 * that block or yield since this may prevent the system workqueue from
3031 * processing other work items in a timely manner.
3032 *
3033 * @note Can be called by ISRs.
3034 *
3035 * @param work Address of delayed work item.
3036 * @param delay Delay before submitting the work item (in milliseconds).
3037 *
3038 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05003039 * @retval -EINVAL Work item is being processed or has completed its work.
3040 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003041 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003042 */
3043static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05003044 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003045{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05003046 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003047}
3048
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003049/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02003050 * @brief Get time remaining before a delayed work gets scheduled.
3051 *
3052 * This routine computes the (approximate) time remaining before a
3053 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02003054 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02003055 *
3056 * @param work Delayed work item.
3057 *
3058 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003059 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02003060 */
Kumar Galacc334c72017-04-21 10:55:34 -05003061static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02003062{
Andy Ross52e444b2018-09-28 09:06:37 -07003063 return __ticks_to_ms(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02003064}
3065
Anas Nashif166f5192018-02-25 08:02:36 -06003066/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003067/**
Anas Nashifce78d162018-05-24 12:43:11 -05003068 * @defgroup mutex_apis Mutex APIs
3069 * @ingroup kernel_apis
3070 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003071 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003072
Anas Nashifce78d162018-05-24 12:43:11 -05003073/**
3074 * Mutex Structure
3075 * @ingroup mutex_apis
3076 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003077struct k_mutex {
3078 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05003079 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04003080 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05003081 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003082 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003083
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003084 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003085};
3086
Anas Nashifce78d162018-05-24 12:43:11 -05003087/**
3088 * @cond INTERNAL_HIDDEN
3089 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003090#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003091 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003092 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003093 .owner = NULL, \
3094 .lock_count = 0, \
3095 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003096 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003097 }
3098
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003099#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
3100
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003101/**
Allan Stephensc98da842016-11-11 15:45:03 -05003102 * INTERNAL_HIDDEN @endcond
3103 */
3104
3105/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003106 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003107 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003108 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003109 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003110 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003112 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003113 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003114 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003115#define K_MUTEX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003116 Z_STRUCT_SECTION_ITERABLE(k_mutex, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003117 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003118
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003119/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003120 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003121 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003122 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003124 * Upon completion, the mutex is available and does not have an owner.
3125 *
3126 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003127 *
3128 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003129 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003130 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003131__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003132
3133/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003134 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003135 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003136 * This routine locks @a mutex. If the mutex is locked by another thread,
3137 * the calling thread waits until the mutex becomes available or until
3138 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003139 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003140 * A thread is permitted to lock a mutex it has already locked. The operation
3141 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003143 * @param mutex Address of the mutex.
3144 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003145 * or one of the special values K_NO_WAIT and K_FOREVER.
3146 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003147 * @retval 0 Mutex locked.
3148 * @retval -EBUSY Returned without waiting.
3149 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003150 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003151 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003152__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003153
3154/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003155 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003156 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003157 * This routine unlocks @a mutex. The mutex must already be locked by the
3158 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003159 *
3160 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003161 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003162 * thread.
3163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003164 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003165 *
3166 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003167 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003168 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003169__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003170
Allan Stephensc98da842016-11-11 15:45:03 -05003171/**
Anas Nashif166f5192018-02-25 08:02:36 -06003172 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003173 */
3174
3175/**
3176 * @cond INTERNAL_HIDDEN
3177 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003178
3179struct k_sem {
3180 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303181 u32_t count;
3182 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003183 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003184
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003185 _OBJECT_TRACING_NEXT_PTR(k_sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003186};
3187
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003188#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003189 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003190 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003191 .count = initial_count, \
3192 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003193 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003194 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003195 }
3196
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003197#define K_SEM_INITIALIZER DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003198
Allan Stephensc98da842016-11-11 15:45:03 -05003199/**
3200 * INTERNAL_HIDDEN @endcond
3201 */
3202
3203/**
3204 * @defgroup semaphore_apis Semaphore APIs
3205 * @ingroup kernel_apis
3206 * @{
3207 */
3208
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003209/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003210 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003211 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003212 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003213 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003214 * @param sem Address of the semaphore.
3215 * @param initial_count Initial semaphore count.
3216 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003217 *
3218 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003219 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003220 */
Andrew Boie99280232017-09-29 14:17:47 -07003221__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
3222 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003223
3224/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003225 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003226 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003227 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003229 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3230 *
3231 * @param sem Address of the semaphore.
3232 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003233 * or one of the special values K_NO_WAIT and K_FOREVER.
3234 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05003235 * @note When porting code from the nanokernel legacy API to the new API, be
3236 * careful with the return value of this function. The return value is the
3237 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
3238 * non-zero means failure, while the nano_sem_take family returns 1 for success
3239 * and 0 for failure.
3240 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003241 * @retval 0 Semaphore taken.
3242 * @retval -EBUSY Returned without waiting.
3243 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003244 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003245 */
Andrew Boie99280232017-09-29 14:17:47 -07003246__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003247
3248/**
3249 * @brief Give a semaphore.
3250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003251 * This routine gives @a sem, unless the semaphore is already at its maximum
3252 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003253 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003254 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003255 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003256 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003257 *
3258 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003259 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003260 */
Andrew Boie99280232017-09-29 14:17:47 -07003261__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003262
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003263/**
3264 * @brief Reset a semaphore's count to zero.
3265 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003266 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003267 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003268 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003269 *
3270 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003271 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003272 */
Andrew Boie990bf162017-10-03 12:36:49 -07003273__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003274
Anas Nashif954d5502018-02-25 08:37:28 -06003275/**
3276 * @internal
3277 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003278static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003279{
Patrik Flykt24d71432019-03-26 19:57:45 -06003280 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003281}
3282
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003283/**
3284 * @brief Get a semaphore's count.
3285 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003286 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003287 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003288 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003290 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003291 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003292 */
Andrew Boie990bf162017-10-03 12:36:49 -07003293__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003294
Anas Nashif954d5502018-02-25 08:37:28 -06003295/**
3296 * @internal
3297 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003298static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003299{
3300 return sem->count;
3301}
3302
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003303/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003304 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003305 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003306 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003307 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003308 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003309 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003310 * @param name Name of the semaphore.
3311 * @param initial_count Initial semaphore count.
3312 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003313 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003314 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003315#define K_SEM_DEFINE(name, initial_count, count_limit) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003316 Z_STRUCT_SECTION_ITERABLE(k_sem, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003317 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303318 BUILD_ASSERT(((count_limit) != 0) && \
3319 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003320
Anas Nashif166f5192018-02-25 08:02:36 -06003321/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003322
3323/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003324 * @defgroup msgq_apis Message Queue APIs
3325 * @ingroup kernel_apis
3326 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003327 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003328
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003329/**
3330 * @brief Message Queue Structure
3331 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003332struct k_msgq {
3333 _wait_q_t wait_q;
Andy Rossbe03dbd2018-07-26 10:23:02 -07003334 struct k_spinlock lock;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003335 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003336 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003337 char *buffer_start;
3338 char *buffer_end;
3339 char *read_ptr;
3340 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003341 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003342
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003343 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Andrew Boie0fe789f2018-04-12 18:35:56 -07003344 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003345};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003346/**
3347 * @cond INTERNAL_HIDDEN
3348 */
3349
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003350
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003351#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003352 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003353 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003354 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003355 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003356 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003357 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003358 .read_ptr = q_buffer, \
3359 .write_ptr = q_buffer, \
3360 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003361 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003362 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003363#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003364/**
3365 * INTERNAL_HIDDEN @endcond
3366 */
3367
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003368
Andrew Boie0fe789f2018-04-12 18:35:56 -07003369#define K_MSGQ_FLAG_ALLOC BIT(0)
3370
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003371/**
3372 * @brief Message Queue Attributes
3373 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303374struct k_msgq_attrs {
3375 size_t msg_size;
3376 u32_t max_msgs;
3377 u32_t used_msgs;
3378};
3379
Allan Stephensc98da842016-11-11 15:45:03 -05003380
3381/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003382 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003383 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003384 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3385 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003386 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3387 * message is similarly aligned to this boundary, @a q_msg_size must also be
3388 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003390 * The message queue can be accessed outside the module where it is defined
3391 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003392 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003393 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003394 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003395 * @param q_name Name of the message queue.
3396 * @param q_msg_size Message size (in bytes).
3397 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003398 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003399 *
3400 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003401 */
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003402#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
3403 static char __noinit __aligned(q_align) \
3404 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
3405 Z_STRUCT_SECTION_ITERABLE(k_msgq, q_name) = \
3406 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003407 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003408
Peter Mitsisd7a37502016-10-13 11:37:40 -04003409/**
3410 * @brief Initialize a message queue.
3411 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003412 * This routine initializes a message queue object, prior to its first use.
3413 *
Allan Stephensda827222016-11-09 14:23:58 -06003414 * The message queue's ring buffer must contain space for @a max_msgs messages,
3415 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3416 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3417 * that each message is similarly aligned to this boundary, @a q_msg_size
3418 * must also be a multiple of N.
3419 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003420 * @param q Address of the message queue.
3421 * @param buffer Pointer to ring buffer that holds queued messages.
3422 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003423 * @param max_msgs Maximum number of messages that can be queued.
3424 *
3425 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003426 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003427 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003428void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3429 u32_t max_msgs);
3430
3431/**
3432 * @brief Initialize a message queue.
3433 *
3434 * This routine initializes a message queue object, prior to its first use,
3435 * allocating its internal ring buffer from the calling thread's resource
3436 * pool.
3437 *
3438 * Memory allocated for the ring buffer can be released by calling
3439 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3440 * all of its references.
3441 *
3442 * @param q Address of the message queue.
3443 * @param msg_size Message size (in bytes).
3444 * @param max_msgs Maximum number of messages that can be queued.
3445 *
3446 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3447 * thread's resource pool, or -EINVAL if the size parameters cause
3448 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003449 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003450 */
3451__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3452 u32_t max_msgs);
3453
3454
3455void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003456
3457/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003458 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003459 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003460 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003461 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003462 * @note Can be called by ISRs.
3463 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003464 * @param q Address of the message queue.
3465 * @param data Pointer to the message.
3466 * @param timeout Waiting period to add the message (in milliseconds),
3467 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003468 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003469 * @retval 0 Message sent.
3470 * @retval -ENOMSG Returned without waiting or queue purged.
3471 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003472 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003473 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003474__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003475
3476/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003477 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003479 * This routine receives a message from message queue @a q in a "first in,
3480 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003481 *
Allan Stephensc98da842016-11-11 15:45:03 -05003482 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003483 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003484 * @param q Address of the message queue.
3485 * @param data Address of area to hold the received message.
3486 * @param timeout Waiting period to receive the message (in milliseconds),
3487 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003488 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003489 * @retval 0 Message received.
3490 * @retval -ENOMSG Returned without waiting.
3491 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003492 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003493 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003494__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003495
3496/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003497 * @brief Peek/read a message from a message queue.
3498 *
3499 * This routine reads a message from message queue @a q in a "first in,
3500 * first out" manner and leaves the message in the queue.
3501 *
3502 * @note Can be called by ISRs.
3503 *
3504 * @param q Address of the message queue.
3505 * @param data Address of area to hold the message read from the queue.
3506 *
3507 * @retval 0 Message read.
3508 * @retval -ENOMSG Returned when the queue has no message.
3509 * @req K-MSGQ-002
3510 */
3511__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3512
3513/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003514 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003515 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003516 * This routine discards all unreceived messages in a message queue's ring
3517 * buffer. Any threads that are blocked waiting to send a message to the
3518 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003519 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003520 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003521 *
3522 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003523 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003524 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003525__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003526
Peter Mitsis67be2492016-10-07 11:44:34 -04003527/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003528 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003530 * This routine returns the number of unused entries in a message queue's
3531 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003533 * @param q Address of the message queue.
3534 *
3535 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003536 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003537 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003538__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3539
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303540/**
3541 * @brief Get basic attributes of a message queue.
3542 *
3543 * This routine fetches basic attributes of message queue into attr argument.
3544 *
3545 * @param q Address of the message queue.
3546 * @param attrs pointer to message queue attribute structure.
3547 *
3548 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003549 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303550 */
3551__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3552
3553
Patrik Flykt4344e272019-03-08 14:19:05 -07003554static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003555{
3556 return q->max_msgs - q->used_msgs;
3557}
3558
Peter Mitsisd7a37502016-10-13 11:37:40 -04003559/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003560 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003562 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003563 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003564 * @param q Address of the message queue.
3565 *
3566 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003567 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003568 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003569__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3570
Patrik Flykt4344e272019-03-08 14:19:05 -07003571static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003572{
3573 return q->used_msgs;
3574}
3575
Anas Nashif166f5192018-02-25 08:02:36 -06003576/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003577
3578/**
3579 * @defgroup mem_pool_apis Memory Pool APIs
3580 * @ingroup kernel_apis
3581 * @{
3582 */
3583
Andy Ross73cb9582017-05-09 10:42:39 -07003584/* Note on sizing: the use of a 20 bit field for block means that,
3585 * assuming a reasonable minimum block size of 16 bytes, we're limited
3586 * to 16M of memory managed by a single pool. Long term it would be
3587 * good to move to a variable bit size based on configuration.
3588 */
3589struct k_mem_block_id {
3590 u32_t pool : 8;
3591 u32_t level : 4;
3592 u32_t block : 20;
3593};
3594
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003595struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003596 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003597 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003598};
3599
Anas Nashif166f5192018-02-25 08:02:36 -06003600/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003601
3602/**
3603 * @defgroup mailbox_apis Mailbox APIs
3604 * @ingroup kernel_apis
3605 * @{
3606 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003607
3608struct k_mbox_msg {
3609 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003610 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003611 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003612 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003613 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003614 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003615 /** sender's message data buffer */
3616 void *tx_data;
3617 /** internal use only - needed for legacy API support */
3618 void *_rx_data;
3619 /** message data block descriptor */
3620 struct k_mem_block tx_block;
3621 /** source thread id */
3622 k_tid_t rx_source_thread;
3623 /** target thread id */
3624 k_tid_t tx_target_thread;
3625 /** internal use only - thread waiting on send (may be a dummy) */
3626 k_tid_t _syncing_thread;
3627#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3628 /** internal use only - semaphore used during asynchronous send */
3629 struct k_sem *_async_sem;
3630#endif
3631};
3632
3633struct k_mbox {
3634 _wait_q_t tx_msg_queue;
3635 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003636 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003637
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003638 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003639};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003640/**
3641 * @cond INTERNAL_HIDDEN
3642 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003643
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003644#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003645 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003646 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3647 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003648 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003649 }
3650
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003651#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3652
Peter Mitsis12092702016-10-14 12:57:23 -04003653/**
Allan Stephensc98da842016-11-11 15:45:03 -05003654 * INTERNAL_HIDDEN @endcond
3655 */
3656
3657/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003658 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003659 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003660 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003661 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003662 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003663 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003664 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003665 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003666 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003667#define K_MBOX_DEFINE(name) \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003668 Z_STRUCT_SECTION_ITERABLE(k_mbox, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003669 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003670
Peter Mitsis12092702016-10-14 12:57:23 -04003671/**
3672 * @brief Initialize a mailbox.
3673 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003674 * This routine initializes a mailbox object, prior to its first use.
3675 *
3676 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003677 *
3678 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003679 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003680 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003681extern void k_mbox_init(struct k_mbox *mbox);
3682
Peter Mitsis12092702016-10-14 12:57:23 -04003683/**
3684 * @brief Send a mailbox message in a synchronous manner.
3685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003686 * This routine sends a message to @a mbox and waits for a receiver to both
3687 * receive and process it. The message data may be in a buffer, in a memory
3688 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003690 * @param mbox Address of the mailbox.
3691 * @param tx_msg Address of the transmit message descriptor.
3692 * @param timeout Waiting period for the message to be received (in
3693 * milliseconds), or one of the special values K_NO_WAIT
3694 * and K_FOREVER. Once the message has been received,
3695 * this routine waits as long as necessary for the message
3696 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003697 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003698 * @retval 0 Message sent.
3699 * @retval -ENOMSG Returned without waiting.
3700 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003701 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003702 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003703extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003704 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003705
Peter Mitsis12092702016-10-14 12:57:23 -04003706/**
3707 * @brief Send a mailbox message in an asynchronous manner.
3708 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003709 * This routine sends a message to @a mbox without waiting for a receiver
3710 * to process it. The message data may be in a buffer, in a memory pool block,
3711 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3712 * will be given when the message has been both received and completely
3713 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003714 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003715 * @param mbox Address of the mailbox.
3716 * @param tx_msg Address of the transmit message descriptor.
3717 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003718 *
3719 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003720 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003721 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003722extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003723 struct k_sem *sem);
3724
Peter Mitsis12092702016-10-14 12:57:23 -04003725/**
3726 * @brief Receive a mailbox message.
3727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003728 * This routine receives a message from @a mbox, then optionally retrieves
3729 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003730 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003731 * @param mbox Address of the mailbox.
3732 * @param rx_msg Address of the receive message descriptor.
3733 * @param buffer Address of the buffer to receive data, or NULL to defer data
3734 * retrieval and message disposal until later.
3735 * @param timeout Waiting period for a message to be received (in
3736 * milliseconds), or one of the special values K_NO_WAIT
3737 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003738 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003739 * @retval 0 Message received.
3740 * @retval -ENOMSG Returned without waiting.
3741 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003742 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003743 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003744extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003745 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003746
3747/**
3748 * @brief Retrieve mailbox message data into a buffer.
3749 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003750 * This routine completes the processing of a received message by retrieving
3751 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003752 *
3753 * Alternatively, this routine can be used to dispose of a received message
3754 * without retrieving its data.
3755 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003756 * @param rx_msg Address of the receive message descriptor.
3757 * @param buffer Address of the buffer to receive data, or NULL to discard
3758 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003759 *
3760 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003761 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003762 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003763extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003764
3765/**
3766 * @brief Retrieve mailbox message data into a memory pool block.
3767 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003768 * This routine completes the processing of a received message by retrieving
3769 * its data into a memory pool block, then disposing of the message.
3770 * The memory pool block that results from successful retrieval must be
3771 * returned to the pool once the data has been processed, even in cases
3772 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003773 *
3774 * Alternatively, this routine can be used to dispose of a received message
3775 * without retrieving its data. In this case there is no need to return a
3776 * memory pool block to the pool.
3777 *
3778 * This routine allocates a new memory pool block for the data only if the
3779 * data is not already in one. If a new block cannot be allocated, the routine
3780 * returns a failure code and the received message is left unchanged. This
3781 * permits the caller to reattempt data retrieval at a later time or to dispose
3782 * of the received message without retrieving its data.
3783 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003784 * @param rx_msg Address of a receive message descriptor.
3785 * @param pool Address of memory pool, or NULL to discard data.
3786 * @param block Address of the area to hold memory pool block info.
3787 * @param timeout Waiting period to wait for a memory pool block (in
3788 * milliseconds), or one of the special values K_NO_WAIT
3789 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003790 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003791 * @retval 0 Data retrieved.
3792 * @retval -ENOMEM Returned without waiting.
3793 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003794 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003795 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003796extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003797 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003798 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003799
Anas Nashif166f5192018-02-25 08:02:36 -06003800/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003801
3802/**
Anas Nashifce78d162018-05-24 12:43:11 -05003803 * @defgroup pipe_apis Pipe APIs
3804 * @ingroup kernel_apis
3805 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003806 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003807
Anas Nashifce78d162018-05-24 12:43:11 -05003808/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003809struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003810 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3811 size_t size; /**< Buffer size */
3812 size_t bytes_used; /**< # bytes used in buffer */
3813 size_t read_index; /**< Where in buffer to read from */
3814 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08003815 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003816
3817 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003818 _wait_q_t readers; /**< Reader wait queue */
3819 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003820 } wait_q;
3821
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003822 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Anas Nashifce78d162018-05-24 12:43:11 -05003823 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003824};
3825
Anas Nashifce78d162018-05-24 12:43:11 -05003826/**
3827 * @cond INTERNAL_HIDDEN
3828 */
3829#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3830
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003831#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
3832 { \
3833 .buffer = pipe_buffer, \
3834 .size = pipe_buffer_size, \
3835 .bytes_used = 0, \
3836 .read_index = 0, \
3837 .write_index = 0, \
3838 .lock = {}, \
3839 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003840 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
3841 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003842 }, \
3843 _OBJECT_TRACING_INIT \
3844 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003845 }
3846
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003847#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3848
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003849/**
Allan Stephensc98da842016-11-11 15:45:03 -05003850 * INTERNAL_HIDDEN @endcond
3851 */
3852
3853/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003854 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003856 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003857 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003858 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003860 * @param name Name of the pipe.
3861 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3862 * or zero if no ring buffer is used.
3863 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003864 *
3865 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003866 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003867#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08003868 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07003869 _k_pipe_buf_##name[pipe_buffer_size]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04003870 Z_STRUCT_SECTION_ITERABLE(k_pipe, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003871 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003872
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003873/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003874 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003876 * This routine initializes a pipe object, prior to its first use.
3877 *
3878 * @param pipe Address of the pipe.
3879 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3880 * is used.
3881 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3882 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003883 *
3884 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003885 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003886 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003887void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3888
3889/**
3890 * @brief Release a pipe's allocated buffer
3891 *
3892 * If a pipe object was given a dynamically allocated buffer via
3893 * k_pipe_alloc_init(), this will free it. This function does nothing
3894 * if the buffer wasn't dynamically allocated.
3895 *
3896 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003897 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003898 */
3899void k_pipe_cleanup(struct k_pipe *pipe);
3900
3901/**
3902 * @brief Initialize a pipe and allocate a buffer for it
3903 *
3904 * Storage for the buffer region will be allocated from the calling thread's
3905 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3906 * or userspace is enabled and the pipe object loses all references to it.
3907 *
3908 * This function should only be called on uninitialized pipe objects.
3909 *
3910 * @param pipe Address of the pipe.
3911 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3912 * buffer is used.
3913 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003914 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003915 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003916 */
3917__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003918
3919/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003920 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003921 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003922 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003923 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003924 * @param pipe Address of the pipe.
3925 * @param data Address of data to write.
3926 * @param bytes_to_write Size of data (in bytes).
3927 * @param bytes_written Address of area to hold the number of bytes written.
3928 * @param min_xfer Minimum number of bytes to write.
3929 * @param timeout Waiting period to wait for the data to be written (in
3930 * milliseconds), or one of the special values K_NO_WAIT
3931 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003932 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003933 * @retval 0 At least @a min_xfer bytes of data were written.
3934 * @retval -EIO Returned without waiting; zero data bytes were written.
3935 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003936 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003937 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003938 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003939__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3940 size_t bytes_to_write, size_t *bytes_written,
3941 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003942
3943/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003944 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003946 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003947 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003948 * @param pipe Address of the pipe.
3949 * @param data Address to place the data read from pipe.
3950 * @param bytes_to_read Maximum number of data bytes to read.
3951 * @param bytes_read Address of area to hold the number of bytes read.
3952 * @param min_xfer Minimum number of data bytes to read.
3953 * @param timeout Waiting period to wait for the data to be read (in
3954 * milliseconds), or one of the special values K_NO_WAIT
3955 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003956 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003957 * @retval 0 At least @a min_xfer bytes of data were read.
3958 * @retval -EIO Returned without waiting; zero data bytes were read.
3959 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003960 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003961 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003962 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003963__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3964 size_t bytes_to_read, size_t *bytes_read,
3965 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003966
3967/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003968 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003969 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003970 * This routine writes the data contained in a memory block to @a pipe.
3971 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003972 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003974 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003975 * @param block Memory block containing data to send
3976 * @param size Number of data bytes in memory block to send
3977 * @param sem Semaphore to signal upon completion (else NULL)
3978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003979 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003980 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003981 */
3982extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3983 size_t size, struct k_sem *sem);
3984
Anas Nashif166f5192018-02-25 08:02:36 -06003985/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003986
Allan Stephensc98da842016-11-11 15:45:03 -05003987/**
3988 * @cond INTERNAL_HIDDEN
3989 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003990
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003991struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003992 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003993 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003994 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003995 char *buffer;
3996 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003997 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003998
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003999 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004000};
4001
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004002#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004003 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004004 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07004005 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004006 .num_blocks = slab_num_blocks, \
4007 .block_size = slab_block_size, \
4008 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004009 .free_list = NULL, \
4010 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05004011 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004012 }
4013
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004014#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
4015
4016
Peter Mitsis578f9112016-10-07 13:50:31 -04004017/**
Allan Stephensc98da842016-11-11 15:45:03 -05004018 * INTERNAL_HIDDEN @endcond
4019 */
4020
4021/**
4022 * @defgroup mem_slab_apis Memory Slab APIs
4023 * @ingroup kernel_apis
4024 * @{
4025 */
4026
4027/**
Allan Stephensda827222016-11-09 14:23:58 -06004028 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04004029 *
Allan Stephensda827222016-11-09 14:23:58 -06004030 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004031 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06004032 * @a slab_align -byte boundary. To ensure that each memory block is similarly
4033 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004034 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04004035 *
Allan Stephensda827222016-11-09 14:23:58 -06004036 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004037 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004038 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004039 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004040 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004041 * @param name Name of the memory slab.
4042 * @param slab_block_size Size of each memory block (in bytes).
4043 * @param slab_num_blocks Number memory blocks.
4044 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004045 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04004046 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004047#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004048 char __noinit __aligned(WB_UP(slab_align)) \
4049 _k_mem_slab_buf_##name[(slab_num_blocks) * WB_UP(slab_block_size)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004050 Z_STRUCT_SECTION_ITERABLE(k_mem_slab, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07004051 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004052 WB_UP(slab_block_size), slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004053
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004054/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004055 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004056 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004057 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004058 *
Allan Stephensda827222016-11-09 14:23:58 -06004059 * The memory slab's buffer contains @a slab_num_blocks memory blocks
4060 * that are @a slab_block_size bytes long. The buffer must be aligned to an
Nicolas Pitre46cd5a02019-05-21 21:40:38 -04004061 * N-byte boundary matching a word boundary, where N is a power of 2
4062 * (i.e. 4 on 32-bit systems, 8, 16, ...).
Allan Stephensda827222016-11-09 14:23:58 -06004063 * To ensure that each memory block is similarly aligned to this boundary,
4064 * @a slab_block_size must also be a multiple of N.
4065 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004066 * @param slab Address of the memory slab.
4067 * @param buffer Pointer to buffer used for the memory blocks.
4068 * @param block_size Size of each memory block (in bytes).
4069 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004070 *
4071 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004072 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004073 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004074extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05004075 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004076
4077/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004078 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004079 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004080 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004081 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004082 * @param slab Address of the memory slab.
4083 * @param mem Pointer to block address area.
4084 * @param timeout Maximum time to wait for operation to complete
4085 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4086 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004087 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004088 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004089 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004090 * @retval -ENOMEM Returned without waiting.
4091 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004092 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004093 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004094extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05004095 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004096
4097/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004098 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004099 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004100 * This routine releases a previously allocated memory block back to its
4101 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004102 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004103 * @param slab Address of the memory slab.
4104 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004105 *
4106 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004107 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004108 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004109extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004110
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004111/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004112 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004113 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004114 * This routine gets the number of memory blocks that are currently
4115 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004117 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004119 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004120 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004121 */
Kumar Galacc334c72017-04-21 10:55:34 -05004122static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004123{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004124 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004125}
4126
Peter Mitsisc001aa82016-10-13 13:53:37 -04004127/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004128 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004130 * This routine gets the number of memory blocks that are currently
4131 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004133 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004135 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004136 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004137 */
Kumar Galacc334c72017-04-21 10:55:34 -05004138static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004139{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004140 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004141}
4142
Anas Nashif166f5192018-02-25 08:02:36 -06004143/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004144
4145/**
4146 * @cond INTERNAL_HIDDEN
4147 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004148
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004149struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004150 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004151 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004152};
4153
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004154/**
Allan Stephensc98da842016-11-11 15:45:03 -05004155 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004156 */
4157
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004158/**
Allan Stephensc98da842016-11-11 15:45:03 -05004159 * @addtogroup mem_pool_apis
4160 * @{
4161 */
4162
4163/**
4164 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004165 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004166 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4167 * long. The memory pool allows blocks to be repeatedly partitioned into
4168 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004169 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004170 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004171 * If the pool is to be accessed outside the module where it is defined, it
4172 * can be declared via
4173 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004174 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004175 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004176 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004177 * @param minsz Size of the smallest blocks in the pool (in bytes).
4178 * @param maxsz Size of the largest blocks in the pool (in bytes).
4179 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004180 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004181 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004182 */
Andy Ross73cb9582017-05-09 10:42:39 -07004183#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004184 char __aligned(WB_UP(align)) _mpool_buf_##name[WB_UP(maxsz) * nmax \
Andy Ross73cb9582017-05-09 10:42:39 -07004185 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004186 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Nicolas Pitreb1d37422019-06-03 10:51:32 -04004187 Z_STRUCT_SECTION_ITERABLE(k_mem_pool, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004188 .base = { \
4189 .buf = _mpool_buf_##name, \
Nicolas Pitrecf974372019-06-26 11:32:58 -04004190 .max_sz = WB_UP(maxsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004191 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004192 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004193 .levels = _mpool_lvls_##name, \
4194 .flags = SYS_MEM_POOL_KERNEL \
4195 } \
Johann Fischer223a2b92019-07-04 15:55:20 +02004196 }; \
4197 BUILD_ASSERT(WB_UP(maxsz) >= _MPOOL_MINBLK);
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004198
Peter Mitsis937042c2016-10-13 13:18:26 -04004199/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004200 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004201 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004202 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004203 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004204 * @param pool Address of the memory pool.
4205 * @param block Pointer to block descriptor for the allocated memory.
4206 * @param size Amount of memory to allocate (in bytes).
4207 * @param timeout Maximum time to wait for operation to complete
4208 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4209 * or K_FOREVER to wait as long as necessary.
4210 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004211 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004212 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004213 * @retval -ENOMEM Returned without waiting.
4214 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004215 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004216 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004217extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004218 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004219
4220/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004221 * @brief Allocate memory from a memory pool with malloc() semantics
4222 *
4223 * Such memory must be released using k_free().
4224 *
4225 * @param pool Address of the memory pool.
4226 * @param size Amount of memory to allocate (in bytes).
4227 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004228 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004229 */
4230extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4231
4232/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004233 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004235 * This routine releases a previously allocated memory block back to its
4236 * memory pool.
4237 *
4238 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004239 *
4240 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004241 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004242 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004243extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004244
4245/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004246 * @brief Free memory allocated from a memory pool.
4247 *
4248 * This routine releases a previously allocated memory block back to its
4249 * memory pool
4250 *
4251 * @param id Memory block identifier.
4252 *
4253 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004254 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004255 */
4256extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4257
4258/**
Anas Nashif166f5192018-02-25 08:02:36 -06004259 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004260 */
4261
4262/**
4263 * @defgroup heap_apis Heap Memory Pool APIs
4264 * @ingroup kernel_apis
4265 * @{
4266 */
4267
4268/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004269 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004270 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004271 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004272 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004273 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004274 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004275 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004276 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004277 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004278 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004279extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004280
4281/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004282 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004283 *
4284 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004285 * returned must have been allocated from the heap memory pool or
4286 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004287 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004288 * If @a ptr is NULL, no operation is performed.
4289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004290 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004291 *
4292 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004293 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004294 */
4295extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004296
Allan Stephensc98da842016-11-11 15:45:03 -05004297/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004298 * @brief Allocate memory from heap, array style
4299 *
4300 * This routine provides traditional calloc() semantics. Memory is
4301 * allocated from the heap memory pool and zeroed.
4302 *
4303 * @param nmemb Number of elements in the requested array
4304 * @param size Size of each array element (in bytes).
4305 *
4306 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004307 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004308 */
4309extern void *k_calloc(size_t nmemb, size_t size);
4310
Anas Nashif166f5192018-02-25 08:02:36 -06004311/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004312
Benjamin Walshacc68c12017-01-29 18:57:45 -05004313/* polling API - PRIVATE */
4314
Benjamin Walshb0179862017-02-02 16:39:57 -05004315#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004316#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004317#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004318#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004319#endif
4320
Benjamin Walshacc68c12017-01-29 18:57:45 -05004321/* private - implementation data created as needed, per-type */
4322struct _poller {
4323 struct k_thread *thread;
Flavio Ceolin76b35182018-12-16 12:48:29 -08004324 volatile bool is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004325};
4326
4327/* private - types bit positions */
4328enum _poll_types_bits {
4329 /* can be used to ignore an event */
4330 _POLL_TYPE_IGNORE,
4331
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004332 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004333 _POLL_TYPE_SIGNAL,
4334
4335 /* semaphore availability */
4336 _POLL_TYPE_SEM_AVAILABLE,
4337
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004338 /* queue/fifo/lifo data availability */
4339 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004340
4341 _POLL_NUM_TYPES
4342};
4343
Patrik Flykt4344e272019-03-08 14:19:05 -07004344#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004345
4346/* private - states bit positions */
4347enum _poll_states_bits {
4348 /* default state when creating event */
4349 _POLL_STATE_NOT_READY,
4350
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004351 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004352 _POLL_STATE_SIGNALED,
4353
4354 /* semaphore is available */
4355 _POLL_STATE_SEM_AVAILABLE,
4356
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004357 /* data is available to read on queue/fifo/lifo */
4358 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004359
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004360 /* queue/fifo/lifo wait was cancelled */
4361 _POLL_STATE_CANCELLED,
4362
Benjamin Walshacc68c12017-01-29 18:57:45 -05004363 _POLL_NUM_STATES
4364};
4365
Patrik Flykt4344e272019-03-08 14:19:05 -07004366#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004367
4368#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004369 (32 - (0 \
4370 + 8 /* tag */ \
4371 + _POLL_NUM_TYPES \
4372 + _POLL_NUM_STATES \
4373 + 1 /* modes */ \
4374 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004375
Benjamin Walshacc68c12017-01-29 18:57:45 -05004376/* end of polling API - PRIVATE */
4377
4378
4379/**
4380 * @defgroup poll_apis Async polling APIs
4381 * @ingroup kernel_apis
4382 * @{
4383 */
4384
4385/* Public polling API */
4386
4387/* public - values for k_poll_event.type bitfield */
4388#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004389#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4390#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4391#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004392#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004393
4394/* public - polling modes */
4395enum k_poll_modes {
4396 /* polling thread does not take ownership of objects when available */
4397 K_POLL_MODE_NOTIFY_ONLY = 0,
4398
4399 K_POLL_NUM_MODES
4400};
4401
4402/* public - values for k_poll_event.state bitfield */
4403#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004404#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4405#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4406#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004407#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004408#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004409
4410/* public - poll signal object */
4411struct k_poll_signal {
4412 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004413 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004414
4415 /*
4416 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4417 * user resets it to 0.
4418 */
4419 unsigned int signaled;
4420
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004421 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004422 int result;
4423};
4424
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004425#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004426 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004427 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004428 .signaled = 0, \
4429 .result = 0, \
4430 }
4431
4432struct k_poll_event {
4433 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004434 sys_dnode_t _node;
4435
4436 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004437 struct _poller *poller;
4438
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004439 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004440 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004441
Benjamin Walshacc68c12017-01-29 18:57:45 -05004442 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004443 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004444
4445 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004446 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004447
4448 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004449 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004450
4451 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004452 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004453
4454 /* per-type data */
4455 union {
4456 void *obj;
4457 struct k_poll_signal *signal;
4458 struct k_sem *sem;
4459 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004460 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004461 };
4462};
4463
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004464#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004465 { \
4466 .poller = NULL, \
4467 .type = event_type, \
4468 .state = K_POLL_STATE_NOT_READY, \
4469 .mode = event_mode, \
4470 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004471 { .obj = event_obj }, \
4472 }
4473
4474#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4475 event_tag) \
4476 { \
4477 .type = event_type, \
4478 .tag = event_tag, \
4479 .state = K_POLL_STATE_NOT_READY, \
4480 .mode = event_mode, \
4481 .unused = 0, \
4482 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004483 }
4484
4485/**
4486 * @brief Initialize one struct k_poll_event instance
4487 *
4488 * After this routine is called on a poll event, the event it ready to be
4489 * placed in an event array to be passed to k_poll().
4490 *
4491 * @param event The event to initialize.
4492 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4493 * values. Only values that apply to the same object being polled
4494 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4495 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004496 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004497 * @param obj Kernel object or poll signal.
4498 *
4499 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004500 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004501 */
4502
Kumar Galacc334c72017-04-21 10:55:34 -05004503extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004504 int mode, void *obj);
4505
4506/**
4507 * @brief Wait for one or many of multiple poll events to occur
4508 *
4509 * This routine allows a thread to wait concurrently for one or many of
4510 * multiple poll events to have occurred. Such events can be a kernel object
4511 * being available, like a semaphore, or a poll signal event.
4512 *
4513 * When an event notifies that a kernel object is available, the kernel object
4514 * is not "given" to the thread calling k_poll(): it merely signals the fact
4515 * that the object was available when the k_poll() call was in effect. Also,
4516 * all threads trying to acquire an object the regular way, i.e. by pending on
4517 * the object, have precedence over the thread polling on the object. This
4518 * means that the polling thread will never get the poll event on an object
4519 * until the object becomes available and its pend queue is empty. For this
4520 * reason, the k_poll() call is more effective when the objects being polled
4521 * only have one thread, the polling thread, trying to acquire them.
4522 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004523 * When k_poll() returns 0, the caller should loop on all the events that were
4524 * passed to k_poll() and check the state field for the values that were
4525 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004526 *
4527 * Before being reused for another call to k_poll(), the user has to reset the
4528 * state field to K_POLL_STATE_NOT_READY.
4529 *
Andrew Boie3772f772018-05-07 16:52:57 -07004530 * When called from user mode, a temporary memory allocation is required from
4531 * the caller's resource pool.
4532 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004533 * @param events An array of pointers to events to be polled for.
4534 * @param num_events The number of events in the array.
4535 * @param timeout Waiting period for an event to be ready (in milliseconds),
4536 * or one of the special values K_NO_WAIT and K_FOREVER.
4537 *
4538 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004539 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004540 * @retval -EINTR Polling has been interrupted, e.g. with
4541 * k_queue_cancel_wait(). All output events are still set and valid,
4542 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4543 * words, -EINTR status means that at least one of output events is
4544 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004545 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4546 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004547 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004548 */
4549
Andrew Boie3772f772018-05-07 16:52:57 -07004550__syscall int k_poll(struct k_poll_event *events, int num_events,
4551 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004552
4553/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004554 * @brief Initialize a poll signal object.
4555 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004556 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004557 *
4558 * @param signal A poll signal.
4559 *
4560 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004561 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004562 */
4563
Andrew Boie3772f772018-05-07 16:52:57 -07004564__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4565
4566/*
4567 * @brief Reset a poll signal object's state to unsignaled.
4568 *
4569 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004570 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004571 */
4572__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4573
Patrik Flykt4344e272019-03-08 14:19:05 -07004574static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004575{
Patrik Flykt24d71432019-03-26 19:57:45 -06004576 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004577}
4578
4579/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004580 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004581 *
4582 * @param signal A poll signal object
4583 * @param signaled An integer buffer which will be written nonzero if the
4584 * object was signaled
4585 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004586 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004587 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004588 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004589 */
4590__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4591 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004592
4593/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004594 * @brief Signal a poll signal object.
4595 *
4596 * This routine makes ready a poll signal, which is basically a poll event of
4597 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4598 * made ready to run. A @a result value can be specified.
4599 *
4600 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004601 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004602 * k_poll_signal_reset(). It thus has to be reset by the user before being
4603 * passed again to k_poll() or k_poll() will consider it being signaled, and
4604 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004605 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004606 * @note The result is stored and the 'signaled' field is set even if
4607 * this function returns an error indicating that an expiring poll was
4608 * not notified. The next k_poll() will detect the missed raise.
4609 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004610 * @param signal A poll signal.
4611 * @param result The value to store in the result field of the signal.
4612 *
4613 * @retval 0 The signal was delivered successfully.
4614 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004615 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004616 */
4617
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004618__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004619
Anas Nashif954d5502018-02-25 08:37:28 -06004620/**
4621 * @internal
4622 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004623extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004624
Anas Nashif166f5192018-02-25 08:02:36 -06004625/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004626
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004627/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004628 * @defgroup cpu_idle_apis CPU Idling APIs
4629 * @ingroup kernel_apis
4630 * @{
4631 */
4632
4633/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004634 * @brief Make the CPU idle.
4635 *
4636 * This function makes the CPU idle until an event wakes it up.
4637 *
4638 * In a regular system, the idle thread should be the only thread responsible
4639 * for making the CPU idle and triggering any type of power management.
4640 * However, in some more constrained systems, such as a single-threaded system,
4641 * the only thread would be responsible for this if needed.
4642 *
4643 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004644 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004645 */
4646extern void k_cpu_idle(void);
4647
4648/**
4649 * @brief Make the CPU idle in an atomic fashion.
4650 *
4651 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4652 * must be done atomically before making the CPU idle.
4653 *
4654 * @param key Interrupt locking key obtained from irq_lock().
4655 *
4656 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004657 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004658 */
4659extern void k_cpu_atomic_idle(unsigned int key);
4660
Anas Nashif30c3cff2019-01-22 08:18:13 -05004661/**
4662 * @}
4663 */
Anas Nashif954d5502018-02-25 08:37:28 -06004664
4665/**
4666 * @internal
4667 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004668extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004669
Patrik Flykt4344e272019-03-08 14:19:05 -07004670#ifdef Z_ARCH_EXCEPT
Andrew Boiecdb94d62017-04-18 15:22:05 -07004671/* This archtecture has direct support for triggering a CPU exception */
Patrik Flykt4344e272019-03-08 14:19:05 -07004672#define z_except_reason(reason) Z_ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004673#else
4674
Andrew Boiecdb94d62017-04-18 15:22:05 -07004675/* NOTE: This is the implementation for arches that do not implement
Patrik Flykt4344e272019-03-08 14:19:05 -07004676 * Z_ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004677 *
4678 * We won't have a real exception frame to determine the PC value when
4679 * the oops occurred, so print file and line number before we jump into
4680 * the fatal error handler.
4681 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004682#define z_except_reason(reason) do { \
Andrew Boiecdb94d62017-04-18 15:22:05 -07004683 printk("@ %s:%d:\n", __FILE__, __LINE__); \
Andrew Boie56236372019-07-15 15:22:29 -07004684 z_fatal_error(reason, NULL); \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004685 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004686
4687#endif /* _ARCH__EXCEPT */
4688
4689/**
4690 * @brief Fatally terminate a thread
4691 *
4692 * This should be called when a thread has encountered an unrecoverable
4693 * runtime condition and needs to terminate. What this ultimately
4694 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004695 * will be called will reason code K_ERR_KERNEL_OOPS.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004696 *
4697 * If this is called from ISR context, the default system fatal error handler
4698 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004699 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004700 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004701#define k_oops() z_except_reason(K_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004702
4703/**
4704 * @brief Fatally terminate the system
4705 *
4706 * This should be called when the Zephyr kernel has encountered an
4707 * unrecoverable runtime condition and needs to terminate. What this ultimately
4708 * means is determined by the _fatal_error_handler() implementation, which
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004709 * will be called will reason code K_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004710 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004711 */
Andrew Boie71ce8ce2019-07-11 14:18:28 -07004712#define k_panic() z_except_reason(K_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004713
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004714/*
4715 * private APIs that are utilized by one or more public APIs
4716 */
4717
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004718#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004719/**
4720 * @internal
4721 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004722extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004723#else
Anas Nashif954d5502018-02-25 08:37:28 -06004724/**
4725 * @internal
4726 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004727#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004728#endif
4729
Anas Nashif954d5502018-02-25 08:37:28 -06004730/**
4731 * @internal
4732 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004733extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004734/**
4735 * @internal
4736 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004737extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004738
Andrew Boiedc5d9352017-06-02 12:56:47 -07004739/* arch/cpu.h may declare an architecture or platform-specific macro
4740 * for properly declaring stacks, compatible with MMU/MPU constraints if
4741 * enabled
4742 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004743
4744/**
4745 * @brief Obtain an extern reference to a stack
4746 *
4747 * This macro properly brings the symbol of a thread stack declared
4748 * elsewhere into scope.
4749 *
4750 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004751 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004752 */
4753#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4754
Patrik Flykt4344e272019-03-08 14:19:05 -07004755#ifdef Z_ARCH_THREAD_STACK_DEFINE
4756#define K_THREAD_STACK_DEFINE(sym, size) Z_ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004757#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Patrik Flykt4344e272019-03-08 14:19:05 -07004758 Z_ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4759#define K_THREAD_STACK_LEN(size) Z_ARCH_THREAD_STACK_LEN(size)
4760#define K_THREAD_STACK_MEMBER(sym, size) Z_ARCH_THREAD_STACK_MEMBER(sym, size)
4761#define K_THREAD_STACK_SIZEOF(sym) Z_ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie575abc02019-03-19 10:42:24 -07004762#define K_THREAD_STACK_RESERVED Z_ARCH_THREAD_STACK_RESERVED
Andrew Boie4e5c0932019-04-04 12:05:28 -07004763static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004764{
Patrik Flykt4344e272019-03-08 14:19:05 -07004765 return Z_ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07004766}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004767#else
4768/**
4769 * @brief Declare a toplevel thread stack memory region
4770 *
4771 * This declares a region of memory suitable for use as a thread's stack.
4772 *
4773 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4774 * 'noinit' section so that it isn't zeroed at boot
4775 *
Andrew Boie507852a2017-07-25 18:47:07 -07004776 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004777 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07004778 * inside needs to be examined, examine thread->stack_info for the associated
4779 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004780 *
4781 * It is legal to precede this definition with the 'static' keyword.
4782 *
4783 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4784 * parameter of k_thread_create(), it may not be the same as the
4785 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4786 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004787 * Some arches may round the size of the usable stack region up to satisfy
4788 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4789 * size.
4790 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004791 * @param sym Thread stack symbol name
4792 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004793 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004794 */
4795#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004796 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004797
4798/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304799 * @brief Calculate size of stacks to be allocated in a stack array
4800 *
4801 * This macro calculates the size to be allocated for the stacks
4802 * inside a stack array. It accepts the indicated "size" as a parameter
4803 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4804 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4805 *
4806 * @param size Size of the stack memory region
4807 * @req K-TSTACK-001
4808 */
4809#define K_THREAD_STACK_LEN(size) (size)
4810
4811/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004812 * @brief Declare a toplevel array of thread stack memory regions
4813 *
4814 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4815 * definition for additional details and constraints.
4816 *
4817 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4818 * 'noinit' section so that it isn't zeroed at boot
4819 *
4820 * @param sym Thread stack symbol name
4821 * @param nmemb Number of stacks to declare
4822 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004823 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004824 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004825#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004826 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304827 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004828
4829/**
4830 * @brief Declare an embedded stack memory region
4831 *
4832 * Used for stacks embedded within other data structures. Use is highly
4833 * discouraged but in some cases necessary. For memory protection scenarios,
4834 * it is very important that any RAM preceding this member not be writable
4835 * by threads else a stack overflow will lead to silent corruption. In other
4836 * words, the containing data structure should live in RAM owned by the kernel.
4837 *
4838 * @param sym Thread stack symbol name
4839 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004840 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004841 */
4842#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004843 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004844
4845/**
4846 * @brief Return the size in bytes of a stack memory region
4847 *
4848 * Convenience macro for passing the desired stack size to k_thread_create()
4849 * since the underlying implementation may actually create something larger
4850 * (for instance a guard area).
4851 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004852 * The value returned here is not guaranteed to match the 'size' parameter
4853 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004854 *
4855 * @param sym Stack memory symbol
4856 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004857 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004858 */
4859#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4860
Andrew Boie575abc02019-03-19 10:42:24 -07004861
4862/**
4863 * @brief Indicate how much additional memory is reserved for stack objects
4864 *
4865 * Any given stack declaration may have additional memory in it for guard
4866 * areas or supervisor mode stacks. This macro indicates how much space
4867 * is reserved for this. The memory reserved may not be contiguous within
4868 * the stack object, and does not account for additional space used due to
4869 * enforce alignment.
4870 */
4871#define K_THREAD_STACK_RESERVED 0
4872
Andrew Boiedc5d9352017-06-02 12:56:47 -07004873/**
4874 * @brief Get a pointer to the physical stack buffer
4875 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07004876 * This macro is deprecated. If a stack buffer needs to be examined, the
4877 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004878 *
4879 * @param sym Declared stack symbol name
4880 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004881 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004882 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07004883static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004884{
4885 return (char *)sym;
4886}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004887
4888#endif /* _ARCH_DECLARE_STACK */
4889
Chunlin Hane9c97022017-07-07 20:29:30 +08004890/**
4891 * @defgroup mem_domain_apis Memory domain APIs
4892 * @ingroup kernel_apis
4893 * @{
4894 */
4895
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004896/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004897 * @def K_MEM_PARTITION_DEFINE
4898 * @brief Used to declare a memory partition
4899 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004900 */
4901#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4902#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4903 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08004904 struct k_mem_partition name =\
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004905 { (uintptr_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004906#else
4907#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
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#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4911
Chunlin Hane9c97022017-07-07 20:29:30 +08004912/* memory partition */
4913struct k_mem_partition {
4914 /* start address of memory partition */
Nicolas Pitre58d839b2019-05-21 11:32:21 -04004915 uintptr_t start;
Chunlin Hane9c97022017-07-07 20:29:30 +08004916 /* size of memory partition */
4917 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004918#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08004919 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304920 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004921#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08004922};
4923
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02004924/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304925 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004926struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304927#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004928 /* partitions in the domain */
4929 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304930#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004931 /* domain q */
4932 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004933 /* number of partitions in the domain */
4934 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004935};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304936
Chunlin Hane9c97022017-07-07 20:29:30 +08004937
4938/**
4939 * @brief Initialize a memory domain.
4940 *
4941 * Initialize a memory domain with given name and memory partitions.
4942 *
Andrew Boiefddd5502019-07-30 18:07:32 -07004943 * See documentation for k_mem_domain_add_partition() for details about
4944 * partition constraints.
4945 *
Chunlin Hane9c97022017-07-07 20:29:30 +08004946 * @param domain The memory domain to be initialized.
4947 * @param num_parts The number of array items of "parts" parameter.
4948 * @param parts An array of pointers to the memory partitions. Can be NULL
4949 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004950 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004951 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004952extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004953 struct k_mem_partition *parts[]);
4954/**
4955 * @brief Destroy a memory domain.
4956 *
4957 * Destroy a memory domain.
4958 *
4959 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004960 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004961 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004962extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4963
4964/**
4965 * @brief Add a memory partition into a memory domain.
4966 *
Andrew Boiefddd5502019-07-30 18:07:32 -07004967 * Add a memory partition into a memory domain. Partitions must conform to
4968 * the following constraints:
4969 *
4970 * - Partition bounds must be within system RAM boundaries on MMU-based
4971 * systems.
4972 * - Partitions in the same memory domain may not overlap each other.
4973 * - Partitions must not be defined which expose private kernel
4974 * data structures or kernel objects.
4975 * - The starting address alignment, and the partition size must conform to
4976 * the constraints of the underlying memory management hardware, which
4977 * varies per architecture.
4978 * - Memory domain partitions are only intended to control access to memory
4979 * from user mode threads.
4980 *
4981 * Violating these constraints may lead to CPU exceptions or undefined
4982 * behavior.
Chunlin Hane9c97022017-07-07 20:29:30 +08004983 *
4984 * @param domain The memory domain to be added a memory partition.
4985 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004986 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004987 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004988extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4989 struct k_mem_partition *part);
4990
4991/**
4992 * @brief Remove a memory partition from a memory domain.
4993 *
4994 * Remove a memory partition from a memory domain.
4995 *
4996 * @param domain The memory domain to be removed a memory partition.
4997 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004998 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004999 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005000extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
5001 struct k_mem_partition *part);
5002
5003/**
5004 * @brief Add a thread into a memory domain.
5005 *
5006 * Add a thread into a memory domain.
5007 *
5008 * @param domain The memory domain that the thread is going to be added into.
5009 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005010 *
5011 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005012 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005013extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
5014 k_tid_t thread);
5015
5016/**
5017 * @brief Remove a thread from its memory domain.
5018 *
5019 * Remove a thread from its memory domain.
5020 *
5021 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005022 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08005023 */
Chunlin Hane9c97022017-07-07 20:29:30 +08005024extern void k_mem_domain_remove_thread(k_tid_t thread);
5025
Anas Nashif166f5192018-02-25 08:02:36 -06005026/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08005027
Andrew Boie756f9072017-10-10 16:01:49 -07005028/**
5029 * @brief Emit a character buffer to the console device
5030 *
5031 * @param c String of characters to print
5032 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04005033 *
5034 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07005035 */
5036__syscall void k_str_out(char *c, size_t n);
5037
Andy Rosse7172672018-01-24 15:48:32 -08005038/**
5039 * @brief Start a numbered CPU on a MP-capable system
5040
5041 * This starts and initializes a specific CPU. The main thread on
5042 * startup is running on CPU zero, other processors are numbered
5043 * sequentially. On return from this function, the CPU is known to
5044 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07005045 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08005046 * with the provided key will work to enable them.
5047 *
5048 * Normally, in SMP mode this function will be called by the kernel
5049 * initialization and should not be used as a user API. But it is
5050 * defined here for special-purpose apps which want Zephyr running on
5051 * one core and to use others for design-specific processing.
5052 *
5053 * @param cpu_num Integer number of the CPU
5054 * @param stack Stack memory for the CPU
5055 * @param sz Stack buffer size, in bytes
5056 * @param fn Function to begin running on the CPU. First argument is
5057 * an irq_unlock() key.
5058 * @param arg Untyped argument to be passed to "fn"
5059 */
Patrik Flykt4344e272019-03-08 14:19:05 -07005060extern void z_arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08005061 void (*fn)(int key, void *data), void *arg);
Andy Rosse7172672018-01-24 15:48:32 -08005062
Ioannis Glaropoulosa6cb8b02019-05-09 21:55:10 +02005063
5064/**
5065 * @brief Disable preservation of floating point context information.
5066 *
5067 * This routine informs the kernel that the specified thread
5068 * will no longer be using the floating point registers.
5069 *
5070 * @warning
5071 * Some architectures apply restrictions on how the disabling of floating
5072 * point preservation may be requested, see z_arch_float_disable.
5073 *
5074 * @warning
5075 * This routine should only be used to disable floating point support for
5076 * a thread that currently has such support enabled.
5077 *
5078 * @param thread ID of thread.
5079 *
5080 * @retval 0 On success.
5081 * @retval -ENOSYS If the floating point disabling is not implemented.
5082 * -EINVAL If the floating point disabling could not be performed.
5083 */
5084__syscall int k_float_disable(struct k_thread *thread);
5085
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005086#ifdef __cplusplus
5087}
5088#endif
5089
Anas Nashif10291a02019-06-25 12:25:12 -04005090#include <debug/tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07005091#include <syscalls/kernel.h>
5092
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05005093#endif /* !_ASMLANGUAGE */
5094
Flavio Ceolin67ca1762018-09-14 10:43:44 -07005095#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */