blob: d40377906d58f7366791ca08483f51f2b962843d [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
Flavio Ceolin02ed85b2018-09-20 15:43:57 -070084extern bool _priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
Andy Ross1acd8c22018-05-03 14:51:49 -070085
86#define _WAIT_Q_INIT(wait_q) { { { .lessthan_fn = _priq_rb_lessthan } } }
87
88#else
89
Andy Rossccf3bf72018-05-10 11:10:34 -070090typedef struct {
91 sys_dlist_t waitq;
92} _wait_q_t;
93
94#define _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
160 * _k_object_find() */
161struct _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 */
208void _k_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 */
Andrew Boie743e4682017-10-04 12:25:50 -0700216static inline void _k_object_init(void *obj)
217{
218 ARG_UNUSED(obj);
219}
220
Anas Nashif954d5502018-02-25 08:37:28 -0600221/**
222 * @internal
223 */
Andrew Boie743e4682017-10-04 12:25:50 -0700224static inline void _impl_k_object_access_grant(void *object,
225 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 */
244static inline void _impl_k_object_release(void *object)
245{
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
329static inline void *_impl_k_object_alloc(enum k_objects otype)
330{
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
345 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
346 *
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 Boieb85ac3e2018-06-01 12:15:13 -0700477 /* Stack Start - Identical to K_THREAD_STACK_BUFFER() on the stack
478 * object. Represents thread-writable stack area without any extras.
479 */
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 _caller_saved caller_saved;
Anas Nashifce78d162018-05-24 12:43:11 -0500518 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700519 struct _callee_saved callee_saved;
520
Anas Nashifce78d162018-05-24 12:43:11 -0500521 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700522 void *init_data;
523
Anas Nashifce78d162018-05-24 12:43:11 -0500524 /**
525 * abort function
526 * @req K-THREAD-002
527 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700528 void (*fn_abort)(void);
529
530#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500531 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700532 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700533
Anas Nashifce78d162018-05-24 12:43:11 -0500534 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700535 struct k_thread *next_thread;
536#endif
537
Anas Nashif57554052018-03-03 02:31:05 -0600538#if defined(CONFIG_THREAD_NAME)
539 /* Thread name */
540 const char *name;
541#endif
542
Andrew Boie73abd322017-04-04 13:19:13 -0700543#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500544 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700545 void *custom_data;
546#endif
547
Daniel Leungfc182432018-08-16 15:42:28 -0700548#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
549 struct _thread_userspace_local_data *userspace_local_data;
550#endif
551
Andrew Boie73abd322017-04-04 13:19:13 -0700552#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700553#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500554 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700555 int errno_var;
556#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700557#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700558
559#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500560 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700561 struct _thread_stack_info stack_info;
562#endif /* CONFIG_THREAD_STACK_INFO */
563
Chunlin Hane9c97022017-07-07 20:29:30 +0800564#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500565 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800566 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500567 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700568 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800569#endif /* CONFIG_USERSPACE */
570
Andy Ross042d8ec2017-12-09 08:37:20 -0800571#if defined(CONFIG_USE_SWITCH)
572 /* When using __switch() a few previously arch-specific items
573 * become part of the core OS
574 */
575
Anas Nashifce78d162018-05-24 12:43:11 -0500576 /** _Swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800577 int swap_retval;
578
Anas Nashifce78d162018-05-24 12:43:11 -0500579 /** Context handle returned via _arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800580 void *switch_handle;
581#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500582 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700583 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800584
Anas Nashifce78d162018-05-24 12:43:11 -0500585 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700586 struct _thread_arch arch;
587};
588
589typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400590typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400591
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400592enum execution_context_types {
593 K_ISR = 0,
594 K_COOP_THREAD,
595 K_PREEMPT_THREAD,
596};
597
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400598/**
Anas Nashif4bcb2942019-01-23 23:06:29 -0500599 * @addtogroup thread_apis
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100600 * @{
601 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530602typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
603 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100604
605/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530606 * @brief Iterate over all the threads in the system.
607 *
608 * This routine iterates over all the threads in the system and
609 * calls the user_cb function for each thread.
610 *
611 * @param user_cb Pointer to the user callback function.
612 * @param user_data Pointer to user data.
613 *
614 * @note CONFIG_THREAD_MONITOR must be set for this function
615 * to be effective. Also this API uses irq_lock to protect the
616 * _kernel.threads list which means creation of new threads and
617 * terminations of existing threads are blocked until this
618 * API returns.
619 *
620 * @return N/A
621 */
622extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
623
Anas Nashif166f5192018-02-25 08:02:36 -0600624/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100625
626/**
Allan Stephensc98da842016-11-11 15:45:03 -0500627 * @defgroup thread_apis Thread APIs
628 * @ingroup kernel_apis
629 * @{
630 */
631
Benjamin Walshed240f22017-01-22 13:05:08 -0500632#endif /* !_ASMLANGUAGE */
633
634
635/*
636 * Thread user options. May be needed by assembly code. Common part uses low
637 * bits, arch-specific use high bits.
638 */
639
Anas Nashifa541e932018-05-24 11:19:16 -0500640/**
641 * @brief system thread that must not abort
642 * @req K-THREAD-000
643 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700644#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500645
646#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500647/**
648 * @brief thread uses floating point registers
649 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700650#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500651#endif
652
Anas Nashifa541e932018-05-24 11:19:16 -0500653/**
654 * @brief user mode thread
655 *
656 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700657 * has additional restrictions
658 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700659#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700660
Anas Nashifa541e932018-05-24 11:19:16 -0500661/**
662 * @brief Inherit Permissions
663 *
664 * @details
665 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700666 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
667 * is not enabled.
668 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700669#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700670
Benjamin Walshed240f22017-01-22 13:05:08 -0500671#ifdef CONFIG_X86
672/* x86 Bitmask definitions for threads user options */
673
674#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
675/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700676#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500677#endif
678#endif
679
680/* end - thread options */
681
682#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400683/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700684 * @brief Create a thread.
685 *
686 * This routine initializes a thread, then schedules it for execution.
687 *
688 * The new thread may be scheduled for immediate execution or a delayed start.
689 * If the newly spawned thread does not have a delayed start the kernel
690 * scheduler may preempt the current thread to allow the new thread to
691 * execute.
692 *
693 * Thread options are architecture-specific, and can include K_ESSENTIAL,
694 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
695 * them using "|" (the logical OR operator).
696 *
697 * Historically, users often would use the beginning of the stack memory region
698 * to store the struct k_thread data, although corruption will occur if the
699 * stack overflows this region and stack protection features may not detect this
700 * situation.
701 *
702 * @param new_thread Pointer to uninitialized struct k_thread
703 * @param stack Pointer to the stack space.
704 * @param stack_size Stack size in bytes.
705 * @param entry Thread entry function.
706 * @param p1 1st entry point parameter.
707 * @param p2 2nd entry point parameter.
708 * @param p3 3rd entry point parameter.
709 * @param prio Thread priority.
710 * @param options Thread options.
711 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
712 *
713 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400714 *
715 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700716 */
Andrew Boie662c3452017-10-02 10:51:18 -0700717__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700718 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700719 size_t stack_size,
720 k_thread_entry_t entry,
721 void *p1, void *p2, void *p3,
722 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700723
Andrew Boie3f091b52017-08-30 14:34:14 -0700724/**
725 * @brief Drop a thread's privileges permanently to user mode
726 *
727 * @param entry Function to start executing from
728 * @param p1 1st entry point parameter
729 * @param p2 2nd entry point parameter
730 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400731 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700732 */
733extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
734 void *p1, void *p2,
735 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700736
Andrew Boied26cf2d2017-03-30 13:07:02 -0700737/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530738 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700739 *
740 * This is a convenience function. For the provided thread, grant access to
741 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700742 *
743 * The thread object must be initialized (i.e. running). The objects don't
744 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530745 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700746 *
747 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530748 * @param ... list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400749 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700750 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530751#define k_thread_access_grant(thread, ...) \
752 FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700753
754/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700755 * @brief Assign a resource memory pool to a thread
756 *
757 * By default, threads have no resource pool assigned unless their parent
758 * thread has a resource pool, in which case it is inherited. Multiple
759 * threads may be assigned to the same memory pool.
760 *
761 * Changing a thread's resource pool will not migrate allocations from the
762 * previous pool.
763 *
764 * @param thread Target thread to assign a memory pool for resource requests,
765 * or NULL if the thread should no longer have a memory pool.
766 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400767 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700768 */
769static inline void k_thread_resource_pool_assign(struct k_thread *thread,
770 struct k_mem_pool *pool)
771{
772 thread->resource_pool = pool;
773}
774
775#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
776/**
777 * @brief Assign the system heap as a thread's resource pool
778 *
779 * Similar to k_thread_resource_pool_assign(), but the thread will use
780 * the kernel heap to draw memory.
781 *
782 * Use with caution, as a malicious thread could perform DoS attacks on the
783 * kernel heap.
784 *
785 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400786 *
787 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700788 */
789void k_thread_system_pool_assign(struct k_thread *thread);
790#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
791
792/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500793 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400794 *
Allan Stephensc98da842016-11-11 15:45:03 -0500795 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500796 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500798 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400799 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200800 * @return Zero if the requested time has elapsed or the number of milliseconds
801 * left to sleep, if thread was woken up by \ref k_wakeup call.
802 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400803 */
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200804__syscall s32_t k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400805
806/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500807 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400808 *
809 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500810 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400811 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400812 * @return N/A
813 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800814__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400815
816/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500817 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500819 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400820 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500821 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400822 *
823 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400824 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400825 */
Andrew Boie468190a2017-09-29 14:00:48 -0700826__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827
828/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500829 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500831 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500833 * If @a thread is not currently sleeping, the routine has no effect.
834 *
835 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836 *
837 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400838 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400839 */
Andrew Boie468190a2017-09-29 14:00:48 -0700840__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400841
842/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500843 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500845 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400846 *
847 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700849__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400850
851/**
Allan Stephensc98da842016-11-11 15:45:03 -0500852 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500854 * This routine permanently stops execution of @a thread. The thread is taken
855 * off all kernel queues it is part of (i.e. the ready queue, the timeout
856 * queue, or a kernel object wait queue). However, any kernel resources the
857 * thread might currently own (such as mutexes or memory blocks) are not
858 * released. It is the responsibility of the caller of this routine to ensure
859 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500861 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400862 *
863 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400864 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865 */
Andrew Boie468190a2017-09-29 14:00:48 -0700866__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400867
Andrew Boie7d627c52017-08-30 11:01:56 -0700868
869/**
870 * @brief Start an inactive thread
871 *
872 * If a thread was created with K_FOREVER in the delay parameter, it will
873 * not be added to the scheduling queue until this function is called
874 * on it.
875 *
876 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400877 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700878 */
Andrew Boie468190a2017-09-29 14:00:48 -0700879__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700880
Allan Stephensc98da842016-11-11 15:45:03 -0500881/**
882 * @cond INTERNAL_HIDDEN
883 */
884
Benjamin Walshd211a522016-12-06 11:44:01 -0500885/* timeout has timed out and is not on _timeout_q anymore */
886#define _EXPIRED (-2)
887
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400888struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700889 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700890 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400891 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700892 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500893 void *init_p1;
894 void *init_p2;
895 void *init_p3;
896 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500897 u32_t init_options;
898 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500899 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600900 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400901};
902
Andrew Boied26cf2d2017-03-30 13:07:02 -0700903#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400904 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600905 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500906 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700907 .init_thread = (thread), \
908 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500909 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700910 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400911 .init_p1 = (void *)p1, \
912 .init_p2 = (void *)p2, \
913 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500914 .init_prio = (prio), \
915 .init_options = (options), \
916 .init_delay = (delay), \
917 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600918 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400919 }
920
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400921/**
Allan Stephensc98da842016-11-11 15:45:03 -0500922 * INTERNAL_HIDDEN @endcond
923 */
924
925/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500926 * @brief Statically define and initialize a thread.
927 *
928 * The thread may be scheduled for immediate execution or a delayed start.
929 *
930 * Thread options are architecture-specific, and can include K_ESSENTIAL,
931 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
932 * them using "|" (the logical OR operator).
933 *
934 * The ID of the thread can be accessed using:
935 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500936 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500937 *
938 * @param name Name of the thread.
939 * @param stack_size Stack size in bytes.
940 * @param entry Thread entry function.
941 * @param p1 1st entry point parameter.
942 * @param p2 2nd entry point parameter.
943 * @param p3 3rd entry point parameter.
944 * @param prio Thread priority.
945 * @param options Thread options.
946 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400947 *
Anas Nashif47420d02018-05-24 14:20:56 -0400948 * @req K-THREAD-010
949 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400950 * @internal It has been observed that the x86 compiler by default aligns
951 * these _static_thread_data structures to 32-byte boundaries, thereby
952 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400953 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400954 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500955#define K_THREAD_DEFINE(name, stack_size, \
956 entry, p1, p2, p3, \
957 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700958 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie41f60112019-01-31 15:53:24 -0800959 struct k_thread _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500960 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500961 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700962 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
963 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500964 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -0600965 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700966 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400967
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400968/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500969 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500971 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500973 * @param thread ID of thread whose priority is needed.
974 *
975 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400976 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400977 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700978__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400979
980/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500981 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500983 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400984 *
985 * Rescheduling can occur immediately depending on the priority @a thread is
986 * set to:
987 *
988 * - If its priority is raised above the priority of the caller of this
989 * function, and the caller is preemptible, @a thread will be scheduled in.
990 *
991 * - If the caller operates on itself, it lowers its priority below that of
992 * other threads in the system, and the caller is preemptible, the thread of
993 * highest priority will be scheduled in.
994 *
995 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
996 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
997 * highest priority.
998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500999 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001000 * @param prio New priority.
1001 *
1002 * @warning Changing the priority of a thread currently involved in mutex
1003 * priority inheritance may result in undefined behavior.
1004 *
1005 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001006 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001007 */
Andrew Boie468190a2017-09-29 14:00:48 -07001008__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001009
Andy Ross4a2e50f2018-05-15 11:06:25 -07001010
1011#ifdef CONFIG_SCHED_DEADLINE
1012/**
1013 * @brief Set deadline expiration time for scheduler
1014 *
1015 * This sets the "deadline" expiration as a time delta from the
1016 * current time, in the same units used by k_cycle_get_32(). The
1017 * scheduler (when deadline scheduling is enabled) will choose the
1018 * next expiring thread when selecting between threads at the same
1019 * static priority. Threads at different priorities will be scheduled
1020 * according to their static priority.
1021 *
1022 * @note Deadlines that are negative (i.e. in the past) are still seen
1023 * as higher priority than others, even if the thread has "finished"
1024 * its work. If you don't want it scheduled anymore, you have to
1025 * reset the deadline into the future, block/pend the thread, or
1026 * modify its priority with k_thread_priority_set().
1027 *
1028 * @note Despite the API naming, the scheduler makes no guarantees the
1029 * the thread WILL be scheduled within that deadline, nor does it take
1030 * extra metadata (like e.g. the "runtime" and "period" parameters in
1031 * Linux sched_setattr()) that allows the kernel to validate the
1032 * scheduling for achievability. Such features could be implemented
1033 * above this call, which is simply input to the priority selection
1034 * logic.
1035 *
1036 * @param thread A thread on which to set the deadline
1037 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001038 *
1039 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001040 */
1041__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1042#endif
1043
Andy Rossab46b1b2019-01-30 15:00:42 -08001044#ifdef CONFIG_SCHED_CPU_MASK
1045/**
1046 * @brief Sets all CPU enable masks to zero
1047 *
1048 * After this returns, the thread will no longer be schedulable on any
1049 * CPUs. The thread must not be currently runnable.
1050 *
1051 * @param thread Thread to operate upon
1052 * @return Zero on success, otherwise error code
1053 */
1054int k_thread_cpu_mask_clear(k_tid_t thread);
1055
1056/**
1057 * @brief Sets all CPU enable masks to one
1058 *
1059 * After this returns, the thread will be schedulable on any CPU. The
1060 * thread must not be currently runnable.
1061 *
1062 * @param thread Thread to operate upon
1063 * @return Zero on success, otherwise error code
1064 */
1065int k_thread_cpu_mask_enable_all(k_tid_t thread);
1066
1067/**
1068 * @brief Enable thread to run on specified CPU
1069 *
1070 * The thread must not be currently runnable.
1071 *
1072 * @param thread Thread to operate upon
1073 * @param cpu CPU index
1074 * @return Zero on success, otherwise error code
1075 */
1076int k_thread_cpu_mask_enable(k_tid_t thread, int cpu);
1077
1078/**
1079 * @brief Prevent thread to run on specified CPU
1080 *
1081 * The thread must not be currently runnable.
1082 *
1083 * @param thread Thread to operate upon
1084 * @param cpu CPU index
1085 * @return Zero on success, otherwise error code
1086 */
1087int k_thread_cpu_mask_disable(k_tid_t thread, int cpu);
1088#endif
1089
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001090/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001091 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001092 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001093 * This routine prevents the kernel scheduler from making @a thread the
1094 * current thread. All other internal operations on @a thread are still
1095 * performed; for example, any timeout it is waiting on keeps ticking,
1096 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001097 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001098 * If @a thread is already suspended, the routine has no effect.
1099 *
1100 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001101 *
1102 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001103 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001104 */
Andrew Boie468190a2017-09-29 14:00:48 -07001105__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001106
1107/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001108 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001110 * This routine allows the kernel scheduler to make @a thread the current
1111 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001112 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001113 * If @a thread is not currently suspended, the routine has no effect.
1114 *
1115 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001116 *
1117 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001118 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001119 */
Andrew Boie468190a2017-09-29 14:00:48 -07001120__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001121
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001122/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001123 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001125 * This routine specifies how the scheduler will perform time slicing of
1126 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001127 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001128 * To enable time slicing, @a slice must be non-zero. The scheduler
1129 * ensures that no thread runs for more than the specified time limit
1130 * before other threads of that priority are given a chance to execute.
1131 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001132 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001133 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001134 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001135 * execute. Once the scheduler selects a thread for execution, there is no
1136 * minimum guaranteed time the thread will execute before threads of greater or
1137 * equal priority are scheduled.
1138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001139 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001140 * for execution, this routine has no effect; the thread is immediately
1141 * rescheduled after the slice period expires.
1142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001143 * To disable timeslicing, set both @a slice and @a prio to zero.
1144 *
1145 * @param slice Maximum time slice length (in milliseconds).
1146 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001147 *
1148 * @return N/A
1149 */
Kumar Galacc334c72017-04-21 10:55:34 -05001150extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001151
Anas Nashif166f5192018-02-25 08:02:36 -06001152/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001153
1154/**
1155 * @addtogroup isr_apis
1156 * @{
1157 */
1158
1159/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001160 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001161 *
Allan Stephensc98da842016-11-11 15:45:03 -05001162 * This routine allows the caller to customize its actions, depending on
1163 * whether it is a thread or an ISR.
1164 *
1165 * @note Can be called by ISRs.
1166 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001167 * @return false if invoked by a thread.
1168 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001169 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001170extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001171
Benjamin Walsh445830d2016-11-10 15:54:27 -05001172/**
1173 * @brief Determine if code is running in a preemptible thread.
1174 *
Allan Stephensc98da842016-11-11 15:45:03 -05001175 * This routine allows the caller to customize its actions, depending on
1176 * whether it can be preempted by another thread. The routine returns a 'true'
1177 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001178 *
Allan Stephensc98da842016-11-11 15:45:03 -05001179 * - The code is running in a thread, not at ISR.
1180 * - The thread's priority is in the preemptible range.
1181 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001182 *
Allan Stephensc98da842016-11-11 15:45:03 -05001183 * @note Can be called by ISRs.
1184 *
1185 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001186 * @return Non-zero if invoked by a preemptible thread.
1187 */
Andrew Boie468190a2017-09-29 14:00:48 -07001188__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001189
Allan Stephensc98da842016-11-11 15:45:03 -05001190/**
Anas Nashif166f5192018-02-25 08:02:36 -06001191 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001192 */
1193
1194/**
1195 * @addtogroup thread_apis
1196 * @{
1197 */
1198
1199/**
1200 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001201 *
Allan Stephensc98da842016-11-11 15:45:03 -05001202 * This routine prevents the current thread from being preempted by another
1203 * thread by instructing the scheduler to treat it as a cooperative thread.
1204 * If the thread subsequently performs an operation that makes it unready,
1205 * it will be context switched out in the normal manner. When the thread
1206 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001207 *
Allan Stephensc98da842016-11-11 15:45:03 -05001208 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001209 *
Allan Stephensc98da842016-11-11 15:45:03 -05001210 * @note k_sched_lock() and k_sched_unlock() should normally be used
1211 * when the operation being performed can be safely interrupted by ISRs.
1212 * However, if the amount of processing involved is very small, better
1213 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001214 *
1215 * @return N/A
1216 */
1217extern void k_sched_lock(void);
1218
Allan Stephensc98da842016-11-11 15:45:03 -05001219/**
1220 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001221 *
Allan Stephensc98da842016-11-11 15:45:03 -05001222 * This routine reverses the effect of a previous call to k_sched_lock().
1223 * A thread must call the routine once for each time it called k_sched_lock()
1224 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001225 *
1226 * @return N/A
1227 */
1228extern void k_sched_unlock(void);
1229
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001230/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001231 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001233 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001235 * Custom data is not used by the kernel itself, and is freely available
1236 * for a thread to use as it sees fit. It can be used as a framework
1237 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001239 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001240 *
1241 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001242 *
1243 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001244 */
Andrew Boie468190a2017-09-29 14:00:48 -07001245__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001246
1247/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001248 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001249 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001250 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001251 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001252 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001253 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001254 */
Andrew Boie468190a2017-09-29 14:00:48 -07001255__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001256
1257/**
Anas Nashif57554052018-03-03 02:31:05 -06001258 * @brief Set current thread name
1259 *
1260 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1261 * tracing and debugging.
1262 *
1263 */
1264__syscall void k_thread_name_set(k_tid_t thread_id, const char *value);
1265
1266/**
1267 * @brief Get thread name
1268 *
1269 * Get the name of a thread
1270 *
1271 * @param thread_id Thread ID
1272 *
1273 */
1274__syscall const char *k_thread_name_get(k_tid_t thread_id);
1275
1276/**
Andy Rosscfe62032018-09-29 07:34:55 -07001277 * @}
1278 */
1279
1280/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001281 * @addtogroup clock_apis
1282 * @{
1283 */
1284
1285/**
1286 * @brief Generate null timeout delay.
1287 *
1288 * This macro generates a timeout delay that that instructs a kernel API
1289 * not to wait if the requested operation cannot be performed immediately.
1290 *
1291 * @return Timeout delay value.
1292 */
1293#define K_NO_WAIT 0
1294
1295/**
1296 * @brief Generate timeout delay from milliseconds.
1297 *
1298 * This macro generates a timeout delay that that instructs a kernel API
1299 * to wait up to @a ms milliseconds to perform the requested operation.
1300 *
1301 * @param ms Duration in milliseconds.
1302 *
1303 * @return Timeout delay value.
1304 */
Johan Hedberg14471692016-11-13 10:52:15 +02001305#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001306
1307/**
1308 * @brief Generate timeout delay from seconds.
1309 *
1310 * This macro generates a timeout delay that that instructs a kernel API
1311 * to wait up to @a s seconds to perform the requested operation.
1312 *
1313 * @param s Duration in seconds.
1314 *
1315 * @return Timeout delay value.
1316 */
Johan Hedberg14471692016-11-13 10:52:15 +02001317#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001318
1319/**
1320 * @brief Generate timeout delay from minutes.
1321 *
1322 * This macro generates a timeout delay that that instructs a kernel API
1323 * to wait up to @a m minutes to perform the requested operation.
1324 *
1325 * @param m Duration in minutes.
1326 *
1327 * @return Timeout delay value.
1328 */
Johan Hedberg14471692016-11-13 10:52:15 +02001329#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001330
1331/**
1332 * @brief Generate timeout delay from hours.
1333 *
1334 * This macro generates a timeout delay that that instructs a kernel API
1335 * to wait up to @a h hours to perform the requested operation.
1336 *
1337 * @param h Duration in hours.
1338 *
1339 * @return Timeout delay value.
1340 */
Johan Hedberg14471692016-11-13 10:52:15 +02001341#define K_HOURS(h) K_MINUTES((h) * 60)
1342
Allan Stephensc98da842016-11-11 15:45:03 -05001343/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001344 * @brief Generate infinite timeout delay.
1345 *
1346 * This macro generates a timeout delay that that instructs a kernel API
1347 * to wait as long as necessary to perform the requested operation.
1348 *
1349 * @return Timeout delay value.
1350 */
1351#define K_FOREVER (-1)
1352
1353/**
Anas Nashif166f5192018-02-25 08:02:36 -06001354 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001355 */
1356
1357/**
Allan Stephensc98da842016-11-11 15:45:03 -05001358 * @cond INTERNAL_HIDDEN
1359 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001360
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001361struct k_timer {
1362 /*
1363 * _timeout structure must be first here if we want to use
1364 * dynamic timer allocation. timeout.node is used in the double-linked
1365 * list of free timers
1366 */
1367 struct _timeout timeout;
1368
Allan Stephens45bfa372016-10-12 12:39:42 -05001369 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001370 _wait_q_t wait_q;
1371
1372 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001373 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001374
1375 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001376 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001377
1378 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001379 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001380
Allan Stephens45bfa372016-10-12 12:39:42 -05001381 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001382 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001383
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001384 /* user-specific data, also used to support legacy features */
1385 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001386
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001387 _OBJECT_TRACING_NEXT_PTR(k_timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001388};
1389
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001390#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001391 { \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001392 .timeout = { \
1393 .node = {},\
1394 .dticks = 0, \
1395 .fn = _timer_expiration_handler \
1396 }, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001397 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001398 .expiry_fn = expiry, \
1399 .stop_fn = stop, \
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01001400 .period = 0, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001401 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001402 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001403 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001404 }
1405
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001406#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1407
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001408/**
Allan Stephensc98da842016-11-11 15:45:03 -05001409 * INTERNAL_HIDDEN @endcond
1410 */
1411
1412/**
1413 * @defgroup timer_apis Timer APIs
1414 * @ingroup kernel_apis
1415 * @{
1416 */
1417
1418/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001419 * @typedef k_timer_expiry_t
1420 * @brief Timer expiry function type.
1421 *
1422 * A timer's expiry function is executed by the system clock interrupt handler
1423 * each time the timer expires. The expiry function is optional, and is only
1424 * invoked if the timer has been initialized with one.
1425 *
1426 * @param timer Address of timer.
1427 *
1428 * @return N/A
1429 */
1430typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1431
1432/**
1433 * @typedef k_timer_stop_t
1434 * @brief Timer stop function type.
1435 *
1436 * A timer's stop function is executed if the timer is stopped prematurely.
1437 * The function runs in the context of the thread that stops the timer.
1438 * The stop function is optional, and is only invoked if the timer has been
1439 * initialized with one.
1440 *
1441 * @param timer Address of timer.
1442 *
1443 * @return N/A
1444 */
1445typedef void (*k_timer_stop_t)(struct k_timer *timer);
1446
1447/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001448 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001450 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001451 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001452 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001453 *
1454 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001455 * @param expiry_fn Function to invoke each time the timer expires.
1456 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001457 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001458#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001459 struct k_timer name \
1460 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001461 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001462
Allan Stephens45bfa372016-10-12 12:39:42 -05001463/**
1464 * @brief Initialize a timer.
1465 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001466 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001467 *
1468 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001469 * @param expiry_fn Function to invoke each time the timer expires.
1470 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001471 *
1472 * @return N/A
1473 */
1474extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001475 k_timer_expiry_t expiry_fn,
1476 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001477
Allan Stephens45bfa372016-10-12 12:39:42 -05001478/**
1479 * @brief Start a timer.
1480 *
1481 * This routine starts a timer, and resets its status to zero. The timer
1482 * begins counting down using the specified duration and period values.
1483 *
1484 * Attempting to start a timer that is already running is permitted.
1485 * The timer's status is reset to zero and the timer begins counting down
1486 * using the new duration and period values.
1487 *
1488 * @param timer Address of timer.
1489 * @param duration Initial timer duration (in milliseconds).
1490 * @param period Timer period (in milliseconds).
1491 *
1492 * @return N/A
1493 */
Andrew Boiea354d492017-09-29 16:22:28 -07001494__syscall void k_timer_start(struct k_timer *timer,
1495 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001496
1497/**
1498 * @brief Stop a timer.
1499 *
1500 * This routine stops a running timer prematurely. The timer's stop function,
1501 * if one exists, is invoked by the caller.
1502 *
1503 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001504 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001505 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001506 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1507 * if @a k_timer_stop is to be called from ISRs.
1508 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001509 * @param timer Address of timer.
1510 *
1511 * @return N/A
1512 */
Andrew Boiea354d492017-09-29 16:22:28 -07001513__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001514
1515/**
1516 * @brief Read timer status.
1517 *
1518 * This routine reads the timer's status, which indicates the number of times
1519 * it has expired since its status was last read.
1520 *
1521 * Calling this routine resets the timer's status to zero.
1522 *
1523 * @param timer Address of timer.
1524 *
1525 * @return Timer status.
1526 */
Andrew Boiea354d492017-09-29 16:22:28 -07001527__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001528
1529/**
1530 * @brief Synchronize thread to timer expiration.
1531 *
1532 * This routine blocks the calling thread until the timer's status is non-zero
1533 * (indicating that it has expired at least once since it was last examined)
1534 * or the timer is stopped. If the timer status is already non-zero,
1535 * or the timer is already stopped, the caller continues without waiting.
1536 *
1537 * Calling this routine resets the timer's status to zero.
1538 *
1539 * This routine must not be used by interrupt handlers, since they are not
1540 * allowed to block.
1541 *
1542 * @param timer Address of timer.
1543 *
1544 * @return Timer status.
1545 */
Andrew Boiea354d492017-09-29 16:22:28 -07001546__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001547
Andy Ross52e444b2018-09-28 09:06:37 -07001548extern s32_t z_timeout_remaining(struct _timeout *timeout);
1549
Allan Stephens45bfa372016-10-12 12:39:42 -05001550/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001551 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001552 *
1553 * This routine computes the (approximate) time remaining before a running
1554 * timer next expires. If the timer is not running, it returns zero.
1555 *
1556 * @param timer Address of timer.
1557 *
1558 * @return Remaining time (in milliseconds).
1559 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001560__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001561
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001562static inline u32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001563{
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001564 return (u32_t)__ticks_to_ms(z_timeout_remaining(&timer->timeout));
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001565}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001566
Allan Stephensc98da842016-11-11 15:45:03 -05001567/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001568 * @brief Associate user-specific data with a timer.
1569 *
1570 * This routine records the @a user_data with the @a timer, to be retrieved
1571 * later.
1572 *
1573 * It can be used e.g. in a timer handler shared across multiple subsystems to
1574 * retrieve data specific to the subsystem this timer is associated with.
1575 *
1576 * @param timer Address of timer.
1577 * @param user_data User data to associate with the timer.
1578 *
1579 * @return N/A
1580 */
Andrew Boiea354d492017-09-29 16:22:28 -07001581__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1582
Anas Nashif954d5502018-02-25 08:37:28 -06001583/**
1584 * @internal
1585 */
Andrew Boiea354d492017-09-29 16:22:28 -07001586static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1587 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001588{
1589 timer->user_data = user_data;
1590}
1591
1592/**
1593 * @brief Retrieve the user-specific data from a timer.
1594 *
1595 * @param timer Address of timer.
1596 *
1597 * @return The user data.
1598 */
Andrew Boiea354d492017-09-29 16:22:28 -07001599__syscall void *k_timer_user_data_get(struct k_timer *timer);
1600
1601static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001602{
1603 return timer->user_data;
1604}
1605
Anas Nashif166f5192018-02-25 08:02:36 -06001606/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001607
Allan Stephensc98da842016-11-11 15:45:03 -05001608/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001609 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001610 * @{
1611 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001612
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001613/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001614 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001615 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001616 * This routine returns the elapsed time since the system booted,
1617 * in milliseconds.
1618 *
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001619 * @note While this function returns time in milliseconds, it does not mean it
1620 * has millisecond resolution. The actual resolution depends on
1621 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option, and with the default
1622 * setting of 100, system time is updated in increments of 10ms.
1623 *
1624 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001625 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001626__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001627
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001628/**
1629 * @brief Enable clock always on in tickless kernel
1630 *
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001631 * This routine enables keeping the clock running (that is, it always
1632 * keeps an active timer interrupt scheduled) when there are no timer
1633 * events programmed in tickless kernel scheduling. This is necessary
1634 * if the clock is used to track passage of time (e.g. via
1635 * k_uptime_get_32()), otherwise the internal hardware counter may
1636 * roll over between interrupts.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001637 *
1638 * @retval prev_status Previous status of always on flag
1639 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001640int k_enable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001641
1642/**
1643 * @brief Disable clock always on in tickless kernel
1644 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001645 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001646 * there are no timer events programmed in tickless kernel
1647 * scheduling. To save power, this routine should be called
1648 * immediately when clock is not used to track time.
1649 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001650void k_disable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001651
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001652/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001653 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001654 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001655 * This routine returns the lower 32-bits of the elapsed time since the system
1656 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001657 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001658 * This routine can be more efficient than k_uptime_get(), as it reduces the
1659 * need for interrupt locking and 64-bit math. However, the 32-bit result
1660 * cannot hold a system uptime time larger than approximately 50 days, so the
1661 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001662 *
Paul Sokolovsky65d51fd2019-02-04 22:44:50 +03001663 * @note While this function returns time in milliseconds, it does not mean it
1664 * has millisecond resolution. The actual resolution depends on
1665 * :option:`CONFIG_SYS_CLOCK_TICKS_PER_SEC` config option, and with the default
1666 * setting of 100, system time is updated in increments of 10ms.
1667 *
1668 * @return Current uptime in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001669 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001670__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001671
1672/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001673 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001674 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001675 * This routine computes the elapsed time between the current system uptime
1676 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001677 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001678 * @param reftime Pointer to a reference time, which is updated to the current
1679 * uptime upon return.
1680 *
1681 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001682 */
Andy Ross987c0e52018-09-27 16:50:00 -07001683static inline s64_t k_uptime_delta(s64_t *reftime)
1684{
1685 s64_t uptime, delta;
1686
1687 uptime = k_uptime_get();
1688 delta = uptime - *reftime;
1689 *reftime = uptime;
1690
1691 return delta;
1692}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001693
1694/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001695 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001696 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001697 * This routine computes the elapsed time between the current system uptime
1698 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001699 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001700 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1701 * need for interrupt locking and 64-bit math. However, the 32-bit result
1702 * cannot hold an elapsed time larger than approximately 50 days, so the
1703 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001705 * @param reftime Pointer to a reference time, which is updated to the current
1706 * uptime upon return.
1707 *
1708 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001709 */
Andy Ross987c0e52018-09-27 16:50:00 -07001710static inline u32_t k_uptime_delta_32(s64_t *reftime)
1711{
1712 return (u32_t)k_uptime_delta(reftime);
1713}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001714
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001715/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001716 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001717 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001718 * This routine returns the current time, as measured by the system's hardware
1719 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001720 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001721 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001722 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001723#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001724
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001725/**
Anas Nashif166f5192018-02-25 08:02:36 -06001726 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001727 */
1728
Allan Stephensc98da842016-11-11 15:45:03 -05001729/**
1730 * @cond INTERNAL_HIDDEN
1731 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001732
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001733struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001734 sys_sflist_t data_q;
Andy Ross603ea422018-07-25 13:01:54 -07001735 struct k_spinlock lock;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001736 union {
1737 _wait_q_t wait_q;
1738
1739 _POLL_EVENT;
1740 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001741
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001742 _OBJECT_TRACING_NEXT_PTR(k_queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001743};
1744
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001745#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001746 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001747 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001748 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001749 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001750 _OBJECT_TRACING_INIT \
1751 }
1752
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001753#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1754
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001755extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1756
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001757/**
1758 * INTERNAL_HIDDEN @endcond
1759 */
1760
1761/**
1762 * @defgroup queue_apis Queue APIs
1763 * @ingroup kernel_apis
1764 * @{
1765 */
1766
1767/**
1768 * @brief Initialize a queue.
1769 *
1770 * This routine initializes a queue object, prior to its first use.
1771 *
1772 * @param queue Address of the queue.
1773 *
1774 * @return N/A
1775 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001776__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001777
1778/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001779 * @brief Cancel waiting on a queue.
1780 *
1781 * This routine causes first thread pending on @a queue, if any, to
1782 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001783 * If the queue is being waited on by k_poll(), it will return with
1784 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1785 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001786 *
1787 * @note Can be called by ISRs.
1788 *
1789 * @param queue Address of the queue.
1790 *
1791 * @return N/A
1792 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001793__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001794
1795/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001796 * @brief Append an element to the end of a queue.
1797 *
1798 * This routine appends a data item to @a queue. A queue data item must be
1799 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1800 * reserved for the kernel's use.
1801 *
1802 * @note Can be called by ISRs.
1803 *
1804 * @param queue Address of the queue.
1805 * @param data Address of the data item.
1806 *
1807 * @return N/A
1808 */
1809extern void k_queue_append(struct k_queue *queue, void *data);
1810
1811/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001812 * @brief Append an element to a queue.
1813 *
1814 * This routine appends a data item to @a queue. There is an implicit
1815 * memory allocation from the calling thread's resource pool, which is
1816 * automatically freed when the item is removed from the queue.
1817 *
1818 * @note Can be called by ISRs.
1819 *
1820 * @param queue Address of the queue.
1821 * @param data Address of the data item.
1822 *
1823 * @retval 0 on success
1824 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1825 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301826__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001827
1828/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001829 * @brief Prepend an element to a queue.
1830 *
1831 * This routine prepends a data item to @a queue. A queue data item must be
1832 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1833 * reserved for the kernel's use.
1834 *
1835 * @note Can be called by ISRs.
1836 *
1837 * @param queue Address of the queue.
1838 * @param data Address of the data item.
1839 *
1840 * @return N/A
1841 */
1842extern void k_queue_prepend(struct k_queue *queue, void *data);
1843
1844/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001845 * @brief Prepend an element to a queue.
1846 *
1847 * This routine prepends a data item to @a queue. There is an implicit
1848 * memory allocation from the calling thread's resource pool, which is
1849 * automatically freed when the item is removed from the queue.
1850 *
1851 * @note Can be called by ISRs.
1852 *
1853 * @param queue Address of the queue.
1854 * @param data Address of the data item.
1855 *
1856 * @retval 0 on success
1857 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1858 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301859__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001860
1861/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001862 * @brief Inserts an element to a queue.
1863 *
1864 * This routine inserts a data item to @a queue after previous item. A queue
1865 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1866 * item are reserved for the kernel's use.
1867 *
1868 * @note Can be called by ISRs.
1869 *
1870 * @param queue Address of the queue.
1871 * @param prev Address of the previous data item.
1872 * @param data Address of the data item.
1873 *
1874 * @return N/A
1875 */
1876extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1877
1878/**
1879 * @brief Atomically append a list of elements to a queue.
1880 *
1881 * This routine adds a list of data items to @a queue in one operation.
1882 * The data items must be in a singly-linked list, with the first 32 bits
1883 * in each data item pointing to the next data item; the list must be
1884 * NULL-terminated.
1885 *
1886 * @note Can be called by ISRs.
1887 *
1888 * @param queue Address of the queue.
1889 * @param head Pointer to first node in singly-linked list.
1890 * @param tail Pointer to last node in singly-linked list.
1891 *
1892 * @return N/A
1893 */
1894extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1895
1896/**
1897 * @brief Atomically add a list of elements to a queue.
1898 *
1899 * This routine adds a list of data items to @a queue in one operation.
1900 * The data items must be in a singly-linked list implemented using a
1901 * sys_slist_t object. Upon completion, the original list is empty.
1902 *
1903 * @note Can be called by ISRs.
1904 *
1905 * @param queue Address of the queue.
1906 * @param list Pointer to sys_slist_t object.
1907 *
1908 * @return N/A
1909 */
1910extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1911
1912/**
1913 * @brief Get an element from a queue.
1914 *
1915 * This routine removes first data item from @a queue. The first 32 bits of the
1916 * data item are reserved for the kernel's use.
1917 *
1918 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1919 *
1920 * @param queue Address of the queue.
1921 * @param timeout Waiting period to obtain a data item (in milliseconds),
1922 * or one of the special values K_NO_WAIT and K_FOREVER.
1923 *
1924 * @return Address of the data item if successful; NULL if returned
1925 * without waiting, or waiting period timed out.
1926 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001927__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001928
1929/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001930 * @brief Remove an element from a queue.
1931 *
1932 * This routine removes data item from @a queue. The first 32 bits of the
1933 * data item are reserved for the kernel's use. Removing elements from k_queue
1934 * rely on sys_slist_find_and_remove which is not a constant time operation.
1935 *
1936 * @note Can be called by ISRs
1937 *
1938 * @param queue Address of the queue.
1939 * @param data Address of the data item.
1940 *
1941 * @return true if data item was removed
1942 */
1943static inline bool k_queue_remove(struct k_queue *queue, void *data)
1944{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001945 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001946}
1947
1948/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02001949 * @brief Append an element to a queue only if it's not present already.
1950 *
1951 * This routine appends data item to @a queue. The first 32 bits of the
1952 * data item are reserved for the kernel's use. Appending elements to k_queue
1953 * relies on sys_slist_is_node_in_list which is not a constant time operation.
1954 *
1955 * @note Can be called by ISRs
1956 *
1957 * @param queue Address of the queue.
1958 * @param data Address of the data item.
1959 *
1960 * @return true if data item was added, false if not
1961 */
1962static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
1963{
1964 sys_sfnode_t *test;
1965
1966 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
1967 if (test == (sys_sfnode_t *) data) {
1968 return false;
1969 }
1970 }
1971
1972 k_queue_append(queue, data);
1973 return true;
1974}
1975
1976/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001977 * @brief Query a queue to see if it has data available.
1978 *
1979 * Note that the data might be already gone by the time this function returns
1980 * if other threads are also trying to read from the queue.
1981 *
1982 * @note Can be called by ISRs.
1983 *
1984 * @param queue Address of the queue.
1985 *
1986 * @return Non-zero if the queue is empty.
1987 * @return 0 if data is available.
1988 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001989__syscall int k_queue_is_empty(struct k_queue *queue);
1990
1991static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001992{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001993 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001994}
1995
1996/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001997 * @brief Peek element at the head of queue.
1998 *
1999 * Return element from the head of queue without removing it.
2000 *
2001 * @param queue Address of the queue.
2002 *
2003 * @return Head element, or NULL if queue is empty.
2004 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002005__syscall void *k_queue_peek_head(struct k_queue *queue);
2006
2007static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002008{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002009 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002010}
2011
2012/**
2013 * @brief Peek element at the tail of queue.
2014 *
2015 * Return element from the tail of queue without removing it.
2016 *
2017 * @param queue Address of the queue.
2018 *
2019 * @return Tail element, or NULL if queue is empty.
2020 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002021__syscall void *k_queue_peek_tail(struct k_queue *queue);
2022
2023static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002024{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002025 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002026}
2027
2028/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002029 * @brief Statically define and initialize a queue.
2030 *
2031 * The queue can be accessed outside the module where it is defined using:
2032 *
2033 * @code extern struct k_queue <name>; @endcode
2034 *
2035 * @param name Name of the queue.
2036 */
2037#define K_QUEUE_DEFINE(name) \
2038 struct k_queue name \
2039 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002040 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002041
Anas Nashif166f5192018-02-25 08:02:36 -06002042/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002043
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002044struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002045 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002046};
2047
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002048/**
2049 * @cond INTERNAL_HIDDEN
2050 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002051#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002052 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002053 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002054 }
2055
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002056#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
2057
Allan Stephensc98da842016-11-11 15:45:03 -05002058/**
2059 * INTERNAL_HIDDEN @endcond
2060 */
2061
2062/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002063 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002064 * @ingroup kernel_apis
2065 * @{
2066 */
2067
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002068/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002069 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002070 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002071 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002072 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002073 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002074 *
2075 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002076 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002077 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002078#define k_fifo_init(fifo) \
2079 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002080
2081/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002082 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002083 *
2084 * This routine causes first thread pending on @a fifo, if any, to
2085 * return from k_fifo_get() call with NULL value (as if timeout
2086 * expired).
2087 *
2088 * @note Can be called by ISRs.
2089 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002090 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002091 *
2092 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002093 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002094 */
2095#define k_fifo_cancel_wait(fifo) \
2096 k_queue_cancel_wait((struct k_queue *) fifo)
2097
2098/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002099 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002100 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002101 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002102 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2103 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002104 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002105 * @note Can be called by ISRs.
2106 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002107 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002108 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002109 *
2110 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002111 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002112 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002113#define k_fifo_put(fifo, data) \
2114 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002115
2116/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002117 * @brief Add an element to a FIFO queue.
2118 *
2119 * This routine adds a data item to @a fifo. There is an implicit
2120 * memory allocation from the calling thread's resource pool, which is
2121 * automatically freed when the item is removed.
2122 *
2123 * @note Can be called by ISRs.
2124 *
2125 * @param fifo Address of the FIFO.
2126 * @param data Address of the data item.
2127 *
2128 * @retval 0 on success
2129 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002130 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002131 */
2132#define k_fifo_alloc_put(fifo, data) \
2133 k_queue_alloc_append((struct k_queue *) fifo, data)
2134
2135/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002136 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002137 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002138 * This routine adds a list of data items to @a fifo in one operation.
2139 * The data items must be in a singly-linked list, with the first 32 bits
2140 * each data item pointing to the next data item; the list must be
2141 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002143 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002144 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002145 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002146 * @param head Pointer to first node in singly-linked list.
2147 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002148 *
2149 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002150 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002151 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002152#define k_fifo_put_list(fifo, head, tail) \
2153 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002154
2155/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002156 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002157 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002158 * This routine adds a list of data items to @a fifo in one operation.
2159 * The data items must be in a singly-linked list implemented using a
2160 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002161 * and must be re-initialized via sys_slist_init().
2162 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002163 * @note Can be called by ISRs.
2164 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002165 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002166 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002167 *
2168 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002169 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002170 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002171#define k_fifo_put_slist(fifo, list) \
2172 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002173
2174/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002175 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002176 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002177 * This routine removes a data item from @a fifo in a "first in, first out"
2178 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002180 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2181 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002182 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002183 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002184 * or one of the special values K_NO_WAIT and K_FOREVER.
2185 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002186 * @return Address of the data item if successful; NULL if returned
2187 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002188 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002189 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002190#define k_fifo_get(fifo, timeout) \
2191 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002192
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002193/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002194 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002195 *
2196 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002197 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002198 *
2199 * @note Can be called by ISRs.
2200 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002201 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002202 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002203 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002204 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002205 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002206 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002207#define k_fifo_is_empty(fifo) \
2208 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002209
2210/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002211 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002212 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002213 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302214 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002215 * on each iteration of processing, a head container will be peeked,
2216 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002217 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002218 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002219 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002220 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002221 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002222 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002223 */
2224#define k_fifo_peek_head(fifo) \
2225 k_queue_peek_head((struct k_queue *) fifo)
2226
2227/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002228 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002229 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002230 * Return element from the tail of FIFO queue (without removing it). A usecase
2231 * for this is if elements of the FIFO queue are themselves containers. Then
2232 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002233 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002234 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002235 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002236 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002237 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002238 */
2239#define k_fifo_peek_tail(fifo) \
2240 k_queue_peek_tail((struct k_queue *) fifo)
2241
2242/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002243 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002244 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002245 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002246 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002247 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002248 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002249 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002250 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002251 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002252#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002253 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002254 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002255 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002256
Anas Nashif166f5192018-02-25 08:02:36 -06002257/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002258
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002259struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002260 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002261};
2262
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002263/**
2264 * @cond INTERNAL_HIDDEN
2265 */
2266
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002267#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002268 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002269 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002270 }
2271
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002272#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2273
Allan Stephensc98da842016-11-11 15:45:03 -05002274/**
2275 * INTERNAL_HIDDEN @endcond
2276 */
2277
2278/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002279 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002280 * @ingroup kernel_apis
2281 * @{
2282 */
2283
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002284/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002285 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002286 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002287 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002288 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002289 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002290 *
2291 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002292 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002293 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002294#define k_lifo_init(lifo) \
2295 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002296
2297/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002298 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002299 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002300 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002301 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2302 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002303 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002304 * @note Can be called by ISRs.
2305 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002306 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002307 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002308 *
2309 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002310 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002311 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002312#define k_lifo_put(lifo, data) \
2313 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002314
2315/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002316 * @brief Add an element to a LIFO queue.
2317 *
2318 * This routine adds a data item to @a lifo. There is an implicit
2319 * memory allocation from the calling thread's resource pool, which is
2320 * automatically freed when the item is removed.
2321 *
2322 * @note Can be called by ISRs.
2323 *
2324 * @param lifo Address of the LIFO.
2325 * @param data Address of the data item.
2326 *
2327 * @retval 0 on success
2328 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002329 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002330 */
2331#define k_lifo_alloc_put(lifo, data) \
2332 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2333
2334/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002335 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002337 * This routine removes a data item from @a lifo in a "last in, first out"
2338 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002339 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002340 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2341 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002342 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002343 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002344 * or one of the special values K_NO_WAIT and K_FOREVER.
2345 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002346 * @return Address of the data item if successful; NULL if returned
2347 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002348 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002349 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002350#define k_lifo_get(lifo, timeout) \
2351 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002352
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002353/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002354 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002355 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002356 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002357 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002358 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002359 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002360 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002361 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002362 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002363#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002364 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002365 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002366 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002367
Anas Nashif166f5192018-02-25 08:02:36 -06002368/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002369
2370/**
2371 * @cond INTERNAL_HIDDEN
2372 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302373#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002374
2375struct k_stack {
2376 _wait_q_t wait_q;
Andy Rossf0933d02018-07-26 10:23:02 -07002377 struct k_spinlock lock;
Kumar Galacc334c72017-04-21 10:55:34 -05002378 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002379
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002380 _OBJECT_TRACING_NEXT_PTR(k_stack)
Andrew Boief3bee952018-05-02 17:44:39 -07002381 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002382};
2383
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002384#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002385 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002386 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002387 .base = stack_buffer, \
2388 .next = stack_buffer, \
2389 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002390 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002391 }
2392
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002393#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2394
Allan Stephensc98da842016-11-11 15:45:03 -05002395/**
2396 * INTERNAL_HIDDEN @endcond
2397 */
2398
2399/**
2400 * @defgroup stack_apis Stack APIs
2401 * @ingroup kernel_apis
2402 * @{
2403 */
2404
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002405/**
2406 * @brief Initialize a stack.
2407 *
2408 * This routine initializes a stack object, prior to its first use.
2409 *
2410 * @param stack Address of the stack.
2411 * @param buffer Address of array used to hold stacked values.
2412 * @param num_entries Maximum number of values that can be stacked.
2413 *
2414 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002415 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002416 */
Andrew Boief3bee952018-05-02 17:44:39 -07002417void k_stack_init(struct k_stack *stack,
Adithya Baglody28080d32018-10-15 11:48:51 +05302418 u32_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002419
2420
2421/**
2422 * @brief Initialize a stack.
2423 *
2424 * This routine initializes a stack object, prior to its first use. Internal
2425 * buffers will be allocated from the calling thread's resource pool.
2426 * This memory will be released if k_stack_cleanup() is called, or
2427 * userspace is enabled and the stack object loses all references to it.
2428 *
2429 * @param stack Address of the stack.
2430 * @param num_entries Maximum number of values that can be stacked.
2431 *
2432 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002433 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002434 */
2435
Adithya Baglody28080d32018-10-15 11:48:51 +05302436__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2437 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002438
2439/**
2440 * @brief Release a stack's allocated buffer
2441 *
2442 * If a stack object was given a dynamically allocated buffer via
2443 * k_stack_alloc_init(), this will free it. This function does nothing
2444 * if the buffer wasn't dynamically allocated.
2445 *
2446 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002447 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002448 */
2449void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002450
2451/**
2452 * @brief Push an element onto a stack.
2453 *
2454 * This routine adds a 32-bit value @a data to @a stack.
2455 *
2456 * @note Can be called by ISRs.
2457 *
2458 * @param stack Address of the stack.
2459 * @param data Value to push onto the stack.
2460 *
2461 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002462 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002463 */
Andrew Boiee8734462017-09-29 16:42:07 -07002464__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002465
2466/**
2467 * @brief Pop an element from a stack.
2468 *
2469 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2470 * manner and stores the value in @a data.
2471 *
2472 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2473 *
2474 * @param stack Address of the stack.
2475 * @param data Address of area to hold the value popped from the stack.
2476 * @param timeout Waiting period to obtain a value (in milliseconds),
2477 * or one of the special values K_NO_WAIT and K_FOREVER.
2478 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002479 * @retval 0 Element popped from stack.
2480 * @retval -EBUSY Returned without waiting.
2481 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002482 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002483 */
Andrew Boiee8734462017-09-29 16:42:07 -07002484__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002485
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002486/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002487 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002489 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002490 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002491 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002492 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002493 * @param name Name of the stack.
2494 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002495 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002496 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002497#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002498 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002499 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002500 struct k_stack name \
2501 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002502 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002503 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002504
Anas Nashif166f5192018-02-25 08:02:36 -06002505/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002506
Allan Stephens6bba9b02016-11-16 14:56:54 -05002507struct k_work;
2508
Allan Stephensc98da842016-11-11 15:45:03 -05002509/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002510 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002511 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002512 */
2513
Allan Stephens6bba9b02016-11-16 14:56:54 -05002514/**
2515 * @typedef k_work_handler_t
2516 * @brief Work item handler function type.
2517 *
2518 * A work item's handler function is executed by a workqueue's thread
2519 * when the work item is processed by the workqueue.
2520 *
2521 * @param work Address of the work item.
2522 *
2523 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002524 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002525 */
2526typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002527
2528/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002529 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002530 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002531
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002532struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002533 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002534 struct k_thread thread;
Andy Rossa37a9812018-07-24 11:26:43 -07002535 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002536};
2537
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002538enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002539 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002540};
2541
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002542struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002543 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002544 k_work_handler_t handler;
2545 atomic_t flags[1];
2546};
2547
Allan Stephens6bba9b02016-11-16 14:56:54 -05002548struct k_delayed_work {
2549 struct k_work work;
2550 struct _timeout timeout;
2551 struct k_work_q *work_q;
2552};
2553
2554extern struct k_work_q k_sys_work_q;
2555
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002557 * INTERNAL_HIDDEN @endcond
2558 */
2559
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002560#define _K_WORK_INITIALIZER(work_handler) \
2561 { \
2562 ._reserved = NULL, \
2563 .handler = work_handler, \
2564 .flags = { 0 } \
2565 }
2566
2567#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2568
Allan Stephens6bba9b02016-11-16 14:56:54 -05002569/**
2570 * @brief Initialize a statically-defined work item.
2571 *
2572 * This macro can be used to initialize a statically-defined workqueue work
2573 * item, prior to its first use. For example,
2574 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002575 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002576 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002577 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002578 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002579 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002580 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002581#define K_WORK_DEFINE(work, work_handler) \
Andrew Boiec2e01df2018-11-12 15:16:54 -08002582 struct k_work work = _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002583
2584/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002585 * @brief Initialize a work item.
2586 *
2587 * This routine initializes a workqueue work item, prior to its first use.
2588 *
2589 * @param work Address of work item.
2590 * @param handler Function to invoke each time work item is processed.
2591 *
2592 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002593 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002594 */
2595static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2596{
Leandro Pereira0e23ad82018-05-29 10:42:17 -07002597 *work = (struct k_work)_K_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002598}
2599
2600/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002601 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002602 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002603 * This routine submits work item @a work to be processed by workqueue
2604 * @a work_q. If the work item is already pending in the workqueue's queue
2605 * as a result of an earlier submission, this routine has no effect on the
2606 * work item. If the work item has already been processed, or is currently
2607 * being processed, its work is considered complete and the work item can be
2608 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002609 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002610 * @warning
2611 * A submitted work item must not be modified until it has been processed
2612 * by the workqueue.
2613 *
2614 * @note Can be called by ISRs.
2615 *
2616 * @param work_q Address of workqueue.
2617 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002618 *
2619 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002620 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002621 */
2622static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2623 struct k_work *work)
2624{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002625 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002626 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002627 }
2628}
2629
2630/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002631 * @brief Submit a work item to a user mode workqueue
2632 *
David B. Kinder06d78352018-12-17 14:32:40 -08002633 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002634 * memory allocation is made from the caller's resource pool which is freed
2635 * once the worker thread consumes the k_work item. The workqueue
2636 * thread must have memory access to the k_work item being submitted. The caller
2637 * must have permission granted on the work_q parameter's queue object.
2638 *
2639 * Otherwise this works the same as k_work_submit_to_queue().
2640 *
2641 * @note Can be called by ISRs.
2642 *
2643 * @param work_q Address of workqueue.
2644 * @param work Address of work item.
2645 *
2646 * @retval -EBUSY if the work item was already in some workqueue
2647 * @retval -ENOMEM if no memory for thread resource pool allocation
2648 * @retval 0 Success
2649 * @req K-WORK-001
2650 */
2651static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2652 struct k_work *work)
2653{
2654 int ret = -EBUSY;
2655
2656 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2657 ret = k_queue_alloc_append(&work_q->queue, work);
2658
2659 /* Couldn't insert into the queue. Clear the pending bit
2660 * so the work item can be submitted again
2661 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002662 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002663 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2664 }
2665 }
2666
2667 return ret;
2668}
2669
2670/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002671 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002672 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002673 * This routine indicates if work item @a work is pending in a workqueue's
2674 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002675 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002676 * @note Can be called by ISRs.
2677 *
2678 * @param work Address of work item.
2679 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002680 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002681 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002682 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002683static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002684{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002685 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002686}
2687
2688/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002689 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002690 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002691 * This routine starts workqueue @a work_q. The workqueue spawns its work
2692 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002693 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002694 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002695 * @param stack Pointer to work queue thread's stack space, as defined by
2696 * K_THREAD_STACK_DEFINE()
2697 * @param stack_size Size of the work queue thread's stack (in bytes), which
2698 * should either be the same constant passed to
2699 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002700 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002701 *
2702 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002703 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002704 */
Andrew Boie507852a2017-07-25 18:47:07 -07002705extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002706 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002707 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002708
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002709/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002710 * @brief Start a workqueue in user mode
2711 *
2712 * This works identically to k_work_q_start() except it is callable from user
2713 * mode, and the worker thread created will run in user mode.
2714 * The caller must have permissions granted on both the work_q parameter's
2715 * thread and queue objects, and the same restrictions on priority apply as
2716 * k_thread_create().
2717 *
2718 * @param work_q Address of workqueue.
2719 * @param stack Pointer to work queue thread's stack space, as defined by
2720 * K_THREAD_STACK_DEFINE()
2721 * @param stack_size Size of the work queue thread's stack (in bytes), which
2722 * should either be the same constant passed to
2723 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2724 * @param prio Priority of the work queue's thread.
2725 *
2726 * @return N/A
2727 * @req K-WORK-001
2728 */
2729extern void k_work_q_user_start(struct k_work_q *work_q,
2730 k_thread_stack_t *stack,
2731 size_t stack_size, int prio);
2732
2733/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002734 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002735 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002736 * This routine initializes a workqueue delayed work item, prior to
2737 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002738 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002739 * @param work Address of delayed work item.
2740 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002741 *
2742 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002743 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002744 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002745extern void k_delayed_work_init(struct k_delayed_work *work,
2746 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002747
2748/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002749 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002750 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002751 * This routine schedules work item @a work to be processed by workqueue
2752 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002753 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002754 * Only when the countdown completes is the work item actually submitted to
2755 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002756 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002757 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002758 * counting down cancels the existing submission and restarts the
2759 * countdown using the new delay. Note that this behavior is
2760 * inherently subject to race conditions with the pre-existing
2761 * timeouts and work queue, so care must be taken to synchronize such
2762 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002763 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002764 * @warning
2765 * A delayed work item must not be modified until it has been processed
2766 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002767 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002768 * @note Can be called by ISRs.
2769 *
2770 * @param work_q Address of workqueue.
2771 * @param work Address of delayed work item.
2772 * @param delay Delay before submitting the work item (in milliseconds).
2773 *
2774 * @retval 0 Work item countdown started.
2775 * @retval -EINPROGRESS Work item is already pending.
2776 * @retval -EINVAL Work item is being processed or has completed its work.
2777 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002778 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002779 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002780extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2781 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002782 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002783
2784/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002785 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002786 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002787 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002788 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002789 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002790 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002791 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002792 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002793 * @param work Address of delayed work item.
2794 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002795 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002796 * @retval -EINPROGRESS Work item is already pending.
2797 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002798 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002799 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002800extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002801
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002802/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002803 * @brief Submit a work item to the system workqueue.
2804 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002805 * This routine submits work item @a work to be processed by the system
2806 * workqueue. If the work item is already pending in the workqueue's queue
2807 * as a result of an earlier submission, this routine has no effect on the
2808 * work item. If the work item has already been processed, or is currently
2809 * being processed, its work is considered complete and the work item can be
2810 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002811 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002812 * @warning
2813 * Work items submitted to the system workqueue should avoid using handlers
2814 * that block or yield since this may prevent the system workqueue from
2815 * processing other work items in a timely manner.
2816 *
2817 * @note Can be called by ISRs.
2818 *
2819 * @param work Address of work item.
2820 *
2821 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002822 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002823 */
2824static inline void k_work_submit(struct k_work *work)
2825{
2826 k_work_submit_to_queue(&k_sys_work_q, work);
2827}
2828
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002829/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002830 * @brief Submit a delayed work item to the system workqueue.
2831 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002832 * This routine schedules work item @a work to be processed by the system
2833 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002834 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002835 * Only when the countdown completes is the work item actually submitted to
2836 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002837 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002838 * Submitting a previously submitted delayed work item that is still
2839 * counting down cancels the existing submission and restarts the countdown
2840 * using the new delay. If the work item is currently pending on the
2841 * workqueue's queue because the countdown has completed it is too late to
2842 * resubmit the item, and resubmission fails without impacting the work item.
2843 * If the work item has already been processed, or is currently being processed,
2844 * its work is considered complete and the work item can be resubmitted.
2845 *
2846 * @warning
2847 * Work items submitted to the system workqueue should avoid using handlers
2848 * that block or yield since this may prevent the system workqueue from
2849 * processing other work items in a timely manner.
2850 *
2851 * @note Can be called by ISRs.
2852 *
2853 * @param work Address of delayed work item.
2854 * @param delay Delay before submitting the work item (in milliseconds).
2855 *
2856 * @retval 0 Work item countdown started.
2857 * @retval -EINPROGRESS Work item is already pending.
2858 * @retval -EINVAL Work item is being processed or has completed its work.
2859 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002860 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002861 */
2862static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002863 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002864{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002865 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002866}
2867
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002868/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002869 * @brief Get time remaining before a delayed work gets scheduled.
2870 *
2871 * This routine computes the (approximate) time remaining before a
2872 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002873 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002874 *
2875 * @param work Delayed work item.
2876 *
2877 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002878 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002879 */
Kumar Galacc334c72017-04-21 10:55:34 -05002880static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002881{
Andy Ross52e444b2018-09-28 09:06:37 -07002882 return __ticks_to_ms(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02002883}
2884
Anas Nashif166f5192018-02-25 08:02:36 -06002885/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002886/**
Anas Nashifce78d162018-05-24 12:43:11 -05002887 * @defgroup mutex_apis Mutex APIs
2888 * @ingroup kernel_apis
2889 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002890 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002891
Anas Nashifce78d162018-05-24 12:43:11 -05002892/**
2893 * Mutex Structure
2894 * @ingroup mutex_apis
2895 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002896struct k_mutex {
2897 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002898 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002899 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002900 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002901 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002902
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002903 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002904};
2905
Anas Nashifce78d162018-05-24 12:43:11 -05002906/**
2907 * @cond INTERNAL_HIDDEN
2908 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002909#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002910 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002911 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002912 .owner = NULL, \
2913 .lock_count = 0, \
2914 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002915 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002916 }
2917
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002918#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2919
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002920/**
Allan Stephensc98da842016-11-11 15:45:03 -05002921 * INTERNAL_HIDDEN @endcond
2922 */
2923
2924/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002925 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002927 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002928 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002929 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002930 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002931 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002932 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002933 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002934#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002935 struct k_mutex name \
2936 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002937 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002938
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002939/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002940 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002941 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002942 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002943 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002944 * Upon completion, the mutex is available and does not have an owner.
2945 *
2946 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002947 *
2948 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002949 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002950 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002951__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002952
2953/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002954 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002956 * This routine locks @a mutex. If the mutex is locked by another thread,
2957 * the calling thread waits until the mutex becomes available or until
2958 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002960 * A thread is permitted to lock a mutex it has already locked. The operation
2961 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002963 * @param mutex Address of the mutex.
2964 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002965 * or one of the special values K_NO_WAIT and K_FOREVER.
2966 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002967 * @retval 0 Mutex locked.
2968 * @retval -EBUSY Returned without waiting.
2969 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002970 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002971 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002972__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002973
2974/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002975 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002977 * This routine unlocks @a mutex. The mutex must already be locked by the
2978 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002979 *
2980 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002981 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002982 * thread.
2983 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002984 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002985 *
2986 * @return N/A
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 void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002990
Allan Stephensc98da842016-11-11 15:45:03 -05002991/**
Anas Nashif166f5192018-02-25 08:02:36 -06002992 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002993 */
2994
2995/**
2996 * @cond INTERNAL_HIDDEN
2997 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002998
2999struct k_sem {
3000 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05303001 u32_t count;
3002 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003003 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003004
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003005 _OBJECT_TRACING_NEXT_PTR(k_sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003006};
3007
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003008#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05003009 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003010 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05003011 .count = initial_count, \
3012 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003013 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05003014 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05003015 }
3016
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003017#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
3018
Allan Stephensc98da842016-11-11 15:45:03 -05003019/**
3020 * INTERNAL_HIDDEN @endcond
3021 */
3022
3023/**
3024 * @defgroup semaphore_apis Semaphore APIs
3025 * @ingroup kernel_apis
3026 * @{
3027 */
3028
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003029/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003030 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003032 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003033 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003034 * @param sem Address of the semaphore.
3035 * @param initial_count Initial semaphore count.
3036 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003037 *
3038 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003039 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003040 */
Andrew Boie99280232017-09-29 14:17:47 -07003041__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
3042 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003043
3044/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003045 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003046 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003047 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003048 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003049 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3050 *
3051 * @param sem Address of the semaphore.
3052 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003053 * or one of the special values K_NO_WAIT and K_FOREVER.
3054 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05003055 * @note When porting code from the nanokernel legacy API to the new API, be
3056 * careful with the return value of this function. The return value is the
3057 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
3058 * non-zero means failure, while the nano_sem_take family returns 1 for success
3059 * and 0 for failure.
3060 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003061 * @retval 0 Semaphore taken.
3062 * @retval -EBUSY Returned without waiting.
3063 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003064 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003065 */
Andrew Boie99280232017-09-29 14:17:47 -07003066__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003067
3068/**
3069 * @brief Give a semaphore.
3070 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003071 * This routine gives @a sem, unless the semaphore is already at its maximum
3072 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003074 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003076 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003077 *
3078 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003079 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003080 */
Andrew Boie99280232017-09-29 14:17:47 -07003081__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003082
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003083/**
3084 * @brief Reset a semaphore's count to zero.
3085 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003086 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003087 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003088 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003089 *
3090 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003091 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003092 */
Andrew Boie990bf162017-10-03 12:36:49 -07003093__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003094
Anas Nashif954d5502018-02-25 08:37:28 -06003095/**
3096 * @internal
3097 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003098static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003099{
3100 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003101}
3102
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003103/**
3104 * @brief Get a semaphore's count.
3105 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003106 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003107 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003108 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003110 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003111 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003112 */
Andrew Boie990bf162017-10-03 12:36:49 -07003113__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003114
Anas Nashif954d5502018-02-25 08:37:28 -06003115/**
3116 * @internal
3117 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003118static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003119{
3120 return sem->count;
3121}
3122
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003123/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003124 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003125 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003126 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003127 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003128 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003130 * @param name Name of the semaphore.
3131 * @param initial_count Initial semaphore count.
3132 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003133 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003134 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003135#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003136 struct k_sem name \
3137 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003138 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303139 BUILD_ASSERT(((count_limit) != 0) && \
3140 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003141
Anas Nashif166f5192018-02-25 08:02:36 -06003142/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003143
3144/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003145 * @defgroup msgq_apis Message Queue APIs
3146 * @ingroup kernel_apis
3147 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003148 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003149
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003150/**
3151 * @brief Message Queue Structure
3152 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003153struct k_msgq {
3154 _wait_q_t wait_q;
Andy Rossbe03dbd2018-07-26 10:23:02 -07003155 struct k_spinlock lock;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003156 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003157 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003158 char *buffer_start;
3159 char *buffer_end;
3160 char *read_ptr;
3161 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003162 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003163
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003164 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Andrew Boie0fe789f2018-04-12 18:35:56 -07003165 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003166};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003167/**
3168 * @cond INTERNAL_HIDDEN
3169 */
3170
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003171
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003172#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003173 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003174 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003175 .max_msgs = q_max_msgs, \
3176 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003177 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003178 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003179 .read_ptr = q_buffer, \
3180 .write_ptr = q_buffer, \
3181 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003182 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003183 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003184#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003185/**
3186 * INTERNAL_HIDDEN @endcond
3187 */
3188
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003189
Andrew Boie0fe789f2018-04-12 18:35:56 -07003190#define K_MSGQ_FLAG_ALLOC BIT(0)
3191
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003192/**
3193 * @brief Message Queue Attributes
3194 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303195struct k_msgq_attrs {
3196 size_t msg_size;
3197 u32_t max_msgs;
3198 u32_t used_msgs;
3199};
3200
Allan Stephensc98da842016-11-11 15:45:03 -05003201
3202/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003203 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003204 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003205 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3206 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003207 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3208 * message is similarly aligned to this boundary, @a q_msg_size must also be
3209 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003210 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003211 * The message queue can be accessed outside the module where it is defined
3212 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003213 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003214 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003215 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003216 * @param q_name Name of the message queue.
3217 * @param q_msg_size Message size (in bytes).
3218 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003219 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003220 *
3221 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003222 */
3223#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08003224 static char __noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003225 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003226 struct k_msgq q_name \
3227 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003228 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003229 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003230
Peter Mitsisd7a37502016-10-13 11:37:40 -04003231/**
3232 * @brief Initialize a message queue.
3233 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003234 * This routine initializes a message queue object, prior to its first use.
3235 *
Allan Stephensda827222016-11-09 14:23:58 -06003236 * The message queue's ring buffer must contain space for @a max_msgs messages,
3237 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3238 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3239 * that each message is similarly aligned to this boundary, @a q_msg_size
3240 * must also be a multiple of N.
3241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003242 * @param q Address of the message queue.
3243 * @param buffer Pointer to ring buffer that holds queued messages.
3244 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003245 * @param max_msgs Maximum number of messages that can be queued.
3246 *
3247 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003248 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003249 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003250void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3251 u32_t max_msgs);
3252
3253/**
3254 * @brief Initialize a message queue.
3255 *
3256 * This routine initializes a message queue object, prior to its first use,
3257 * allocating its internal ring buffer from the calling thread's resource
3258 * pool.
3259 *
3260 * Memory allocated for the ring buffer can be released by calling
3261 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3262 * all of its references.
3263 *
3264 * @param q Address of the message queue.
3265 * @param msg_size Message size (in bytes).
3266 * @param max_msgs Maximum number of messages that can be queued.
3267 *
3268 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3269 * thread's resource pool, or -EINVAL if the size parameters cause
3270 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003271 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003272 */
3273__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3274 u32_t max_msgs);
3275
3276
3277void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003278
3279/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003280 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003281 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003282 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003283 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003284 * @note Can be called by ISRs.
3285 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003286 * @param q Address of the message queue.
3287 * @param data Pointer to the message.
3288 * @param timeout Waiting period to add the message (in milliseconds),
3289 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003290 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003291 * @retval 0 Message sent.
3292 * @retval -ENOMSG Returned without waiting or queue purged.
3293 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003294 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003295 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003296__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003297
3298/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003299 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003300 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003301 * This routine receives a message from message queue @a q in a "first in,
3302 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003303 *
Allan Stephensc98da842016-11-11 15:45:03 -05003304 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003305 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003306 * @param q Address of the message queue.
3307 * @param data Address of area to hold the received message.
3308 * @param timeout Waiting period to receive the message (in milliseconds),
3309 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003310 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003311 * @retval 0 Message received.
3312 * @retval -ENOMSG Returned without waiting.
3313 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003314 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003315 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003316__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003317
3318/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003319 * @brief Peek/read a message from a message queue.
3320 *
3321 * This routine reads a message from message queue @a q in a "first in,
3322 * first out" manner and leaves the message in the queue.
3323 *
3324 * @note Can be called by ISRs.
3325 *
3326 * @param q Address of the message queue.
3327 * @param data Address of area to hold the message read from the queue.
3328 *
3329 * @retval 0 Message read.
3330 * @retval -ENOMSG Returned when the queue has no message.
3331 * @req K-MSGQ-002
3332 */
3333__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3334
3335/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003336 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003337 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003338 * This routine discards all unreceived messages in a message queue's ring
3339 * buffer. Any threads that are blocked waiting to send a message to the
3340 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003341 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003342 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003343 *
3344 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003345 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003346 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003347__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003348
Peter Mitsis67be2492016-10-07 11:44:34 -04003349/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003350 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003351 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003352 * This routine returns the number of unused entries in a message queue's
3353 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003354 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003355 * @param q Address of the message queue.
3356 *
3357 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003358 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003359 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003360__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3361
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303362/**
3363 * @brief Get basic attributes of a message queue.
3364 *
3365 * This routine fetches basic attributes of message queue into attr argument.
3366 *
3367 * @param q Address of the message queue.
3368 * @param attrs pointer to message queue attribute structure.
3369 *
3370 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003371 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303372 */
3373__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3374
3375
Andrew Boie82edb6e2017-10-02 10:53:06 -07003376static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003377{
3378 return q->max_msgs - q->used_msgs;
3379}
3380
Peter Mitsisd7a37502016-10-13 11:37:40 -04003381/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003382 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003383 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003384 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003386 * @param q Address of the message queue.
3387 *
3388 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003389 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003390 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003391__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3392
3393static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003394{
3395 return q->used_msgs;
3396}
3397
Anas Nashif166f5192018-02-25 08:02:36 -06003398/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003399
3400/**
3401 * @defgroup mem_pool_apis Memory Pool APIs
3402 * @ingroup kernel_apis
3403 * @{
3404 */
3405
Andy Ross73cb9582017-05-09 10:42:39 -07003406/* Note on sizing: the use of a 20 bit field for block means that,
3407 * assuming a reasonable minimum block size of 16 bytes, we're limited
3408 * to 16M of memory managed by a single pool. Long term it would be
3409 * good to move to a variable bit size based on configuration.
3410 */
3411struct k_mem_block_id {
3412 u32_t pool : 8;
3413 u32_t level : 4;
3414 u32_t block : 20;
3415};
3416
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003417struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003418 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003419 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003420};
3421
Anas Nashif166f5192018-02-25 08:02:36 -06003422/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003423
3424/**
3425 * @defgroup mailbox_apis Mailbox APIs
3426 * @ingroup kernel_apis
3427 * @{
3428 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003429
3430struct k_mbox_msg {
3431 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003432 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003433 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003434 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003435 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003436 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003437 /** sender's message data buffer */
3438 void *tx_data;
3439 /** internal use only - needed for legacy API support */
3440 void *_rx_data;
3441 /** message data block descriptor */
3442 struct k_mem_block tx_block;
3443 /** source thread id */
3444 k_tid_t rx_source_thread;
3445 /** target thread id */
3446 k_tid_t tx_target_thread;
3447 /** internal use only - thread waiting on send (may be a dummy) */
3448 k_tid_t _syncing_thread;
3449#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3450 /** internal use only - semaphore used during asynchronous send */
3451 struct k_sem *_async_sem;
3452#endif
3453};
3454
3455struct k_mbox {
3456 _wait_q_t tx_msg_queue;
3457 _wait_q_t rx_msg_queue;
Andy Ross9eeb6b82018-07-25 15:06:24 -07003458 struct k_spinlock lock;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003459
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003460 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003461};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003462/**
3463 * @cond INTERNAL_HIDDEN
3464 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003465
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003466#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003467 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003468 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3469 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003470 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003471 }
3472
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003473#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3474
Peter Mitsis12092702016-10-14 12:57:23 -04003475/**
Allan Stephensc98da842016-11-11 15:45:03 -05003476 * INTERNAL_HIDDEN @endcond
3477 */
3478
3479/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003480 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003482 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003483 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003484 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003485 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003486 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003487 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003488 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003489#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003490 struct k_mbox name \
3491 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003492 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003493
Peter Mitsis12092702016-10-14 12:57:23 -04003494/**
3495 * @brief Initialize a mailbox.
3496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003497 * This routine initializes a mailbox object, prior to its first use.
3498 *
3499 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003500 *
3501 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003502 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003503 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003504extern void k_mbox_init(struct k_mbox *mbox);
3505
Peter Mitsis12092702016-10-14 12:57:23 -04003506/**
3507 * @brief Send a mailbox message in a synchronous manner.
3508 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003509 * This routine sends a message to @a mbox and waits for a receiver to both
3510 * receive and process it. The message data may be in a buffer, in a memory
3511 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003512 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003513 * @param mbox Address of the mailbox.
3514 * @param tx_msg Address of the transmit message descriptor.
3515 * @param timeout Waiting period for the message to be received (in
3516 * milliseconds), or one of the special values K_NO_WAIT
3517 * and K_FOREVER. Once the message has been received,
3518 * this routine waits as long as necessary for the message
3519 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003520 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003521 * @retval 0 Message sent.
3522 * @retval -ENOMSG Returned without waiting.
3523 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003524 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003525 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003526extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003527 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003528
Peter Mitsis12092702016-10-14 12:57:23 -04003529/**
3530 * @brief Send a mailbox message in an asynchronous manner.
3531 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003532 * This routine sends a message to @a mbox without waiting for a receiver
3533 * to process it. The message data may be in a buffer, in a memory pool block,
3534 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3535 * will be given when the message has been both received and completely
3536 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003537 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003538 * @param mbox Address of the mailbox.
3539 * @param tx_msg Address of the transmit message descriptor.
3540 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003541 *
3542 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003543 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003544 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003545extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003546 struct k_sem *sem);
3547
Peter Mitsis12092702016-10-14 12:57:23 -04003548/**
3549 * @brief Receive a mailbox message.
3550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003551 * This routine receives a message from @a mbox, then optionally retrieves
3552 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003554 * @param mbox Address of the mailbox.
3555 * @param rx_msg Address of the receive message descriptor.
3556 * @param buffer Address of the buffer to receive data, or NULL to defer data
3557 * retrieval and message disposal until later.
3558 * @param timeout Waiting period for a message to be received (in
3559 * milliseconds), or one of the special values K_NO_WAIT
3560 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003561 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003562 * @retval 0 Message received.
3563 * @retval -ENOMSG Returned without waiting.
3564 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003565 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003566 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003567extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003568 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003569
3570/**
3571 * @brief Retrieve mailbox message data into a buffer.
3572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003573 * This routine completes the processing of a received message by retrieving
3574 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003575 *
3576 * Alternatively, this routine can be used to dispose of a received message
3577 * without retrieving its data.
3578 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003579 * @param rx_msg Address of the receive message descriptor.
3580 * @param buffer Address of the buffer to receive data, or NULL to discard
3581 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003582 *
3583 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003584 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003585 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003586extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003587
3588/**
3589 * @brief Retrieve mailbox message data into a memory pool block.
3590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003591 * This routine completes the processing of a received message by retrieving
3592 * its data into a memory pool block, then disposing of the message.
3593 * The memory pool block that results from successful retrieval must be
3594 * returned to the pool once the data has been processed, even in cases
3595 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003596 *
3597 * Alternatively, this routine can be used to dispose of a received message
3598 * without retrieving its data. In this case there is no need to return a
3599 * memory pool block to the pool.
3600 *
3601 * This routine allocates a new memory pool block for the data only if the
3602 * data is not already in one. If a new block cannot be allocated, the routine
3603 * returns a failure code and the received message is left unchanged. This
3604 * permits the caller to reattempt data retrieval at a later time or to dispose
3605 * of the received message without retrieving its data.
3606 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003607 * @param rx_msg Address of a receive message descriptor.
3608 * @param pool Address of memory pool, or NULL to discard data.
3609 * @param block Address of the area to hold memory pool block info.
3610 * @param timeout Waiting period to wait for a memory pool block (in
3611 * milliseconds), or one of the special values K_NO_WAIT
3612 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003613 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003614 * @retval 0 Data retrieved.
3615 * @retval -ENOMEM Returned without waiting.
3616 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003617 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003618 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003619extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003620 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003621 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003622
Anas Nashif166f5192018-02-25 08:02:36 -06003623/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003624
3625/**
Anas Nashifce78d162018-05-24 12:43:11 -05003626 * @defgroup pipe_apis Pipe APIs
3627 * @ingroup kernel_apis
3628 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003629 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003630
Anas Nashifce78d162018-05-24 12:43:11 -05003631/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003632struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003633 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3634 size_t size; /**< Buffer size */
3635 size_t bytes_used; /**< # bytes used in buffer */
3636 size_t read_index; /**< Where in buffer to read from */
3637 size_t write_index; /**< Where in buffer to write */
Andy Rossf582b552019-02-05 16:10:18 -08003638 struct k_spinlock lock; /**< Synchronization lock */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003639
3640 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003641 _wait_q_t readers; /**< Reader wait queue */
3642 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003643 } wait_q;
3644
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003645 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Anas Nashifce78d162018-05-24 12:43:11 -05003646 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003647};
3648
Anas Nashifce78d162018-05-24 12:43:11 -05003649/**
3650 * @cond INTERNAL_HIDDEN
3651 */
3652#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3653
Krzysztof Chruscinskibe063272019-02-13 11:19:54 +01003654#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
3655 { \
3656 .buffer = pipe_buffer, \
3657 .size = pipe_buffer_size, \
3658 .bytes_used = 0, \
3659 .read_index = 0, \
3660 .write_index = 0, \
3661 .lock = {}, \
3662 .wait_q = { \
3663 .readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
3664 .writers = _WAIT_Q_INIT(&obj.wait_q.writers) \
3665 }, \
3666 _OBJECT_TRACING_INIT \
3667 .flags = 0 \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003668 }
3669
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003670#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3671
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003672/**
Allan Stephensc98da842016-11-11 15:45:03 -05003673 * INTERNAL_HIDDEN @endcond
3674 */
3675
3676/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003677 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003678 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003679 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003680 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003681 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003682 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003683 * @param name Name of the pipe.
3684 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3685 * or zero if no ring buffer is used.
3686 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003687 *
3688 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003689 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003690#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
Andrew Boie41f60112019-01-31 15:53:24 -08003691 static unsigned char __noinit __aligned(pipe_align) \
Andrew Boie44fe8122018-04-12 17:38:12 -07003692 _k_pipe_buf_##name[pipe_buffer_size]; \
3693 struct k_pipe name \
3694 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003695 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003696
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003697/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003698 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003699 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003700 * This routine initializes a pipe object, prior to its first use.
3701 *
3702 * @param pipe Address of the pipe.
3703 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3704 * is used.
3705 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3706 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003707 *
3708 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003709 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003710 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003711void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3712
3713/**
3714 * @brief Release a pipe's allocated buffer
3715 *
3716 * If a pipe object was given a dynamically allocated buffer via
3717 * k_pipe_alloc_init(), this will free it. This function does nothing
3718 * if the buffer wasn't dynamically allocated.
3719 *
3720 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003721 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003722 */
3723void k_pipe_cleanup(struct k_pipe *pipe);
3724
3725/**
3726 * @brief Initialize a pipe and allocate a buffer for it
3727 *
3728 * Storage for the buffer region will be allocated from the calling thread's
3729 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3730 * or userspace is enabled and the pipe object loses all references to it.
3731 *
3732 * This function should only be called on uninitialized pipe objects.
3733 *
3734 * @param pipe Address of the pipe.
3735 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3736 * buffer is used.
3737 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003738 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003739 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003740 */
3741__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003742
3743/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003744 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003745 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003746 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003747 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003748 * @param pipe Address of the pipe.
3749 * @param data Address of data to write.
3750 * @param bytes_to_write Size of data (in bytes).
3751 * @param bytes_written Address of area to hold the number of bytes written.
3752 * @param min_xfer Minimum number of bytes to write.
3753 * @param timeout Waiting period to wait for the data to be written (in
3754 * milliseconds), or one of the special values K_NO_WAIT
3755 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003756 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003757 * @retval 0 At least @a min_xfer bytes of data were written.
3758 * @retval -EIO Returned without waiting; zero data bytes were written.
3759 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003760 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003761 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003762 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003763__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3764 size_t bytes_to_write, size_t *bytes_written,
3765 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003766
3767/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003768 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003769 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003770 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003771 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003772 * @param pipe Address of the pipe.
3773 * @param data Address to place the data read from pipe.
3774 * @param bytes_to_read Maximum number of data bytes to read.
3775 * @param bytes_read Address of area to hold the number of bytes read.
3776 * @param min_xfer Minimum number of data bytes to read.
3777 * @param timeout Waiting period to wait for the data to be read (in
3778 * milliseconds), or one of the special values K_NO_WAIT
3779 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003780 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003781 * @retval 0 At least @a min_xfer bytes of data were read.
3782 * @retval -EIO Returned without waiting; zero data bytes were read.
3783 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003784 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003785 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003786 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003787__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3788 size_t bytes_to_read, size_t *bytes_read,
3789 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003790
3791/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003792 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003793 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003794 * This routine writes the data contained in a memory block to @a pipe.
3795 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003796 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003798 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003799 * @param block Memory block containing data to send
3800 * @param size Number of data bytes in memory block to send
3801 * @param sem Semaphore to signal upon completion (else NULL)
3802 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003803 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003804 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003805 */
3806extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3807 size_t size, struct k_sem *sem);
3808
Anas Nashif166f5192018-02-25 08:02:36 -06003809/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003810
Allan Stephensc98da842016-11-11 15:45:03 -05003811/**
3812 * @cond INTERNAL_HIDDEN
3813 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003814
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003815struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003816 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003817 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003818 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003819 char *buffer;
3820 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003821 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003822
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003823 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003824};
3825
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003826#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003827 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003828 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003829 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003830 .num_blocks = slab_num_blocks, \
3831 .block_size = slab_block_size, \
3832 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003833 .free_list = NULL, \
3834 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003835 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003836 }
3837
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003838#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3839
3840
Peter Mitsis578f9112016-10-07 13:50:31 -04003841/**
Allan Stephensc98da842016-11-11 15:45:03 -05003842 * INTERNAL_HIDDEN @endcond
3843 */
3844
3845/**
3846 * @defgroup mem_slab_apis Memory Slab APIs
3847 * @ingroup kernel_apis
3848 * @{
3849 */
3850
3851/**
Allan Stephensda827222016-11-09 14:23:58 -06003852 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003853 *
Allan Stephensda827222016-11-09 14:23:58 -06003854 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003855 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003856 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3857 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003858 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003859 *
Allan Stephensda827222016-11-09 14:23:58 -06003860 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003861 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003862 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003863 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003865 * @param name Name of the memory slab.
3866 * @param slab_block_size Size of each memory block (in bytes).
3867 * @param slab_num_blocks Number memory blocks.
3868 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003869 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003870 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003871#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3872 char __noinit __aligned(slab_align) \
3873 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3874 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003875 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003876 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003877 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003878
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003879/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003880 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003882 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003883 *
Allan Stephensda827222016-11-09 14:23:58 -06003884 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3885 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3886 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3887 * To ensure that each memory block is similarly aligned to this boundary,
3888 * @a slab_block_size must also be a multiple of N.
3889 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003890 * @param slab Address of the memory slab.
3891 * @param buffer Pointer to buffer used for the memory blocks.
3892 * @param block_size Size of each memory block (in bytes).
3893 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003894 *
3895 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003896 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003897 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003898extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003899 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003900
3901/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003902 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003903 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003904 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003905 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003906 * @param slab Address of the memory slab.
3907 * @param mem Pointer to block address area.
3908 * @param timeout Maximum time to wait for operation to complete
3909 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3910 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003911 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003912 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003913 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003914 * @retval -ENOMEM Returned without waiting.
3915 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003916 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003917 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003918extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003919 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003920
3921/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003922 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003923 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003924 * This routine releases a previously allocated memory block back to its
3925 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003927 * @param slab Address of the memory slab.
3928 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003929 *
3930 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003931 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003932 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003933extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003934
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003935/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003936 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003937 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003938 * This routine gets the number of memory blocks that are currently
3939 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003940 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003941 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003943 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003944 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003945 */
Kumar Galacc334c72017-04-21 10:55:34 -05003946static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003947{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003948 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003949}
3950
Peter Mitsisc001aa82016-10-13 13:53:37 -04003951/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003952 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003954 * This routine gets the number of memory blocks that are currently
3955 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003957 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003958 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003959 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003960 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04003961 */
Kumar Galacc334c72017-04-21 10:55:34 -05003962static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003963{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003964 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003965}
3966
Anas Nashif166f5192018-02-25 08:02:36 -06003967/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003968
3969/**
3970 * @cond INTERNAL_HIDDEN
3971 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003972
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003973struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003974 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003975 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003976};
3977
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003978/**
Allan Stephensc98da842016-11-11 15:45:03 -05003979 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003980 */
3981
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003982/**
Allan Stephensc98da842016-11-11 15:45:03 -05003983 * @addtogroup mem_pool_apis
3984 * @{
3985 */
3986
3987/**
3988 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003989 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003990 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3991 * long. The memory pool allows blocks to be repeatedly partitioned into
3992 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003993 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003994 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003995 * If the pool is to be accessed outside the module where it is defined, it
3996 * can be declared via
3997 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003998 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003999 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004000 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004001 * @param minsz Size of the smallest blocks in the pool (in bytes).
4002 * @param maxsz Size of the largest blocks in the pool (in bytes).
4003 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004004 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004005 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004006 */
Andy Ross73cb9582017-05-09 10:42:39 -07004007#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4008 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4009 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08004010 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07004011 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004012 .base = { \
4013 .buf = _mpool_buf_##name, \
4014 .max_sz = maxsz, \
4015 .n_max = nmax, \
4016 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
4017 .levels = _mpool_lvls_##name, \
4018 .flags = SYS_MEM_POOL_KERNEL \
4019 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004020 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004021
Peter Mitsis937042c2016-10-13 13:18:26 -04004022/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004023 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004025 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004026 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004027 * @param pool Address of the memory pool.
4028 * @param block Pointer to block descriptor for the allocated memory.
4029 * @param size Amount of memory to allocate (in bytes).
4030 * @param timeout Maximum time to wait for operation to complete
4031 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4032 * or K_FOREVER to wait as long as necessary.
4033 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004034 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004035 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004036 * @retval -ENOMEM Returned without waiting.
4037 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004038 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004039 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004040extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004041 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004042
4043/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004044 * @brief Allocate memory from a memory pool with malloc() semantics
4045 *
4046 * Such memory must be released using k_free().
4047 *
4048 * @param pool Address of the memory pool.
4049 * @param size Amount of memory to allocate (in bytes).
4050 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004051 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004052 */
4053extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4054
4055/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004056 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004058 * This routine releases a previously allocated memory block back to its
4059 * memory pool.
4060 *
4061 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004062 *
4063 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004064 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004065 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004066extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004067
4068/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004069 * @brief Free memory allocated from a memory pool.
4070 *
4071 * This routine releases a previously allocated memory block back to its
4072 * memory pool
4073 *
4074 * @param id Memory block identifier.
4075 *
4076 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004077 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004078 */
4079extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4080
4081/**
Anas Nashif166f5192018-02-25 08:02:36 -06004082 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004083 */
4084
4085/**
4086 * @defgroup heap_apis Heap Memory Pool APIs
4087 * @ingroup kernel_apis
4088 * @{
4089 */
4090
4091/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004092 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004094 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004095 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004096 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004097 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004098 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004099 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004100 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004101 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004102extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004103
4104/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004105 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004106 *
4107 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004108 * returned must have been allocated from the heap memory pool or
4109 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004110 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004111 * If @a ptr is NULL, no operation is performed.
4112 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004113 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004114 *
4115 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004116 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004117 */
4118extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004119
Allan Stephensc98da842016-11-11 15:45:03 -05004120/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004121 * @brief Allocate memory from heap, array style
4122 *
4123 * This routine provides traditional calloc() semantics. Memory is
4124 * allocated from the heap memory pool and zeroed.
4125 *
4126 * @param nmemb Number of elements in the requested array
4127 * @param size Size of each array element (in bytes).
4128 *
4129 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004130 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004131 */
4132extern void *k_calloc(size_t nmemb, size_t size);
4133
Anas Nashif166f5192018-02-25 08:02:36 -06004134/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004135
Benjamin Walshacc68c12017-01-29 18:57:45 -05004136/* polling API - PRIVATE */
4137
Benjamin Walshb0179862017-02-02 16:39:57 -05004138#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004139#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004140#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004141#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004142#endif
4143
Benjamin Walshacc68c12017-01-29 18:57:45 -05004144/* private - implementation data created as needed, per-type */
4145struct _poller {
4146 struct k_thread *thread;
Flavio Ceolin76b35182018-12-16 12:48:29 -08004147 volatile bool is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004148};
4149
4150/* private - types bit positions */
4151enum _poll_types_bits {
4152 /* can be used to ignore an event */
4153 _POLL_TYPE_IGNORE,
4154
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004155 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004156 _POLL_TYPE_SIGNAL,
4157
4158 /* semaphore availability */
4159 _POLL_TYPE_SEM_AVAILABLE,
4160
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004161 /* queue/fifo/lifo data availability */
4162 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004163
4164 _POLL_NUM_TYPES
4165};
4166
4167#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4168
4169/* private - states bit positions */
4170enum _poll_states_bits {
4171 /* default state when creating event */
4172 _POLL_STATE_NOT_READY,
4173
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004174 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004175 _POLL_STATE_SIGNALED,
4176
4177 /* semaphore is available */
4178 _POLL_STATE_SEM_AVAILABLE,
4179
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004180 /* data is available to read on queue/fifo/lifo */
4181 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004182
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004183 /* queue/fifo/lifo wait was cancelled */
4184 _POLL_STATE_CANCELLED,
4185
Benjamin Walshacc68c12017-01-29 18:57:45 -05004186 _POLL_NUM_STATES
4187};
4188
4189#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4190
4191#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004192 (32 - (0 \
4193 + 8 /* tag */ \
4194 + _POLL_NUM_TYPES \
4195 + _POLL_NUM_STATES \
4196 + 1 /* modes */ \
4197 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004198
Benjamin Walshacc68c12017-01-29 18:57:45 -05004199/* end of polling API - PRIVATE */
4200
4201
4202/**
4203 * @defgroup poll_apis Async polling APIs
4204 * @ingroup kernel_apis
4205 * @{
4206 */
4207
4208/* Public polling API */
4209
4210/* public - values for k_poll_event.type bitfield */
4211#define K_POLL_TYPE_IGNORE 0
4212#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4213#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004214#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4215#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004216
4217/* public - polling modes */
4218enum k_poll_modes {
4219 /* polling thread does not take ownership of objects when available */
4220 K_POLL_MODE_NOTIFY_ONLY = 0,
4221
4222 K_POLL_NUM_MODES
4223};
4224
4225/* public - values for k_poll_event.state bitfield */
4226#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004227#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4228#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004229#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4230#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004231#define K_POLL_STATE_CANCELLED _POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004232
4233/* public - poll signal object */
4234struct k_poll_signal {
4235 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004236 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004237
4238 /*
4239 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4240 * user resets it to 0.
4241 */
4242 unsigned int signaled;
4243
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004244 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004245 int result;
4246};
4247
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004248#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004249 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004250 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004251 .signaled = 0, \
4252 .result = 0, \
4253 }
4254
4255struct k_poll_event {
4256 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004257 sys_dnode_t _node;
4258
4259 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004260 struct _poller *poller;
4261
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004262 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004263 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004264
Benjamin Walshacc68c12017-01-29 18:57:45 -05004265 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004266 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004267
4268 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004269 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004270
4271 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004272 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004273
4274 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004275 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004276
4277 /* per-type data */
4278 union {
4279 void *obj;
4280 struct k_poll_signal *signal;
4281 struct k_sem *sem;
4282 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004283 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004284 };
4285};
4286
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004287#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004288 { \
4289 .poller = NULL, \
4290 .type = event_type, \
4291 .state = K_POLL_STATE_NOT_READY, \
4292 .mode = event_mode, \
4293 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004294 { .obj = event_obj }, \
4295 }
4296
4297#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4298 event_tag) \
4299 { \
4300 .type = event_type, \
4301 .tag = event_tag, \
4302 .state = K_POLL_STATE_NOT_READY, \
4303 .mode = event_mode, \
4304 .unused = 0, \
4305 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004306 }
4307
4308/**
4309 * @brief Initialize one struct k_poll_event instance
4310 *
4311 * After this routine is called on a poll event, the event it ready to be
4312 * placed in an event array to be passed to k_poll().
4313 *
4314 * @param event The event to initialize.
4315 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4316 * values. Only values that apply to the same object being polled
4317 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4318 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004319 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004320 * @param obj Kernel object or poll signal.
4321 *
4322 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004323 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004324 */
4325
Kumar Galacc334c72017-04-21 10:55:34 -05004326extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004327 int mode, void *obj);
4328
4329/**
4330 * @brief Wait for one or many of multiple poll events to occur
4331 *
4332 * This routine allows a thread to wait concurrently for one or many of
4333 * multiple poll events to have occurred. Such events can be a kernel object
4334 * being available, like a semaphore, or a poll signal event.
4335 *
4336 * When an event notifies that a kernel object is available, the kernel object
4337 * is not "given" to the thread calling k_poll(): it merely signals the fact
4338 * that the object was available when the k_poll() call was in effect. Also,
4339 * all threads trying to acquire an object the regular way, i.e. by pending on
4340 * the object, have precedence over the thread polling on the object. This
4341 * means that the polling thread will never get the poll event on an object
4342 * until the object becomes available and its pend queue is empty. For this
4343 * reason, the k_poll() call is more effective when the objects being polled
4344 * only have one thread, the polling thread, trying to acquire them.
4345 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004346 * When k_poll() returns 0, the caller should loop on all the events that were
4347 * passed to k_poll() and check the state field for the values that were
4348 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004349 *
4350 * Before being reused for another call to k_poll(), the user has to reset the
4351 * state field to K_POLL_STATE_NOT_READY.
4352 *
Andrew Boie3772f772018-05-07 16:52:57 -07004353 * When called from user mode, a temporary memory allocation is required from
4354 * the caller's resource pool.
4355 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004356 * @param events An array of pointers to events to be polled for.
4357 * @param num_events The number of events in the array.
4358 * @param timeout Waiting period for an event to be ready (in milliseconds),
4359 * or one of the special values K_NO_WAIT and K_FOREVER.
4360 *
4361 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004362 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004363 * @retval -EINTR Polling has been interrupted, e.g. with
4364 * k_queue_cancel_wait(). All output events are still set and valid,
4365 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4366 * words, -EINTR status means that at least one of output events is
4367 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004368 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4369 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004370 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004371 */
4372
Andrew Boie3772f772018-05-07 16:52:57 -07004373__syscall int k_poll(struct k_poll_event *events, int num_events,
4374 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004375
4376/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004377 * @brief Initialize a poll signal object.
4378 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004379 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004380 *
4381 * @param signal A poll signal.
4382 *
4383 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004384 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004385 */
4386
Andrew Boie3772f772018-05-07 16:52:57 -07004387__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4388
4389/*
4390 * @brief Reset a poll signal object's state to unsignaled.
4391 *
4392 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004393 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004394 */
4395__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4396
4397static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4398{
4399 signal->signaled = 0;
4400}
4401
4402/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004403 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004404 *
4405 * @param signal A poll signal object
4406 * @param signaled An integer buffer which will be written nonzero if the
4407 * object was signaled
4408 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004409 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004410 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004411 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004412 */
4413__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4414 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004415
4416/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004417 * @brief Signal a poll signal object.
4418 *
4419 * This routine makes ready a poll signal, which is basically a poll event of
4420 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4421 * made ready to run. A @a result value can be specified.
4422 *
4423 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004424 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004425 * k_poll_signal_reset(). It thus has to be reset by the user before being
4426 * passed again to k_poll() or k_poll() will consider it being signaled, and
4427 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004428 *
4429 * @param signal A poll signal.
4430 * @param result The value to store in the result field of the signal.
4431 *
4432 * @retval 0 The signal was delivered successfully.
4433 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004434 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004435 */
4436
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004437__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004438
Anas Nashif954d5502018-02-25 08:37:28 -06004439/**
4440 * @internal
4441 */
Andy Ross8606fab2018-03-26 10:54:40 -07004442extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004443
Anas Nashif166f5192018-02-25 08:02:36 -06004444/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004445
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004446/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004447 * @defgroup cpu_idle_apis CPU Idling APIs
4448 * @ingroup kernel_apis
4449 * @{
4450 */
4451
4452/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004453 * @brief Make the CPU idle.
4454 *
4455 * This function makes the CPU idle until an event wakes it up.
4456 *
4457 * In a regular system, the idle thread should be the only thread responsible
4458 * for making the CPU idle and triggering any type of power management.
4459 * However, in some more constrained systems, such as a single-threaded system,
4460 * the only thread would be responsible for this if needed.
4461 *
4462 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004463 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004464 */
4465extern void k_cpu_idle(void);
4466
4467/**
4468 * @brief Make the CPU idle in an atomic fashion.
4469 *
4470 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4471 * must be done atomically before making the CPU idle.
4472 *
4473 * @param key Interrupt locking key obtained from irq_lock().
4474 *
4475 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004476 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004477 */
4478extern void k_cpu_atomic_idle(unsigned int key);
4479
Anas Nashif30c3cff2019-01-22 08:18:13 -05004480/**
4481 * @}
4482 */
Anas Nashif954d5502018-02-25 08:37:28 -06004483
4484/**
4485 * @internal
4486 */
Kumar Galacc334c72017-04-21 10:55:34 -05004487extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004488
Andrew Boiecdb94d62017-04-18 15:22:05 -07004489#ifdef _ARCH_EXCEPT
4490/* This archtecture has direct support for triggering a CPU exception */
4491#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4492#else
4493
Andrew Boiecdb94d62017-04-18 15:22:05 -07004494/* NOTE: This is the implementation for arches that do not implement
4495 * _ARCH_EXCEPT() to generate a real CPU exception.
4496 *
4497 * We won't have a real exception frame to determine the PC value when
4498 * the oops occurred, so print file and line number before we jump into
4499 * the fatal error handler.
4500 */
4501#define _k_except_reason(reason) do { \
4502 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4503 _NanoFatalErrorHandler(reason, &_default_esf); \
4504 CODE_UNREACHABLE; \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004505 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004506
4507#endif /* _ARCH__EXCEPT */
4508
4509/**
4510 * @brief Fatally terminate a thread
4511 *
4512 * This should be called when a thread has encountered an unrecoverable
4513 * runtime condition and needs to terminate. What this ultimately
4514 * means is determined by the _fatal_error_handler() implementation, which
4515 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4516 *
4517 * If this is called from ISR context, the default system fatal error handler
4518 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004519 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004520 */
4521#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4522
4523/**
4524 * @brief Fatally terminate the system
4525 *
4526 * This should be called when the Zephyr kernel has encountered an
4527 * unrecoverable runtime condition and needs to terminate. What this ultimately
4528 * means is determined by the _fatal_error_handler() implementation, which
4529 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004530 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004531 */
4532#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4533
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004534/*
4535 * private APIs that are utilized by one or more public APIs
4536 */
4537
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004538#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004539/**
4540 * @internal
4541 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004542extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004543#else
Anas Nashif954d5502018-02-25 08:37:28 -06004544/**
4545 * @internal
4546 */
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004547#define _init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004548#endif
4549
Anas Nashif954d5502018-02-25 08:37:28 -06004550/**
4551 * @internal
4552 */
Flavio Ceolin09e362e2018-12-17 12:34:05 -08004553extern bool _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004554/**
4555 * @internal
4556 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004557extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004558
Andrew Boiedc5d9352017-06-02 12:56:47 -07004559/* arch/cpu.h may declare an architecture or platform-specific macro
4560 * for properly declaring stacks, compatible with MMU/MPU constraints if
4561 * enabled
4562 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004563
4564/**
4565 * @brief Obtain an extern reference to a stack
4566 *
4567 * This macro properly brings the symbol of a thread stack declared
4568 * elsewhere into scope.
4569 *
4570 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004571 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004572 */
4573#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4574
Andrew Boiedc5d9352017-06-02 12:56:47 -07004575#ifdef _ARCH_THREAD_STACK_DEFINE
4576#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4577#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4578 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304579#define K_THREAD_STACK_LEN(size) _ARCH_THREAD_STACK_LEN(size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004580#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4581#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004582static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004583{
4584 return _ARCH_THREAD_STACK_BUFFER(sym);
4585}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004586#else
4587/**
4588 * @brief Declare a toplevel thread stack memory region
4589 *
4590 * This declares a region of memory suitable for use as a thread's stack.
4591 *
4592 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4593 * 'noinit' section so that it isn't zeroed at boot
4594 *
Andrew Boie507852a2017-07-25 18:47:07 -07004595 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004596 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004597 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004598 *
4599 * It is legal to precede this definition with the 'static' keyword.
4600 *
4601 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4602 * parameter of k_thread_create(), it may not be the same as the
4603 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4604 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004605 * Some arches may round the size of the usable stack region up to satisfy
4606 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4607 * size.
4608 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004609 * @param sym Thread stack symbol name
4610 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004611 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004612 */
4613#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004614 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004615
4616/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304617 * @brief Calculate size of stacks to be allocated in a stack array
4618 *
4619 * This macro calculates the size to be allocated for the stacks
4620 * inside a stack array. It accepts the indicated "size" as a parameter
4621 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4622 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4623 *
4624 * @param size Size of the stack memory region
4625 * @req K-TSTACK-001
4626 */
4627#define K_THREAD_STACK_LEN(size) (size)
4628
4629/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004630 * @brief Declare a toplevel array of thread stack memory regions
4631 *
4632 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4633 * definition for additional details and constraints.
4634 *
4635 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4636 * 'noinit' section so that it isn't zeroed at boot
4637 *
4638 * @param sym Thread stack symbol name
4639 * @param nmemb Number of stacks to declare
4640 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004641 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004642 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004643#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004644 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304645 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004646
4647/**
4648 * @brief Declare an embedded stack memory region
4649 *
4650 * Used for stacks embedded within other data structures. Use is highly
4651 * discouraged but in some cases necessary. For memory protection scenarios,
4652 * it is very important that any RAM preceding this member not be writable
4653 * by threads else a stack overflow will lead to silent corruption. In other
4654 * words, the containing data structure should live in RAM owned by the kernel.
4655 *
4656 * @param sym Thread stack symbol name
4657 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004658 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004659 */
4660#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004661 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004662
4663/**
4664 * @brief Return the size in bytes of a stack memory region
4665 *
4666 * Convenience macro for passing the desired stack size to k_thread_create()
4667 * since the underlying implementation may actually create something larger
4668 * (for instance a guard area).
4669 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004670 * The value returned here is not guaranteed to match the 'size' parameter
4671 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004672 *
4673 * @param sym Stack memory symbol
4674 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004675 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004676 */
4677#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4678
4679/**
4680 * @brief Get a pointer to the physical stack buffer
4681 *
4682 * Convenience macro to get at the real underlying stack buffer used by
4683 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4684 * This is really only intended for diagnostic tools which want to examine
4685 * stack memory contents.
4686 *
4687 * @param sym Declared stack symbol name
4688 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004689 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004690 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004691static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004692{
4693 return (char *)sym;
4694}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004695
4696#endif /* _ARCH_DECLARE_STACK */
4697
Chunlin Hane9c97022017-07-07 20:29:30 +08004698/**
4699 * @defgroup mem_domain_apis Memory domain APIs
4700 * @ingroup kernel_apis
4701 * @{
4702 */
4703
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004704/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004705 * @def K_MEM_PARTITION_DEFINE
4706 * @brief Used to declare a memory partition
4707 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004708 */
4709#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4710#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4711 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Andrew Boie41f60112019-01-31 15:53:24 -08004712 struct k_mem_partition name =\
Ioannis Glaropoulos293247e2018-12-03 18:32:32 +01004713 { (u32_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004714#else
4715#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Andrew Boie41f60112019-01-31 15:53:24 -08004716 struct k_mem_partition name =\
Ioannis Glaropoulos293247e2018-12-03 18:32:32 +01004717 { (u32_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004718#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4719
Chunlin Hane9c97022017-07-07 20:29:30 +08004720/* memory partition */
4721struct k_mem_partition {
4722 /* start address of memory partition */
4723 u32_t start;
4724 /* size of memory partition */
4725 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004726#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08004727 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304728 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004729#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08004730};
4731
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02004732/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304733 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004734struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304735#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004736 /* partitions in the domain */
4737 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304738#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004739 /* domain q */
4740 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004741 /* number of partitions in the domain */
4742 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004743};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304744
Chunlin Hane9c97022017-07-07 20:29:30 +08004745
4746/**
4747 * @brief Initialize a memory domain.
4748 *
4749 * Initialize a memory domain with given name and memory partitions.
4750 *
4751 * @param domain The memory domain to be initialized.
4752 * @param num_parts The number of array items of "parts" parameter.
4753 * @param parts An array of pointers to the memory partitions. Can be NULL
4754 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004755 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004756 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004757extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004758 struct k_mem_partition *parts[]);
4759/**
4760 * @brief Destroy a memory domain.
4761 *
4762 * Destroy a memory domain.
4763 *
4764 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004765 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004766 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004767extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4768
4769/**
4770 * @brief Add a memory partition into a memory domain.
4771 *
4772 * Add a memory partition into a memory domain.
4773 *
4774 * @param domain The memory domain to be added a memory partition.
4775 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004776 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004777 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004778extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4779 struct k_mem_partition *part);
4780
4781/**
4782 * @brief Remove a memory partition from a memory domain.
4783 *
4784 * Remove a memory partition from a memory domain.
4785 *
4786 * @param domain The memory domain to be removed a memory partition.
4787 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004788 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004789 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004790extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4791 struct k_mem_partition *part);
4792
4793/**
4794 * @brief Add a thread into a memory domain.
4795 *
4796 * Add a thread into a memory domain.
4797 *
4798 * @param domain The memory domain that the thread is going to be added into.
4799 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004800 *
4801 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004802 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004803extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4804 k_tid_t thread);
4805
4806/**
4807 * @brief Remove a thread from its memory domain.
4808 *
4809 * Remove a thread from its memory domain.
4810 *
4811 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004812 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004813 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004814extern void k_mem_domain_remove_thread(k_tid_t thread);
4815
Anas Nashif166f5192018-02-25 08:02:36 -06004816/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004817
Andrew Boie756f9072017-10-10 16:01:49 -07004818/**
4819 * @brief Emit a character buffer to the console device
4820 *
4821 * @param c String of characters to print
4822 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004823 *
4824 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004825 */
4826__syscall void k_str_out(char *c, size_t n);
4827
Andy Rosse7172672018-01-24 15:48:32 -08004828/**
4829 * @brief Start a numbered CPU on a MP-capable system
4830
4831 * This starts and initializes a specific CPU. The main thread on
4832 * startup is running on CPU zero, other processors are numbered
4833 * sequentially. On return from this function, the CPU is known to
4834 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004835 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004836 * with the provided key will work to enable them.
4837 *
4838 * Normally, in SMP mode this function will be called by the kernel
4839 * initialization and should not be used as a user API. But it is
4840 * defined here for special-purpose apps which want Zephyr running on
4841 * one core and to use others for design-specific processing.
4842 *
4843 * @param cpu_num Integer number of the CPU
4844 * @param stack Stack memory for the CPU
4845 * @param sz Stack buffer size, in bytes
4846 * @param fn Function to begin running on the CPU. First argument is
4847 * an irq_unlock() key.
4848 * @param arg Untyped argument to be passed to "fn"
4849 */
4850extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08004851 void (*fn)(int key, void *data), void *arg);
Andy Rosse7172672018-01-24 15:48:32 -08004852
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004853#ifdef __cplusplus
4854}
4855#endif
4856
Anas Nashifb6304e62018-07-04 08:03:03 -05004857#include <tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07004858#include <syscalls/kernel.h>
4859
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004860#endif /* !_ASMLANGUAGE */
4861
Flavio Ceolin67ca1762018-09-14 10:43:44 -07004862#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */