blob: 1acafea7a89e4653d52c075c41c3d1766a5cf2ce [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
Flavio Ceolin67ca1762018-09-14 10:43:44 -070013#ifndef ZEPHYR_INCLUDE_KERNEL_H_
14#define ZEPHYR_INCLUDE_KERNEL_H_
Benjamin Walsh456c6da2016-09-02 18:55:39 -040015
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Ioannis Glaropoulos92b8a412018-06-20 17:30:48 +020017#include <kernel_includes.h>
Kumar Gala8777ff12018-07-25 20:24:34 -050018#include <errno.h>
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -070019#include <stdbool.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020
21#ifdef __cplusplus
22extern "C" {
23#endif
24
Anas Nashifbbb157d2017-01-15 08:46:31 -050025/**
26 * @brief Kernel APIs
27 * @defgroup kernel_apis Kernel APIs
28 * @{
29 * @}
30 */
31
Anas Nashif61f4b242016-11-18 10:53:59 -050032#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040033#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
34#else
35#define K_DEBUG(fmt, ...)
36#endif
37
Benjamin Walsh2f280412017-01-14 19:23:46 -050038#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
39#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
40#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
41#elif defined(CONFIG_COOP_ENABLED)
42#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
43#define _NUM_PREEMPT_PRIO (0)
44#elif defined(CONFIG_PREEMPT_ENABLED)
45#define _NUM_COOP_PRIO (0)
46#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
47#else
48#error "invalid configuration"
49#endif
50
51#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040052#define K_PRIO_PREEMPT(x) (x)
53
Benjamin Walsh456c6da2016-09-02 18:55:39 -040054#define K_ANY NULL
55#define K_END NULL
56
Benjamin Walshedb35702017-01-14 18:47:22 -050057#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040058#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050059#elif defined(CONFIG_COOP_ENABLED)
60#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
61#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040062#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050063#else
64#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040065#endif
66
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050067#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040068#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
69#else
70#define K_LOWEST_THREAD_PRIO -1
71#endif
72
Benjamin Walshfab8d922016-11-08 15:36:36 -050073#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
74
Benjamin Walsh456c6da2016-09-02 18:55:39 -040075#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
76#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
77
Andy Ross225c74b2018-06-27 11:20:50 -070078#ifdef CONFIG_WAITQ_SCALABLE
Andy Ross1acd8c22018-05-03 14:51:49 -070079
80typedef struct {
81 struct _priq_rb waitq;
82} _wait_q_t;
83
Patrik Flykt4344e272019-03-08 14:19:05 -070084extern bool z_priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
Andy Ross1acd8c22018-05-03 14:51:49 -070085
Patrik Flykt4344e272019-03-08 14:19:05 -070086#define Z_WAIT_Q_INIT(wait_q) { { { .lessthan_fn = z_priq_rb_lessthan } } }
Andy Ross1acd8c22018-05-03 14:51:49 -070087
88#else
89
Andy Rossccf3bf72018-05-10 11:10:34 -070090typedef struct {
91 sys_dlist_t waitq;
92} _wait_q_t;
93
Patrik Flykt4344e272019-03-08 14:19:05 -070094#define Z_WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
Benjamin Walsh456c6da2016-09-02 18:55:39 -040095
Andy Ross1acd8c22018-05-03 14:51:49 -070096#endif
97
Anas Nashif2f203c22016-12-18 06:57:45 -050098#ifdef CONFIG_OBJECT_TRACING
Flavio Ceolind1ed3362018-12-07 11:39:13 -080099#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next;
Anas Nashif2f203c22016-12-18 06:57:45 -0500100#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400101#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500102#define _OBJECT_TRACING_INIT
Flavio Ceolind1ed3362018-12-07 11:39:13 -0800103#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400104#endif
105
Benjamin Walshacc68c12017-01-29 18:57:45 -0500106#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300107#define _POLL_EVENT_OBJ_INIT(obj) \
108 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
109#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500110#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300111#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500112#define _POLL_EVENT
113#endif
114
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500115struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400116struct k_mutex;
117struct k_sem;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400118struct k_msgq;
119struct k_mbox;
120struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200121struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122struct k_fifo;
123struct k_lifo;
124struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400125struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400126struct k_mem_pool;
127struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500128struct k_poll_event;
129struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800130struct k_mem_domain;
131struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400132
Andrew Boie5bd891d2017-09-27 12:59:28 -0700133/* This enumeration needs to be kept in sync with the lists of kernel objects
134 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
135 * function in kernel/userspace.c
136 */
Andrew Boie945af952017-08-22 13:15:23 -0700137enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700138 K_OBJ_ANY,
139
Leandro Pereirac2003672018-04-04 13:50:32 -0700140 /** @cond
141 * Doxygen should ignore this build-time generated include file
142 * when genrating API documentation. Enumeration values are
143 * generated during build by gen_kobject_list.py. It includes
144 * basic kernel objects (e.g. pipes and mutexes) and driver types.
145 */
146#include <kobj-types-enum.h>
147 /** @endcond
148 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700149
Andrew Boie945af952017-08-22 13:15:23 -0700150 K_OBJ_LAST
151};
Anas Nashif4bcb2942019-01-23 23:06:29 -0500152/**
153 * @defgroup usermode_apis User Mode APIs
154 * @ingroup kernel_apis
155 * @{
156 */
Andrew Boie945af952017-08-22 13:15:23 -0700157
158#ifdef CONFIG_USERSPACE
159/* Table generated by gperf, these objects are retrieved via
Patrik Flykt4344e272019-03-08 14:19:05 -0700160 * z_object_find() */
Andrew Boie945af952017-08-22 13:15:23 -0700161struct _k_object {
162 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700163 u8_t perms[CONFIG_MAX_THREAD_BYTES];
164 u8_t type;
165 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700166 u32_t data;
Andrew Boiedf555242018-05-25 07:28:54 -0700167} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700168
Andrew Boie877f82e2017-10-17 11:20:22 -0700169struct _k_object_assignment {
170 struct k_thread *thread;
171 void * const *objects;
172};
173
174/**
175 * @brief Grant a static thread access to a list of kernel objects
176 *
177 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
178 * a set of kernel objects. These objects do not need to be in an initialized
179 * state. The permissions will be granted when the threads are initialized
180 * in the early boot sequence.
181 *
182 * All arguments beyond the first must be pointers to kernel objects.
183 *
184 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
185 */
186#define K_THREAD_ACCESS_GRANT(name_, ...) \
187 static void * const _CONCAT(_object_list_, name_)[] = \
188 { __VA_ARGS__, NULL }; \
189 static __used __in_section_unique(object_access) \
190 const struct _k_object_assignment \
191 _CONCAT(_object_access_, name_) = \
192 { (&_k_thread_obj_ ## name_), \
193 (_CONCAT(_object_list_, name_)) }
194
Andrew Boie945af952017-08-22 13:15:23 -0700195#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700196#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700197#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie945af952017-08-22 13:15:23 -0700198
199/**
200 * Lookup a kernel object and init its metadata if it exists
201 *
202 * Calling this on an object will make it usable from userspace.
203 * Intended to be called as the last statement in kernel object init
204 * functions.
205 *
Anas Nashif50e3acd2018-12-08 13:26:18 -0500206 * @param obj Address of the kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700207 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700208void z_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700209#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700210
211#define K_THREAD_ACCESS_GRANT(thread, ...)
212
Anas Nashif954d5502018-02-25 08:37:28 -0600213/**
214 * @internal
215 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700216static inline void z_object_init(void *obj)
Andrew Boie743e4682017-10-04 12:25:50 -0700217{
218 ARG_UNUSED(obj);
219}
220
Anas Nashif954d5502018-02-25 08:37:28 -0600221/**
222 * @internal
223 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700224static inline void z_impl_k_object_access_grant(void *object,
Andrew Boie743e4682017-10-04 12:25:50 -0700225 struct k_thread *thread)
226{
227 ARG_UNUSED(object);
228 ARG_UNUSED(thread);
229}
230
Anas Nashif954d5502018-02-25 08:37:28 -0600231/**
232 * @internal
233 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700234static inline void k_object_access_revoke(void *object,
235 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700236{
237 ARG_UNUSED(object);
238 ARG_UNUSED(thread);
239}
240
Andrew Boiee9cfc542018-04-13 13:15:28 -0700241/**
242 * @internal
243 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700244static inline void z_impl_k_object_release(void *object)
Andrew Boiee9cfc542018-04-13 13:15:28 -0700245{
246 ARG_UNUSED(object);
247}
248
Andrew Boie41bab6e2017-10-14 14:42:23 -0700249static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700250{
251 ARG_UNUSED(object);
252}
253#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700254
255/**
256 * grant a thread access to a kernel object
257 *
258 * The thread will be granted access to the object if the caller is from
259 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700260 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700261 *
262 * @param object Address of kernel object
263 * @param thread Thread to grant access to the object
264 */
Andrew Boie743e4682017-10-04 12:25:50 -0700265__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700266
Andrew Boiea89bf012017-10-09 14:47:55 -0700267/**
268 * grant a thread access to a kernel object
269 *
270 * The thread will lose access to the object if the caller is from
271 * supervisor mode, or the caller is from user mode AND has permissions
272 * on both the object and the thread whose access is being revoked.
273 *
274 * @param object Address of kernel object
275 * @param thread Thread to remove access to the object
276 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700277void k_object_access_revoke(void *object, struct k_thread *thread);
278
279
280__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700281
282/**
283 * grant all present and future threads access to an object
284 *
285 * If the caller is from supervisor mode, or the caller is from user mode and
286 * have sufficient permissions on the object, then that object will have
287 * permissions granted to it for *all* current and future threads running in
288 * the system, effectively becoming a public kernel object.
289 *
290 * Use of this API should be avoided on systems that are running untrusted code
291 * as it is possible for such code to derive the addresses of kernel objects
292 * and perform unwanted operations on them.
293 *
Andrew Boie04caa672017-10-13 13:57:07 -0700294 * It is not possible to revoke permissions on public objects; once public,
295 * any thread may use it.
296 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700297 * @param object Address of kernel object
298 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700299void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700300
Andrew Boie31bdfc02017-11-08 16:38:03 -0800301/**
302 * Allocate a kernel object of a designated type
303 *
304 * This will instantiate at runtime a kernel object of the specified type,
305 * returning a pointer to it. The object will be returned in an uninitialized
306 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700307 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800308 *
309 * Currently, allocation of thread stacks is not supported.
310 *
311 * @param otype Requested kernel object type
312 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
313 * available
314 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700315__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800316
Andrew Boie97bf0012018-04-24 17:01:37 -0700317#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800318/**
319 * Free a kernel object previously allocated with k_object_alloc()
320 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700321 * This will return memory for a kernel object back to resource pool it was
322 * allocated from. Care must be exercised that the object will not be used
323 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800324 *
325 * @param obj Pointer to the kernel object memory address.
326 */
327void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700328#else
Patrik Flykt4344e272019-03-08 14:19:05 -0700329static inline void *z_impl_k_object_alloc(enum k_objects otype)
Andrew Boie97bf0012018-04-24 17:01:37 -0700330{
Kumar Gala85699f72018-05-17 09:26:03 -0500331 ARG_UNUSED(otype);
332
Andrew Boie97bf0012018-04-24 17:01:37 -0700333 return NULL;
334}
335
336static inline void k_obj_free(void *obj)
337{
338 ARG_UNUSED(obj);
339}
Andrew Boie31bdfc02017-11-08 16:38:03 -0800340#endif /* CONFIG_DYNAMIC_OBJECTS */
341
Anas Nashif4bcb2942019-01-23 23:06:29 -0500342/** @} */
343
Andrew Boiebca15da2017-10-15 14:17:48 -0700344/* Using typedef deliberately here, this is quite intended to be an opaque
Andrew Boie4e5c0932019-04-04 12:05:28 -0700345 * type.
Andrew Boiebca15da2017-10-15 14:17:48 -0700346 *
347 * The purpose of this data type is to clearly distinguish between the
348 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
349 * buffer which composes the stack data actually used by the underlying
350 * thread; they cannot be used interchangably as some arches precede the
351 * stack buffer region with guard areas that trigger a MPU or MMU fault
352 * if written to.
353 *
354 * APIs that want to work with the buffer inside should continue to use
355 * char *.
356 *
357 * Stacks should always be created with K_THREAD_STACK_DEFINE().
358 */
359struct __packed _k_thread_stack_element {
360 char data;
361};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700362typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700363
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700364/**
365 * @typedef k_thread_entry_t
366 * @brief Thread entry point function type.
367 *
368 * A thread's entry point function is invoked when the thread starts executing.
369 * Up to 3 argument values can be passed to the function.
370 *
371 * The thread terminates execution permanently if the entry point function
372 * returns. The thread is responsible for releasing any shared resources
373 * it may own (such as mutexes and dynamically allocated memory), prior to
374 * returning.
375 *
376 * @param p1 First argument.
377 * @param p2 Second argument.
378 * @param p3 Third argument.
379 *
380 * @return N/A
381 */
382typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700383
384#ifdef CONFIG_THREAD_MONITOR
385struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700386 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700387 void *parameter1;
388 void *parameter2;
389 void *parameter3;
390};
391#endif
392
393/* can be used for creating 'dummy' threads, e.g. for pending on objects */
394struct _thread_base {
395
396 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700397 union {
Peter A. Bigot82ad0d22019-01-03 23:49:28 -0600398 sys_dnode_t qnode_dlist;
Andy Ross1acd8c22018-05-03 14:51:49 -0700399 struct rbnode qnode_rb;
400 };
401
Andy Ross1acd8c22018-05-03 14:51:49 -0700402 /* wait queue on which the thread is pended (needed only for
403 * trees, not dumb lists)
404 */
405 _wait_q_t *pended_on;
Andrew Boie73abd322017-04-04 13:19:13 -0700406
407 /* user facing 'thread options'; values defined in include/kernel.h */
408 u8_t user_options;
409
410 /* thread state */
411 u8_t thread_state;
412
413 /*
414 * scheduler lock count and thread priority
415 *
416 * These two fields control the preemptibility of a thread.
417 *
418 * When the scheduler is locked, sched_locked is decremented, which
419 * means that the scheduler is locked for values from 0xff to 0x01. A
420 * thread is coop if its prio is negative, thus 0x80 to 0xff when
421 * looked at the value as unsigned.
422 *
423 * By putting them end-to-end, this means that a thread is
424 * non-preemptible if the bundled value is greater than or equal to
425 * 0x0080.
426 */
427 union {
428 struct {
429#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
430 u8_t sched_locked;
431 s8_t prio;
432#else /* LITTLE and PDP */
433 s8_t prio;
434 u8_t sched_locked;
435#endif
436 };
437 u16_t preempt;
438 };
439
Andy Ross4a2e50f2018-05-15 11:06:25 -0700440#ifdef CONFIG_SCHED_DEADLINE
441 int prio_deadline;
442#endif
443
Andy Ross1acd8c22018-05-03 14:51:49 -0700444 u32_t order_key;
445
Andy Ross2724fd12018-01-29 14:55:20 -0800446#ifdef CONFIG_SMP
447 /* True for the per-CPU idle threads */
448 u8_t is_idle;
449
Andy Ross2724fd12018-01-29 14:55:20 -0800450 /* CPU index on which thread was last run */
451 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700452
453 /* Recursive count of irq_lock() calls */
454 u8_t global_lock_count;
Andy Rossab46b1b2019-01-30 15:00:42 -0800455
456#endif
457
458#ifdef CONFIG_SCHED_CPU_MASK
459 /* "May run on" bits for each CPU */
460 u8_t cpu_mask;
Andy Ross2724fd12018-01-29 14:55:20 -0800461#endif
462
Andrew Boie73abd322017-04-04 13:19:13 -0700463 /* data returned by APIs */
464 void *swap_data;
465
466#ifdef CONFIG_SYS_CLOCK_EXISTS
467 /* this thread's entry in a timeout queue */
468 struct _timeout timeout;
469#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700470};
471
472typedef struct _thread_base _thread_base_t;
473
474#if defined(CONFIG_THREAD_STACK_INFO)
475/* Contains the stack information of a thread */
476struct _thread_stack_info {
Andrew Boie4e5c0932019-04-04 12:05:28 -0700477 /* Stack start - Represents the start address of the thread-writable
478 * stack area.
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700479 */
Andrew Boie73abd322017-04-04 13:19:13 -0700480 u32_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700481
482 /* Stack Size - Thread writable stack buffer size. Represents
483 * the size of the actual area, starting from the start member,
484 * that should be writable by the thread
485 */
Andrew Boie73abd322017-04-04 13:19:13 -0700486 u32_t size;
487};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700488
489typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700490#endif /* CONFIG_THREAD_STACK_INFO */
491
Chunlin Hane9c97022017-07-07 20:29:30 +0800492#if defined(CONFIG_USERSPACE)
493struct _mem_domain_info {
494 /* memory domain queue node */
495 sys_dnode_t mem_domain_q_node;
496 /* memory domain of the thread */
497 struct k_mem_domain *mem_domain;
498};
499
500#endif /* CONFIG_USERSPACE */
501
Daniel Leungfc182432018-08-16 15:42:28 -0700502#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
503struct _thread_userspace_local_data {
504 int errno_var;
505};
506#endif
507
Anas Nashifce78d162018-05-24 12:43:11 -0500508/**
509 * @ingroup thread_apis
510 * Thread Structure
511 */
Andrew Boie73abd322017-04-04 13:19:13 -0700512struct k_thread {
513
514 struct _thread_base base;
515
Anas Nashifce78d162018-05-24 12:43:11 -0500516 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700517 struct _callee_saved callee_saved;
518
Anas Nashifce78d162018-05-24 12:43:11 -0500519 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700520 void *init_data;
521
Anas Nashifce78d162018-05-24 12:43:11 -0500522 /**
523 * abort function
524 * @req K-THREAD-002
525 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700526 void (*fn_abort)(void);
527
528#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500529 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700530 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700531
Anas Nashifce78d162018-05-24 12:43:11 -0500532 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700533 struct k_thread *next_thread;
534#endif
535
Anas Nashif57554052018-03-03 02:31:05 -0600536#if defined(CONFIG_THREAD_NAME)
537 /* Thread name */
538 const char *name;
539#endif
540
Andrew Boie73abd322017-04-04 13:19:13 -0700541#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500542 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700543 void *custom_data;
544#endif
545
Daniel Leungfc182432018-08-16 15:42:28 -0700546#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
547 struct _thread_userspace_local_data *userspace_local_data;
548#endif
549
Andrew Boie73abd322017-04-04 13:19:13 -0700550#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700551#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500552 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700553 int errno_var;
554#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700555#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700556
557#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500558 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700559 struct _thread_stack_info stack_info;
560#endif /* CONFIG_THREAD_STACK_INFO */
561
Chunlin Hane9c97022017-07-07 20:29:30 +0800562#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500563 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800564 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500565 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700566 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800567#endif /* CONFIG_USERSPACE */
568
Andy Ross042d8ec2017-12-09 08:37:20 -0800569#if defined(CONFIG_USE_SWITCH)
570 /* When using __switch() a few previously arch-specific items
571 * become part of the core OS
572 */
573
Patrik Flykt4344e272019-03-08 14:19:05 -0700574 /** z_swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800575 int swap_retval;
576
Patrik Flykt7c0a2452019-03-14 09:20:46 -0600577 /** Context handle returned via z_arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800578 void *switch_handle;
579#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500580 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700581 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800582
Anas Nashifce78d162018-05-24 12:43:11 -0500583 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700584 struct _thread_arch arch;
585};
586
587typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400588typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400589
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400590enum execution_context_types {
591 K_ISR = 0,
592 K_COOP_THREAD,
593 K_PREEMPT_THREAD,
594};
595
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400596/**
Anas Nashif4bcb2942019-01-23 23:06:29 -0500597 * @addtogroup thread_apis
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100598 * @{
599 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530600typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
601 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100602
603/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530604 * @brief Iterate over all the threads in the system.
605 *
606 * This routine iterates over all the threads in the system and
607 * calls the user_cb function for each thread.
608 *
609 * @param user_cb Pointer to the user callback function.
610 * @param user_data Pointer to user data.
611 *
612 * @note CONFIG_THREAD_MONITOR must be set for this function
613 * to be effective. Also this API uses irq_lock to protect the
614 * _kernel.threads list which means creation of new threads and
615 * terminations of existing threads are blocked until this
616 * API returns.
617 *
618 * @return N/A
619 */
620extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
621
Anas Nashif166f5192018-02-25 08:02:36 -0600622/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100623
624/**
Allan Stephensc98da842016-11-11 15:45:03 -0500625 * @defgroup thread_apis Thread APIs
626 * @ingroup kernel_apis
627 * @{
628 */
629
Benjamin Walshed240f22017-01-22 13:05:08 -0500630#endif /* !_ASMLANGUAGE */
631
632
633/*
634 * Thread user options. May be needed by assembly code. Common part uses low
635 * bits, arch-specific use high bits.
636 */
637
Anas Nashifa541e932018-05-24 11:19:16 -0500638/**
639 * @brief system thread that must not abort
640 * @req K-THREAD-000
641 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700642#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500643
644#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500645/**
646 * @brief thread uses floating point registers
647 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700648#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500649#endif
650
Anas Nashifa541e932018-05-24 11:19:16 -0500651/**
652 * @brief user mode thread
653 *
654 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700655 * has additional restrictions
656 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700657#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700658
Anas Nashifa541e932018-05-24 11:19:16 -0500659/**
660 * @brief Inherit Permissions
661 *
662 * @details
663 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700664 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
665 * is not enabled.
666 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700667#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700668
Benjamin Walshed240f22017-01-22 13:05:08 -0500669#ifdef CONFIG_X86
670/* x86 Bitmask definitions for threads user options */
671
672#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
673/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700674#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500675#endif
676#endif
677
678/* end - thread options */
679
680#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400681/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700682 * @brief Create a thread.
683 *
684 * This routine initializes a thread, then schedules it for execution.
685 *
686 * The new thread may be scheduled for immediate execution or a delayed start.
687 * If the newly spawned thread does not have a delayed start the kernel
688 * scheduler may preempt the current thread to allow the new thread to
689 * execute.
690 *
691 * Thread options are architecture-specific, and can include K_ESSENTIAL,
692 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
693 * them using "|" (the logical OR operator).
694 *
695 * Historically, users often would use the beginning of the stack memory region
696 * to store the struct k_thread data, although corruption will occur if the
697 * stack overflows this region and stack protection features may not detect this
698 * situation.
699 *
700 * @param new_thread Pointer to uninitialized struct k_thread
701 * @param stack Pointer to the stack space.
702 * @param stack_size Stack size in bytes.
703 * @param entry Thread entry function.
704 * @param p1 1st entry point parameter.
705 * @param p2 2nd entry point parameter.
706 * @param p3 3rd entry point parameter.
707 * @param prio Thread priority.
708 * @param options Thread options.
709 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
710 *
711 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400712 *
713 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700714 */
Andrew Boie662c3452017-10-02 10:51:18 -0700715__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700716 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700717 size_t stack_size,
718 k_thread_entry_t entry,
719 void *p1, void *p2, void *p3,
720 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700721
Andrew Boie3f091b52017-08-30 14:34:14 -0700722/**
723 * @brief Drop a thread's privileges permanently to user mode
724 *
725 * @param entry Function to start executing from
726 * @param p1 1st entry point parameter
727 * @param p2 2nd entry point parameter
728 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400729 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700730 */
731extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
732 void *p1, void *p2,
733 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700734
Andrew Boied26cf2d2017-03-30 13:07:02 -0700735/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530736 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700737 *
738 * This is a convenience function. For the provided thread, grant access to
739 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700740 *
741 * The thread object must be initialized (i.e. running). The objects don't
742 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530743 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700744 *
745 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530746 * @param ... list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400747 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700748 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530749#define k_thread_access_grant(thread, ...) \
750 FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700751
752/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700753 * @brief Assign a resource memory pool to a thread
754 *
755 * By default, threads have no resource pool assigned unless their parent
756 * thread has a resource pool, in which case it is inherited. Multiple
757 * threads may be assigned to the same memory pool.
758 *
759 * Changing a thread's resource pool will not migrate allocations from the
760 * previous pool.
761 *
762 * @param thread Target thread to assign a memory pool for resource requests,
763 * or NULL if the thread should no longer have a memory pool.
764 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400765 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700766 */
767static inline void k_thread_resource_pool_assign(struct k_thread *thread,
768 struct k_mem_pool *pool)
769{
770 thread->resource_pool = pool;
771}
772
773#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
774/**
775 * @brief Assign the system heap as a thread's resource pool
776 *
777 * Similar to k_thread_resource_pool_assign(), but the thread will use
778 * the kernel heap to draw memory.
779 *
780 * Use with caution, as a malicious thread could perform DoS attacks on the
781 * kernel heap.
782 *
783 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400784 *
785 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700786 */
787void k_thread_system_pool_assign(struct k_thread *thread);
788#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
789
790/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500791 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400792 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700793 * This routine puts the current thread to sleep for @a duration milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400794 *
Charles E. Yousea5678312019-05-09 16:46:46 -0700795 * @param ms Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400796 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200797 * @return Zero if the requested time has elapsed or the number of milliseconds
798 * left to sleep, if thread was woken up by \ref k_wakeup call.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400799 */
Charles E. Yousea5678312019-05-09 16:46:46 -0700800__syscall s32_t k_sleep(s32_t ms);
801
802/**
803 * @brief Put the current thread to sleep with microsecond resolution.
804 *
805 * This function is unlikely to work as expected without kernel tuning.
806 * In particular, because the lower bound on the duration of a sleep is
807 * the duration of a tick, CONFIG_SYS_CLOCK_TICKS_PER_SEC must be adjusted
808 * to achieve the resolution desired. The implications of doing this must
809 * be understood before attempting to use k_usleep(). Use with caution.
810 *
811 * @param us Number of microseconds to sleep.
812 *
813 * @return Zero if the requested time has elapsed or the number of microseconds
814 * left to sleep, if thread was woken up by \ref k_wakeup call.
815 */
816__syscall s32_t k_usleep(s32_t us);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400817
818/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500819 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400820 *
821 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500822 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400823 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400824 * @return N/A
825 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800826__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827
828/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500829 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500831 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500833 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400834 *
835 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400836 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400837 */
Andrew Boie468190a2017-09-29 14:00:48 -0700838__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400839
840/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500841 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400842 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500843 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500845 * If @a thread is not currently sleeping, the routine has no effect.
846 *
847 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848 *
849 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400850 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 */
Andrew Boie468190a2017-09-29 14:00:48 -0700852__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400853
854/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500855 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500857 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400858 *
859 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400860 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700861__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400862
863/**
Allan Stephensc98da842016-11-11 15:45:03 -0500864 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500866 * This routine permanently stops execution of @a thread. The thread is taken
867 * off all kernel queues it is part of (i.e. the ready queue, the timeout
868 * queue, or a kernel object wait queue). However, any kernel resources the
869 * thread might currently own (such as mutexes or memory blocks) are not
870 * released. It is the responsibility of the caller of this routine to ensure
871 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400872 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500873 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400874 *
875 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400876 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400877 */
Andrew Boie468190a2017-09-29 14:00:48 -0700878__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400879
Andrew Boie7d627c52017-08-30 11:01:56 -0700880
881/**
882 * @brief Start an inactive thread
883 *
884 * If a thread was created with K_FOREVER in the delay parameter, it will
885 * not be added to the scheduling queue until this function is called
886 * on it.
887 *
888 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400889 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700890 */
Andrew Boie468190a2017-09-29 14:00:48 -0700891__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700892
Allan Stephensc98da842016-11-11 15:45:03 -0500893/**
894 * @cond INTERNAL_HIDDEN
895 */
896
Benjamin Walshd211a522016-12-06 11:44:01 -0500897/* timeout has timed out and is not on _timeout_q anymore */
898#define _EXPIRED (-2)
899
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400900struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700901 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700902 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400903 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700904 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500905 void *init_p1;
906 void *init_p2;
907 void *init_p3;
908 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500909 u32_t init_options;
910 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500911 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600912 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400913};
914
Andrew Boied26cf2d2017-03-30 13:07:02 -0700915#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400916 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600917 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500918 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700919 .init_thread = (thread), \
920 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500921 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700922 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400923 .init_p1 = (void *)p1, \
924 .init_p2 = (void *)p2, \
925 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500926 .init_prio = (prio), \
927 .init_options = (options), \
928 .init_delay = (delay), \
929 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600930 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400931 }
932
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400933/**
Allan Stephensc98da842016-11-11 15:45:03 -0500934 * INTERNAL_HIDDEN @endcond
935 */
936
937/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500938 * @brief Statically define and initialize a thread.
939 *
940 * The thread may be scheduled for immediate execution or a delayed start.
941 *
942 * Thread options are architecture-specific, and can include K_ESSENTIAL,
943 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
944 * them using "|" (the logical OR operator).
945 *
946 * The ID of the thread can be accessed using:
947 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500948 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500949 *
950 * @param name Name of the thread.
951 * @param stack_size Stack size in bytes.
952 * @param entry Thread entry function.
953 * @param p1 1st entry point parameter.
954 * @param p2 2nd entry point parameter.
955 * @param p3 3rd entry point parameter.
956 * @param prio Thread priority.
957 * @param options Thread options.
958 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400959 *
Anas Nashif47420d02018-05-24 14:20:56 -0400960 * @req K-THREAD-010
961 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400962 * @internal It has been observed that the x86 compiler by default aligns
963 * these _static_thread_data structures to 32-byte boundaries, thereby
964 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400965 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400966 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500967#define K_THREAD_DEFINE(name, stack_size, \
968 entry, p1, p2, p3, \
969 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700970 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie41f60112019-01-31 15:53:24 -0800971 struct k_thread _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500972 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500973 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700974 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
975 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500976 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -0600977 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700978 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400979
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400980/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500981 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500983 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400984 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500985 * @param thread ID of thread whose priority is needed.
986 *
987 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400988 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400989 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700990__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400991
992/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500993 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400994 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500995 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400996 *
997 * Rescheduling can occur immediately depending on the priority @a thread is
998 * set to:
999 *
1000 * - If its priority is raised above the priority of the caller of this
1001 * function, and the caller is preemptible, @a thread will be scheduled in.
1002 *
1003 * - If the caller operates on itself, it lowers its priority below that of
1004 * other threads in the system, and the caller is preemptible, the thread of
1005 * highest priority will be scheduled in.
1006 *
1007 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1008 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1009 * highest priority.
1010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001011 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001012 * @param prio New priority.
1013 *
1014 * @warning Changing the priority of a thread currently involved in mutex
1015 * priority inheritance may result in undefined behavior.
1016 *
1017 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001018 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001019 */
Andrew Boie468190a2017-09-29 14:00:48 -07001020__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001021
Andy Ross4a2e50f2018-05-15 11:06:25 -07001022
1023#ifdef CONFIG_SCHED_DEADLINE
1024/**
1025 * @brief Set deadline expiration time for scheduler
1026 *
1027 * This sets the "deadline" expiration as a time delta from the
1028 * current time, in the same units used by k_cycle_get_32(). The
1029 * scheduler (when deadline scheduling is enabled) will choose the
1030 * next expiring thread when selecting between threads at the same
1031 * static priority. Threads at different priorities will be scheduled
1032 * according to their static priority.
1033 *
1034 * @note Deadlines that are negative (i.e. in the past) are still seen
1035 * as higher priority than others, even if the thread has "finished"
1036 * its work. If you don't want it scheduled anymore, you have to
1037 * reset the deadline into the future, block/pend the thread, or
1038 * modify its priority with k_thread_priority_set().
1039 *
1040 * @note Despite the API naming, the scheduler makes no guarantees the
1041 * the thread WILL be scheduled within that deadline, nor does it take
1042 * extra metadata (like e.g. the "runtime" and "period" parameters in
1043 * Linux sched_setattr()) that allows the kernel to validate the
1044 * scheduling for achievability. Such features could be implemented
1045 * above this call, which is simply input to the priority selection
1046 * logic.
1047 *
1048 * @param thread A thread on which to set the deadline
1049 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001050 *
1051 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001052 */
1053__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1054#endif
1055
Andy Rossab46b1b2019-01-30 15:00:42 -08001056#ifdef CONFIG_SCHED_CPU_MASK
1057/**
1058 * @brief Sets all CPU enable masks to zero
1059 *
1060 * After this returns, the thread will no longer be schedulable on any
1061 * CPUs. The thread must not be currently runnable.
1062 *
1063 * @param thread Thread to operate upon
1064 * @return Zero on success, otherwise error code
1065 */
1066int k_thread_cpu_mask_clear(k_tid_t thread);
1067
1068/**
1069 * @brief Sets all CPU enable masks to one
1070 *
1071 * After this returns, the thread will be schedulable on any CPU. The
1072 * thread must not be currently runnable.
1073 *
1074 * @param thread Thread to operate upon
1075 * @return Zero on success, otherwise error code
1076 */
1077int k_thread_cpu_mask_enable_all(k_tid_t thread);
1078
1079/**
1080 * @brief Enable thread to run on specified CPU
1081 *
1082 * The thread must not be currently runnable.
1083 *
1084 * @param thread Thread to operate upon
1085 * @param cpu CPU index
1086 * @return Zero on success, otherwise error code
1087 */
1088int k_thread_cpu_mask_enable(k_tid_t thread, int cpu);
1089
1090/**
1091 * @brief Prevent thread to run on specified CPU
1092 *
1093 * The thread must not be currently runnable.
1094 *
1095 * @param thread Thread to operate upon
1096 * @param cpu CPU index
1097 * @return Zero on success, otherwise error code
1098 */
1099int k_thread_cpu_mask_disable(k_tid_t thread, int cpu);
1100#endif
1101
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001102/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001103 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001104 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001105 * This routine prevents the kernel scheduler from making @a thread the
1106 * current thread. All other internal operations on @a thread are still
1107 * performed; for example, any timeout it is waiting on keeps ticking,
1108 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001110 * If @a thread is already suspended, the routine has no effect.
1111 *
1112 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001113 *
1114 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001115 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001116 */
Andrew Boie468190a2017-09-29 14:00:48 -07001117__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001118
1119/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001120 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001121 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001122 * This routine allows the kernel scheduler to make @a thread the current
1123 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001125 * If @a thread is not currently suspended, the routine has no effect.
1126 *
1127 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001128 *
1129 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001130 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001131 */
Andrew Boie468190a2017-09-29 14:00:48 -07001132__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001133
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001134/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001135 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001137 * This routine specifies how the scheduler will perform time slicing of
1138 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001139 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001140 * To enable time slicing, @a slice must be non-zero. The scheduler
1141 * ensures that no thread runs for more than the specified time limit
1142 * before other threads of that priority are given a chance to execute.
1143 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001144 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001145 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001146 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001147 * execute. Once the scheduler selects a thread for execution, there is no
1148 * minimum guaranteed time the thread will execute before threads of greater or
1149 * equal priority are scheduled.
1150 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001151 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001152 * for execution, this routine has no effect; the thread is immediately
1153 * rescheduled after the slice period expires.
1154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001155 * To disable timeslicing, set both @a slice and @a prio to zero.
1156 *
1157 * @param slice Maximum time slice length (in milliseconds).
1158 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001159 *
1160 * @return N/A
1161 */
Kumar Galacc334c72017-04-21 10:55:34 -05001162extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001163
Anas Nashif166f5192018-02-25 08:02:36 -06001164/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001165
1166/**
1167 * @addtogroup isr_apis
1168 * @{
1169 */
1170
1171/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001172 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001173 *
Allan Stephensc98da842016-11-11 15:45:03 -05001174 * This routine allows the caller to customize its actions, depending on
1175 * whether it is a thread or an ISR.
1176 *
1177 * @note Can be called by ISRs.
1178 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001179 * @return false if invoked by a thread.
1180 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001181 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001182extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001183
Benjamin Walsh445830d2016-11-10 15:54:27 -05001184/**
1185 * @brief Determine if code is running in a preemptible thread.
1186 *
Allan Stephensc98da842016-11-11 15:45:03 -05001187 * This routine allows the caller to customize its actions, depending on
1188 * whether it can be preempted by another thread. The routine returns a 'true'
1189 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001190 *
Allan Stephensc98da842016-11-11 15:45:03 -05001191 * - The code is running in a thread, not at ISR.
1192 * - The thread's priority is in the preemptible range.
1193 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001194 *
Allan Stephensc98da842016-11-11 15:45:03 -05001195 * @note Can be called by ISRs.
1196 *
1197 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001198 * @return Non-zero if invoked by a preemptible thread.
1199 */
Andrew Boie468190a2017-09-29 14:00:48 -07001200__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001201
Allan Stephensc98da842016-11-11 15:45:03 -05001202/**
Anas Nashif166f5192018-02-25 08:02:36 -06001203 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001204 */
1205
1206/**
1207 * @addtogroup thread_apis
1208 * @{
1209 */
1210
1211/**
1212 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001213 *
Allan Stephensc98da842016-11-11 15:45:03 -05001214 * This routine prevents the current thread from being preempted by another
1215 * thread by instructing the scheduler to treat it as a cooperative thread.
1216 * If the thread subsequently performs an operation that makes it unready,
1217 * it will be context switched out in the normal manner. When the thread
1218 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001219 *
Allan Stephensc98da842016-11-11 15:45:03 -05001220 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001221 *
Allan Stephensc98da842016-11-11 15:45:03 -05001222 * @note k_sched_lock() and k_sched_unlock() should normally be used
1223 * when the operation being performed can be safely interrupted by ISRs.
1224 * However, if the amount of processing involved is very small, better
1225 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001226 *
1227 * @return N/A
1228 */
1229extern void k_sched_lock(void);
1230
Allan Stephensc98da842016-11-11 15:45:03 -05001231/**
1232 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001233 *
Allan Stephensc98da842016-11-11 15:45:03 -05001234 * This routine reverses the effect of a previous call to k_sched_lock().
1235 * A thread must call the routine once for each time it called k_sched_lock()
1236 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001237 *
1238 * @return N/A
1239 */
1240extern void k_sched_unlock(void);
1241
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001242/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001243 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001244 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001245 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001246 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001247 * Custom data is not used by the kernel itself, and is freely available
1248 * for a thread to use as it sees fit. It can be used as a framework
1249 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001251 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001252 *
1253 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001254 *
1255 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001256 */
Andrew Boie468190a2017-09-29 14:00:48 -07001257__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001258
1259/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001260 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001261 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001262 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001264 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001265 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001266 */
Andrew Boie468190a2017-09-29 14:00:48 -07001267__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001268
1269/**
Anas Nashif57554052018-03-03 02:31:05 -06001270 * @brief Set current thread name
1271 *
1272 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1273 * tracing and debugging.
1274 *
1275 */
1276__syscall void k_thread_name_set(k_tid_t thread_id, const char *value);
1277
1278/**
1279 * @brief Get thread name
1280 *
1281 * Get the name of a thread
1282 *
1283 * @param thread_id Thread ID
1284 *
1285 */
1286__syscall const char *k_thread_name_get(k_tid_t thread_id);
1287
1288/**
Andy Rosscfe62032018-09-29 07:34:55 -07001289 * @}
1290 */
1291
1292/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001293 * @addtogroup clock_apis
1294 * @{
1295 */
1296
1297/**
1298 * @brief Generate null timeout delay.
1299 *
1300 * This macro generates a timeout delay that that instructs a kernel API
1301 * not to wait if the requested operation cannot be performed immediately.
1302 *
1303 * @return Timeout delay value.
1304 */
1305#define K_NO_WAIT 0
1306
1307/**
1308 * @brief Generate timeout delay from milliseconds.
1309 *
1310 * This macro generates a timeout delay that that instructs a kernel API
1311 * to wait up to @a ms milliseconds to perform the requested operation.
1312 *
1313 * @param ms Duration in milliseconds.
1314 *
1315 * @return Timeout delay value.
1316 */
Johan Hedberg14471692016-11-13 10:52:15 +02001317#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001318
1319/**
1320 * @brief Generate timeout delay from seconds.
1321 *
1322 * This macro generates a timeout delay that that instructs a kernel API
1323 * to wait up to @a s seconds to perform the requested operation.
1324 *
1325 * @param s Duration in seconds.
1326 *
1327 * @return Timeout delay value.
1328 */
Johan Hedberg14471692016-11-13 10:52:15 +02001329#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001330
1331/**
1332 * @brief Generate timeout delay from minutes.
1333 *
1334 * This macro generates a timeout delay that that instructs a kernel API
1335 * to wait up to @a m minutes to perform the requested operation.
1336 *
1337 * @param m Duration in minutes.
1338 *
1339 * @return Timeout delay value.
1340 */
Johan Hedberg14471692016-11-13 10:52:15 +02001341#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001342
1343/**
1344 * @brief Generate timeout delay from hours.
1345 *
1346 * This macro generates a timeout delay that that instructs a kernel API
1347 * to wait up to @a h hours to perform the requested operation.
1348 *
1349 * @param h Duration in hours.
1350 *
1351 * @return Timeout delay value.
1352 */
Johan Hedberg14471692016-11-13 10:52:15 +02001353#define K_HOURS(h) K_MINUTES((h) * 60)
1354
Allan Stephensc98da842016-11-11 15:45:03 -05001355/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001356 * @brief Generate infinite timeout delay.
1357 *
1358 * This macro generates a timeout delay that that instructs a kernel API
1359 * to wait as long as necessary to perform the requested operation.
1360 *
1361 * @return Timeout delay value.
1362 */
1363#define K_FOREVER (-1)
1364
1365/**
Anas Nashif166f5192018-02-25 08:02:36 -06001366 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001367 */
1368
1369/**
Allan Stephensc98da842016-11-11 15:45:03 -05001370 * @cond INTERNAL_HIDDEN
1371 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001372
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001373struct k_timer {
1374 /*
1375 * _timeout structure must be first here if we want to use
1376 * dynamic timer allocation. timeout.node is used in the double-linked
1377 * list of free timers
1378 */
1379 struct _timeout timeout;
1380
Allan Stephens45bfa372016-10-12 12:39:42 -05001381 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001382 _wait_q_t wait_q;
1383
1384 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001385 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001386
1387 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001388 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001389
1390 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001391 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001392
Allan Stephens45bfa372016-10-12 12:39:42 -05001393 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001394 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001395
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001396 /* user-specific data, also used to support legacy features */
1397 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001398
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001399 _OBJECT_TRACING_NEXT_PTR(k_timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001400};
1401
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001402#define Z_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001403 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001404 .timeout = { \
1405 .node = {},\
1406 .dticks = 0, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001407 .fn = z_timer_expiration_handler \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001408 }, \
Patrik Flykt4344e272019-03-08 14:19:05 -07001409 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001410 .expiry_fn = expiry, \
1411 .stop_fn = stop, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001412 .period = 0, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001413 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001414 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001415 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001416 }
1417
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001418#define K_TIMER_INITIALIZER DEPRECATED_MACRO Z_TIMER_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001419
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001420/**
Allan Stephensc98da842016-11-11 15:45:03 -05001421 * INTERNAL_HIDDEN @endcond
1422 */
1423
1424/**
1425 * @defgroup timer_apis Timer APIs
1426 * @ingroup kernel_apis
1427 * @{
1428 */
1429
1430/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001431 * @typedef k_timer_expiry_t
1432 * @brief Timer expiry function type.
1433 *
1434 * A timer's expiry function is executed by the system clock interrupt handler
1435 * each time the timer expires. The expiry function is optional, and is only
1436 * invoked if the timer has been initialized with one.
1437 *
1438 * @param timer Address of timer.
1439 *
1440 * @return N/A
1441 */
1442typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1443
1444/**
1445 * @typedef k_timer_stop_t
1446 * @brief Timer stop function type.
1447 *
1448 * A timer's stop function is executed if the timer is stopped prematurely.
1449 * The function runs in the context of the thread that stops the timer.
1450 * The stop function is optional, and is only invoked if the timer has been
1451 * initialized with one.
1452 *
1453 * @param timer Address of timer.
1454 *
1455 * @return N/A
1456 */
1457typedef void (*k_timer_stop_t)(struct k_timer *timer);
1458
1459/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001460 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001461 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001462 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001463 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001464 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001465 *
1466 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001467 * @param expiry_fn Function to invoke each time the timer expires.
1468 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001469 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001470#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001471 struct k_timer name \
1472 __in_section(_k_timer, static, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06001473 Z_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001474
Allan Stephens45bfa372016-10-12 12:39:42 -05001475/**
1476 * @brief Initialize a timer.
1477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001478 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001479 *
1480 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001481 * @param expiry_fn Function to invoke each time the timer expires.
1482 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001483 *
1484 * @return N/A
1485 */
1486extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001487 k_timer_expiry_t expiry_fn,
1488 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001489
Allan Stephens45bfa372016-10-12 12:39:42 -05001490/**
1491 * @brief Start a timer.
1492 *
1493 * This routine starts a timer, and resets its status to zero. The timer
1494 * begins counting down using the specified duration and period values.
1495 *
1496 * Attempting to start a timer that is already running is permitted.
1497 * The timer's status is reset to zero and the timer begins counting down
1498 * using the new duration and period values.
1499 *
1500 * @param timer Address of timer.
1501 * @param duration Initial timer duration (in milliseconds).
1502 * @param period Timer period (in milliseconds).
1503 *
1504 * @return N/A
1505 */
Andrew Boiea354d492017-09-29 16:22:28 -07001506__syscall void k_timer_start(struct k_timer *timer,
1507 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001508
1509/**
1510 * @brief Stop a timer.
1511 *
1512 * This routine stops a running timer prematurely. The timer's stop function,
1513 * if one exists, is invoked by the caller.
1514 *
1515 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001516 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001517 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001518 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1519 * if @a k_timer_stop is to be called from ISRs.
1520 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001521 * @param timer Address of timer.
1522 *
1523 * @return N/A
1524 */
Andrew Boiea354d492017-09-29 16:22:28 -07001525__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001526
1527/**
1528 * @brief Read timer status.
1529 *
1530 * This routine reads the timer's status, which indicates the number of times
1531 * it has expired since its status was last read.
1532 *
1533 * Calling this routine resets the timer's status to zero.
1534 *
1535 * @param timer Address of timer.
1536 *
1537 * @return Timer status.
1538 */
Andrew Boiea354d492017-09-29 16:22:28 -07001539__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001540
1541/**
1542 * @brief Synchronize thread to timer expiration.
1543 *
1544 * This routine blocks the calling thread until the timer's status is non-zero
1545 * (indicating that it has expired at least once since it was last examined)
1546 * or the timer is stopped. If the timer status is already non-zero,
1547 * or the timer is already stopped, the caller continues without waiting.
1548 *
1549 * Calling this routine resets the timer's status to zero.
1550 *
1551 * This routine must not be used by interrupt handlers, since they are not
1552 * allowed to block.
1553 *
1554 * @param timer Address of timer.
1555 *
1556 * @return Timer status.
1557 */
Andrew Boiea354d492017-09-29 16:22:28 -07001558__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001559
Andy Ross52e444b2018-09-28 09:06:37 -07001560extern s32_t z_timeout_remaining(struct _timeout *timeout);
1561
Allan Stephens45bfa372016-10-12 12:39:42 -05001562/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001563 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001564 *
1565 * This routine computes the (approximate) time remaining before a running
1566 * timer next expires. If the timer is not running, it returns zero.
1567 *
1568 * @param timer Address of timer.
1569 *
1570 * @return Remaining time (in milliseconds).
1571 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001572__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001573
Patrik Flykt4344e272019-03-08 14:19:05 -07001574static inline u32_t z_impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001575{
Charles E. Youse0ad40222019-03-01 10:51:04 -08001576 const s32_t ticks = z_timeout_remaining(&timer->timeout);
1577 return (ticks > 0) ? (u32_t)__ticks_to_ms(ticks) : 0U;
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001578}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001579
Allan Stephensc98da842016-11-11 15:45:03 -05001580/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001581 * @brief Associate user-specific data with a timer.
1582 *
1583 * This routine records the @a user_data with the @a timer, to be retrieved
1584 * later.
1585 *
1586 * It can be used e.g. in a timer handler shared across multiple subsystems to
1587 * retrieve data specific to the subsystem this timer is associated with.
1588 *
1589 * @param timer Address of timer.
1590 * @param user_data User data to associate with the timer.
1591 *
1592 * @return N/A
1593 */
Andrew Boiea354d492017-09-29 16:22:28 -07001594__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1595
Anas Nashif954d5502018-02-25 08:37:28 -06001596/**
1597 * @internal
1598 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001599static inline void z_impl_k_timer_user_data_set(struct k_timer *timer,
Andrew Boiea354d492017-09-29 16:22:28 -07001600 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001601{
1602 timer->user_data = user_data;
1603}
1604
1605/**
1606 * @brief Retrieve the user-specific data from a timer.
1607 *
1608 * @param timer Address of timer.
1609 *
1610 * @return The user data.
1611 */
Andrew Boiea354d492017-09-29 16:22:28 -07001612__syscall void *k_timer_user_data_get(struct k_timer *timer);
1613
Patrik Flykt4344e272019-03-08 14:19:05 -07001614static inline void *z_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001615{
1616 return timer->user_data;
1617}
1618
Anas Nashif166f5192018-02-25 08:02:36 -06001619/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001620
Allan Stephensc98da842016-11-11 15:45:03 -05001621/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001622 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001623 * @{
1624 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001625
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001626/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001627 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001628 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001629 * This routine returns the elapsed time since the system booted,
1630 * in milliseconds.
1631 *
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001632 * @note While this function returns time in milliseconds, it does not mean it
1633 * has millisecond resolution. The actual resolution depends on
1634 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option, and with the default
1635 * setting of 100, system time is updated in increments of 10ms.
1636 *
1637 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001638 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001639__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001640
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001641/**
1642 * @brief Enable clock always on in tickless kernel
1643 *
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001644 * This routine enables keeping the clock running (that is, it always
1645 * keeps an active timer interrupt scheduled) when there are no timer
1646 * events programmed in tickless kernel scheduling. This is necessary
1647 * if the clock is used to track passage of time (e.g. via
1648 * k_uptime_get_32()), otherwise the internal hardware counter may
1649 * roll over between interrupts.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001650 *
1651 * @retval prev_status Previous status of always on flag
1652 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001653int k_enable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001654
1655/**
1656 * @brief Disable clock always on in tickless kernel
1657 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001658 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001659 * there are no timer events programmed in tickless kernel
1660 * scheduling. To save power, this routine should be called
1661 * immediately when clock is not used to track time.
1662 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001663void k_disable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001664
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001665/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001666 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001667 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001668 * This routine returns the lower 32-bits of the elapsed time since the system
1669 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001670 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001671 * This routine can be more efficient than k_uptime_get(), as it reduces the
1672 * need for interrupt locking and 64-bit math. However, the 32-bit result
1673 * cannot hold a system uptime time larger than approximately 50 days, so the
1674 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001675 *
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001676 * @note While this function returns time in milliseconds, it does not mean it
1677 * has millisecond resolution. The actual resolution depends on
1678 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option, and with the default
1679 * setting of 100, system time is updated in increments of 10ms.
1680 *
1681 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001682 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001683__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001684
1685/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001686 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001688 * This routine computes the elapsed time between the current system uptime
1689 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001691 * @param reftime Pointer to a reference time, which is updated to the current
1692 * uptime upon return.
1693 *
1694 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001695 */
Andy Ross987c0e52018-09-27 16:50:00 -07001696static inline s64_t k_uptime_delta(s64_t *reftime)
1697{
1698 s64_t uptime, delta;
1699
1700 uptime = k_uptime_get();
1701 delta = uptime - *reftime;
1702 *reftime = uptime;
1703
1704 return delta;
1705}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001706
1707/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001708 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001709 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001710 * This routine computes the elapsed time between the current system uptime
1711 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001712 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001713 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1714 * need for interrupt locking and 64-bit math. However, the 32-bit result
1715 * cannot hold an elapsed time larger than approximately 50 days, so the
1716 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001717 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001718 * @param reftime Pointer to a reference time, which is updated to the current
1719 * uptime upon return.
1720 *
1721 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001722 */
Andy Ross987c0e52018-09-27 16:50:00 -07001723static inline u32_t k_uptime_delta_32(s64_t *reftime)
1724{
1725 return (u32_t)k_uptime_delta(reftime);
1726}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001727
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001728/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001729 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001730 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001731 * This routine returns the current time, as measured by the system's hardware
1732 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001734 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001735 */
Patrik Flykt4344e272019-03-08 14:19:05 -07001736#define k_cycle_get_32() z_arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001737
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001738/**
Anas Nashif166f5192018-02-25 08:02:36 -06001739 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001740 */
1741
Allan Stephensc98da842016-11-11 15:45:03 -05001742/**
1743 * @cond INTERNAL_HIDDEN
1744 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001745
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001746struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001747 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001748 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001749 union {
1750 _wait_q_t wait_q;
1751
1752 _POLL_EVENT;
1753 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001754
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001755 _OBJECT_TRACING_NEXT_PTR(k_queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001756};
1757
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001758#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001759 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001760 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Patrik Flykt4344e272019-03-08 14:19:05 -07001761 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001762 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001763 _OBJECT_TRACING_INIT \
1764 }
1765
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001766#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1767
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001768extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1769
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001770/**
1771 * INTERNAL_HIDDEN @endcond
1772 */
1773
1774/**
1775 * @defgroup queue_apis Queue APIs
1776 * @ingroup kernel_apis
1777 * @{
1778 */
1779
1780/**
1781 * @brief Initialize a queue.
1782 *
1783 * This routine initializes a queue object, prior to its first use.
1784 *
1785 * @param queue Address of the queue.
1786 *
1787 * @return N/A
1788 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001789__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001790
1791/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001792 * @brief Cancel waiting on a queue.
1793 *
1794 * This routine causes first thread pending on @a queue, if any, to
1795 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001796 * If the queue is being waited on by k_poll(), it will return with
1797 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1798 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001799 *
1800 * @note Can be called by ISRs.
1801 *
1802 * @param queue Address of the queue.
1803 *
1804 * @return N/A
1805 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001806__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001807
1808/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001809 * @brief Append an element to the end of a queue.
1810 *
1811 * This routine appends a data item to @a queue. A queue data item must be
1812 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1813 * reserved for the kernel's use.
1814 *
1815 * @note Can be called by ISRs.
1816 *
1817 * @param queue Address of the queue.
1818 * @param data Address of the data item.
1819 *
1820 * @return N/A
1821 */
1822extern void k_queue_append(struct k_queue *queue, void *data);
1823
1824/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001825 * @brief Append an element to a queue.
1826 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001827 * This routine appends a data item to @a queue. There is an implicit memory
1828 * allocation to create an additional temporary bookkeeping data structure from
1829 * the calling thread's resource pool, which is automatically freed when the
1830 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001831 *
1832 * @note Can be called by ISRs.
1833 *
1834 * @param queue Address of the queue.
1835 * @param data Address of the data item.
1836 *
1837 * @retval 0 on success
1838 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1839 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301840__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001841
1842/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001843 * @brief Prepend an element to a queue.
1844 *
1845 * This routine prepends a data item to @a queue. A queue data item must be
1846 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1847 * reserved for the kernel's use.
1848 *
1849 * @note Can be called by ISRs.
1850 *
1851 * @param queue Address of the queue.
1852 * @param data Address of the data item.
1853 *
1854 * @return N/A
1855 */
1856extern void k_queue_prepend(struct k_queue *queue, void *data);
1857
1858/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001859 * @brief Prepend an element to a queue.
1860 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07001861 * This routine prepends a data item to @a queue. There is an implicit memory
1862 * allocation to create an additional temporary bookkeeping data structure from
1863 * the calling thread's resource pool, which is automatically freed when the
1864 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001865 *
1866 * @note Can be called by ISRs.
1867 *
1868 * @param queue Address of the queue.
1869 * @param data Address of the data item.
1870 *
1871 * @retval 0 on success
1872 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1873 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301874__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001875
1876/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001877 * @brief Inserts an element to a queue.
1878 *
1879 * This routine inserts a data item to @a queue after previous item. A queue
1880 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1881 * item are reserved for the kernel's use.
1882 *
1883 * @note Can be called by ISRs.
1884 *
1885 * @param queue Address of the queue.
1886 * @param prev Address of the previous data item.
1887 * @param data Address of the data item.
1888 *
1889 * @return N/A
1890 */
1891extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1892
1893/**
1894 * @brief Atomically append a list of elements to a queue.
1895 *
1896 * This routine adds a list of data items to @a queue in one operation.
1897 * The data items must be in a singly-linked list, with the first 32 bits
1898 * in each data item pointing to the next data item; the list must be
1899 * NULL-terminated.
1900 *
1901 * @note Can be called by ISRs.
1902 *
1903 * @param queue Address of the queue.
1904 * @param head Pointer to first node in singly-linked list.
1905 * @param tail Pointer to last node in singly-linked list.
1906 *
1907 * @return N/A
1908 */
1909extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1910
1911/**
1912 * @brief Atomically add a list of elements to a queue.
1913 *
1914 * This routine adds a list of data items to @a queue in one operation.
1915 * The data items must be in a singly-linked list implemented using a
1916 * sys_slist_t object. Upon completion, the original list is empty.
1917 *
1918 * @note Can be called by ISRs.
1919 *
1920 * @param queue Address of the queue.
1921 * @param list Pointer to sys_slist_t object.
1922 *
1923 * @return N/A
1924 */
1925extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1926
1927/**
1928 * @brief Get an element from a queue.
1929 *
1930 * This routine removes first data item from @a queue. The first 32 bits of the
1931 * data item are reserved for the kernel's use.
1932 *
1933 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1934 *
1935 * @param queue Address of the queue.
1936 * @param timeout Waiting period to obtain a data item (in milliseconds),
1937 * or one of the special values K_NO_WAIT and K_FOREVER.
1938 *
1939 * @return Address of the data item if successful; NULL if returned
1940 * without waiting, or waiting period timed out.
1941 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001942__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001943
1944/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001945 * @brief Remove an element from a queue.
1946 *
1947 * This routine removes data item from @a queue. The first 32 bits of the
1948 * data item are reserved for the kernel's use. Removing elements from k_queue
1949 * rely on sys_slist_find_and_remove which is not a constant time operation.
1950 *
1951 * @note Can be called by ISRs
1952 *
1953 * @param queue Address of the queue.
1954 * @param data Address of the data item.
1955 *
1956 * @return true if data item was removed
1957 */
1958static inline bool k_queue_remove(struct k_queue *queue, void *data)
1959{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001960 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001961}
1962
1963/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02001964 * @brief Append an element to a queue only if it's not present already.
1965 *
1966 * This routine appends data item to @a queue. The first 32 bits of the
1967 * data item are reserved for the kernel's use. Appending elements to k_queue
1968 * relies on sys_slist_is_node_in_list which is not a constant time operation.
1969 *
1970 * @note Can be called by ISRs
1971 *
1972 * @param queue Address of the queue.
1973 * @param data Address of the data item.
1974 *
1975 * @return true if data item was added, false if not
1976 */
1977static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
1978{
1979 sys_sfnode_t *test;
1980
1981 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
1982 if (test == (sys_sfnode_t *) data) {
1983 return false;
1984 }
1985 }
1986
1987 k_queue_append(queue, data);
1988 return true;
1989}
1990
1991/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001992 * @brief Query a queue to see if it has data available.
1993 *
1994 * Note that the data might be already gone by the time this function returns
1995 * if other threads are also trying to read from the queue.
1996 *
1997 * @note Can be called by ISRs.
1998 *
1999 * @param queue Address of the queue.
2000 *
2001 * @return Non-zero if the queue is empty.
2002 * @return 0 if data is available.
2003 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002004__syscall int k_queue_is_empty(struct k_queue *queue);
2005
Patrik Flykt4344e272019-03-08 14:19:05 -07002006static inline int z_impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002007{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002008 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002009}
2010
2011/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002012 * @brief Peek element at the head of queue.
2013 *
2014 * Return element from the head of queue without removing it.
2015 *
2016 * @param queue Address of the queue.
2017 *
2018 * @return Head element, or NULL if queue is empty.
2019 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002020__syscall void *k_queue_peek_head(struct k_queue *queue);
2021
Patrik Flykt4344e272019-03-08 14:19:05 -07002022static inline void *z_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002023{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002024 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002025}
2026
2027/**
2028 * @brief Peek element at the tail of queue.
2029 *
2030 * Return element from the tail of queue without removing it.
2031 *
2032 * @param queue Address of the queue.
2033 *
2034 * @return Tail element, or NULL if queue is empty.
2035 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002036__syscall void *k_queue_peek_tail(struct k_queue *queue);
2037
Patrik Flykt4344e272019-03-08 14:19:05 -07002038static inline void *z_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002039{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002040 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002041}
2042
2043/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002044 * @brief Statically define and initialize a queue.
2045 *
2046 * The queue can be accessed outside the module where it is defined using:
2047 *
2048 * @code extern struct k_queue <name>; @endcode
2049 *
2050 * @param name Name of the queue.
2051 */
2052#define K_QUEUE_DEFINE(name) \
2053 struct k_queue name \
2054 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002055 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002056
Anas Nashif166f5192018-02-25 08:02:36 -06002057/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002058
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002059struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002060 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002061};
2062
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002063/**
2064 * @cond INTERNAL_HIDDEN
2065 */
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002066#define Z_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002067 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002068 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002069 }
2070
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002071#define K_FIFO_INITIALIZER DEPRECATED_MACRO Z_FIFO_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002072
Allan Stephensc98da842016-11-11 15:45:03 -05002073/**
2074 * INTERNAL_HIDDEN @endcond
2075 */
2076
2077/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002078 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002079 * @ingroup kernel_apis
2080 * @{
2081 */
2082
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002083/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002084 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002085 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002086 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002087 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002088 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002089 *
2090 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002091 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002092 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002093#define k_fifo_init(fifo) \
2094 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002095
2096/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002097 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002098 *
2099 * This routine causes first thread pending on @a fifo, if any, to
2100 * return from k_fifo_get() call with NULL value (as if timeout
2101 * expired).
2102 *
2103 * @note Can be called by ISRs.
2104 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002105 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002106 *
2107 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002108 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002109 */
2110#define k_fifo_cancel_wait(fifo) \
2111 k_queue_cancel_wait((struct k_queue *) fifo)
2112
2113/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002114 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002115 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002116 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002117 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2118 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002119 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002120 * @note Can be called by ISRs.
2121 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002122 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002123 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002124 *
2125 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002126 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002127 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002128#define k_fifo_put(fifo, data) \
2129 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002130
2131/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002132 * @brief Add an element to a FIFO queue.
2133 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002134 * This routine adds a data item to @a fifo. There is an implicit memory
2135 * allocation to create an additional temporary bookkeeping data structure from
2136 * the calling thread's resource pool, which is automatically freed when the
2137 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002138 *
2139 * @note Can be called by ISRs.
2140 *
2141 * @param fifo Address of the FIFO.
2142 * @param data Address of the data item.
2143 *
2144 * @retval 0 on success
2145 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002146 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002147 */
2148#define k_fifo_alloc_put(fifo, data) \
2149 k_queue_alloc_append((struct k_queue *) fifo, data)
2150
2151/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002152 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002153 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002154 * This routine adds a list of data items to @a fifo in one operation.
2155 * The data items must be in a singly-linked list, with the first 32 bits
2156 * each data item pointing to the next data item; the list must be
2157 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002158 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002159 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002160 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002161 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002162 * @param head Pointer to first node in singly-linked list.
2163 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002164 *
2165 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002166 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002167 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002168#define k_fifo_put_list(fifo, head, tail) \
2169 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002170
2171/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002172 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002174 * This routine adds a list of data items to @a fifo in one operation.
2175 * The data items must be in a singly-linked list implemented using a
2176 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002177 * and must be re-initialized via sys_slist_init().
2178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002179 * @note Can be called by ISRs.
2180 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002181 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002182 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002183 *
2184 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002185 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002186 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002187#define k_fifo_put_slist(fifo, list) \
2188 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002189
2190/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002191 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002192 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002193 * This routine removes a data item from @a fifo in a "first in, first out"
2194 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002195 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002196 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2197 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002198 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002199 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002200 * or one of the special values K_NO_WAIT and K_FOREVER.
2201 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002202 * @return Address of the data item if successful; NULL if returned
2203 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002204 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002205 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002206#define k_fifo_get(fifo, timeout) \
2207 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002208
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002209/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002210 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002211 *
2212 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002213 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002214 *
2215 * @note Can be called by ISRs.
2216 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002217 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002218 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002219 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002220 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002221 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002222 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002223#define k_fifo_is_empty(fifo) \
2224 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002225
2226/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002227 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002228 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002229 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302230 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002231 * on each iteration of processing, a head container will be peeked,
2232 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002233 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002234 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002235 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002236 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002237 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002238 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002239 */
2240#define k_fifo_peek_head(fifo) \
2241 k_queue_peek_head((struct k_queue *) fifo)
2242
2243/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002244 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002245 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002246 * Return element from the tail of FIFO queue (without removing it). A usecase
2247 * for this is if elements of the FIFO queue are themselves containers. Then
2248 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002249 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002250 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002251 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002252 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002253 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002254 */
2255#define k_fifo_peek_tail(fifo) \
2256 k_queue_peek_tail((struct k_queue *) fifo)
2257
2258/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002259 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002260 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002261 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002262 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002263 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002264 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002265 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002266 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002267 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002268#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002269 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002270 __in_section(_k_queue, static, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06002271 Z_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002272
Anas Nashif166f5192018-02-25 08:02:36 -06002273/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002274
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002275struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002276 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002277};
2278
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002279/**
2280 * @cond INTERNAL_HIDDEN
2281 */
2282
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002283#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002284 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002285 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002286 }
2287
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002288#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2289
Allan Stephensc98da842016-11-11 15:45:03 -05002290/**
2291 * INTERNAL_HIDDEN @endcond
2292 */
2293
2294/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002295 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002296 * @ingroup kernel_apis
2297 * @{
2298 */
2299
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002300/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002301 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002302 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002303 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002304 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002305 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002306 *
2307 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002308 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002309 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002310#define k_lifo_init(lifo) \
2311 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002312
2313/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002314 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002315 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002316 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002317 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2318 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002319 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002320 * @note Can be called by ISRs.
2321 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002322 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002323 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002324 *
2325 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002326 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002327 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002328#define k_lifo_put(lifo, data) \
2329 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002330
2331/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002332 * @brief Add an element to a LIFO queue.
2333 *
Andrew Boieac3dcc12019-04-01 12:28:03 -07002334 * This routine adds a data item to @a lifo. There is an implicit memory
2335 * allocation to create an additional temporary bookkeeping data structure from
2336 * the calling thread's resource pool, which is automatically freed when the
2337 * item is removed. The data itself is not copied.
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002338 *
2339 * @note Can be called by ISRs.
2340 *
2341 * @param lifo Address of the LIFO.
2342 * @param data Address of the data item.
2343 *
2344 * @retval 0 on success
2345 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002346 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002347 */
2348#define k_lifo_alloc_put(lifo, data) \
2349 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2350
2351/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002352 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002353 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002354 * This routine removes a data item from @a lifo in a "last in, first out"
2355 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002356 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002357 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2358 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002359 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002360 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002361 * or one of the special values K_NO_WAIT and K_FOREVER.
2362 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002363 * @return Address of the data item if successful; NULL if returned
2364 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002365 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002366 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002367#define k_lifo_get(lifo, timeout) \
2368 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002369
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002370/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002371 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002372 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002373 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002374 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002375 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002376 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002377 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002378 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002379 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002380#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002381 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002382 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002383 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002384
Anas Nashif166f5192018-02-25 08:02:36 -06002385/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002386
2387/**
2388 * @cond INTERNAL_HIDDEN
2389 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302390#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002391
2392struct k_stack {
2393 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002394 struct k_spinlock lock;
Kumar Galacc334c72017-04-21 10:55:34 -05002395 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002396
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002397 _OBJECT_TRACING_NEXT_PTR(k_stack)
Andrew Boief3bee952018-05-02 17:44:39 -07002398 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002399};
2400
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002401#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002402 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002403 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002404 .base = stack_buffer, \
2405 .next = stack_buffer, \
2406 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002407 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002408 }
2409
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002410#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2411
Allan Stephensc98da842016-11-11 15:45:03 -05002412/**
2413 * INTERNAL_HIDDEN @endcond
2414 */
2415
2416/**
2417 * @defgroup stack_apis Stack APIs
2418 * @ingroup kernel_apis
2419 * @{
2420 */
2421
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002422/**
2423 * @brief Initialize a stack.
2424 *
2425 * This routine initializes a stack object, prior to its first use.
2426 *
2427 * @param stack Address of the stack.
2428 * @param buffer Address of array used to hold stacked values.
2429 * @param num_entries Maximum number of values that can be stacked.
2430 *
2431 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002432 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002433 */
Andrew Boief3bee952018-05-02 17:44:39 -07002434void k_stack_init(struct k_stack *stack,
Adithya Baglody28080d32018-10-15 11:48:51 +05302435 u32_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002436
2437
2438/**
2439 * @brief Initialize a stack.
2440 *
2441 * This routine initializes a stack object, prior to its first use. Internal
2442 * buffers will be allocated from the calling thread's resource pool.
2443 * This memory will be released if k_stack_cleanup() is called, or
2444 * userspace is enabled and the stack object loses all references to it.
2445 *
2446 * @param stack Address of the stack.
2447 * @param num_entries Maximum number of values that can be stacked.
2448 *
2449 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002450 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002451 */
2452
Adithya Baglody28080d32018-10-15 11:48:51 +05302453__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2454 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002455
2456/**
2457 * @brief Release a stack's allocated buffer
2458 *
2459 * If a stack object was given a dynamically allocated buffer via
2460 * k_stack_alloc_init(), this will free it. This function does nothing
2461 * if the buffer wasn't dynamically allocated.
2462 *
2463 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002464 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002465 */
2466void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002467
2468/**
2469 * @brief Push an element onto a stack.
2470 *
2471 * This routine adds a 32-bit value @a data to @a stack.
2472 *
2473 * @note Can be called by ISRs.
2474 *
2475 * @param stack Address of the stack.
2476 * @param data Value to push onto the stack.
2477 *
2478 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002479 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002480 */
Andrew Boiee8734462017-09-29 16:42:07 -07002481__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002482
2483/**
2484 * @brief Pop an element from a stack.
2485 *
2486 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2487 * manner and stores the value in @a data.
2488 *
2489 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2490 *
2491 * @param stack Address of the stack.
2492 * @param data Address of area to hold the value popped from the stack.
2493 * @param timeout Waiting period to obtain a value (in milliseconds),
2494 * or one of the special values K_NO_WAIT and K_FOREVER.
2495 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002496 * @retval 0 Element popped from stack.
2497 * @retval -EBUSY Returned without waiting.
2498 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002499 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002500 */
Andrew Boiee8734462017-09-29 16:42:07 -07002501__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002502
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002503/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002504 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002506 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002507 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002508 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002510 * @param name Name of the stack.
2511 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002512 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002513 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002514#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002515 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002516 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002517 struct k_stack name \
2518 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002519 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002520 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002521
Anas Nashif166f5192018-02-25 08:02:36 -06002522/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002523
Allan Stephens6bba9b02016-11-16 14:56:54 -05002524struct k_work;
2525
Allan Stephensc98da842016-11-11 15:45:03 -05002526/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002527 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002528 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002529 */
2530
Allan Stephens6bba9b02016-11-16 14:56:54 -05002531/**
2532 * @typedef k_work_handler_t
2533 * @brief Work item handler function type.
2534 *
2535 * A work item's handler function is executed by a workqueue's thread
2536 * when the work item is processed by the workqueue.
2537 *
2538 * @param work Address of the work item.
2539 *
2540 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002541 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002542 */
2543typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002544
2545/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002546 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002547 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002548
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002549struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002550 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002551 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002552};
2553
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002554enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002555 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556};
2557
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002558struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002559 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002560 k_work_handler_t handler;
2561 atomic_t flags[1];
2562};
2563
Allan Stephens6bba9b02016-11-16 14:56:54 -05002564struct k_delayed_work {
2565 struct k_work work;
2566 struct _timeout timeout;
2567 struct k_work_q *work_q;
2568};
2569
2570extern struct k_work_q k_sys_work_q;
2571
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002572/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002573 * INTERNAL_HIDDEN @endcond
2574 */
2575
Patrik Flykt4344e272019-03-08 14:19:05 -07002576#define Z_WORK_INITIALIZER(work_handler) \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002577 { \
2578 ._reserved = NULL, \
2579 .handler = work_handler, \
2580 .flags = { 0 } \
2581 }
2582
Patrik Flykt4344e272019-03-08 14:19:05 -07002583#define K_WORK_INITIALIZER DEPRECATED_MACRO Z_WORK_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002584
Allan Stephens6bba9b02016-11-16 14:56:54 -05002585/**
2586 * @brief Initialize a statically-defined work item.
2587 *
2588 * This macro can be used to initialize a statically-defined workqueue work
2589 * item, prior to its first use. For example,
2590 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002591 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002592 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002593 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002594 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002595 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002596 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002597#define K_WORK_DEFINE(work, work_handler) \
Patrik Flykt4344e272019-03-08 14:19:05 -07002598 struct k_work work = Z_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002599
2600/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002601 * @brief Initialize a work item.
2602 *
2603 * This routine initializes a workqueue work item, prior to its first use.
2604 *
2605 * @param work Address of work item.
2606 * @param handler Function to invoke each time work item is processed.
2607 *
2608 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002609 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002610 */
2611static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2612{
Patrik Flykt4344e272019-03-08 14:19:05 -07002613 *work = (struct k_work)Z_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002614}
2615
2616/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002617 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002618 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002619 * This routine submits work item @a work to be processed by workqueue
2620 * @a work_q. If the work item is already pending in the workqueue's queue
2621 * as a result of an earlier submission, this routine has no effect on the
2622 * work item. If the work item has already been processed, or is currently
2623 * being processed, its work is considered complete and the work item can be
2624 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002625 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002626 * @warning
2627 * A submitted work item must not be modified until it has been processed
2628 * by the workqueue.
2629 *
2630 * @note Can be called by ISRs.
2631 *
2632 * @param work_q Address of workqueue.
2633 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002634 *
2635 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002636 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002637 */
2638static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2639 struct k_work *work)
2640{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002641 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002642 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002643 }
2644}
2645
2646/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002647 * @brief Submit a work item to a user mode workqueue
2648 *
David B. Kinder06d78352018-12-17 14:32:40 -08002649 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002650 * memory allocation is made from the caller's resource pool which is freed
2651 * once the worker thread consumes the k_work item. The workqueue
2652 * thread must have memory access to the k_work item being submitted. The caller
2653 * must have permission granted on the work_q parameter's queue object.
2654 *
2655 * Otherwise this works the same as k_work_submit_to_queue().
2656 *
2657 * @note Can be called by ISRs.
2658 *
2659 * @param work_q Address of workqueue.
2660 * @param work Address of work item.
2661 *
2662 * @retval -EBUSY if the work item was already in some workqueue
2663 * @retval -ENOMEM if no memory for thread resource pool allocation
2664 * @retval 0 Success
2665 * @req K-WORK-001
2666 */
2667static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2668 struct k_work *work)
2669{
2670 int ret = -EBUSY;
2671
2672 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2673 ret = k_queue_alloc_append(&work_q->queue, work);
2674
2675 /* Couldn't insert into the queue. Clear the pending bit
2676 * so the work item can be submitted again
2677 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002678 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002679 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2680 }
2681 }
2682
2683 return ret;
2684}
2685
2686/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002687 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002688 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002689 * This routine indicates if work item @a work is pending in a workqueue's
2690 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002691 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002692 * @note Can be called by ISRs.
2693 *
2694 * @param work Address of work item.
2695 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002696 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002697 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002698 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002699static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002700{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002701 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002702}
2703
2704/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002705 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002706 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002707 * This routine starts workqueue @a work_q. The workqueue spawns its work
2708 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002709 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002710 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002711 * @param stack Pointer to work queue thread's stack space, as defined by
2712 * K_THREAD_STACK_DEFINE()
2713 * @param stack_size Size of the work queue thread's stack (in bytes), which
2714 * should either be the same constant passed to
2715 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002716 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002717 *
2718 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002719 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002720 */
Andrew Boie507852a2017-07-25 18:47:07 -07002721extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002722 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002723 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002724
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002725/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002726 * @brief Start a workqueue in user mode
2727 *
2728 * This works identically to k_work_q_start() except it is callable from user
2729 * mode, and the worker thread created will run in user mode.
2730 * The caller must have permissions granted on both the work_q parameter's
2731 * thread and queue objects, and the same restrictions on priority apply as
2732 * k_thread_create().
2733 *
2734 * @param work_q Address of workqueue.
2735 * @param stack Pointer to work queue thread's stack space, as defined by
2736 * K_THREAD_STACK_DEFINE()
2737 * @param stack_size Size of the work queue thread's stack (in bytes), which
2738 * should either be the same constant passed to
2739 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2740 * @param prio Priority of the work queue's thread.
2741 *
2742 * @return N/A
2743 * @req K-WORK-001
2744 */
2745extern void k_work_q_user_start(struct k_work_q *work_q,
2746 k_thread_stack_t *stack,
2747 size_t stack_size, int prio);
2748
2749/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002750 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002751 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002752 * This routine initializes a workqueue delayed work item, prior to
2753 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002754 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002755 * @param work Address of delayed work item.
2756 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002757 *
2758 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002759 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002760 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002761extern void k_delayed_work_init(struct k_delayed_work *work,
2762 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002763
2764/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002765 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002766 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002767 * This routine schedules work item @a work to be processed by workqueue
2768 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002769 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002770 * Only when the countdown completes is the work item actually submitted to
2771 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002772 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002773 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002774 * counting down cancels the existing submission and restarts the
2775 * countdown using the new delay. Note that this behavior is
2776 * inherently subject to race conditions with the pre-existing
2777 * timeouts and work queue, so care must be taken to synchronize such
2778 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002779 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002780 * @warning
2781 * A delayed work item must not be modified until it has been processed
2782 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002783 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002784 * @note Can be called by ISRs.
2785 *
2786 * @param work_q Address of workqueue.
2787 * @param work Address of delayed work item.
2788 * @param delay Delay before submitting the work item (in milliseconds).
2789 *
2790 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002791 * @retval -EINVAL Work item is being processed or has completed its work.
2792 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002793 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002794 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002795extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2796 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002797 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002798
2799/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002800 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002801 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002802 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002803 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002804 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002805 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002806 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002807 *
Andy Rossd7ae2a82019-03-08 08:51:13 -08002808 * @note The result of calling this on a k_delayed_work item that has
2809 * not been submitted (i.e. before the return of the
2810 * k_delayed_work_submit_to_queue() call) is undefined.
2811 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002812 * @param work Address of delayed work item.
2813 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002814 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002815 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002816 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002817 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002818extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002819
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002820/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002821 * @brief Submit a work item to the system workqueue.
2822 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002823 * This routine submits work item @a work to be processed by the system
2824 * workqueue. If the work item is already pending in the workqueue's queue
2825 * as a result of an earlier submission, this routine has no effect on the
2826 * work item. If the work item has already been processed, or is currently
2827 * being processed, its work is considered complete and the work item can be
2828 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002829 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002830 * @warning
2831 * Work items submitted to the system workqueue should avoid using handlers
2832 * that block or yield since this may prevent the system workqueue from
2833 * processing other work items in a timely manner.
2834 *
2835 * @note Can be called by ISRs.
2836 *
2837 * @param work Address of work item.
2838 *
2839 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002840 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002841 */
2842static inline void k_work_submit(struct k_work *work)
2843{
2844 k_work_submit_to_queue(&k_sys_work_q, work);
2845}
2846
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002847/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002848 * @brief Submit a delayed work item to the system workqueue.
2849 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002850 * This routine schedules work item @a work to be processed by the system
2851 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002852 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002853 * Only when the countdown completes is the work item actually submitted to
2854 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002855 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002856 * Submitting a previously submitted delayed work item that is still
2857 * counting down cancels the existing submission and restarts the countdown
2858 * using the new delay. If the work item is currently pending on the
2859 * workqueue's queue because the countdown has completed it is too late to
2860 * resubmit the item, and resubmission fails without impacting the work item.
2861 * If the work item has already been processed, or is currently being processed,
2862 * its work is considered complete and the work item can be resubmitted.
2863 *
2864 * @warning
2865 * Work items submitted to the system workqueue should avoid using handlers
2866 * that block or yield since this may prevent the system workqueue from
2867 * processing other work items in a timely manner.
2868 *
2869 * @note Can be called by ISRs.
2870 *
2871 * @param work Address of delayed work item.
2872 * @param delay Delay before submitting the work item (in milliseconds).
2873 *
2874 * @retval 0 Work item countdown started.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002875 * @retval -EINVAL Work item is being processed or has completed its work.
2876 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002877 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002878 */
2879static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002880 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002881{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002882 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002883}
2884
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002885/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002886 * @brief Get time remaining before a delayed work gets scheduled.
2887 *
2888 * This routine computes the (approximate) time remaining before a
2889 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002890 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002891 *
2892 * @param work Delayed work item.
2893 *
2894 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002895 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002896 */
Kumar Galacc334c72017-04-21 10:55:34 -05002897static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002898{
Andy Ross52e444b2018-09-28 09:06:37 -07002899 return __ticks_to_ms(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02002900}
2901
Anas Nashif166f5192018-02-25 08:02:36 -06002902/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002903/**
Anas Nashifce78d162018-05-24 12:43:11 -05002904 * @defgroup mutex_apis Mutex APIs
2905 * @ingroup kernel_apis
2906 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002907 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002908
Anas Nashifce78d162018-05-24 12:43:11 -05002909/**
2910 * Mutex Structure
2911 * @ingroup mutex_apis
2912 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002913struct k_mutex {
2914 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002915 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002916 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002917 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002918 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002919
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002920 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002921};
2922
Anas Nashifce78d162018-05-24 12:43:11 -05002923/**
2924 * @cond INTERNAL_HIDDEN
2925 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002926#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002927 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07002928 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002929 .owner = NULL, \
2930 .lock_count = 0, \
2931 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002932 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002933 }
2934
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002935#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2936
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002937/**
Allan Stephensc98da842016-11-11 15:45:03 -05002938 * INTERNAL_HIDDEN @endcond
2939 */
2940
2941/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002942 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002943 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002944 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002945 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002946 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002947 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002948 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002949 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002950 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002951#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002952 struct k_mutex name \
2953 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002954 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002955
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002956/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002957 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002958 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002959 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002960 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002961 * Upon completion, the mutex is available and does not have an owner.
2962 *
2963 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002964 *
2965 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002966 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002967 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002968__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002969
2970/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002971 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002973 * This routine locks @a mutex. If the mutex is locked by another thread,
2974 * the calling thread waits until the mutex becomes available or until
2975 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002977 * A thread is permitted to lock a mutex it has already locked. The operation
2978 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002979 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002980 * @param mutex Address of the mutex.
2981 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002982 * or one of the special values K_NO_WAIT and K_FOREVER.
2983 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002984 * @retval 0 Mutex locked.
2985 * @retval -EBUSY Returned without waiting.
2986 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002987 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002988 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002989__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002990
2991/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002992 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002994 * This routine unlocks @a mutex. The mutex must already be locked by the
2995 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002996 *
2997 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002998 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002999 * thread.
3000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003001 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003002 *
3003 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003004 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003005 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07003006__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003007
Allan Stephensc98da842016-11-11 15:45:03 -05003008/**
Anas Nashif166f5192018-02-25 08:02:36 -06003009 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003010 */
3011
3012/**
3013 * @cond INTERNAL_HIDDEN
3014 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003015
3016struct k_sem {
3017 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303018 u32_t count;
3019 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003020 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003021
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003022 _OBJECT_TRACING_NEXT_PTR(k_sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003023};
3024
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003025#define Z_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003026 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003027 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003028 .count = initial_count, \
3029 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003030 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003031 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003032 }
3033
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003034#define K_SEM_INITIALIZER DEPRECATED_MACRO Z_SEM_INITIALIZER
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003035
Allan Stephensc98da842016-11-11 15:45:03 -05003036/**
3037 * INTERNAL_HIDDEN @endcond
3038 */
3039
3040/**
3041 * @defgroup semaphore_apis Semaphore APIs
3042 * @ingroup kernel_apis
3043 * @{
3044 */
3045
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003046/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003047 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003048 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003049 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003050 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003051 * @param sem Address of the semaphore.
3052 * @param initial_count Initial semaphore count.
3053 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003054 *
3055 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003056 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003057 */
Andrew Boie99280232017-09-29 14:17:47 -07003058__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
3059 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003060
3061/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003062 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003063 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003064 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003065 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003066 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3067 *
3068 * @param sem Address of the semaphore.
3069 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003070 * or one of the special values K_NO_WAIT and K_FOREVER.
3071 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05003072 * @note When porting code from the nanokernel legacy API to the new API, be
3073 * careful with the return value of this function. The return value is the
3074 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
3075 * non-zero means failure, while the nano_sem_take family returns 1 for success
3076 * and 0 for failure.
3077 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003078 * @retval 0 Semaphore taken.
3079 * @retval -EBUSY Returned without waiting.
3080 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003081 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003082 */
Andrew Boie99280232017-09-29 14:17:47 -07003083__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003084
3085/**
3086 * @brief Give a semaphore.
3087 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003088 * This routine gives @a sem, unless the semaphore is already at its maximum
3089 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003090 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003091 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003092 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003093 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003094 *
3095 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003096 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003097 */
Andrew Boie99280232017-09-29 14:17:47 -07003098__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003099
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003100/**
3101 * @brief Reset a semaphore's count to zero.
3102 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003103 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003104 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003105 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003106 *
3107 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003108 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003109 */
Andrew Boie990bf162017-10-03 12:36:49 -07003110__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003111
Anas Nashif954d5502018-02-25 08:37:28 -06003112/**
3113 * @internal
3114 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003115static inline void z_impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003116{
Patrik Flykt24d71432019-03-26 19:57:45 -06003117 sem->count = 0U;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003118}
3119
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003120/**
3121 * @brief Get a semaphore's count.
3122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003123 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003125 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003127 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003128 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003129 */
Andrew Boie990bf162017-10-03 12:36:49 -07003130__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003131
Anas Nashif954d5502018-02-25 08:37:28 -06003132/**
3133 * @internal
3134 */
Patrik Flykt4344e272019-03-08 14:19:05 -07003135static inline unsigned int z_impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003136{
3137 return sem->count;
3138}
3139
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003140/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003141 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003143 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003144 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003145 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003146 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003147 * @param name Name of the semaphore.
3148 * @param initial_count Initial semaphore count.
3149 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003150 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003151 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003152#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003153 struct k_sem name \
3154 __in_section(_k_sem, static, name) = \
Patrik Flykt97b3bd12019-03-12 15:15:42 -06003155 Z_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303156 BUILD_ASSERT(((count_limit) != 0) && \
3157 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003158
Anas Nashif166f5192018-02-25 08:02:36 -06003159/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003160
3161/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003162 * @defgroup msgq_apis Message Queue APIs
3163 * @ingroup kernel_apis
3164 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003165 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003166
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003167/**
3168 * @brief Message Queue Structure
3169 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003170struct k_msgq {
3171 _wait_q_t wait_q;
Andy Rossbe03dbd2018-07-26 10:23:02 -07003172 struct k_spinlock lock;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003173 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003174 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003175 char *buffer_start;
3176 char *buffer_end;
3177 char *read_ptr;
3178 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003179 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003180
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003181 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Andrew Boie0fe789f2018-04-12 18:35:56 -07003182 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003183};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003184/**
3185 * @cond INTERNAL_HIDDEN
3186 */
3187
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003188
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003189#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003190 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003191 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003192 .msg_size = q_msg_size, \
Charles E. Youse6d01f672019-03-18 10:27:34 -07003193 .max_msgs = q_max_msgs, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003194 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003195 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003196 .read_ptr = q_buffer, \
3197 .write_ptr = q_buffer, \
3198 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003199 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003200 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003201#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003202/**
3203 * INTERNAL_HIDDEN @endcond
3204 */
3205
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003206
Andrew Boie0fe789f2018-04-12 18:35:56 -07003207#define K_MSGQ_FLAG_ALLOC BIT(0)
3208
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003209/**
3210 * @brief Message Queue Attributes
3211 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303212struct k_msgq_attrs {
3213 size_t msg_size;
3214 u32_t max_msgs;
3215 u32_t used_msgs;
3216};
3217
Allan Stephensc98da842016-11-11 15:45:03 -05003218
3219/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003220 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003221 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003222 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3223 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003224 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3225 * message is similarly aligned to this boundary, @a q_msg_size must also be
3226 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003227 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003228 * The message queue can be accessed outside the module where it is defined
3229 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003230 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003231 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003233 * @param q_name Name of the message queue.
3234 * @param q_msg_size Message size (in bytes).
3235 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003236 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003237 *
3238 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003239 */
3240#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08003241 static char __noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003242 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003243 struct k_msgq q_name \
3244 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003245 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003246 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003247
Peter Mitsisd7a37502016-10-13 11:37:40 -04003248/**
3249 * @brief Initialize a message queue.
3250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003251 * This routine initializes a message queue object, prior to its first use.
3252 *
Allan Stephensda827222016-11-09 14:23:58 -06003253 * The message queue's ring buffer must contain space for @a max_msgs messages,
3254 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3255 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3256 * that each message is similarly aligned to this boundary, @a q_msg_size
3257 * must also be a multiple of N.
3258 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003259 * @param q Address of the message queue.
3260 * @param buffer Pointer to ring buffer that holds queued messages.
3261 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003262 * @param max_msgs Maximum number of messages that can be queued.
3263 *
3264 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003265 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003266 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003267void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3268 u32_t max_msgs);
3269
3270/**
3271 * @brief Initialize a message queue.
3272 *
3273 * This routine initializes a message queue object, prior to its first use,
3274 * allocating its internal ring buffer from the calling thread's resource
3275 * pool.
3276 *
3277 * Memory allocated for the ring buffer can be released by calling
3278 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3279 * all of its references.
3280 *
3281 * @param q Address of the message queue.
3282 * @param msg_size Message size (in bytes).
3283 * @param max_msgs Maximum number of messages that can be queued.
3284 *
3285 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3286 * thread's resource pool, or -EINVAL if the size parameters cause
3287 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003288 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003289 */
3290__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3291 u32_t max_msgs);
3292
3293
3294void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003295
3296/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003297 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003298 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003299 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003300 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003301 * @note Can be called by ISRs.
3302 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003303 * @param q Address of the message queue.
3304 * @param data Pointer to the message.
3305 * @param timeout Waiting period to add the message (in milliseconds),
3306 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003307 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003308 * @retval 0 Message sent.
3309 * @retval -ENOMSG Returned without waiting or queue purged.
3310 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003311 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003312 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003313__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003314
3315/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003316 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003318 * This routine receives a message from message queue @a q in a "first in,
3319 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003320 *
Allan Stephensc98da842016-11-11 15:45:03 -05003321 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003322 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003323 * @param q Address of the message queue.
3324 * @param data Address of area to hold the received message.
3325 * @param timeout Waiting period to receive the message (in milliseconds),
3326 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003327 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003328 * @retval 0 Message received.
3329 * @retval -ENOMSG Returned without waiting.
3330 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003331 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003332 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003333__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003334
3335/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003336 * @brief Peek/read a message from a message queue.
3337 *
3338 * This routine reads a message from message queue @a q in a "first in,
3339 * first out" manner and leaves the message in the queue.
3340 *
3341 * @note Can be called by ISRs.
3342 *
3343 * @param q Address of the message queue.
3344 * @param data Address of area to hold the message read from the queue.
3345 *
3346 * @retval 0 Message read.
3347 * @retval -ENOMSG Returned when the queue has no message.
3348 * @req K-MSGQ-002
3349 */
3350__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3351
3352/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003353 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003355 * This routine discards all unreceived messages in a message queue's ring
3356 * buffer. Any threads that are blocked waiting to send a message to the
3357 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003358 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003359 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003360 *
3361 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003362 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003363 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003364__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003365
Peter Mitsis67be2492016-10-07 11:44:34 -04003366/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003367 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003368 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003369 * This routine returns the number of unused entries in a message queue's
3370 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003371 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003372 * @param q Address of the message queue.
3373 *
3374 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003375 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003376 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003377__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3378
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303379/**
3380 * @brief Get basic attributes of a message queue.
3381 *
3382 * This routine fetches basic attributes of message queue into attr argument.
3383 *
3384 * @param q Address of the message queue.
3385 * @param attrs pointer to message queue attribute structure.
3386 *
3387 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003388 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303389 */
3390__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3391
3392
Patrik Flykt4344e272019-03-08 14:19:05 -07003393static inline u32_t z_impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003394{
3395 return q->max_msgs - q->used_msgs;
3396}
3397
Peter Mitsisd7a37502016-10-13 11:37:40 -04003398/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003399 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003400 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003401 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003403 * @param q Address of the message queue.
3404 *
3405 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003406 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003407 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003408__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3409
Patrik Flykt4344e272019-03-08 14:19:05 -07003410static inline u32_t z_impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003411{
3412 return q->used_msgs;
3413}
3414
Anas Nashif166f5192018-02-25 08:02:36 -06003415/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003416
3417/**
3418 * @defgroup mem_pool_apis Memory Pool APIs
3419 * @ingroup kernel_apis
3420 * @{
3421 */
3422
Andy Ross73cb9582017-05-09 10:42:39 -07003423/* Note on sizing: the use of a 20 bit field for block means that,
3424 * assuming a reasonable minimum block size of 16 bytes, we're limited
3425 * to 16M of memory managed by a single pool. Long term it would be
3426 * good to move to a variable bit size based on configuration.
3427 */
3428struct k_mem_block_id {
3429 u32_t pool : 8;
3430 u32_t level : 4;
3431 u32_t block : 20;
3432};
3433
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003434struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003435 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003436 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003437};
3438
Anas Nashif166f5192018-02-25 08:02:36 -06003439/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003440
3441/**
3442 * @defgroup mailbox_apis Mailbox APIs
3443 * @ingroup kernel_apis
3444 * @{
3445 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003446
3447struct k_mbox_msg {
3448 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003449 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003450 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003451 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003452 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003453 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003454 /** sender's message data buffer */
3455 void *tx_data;
3456 /** internal use only - needed for legacy API support */
3457 void *_rx_data;
3458 /** message data block descriptor */
3459 struct k_mem_block tx_block;
3460 /** source thread id */
3461 k_tid_t rx_source_thread;
3462 /** target thread id */
3463 k_tid_t tx_target_thread;
3464 /** internal use only - thread waiting on send (may be a dummy) */
3465 k_tid_t _syncing_thread;
3466#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3467 /** internal use only - semaphore used during asynchronous send */
3468 struct k_sem *_async_sem;
3469#endif
3470};
3471
3472struct k_mbox {
3473 _wait_q_t tx_msg_queue;
3474 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003475 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003476
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003477 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003478};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003479/**
3480 * @cond INTERNAL_HIDDEN
3481 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003482
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003483#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003484 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003485 .tx_msg_queue = Z_WAIT_Q_INIT(&obj.tx_msg_queue), \
3486 .rx_msg_queue = Z_WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003487 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003488 }
3489
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003490#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3491
Peter Mitsis12092702016-10-14 12:57:23 -04003492/**
Allan Stephensc98da842016-11-11 15:45:03 -05003493 * INTERNAL_HIDDEN @endcond
3494 */
3495
3496/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003497 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003499 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003500 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003501 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003502 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003503 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003504 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003505 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003506#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003507 struct k_mbox name \
3508 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003509 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003510
Peter Mitsis12092702016-10-14 12:57:23 -04003511/**
3512 * @brief Initialize a mailbox.
3513 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003514 * This routine initializes a mailbox object, prior to its first use.
3515 *
3516 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003517 *
3518 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003519 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003520 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003521extern void k_mbox_init(struct k_mbox *mbox);
3522
Peter Mitsis12092702016-10-14 12:57:23 -04003523/**
3524 * @brief Send a mailbox message in a synchronous manner.
3525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003526 * This routine sends a message to @a mbox and waits for a receiver to both
3527 * receive and process it. The message data may be in a buffer, in a memory
3528 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003530 * @param mbox Address of the mailbox.
3531 * @param tx_msg Address of the transmit message descriptor.
3532 * @param timeout Waiting period for the message to be received (in
3533 * milliseconds), or one of the special values K_NO_WAIT
3534 * and K_FOREVER. Once the message has been received,
3535 * this routine waits as long as necessary for the message
3536 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003537 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003538 * @retval 0 Message sent.
3539 * @retval -ENOMSG Returned without waiting.
3540 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003541 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003542 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003543extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003544 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003545
Peter Mitsis12092702016-10-14 12:57:23 -04003546/**
3547 * @brief Send a mailbox message in an asynchronous manner.
3548 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003549 * This routine sends a message to @a mbox without waiting for a receiver
3550 * to process it. The message data may be in a buffer, in a memory pool block,
3551 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3552 * will be given when the message has been both received and completely
3553 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003554 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003555 * @param mbox Address of the mailbox.
3556 * @param tx_msg Address of the transmit message descriptor.
3557 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003558 *
3559 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003560 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003561 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003562extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003563 struct k_sem *sem);
3564
Peter Mitsis12092702016-10-14 12:57:23 -04003565/**
3566 * @brief Receive a mailbox message.
3567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003568 * This routine receives a message from @a mbox, then optionally retrieves
3569 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003570 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003571 * @param mbox Address of the mailbox.
3572 * @param rx_msg Address of the receive message descriptor.
3573 * @param buffer Address of the buffer to receive data, or NULL to defer data
3574 * retrieval and message disposal until later.
3575 * @param timeout Waiting period for a message to be received (in
3576 * milliseconds), or one of the special values K_NO_WAIT
3577 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003578 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003579 * @retval 0 Message received.
3580 * @retval -ENOMSG Returned without waiting.
3581 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003582 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003583 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003584extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003585 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003586
3587/**
3588 * @brief Retrieve mailbox message data into a buffer.
3589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003590 * This routine completes the processing of a received message by retrieving
3591 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003592 *
3593 * Alternatively, this routine can be used to dispose of a received message
3594 * without retrieving its data.
3595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003596 * @param rx_msg Address of the receive message descriptor.
3597 * @param buffer Address of the buffer to receive data, or NULL to discard
3598 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003599 *
3600 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003601 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003602 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003603extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003604
3605/**
3606 * @brief Retrieve mailbox message data into a memory pool block.
3607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003608 * This routine completes the processing of a received message by retrieving
3609 * its data into a memory pool block, then disposing of the message.
3610 * The memory pool block that results from successful retrieval must be
3611 * returned to the pool once the data has been processed, even in cases
3612 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003613 *
3614 * Alternatively, this routine can be used to dispose of a received message
3615 * without retrieving its data. In this case there is no need to return a
3616 * memory pool block to the pool.
3617 *
3618 * This routine allocates a new memory pool block for the data only if the
3619 * data is not already in one. If a new block cannot be allocated, the routine
3620 * returns a failure code and the received message is left unchanged. This
3621 * permits the caller to reattempt data retrieval at a later time or to dispose
3622 * of the received message without retrieving its data.
3623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003624 * @param rx_msg Address of a receive message descriptor.
3625 * @param pool Address of memory pool, or NULL to discard data.
3626 * @param block Address of the area to hold memory pool block info.
3627 * @param timeout Waiting period to wait for a memory pool block (in
3628 * milliseconds), or one of the special values K_NO_WAIT
3629 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003630 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003631 * @retval 0 Data retrieved.
3632 * @retval -ENOMEM Returned without waiting.
3633 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003634 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003635 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003636extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003637 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003638 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003639
Anas Nashif166f5192018-02-25 08:02:36 -06003640/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003641
3642/**
Anas Nashifce78d162018-05-24 12:43:11 -05003643 * @defgroup pipe_apis Pipe APIs
3644 * @ingroup kernel_apis
3645 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003646 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003647
Anas Nashifce78d162018-05-24 12:43:11 -05003648/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003649struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003650 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3651 size_t size; /**< Buffer size */
3652 size_t bytes_used; /**< # bytes used in buffer */
3653 size_t read_index; /**< Where in buffer to read from */
3654 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08003655 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003656
3657 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003658 _wait_q_t readers; /**< Reader wait queue */
3659 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003660 } wait_q;
3661
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003662 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Anas Nashifce78d162018-05-24 12:43:11 -05003663 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003664};
3665
Anas Nashifce78d162018-05-24 12:43:11 -05003666/**
3667 * @cond INTERNAL_HIDDEN
3668 */
3669#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3670
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003671#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
3672 { \
3673 .buffer = pipe_buffer, \
3674 .size = pipe_buffer_size, \
3675 .bytes_used = 0, \
3676 .read_index = 0, \
3677 .write_index = 0, \
3678 .lock = {}, \
3679 .wait_q = { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003680 .readers = Z_WAIT_Q_INIT(&obj.wait_q.readers), \
3681 .writers = Z_WAIT_Q_INIT(&obj.wait_q.writers) \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003682 }, \
3683 _OBJECT_TRACING_INIT \
3684 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003685 }
3686
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003687#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3688
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003689/**
Allan Stephensc98da842016-11-11 15:45:03 -05003690 * INTERNAL_HIDDEN @endcond
3691 */
3692
3693/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003694 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003695 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003696 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003697 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003698 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003699 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003700 * @param name Name of the pipe.
3701 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3702 * or zero if no ring buffer is used.
3703 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003704 *
3705 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003706 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003707#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08003708 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07003709 _k_pipe_buf_##name[pipe_buffer_size]; \
3710 struct k_pipe name \
3711 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003712 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003713
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003714/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003715 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003717 * This routine initializes a pipe object, prior to its first use.
3718 *
3719 * @param pipe Address of the pipe.
3720 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3721 * is used.
3722 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3723 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003724 *
3725 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003726 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003727 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003728void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3729
3730/**
3731 * @brief Release a pipe's allocated buffer
3732 *
3733 * If a pipe object was given a dynamically allocated buffer via
3734 * k_pipe_alloc_init(), this will free it. This function does nothing
3735 * if the buffer wasn't dynamically allocated.
3736 *
3737 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003738 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003739 */
3740void k_pipe_cleanup(struct k_pipe *pipe);
3741
3742/**
3743 * @brief Initialize a pipe and allocate a buffer for it
3744 *
3745 * Storage for the buffer region will be allocated from the calling thread's
3746 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3747 * or userspace is enabled and the pipe object loses all references to it.
3748 *
3749 * This function should only be called on uninitialized pipe objects.
3750 *
3751 * @param pipe Address of the pipe.
3752 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3753 * buffer is used.
3754 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003755 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003756 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003757 */
3758__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003759
3760/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003761 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003762 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003763 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003764 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003765 * @param pipe Address of the pipe.
3766 * @param data Address of data to write.
3767 * @param bytes_to_write Size of data (in bytes).
3768 * @param bytes_written Address of area to hold the number of bytes written.
3769 * @param min_xfer Minimum number of bytes to write.
3770 * @param timeout Waiting period to wait for the data to be written (in
3771 * milliseconds), or one of the special values K_NO_WAIT
3772 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003773 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003774 * @retval 0 At least @a min_xfer bytes of data were written.
3775 * @retval -EIO Returned without waiting; zero data bytes were written.
3776 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003777 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003778 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003779 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003780__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3781 size_t bytes_to_write, size_t *bytes_written,
3782 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003783
3784/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003785 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003786 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003787 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003788 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003789 * @param pipe Address of the pipe.
3790 * @param data Address to place the data read from pipe.
3791 * @param bytes_to_read Maximum number of data bytes to read.
3792 * @param bytes_read Address of area to hold the number of bytes read.
3793 * @param min_xfer Minimum number of data bytes to read.
3794 * @param timeout Waiting period to wait for the data to be read (in
3795 * milliseconds), or one of the special values K_NO_WAIT
3796 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003797 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003798 * @retval 0 At least @a min_xfer bytes of data were read.
3799 * @retval -EIO Returned without waiting; zero data bytes were read.
3800 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003801 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003802 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003803 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003804__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3805 size_t bytes_to_read, size_t *bytes_read,
3806 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003807
3808/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003809 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003810 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003811 * This routine writes the data contained in a memory block to @a pipe.
3812 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003813 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003815 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003816 * @param block Memory block containing data to send
3817 * @param size Number of data bytes in memory block to send
3818 * @param sem Semaphore to signal upon completion (else NULL)
3819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003820 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003821 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003822 */
3823extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3824 size_t size, struct k_sem *sem);
3825
Anas Nashif166f5192018-02-25 08:02:36 -06003826/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003827
Allan Stephensc98da842016-11-11 15:45:03 -05003828/**
3829 * @cond INTERNAL_HIDDEN
3830 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003831
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003832struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003833 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003834 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003835 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003836 char *buffer;
3837 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003838 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003839
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003840 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003841};
3842
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003843#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003844 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003845 { \
Patrik Flykt4344e272019-03-08 14:19:05 -07003846 .wait_q = Z_WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003847 .num_blocks = slab_num_blocks, \
3848 .block_size = slab_block_size, \
3849 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003850 .free_list = NULL, \
3851 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003852 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003853 }
3854
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003855#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3856
3857
Peter Mitsis578f9112016-10-07 13:50:31 -04003858/**
Allan Stephensc98da842016-11-11 15:45:03 -05003859 * INTERNAL_HIDDEN @endcond
3860 */
3861
3862/**
3863 * @defgroup mem_slab_apis Memory Slab APIs
3864 * @ingroup kernel_apis
3865 * @{
3866 */
3867
3868/**
Allan Stephensda827222016-11-09 14:23:58 -06003869 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003870 *
Allan Stephensda827222016-11-09 14:23:58 -06003871 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003872 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003873 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3874 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003875 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003876 *
Allan Stephensda827222016-11-09 14:23:58 -06003877 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003878 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003879 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003880 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003882 * @param name Name of the memory slab.
3883 * @param slab_block_size Size of each memory block (in bytes).
3884 * @param slab_num_blocks Number memory blocks.
3885 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003886 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003887 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003888#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3889 char __noinit __aligned(slab_align) \
3890 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3891 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003892 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003893 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003894 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003895
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003896/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003897 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003898 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003899 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003900 *
Allan Stephensda827222016-11-09 14:23:58 -06003901 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3902 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3903 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3904 * To ensure that each memory block is similarly aligned to this boundary,
3905 * @a slab_block_size must also be a multiple of N.
3906 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003907 * @param slab Address of the memory slab.
3908 * @param buffer Pointer to buffer used for the memory blocks.
3909 * @param block_size Size of each memory block (in bytes).
3910 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003911 *
3912 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003913 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003914 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003915extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003916 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003917
3918/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003919 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003921 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003923 * @param slab Address of the memory slab.
3924 * @param mem Pointer to block address area.
3925 * @param timeout Maximum time to wait for operation to complete
3926 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3927 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003928 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003929 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003930 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003931 * @retval -ENOMEM Returned without waiting.
3932 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003933 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003934 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003935extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003936 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003937
3938/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003939 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003940 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003941 * This routine releases a previously allocated memory block back to its
3942 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003943 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003944 * @param slab Address of the memory slab.
3945 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003946 *
3947 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003948 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003949 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003950extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003951
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003952/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003953 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003954 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003955 * This routine gets the number of memory blocks that are currently
3956 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003957 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003958 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003960 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003961 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003962 */
Kumar Galacc334c72017-04-21 10:55:34 -05003963static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003964{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003965 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003966}
3967
Peter Mitsisc001aa82016-10-13 13:53:37 -04003968/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003969 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003971 * This routine gets the number of memory blocks that are currently
3972 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003974 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003975 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003976 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003977 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04003978 */
Kumar Galacc334c72017-04-21 10:55:34 -05003979static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003980{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003981 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003982}
3983
Anas Nashif166f5192018-02-25 08:02:36 -06003984/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003985
3986/**
3987 * @cond INTERNAL_HIDDEN
3988 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003989
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003990struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003991 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003992 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003993};
3994
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003995/**
Allan Stephensc98da842016-11-11 15:45:03 -05003996 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003997 */
3998
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003999/**
Allan Stephensc98da842016-11-11 15:45:03 -05004000 * @addtogroup mem_pool_apis
4001 * @{
4002 */
4003
4004/**
4005 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004007 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4008 * long. The memory pool allows blocks to be repeatedly partitioned into
4009 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004010 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004011 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004012 * If the pool is to be accessed outside the module where it is defined, it
4013 * can be declared via
4014 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004015 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004016 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004017 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004018 * @param minsz Size of the smallest blocks in the pool (in bytes).
4019 * @param maxsz Size of the largest blocks in the pool (in bytes).
4020 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004021 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004022 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004023 */
Andy Ross73cb9582017-05-09 10:42:39 -07004024#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4025 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4026 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Patrik Flykt4344e272019-03-08 14:19:05 -07004027 struct sys_mem_pool_lvl _mpool_lvls_##name[Z_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07004028 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004029 .base = { \
4030 .buf = _mpool_buf_##name, \
4031 .max_sz = maxsz, \
4032 .n_max = nmax, \
Patrik Flykt4344e272019-03-08 14:19:05 -07004033 .n_levels = Z_MPOOL_LVLS(maxsz, minsz), \
Andrew Boieaa6de292018-03-06 17:12:37 -08004034 .levels = _mpool_lvls_##name, \
4035 .flags = SYS_MEM_POOL_KERNEL \
4036 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004037 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004038
Peter Mitsis937042c2016-10-13 13:18:26 -04004039/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004040 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004042 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004043 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004044 * @param pool Address of the memory pool.
4045 * @param block Pointer to block descriptor for the allocated memory.
4046 * @param size Amount of memory to allocate (in bytes).
4047 * @param timeout Maximum time to wait for operation to complete
4048 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4049 * or K_FOREVER to wait as long as necessary.
4050 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004051 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004052 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004053 * @retval -ENOMEM Returned without waiting.
4054 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004055 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004056 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004057extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004058 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004059
4060/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004061 * @brief Allocate memory from a memory pool with malloc() semantics
4062 *
4063 * Such memory must be released using k_free().
4064 *
4065 * @param pool Address of the memory pool.
4066 * @param size Amount of memory to allocate (in bytes).
4067 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004068 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004069 */
4070extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4071
4072/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004073 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004075 * This routine releases a previously allocated memory block back to its
4076 * memory pool.
4077 *
4078 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004079 *
4080 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004081 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004082 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004083extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004084
4085/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004086 * @brief Free memory allocated from a memory pool.
4087 *
4088 * This routine releases a previously allocated memory block back to its
4089 * memory pool
4090 *
4091 * @param id Memory block identifier.
4092 *
4093 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004094 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004095 */
4096extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4097
4098/**
Anas Nashif166f5192018-02-25 08:02:36 -06004099 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004100 */
4101
4102/**
4103 * @defgroup heap_apis Heap Memory Pool APIs
4104 * @ingroup kernel_apis
4105 * @{
4106 */
4107
4108/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004109 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004110 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004111 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004112 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004113 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004114 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004115 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004116 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004117 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004118 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004119extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004120
4121/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004122 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004123 *
4124 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004125 * returned must have been allocated from the heap memory pool or
4126 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004127 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004128 * If @a ptr is NULL, no operation is performed.
4129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004130 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004131 *
4132 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004133 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004134 */
4135extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004136
Allan Stephensc98da842016-11-11 15:45:03 -05004137/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004138 * @brief Allocate memory from heap, array style
4139 *
4140 * This routine provides traditional calloc() semantics. Memory is
4141 * allocated from the heap memory pool and zeroed.
4142 *
4143 * @param nmemb Number of elements in the requested array
4144 * @param size Size of each array element (in bytes).
4145 *
4146 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004147 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004148 */
4149extern void *k_calloc(size_t nmemb, size_t size);
4150
Anas Nashif166f5192018-02-25 08:02:36 -06004151/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004152
Benjamin Walshacc68c12017-01-29 18:57:45 -05004153/* polling API - PRIVATE */
4154
Benjamin Walshb0179862017-02-02 16:39:57 -05004155#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004156#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004157#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004158#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004159#endif
4160
Benjamin Walshacc68c12017-01-29 18:57:45 -05004161/* private - implementation data created as needed, per-type */
4162struct _poller {
4163 struct k_thread *thread;
Flavio Ceolin76b35182018-12-16 12:48:29 -08004164 volatile bool is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004165};
4166
4167/* private - types bit positions */
4168enum _poll_types_bits {
4169 /* can be used to ignore an event */
4170 _POLL_TYPE_IGNORE,
4171
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004172 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004173 _POLL_TYPE_SIGNAL,
4174
4175 /* semaphore availability */
4176 _POLL_TYPE_SEM_AVAILABLE,
4177
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004178 /* queue/fifo/lifo data availability */
4179 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004180
4181 _POLL_NUM_TYPES
4182};
4183
Patrik Flykt4344e272019-03-08 14:19:05 -07004184#define Z_POLL_TYPE_BIT(type) (1 << ((type) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004185
4186/* private - states bit positions */
4187enum _poll_states_bits {
4188 /* default state when creating event */
4189 _POLL_STATE_NOT_READY,
4190
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004191 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004192 _POLL_STATE_SIGNALED,
4193
4194 /* semaphore is available */
4195 _POLL_STATE_SEM_AVAILABLE,
4196
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004197 /* data is available to read on queue/fifo/lifo */
4198 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004199
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004200 /* queue/fifo/lifo wait was cancelled */
4201 _POLL_STATE_CANCELLED,
4202
Benjamin Walshacc68c12017-01-29 18:57:45 -05004203 _POLL_NUM_STATES
4204};
4205
Patrik Flykt4344e272019-03-08 14:19:05 -07004206#define Z_POLL_STATE_BIT(state) (1 << ((state) - 1))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004207
4208#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004209 (32 - (0 \
4210 + 8 /* tag */ \
4211 + _POLL_NUM_TYPES \
4212 + _POLL_NUM_STATES \
4213 + 1 /* modes */ \
4214 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004215
Benjamin Walshacc68c12017-01-29 18:57:45 -05004216/* end of polling API - PRIVATE */
4217
4218
4219/**
4220 * @defgroup poll_apis Async polling APIs
4221 * @ingroup kernel_apis
4222 * @{
4223 */
4224
4225/* Public polling API */
4226
4227/* public - values for k_poll_event.type bitfield */
4228#define K_POLL_TYPE_IGNORE 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004229#define K_POLL_TYPE_SIGNAL Z_POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4230#define K_POLL_TYPE_SEM_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
4231#define K_POLL_TYPE_DATA_AVAILABLE Z_POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004232#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004233
4234/* public - polling modes */
4235enum k_poll_modes {
4236 /* polling thread does not take ownership of objects when available */
4237 K_POLL_MODE_NOTIFY_ONLY = 0,
4238
4239 K_POLL_NUM_MODES
4240};
4241
4242/* public - values for k_poll_event.state bitfield */
4243#define K_POLL_STATE_NOT_READY 0
Patrik Flykt4344e272019-03-08 14:19:05 -07004244#define K_POLL_STATE_SIGNALED Z_POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4245#define K_POLL_STATE_SEM_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
4246#define K_POLL_STATE_DATA_AVAILABLE Z_POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004247#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Patrik Flykt4344e272019-03-08 14:19:05 -07004248#define K_POLL_STATE_CANCELLED Z_POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004249
4250/* public - poll signal object */
4251struct k_poll_signal {
4252 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004253 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004254
4255 /*
4256 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4257 * user resets it to 0.
4258 */
4259 unsigned int signaled;
4260
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004261 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004262 int result;
4263};
4264
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004265#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004266 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004267 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004268 .signaled = 0, \
4269 .result = 0, \
4270 }
4271
4272struct k_poll_event {
4273 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004274 sys_dnode_t _node;
4275
4276 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004277 struct _poller *poller;
4278
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004279 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004280 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004281
Benjamin Walshacc68c12017-01-29 18:57:45 -05004282 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004283 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004284
4285 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004286 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004287
4288 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004289 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004290
4291 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004292 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004293
4294 /* per-type data */
4295 union {
4296 void *obj;
4297 struct k_poll_signal *signal;
4298 struct k_sem *sem;
4299 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004300 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004301 };
4302};
4303
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004304#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004305 { \
4306 .poller = NULL, \
4307 .type = event_type, \
4308 .state = K_POLL_STATE_NOT_READY, \
4309 .mode = event_mode, \
4310 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004311 { .obj = event_obj }, \
4312 }
4313
4314#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4315 event_tag) \
4316 { \
4317 .type = event_type, \
4318 .tag = event_tag, \
4319 .state = K_POLL_STATE_NOT_READY, \
4320 .mode = event_mode, \
4321 .unused = 0, \
4322 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004323 }
4324
4325/**
4326 * @brief Initialize one struct k_poll_event instance
4327 *
4328 * After this routine is called on a poll event, the event it ready to be
4329 * placed in an event array to be passed to k_poll().
4330 *
4331 * @param event The event to initialize.
4332 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4333 * values. Only values that apply to the same object being polled
4334 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4335 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004336 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004337 * @param obj Kernel object or poll signal.
4338 *
4339 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004340 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004341 */
4342
Kumar Galacc334c72017-04-21 10:55:34 -05004343extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004344 int mode, void *obj);
4345
4346/**
4347 * @brief Wait for one or many of multiple poll events to occur
4348 *
4349 * This routine allows a thread to wait concurrently for one or many of
4350 * multiple poll events to have occurred. Such events can be a kernel object
4351 * being available, like a semaphore, or a poll signal event.
4352 *
4353 * When an event notifies that a kernel object is available, the kernel object
4354 * is not "given" to the thread calling k_poll(): it merely signals the fact
4355 * that the object was available when the k_poll() call was in effect. Also,
4356 * all threads trying to acquire an object the regular way, i.e. by pending on
4357 * the object, have precedence over the thread polling on the object. This
4358 * means that the polling thread will never get the poll event on an object
4359 * until the object becomes available and its pend queue is empty. For this
4360 * reason, the k_poll() call is more effective when the objects being polled
4361 * only have one thread, the polling thread, trying to acquire them.
4362 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004363 * When k_poll() returns 0, the caller should loop on all the events that were
4364 * passed to k_poll() and check the state field for the values that were
4365 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004366 *
4367 * Before being reused for another call to k_poll(), the user has to reset the
4368 * state field to K_POLL_STATE_NOT_READY.
4369 *
Andrew Boie3772f772018-05-07 16:52:57 -07004370 * When called from user mode, a temporary memory allocation is required from
4371 * the caller's resource pool.
4372 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004373 * @param events An array of pointers to events to be polled for.
4374 * @param num_events The number of events in the array.
4375 * @param timeout Waiting period for an event to be ready (in milliseconds),
4376 * or one of the special values K_NO_WAIT and K_FOREVER.
4377 *
4378 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004379 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004380 * @retval -EINTR Polling has been interrupted, e.g. with
4381 * k_queue_cancel_wait(). All output events are still set and valid,
4382 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4383 * words, -EINTR status means that at least one of output events is
4384 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004385 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4386 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004387 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004388 */
4389
Andrew Boie3772f772018-05-07 16:52:57 -07004390__syscall int k_poll(struct k_poll_event *events, int num_events,
4391 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004392
4393/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004394 * @brief Initialize a poll signal object.
4395 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004396 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004397 *
4398 * @param signal A poll signal.
4399 *
4400 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004401 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004402 */
4403
Andrew Boie3772f772018-05-07 16:52:57 -07004404__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4405
4406/*
4407 * @brief Reset a poll signal object's state to unsignaled.
4408 *
4409 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004410 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004411 */
4412__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4413
Patrik Flykt4344e272019-03-08 14:19:05 -07004414static inline void z_impl_k_poll_signal_reset(struct k_poll_signal *signal)
Andrew Boie3772f772018-05-07 16:52:57 -07004415{
Patrik Flykt24d71432019-03-26 19:57:45 -06004416 signal->signaled = 0U;
Andrew Boie3772f772018-05-07 16:52:57 -07004417}
4418
4419/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004420 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004421 *
4422 * @param signal A poll signal object
4423 * @param signaled An integer buffer which will be written nonzero if the
4424 * object was signaled
4425 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004426 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004427 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004428 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004429 */
4430__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4431 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004432
4433/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004434 * @brief Signal a poll signal object.
4435 *
4436 * This routine makes ready a poll signal, which is basically a poll event of
4437 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4438 * made ready to run. A @a result value can be specified.
4439 *
4440 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004441 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004442 * k_poll_signal_reset(). It thus has to be reset by the user before being
4443 * passed again to k_poll() or k_poll() will consider it being signaled, and
4444 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004445 *
Peter A. Bigot773bd982019-04-30 07:06:39 -05004446 * @note The result is stored and the 'signaled' field is set even if
4447 * this function returns an error indicating that an expiring poll was
4448 * not notified. The next k_poll() will detect the missed raise.
4449 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004450 * @param signal A poll signal.
4451 * @param result The value to store in the result field of the signal.
4452 *
4453 * @retval 0 The signal was delivered successfully.
4454 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004455 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004456 */
4457
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004458__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004459
Anas Nashif954d5502018-02-25 08:37:28 -06004460/**
4461 * @internal
4462 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004463extern void z_handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004464
Anas Nashif166f5192018-02-25 08:02:36 -06004465/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004466
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004467/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004468 * @defgroup cpu_idle_apis CPU Idling APIs
4469 * @ingroup kernel_apis
4470 * @{
4471 */
4472
4473/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004474 * @brief Make the CPU idle.
4475 *
4476 * This function makes the CPU idle until an event wakes it up.
4477 *
4478 * In a regular system, the idle thread should be the only thread responsible
4479 * for making the CPU idle and triggering any type of power management.
4480 * However, in some more constrained systems, such as a single-threaded system,
4481 * the only thread would be responsible for this if needed.
4482 *
4483 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004484 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004485 */
4486extern void k_cpu_idle(void);
4487
4488/**
4489 * @brief Make the CPU idle in an atomic fashion.
4490 *
4491 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4492 * must be done atomically before making the CPU idle.
4493 *
4494 * @param key Interrupt locking key obtained from irq_lock().
4495 *
4496 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004497 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004498 */
4499extern void k_cpu_atomic_idle(unsigned int key);
4500
Anas Nashif30c3cff2019-01-22 08:18:13 -05004501/**
4502 * @}
4503 */
Anas Nashif954d5502018-02-25 08:37:28 -06004504
4505/**
4506 * @internal
4507 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004508extern void z_sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004509
Patrik Flykt4344e272019-03-08 14:19:05 -07004510#ifdef Z_ARCH_EXCEPT
Andrew Boiecdb94d62017-04-18 15:22:05 -07004511/* This archtecture has direct support for triggering a CPU exception */
Patrik Flykt4344e272019-03-08 14:19:05 -07004512#define z_except_reason(reason) Z_ARCH_EXCEPT(reason)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004513#else
4514
Andrew Boiecdb94d62017-04-18 15:22:05 -07004515/* NOTE: This is the implementation for arches that do not implement
Patrik Flykt4344e272019-03-08 14:19:05 -07004516 * Z_ARCH_EXCEPT() to generate a real CPU exception.
Andrew Boiecdb94d62017-04-18 15:22:05 -07004517 *
4518 * We won't have a real exception frame to determine the PC value when
4519 * the oops occurred, so print file and line number before we jump into
4520 * the fatal error handler.
4521 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004522#define z_except_reason(reason) do { \
Andrew Boiecdb94d62017-04-18 15:22:05 -07004523 printk("@ %s:%d:\n", __FILE__, __LINE__); \
Patrik Flykt4344e272019-03-08 14:19:05 -07004524 z_NanoFatalErrorHandler(reason, &_default_esf); \
Andrew Boiecdb94d62017-04-18 15:22:05 -07004525 CODE_UNREACHABLE; \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004526 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004527
4528#endif /* _ARCH__EXCEPT */
4529
4530/**
4531 * @brief Fatally terminate a thread
4532 *
4533 * This should be called when a thread has encountered an unrecoverable
4534 * runtime condition and needs to terminate. What this ultimately
4535 * means is determined by the _fatal_error_handler() implementation, which
4536 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4537 *
4538 * If this is called from ISR context, the default system fatal error handler
4539 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004540 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004541 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004542#define k_oops() z_except_reason(_NANO_ERR_KERNEL_OOPS)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004543
4544/**
4545 * @brief Fatally terminate the system
4546 *
4547 * This should be called when the Zephyr kernel has encountered an
4548 * unrecoverable runtime condition and needs to terminate. What this ultimately
4549 * means is determined by the _fatal_error_handler() implementation, which
4550 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004551 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004552 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004553#define k_panic() z_except_reason(_NANO_ERR_KERNEL_PANIC)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004554
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004555/*
4556 * private APIs that are utilized by one or more public APIs
4557 */
4558
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004559#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004560/**
4561 * @internal
4562 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004563extern void z_init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004564#else
Anas Nashif954d5502018-02-25 08:37:28 -06004565/**
4566 * @internal
4567 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004568#define z_init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004569#endif
4570
Anas Nashif954d5502018-02-25 08:37:28 -06004571/**
4572 * @internal
4573 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004574extern bool z_is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004575/**
4576 * @internal
4577 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004578extern void z_timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004579
Andrew Boiedc5d9352017-06-02 12:56:47 -07004580/* arch/cpu.h may declare an architecture or platform-specific macro
4581 * for properly declaring stacks, compatible with MMU/MPU constraints if
4582 * enabled
4583 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004584
4585/**
4586 * @brief Obtain an extern reference to a stack
4587 *
4588 * This macro properly brings the symbol of a thread stack declared
4589 * elsewhere into scope.
4590 *
4591 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004592 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004593 */
4594#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4595
Patrik Flykt4344e272019-03-08 14:19:05 -07004596#ifdef Z_ARCH_THREAD_STACK_DEFINE
4597#define K_THREAD_STACK_DEFINE(sym, size) Z_ARCH_THREAD_STACK_DEFINE(sym, size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004598#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Patrik Flykt4344e272019-03-08 14:19:05 -07004599 Z_ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4600#define K_THREAD_STACK_LEN(size) Z_ARCH_THREAD_STACK_LEN(size)
4601#define K_THREAD_STACK_MEMBER(sym, size) Z_ARCH_THREAD_STACK_MEMBER(sym, size)
4602#define K_THREAD_STACK_SIZEOF(sym) Z_ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boie575abc02019-03-19 10:42:24 -07004603#define K_THREAD_STACK_RESERVED Z_ARCH_THREAD_STACK_RESERVED
Andrew Boie4e5c0932019-04-04 12:05:28 -07004604static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004605{
Patrik Flykt4344e272019-03-08 14:19:05 -07004606 return Z_ARCH_THREAD_STACK_BUFFER(sym);
Andrew Boie507852a2017-07-25 18:47:07 -07004607}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004608#else
4609/**
4610 * @brief Declare a toplevel thread stack memory region
4611 *
4612 * This declares a region of memory suitable for use as a thread's stack.
4613 *
4614 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4615 * 'noinit' section so that it isn't zeroed at boot
4616 *
Andrew Boie507852a2017-07-25 18:47:07 -07004617 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004618 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie4e5c0932019-04-04 12:05:28 -07004619 * inside needs to be examined, examine thread->stack_info for the associated
4620 * thread object to obtain the boundaries.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004621 *
4622 * It is legal to precede this definition with the 'static' keyword.
4623 *
4624 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4625 * parameter of k_thread_create(), it may not be the same as the
4626 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4627 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004628 * Some arches may round the size of the usable stack region up to satisfy
4629 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4630 * size.
4631 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004632 * @param sym Thread stack symbol name
4633 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004634 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004635 */
4636#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004637 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004638
4639/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304640 * @brief Calculate size of stacks to be allocated in a stack array
4641 *
4642 * This macro calculates the size to be allocated for the stacks
4643 * inside a stack array. It accepts the indicated "size" as a parameter
4644 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4645 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4646 *
4647 * @param size Size of the stack memory region
4648 * @req K-TSTACK-001
4649 */
4650#define K_THREAD_STACK_LEN(size) (size)
4651
4652/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004653 * @brief Declare a toplevel array of thread stack memory regions
4654 *
4655 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4656 * definition for additional details and constraints.
4657 *
4658 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4659 * 'noinit' section so that it isn't zeroed at boot
4660 *
4661 * @param sym Thread stack symbol name
4662 * @param nmemb Number of stacks to declare
4663 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004664 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004665 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004666#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004667 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304668 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004669
4670/**
4671 * @brief Declare an embedded stack memory region
4672 *
4673 * Used for stacks embedded within other data structures. Use is highly
4674 * discouraged but in some cases necessary. For memory protection scenarios,
4675 * it is very important that any RAM preceding this member not be writable
4676 * by threads else a stack overflow will lead to silent corruption. In other
4677 * words, the containing data structure should live in RAM owned by the kernel.
4678 *
4679 * @param sym Thread stack symbol name
4680 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004681 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004682 */
4683#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004684 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004685
4686/**
4687 * @brief Return the size in bytes of a stack memory region
4688 *
4689 * Convenience macro for passing the desired stack size to k_thread_create()
4690 * since the underlying implementation may actually create something larger
4691 * (for instance a guard area).
4692 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004693 * The value returned here is not guaranteed to match the 'size' parameter
4694 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004695 *
4696 * @param sym Stack memory symbol
4697 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004698 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004699 */
4700#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4701
Andrew Boie575abc02019-03-19 10:42:24 -07004702
4703/**
4704 * @brief Indicate how much additional memory is reserved for stack objects
4705 *
4706 * Any given stack declaration may have additional memory in it for guard
4707 * areas or supervisor mode stacks. This macro indicates how much space
4708 * is reserved for this. The memory reserved may not be contiguous within
4709 * the stack object, and does not account for additional space used due to
4710 * enforce alignment.
4711 */
4712#define K_THREAD_STACK_RESERVED 0
4713
Andrew Boiedc5d9352017-06-02 12:56:47 -07004714/**
4715 * @brief Get a pointer to the physical stack buffer
4716 *
Andrew Boie4e5c0932019-04-04 12:05:28 -07004717 * This macro is deprecated. If a stack buffer needs to be examined, the
4718 * bounds should be obtained from the associated thread's stack_info struct.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004719 *
4720 * @param sym Declared stack symbol name
4721 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004722 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004723 */
Andrew Boie4e5c0932019-04-04 12:05:28 -07004724static inline char *Z_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004725{
4726 return (char *)sym;
4727}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004728
4729#endif /* _ARCH_DECLARE_STACK */
4730
Chunlin Hane9c97022017-07-07 20:29:30 +08004731/**
4732 * @defgroup mem_domain_apis Memory domain APIs
4733 * @ingroup kernel_apis
4734 * @{
4735 */
4736
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004737/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004738 * @def K_MEM_PARTITION_DEFINE
4739 * @brief Used to declare a memory partition
4740 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004741 */
4742#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4743#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4744 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08004745 struct k_mem_partition name =\
Ioannis Glaropoulos293247e2018-12-03 18:32:32 +01004746 { (u32_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004747#else
4748#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08004749 struct k_mem_partition name =\
Ioannis Glaropoulos293247e2018-12-03 18:32:32 +01004750 { (u32_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004751#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4752
Chunlin Hane9c97022017-07-07 20:29:30 +08004753/* memory partition */
4754struct k_mem_partition {
4755 /* start address of memory partition */
4756 u32_t start;
4757 /* size of memory partition */
4758 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004759#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08004760 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304761 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004762#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08004763};
4764
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02004765/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304766 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004767struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304768#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004769 /* partitions in the domain */
4770 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304771#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004772 /* domain q */
4773 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004774 /* number of partitions in the domain */
4775 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004776};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304777
Chunlin Hane9c97022017-07-07 20:29:30 +08004778
4779/**
4780 * @brief Initialize a memory domain.
4781 *
4782 * Initialize a memory domain with given name and memory partitions.
4783 *
4784 * @param domain The memory domain to be initialized.
4785 * @param num_parts The number of array items of "parts" parameter.
4786 * @param parts An array of pointers to the memory partitions. Can be NULL
4787 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004788 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004789 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004790extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004791 struct k_mem_partition *parts[]);
4792/**
4793 * @brief Destroy a memory domain.
4794 *
4795 * Destroy a memory domain.
4796 *
4797 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004798 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004799 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004800extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4801
4802/**
4803 * @brief Add a memory partition into a memory domain.
4804 *
4805 * Add a memory partition into a memory domain.
4806 *
4807 * @param domain The memory domain to be added a memory partition.
4808 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004809 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004810 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004811extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4812 struct k_mem_partition *part);
4813
4814/**
4815 * @brief Remove a memory partition from a memory domain.
4816 *
4817 * Remove a memory partition from a memory domain.
4818 *
4819 * @param domain The memory domain to be removed a memory partition.
4820 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004821 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004822 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004823extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4824 struct k_mem_partition *part);
4825
4826/**
4827 * @brief Add a thread into a memory domain.
4828 *
4829 * Add a thread into a memory domain.
4830 *
4831 * @param domain The memory domain that the thread is going to be added into.
4832 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004833 *
4834 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004835 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004836extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4837 k_tid_t thread);
4838
4839/**
4840 * @brief Remove a thread from its memory domain.
4841 *
4842 * Remove a thread from its memory domain.
4843 *
4844 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004845 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004846 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004847extern void k_mem_domain_remove_thread(k_tid_t thread);
4848
Anas Nashif166f5192018-02-25 08:02:36 -06004849/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004850
Andrew Boie756f9072017-10-10 16:01:49 -07004851/**
4852 * @brief Emit a character buffer to the console device
4853 *
4854 * @param c String of characters to print
4855 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004856 *
4857 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004858 */
4859__syscall void k_str_out(char *c, size_t n);
4860
Andy Rosse7172672018-01-24 15:48:32 -08004861/**
4862 * @brief Start a numbered CPU on a MP-capable system
4863
4864 * This starts and initializes a specific CPU. The main thread on
4865 * startup is running on CPU zero, other processors are numbered
4866 * sequentially. On return from this function, the CPU is known to
4867 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004868 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004869 * with the provided key will work to enable them.
4870 *
4871 * Normally, in SMP mode this function will be called by the kernel
4872 * initialization and should not be used as a user API. But it is
4873 * defined here for special-purpose apps which want Zephyr running on
4874 * one core and to use others for design-specific processing.
4875 *
4876 * @param cpu_num Integer number of the CPU
4877 * @param stack Stack memory for the CPU
4878 * @param sz Stack buffer size, in bytes
4879 * @param fn Function to begin running on the CPU. First argument is
4880 * an irq_unlock() key.
4881 * @param arg Untyped argument to be passed to "fn"
4882 */
Patrik Flykt4344e272019-03-08 14:19:05 -07004883extern void z_arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08004884 void (*fn)(int key, void *data), void *arg);
Andy Rosse7172672018-01-24 15:48:32 -08004885
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004886#ifdef __cplusplus
4887}
4888#endif
4889
Anas Nashifb6304e62018-07-04 08:03:03 -05004890#include <tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07004891#include <syscalls/kernel.h>
4892
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004893#endif /* !_ASMLANGUAGE */
4894
Flavio Ceolin67ca1762018-09-14 10:43:44 -07004895#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */