blob: 2edd113527a0d4d2f0b4458b6c75849ba4cd27d5 [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};
152
153#ifdef CONFIG_USERSPACE
154/* Table generated by gperf, these objects are retrieved via
155 * _k_object_find() */
156struct _k_object {
157 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700158 u8_t perms[CONFIG_MAX_THREAD_BYTES];
159 u8_t type;
160 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700161 u32_t data;
Andrew Boiedf555242018-05-25 07:28:54 -0700162} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700163
Andrew Boie877f82e2017-10-17 11:20:22 -0700164struct _k_object_assignment {
165 struct k_thread *thread;
166 void * const *objects;
167};
168
169/**
170 * @brief Grant a static thread access to a list of kernel objects
171 *
172 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
173 * a set of kernel objects. These objects do not need to be in an initialized
174 * state. The permissions will be granted when the threads are initialized
175 * in the early boot sequence.
176 *
177 * All arguments beyond the first must be pointers to kernel objects.
178 *
179 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
180 */
181#define K_THREAD_ACCESS_GRANT(name_, ...) \
182 static void * const _CONCAT(_object_list_, name_)[] = \
183 { __VA_ARGS__, NULL }; \
184 static __used __in_section_unique(object_access) \
185 const struct _k_object_assignment \
186 _CONCAT(_object_access_, name_) = \
187 { (&_k_thread_obj_ ## name_), \
188 (_CONCAT(_object_list_, name_)) }
189
Andrew Boie945af952017-08-22 13:15:23 -0700190#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700191#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700192#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie945af952017-08-22 13:15:23 -0700193
194/**
195 * Lookup a kernel object and init its metadata if it exists
196 *
197 * Calling this on an object will make it usable from userspace.
198 * Intended to be called as the last statement in kernel object init
199 * functions.
200 *
Anas Nashif50e3acd2018-12-08 13:26:18 -0500201 * @param obj Address of the kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700202 */
203void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700204#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700205
206#define K_THREAD_ACCESS_GRANT(thread, ...)
207
Anas Nashif954d5502018-02-25 08:37:28 -0600208/**
209 * @internal
210 */
Andrew Boie743e4682017-10-04 12:25:50 -0700211static inline void _k_object_init(void *obj)
212{
213 ARG_UNUSED(obj);
214}
215
Anas Nashif954d5502018-02-25 08:37:28 -0600216/**
217 * @internal
218 */
Andrew Boie743e4682017-10-04 12:25:50 -0700219static inline void _impl_k_object_access_grant(void *object,
220 struct k_thread *thread)
221{
222 ARG_UNUSED(object);
223 ARG_UNUSED(thread);
224}
225
Anas Nashif954d5502018-02-25 08:37:28 -0600226/**
227 * @internal
228 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700229static inline void k_object_access_revoke(void *object,
230 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700231{
232 ARG_UNUSED(object);
233 ARG_UNUSED(thread);
234}
235
Andrew Boiee9cfc542018-04-13 13:15:28 -0700236/**
237 * @internal
238 */
239static inline void _impl_k_object_release(void *object)
240{
241 ARG_UNUSED(object);
242}
243
Andrew Boie41bab6e2017-10-14 14:42:23 -0700244static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700245{
246 ARG_UNUSED(object);
247}
248#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700249
250/**
251 * grant a thread access to a kernel object
252 *
253 * The thread will be granted access to the object if the caller is from
254 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700255 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700256 *
257 * @param object Address of kernel object
258 * @param thread Thread to grant access to the object
259 */
Andrew Boie743e4682017-10-04 12:25:50 -0700260__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700261
Andrew Boiea89bf012017-10-09 14:47:55 -0700262/**
263 * grant a thread access to a kernel object
264 *
265 * The thread will lose access to the object if the caller is from
266 * supervisor mode, or the caller is from user mode AND has permissions
267 * on both the object and the thread whose access is being revoked.
268 *
269 * @param object Address of kernel object
270 * @param thread Thread to remove access to the object
271 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700272void k_object_access_revoke(void *object, struct k_thread *thread);
273
274
275__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700276
277/**
278 * grant all present and future threads access to an object
279 *
280 * If the caller is from supervisor mode, or the caller is from user mode and
281 * have sufficient permissions on the object, then that object will have
282 * permissions granted to it for *all* current and future threads running in
283 * the system, effectively becoming a public kernel object.
284 *
285 * Use of this API should be avoided on systems that are running untrusted code
286 * as it is possible for such code to derive the addresses of kernel objects
287 * and perform unwanted operations on them.
288 *
Andrew Boie04caa672017-10-13 13:57:07 -0700289 * It is not possible to revoke permissions on public objects; once public,
290 * any thread may use it.
291 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700292 * @param object Address of kernel object
293 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700294void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700295
Andrew Boie31bdfc02017-11-08 16:38:03 -0800296/**
297 * Allocate a kernel object of a designated type
298 *
299 * This will instantiate at runtime a kernel object of the specified type,
300 * returning a pointer to it. The object will be returned in an uninitialized
301 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700302 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800303 *
304 * Currently, allocation of thread stacks is not supported.
305 *
306 * @param otype Requested kernel object type
307 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
308 * available
309 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700310__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800311
Andrew Boie97bf0012018-04-24 17:01:37 -0700312#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800313/**
314 * Free a kernel object previously allocated with k_object_alloc()
315 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700316 * This will return memory for a kernel object back to resource pool it was
317 * allocated from. Care must be exercised that the object will not be used
318 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800319 *
320 * @param obj Pointer to the kernel object memory address.
321 */
322void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700323#else
324static inline void *_impl_k_object_alloc(enum k_objects otype)
325{
Kumar Gala85699f72018-05-17 09:26:03 -0500326 ARG_UNUSED(otype);
327
Andrew Boie97bf0012018-04-24 17:01:37 -0700328 return NULL;
329}
330
331static inline void k_obj_free(void *obj)
332{
333 ARG_UNUSED(obj);
334}
Andrew Boie31bdfc02017-11-08 16:38:03 -0800335#endif /* CONFIG_DYNAMIC_OBJECTS */
336
Andrew Boiebca15da2017-10-15 14:17:48 -0700337/* Using typedef deliberately here, this is quite intended to be an opaque
338 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
339 *
340 * The purpose of this data type is to clearly distinguish between the
341 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
342 * buffer which composes the stack data actually used by the underlying
343 * thread; they cannot be used interchangably as some arches precede the
344 * stack buffer region with guard areas that trigger a MPU or MMU fault
345 * if written to.
346 *
347 * APIs that want to work with the buffer inside should continue to use
348 * char *.
349 *
350 * Stacks should always be created with K_THREAD_STACK_DEFINE().
351 */
352struct __packed _k_thread_stack_element {
353 char data;
354};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700355typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700356
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700357/**
358 * @typedef k_thread_entry_t
359 * @brief Thread entry point function type.
360 *
361 * A thread's entry point function is invoked when the thread starts executing.
362 * Up to 3 argument values can be passed to the function.
363 *
364 * The thread terminates execution permanently if the entry point function
365 * returns. The thread is responsible for releasing any shared resources
366 * it may own (such as mutexes and dynamically allocated memory), prior to
367 * returning.
368 *
369 * @param p1 First argument.
370 * @param p2 Second argument.
371 * @param p3 Third argument.
372 *
373 * @return N/A
374 */
375typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700376
377#ifdef CONFIG_THREAD_MONITOR
378struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700379 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700380 void *parameter1;
381 void *parameter2;
382 void *parameter3;
383};
384#endif
385
386/* can be used for creating 'dummy' threads, e.g. for pending on objects */
387struct _thread_base {
388
389 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700390 union {
Peter A. Bigot82ad0d22019-01-03 23:49:28 -0600391 sys_dnode_t qnode_dlist;
Andy Ross1acd8c22018-05-03 14:51:49 -0700392 struct rbnode qnode_rb;
393 };
394
Andy Ross1acd8c22018-05-03 14:51:49 -0700395 /* wait queue on which the thread is pended (needed only for
396 * trees, not dumb lists)
397 */
398 _wait_q_t *pended_on;
Andrew Boie73abd322017-04-04 13:19:13 -0700399
400 /* user facing 'thread options'; values defined in include/kernel.h */
401 u8_t user_options;
402
403 /* thread state */
404 u8_t thread_state;
405
406 /*
407 * scheduler lock count and thread priority
408 *
409 * These two fields control the preemptibility of a thread.
410 *
411 * When the scheduler is locked, sched_locked is decremented, which
412 * means that the scheduler is locked for values from 0xff to 0x01. A
413 * thread is coop if its prio is negative, thus 0x80 to 0xff when
414 * looked at the value as unsigned.
415 *
416 * By putting them end-to-end, this means that a thread is
417 * non-preemptible if the bundled value is greater than or equal to
418 * 0x0080.
419 */
420 union {
421 struct {
422#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
423 u8_t sched_locked;
424 s8_t prio;
425#else /* LITTLE and PDP */
426 s8_t prio;
427 u8_t sched_locked;
428#endif
429 };
430 u16_t preempt;
431 };
432
Andy Ross4a2e50f2018-05-15 11:06:25 -0700433#ifdef CONFIG_SCHED_DEADLINE
434 int prio_deadline;
435#endif
436
Andy Ross1acd8c22018-05-03 14:51:49 -0700437 u32_t order_key;
438
Andy Ross2724fd12018-01-29 14:55:20 -0800439#ifdef CONFIG_SMP
440 /* True for the per-CPU idle threads */
441 u8_t is_idle;
442
Andy Ross2724fd12018-01-29 14:55:20 -0800443 /* CPU index on which thread was last run */
444 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700445
446 /* Recursive count of irq_lock() calls */
447 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800448#endif
449
Andrew Boie73abd322017-04-04 13:19:13 -0700450 /* data returned by APIs */
451 void *swap_data;
452
453#ifdef CONFIG_SYS_CLOCK_EXISTS
454 /* this thread's entry in a timeout queue */
455 struct _timeout timeout;
456#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700457};
458
459typedef struct _thread_base _thread_base_t;
460
461#if defined(CONFIG_THREAD_STACK_INFO)
462/* Contains the stack information of a thread */
463struct _thread_stack_info {
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700464 /* Stack Start - Identical to K_THREAD_STACK_BUFFER() on the stack
465 * object. Represents thread-writable stack area without any extras.
466 */
Andrew Boie73abd322017-04-04 13:19:13 -0700467 u32_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700468
469 /* Stack Size - Thread writable stack buffer size. Represents
470 * the size of the actual area, starting from the start member,
471 * that should be writable by the thread
472 */
Andrew Boie73abd322017-04-04 13:19:13 -0700473 u32_t size;
474};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700475
476typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700477#endif /* CONFIG_THREAD_STACK_INFO */
478
Chunlin Hane9c97022017-07-07 20:29:30 +0800479#if defined(CONFIG_USERSPACE)
480struct _mem_domain_info {
481 /* memory domain queue node */
482 sys_dnode_t mem_domain_q_node;
483 /* memory domain of the thread */
484 struct k_mem_domain *mem_domain;
485};
486
487#endif /* CONFIG_USERSPACE */
488
Daniel Leungfc182432018-08-16 15:42:28 -0700489#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
490struct _thread_userspace_local_data {
491 int errno_var;
492};
493#endif
494
Anas Nashifce78d162018-05-24 12:43:11 -0500495/**
496 * @ingroup thread_apis
497 * Thread Structure
498 */
Andrew Boie73abd322017-04-04 13:19:13 -0700499struct k_thread {
500
501 struct _thread_base base;
502
Anas Nashifce78d162018-05-24 12:43:11 -0500503 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700504 struct _caller_saved caller_saved;
Anas Nashifce78d162018-05-24 12:43:11 -0500505 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700506 struct _callee_saved callee_saved;
507
Anas Nashifce78d162018-05-24 12:43:11 -0500508 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700509 void *init_data;
510
Anas Nashifce78d162018-05-24 12:43:11 -0500511 /**
512 * abort function
513 * @req K-THREAD-002
514 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700515 void (*fn_abort)(void);
516
517#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500518 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700519 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700520
Anas Nashifce78d162018-05-24 12:43:11 -0500521 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700522 struct k_thread *next_thread;
523#endif
524
Anas Nashif57554052018-03-03 02:31:05 -0600525#if defined(CONFIG_THREAD_NAME)
526 /* Thread name */
527 const char *name;
528#endif
529
Andrew Boie73abd322017-04-04 13:19:13 -0700530#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500531 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700532 void *custom_data;
533#endif
534
Daniel Leungfc182432018-08-16 15:42:28 -0700535#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
536 struct _thread_userspace_local_data *userspace_local_data;
537#endif
538
Andrew Boie73abd322017-04-04 13:19:13 -0700539#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700540#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500541 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700542 int errno_var;
543#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700544#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700545
546#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500547 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700548 struct _thread_stack_info stack_info;
549#endif /* CONFIG_THREAD_STACK_INFO */
550
Chunlin Hane9c97022017-07-07 20:29:30 +0800551#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500552 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800553 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500554 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700555 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800556#endif /* CONFIG_USERSPACE */
557
Andy Ross042d8ec2017-12-09 08:37:20 -0800558#if defined(CONFIG_USE_SWITCH)
559 /* When using __switch() a few previously arch-specific items
560 * become part of the core OS
561 */
562
Anas Nashifce78d162018-05-24 12:43:11 -0500563 /** _Swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800564 int swap_retval;
565
Anas Nashifce78d162018-05-24 12:43:11 -0500566 /** Context handle returned via _arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800567 void *switch_handle;
568#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500569 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700570 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800571
Anas Nashifce78d162018-05-24 12:43:11 -0500572 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700573 struct _thread_arch arch;
574};
575
576typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400577typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400578
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400579enum execution_context_types {
580 K_ISR = 0,
581 K_COOP_THREAD,
582 K_PREEMPT_THREAD,
583};
584
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400585/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100586 * @defgroup profiling_apis Profiling APIs
587 * @ingroup kernel_apis
588 * @{
589 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530590typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
591 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100592
593/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530594 * @brief Iterate over all the threads in the system.
595 *
596 * This routine iterates over all the threads in the system and
597 * calls the user_cb function for each thread.
598 *
599 * @param user_cb Pointer to the user callback function.
600 * @param user_data Pointer to user data.
601 *
602 * @note CONFIG_THREAD_MONITOR must be set for this function
603 * to be effective. Also this API uses irq_lock to protect the
604 * _kernel.threads list which means creation of new threads and
605 * terminations of existing threads are blocked until this
606 * API returns.
607 *
608 * @return N/A
609 */
610extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
611
Anas Nashif166f5192018-02-25 08:02:36 -0600612/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100613
614/**
Allan Stephensc98da842016-11-11 15:45:03 -0500615 * @defgroup thread_apis Thread APIs
616 * @ingroup kernel_apis
617 * @{
618 */
619
Benjamin Walshed240f22017-01-22 13:05:08 -0500620#endif /* !_ASMLANGUAGE */
621
622
623/*
624 * Thread user options. May be needed by assembly code. Common part uses low
625 * bits, arch-specific use high bits.
626 */
627
Anas Nashifa541e932018-05-24 11:19:16 -0500628/**
629 * @brief system thread that must not abort
630 * @req K-THREAD-000
631 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700632#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500633
634#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500635/**
636 * @brief thread uses floating point registers
637 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700638#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500639#endif
640
Anas Nashifa541e932018-05-24 11:19:16 -0500641/**
642 * @brief user mode thread
643 *
644 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700645 * has additional restrictions
646 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700647#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700648
Anas Nashifa541e932018-05-24 11:19:16 -0500649/**
650 * @brief Inherit Permissions
651 *
652 * @details
653 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700654 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
655 * is not enabled.
656 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700657#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700658
Benjamin Walshed240f22017-01-22 13:05:08 -0500659#ifdef CONFIG_X86
660/* x86 Bitmask definitions for threads user options */
661
662#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
663/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700664#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500665#endif
666#endif
667
668/* end - thread options */
669
670#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400671/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700672 * @brief Create a thread.
673 *
674 * This routine initializes a thread, then schedules it for execution.
675 *
676 * The new thread may be scheduled for immediate execution or a delayed start.
677 * If the newly spawned thread does not have a delayed start the kernel
678 * scheduler may preempt the current thread to allow the new thread to
679 * execute.
680 *
681 * Thread options are architecture-specific, and can include K_ESSENTIAL,
682 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
683 * them using "|" (the logical OR operator).
684 *
685 * Historically, users often would use the beginning of the stack memory region
686 * to store the struct k_thread data, although corruption will occur if the
687 * stack overflows this region and stack protection features may not detect this
688 * situation.
689 *
690 * @param new_thread Pointer to uninitialized struct k_thread
691 * @param stack Pointer to the stack space.
692 * @param stack_size Stack size in bytes.
693 * @param entry Thread entry function.
694 * @param p1 1st entry point parameter.
695 * @param p2 2nd entry point parameter.
696 * @param p3 3rd entry point parameter.
697 * @param prio Thread priority.
698 * @param options Thread options.
699 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
700 *
701 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400702 *
703 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700704 */
Andrew Boie662c3452017-10-02 10:51:18 -0700705__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700706 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700707 size_t stack_size,
708 k_thread_entry_t entry,
709 void *p1, void *p2, void *p3,
710 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700711
Andrew Boie3f091b52017-08-30 14:34:14 -0700712/**
713 * @brief Drop a thread's privileges permanently to user mode
714 *
715 * @param entry Function to start executing from
716 * @param p1 1st entry point parameter
717 * @param p2 2nd entry point parameter
718 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400719 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700720 */
721extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
722 void *p1, void *p2,
723 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700724
Andrew Boied26cf2d2017-03-30 13:07:02 -0700725/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530726 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700727 *
728 * This is a convenience function. For the provided thread, grant access to
729 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700730 *
731 * The thread object must be initialized (i.e. running). The objects don't
732 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530733 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700734 *
735 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530736 * @param ... list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400737 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700738 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530739#define k_thread_access_grant(thread, ...) \
740 FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700741
742/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700743 * @brief Assign a resource memory pool to a thread
744 *
745 * By default, threads have no resource pool assigned unless their parent
746 * thread has a resource pool, in which case it is inherited. Multiple
747 * threads may be assigned to the same memory pool.
748 *
749 * Changing a thread's resource pool will not migrate allocations from the
750 * previous pool.
751 *
752 * @param thread Target thread to assign a memory pool for resource requests,
753 * or NULL if the thread should no longer have a memory pool.
754 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400755 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700756 */
757static inline void k_thread_resource_pool_assign(struct k_thread *thread,
758 struct k_mem_pool *pool)
759{
760 thread->resource_pool = pool;
761}
762
763#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
764/**
765 * @brief Assign the system heap as a thread's resource pool
766 *
767 * Similar to k_thread_resource_pool_assign(), but the thread will use
768 * the kernel heap to draw memory.
769 *
770 * Use with caution, as a malicious thread could perform DoS attacks on the
771 * kernel heap.
772 *
773 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400774 *
775 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700776 */
777void k_thread_system_pool_assign(struct k_thread *thread);
778#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
779
780/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500781 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400782 *
Allan Stephensc98da842016-11-11 15:45:03 -0500783 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500784 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400785 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500786 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400787 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200788 * @return Zero if the requested time has elapsed or the number of milliseconds
789 * left to sleep, if thread was woken up by \ref k_wakeup call.
790 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400791 */
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200792__syscall s32_t k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400793
794/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500795 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400796 *
797 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500798 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400799 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400800 * @return N/A
801 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800802__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400803
804/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500805 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400806 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500807 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400808 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500809 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810 *
811 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400812 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400813 */
Andrew Boie468190a2017-09-29 14:00:48 -0700814__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400815
816/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500817 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500819 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400820 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500821 * If @a thread is not currently sleeping, the routine has no effect.
822 *
823 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400824 *
825 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400826 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827 */
Andrew Boie468190a2017-09-29 14:00:48 -0700828__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829
830/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500831 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500833 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400834 *
835 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700837__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838
839/**
Allan Stephensc98da842016-11-11 15:45:03 -0500840 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500842 * This routine permanently stops execution of @a thread. The thread is taken
843 * off all kernel queues it is part of (i.e. the ready queue, the timeout
844 * queue, or a kernel object wait queue). However, any kernel resources the
845 * thread might currently own (such as mutexes or memory blocks) are not
846 * released. It is the responsibility of the caller of this routine to ensure
847 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500849 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400850 *
851 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400852 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400853 */
Andrew Boie468190a2017-09-29 14:00:48 -0700854__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400855
Andrew Boie7d627c52017-08-30 11:01:56 -0700856
857/**
858 * @brief Start an inactive thread
859 *
860 * If a thread was created with K_FOREVER in the delay parameter, it will
861 * not be added to the scheduling queue until this function is called
862 * on it.
863 *
864 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400865 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700866 */
Andrew Boie468190a2017-09-29 14:00:48 -0700867__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700868
Allan Stephensc98da842016-11-11 15:45:03 -0500869/**
870 * @cond INTERNAL_HIDDEN
871 */
872
Benjamin Walshd211a522016-12-06 11:44:01 -0500873/* timeout has timed out and is not on _timeout_q anymore */
874#define _EXPIRED (-2)
875
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400876struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700877 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700878 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400879 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700880 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500881 void *init_p1;
882 void *init_p2;
883 void *init_p3;
884 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500885 u32_t init_options;
886 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500887 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600888 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400889};
890
Andrew Boied26cf2d2017-03-30 13:07:02 -0700891#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400892 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600893 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500894 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700895 .init_thread = (thread), \
896 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500897 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700898 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400899 .init_p1 = (void *)p1, \
900 .init_p2 = (void *)p2, \
901 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500902 .init_prio = (prio), \
903 .init_options = (options), \
904 .init_delay = (delay), \
905 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600906 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400907 }
908
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400909/**
Allan Stephensc98da842016-11-11 15:45:03 -0500910 * INTERNAL_HIDDEN @endcond
911 */
912
913/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500914 * @brief Statically define and initialize a thread.
915 *
916 * The thread may be scheduled for immediate execution or a delayed start.
917 *
918 * Thread options are architecture-specific, and can include K_ESSENTIAL,
919 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
920 * them using "|" (the logical OR operator).
921 *
922 * The ID of the thread can be accessed using:
923 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500924 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500925 *
926 * @param name Name of the thread.
927 * @param stack_size Stack size in bytes.
928 * @param entry Thread entry function.
929 * @param p1 1st entry point parameter.
930 * @param p2 2nd entry point parameter.
931 * @param p3 3rd entry point parameter.
932 * @param prio Thread priority.
933 * @param options Thread options.
934 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400935 *
Anas Nashif47420d02018-05-24 14:20:56 -0400936 * @req K-THREAD-010
937 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400938 * @internal It has been observed that the x86 compiler by default aligns
939 * these _static_thread_data structures to 32-byte boundaries, thereby
940 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400941 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400942 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500943#define K_THREAD_DEFINE(name, stack_size, \
944 entry, p1, p2, p3, \
945 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700946 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700947 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500948 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500949 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700950 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
951 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500952 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -0600953 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700954 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400955
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400956/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500957 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400958 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500959 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400960 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500961 * @param thread ID of thread whose priority is needed.
962 *
963 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400964 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400965 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700966__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400967
968/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500969 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500971 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400972 *
973 * Rescheduling can occur immediately depending on the priority @a thread is
974 * set to:
975 *
976 * - If its priority is raised above the priority of the caller of this
977 * function, and the caller is preemptible, @a thread will be scheduled in.
978 *
979 * - If the caller operates on itself, it lowers its priority below that of
980 * other threads in the system, and the caller is preemptible, the thread of
981 * highest priority will be scheduled in.
982 *
983 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
984 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
985 * highest priority.
986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500987 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400988 * @param prio New priority.
989 *
990 * @warning Changing the priority of a thread currently involved in mutex
991 * priority inheritance may result in undefined behavior.
992 *
993 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400994 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400995 */
Andrew Boie468190a2017-09-29 14:00:48 -0700996__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400997
Andy Ross4a2e50f2018-05-15 11:06:25 -0700998
999#ifdef CONFIG_SCHED_DEADLINE
1000/**
1001 * @brief Set deadline expiration time for scheduler
1002 *
1003 * This sets the "deadline" expiration as a time delta from the
1004 * current time, in the same units used by k_cycle_get_32(). The
1005 * scheduler (when deadline scheduling is enabled) will choose the
1006 * next expiring thread when selecting between threads at the same
1007 * static priority. Threads at different priorities will be scheduled
1008 * according to their static priority.
1009 *
1010 * @note Deadlines that are negative (i.e. in the past) are still seen
1011 * as higher priority than others, even if the thread has "finished"
1012 * its work. If you don't want it scheduled anymore, you have to
1013 * reset the deadline into the future, block/pend the thread, or
1014 * modify its priority with k_thread_priority_set().
1015 *
1016 * @note Despite the API naming, the scheduler makes no guarantees the
1017 * the thread WILL be scheduled within that deadline, nor does it take
1018 * extra metadata (like e.g. the "runtime" and "period" parameters in
1019 * Linux sched_setattr()) that allows the kernel to validate the
1020 * scheduling for achievability. Such features could be implemented
1021 * above this call, which is simply input to the priority selection
1022 * logic.
1023 *
1024 * @param thread A thread on which to set the deadline
1025 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001026 *
1027 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001028 */
1029__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1030#endif
1031
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001032/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001033 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001035 * This routine prevents the kernel scheduler from making @a thread the
1036 * current thread. All other internal operations on @a thread are still
1037 * performed; for example, any timeout it is waiting on keeps ticking,
1038 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001039 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001040 * If @a thread is already suspended, the routine has no effect.
1041 *
1042 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001043 *
1044 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001045 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001046 */
Andrew Boie468190a2017-09-29 14:00:48 -07001047__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001048
1049/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001050 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001051 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001052 * This routine allows the kernel scheduler to make @a thread the current
1053 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001055 * If @a thread is not currently suspended, the routine has no effect.
1056 *
1057 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001058 *
1059 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001060 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001061 */
Andrew Boie468190a2017-09-29 14:00:48 -07001062__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001063
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001064/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001065 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001066 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001067 * This routine specifies how the scheduler will perform time slicing of
1068 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001069 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001070 * To enable time slicing, @a slice must be non-zero. The scheduler
1071 * ensures that no thread runs for more than the specified time limit
1072 * before other threads of that priority are given a chance to execute.
1073 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001074 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001076 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001077 * execute. Once the scheduler selects a thread for execution, there is no
1078 * minimum guaranteed time the thread will execute before threads of greater or
1079 * equal priority are scheduled.
1080 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001081 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001082 * for execution, this routine has no effect; the thread is immediately
1083 * rescheduled after the slice period expires.
1084 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001085 * To disable timeslicing, set both @a slice and @a prio to zero.
1086 *
1087 * @param slice Maximum time slice length (in milliseconds).
1088 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001089 *
1090 * @return N/A
1091 */
Kumar Galacc334c72017-04-21 10:55:34 -05001092extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001093
Anas Nashif166f5192018-02-25 08:02:36 -06001094/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001095
1096/**
1097 * @addtogroup isr_apis
1098 * @{
1099 */
1100
1101/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001102 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001103 *
Allan Stephensc98da842016-11-11 15:45:03 -05001104 * This routine allows the caller to customize its actions, depending on
1105 * whether it is a thread or an ISR.
1106 *
1107 * @note Can be called by ISRs.
1108 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001109 * @return false if invoked by a thread.
1110 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001111 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001112extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001113
Benjamin Walsh445830d2016-11-10 15:54:27 -05001114/**
1115 * @brief Determine if code is running in a preemptible thread.
1116 *
Allan Stephensc98da842016-11-11 15:45:03 -05001117 * This routine allows the caller to customize its actions, depending on
1118 * whether it can be preempted by another thread. The routine returns a 'true'
1119 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001120 *
Allan Stephensc98da842016-11-11 15:45:03 -05001121 * - The code is running in a thread, not at ISR.
1122 * - The thread's priority is in the preemptible range.
1123 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001124 *
Allan Stephensc98da842016-11-11 15:45:03 -05001125 * @note Can be called by ISRs.
1126 *
1127 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001128 * @return Non-zero if invoked by a preemptible thread.
1129 */
Andrew Boie468190a2017-09-29 14:00:48 -07001130__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001131
Allan Stephensc98da842016-11-11 15:45:03 -05001132/**
Anas Nashif166f5192018-02-25 08:02:36 -06001133 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001134 */
1135
1136/**
1137 * @addtogroup thread_apis
1138 * @{
1139 */
1140
1141/**
1142 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001143 *
Allan Stephensc98da842016-11-11 15:45:03 -05001144 * This routine prevents the current thread from being preempted by another
1145 * thread by instructing the scheduler to treat it as a cooperative thread.
1146 * If the thread subsequently performs an operation that makes it unready,
1147 * it will be context switched out in the normal manner. When the thread
1148 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001149 *
Allan Stephensc98da842016-11-11 15:45:03 -05001150 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001151 *
Allan Stephensc98da842016-11-11 15:45:03 -05001152 * @note k_sched_lock() and k_sched_unlock() should normally be used
1153 * when the operation being performed can be safely interrupted by ISRs.
1154 * However, if the amount of processing involved is very small, better
1155 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001156 *
1157 * @return N/A
1158 */
1159extern void k_sched_lock(void);
1160
Allan Stephensc98da842016-11-11 15:45:03 -05001161/**
1162 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001163 *
Allan Stephensc98da842016-11-11 15:45:03 -05001164 * This routine reverses the effect of a previous call to k_sched_lock().
1165 * A thread must call the routine once for each time it called k_sched_lock()
1166 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001167 *
1168 * @return N/A
1169 */
1170extern void k_sched_unlock(void);
1171
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001172/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001173 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001174 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001175 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001176 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001177 * Custom data is not used by the kernel itself, and is freely available
1178 * for a thread to use as it sees fit. It can be used as a framework
1179 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001181 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001182 *
1183 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001184 *
1185 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001186 */
Andrew Boie468190a2017-09-29 14:00:48 -07001187__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001188
1189/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001190 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001191 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001192 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001193 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001194 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001195 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001196 */
Andrew Boie468190a2017-09-29 14:00:48 -07001197__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001198
1199/**
Anas Nashif57554052018-03-03 02:31:05 -06001200 * @brief Set current thread name
1201 *
1202 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1203 * tracing and debugging.
1204 *
1205 */
1206__syscall void k_thread_name_set(k_tid_t thread_id, const char *value);
1207
1208/**
1209 * @brief Get thread name
1210 *
1211 * Get the name of a thread
1212 *
1213 * @param thread_id Thread ID
1214 *
1215 */
1216__syscall const char *k_thread_name_get(k_tid_t thread_id);
1217
1218/**
Andy Rosscfe62032018-09-29 07:34:55 -07001219 * @}
1220 */
1221
1222/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001223 * @addtogroup clock_apis
1224 * @{
1225 */
1226
1227/**
1228 * @brief Generate null timeout delay.
1229 *
1230 * This macro generates a timeout delay that that instructs a kernel API
1231 * not to wait if the requested operation cannot be performed immediately.
1232 *
1233 * @return Timeout delay value.
1234 */
1235#define K_NO_WAIT 0
1236
1237/**
1238 * @brief Generate timeout delay from milliseconds.
1239 *
1240 * This macro generates a timeout delay that that instructs a kernel API
1241 * to wait up to @a ms milliseconds to perform the requested operation.
1242 *
1243 * @param ms Duration in milliseconds.
1244 *
1245 * @return Timeout delay value.
1246 */
Johan Hedberg14471692016-11-13 10:52:15 +02001247#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001248
1249/**
1250 * @brief Generate timeout delay from seconds.
1251 *
1252 * This macro generates a timeout delay that that instructs a kernel API
1253 * to wait up to @a s seconds to perform the requested operation.
1254 *
1255 * @param s Duration in seconds.
1256 *
1257 * @return Timeout delay value.
1258 */
Johan Hedberg14471692016-11-13 10:52:15 +02001259#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001260
1261/**
1262 * @brief Generate timeout delay from minutes.
1263 *
1264 * This macro generates a timeout delay that that instructs a kernel API
1265 * to wait up to @a m minutes to perform the requested operation.
1266 *
1267 * @param m Duration in minutes.
1268 *
1269 * @return Timeout delay value.
1270 */
Johan Hedberg14471692016-11-13 10:52:15 +02001271#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001272
1273/**
1274 * @brief Generate timeout delay from hours.
1275 *
1276 * This macro generates a timeout delay that that instructs a kernel API
1277 * to wait up to @a h hours to perform the requested operation.
1278 *
1279 * @param h Duration in hours.
1280 *
1281 * @return Timeout delay value.
1282 */
Johan Hedberg14471692016-11-13 10:52:15 +02001283#define K_HOURS(h) K_MINUTES((h) * 60)
1284
Allan Stephensc98da842016-11-11 15:45:03 -05001285/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001286 * @brief Generate infinite timeout delay.
1287 *
1288 * This macro generates a timeout delay that that instructs a kernel API
1289 * to wait as long as necessary to perform the requested operation.
1290 *
1291 * @return Timeout delay value.
1292 */
1293#define K_FOREVER (-1)
1294
1295/**
Anas Nashif166f5192018-02-25 08:02:36 -06001296 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001297 */
1298
1299/**
Allan Stephensc98da842016-11-11 15:45:03 -05001300 * @cond INTERNAL_HIDDEN
1301 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001302
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001303struct k_timer {
1304 /*
1305 * _timeout structure must be first here if we want to use
1306 * dynamic timer allocation. timeout.node is used in the double-linked
1307 * list of free timers
1308 */
1309 struct _timeout timeout;
1310
Allan Stephens45bfa372016-10-12 12:39:42 -05001311 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001312 _wait_q_t wait_q;
1313
1314 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001315 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001316
1317 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001318 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001319
1320 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001321 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001322
Allan Stephens45bfa372016-10-12 12:39:42 -05001323 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001324 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001325
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001326 /* user-specific data, also used to support legacy features */
1327 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001328
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001329 _OBJECT_TRACING_NEXT_PTR(k_timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001330};
1331
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001332#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001333 { \
Peter A. Bigotb4ece0a2019-01-02 08:29:43 -06001334 .timeout.dticks = 0, \
Andy Ross987c0e52018-09-27 16:50:00 -07001335 .timeout.fn = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001336 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001337 .expiry_fn = expiry, \
1338 .stop_fn = stop, \
1339 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001340 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001341 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001342 }
1343
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001344#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1345
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001346/**
Allan Stephensc98da842016-11-11 15:45:03 -05001347 * INTERNAL_HIDDEN @endcond
1348 */
1349
1350/**
1351 * @defgroup timer_apis Timer APIs
1352 * @ingroup kernel_apis
1353 * @{
1354 */
1355
1356/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001357 * @typedef k_timer_expiry_t
1358 * @brief Timer expiry function type.
1359 *
1360 * A timer's expiry function is executed by the system clock interrupt handler
1361 * each time the timer expires. The expiry function is optional, and is only
1362 * invoked if the timer has been initialized with one.
1363 *
1364 * @param timer Address of timer.
1365 *
1366 * @return N/A
1367 */
1368typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1369
1370/**
1371 * @typedef k_timer_stop_t
1372 * @brief Timer stop function type.
1373 *
1374 * A timer's stop function is executed if the timer is stopped prematurely.
1375 * The function runs in the context of the thread that stops the timer.
1376 * The stop function is optional, and is only invoked if the timer has been
1377 * initialized with one.
1378 *
1379 * @param timer Address of timer.
1380 *
1381 * @return N/A
1382 */
1383typedef void (*k_timer_stop_t)(struct k_timer *timer);
1384
1385/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001386 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001388 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001389 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001390 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001391 *
1392 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001393 * @param expiry_fn Function to invoke each time the timer expires.
1394 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001395 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001396#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001397 struct k_timer name \
1398 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001399 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001400
Allan Stephens45bfa372016-10-12 12:39:42 -05001401/**
1402 * @brief Initialize a timer.
1403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001404 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001405 *
1406 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001407 * @param expiry_fn Function to invoke each time the timer expires.
1408 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001409 *
1410 * @return N/A
1411 */
1412extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001413 k_timer_expiry_t expiry_fn,
1414 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001415
Allan Stephens45bfa372016-10-12 12:39:42 -05001416/**
1417 * @brief Start a timer.
1418 *
1419 * This routine starts a timer, and resets its status to zero. The timer
1420 * begins counting down using the specified duration and period values.
1421 *
1422 * Attempting to start a timer that is already running is permitted.
1423 * The timer's status is reset to zero and the timer begins counting down
1424 * using the new duration and period values.
1425 *
1426 * @param timer Address of timer.
1427 * @param duration Initial timer duration (in milliseconds).
1428 * @param period Timer period (in milliseconds).
1429 *
1430 * @return N/A
1431 */
Andrew Boiea354d492017-09-29 16:22:28 -07001432__syscall void k_timer_start(struct k_timer *timer,
1433 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001434
1435/**
1436 * @brief Stop a timer.
1437 *
1438 * This routine stops a running timer prematurely. The timer's stop function,
1439 * if one exists, is invoked by the caller.
1440 *
1441 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001442 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001443 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001444 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1445 * if @a k_timer_stop is to be called from ISRs.
1446 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001447 * @param timer Address of timer.
1448 *
1449 * @return N/A
1450 */
Andrew Boiea354d492017-09-29 16:22:28 -07001451__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001452
1453/**
1454 * @brief Read timer status.
1455 *
1456 * This routine reads the timer's status, which indicates the number of times
1457 * it has expired since its status was last read.
1458 *
1459 * Calling this routine resets the timer's status to zero.
1460 *
1461 * @param timer Address of timer.
1462 *
1463 * @return Timer status.
1464 */
Andrew Boiea354d492017-09-29 16:22:28 -07001465__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001466
1467/**
1468 * @brief Synchronize thread to timer expiration.
1469 *
1470 * This routine blocks the calling thread until the timer's status is non-zero
1471 * (indicating that it has expired at least once since it was last examined)
1472 * or the timer is stopped. If the timer status is already non-zero,
1473 * or the timer is already stopped, the caller continues without waiting.
1474 *
1475 * Calling this routine resets the timer's status to zero.
1476 *
1477 * This routine must not be used by interrupt handlers, since they are not
1478 * allowed to block.
1479 *
1480 * @param timer Address of timer.
1481 *
1482 * @return Timer status.
1483 */
Andrew Boiea354d492017-09-29 16:22:28 -07001484__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001485
Andy Ross52e444b2018-09-28 09:06:37 -07001486extern s32_t z_timeout_remaining(struct _timeout *timeout);
1487
Allan Stephens45bfa372016-10-12 12:39:42 -05001488/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001489 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001490 *
1491 * This routine computes the (approximate) time remaining before a running
1492 * timer next expires. If the timer is not running, it returns zero.
1493 *
1494 * @param timer Address of timer.
1495 *
1496 * @return Remaining time (in milliseconds).
1497 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001498__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001499
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001500static inline u32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001501{
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001502 return (u32_t)__ticks_to_ms(z_timeout_remaining(&timer->timeout));
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001503}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001504
Allan Stephensc98da842016-11-11 15:45:03 -05001505/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001506 * @brief Associate user-specific data with a timer.
1507 *
1508 * This routine records the @a user_data with the @a timer, to be retrieved
1509 * later.
1510 *
1511 * It can be used e.g. in a timer handler shared across multiple subsystems to
1512 * retrieve data specific to the subsystem this timer is associated with.
1513 *
1514 * @param timer Address of timer.
1515 * @param user_data User data to associate with the timer.
1516 *
1517 * @return N/A
1518 */
Andrew Boiea354d492017-09-29 16:22:28 -07001519__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1520
Anas Nashif954d5502018-02-25 08:37:28 -06001521/**
1522 * @internal
1523 */
Andrew Boiea354d492017-09-29 16:22:28 -07001524static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1525 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001526{
1527 timer->user_data = user_data;
1528}
1529
1530/**
1531 * @brief Retrieve the user-specific data from a timer.
1532 *
1533 * @param timer Address of timer.
1534 *
1535 * @return The user data.
1536 */
Andrew Boiea354d492017-09-29 16:22:28 -07001537__syscall void *k_timer_user_data_get(struct k_timer *timer);
1538
1539static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001540{
1541 return timer->user_data;
1542}
1543
Anas Nashif166f5192018-02-25 08:02:36 -06001544/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001545
Allan Stephensc98da842016-11-11 15:45:03 -05001546/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001547 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001548 * @{
1549 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001550
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001551/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001552 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001554 * This routine returns the elapsed time since the system booted,
1555 * in milliseconds.
1556 *
1557 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001558 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001559__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001560
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001561/**
1562 * @brief Enable clock always on in tickless kernel
1563 *
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001564 * This routine enables keeping the clock running (that is, it always
1565 * keeps an active timer interrupt scheduled) when there are no timer
1566 * events programmed in tickless kernel scheduling. This is necessary
1567 * if the clock is used to track passage of time (e.g. via
1568 * k_uptime_get_32()), otherwise the internal hardware counter may
1569 * roll over between interrupts.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001570 *
1571 * @retval prev_status Previous status of always on flag
1572 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001573int k_enable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001574
1575/**
1576 * @brief Disable clock always on in tickless kernel
1577 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001578 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001579 * there are no timer events programmed in tickless kernel
1580 * scheduling. To save power, this routine should be called
1581 * immediately when clock is not used to track time.
1582 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001583void k_disable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001584
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001585/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001586 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001587 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001588 * This routine returns the lower 32-bits of the elapsed time since the system
1589 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001591 * This routine can be more efficient than k_uptime_get(), as it reduces the
1592 * need for interrupt locking and 64-bit math. However, the 32-bit result
1593 * cannot hold a system uptime time larger than approximately 50 days, so the
1594 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001596 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001597 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001598__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001599
1600/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001601 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001603 * This routine computes the elapsed time between the current system uptime
1604 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001606 * @param reftime Pointer to a reference time, which is updated to the current
1607 * uptime upon return.
1608 *
1609 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001610 */
Andy Ross987c0e52018-09-27 16:50:00 -07001611static inline s64_t k_uptime_delta(s64_t *reftime)
1612{
1613 s64_t uptime, delta;
1614
1615 uptime = k_uptime_get();
1616 delta = uptime - *reftime;
1617 *reftime = uptime;
1618
1619 return delta;
1620}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001621
1622/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001623 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001624 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001625 * This routine computes the elapsed time between the current system uptime
1626 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001627 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001628 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1629 * need for interrupt locking and 64-bit math. However, the 32-bit result
1630 * cannot hold an elapsed time larger than approximately 50 days, so the
1631 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001632 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001633 * @param reftime Pointer to a reference time, which is updated to the current
1634 * uptime upon return.
1635 *
1636 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001637 */
Andy Ross987c0e52018-09-27 16:50:00 -07001638static inline u32_t k_uptime_delta_32(s64_t *reftime)
1639{
1640 return (u32_t)k_uptime_delta(reftime);
1641}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001642
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001643/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001644 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001645 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001646 * This routine returns the current time, as measured by the system's hardware
1647 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001648 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001649 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001650 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001651#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001652
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001653/**
Anas Nashif166f5192018-02-25 08:02:36 -06001654 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001655 */
1656
Allan Stephensc98da842016-11-11 15:45:03 -05001657/**
1658 * @cond INTERNAL_HIDDEN
1659 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001660
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001661struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001662 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001663 union {
1664 _wait_q_t wait_q;
1665
1666 _POLL_EVENT;
1667 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001668
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001669 _OBJECT_TRACING_NEXT_PTR(k_queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001670};
1671
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001672#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001673 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001674 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001675 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001676 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001677 _OBJECT_TRACING_INIT \
1678 }
1679
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001680#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1681
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001682extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1683
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001684/**
1685 * INTERNAL_HIDDEN @endcond
1686 */
1687
1688/**
1689 * @defgroup queue_apis Queue APIs
1690 * @ingroup kernel_apis
1691 * @{
1692 */
1693
1694/**
1695 * @brief Initialize a queue.
1696 *
1697 * This routine initializes a queue object, prior to its first use.
1698 *
1699 * @param queue Address of the queue.
1700 *
1701 * @return N/A
1702 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001703__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001704
1705/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001706 * @brief Cancel waiting on a queue.
1707 *
1708 * This routine causes first thread pending on @a queue, if any, to
1709 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001710 * If the queue is being waited on by k_poll(), it will return with
1711 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1712 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001713 *
1714 * @note Can be called by ISRs.
1715 *
1716 * @param queue Address of the queue.
1717 *
1718 * @return N/A
1719 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001720__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001721
1722/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001723 * @brief Append an element to the end of a queue.
1724 *
1725 * This routine appends a data item to @a queue. A queue data item must be
1726 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1727 * reserved for the kernel's use.
1728 *
1729 * @note Can be called by ISRs.
1730 *
1731 * @param queue Address of the queue.
1732 * @param data Address of the data item.
1733 *
1734 * @return N/A
1735 */
1736extern void k_queue_append(struct k_queue *queue, void *data);
1737
1738/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001739 * @brief Append an element to a queue.
1740 *
1741 * This routine appends a data item to @a queue. There is an implicit
1742 * memory allocation from the calling thread's resource pool, which is
1743 * automatically freed when the item is removed from the queue.
1744 *
1745 * @note Can be called by ISRs.
1746 *
1747 * @param queue Address of the queue.
1748 * @param data Address of the data item.
1749 *
1750 * @retval 0 on success
1751 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1752 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301753__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001754
1755/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001756 * @brief Prepend an element to a queue.
1757 *
1758 * This routine prepends a data item to @a queue. A queue data item must be
1759 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1760 * reserved for the kernel's use.
1761 *
1762 * @note Can be called by ISRs.
1763 *
1764 * @param queue Address of the queue.
1765 * @param data Address of the data item.
1766 *
1767 * @return N/A
1768 */
1769extern void k_queue_prepend(struct k_queue *queue, void *data);
1770
1771/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001772 * @brief Prepend an element to a queue.
1773 *
1774 * This routine prepends a data item to @a queue. There is an implicit
1775 * memory allocation from the calling thread's resource pool, which is
1776 * automatically freed when the item is removed from the queue.
1777 *
1778 * @note Can be called by ISRs.
1779 *
1780 * @param queue Address of the queue.
1781 * @param data Address of the data item.
1782 *
1783 * @retval 0 on success
1784 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1785 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301786__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001787
1788/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001789 * @brief Inserts an element to a queue.
1790 *
1791 * This routine inserts a data item to @a queue after previous item. A queue
1792 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1793 * item are reserved for the kernel's use.
1794 *
1795 * @note Can be called by ISRs.
1796 *
1797 * @param queue Address of the queue.
1798 * @param prev Address of the previous data item.
1799 * @param data Address of the data item.
1800 *
1801 * @return N/A
1802 */
1803extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1804
1805/**
1806 * @brief Atomically append a list of elements to a queue.
1807 *
1808 * This routine adds a list of data items to @a queue in one operation.
1809 * The data items must be in a singly-linked list, with the first 32 bits
1810 * in each data item pointing to the next data item; the list must be
1811 * NULL-terminated.
1812 *
1813 * @note Can be called by ISRs.
1814 *
1815 * @param queue Address of the queue.
1816 * @param head Pointer to first node in singly-linked list.
1817 * @param tail Pointer to last node in singly-linked list.
1818 *
1819 * @return N/A
1820 */
1821extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1822
1823/**
1824 * @brief Atomically add a list of elements to a queue.
1825 *
1826 * This routine adds a list of data items to @a queue in one operation.
1827 * The data items must be in a singly-linked list implemented using a
1828 * sys_slist_t object. Upon completion, the original list is empty.
1829 *
1830 * @note Can be called by ISRs.
1831 *
1832 * @param queue Address of the queue.
1833 * @param list Pointer to sys_slist_t object.
1834 *
1835 * @return N/A
1836 */
1837extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1838
1839/**
1840 * @brief Get an element from a queue.
1841 *
1842 * This routine removes first data item from @a queue. The first 32 bits of the
1843 * data item are reserved for the kernel's use.
1844 *
1845 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1846 *
1847 * @param queue Address of the queue.
1848 * @param timeout Waiting period to obtain a data item (in milliseconds),
1849 * or one of the special values K_NO_WAIT and K_FOREVER.
1850 *
1851 * @return Address of the data item if successful; NULL if returned
1852 * without waiting, or waiting period timed out.
1853 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001854__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001855
1856/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001857 * @brief Remove an element from a queue.
1858 *
1859 * This routine removes data item from @a queue. The first 32 bits of the
1860 * data item are reserved for the kernel's use. Removing elements from k_queue
1861 * rely on sys_slist_find_and_remove which is not a constant time operation.
1862 *
1863 * @note Can be called by ISRs
1864 *
1865 * @param queue Address of the queue.
1866 * @param data Address of the data item.
1867 *
1868 * @return true if data item was removed
1869 */
1870static inline bool k_queue_remove(struct k_queue *queue, void *data)
1871{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001872 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001873}
1874
1875/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02001876 * @brief Append an element to a queue only if it's not present already.
1877 *
1878 * This routine appends data item to @a queue. The first 32 bits of the
1879 * data item are reserved for the kernel's use. Appending elements to k_queue
1880 * relies on sys_slist_is_node_in_list which is not a constant time operation.
1881 *
1882 * @note Can be called by ISRs
1883 *
1884 * @param queue Address of the queue.
1885 * @param data Address of the data item.
1886 *
1887 * @return true if data item was added, false if not
1888 */
1889static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
1890{
1891 sys_sfnode_t *test;
1892
1893 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
1894 if (test == (sys_sfnode_t *) data) {
1895 return false;
1896 }
1897 }
1898
1899 k_queue_append(queue, data);
1900 return true;
1901}
1902
1903/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001904 * @brief Query a queue to see if it has data available.
1905 *
1906 * Note that the data might be already gone by the time this function returns
1907 * if other threads are also trying to read from the queue.
1908 *
1909 * @note Can be called by ISRs.
1910 *
1911 * @param queue Address of the queue.
1912 *
1913 * @return Non-zero if the queue is empty.
1914 * @return 0 if data is available.
1915 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001916__syscall int k_queue_is_empty(struct k_queue *queue);
1917
1918static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001919{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001920 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001921}
1922
1923/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001924 * @brief Peek element at the head of queue.
1925 *
1926 * Return element from the head of queue without removing it.
1927 *
1928 * @param queue Address of the queue.
1929 *
1930 * @return Head element, or NULL if queue is empty.
1931 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001932__syscall void *k_queue_peek_head(struct k_queue *queue);
1933
1934static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001935{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001936 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001937}
1938
1939/**
1940 * @brief Peek element at the tail of queue.
1941 *
1942 * Return element from the tail of queue without removing it.
1943 *
1944 * @param queue Address of the queue.
1945 *
1946 * @return Tail element, or NULL if queue is empty.
1947 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001948__syscall void *k_queue_peek_tail(struct k_queue *queue);
1949
1950static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001951{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001952 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001953}
1954
1955/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001956 * @brief Statically define and initialize a queue.
1957 *
1958 * The queue can be accessed outside the module where it is defined using:
1959 *
1960 * @code extern struct k_queue <name>; @endcode
1961 *
1962 * @param name Name of the queue.
1963 */
1964#define K_QUEUE_DEFINE(name) \
1965 struct k_queue name \
1966 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001967 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001968
Anas Nashif166f5192018-02-25 08:02:36 -06001969/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001970
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001971struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001972 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001973};
1974
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04001975/**
1976 * @cond INTERNAL_HIDDEN
1977 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001978#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001979 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001980 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001981 }
1982
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001983#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1984
Allan Stephensc98da842016-11-11 15:45:03 -05001985/**
1986 * INTERNAL_HIDDEN @endcond
1987 */
1988
1989/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001990 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001991 * @ingroup kernel_apis
1992 * @{
1993 */
1994
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001995/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001996 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001997 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001998 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001999 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002000 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002001 *
2002 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002003 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002004 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002005#define k_fifo_init(fifo) \
2006 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002007
2008/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002009 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002010 *
2011 * This routine causes first thread pending on @a fifo, if any, to
2012 * return from k_fifo_get() call with NULL value (as if timeout
2013 * expired).
2014 *
2015 * @note Can be called by ISRs.
2016 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002017 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002018 *
2019 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002020 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002021 */
2022#define k_fifo_cancel_wait(fifo) \
2023 k_queue_cancel_wait((struct k_queue *) fifo)
2024
2025/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002026 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002027 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002028 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002029 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2030 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002032 * @note Can be called by ISRs.
2033 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002034 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002035 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002036 *
2037 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002038 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002039 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002040#define k_fifo_put(fifo, data) \
2041 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002042
2043/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002044 * @brief Add an element to a FIFO queue.
2045 *
2046 * This routine adds a data item to @a fifo. There is an implicit
2047 * memory allocation from the calling thread's resource pool, which is
2048 * automatically freed when the item is removed.
2049 *
2050 * @note Can be called by ISRs.
2051 *
2052 * @param fifo Address of the FIFO.
2053 * @param data Address of the data item.
2054 *
2055 * @retval 0 on success
2056 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002057 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002058 */
2059#define k_fifo_alloc_put(fifo, data) \
2060 k_queue_alloc_append((struct k_queue *) fifo, data)
2061
2062/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002063 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002064 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002065 * This routine adds a list of data items to @a fifo in one operation.
2066 * The data items must be in a singly-linked list, with the first 32 bits
2067 * each data item pointing to the next data item; the list must be
2068 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002069 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002070 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002071 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002072 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002073 * @param head Pointer to first node in singly-linked list.
2074 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002075 *
2076 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002077 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002078 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002079#define k_fifo_put_list(fifo, head, tail) \
2080 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002081
2082/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002083 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002084 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002085 * This routine adds a list of data items to @a fifo in one operation.
2086 * The data items must be in a singly-linked list implemented using a
2087 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002088 * and must be re-initialized via sys_slist_init().
2089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002090 * @note Can be called by ISRs.
2091 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002092 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002093 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002094 *
2095 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002096 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002097 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002098#define k_fifo_put_slist(fifo, list) \
2099 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002100
2101/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002102 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002104 * This routine removes a data item from @a fifo in a "first in, first out"
2105 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002107 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2108 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002109 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002110 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002111 * or one of the special values K_NO_WAIT and K_FOREVER.
2112 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002113 * @return Address of the data item if successful; NULL if returned
2114 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002115 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002116 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002117#define k_fifo_get(fifo, timeout) \
2118 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002119
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002120/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002121 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002122 *
2123 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002124 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002125 *
2126 * @note Can be called by ISRs.
2127 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002128 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002129 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002130 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002131 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002132 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002133 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002134#define k_fifo_is_empty(fifo) \
2135 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002136
2137/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002138 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002139 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002140 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302141 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002142 * on each iteration of processing, a head container will be peeked,
2143 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002144 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002145 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002146 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002147 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002148 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002149 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002150 */
2151#define k_fifo_peek_head(fifo) \
2152 k_queue_peek_head((struct k_queue *) fifo)
2153
2154/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002155 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002156 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002157 * Return element from the tail of FIFO queue (without removing it). A usecase
2158 * for this is if elements of the FIFO queue are themselves containers. Then
2159 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002160 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002161 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002162 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002163 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002164 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002165 */
2166#define k_fifo_peek_tail(fifo) \
2167 k_queue_peek_tail((struct k_queue *) fifo)
2168
2169/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002170 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002171 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002172 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002173 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002174 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002175 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002176 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002177 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002178 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002179#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002180 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002181 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002182 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002183
Anas Nashif166f5192018-02-25 08:02:36 -06002184/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002185
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002186struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002187 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002188};
2189
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002190/**
2191 * @cond INTERNAL_HIDDEN
2192 */
2193
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002194#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002195 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002196 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002197 }
2198
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002199#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2200
Allan Stephensc98da842016-11-11 15:45:03 -05002201/**
2202 * INTERNAL_HIDDEN @endcond
2203 */
2204
2205/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002206 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002207 * @ingroup kernel_apis
2208 * @{
2209 */
2210
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002211/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002212 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002213 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002214 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002215 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002216 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002217 *
2218 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002219 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002220 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002221#define k_lifo_init(lifo) \
2222 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002223
2224/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002225 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002226 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002227 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002228 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2229 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002231 * @note Can be called by ISRs.
2232 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002233 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002234 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002235 *
2236 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002237 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002238 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002239#define k_lifo_put(lifo, data) \
2240 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002241
2242/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002243 * @brief Add an element to a LIFO queue.
2244 *
2245 * This routine adds a data item to @a lifo. There is an implicit
2246 * memory allocation from the calling thread's resource pool, which is
2247 * automatically freed when the item is removed.
2248 *
2249 * @note Can be called by ISRs.
2250 *
2251 * @param lifo Address of the LIFO.
2252 * @param data Address of the data item.
2253 *
2254 * @retval 0 on success
2255 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002256 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002257 */
2258#define k_lifo_alloc_put(lifo, data) \
2259 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2260
2261/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002262 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002264 * This routine removes a data item from @a lifo in a "last in, first out"
2265 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002267 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2268 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002269 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002270 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002271 * or one of the special values K_NO_WAIT and K_FOREVER.
2272 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002273 * @return Address of the data item if successful; NULL if returned
2274 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002275 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002276 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002277#define k_lifo_get(lifo, timeout) \
2278 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002279
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002280/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002281 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002282 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002283 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002284 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002285 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002286 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002287 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002288 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002289 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002290#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002291 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002292 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002293 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002294
Anas Nashif166f5192018-02-25 08:02:36 -06002295/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002296
2297/**
2298 * @cond INTERNAL_HIDDEN
2299 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302300#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002301
2302struct k_stack {
2303 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002304 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002305
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002306 _OBJECT_TRACING_NEXT_PTR(k_stack)
Andrew Boief3bee952018-05-02 17:44:39 -07002307 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002308};
2309
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002310#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002311 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002312 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002313 .base = stack_buffer, \
2314 .next = stack_buffer, \
2315 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002316 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002317 }
2318
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002319#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2320
Allan Stephensc98da842016-11-11 15:45:03 -05002321/**
2322 * INTERNAL_HIDDEN @endcond
2323 */
2324
2325/**
2326 * @defgroup stack_apis Stack APIs
2327 * @ingroup kernel_apis
2328 * @{
2329 */
2330
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002331/**
2332 * @brief Initialize a stack.
2333 *
2334 * This routine initializes a stack object, prior to its first use.
2335 *
2336 * @param stack Address of the stack.
2337 * @param buffer Address of array used to hold stacked values.
2338 * @param num_entries Maximum number of values that can be stacked.
2339 *
2340 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002341 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002342 */
Andrew Boief3bee952018-05-02 17:44:39 -07002343void k_stack_init(struct k_stack *stack,
Adithya Baglody28080d32018-10-15 11:48:51 +05302344 u32_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002345
2346
2347/**
2348 * @brief Initialize a stack.
2349 *
2350 * This routine initializes a stack object, prior to its first use. Internal
2351 * buffers will be allocated from the calling thread's resource pool.
2352 * This memory will be released if k_stack_cleanup() is called, or
2353 * userspace is enabled and the stack object loses all references to it.
2354 *
2355 * @param stack Address of the stack.
2356 * @param num_entries Maximum number of values that can be stacked.
2357 *
2358 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002359 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002360 */
2361
Adithya Baglody28080d32018-10-15 11:48:51 +05302362__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2363 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002364
2365/**
2366 * @brief Release a stack's allocated buffer
2367 *
2368 * If a stack object was given a dynamically allocated buffer via
2369 * k_stack_alloc_init(), this will free it. This function does nothing
2370 * if the buffer wasn't dynamically allocated.
2371 *
2372 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002373 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002374 */
2375void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002376
2377/**
2378 * @brief Push an element onto a stack.
2379 *
2380 * This routine adds a 32-bit value @a data to @a stack.
2381 *
2382 * @note Can be called by ISRs.
2383 *
2384 * @param stack Address of the stack.
2385 * @param data Value to push onto the stack.
2386 *
2387 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002388 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002389 */
Andrew Boiee8734462017-09-29 16:42:07 -07002390__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002391
2392/**
2393 * @brief Pop an element from a stack.
2394 *
2395 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2396 * manner and stores the value in @a data.
2397 *
2398 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2399 *
2400 * @param stack Address of the stack.
2401 * @param data Address of area to hold the value popped from the stack.
2402 * @param timeout Waiting period to obtain a value (in milliseconds),
2403 * or one of the special values K_NO_WAIT and K_FOREVER.
2404 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002405 * @retval 0 Element popped from stack.
2406 * @retval -EBUSY Returned without waiting.
2407 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002408 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002409 */
Andrew Boiee8734462017-09-29 16:42:07 -07002410__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002411
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002412/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002413 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002415 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002416 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002417 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002419 * @param name Name of the stack.
2420 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002421 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002422 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002423#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002424 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002425 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002426 struct k_stack name \
2427 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002428 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002429 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002430
Anas Nashif166f5192018-02-25 08:02:36 -06002431/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002432
Allan Stephens6bba9b02016-11-16 14:56:54 -05002433struct k_work;
2434
Allan Stephensc98da842016-11-11 15:45:03 -05002435/**
2436 * @defgroup workqueue_apis Workqueue Thread APIs
2437 * @ingroup kernel_apis
2438 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002439 */
2440
Allan Stephens6bba9b02016-11-16 14:56:54 -05002441/**
2442 * @typedef k_work_handler_t
2443 * @brief Work item handler function type.
2444 *
2445 * A work item's handler function is executed by a workqueue's thread
2446 * when the work item is processed by the workqueue.
2447 *
2448 * @param work Address of the work item.
2449 *
2450 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002451 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002452 */
2453typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002454
2455/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002456 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002457 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002458
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002459struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002460 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002461 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002462};
2463
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002464enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002465 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002466};
2467
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002468struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002469 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002470 k_work_handler_t handler;
2471 atomic_t flags[1];
2472};
2473
Allan Stephens6bba9b02016-11-16 14:56:54 -05002474struct k_delayed_work {
2475 struct k_work work;
2476 struct _timeout timeout;
2477 struct k_work_q *work_q;
2478};
2479
2480extern struct k_work_q k_sys_work_q;
2481
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002482/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002483 * INTERNAL_HIDDEN @endcond
2484 */
2485
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002486#define _K_WORK_INITIALIZER(work_handler) \
2487 { \
2488 ._reserved = NULL, \
2489 .handler = work_handler, \
2490 .flags = { 0 } \
2491 }
2492
2493#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2494
Allan Stephens6bba9b02016-11-16 14:56:54 -05002495/**
2496 * @brief Initialize a statically-defined work item.
2497 *
2498 * This macro can be used to initialize a statically-defined workqueue work
2499 * item, prior to its first use. For example,
2500 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002501 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002502 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002503 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002504 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002505 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002506 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002507#define K_WORK_DEFINE(work, work_handler) \
Andrew Boiec2e01df2018-11-12 15:16:54 -08002508 struct k_work work = _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002509
2510/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002511 * @brief Initialize a work item.
2512 *
2513 * This routine initializes a workqueue work item, prior to its first use.
2514 *
2515 * @param work Address of work item.
2516 * @param handler Function to invoke each time work item is processed.
2517 *
2518 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002519 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002520 */
2521static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2522{
Leandro Pereira0e23ad82018-05-29 10:42:17 -07002523 *work = (struct k_work)_K_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002524}
2525
2526/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002527 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002528 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002529 * This routine submits work item @a work to be processed by workqueue
2530 * @a work_q. If the work item is already pending in the workqueue's queue
2531 * as a result of an earlier submission, this routine has no effect on the
2532 * work item. If the work item has already been processed, or is currently
2533 * being processed, its work is considered complete and the work item can be
2534 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002535 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002536 * @warning
2537 * A submitted work item must not be modified until it has been processed
2538 * by the workqueue.
2539 *
2540 * @note Can be called by ISRs.
2541 *
2542 * @param work_q Address of workqueue.
2543 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002544 *
2545 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002546 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002547 */
2548static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2549 struct k_work *work)
2550{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002551 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002552 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002553 }
2554}
2555
2556/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002557 * @brief Submit a work item to a user mode workqueue
2558 *
David B. Kinder06d78352018-12-17 14:32:40 -08002559 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002560 * memory allocation is made from the caller's resource pool which is freed
2561 * once the worker thread consumes the k_work item. The workqueue
2562 * thread must have memory access to the k_work item being submitted. The caller
2563 * must have permission granted on the work_q parameter's queue object.
2564 *
2565 * Otherwise this works the same as k_work_submit_to_queue().
2566 *
2567 * @note Can be called by ISRs.
2568 *
2569 * @param work_q Address of workqueue.
2570 * @param work Address of work item.
2571 *
2572 * @retval -EBUSY if the work item was already in some workqueue
2573 * @retval -ENOMEM if no memory for thread resource pool allocation
2574 * @retval 0 Success
2575 * @req K-WORK-001
2576 */
2577static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2578 struct k_work *work)
2579{
2580 int ret = -EBUSY;
2581
2582 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2583 ret = k_queue_alloc_append(&work_q->queue, work);
2584
2585 /* Couldn't insert into the queue. Clear the pending bit
2586 * so the work item can be submitted again
2587 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002588 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002589 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2590 }
2591 }
2592
2593 return ret;
2594}
2595
2596/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002597 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002598 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002599 * This routine indicates if work item @a work is pending in a workqueue's
2600 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002601 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002602 * @note Can be called by ISRs.
2603 *
2604 * @param work Address of work item.
2605 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002606 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002607 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002608 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002609static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002610{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002611 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002612}
2613
2614/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002615 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002616 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002617 * This routine starts workqueue @a work_q. The workqueue spawns its work
2618 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002619 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002620 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002621 * @param stack Pointer to work queue thread's stack space, as defined by
2622 * K_THREAD_STACK_DEFINE()
2623 * @param stack_size Size of the work queue thread's stack (in bytes), which
2624 * should either be the same constant passed to
2625 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002626 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002627 *
2628 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002629 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002630 */
Andrew Boie507852a2017-07-25 18:47:07 -07002631extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002632 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002633 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002634
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002635/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002636 * @brief Start a workqueue in user mode
2637 *
2638 * This works identically to k_work_q_start() except it is callable from user
2639 * mode, and the worker thread created will run in user mode.
2640 * The caller must have permissions granted on both the work_q parameter's
2641 * thread and queue objects, and the same restrictions on priority apply as
2642 * k_thread_create().
2643 *
2644 * @param work_q Address of workqueue.
2645 * @param stack Pointer to work queue thread's stack space, as defined by
2646 * K_THREAD_STACK_DEFINE()
2647 * @param stack_size Size of the work queue thread's stack (in bytes), which
2648 * should either be the same constant passed to
2649 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2650 * @param prio Priority of the work queue's thread.
2651 *
2652 * @return N/A
2653 * @req K-WORK-001
2654 */
2655extern void k_work_q_user_start(struct k_work_q *work_q,
2656 k_thread_stack_t *stack,
2657 size_t stack_size, int prio);
2658
2659/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002660 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002661 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002662 * This routine initializes a workqueue delayed work item, prior to
2663 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002664 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002665 * @param work Address of delayed work item.
2666 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002667 *
2668 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002669 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002670 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002671extern void k_delayed_work_init(struct k_delayed_work *work,
2672 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002673
2674/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002675 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002676 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002677 * This routine schedules work item @a work to be processed by workqueue
2678 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002679 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002680 * Only when the countdown completes is the work item actually submitted to
2681 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002682 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002683 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002684 * counting down cancels the existing submission and restarts the
2685 * countdown using the new delay. Note that this behavior is
2686 * inherently subject to race conditions with the pre-existing
2687 * timeouts and work queue, so care must be taken to synchronize such
2688 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002689 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002690 * @warning
2691 * A delayed work item must not be modified until it has been processed
2692 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002693 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002694 * @note Can be called by ISRs.
2695 *
2696 * @param work_q Address of workqueue.
2697 * @param work Address of delayed work item.
2698 * @param delay Delay before submitting the work item (in milliseconds).
2699 *
2700 * @retval 0 Work item countdown started.
2701 * @retval -EINPROGRESS Work item is already pending.
2702 * @retval -EINVAL Work item is being processed or has completed its work.
2703 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002704 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002705 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002706extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2707 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002708 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002709
2710/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002711 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002712 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002713 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002714 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002715 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002716 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002717 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002718 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002719 * @param work Address of delayed work item.
2720 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002721 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002722 * @retval -EINPROGRESS Work item is already pending.
2723 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002724 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002725 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002726extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002727
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002728/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002729 * @brief Submit a work item to the system workqueue.
2730 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002731 * This routine submits work item @a work to be processed by the system
2732 * workqueue. If the work item is already pending in the workqueue's queue
2733 * as a result of an earlier submission, this routine has no effect on the
2734 * work item. If the work item has already been processed, or is currently
2735 * being processed, its work is considered complete and the work item can be
2736 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002737 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002738 * @warning
2739 * Work items submitted to the system workqueue should avoid using handlers
2740 * that block or yield since this may prevent the system workqueue from
2741 * processing other work items in a timely manner.
2742 *
2743 * @note Can be called by ISRs.
2744 *
2745 * @param work Address of work item.
2746 *
2747 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002748 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002749 */
2750static inline void k_work_submit(struct k_work *work)
2751{
2752 k_work_submit_to_queue(&k_sys_work_q, work);
2753}
2754
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002755/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002756 * @brief Submit a delayed work item to the system workqueue.
2757 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002758 * This routine schedules work item @a work to be processed by the system
2759 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002760 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002761 * Only when the countdown completes is the work item actually submitted to
2762 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002763 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002764 * Submitting a previously submitted delayed work item that is still
2765 * counting down cancels the existing submission and restarts the countdown
2766 * using the new delay. If the work item is currently pending on the
2767 * workqueue's queue because the countdown has completed it is too late to
2768 * resubmit the item, and resubmission fails without impacting the work item.
2769 * If the work item has already been processed, or is currently being processed,
2770 * its work is considered complete and the work item can be resubmitted.
2771 *
2772 * @warning
2773 * Work items submitted to the system workqueue should avoid using handlers
2774 * that block or yield since this may prevent the system workqueue from
2775 * processing other work items in a timely manner.
2776 *
2777 * @note Can be called by ISRs.
2778 *
2779 * @param work Address of delayed work item.
2780 * @param delay Delay before submitting the work item (in milliseconds).
2781 *
2782 * @retval 0 Work item countdown started.
2783 * @retval -EINPROGRESS Work item is already pending.
2784 * @retval -EINVAL Work item is being processed or has completed its work.
2785 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002786 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002787 */
2788static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002789 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002790{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002791 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002792}
2793
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002794/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002795 * @brief Get time remaining before a delayed work gets scheduled.
2796 *
2797 * This routine computes the (approximate) time remaining before a
2798 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002799 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002800 *
2801 * @param work Delayed work item.
2802 *
2803 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002804 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002805 */
Kumar Galacc334c72017-04-21 10:55:34 -05002806static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002807{
Andy Ross52e444b2018-09-28 09:06:37 -07002808 return __ticks_to_ms(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02002809}
2810
Anas Nashif166f5192018-02-25 08:02:36 -06002811/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002812/**
Anas Nashifce78d162018-05-24 12:43:11 -05002813 * @defgroup mutex_apis Mutex APIs
2814 * @ingroup kernel_apis
2815 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002816 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002817
Anas Nashifce78d162018-05-24 12:43:11 -05002818/**
2819 * Mutex Structure
2820 * @ingroup mutex_apis
2821 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002822struct k_mutex {
2823 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002824 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002825 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002826 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002827 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002828
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002829 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002830};
2831
Anas Nashifce78d162018-05-24 12:43:11 -05002832/**
2833 * @cond INTERNAL_HIDDEN
2834 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002835#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002836 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002837 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002838 .owner = NULL, \
2839 .lock_count = 0, \
2840 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002841 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002842 }
2843
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002844#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2845
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002846/**
Allan Stephensc98da842016-11-11 15:45:03 -05002847 * INTERNAL_HIDDEN @endcond
2848 */
2849
2850/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002851 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002853 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002854 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002855 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002857 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002858 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002859 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002860#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002861 struct k_mutex name \
2862 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002863 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002864
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002865/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002866 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002868 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002870 * Upon completion, the mutex is available and does not have an owner.
2871 *
2872 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002873 *
2874 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002875 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002876 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002877__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002878
2879/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002880 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002882 * This routine locks @a mutex. If the mutex is locked by another thread,
2883 * the calling thread waits until the mutex becomes available or until
2884 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002886 * A thread is permitted to lock a mutex it has already locked. The operation
2887 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002888 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002889 * @param mutex Address of the mutex.
2890 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002891 * or one of the special values K_NO_WAIT and K_FOREVER.
2892 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002893 * @retval 0 Mutex locked.
2894 * @retval -EBUSY Returned without waiting.
2895 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002896 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002897 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002898__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002899
2900/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002901 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002902 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002903 * This routine unlocks @a mutex. The mutex must already be locked by the
2904 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002905 *
2906 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002907 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002908 * thread.
2909 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002910 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002911 *
2912 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002913 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002914 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002915__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002916
Allan Stephensc98da842016-11-11 15:45:03 -05002917/**
Anas Nashif166f5192018-02-25 08:02:36 -06002918 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002919 */
2920
2921/**
2922 * @cond INTERNAL_HIDDEN
2923 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002924
2925struct k_sem {
2926 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05302927 u32_t count;
2928 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002929 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002930
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002931 _OBJECT_TRACING_NEXT_PTR(k_sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002932};
2933
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002934#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002935 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002936 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002937 .count = initial_count, \
2938 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002939 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002940 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002941 }
2942
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002943#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2944
Allan Stephensc98da842016-11-11 15:45:03 -05002945/**
2946 * INTERNAL_HIDDEN @endcond
2947 */
2948
2949/**
2950 * @defgroup semaphore_apis Semaphore APIs
2951 * @ingroup kernel_apis
2952 * @{
2953 */
2954
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002955/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002956 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002957 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002958 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002960 * @param sem Address of the semaphore.
2961 * @param initial_count Initial semaphore count.
2962 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002963 *
2964 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002965 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002966 */
Andrew Boie99280232017-09-29 14:17:47 -07002967__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2968 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002969
2970/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002971 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002973 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002975 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2976 *
2977 * @param sem Address of the semaphore.
2978 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002979 * or one of the special values K_NO_WAIT and K_FOREVER.
2980 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002981 * @note When porting code from the nanokernel legacy API to the new API, be
2982 * careful with the return value of this function. The return value is the
2983 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2984 * non-zero means failure, while the nano_sem_take family returns 1 for success
2985 * and 0 for failure.
2986 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002987 * @retval 0 Semaphore taken.
2988 * @retval -EBUSY Returned without waiting.
2989 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002990 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002991 */
Andrew Boie99280232017-09-29 14:17:47 -07002992__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002993
2994/**
2995 * @brief Give a semaphore.
2996 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002997 * This routine gives @a sem, unless the semaphore is already at its maximum
2998 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002999 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003000 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003001 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003002 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003003 *
3004 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003005 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003006 */
Andrew Boie99280232017-09-29 14:17:47 -07003007__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003008
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003009/**
3010 * @brief Reset a semaphore's count to zero.
3011 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003012 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003014 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003015 *
3016 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003017 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003018 */
Andrew Boie990bf162017-10-03 12:36:49 -07003019__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003020
Anas Nashif954d5502018-02-25 08:37:28 -06003021/**
3022 * @internal
3023 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003024static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003025{
3026 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003027}
3028
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003029/**
3030 * @brief Get a semaphore's count.
3031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003032 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003033 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003034 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003035 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003036 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003037 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003038 */
Andrew Boie990bf162017-10-03 12:36:49 -07003039__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003040
Anas Nashif954d5502018-02-25 08:37:28 -06003041/**
3042 * @internal
3043 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003044static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003045{
3046 return sem->count;
3047}
3048
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003049/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003050 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003051 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003052 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003053 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003054 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003055 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003056 * @param name Name of the semaphore.
3057 * @param initial_count Initial semaphore count.
3058 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003059 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003060 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003061#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003062 struct k_sem name \
3063 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003064 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303065 BUILD_ASSERT(((count_limit) != 0) && \
3066 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003067
Anas Nashif166f5192018-02-25 08:02:36 -06003068/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003069
3070/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003071 * @defgroup msgq_apis Message Queue APIs
3072 * @ingroup kernel_apis
3073 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003074 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003075
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003076/**
3077 * @brief Message Queue Structure
3078 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003079struct k_msgq {
3080 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003081 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003082 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003083 char *buffer_start;
3084 char *buffer_end;
3085 char *read_ptr;
3086 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003087 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003088
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003089 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Andrew Boie0fe789f2018-04-12 18:35:56 -07003090 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003091};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003092/**
3093 * @cond INTERNAL_HIDDEN
3094 */
3095
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003096
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003097#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003098 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003099 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003100 .max_msgs = q_max_msgs, \
3101 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003102 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003103 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003104 .read_ptr = q_buffer, \
3105 .write_ptr = q_buffer, \
3106 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003107 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003108 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003109#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003110/**
3111 * INTERNAL_HIDDEN @endcond
3112 */
3113
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003114
Andrew Boie0fe789f2018-04-12 18:35:56 -07003115#define K_MSGQ_FLAG_ALLOC BIT(0)
3116
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003117/**
3118 * @brief Message Queue Attributes
3119 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303120struct k_msgq_attrs {
3121 size_t msg_size;
3122 u32_t max_msgs;
3123 u32_t used_msgs;
3124};
3125
Allan Stephensc98da842016-11-11 15:45:03 -05003126
3127/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003128 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003130 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3131 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003132 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3133 * message is similarly aligned to this boundary, @a q_msg_size must also be
3134 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003135 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003136 * The message queue can be accessed outside the module where it is defined
3137 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003138 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003139 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003140 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003141 * @param q_name Name of the message queue.
3142 * @param q_msg_size Message size (in bytes).
3143 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003144 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003145 *
3146 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003147 */
3148#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003149 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003150 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003151 struct k_msgq q_name \
3152 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003153 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003154 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003155
Peter Mitsisd7a37502016-10-13 11:37:40 -04003156/**
3157 * @brief Initialize a message queue.
3158 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003159 * This routine initializes a message queue object, prior to its first use.
3160 *
Allan Stephensda827222016-11-09 14:23:58 -06003161 * The message queue's ring buffer must contain space for @a max_msgs messages,
3162 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3163 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3164 * that each message is similarly aligned to this boundary, @a q_msg_size
3165 * must also be a multiple of N.
3166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003167 * @param q Address of the message queue.
3168 * @param buffer Pointer to ring buffer that holds queued messages.
3169 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003170 * @param max_msgs Maximum number of messages that can be queued.
3171 *
3172 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003173 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003174 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003175void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3176 u32_t max_msgs);
3177
3178/**
3179 * @brief Initialize a message queue.
3180 *
3181 * This routine initializes a message queue object, prior to its first use,
3182 * allocating its internal ring buffer from the calling thread's resource
3183 * pool.
3184 *
3185 * Memory allocated for the ring buffer can be released by calling
3186 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3187 * all of its references.
3188 *
3189 * @param q Address of the message queue.
3190 * @param msg_size Message size (in bytes).
3191 * @param max_msgs Maximum number of messages that can be queued.
3192 *
3193 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3194 * thread's resource pool, or -EINVAL if the size parameters cause
3195 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003196 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003197 */
3198__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3199 u32_t max_msgs);
3200
3201
3202void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003203
3204/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003205 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003206 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003207 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003208 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003209 * @note Can be called by ISRs.
3210 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003211 * @param q Address of the message queue.
3212 * @param data Pointer to the message.
3213 * @param timeout Waiting period to add the message (in milliseconds),
3214 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003215 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003216 * @retval 0 Message sent.
3217 * @retval -ENOMSG Returned without waiting or queue purged.
3218 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003219 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003220 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003221__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003222
3223/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003224 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003225 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003226 * This routine receives a message from message queue @a q in a "first in,
3227 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003228 *
Allan Stephensc98da842016-11-11 15:45:03 -05003229 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003231 * @param q Address of the message queue.
3232 * @param data Address of area to hold the received message.
3233 * @param timeout Waiting period to receive the message (in milliseconds),
3234 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003235 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003236 * @retval 0 Message received.
3237 * @retval -ENOMSG Returned without waiting.
3238 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003239 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003240 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003241__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003242
3243/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003244 * @brief Peek/read a message from a message queue.
3245 *
3246 * This routine reads a message from message queue @a q in a "first in,
3247 * first out" manner and leaves the message in the queue.
3248 *
3249 * @note Can be called by ISRs.
3250 *
3251 * @param q Address of the message queue.
3252 * @param data Address of area to hold the message read from the queue.
3253 *
3254 * @retval 0 Message read.
3255 * @retval -ENOMSG Returned when the queue has no message.
3256 * @req K-MSGQ-002
3257 */
3258__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3259
3260/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003261 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003262 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003263 * This routine discards all unreceived messages in a message queue's ring
3264 * buffer. Any threads that are blocked waiting to send a message to the
3265 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003267 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003268 *
3269 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003270 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003271 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003272__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003273
Peter Mitsis67be2492016-10-07 11:44:34 -04003274/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003275 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003276 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003277 * This routine returns the number of unused entries in a message queue's
3278 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003279 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003280 * @param q Address of the message queue.
3281 *
3282 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003283 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003284 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003285__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3286
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303287/**
3288 * @brief Get basic attributes of a message queue.
3289 *
3290 * This routine fetches basic attributes of message queue into attr argument.
3291 *
3292 * @param q Address of the message queue.
3293 * @param attrs pointer to message queue attribute structure.
3294 *
3295 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003296 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303297 */
3298__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3299
3300
Andrew Boie82edb6e2017-10-02 10:53:06 -07003301static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003302{
3303 return q->max_msgs - q->used_msgs;
3304}
3305
Peter Mitsisd7a37502016-10-13 11:37:40 -04003306/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003307 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003308 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003309 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003310 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003311 * @param q Address of the message queue.
3312 *
3313 * @return Number of messages.
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 u32_t k_msgq_num_used_get(struct k_msgq *q);
3317
3318static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003319{
3320 return q->used_msgs;
3321}
3322
Anas Nashif166f5192018-02-25 08:02:36 -06003323/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003324
3325/**
3326 * @defgroup mem_pool_apis Memory Pool APIs
3327 * @ingroup kernel_apis
3328 * @{
3329 */
3330
Andy Ross73cb9582017-05-09 10:42:39 -07003331/* Note on sizing: the use of a 20 bit field for block means that,
3332 * assuming a reasonable minimum block size of 16 bytes, we're limited
3333 * to 16M of memory managed by a single pool. Long term it would be
3334 * good to move to a variable bit size based on configuration.
3335 */
3336struct k_mem_block_id {
3337 u32_t pool : 8;
3338 u32_t level : 4;
3339 u32_t block : 20;
3340};
3341
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003342struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003343 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003344 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003345};
3346
Anas Nashif166f5192018-02-25 08:02:36 -06003347/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003348
3349/**
3350 * @defgroup mailbox_apis Mailbox APIs
3351 * @ingroup kernel_apis
3352 * @{
3353 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003354
3355struct k_mbox_msg {
3356 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003357 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003358 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003359 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003360 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003361 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003362 /** sender's message data buffer */
3363 void *tx_data;
3364 /** internal use only - needed for legacy API support */
3365 void *_rx_data;
3366 /** message data block descriptor */
3367 struct k_mem_block tx_block;
3368 /** source thread id */
3369 k_tid_t rx_source_thread;
3370 /** target thread id */
3371 k_tid_t tx_target_thread;
3372 /** internal use only - thread waiting on send (may be a dummy) */
3373 k_tid_t _syncing_thread;
3374#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3375 /** internal use only - semaphore used during asynchronous send */
3376 struct k_sem *_async_sem;
3377#endif
3378};
3379
3380struct k_mbox {
3381 _wait_q_t tx_msg_queue;
3382 _wait_q_t rx_msg_queue;
3383
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003384 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003385};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003386/**
3387 * @cond INTERNAL_HIDDEN
3388 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003389
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003390#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003391 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003392 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3393 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003394 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003395 }
3396
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003397#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3398
Peter Mitsis12092702016-10-14 12:57:23 -04003399/**
Allan Stephensc98da842016-11-11 15:45:03 -05003400 * INTERNAL_HIDDEN @endcond
3401 */
3402
3403/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003404 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003406 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003407 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003408 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003409 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003410 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003411 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003412 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003413#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003414 struct k_mbox name \
3415 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003416 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003417
Peter Mitsis12092702016-10-14 12:57:23 -04003418/**
3419 * @brief Initialize a mailbox.
3420 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003421 * This routine initializes a mailbox object, prior to its first use.
3422 *
3423 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003424 *
3425 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003426 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003427 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003428extern void k_mbox_init(struct k_mbox *mbox);
3429
Peter Mitsis12092702016-10-14 12:57:23 -04003430/**
3431 * @brief Send a mailbox message in a synchronous manner.
3432 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003433 * This routine sends a message to @a mbox and waits for a receiver to both
3434 * receive and process it. The message data may be in a buffer, in a memory
3435 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003436 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003437 * @param mbox Address of the mailbox.
3438 * @param tx_msg Address of the transmit message descriptor.
3439 * @param timeout Waiting period for the message to be received (in
3440 * milliseconds), or one of the special values K_NO_WAIT
3441 * and K_FOREVER. Once the message has been received,
3442 * this routine waits as long as necessary for the message
3443 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003444 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003445 * @retval 0 Message sent.
3446 * @retval -ENOMSG Returned without waiting.
3447 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003448 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003449 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003450extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003451 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003452
Peter Mitsis12092702016-10-14 12:57:23 -04003453/**
3454 * @brief Send a mailbox message in an asynchronous manner.
3455 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003456 * This routine sends a message to @a mbox without waiting for a receiver
3457 * to process it. The message data may be in a buffer, in a memory pool block,
3458 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3459 * will be given when the message has been both received and completely
3460 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003461 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003462 * @param mbox Address of the mailbox.
3463 * @param tx_msg Address of the transmit message descriptor.
3464 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003465 *
3466 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003467 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003468 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003469extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003470 struct k_sem *sem);
3471
Peter Mitsis12092702016-10-14 12:57:23 -04003472/**
3473 * @brief Receive a mailbox message.
3474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003475 * This routine receives a message from @a mbox, then optionally retrieves
3476 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003478 * @param mbox Address of the mailbox.
3479 * @param rx_msg Address of the receive message descriptor.
3480 * @param buffer Address of the buffer to receive data, or NULL to defer data
3481 * retrieval and message disposal until later.
3482 * @param timeout Waiting period for a message to be received (in
3483 * milliseconds), or one of the special values K_NO_WAIT
3484 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003485 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003486 * @retval 0 Message received.
3487 * @retval -ENOMSG Returned without waiting.
3488 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003489 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003490 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003491extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003492 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003493
3494/**
3495 * @brief Retrieve mailbox message data into a buffer.
3496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003497 * This routine completes the processing of a received message by retrieving
3498 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003499 *
3500 * Alternatively, this routine can be used to dispose of a received message
3501 * without retrieving its data.
3502 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003503 * @param rx_msg Address of the receive message descriptor.
3504 * @param buffer Address of the buffer to receive data, or NULL to discard
3505 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003506 *
3507 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003508 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003509 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003510extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003511
3512/**
3513 * @brief Retrieve mailbox message data into a memory pool block.
3514 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003515 * This routine completes the processing of a received message by retrieving
3516 * its data into a memory pool block, then disposing of the message.
3517 * The memory pool block that results from successful retrieval must be
3518 * returned to the pool once the data has been processed, even in cases
3519 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003520 *
3521 * Alternatively, this routine can be used to dispose of a received message
3522 * without retrieving its data. In this case there is no need to return a
3523 * memory pool block to the pool.
3524 *
3525 * This routine allocates a new memory pool block for the data only if the
3526 * data is not already in one. If a new block cannot be allocated, the routine
3527 * returns a failure code and the received message is left unchanged. This
3528 * permits the caller to reattempt data retrieval at a later time or to dispose
3529 * of the received message without retrieving its data.
3530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003531 * @param rx_msg Address of a receive message descriptor.
3532 * @param pool Address of memory pool, or NULL to discard data.
3533 * @param block Address of the area to hold memory pool block info.
3534 * @param timeout Waiting period to wait for a memory pool block (in
3535 * milliseconds), or one of the special values K_NO_WAIT
3536 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003537 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003538 * @retval 0 Data retrieved.
3539 * @retval -ENOMEM Returned without waiting.
3540 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003541 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003542 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003543extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003544 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003545 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003546
Anas Nashif166f5192018-02-25 08:02:36 -06003547/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003548
3549/**
Anas Nashifce78d162018-05-24 12:43:11 -05003550 * @defgroup pipe_apis Pipe APIs
3551 * @ingroup kernel_apis
3552 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003553 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003554
Anas Nashifce78d162018-05-24 12:43:11 -05003555/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003556struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003557 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3558 size_t size; /**< Buffer size */
3559 size_t bytes_used; /**< # bytes used in buffer */
3560 size_t read_index; /**< Where in buffer to read from */
3561 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003562
3563 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003564 _wait_q_t readers; /**< Reader wait queue */
3565 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003566 } wait_q;
3567
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003568 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Anas Nashifce78d162018-05-24 12:43:11 -05003569 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003570};
3571
Anas Nashifce78d162018-05-24 12:43:11 -05003572/**
3573 * @cond INTERNAL_HIDDEN
3574 */
3575#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3576
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003577#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003578 { \
3579 .buffer = pipe_buffer, \
3580 .size = pipe_buffer_size, \
3581 .bytes_used = 0, \
3582 .read_index = 0, \
3583 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003584 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3585 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003586 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003587 }
3588
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003589#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3590
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003591/**
Allan Stephensc98da842016-11-11 15:45:03 -05003592 * INTERNAL_HIDDEN @endcond
3593 */
3594
3595/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003596 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003597 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003598 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003599 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003600 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003601 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003602 * @param name Name of the pipe.
3603 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3604 * or zero if no ring buffer is used.
3605 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003606 *
3607 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003608 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003609#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3610 static unsigned char __kernel_noinit __aligned(pipe_align) \
3611 _k_pipe_buf_##name[pipe_buffer_size]; \
3612 struct k_pipe name \
3613 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003614 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003615
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003616/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003617 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003619 * This routine initializes a pipe object, prior to its first use.
3620 *
3621 * @param pipe Address of the pipe.
3622 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3623 * is used.
3624 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3625 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003626 *
3627 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003628 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003629 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003630void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3631
3632/**
3633 * @brief Release a pipe's allocated buffer
3634 *
3635 * If a pipe object was given a dynamically allocated buffer via
3636 * k_pipe_alloc_init(), this will free it. This function does nothing
3637 * if the buffer wasn't dynamically allocated.
3638 *
3639 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003640 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003641 */
3642void k_pipe_cleanup(struct k_pipe *pipe);
3643
3644/**
3645 * @brief Initialize a pipe and allocate a buffer for it
3646 *
3647 * Storage for the buffer region will be allocated from the calling thread's
3648 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3649 * or userspace is enabled and the pipe object loses all references to it.
3650 *
3651 * This function should only be called on uninitialized pipe objects.
3652 *
3653 * @param pipe Address of the pipe.
3654 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3655 * buffer is used.
3656 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003657 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003658 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003659 */
3660__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003661
3662/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003663 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003664 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003665 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003667 * @param pipe Address of the pipe.
3668 * @param data Address of data to write.
3669 * @param bytes_to_write Size of data (in bytes).
3670 * @param bytes_written Address of area to hold the number of bytes written.
3671 * @param min_xfer Minimum number of bytes to write.
3672 * @param timeout Waiting period to wait for the data to be written (in
3673 * milliseconds), or one of the special values K_NO_WAIT
3674 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003675 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003676 * @retval 0 At least @a min_xfer bytes of data were written.
3677 * @retval -EIO Returned without waiting; zero data bytes were written.
3678 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003679 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003680 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003681 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003682__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3683 size_t bytes_to_write, size_t *bytes_written,
3684 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003685
3686/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003687 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003688 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003689 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003691 * @param pipe Address of the pipe.
3692 * @param data Address to place the data read from pipe.
3693 * @param bytes_to_read Maximum number of data bytes to read.
3694 * @param bytes_read Address of area to hold the number of bytes read.
3695 * @param min_xfer Minimum number of data bytes to read.
3696 * @param timeout Waiting period to wait for the data to be read (in
3697 * milliseconds), or one of the special values K_NO_WAIT
3698 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003699 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003700 * @retval 0 At least @a min_xfer bytes of data were read.
3701 * @retval -EIO Returned without waiting; zero data bytes were read.
3702 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003703 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003704 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003705 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003706__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3707 size_t bytes_to_read, size_t *bytes_read,
3708 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003709
3710/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003711 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003712 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003713 * This routine writes the data contained in a memory block to @a pipe.
3714 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003715 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003717 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003718 * @param block Memory block containing data to send
3719 * @param size Number of data bytes in memory block to send
3720 * @param sem Semaphore to signal upon completion (else NULL)
3721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003722 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003723 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003724 */
3725extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3726 size_t size, struct k_sem *sem);
3727
Anas Nashif166f5192018-02-25 08:02:36 -06003728/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003729
Allan Stephensc98da842016-11-11 15:45:03 -05003730/**
3731 * @cond INTERNAL_HIDDEN
3732 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003733
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003734struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003735 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003736 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003737 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003738 char *buffer;
3739 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003740 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003741
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003742 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003743};
3744
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003745#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003746 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003747 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003748 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003749 .num_blocks = slab_num_blocks, \
3750 .block_size = slab_block_size, \
3751 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003752 .free_list = NULL, \
3753 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003754 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003755 }
3756
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003757#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3758
3759
Peter Mitsis578f9112016-10-07 13:50:31 -04003760/**
Allan Stephensc98da842016-11-11 15:45:03 -05003761 * INTERNAL_HIDDEN @endcond
3762 */
3763
3764/**
3765 * @defgroup mem_slab_apis Memory Slab APIs
3766 * @ingroup kernel_apis
3767 * @{
3768 */
3769
3770/**
Allan Stephensda827222016-11-09 14:23:58 -06003771 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003772 *
Allan Stephensda827222016-11-09 14:23:58 -06003773 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003774 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003775 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3776 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003777 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003778 *
Allan Stephensda827222016-11-09 14:23:58 -06003779 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003780 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003781 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003782 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003783 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003784 * @param name Name of the memory slab.
3785 * @param slab_block_size Size of each memory block (in bytes).
3786 * @param slab_num_blocks Number memory blocks.
3787 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003788 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003789 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003790#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3791 char __noinit __aligned(slab_align) \
3792 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3793 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003794 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003795 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003796 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003797
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003798/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003799 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003801 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003802 *
Allan Stephensda827222016-11-09 14:23:58 -06003803 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3804 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3805 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3806 * To ensure that each memory block is similarly aligned to this boundary,
3807 * @a slab_block_size must also be a multiple of N.
3808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003809 * @param slab Address of the memory slab.
3810 * @param buffer Pointer to buffer used for the memory blocks.
3811 * @param block_size Size of each memory block (in bytes).
3812 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003813 *
3814 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003815 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003816 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003817extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003818 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003819
3820/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003821 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003823 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003825 * @param slab Address of the memory slab.
3826 * @param mem Pointer to block address area.
3827 * @param timeout Maximum time to wait for operation to complete
3828 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3829 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003830 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003831 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003832 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003833 * @retval -ENOMEM Returned without waiting.
3834 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003835 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003836 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003837extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003838 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003839
3840/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003841 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003842 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003843 * This routine releases a previously allocated memory block back to its
3844 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003846 * @param slab Address of the memory slab.
3847 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003848 *
3849 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003850 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003851 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003852extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003853
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003854/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003855 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003857 * This routine gets the number of memory blocks that are currently
3858 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003860 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003862 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003863 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003864 */
Kumar Galacc334c72017-04-21 10:55:34 -05003865static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003866{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003867 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003868}
3869
Peter Mitsisc001aa82016-10-13 13:53:37 -04003870/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003871 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003872 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003873 * This routine gets the number of memory blocks that are currently
3874 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003876 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003878 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003879 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04003880 */
Kumar Galacc334c72017-04-21 10:55:34 -05003881static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003882{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003883 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003884}
3885
Anas Nashif166f5192018-02-25 08:02:36 -06003886/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003887
3888/**
3889 * @cond INTERNAL_HIDDEN
3890 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003891
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003892struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003893 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003894 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003895};
3896
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003897/**
Allan Stephensc98da842016-11-11 15:45:03 -05003898 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003899 */
3900
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003901/**
Allan Stephensc98da842016-11-11 15:45:03 -05003902 * @addtogroup mem_pool_apis
3903 * @{
3904 */
3905
3906/**
3907 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003909 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3910 * long. The memory pool allows blocks to be repeatedly partitioned into
3911 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003912 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003913 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003914 * If the pool is to be accessed outside the module where it is defined, it
3915 * can be declared via
3916 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003917 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003918 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003919 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003920 * @param minsz Size of the smallest blocks in the pool (in bytes).
3921 * @param maxsz Size of the largest blocks in the pool (in bytes).
3922 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003923 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003924 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003925 */
Andy Ross73cb9582017-05-09 10:42:39 -07003926#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3927 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3928 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003929 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003930 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003931 .base = { \
3932 .buf = _mpool_buf_##name, \
3933 .max_sz = maxsz, \
3934 .n_max = nmax, \
3935 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3936 .levels = _mpool_lvls_##name, \
3937 .flags = SYS_MEM_POOL_KERNEL \
3938 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003939 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003940
Peter Mitsis937042c2016-10-13 13:18:26 -04003941/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003942 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003943 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003944 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003946 * @param pool Address of the memory pool.
3947 * @param block Pointer to block descriptor for the allocated memory.
3948 * @param size Amount of memory to allocate (in bytes).
3949 * @param timeout Maximum time to wait for operation to complete
3950 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3951 * or K_FOREVER to wait as long as necessary.
3952 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003953 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003954 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003955 * @retval -ENOMEM Returned without waiting.
3956 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003957 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04003958 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003959extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003960 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003961
3962/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07003963 * @brief Allocate memory from a memory pool with malloc() semantics
3964 *
3965 * Such memory must be released using k_free().
3966 *
3967 * @param pool Address of the memory pool.
3968 * @param size Amount of memory to allocate (in bytes).
3969 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003970 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07003971 */
3972extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
3973
3974/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003975 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003977 * This routine releases a previously allocated memory block back to its
3978 * memory pool.
3979 *
3980 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003981 *
3982 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003983 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04003984 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003985extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003986
3987/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003988 * @brief Free memory allocated from a memory pool.
3989 *
3990 * This routine releases a previously allocated memory block back to its
3991 * memory pool
3992 *
3993 * @param id Memory block identifier.
3994 *
3995 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003996 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003997 */
3998extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3999
4000/**
Anas Nashif166f5192018-02-25 08:02:36 -06004001 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004002 */
4003
4004/**
4005 * @defgroup heap_apis Heap Memory Pool APIs
4006 * @ingroup kernel_apis
4007 * @{
4008 */
4009
4010/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004011 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004013 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004014 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004016 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004018 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004019 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004020 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004021extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004022
4023/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004024 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004025 *
4026 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004027 * returned must have been allocated from the heap memory pool or
4028 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004029 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004030 * If @a ptr is NULL, no operation is performed.
4031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004032 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004033 *
4034 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004035 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004036 */
4037extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004038
Allan Stephensc98da842016-11-11 15:45:03 -05004039/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004040 * @brief Allocate memory from heap, array style
4041 *
4042 * This routine provides traditional calloc() semantics. Memory is
4043 * allocated from the heap memory pool and zeroed.
4044 *
4045 * @param nmemb Number of elements in the requested array
4046 * @param size Size of each array element (in bytes).
4047 *
4048 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004049 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004050 */
4051extern void *k_calloc(size_t nmemb, size_t size);
4052
Anas Nashif166f5192018-02-25 08:02:36 -06004053/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004054
Benjamin Walshacc68c12017-01-29 18:57:45 -05004055/* polling API - PRIVATE */
4056
Benjamin Walshb0179862017-02-02 16:39:57 -05004057#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004058#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004059#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004060#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004061#endif
4062
Benjamin Walshacc68c12017-01-29 18:57:45 -05004063/* private - implementation data created as needed, per-type */
4064struct _poller {
4065 struct k_thread *thread;
Flavio Ceolin76b35182018-12-16 12:48:29 -08004066 volatile bool is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004067};
4068
4069/* private - types bit positions */
4070enum _poll_types_bits {
4071 /* can be used to ignore an event */
4072 _POLL_TYPE_IGNORE,
4073
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004074 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004075 _POLL_TYPE_SIGNAL,
4076
4077 /* semaphore availability */
4078 _POLL_TYPE_SEM_AVAILABLE,
4079
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004080 /* queue/fifo/lifo data availability */
4081 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004082
4083 _POLL_NUM_TYPES
4084};
4085
4086#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4087
4088/* private - states bit positions */
4089enum _poll_states_bits {
4090 /* default state when creating event */
4091 _POLL_STATE_NOT_READY,
4092
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004093 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004094 _POLL_STATE_SIGNALED,
4095
4096 /* semaphore is available */
4097 _POLL_STATE_SEM_AVAILABLE,
4098
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004099 /* data is available to read on queue/fifo/lifo */
4100 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004101
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004102 /* queue/fifo/lifo wait was cancelled */
4103 _POLL_STATE_CANCELLED,
4104
Benjamin Walshacc68c12017-01-29 18:57:45 -05004105 _POLL_NUM_STATES
4106};
4107
4108#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4109
4110#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004111 (32 - (0 \
4112 + 8 /* tag */ \
4113 + _POLL_NUM_TYPES \
4114 + _POLL_NUM_STATES \
4115 + 1 /* modes */ \
4116 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004117
Benjamin Walshacc68c12017-01-29 18:57:45 -05004118/* end of polling API - PRIVATE */
4119
4120
4121/**
4122 * @defgroup poll_apis Async polling APIs
4123 * @ingroup kernel_apis
4124 * @{
4125 */
4126
4127/* Public polling API */
4128
4129/* public - values for k_poll_event.type bitfield */
4130#define K_POLL_TYPE_IGNORE 0
4131#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4132#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004133#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4134#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004135
4136/* public - polling modes */
4137enum k_poll_modes {
4138 /* polling thread does not take ownership of objects when available */
4139 K_POLL_MODE_NOTIFY_ONLY = 0,
4140
4141 K_POLL_NUM_MODES
4142};
4143
4144/* public - values for k_poll_event.state bitfield */
4145#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004146#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4147#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004148#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4149#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004150#define K_POLL_STATE_CANCELLED _POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004151
4152/* public - poll signal object */
4153struct k_poll_signal {
4154 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004155 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004156
4157 /*
4158 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4159 * user resets it to 0.
4160 */
4161 unsigned int signaled;
4162
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004163 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004164 int result;
4165};
4166
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004167#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004168 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004169 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004170 .signaled = 0, \
4171 .result = 0, \
4172 }
4173
4174struct k_poll_event {
4175 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004176 sys_dnode_t _node;
4177
4178 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004179 struct _poller *poller;
4180
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004181 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004182 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004183
Benjamin Walshacc68c12017-01-29 18:57:45 -05004184 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004185 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004186
4187 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004188 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004189
4190 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004191 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004192
4193 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004194 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004195
4196 /* per-type data */
4197 union {
4198 void *obj;
4199 struct k_poll_signal *signal;
4200 struct k_sem *sem;
4201 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004202 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004203 };
4204};
4205
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004206#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004207 { \
4208 .poller = NULL, \
4209 .type = event_type, \
4210 .state = K_POLL_STATE_NOT_READY, \
4211 .mode = event_mode, \
4212 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004213 { .obj = event_obj }, \
4214 }
4215
4216#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4217 event_tag) \
4218 { \
4219 .type = event_type, \
4220 .tag = event_tag, \
4221 .state = K_POLL_STATE_NOT_READY, \
4222 .mode = event_mode, \
4223 .unused = 0, \
4224 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004225 }
4226
4227/**
4228 * @brief Initialize one struct k_poll_event instance
4229 *
4230 * After this routine is called on a poll event, the event it ready to be
4231 * placed in an event array to be passed to k_poll().
4232 *
4233 * @param event The event to initialize.
4234 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4235 * values. Only values that apply to the same object being polled
4236 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4237 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004238 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004239 * @param obj Kernel object or poll signal.
4240 *
4241 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004242 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004243 */
4244
Kumar Galacc334c72017-04-21 10:55:34 -05004245extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004246 int mode, void *obj);
4247
4248/**
4249 * @brief Wait for one or many of multiple poll events to occur
4250 *
4251 * This routine allows a thread to wait concurrently for one or many of
4252 * multiple poll events to have occurred. Such events can be a kernel object
4253 * being available, like a semaphore, or a poll signal event.
4254 *
4255 * When an event notifies that a kernel object is available, the kernel object
4256 * is not "given" to the thread calling k_poll(): it merely signals the fact
4257 * that the object was available when the k_poll() call was in effect. Also,
4258 * all threads trying to acquire an object the regular way, i.e. by pending on
4259 * the object, have precedence over the thread polling on the object. This
4260 * means that the polling thread will never get the poll event on an object
4261 * until the object becomes available and its pend queue is empty. For this
4262 * reason, the k_poll() call is more effective when the objects being polled
4263 * only have one thread, the polling thread, trying to acquire them.
4264 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004265 * When k_poll() returns 0, the caller should loop on all the events that were
4266 * passed to k_poll() and check the state field for the values that were
4267 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004268 *
4269 * Before being reused for another call to k_poll(), the user has to reset the
4270 * state field to K_POLL_STATE_NOT_READY.
4271 *
Andrew Boie3772f772018-05-07 16:52:57 -07004272 * When called from user mode, a temporary memory allocation is required from
4273 * the caller's resource pool.
4274 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004275 * @param events An array of pointers to events to be polled for.
4276 * @param num_events The number of events in the array.
4277 * @param timeout Waiting period for an event to be ready (in milliseconds),
4278 * or one of the special values K_NO_WAIT and K_FOREVER.
4279 *
4280 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004281 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004282 * @retval -EINTR Polling has been interrupted, e.g. with
4283 * k_queue_cancel_wait(). All output events are still set and valid,
4284 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4285 * words, -EINTR status means that at least one of output events is
4286 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004287 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4288 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004289 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004290 */
4291
Andrew Boie3772f772018-05-07 16:52:57 -07004292__syscall int k_poll(struct k_poll_event *events, int num_events,
4293 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004294
4295/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004296 * @brief Initialize a poll signal object.
4297 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004298 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004299 *
4300 * @param signal A poll signal.
4301 *
4302 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004303 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004304 */
4305
Andrew Boie3772f772018-05-07 16:52:57 -07004306__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4307
4308/*
4309 * @brief Reset a poll signal object's state to unsignaled.
4310 *
4311 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004312 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004313 */
4314__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4315
4316static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4317{
4318 signal->signaled = 0;
4319}
4320
4321/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004322 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004323 *
4324 * @param signal A poll signal object
4325 * @param signaled An integer buffer which will be written nonzero if the
4326 * object was signaled
4327 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004328 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004329 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004330 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004331 */
4332__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4333 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004334
4335/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004336 * @brief Signal a poll signal object.
4337 *
4338 * This routine makes ready a poll signal, which is basically a poll event of
4339 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4340 * made ready to run. A @a result value can be specified.
4341 *
4342 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004343 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004344 * k_poll_signal_reset(). It thus has to be reset by the user before being
4345 * passed again to k_poll() or k_poll() will consider it being signaled, and
4346 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004347 *
4348 * @param signal A poll signal.
4349 * @param result The value to store in the result field of the signal.
4350 *
4351 * @retval 0 The signal was delivered successfully.
4352 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004353 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004354 */
4355
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004356__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004357
Anas Nashif954d5502018-02-25 08:37:28 -06004358/**
4359 * @internal
4360 */
Andy Ross8606fab2018-03-26 10:54:40 -07004361extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004362
Anas Nashif166f5192018-02-25 08:02:36 -06004363/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004364
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004365/**
4366 * @brief Make the CPU idle.
4367 *
4368 * This function makes the CPU idle until an event wakes it up.
4369 *
4370 * In a regular system, the idle thread should be the only thread responsible
4371 * for making the CPU idle and triggering any type of power management.
4372 * However, in some more constrained systems, such as a single-threaded system,
4373 * the only thread would be responsible for this if needed.
4374 *
4375 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004376 * @req K-MISC-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004377 */
4378extern void k_cpu_idle(void);
4379
4380/**
4381 * @brief Make the CPU idle in an atomic fashion.
4382 *
4383 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4384 * must be done atomically before making the CPU idle.
4385 *
4386 * @param key Interrupt locking key obtained from irq_lock().
4387 *
4388 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004389 * @req K-MISC-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004390 */
4391extern void k_cpu_atomic_idle(unsigned int key);
4392
Anas Nashif954d5502018-02-25 08:37:28 -06004393
4394/**
4395 * @internal
4396 */
Kumar Galacc334c72017-04-21 10:55:34 -05004397extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004398
Andrew Boiecdb94d62017-04-18 15:22:05 -07004399#ifdef _ARCH_EXCEPT
4400/* This archtecture has direct support for triggering a CPU exception */
4401#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4402#else
4403
Andrew Boiecdb94d62017-04-18 15:22:05 -07004404/* NOTE: This is the implementation for arches that do not implement
4405 * _ARCH_EXCEPT() to generate a real CPU exception.
4406 *
4407 * We won't have a real exception frame to determine the PC value when
4408 * the oops occurred, so print file and line number before we jump into
4409 * the fatal error handler.
4410 */
4411#define _k_except_reason(reason) do { \
4412 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4413 _NanoFatalErrorHandler(reason, &_default_esf); \
4414 CODE_UNREACHABLE; \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004415 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004416
4417#endif /* _ARCH__EXCEPT */
4418
4419/**
4420 * @brief Fatally terminate a thread
4421 *
4422 * This should be called when a thread has encountered an unrecoverable
4423 * runtime condition and needs to terminate. What this ultimately
4424 * means is determined by the _fatal_error_handler() implementation, which
4425 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4426 *
4427 * If this is called from ISR context, the default system fatal error handler
4428 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004429 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004430 */
4431#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4432
4433/**
4434 * @brief Fatally terminate the system
4435 *
4436 * This should be called when the Zephyr kernel has encountered an
4437 * unrecoverable runtime condition and needs to terminate. What this ultimately
4438 * means is determined by the _fatal_error_handler() implementation, which
4439 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004440 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004441 */
4442#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4443
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004444/*
4445 * private APIs that are utilized by one or more public APIs
4446 */
4447
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004448#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004449/**
4450 * @internal
4451 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004452extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004453#else
Anas Nashif954d5502018-02-25 08:37:28 -06004454/**
4455 * @internal
4456 */
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004457#define _init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004458#endif
4459
Anas Nashif954d5502018-02-25 08:37:28 -06004460/**
4461 * @internal
4462 */
Flavio Ceolin09e362e2018-12-17 12:34:05 -08004463extern bool _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004464/**
4465 * @internal
4466 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004467extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004468
Andrew Boiedc5d9352017-06-02 12:56:47 -07004469/* arch/cpu.h may declare an architecture or platform-specific macro
4470 * for properly declaring stacks, compatible with MMU/MPU constraints if
4471 * enabled
4472 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004473
4474/**
4475 * @brief Obtain an extern reference to a stack
4476 *
4477 * This macro properly brings the symbol of a thread stack declared
4478 * elsewhere into scope.
4479 *
4480 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004481 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004482 */
4483#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4484
Andrew Boiedc5d9352017-06-02 12:56:47 -07004485#ifdef _ARCH_THREAD_STACK_DEFINE
4486#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4487#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4488 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304489#define K_THREAD_STACK_LEN(size) _ARCH_THREAD_STACK_LEN(size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004490#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4491#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004492static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004493{
4494 return _ARCH_THREAD_STACK_BUFFER(sym);
4495}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004496#else
4497/**
4498 * @brief Declare a toplevel thread stack memory region
4499 *
4500 * This declares a region of memory suitable for use as a thread's stack.
4501 *
4502 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4503 * 'noinit' section so that it isn't zeroed at boot
4504 *
Andrew Boie507852a2017-07-25 18:47:07 -07004505 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004506 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004507 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004508 *
4509 * It is legal to precede this definition with the 'static' keyword.
4510 *
4511 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4512 * parameter of k_thread_create(), it may not be the same as the
4513 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4514 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004515 * Some arches may round the size of the usable stack region up to satisfy
4516 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4517 * size.
4518 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004519 * @param sym Thread stack symbol name
4520 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004521 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004522 */
4523#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004524 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004525
4526/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304527 * @brief Calculate size of stacks to be allocated in a stack array
4528 *
4529 * This macro calculates the size to be allocated for the stacks
4530 * inside a stack array. It accepts the indicated "size" as a parameter
4531 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4532 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4533 *
4534 * @param size Size of the stack memory region
4535 * @req K-TSTACK-001
4536 */
4537#define K_THREAD_STACK_LEN(size) (size)
4538
4539/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004540 * @brief Declare a toplevel array of thread stack memory regions
4541 *
4542 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4543 * definition for additional details and constraints.
4544 *
4545 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4546 * 'noinit' section so that it isn't zeroed at boot
4547 *
4548 * @param sym Thread stack symbol name
4549 * @param nmemb Number of stacks to declare
4550 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004551 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004552 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004553#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004554 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304555 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004556
4557/**
4558 * @brief Declare an embedded stack memory region
4559 *
4560 * Used for stacks embedded within other data structures. Use is highly
4561 * discouraged but in some cases necessary. For memory protection scenarios,
4562 * it is very important that any RAM preceding this member not be writable
4563 * by threads else a stack overflow will lead to silent corruption. In other
4564 * words, the containing data structure should live in RAM owned by the kernel.
4565 *
4566 * @param sym Thread stack symbol name
4567 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004568 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004569 */
4570#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004571 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004572
4573/**
4574 * @brief Return the size in bytes of a stack memory region
4575 *
4576 * Convenience macro for passing the desired stack size to k_thread_create()
4577 * since the underlying implementation may actually create something larger
4578 * (for instance a guard area).
4579 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004580 * The value returned here is not guaranteed to match the 'size' parameter
4581 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004582 *
4583 * @param sym Stack memory symbol
4584 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004585 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004586 */
4587#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4588
4589/**
4590 * @brief Get a pointer to the physical stack buffer
4591 *
4592 * Convenience macro to get at the real underlying stack buffer used by
4593 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4594 * This is really only intended for diagnostic tools which want to examine
4595 * stack memory contents.
4596 *
4597 * @param sym Declared stack symbol name
4598 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004599 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004600 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004601static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004602{
4603 return (char *)sym;
4604}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004605
4606#endif /* _ARCH_DECLARE_STACK */
4607
Chunlin Hane9c97022017-07-07 20:29:30 +08004608/**
4609 * @defgroup mem_domain_apis Memory domain APIs
4610 * @ingroup kernel_apis
4611 * @{
4612 */
4613
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004614/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004615 * @def K_MEM_PARTITION_DEFINE
4616 * @brief Used to declare a memory partition
4617 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004618 */
4619#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4620#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4621 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304622 __kernel struct k_mem_partition name =\
Ioannis Glaropoulos293247e2018-12-03 18:32:32 +01004623 { (u32_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004624#else
4625#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304626 __kernel struct k_mem_partition name =\
Ioannis Glaropoulos293247e2018-12-03 18:32:32 +01004627 { (u32_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004628#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4629
Chunlin Hane9c97022017-07-07 20:29:30 +08004630/* memory partition */
4631struct k_mem_partition {
4632 /* start address of memory partition */
4633 u32_t start;
4634 /* size of memory partition */
4635 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004636#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08004637 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304638 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004639#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08004640};
4641
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02004642/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304643 * Note: Always declare this structure with __kernel prefix
4644 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004645struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304646#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004647 /* partitions in the domain */
4648 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304649#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004650 /* domain q */
4651 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004652 /* number of partitions in the domain */
4653 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004654};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304655
Chunlin Hane9c97022017-07-07 20:29:30 +08004656
4657/**
4658 * @brief Initialize a memory domain.
4659 *
4660 * Initialize a memory domain with given name and memory partitions.
4661 *
4662 * @param domain The memory domain to be initialized.
4663 * @param num_parts The number of array items of "parts" parameter.
4664 * @param parts An array of pointers to the memory partitions. Can be NULL
4665 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004666 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004667 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004668extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004669 struct k_mem_partition *parts[]);
4670/**
4671 * @brief Destroy a memory domain.
4672 *
4673 * Destroy a memory domain.
4674 *
4675 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004676 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004677 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004678extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4679
4680/**
4681 * @brief Add a memory partition into a memory domain.
4682 *
4683 * Add a memory partition into a memory domain.
4684 *
4685 * @param domain The memory domain to be added a memory partition.
4686 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004687 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004688 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004689extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4690 struct k_mem_partition *part);
4691
4692/**
4693 * @brief Remove a memory partition from a memory domain.
4694 *
4695 * Remove a memory partition from a memory domain.
4696 *
4697 * @param domain The memory domain to be removed a memory partition.
4698 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004699 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004700 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004701extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4702 struct k_mem_partition *part);
4703
4704/**
4705 * @brief Add a thread into a memory domain.
4706 *
4707 * Add a thread into a memory domain.
4708 *
4709 * @param domain The memory domain that the thread is going to be added into.
4710 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004711 *
4712 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004713 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004714extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4715 k_tid_t thread);
4716
4717/**
4718 * @brief Remove a thread from its memory domain.
4719 *
4720 * Remove a thread from its memory domain.
4721 *
4722 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004723 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004724 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004725extern void k_mem_domain_remove_thread(k_tid_t thread);
4726
Anas Nashif166f5192018-02-25 08:02:36 -06004727/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004728
Andrew Boie756f9072017-10-10 16:01:49 -07004729/**
4730 * @brief Emit a character buffer to the console device
4731 *
4732 * @param c String of characters to print
4733 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004734 *
4735 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004736 */
4737__syscall void k_str_out(char *c, size_t n);
4738
Andy Rosse7172672018-01-24 15:48:32 -08004739/**
4740 * @brief Start a numbered CPU on a MP-capable system
4741
4742 * This starts and initializes a specific CPU. The main thread on
4743 * startup is running on CPU zero, other processors are numbered
4744 * sequentially. On return from this function, the CPU is known to
4745 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004746 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004747 * with the provided key will work to enable them.
4748 *
4749 * Normally, in SMP mode this function will be called by the kernel
4750 * initialization and should not be used as a user API. But it is
4751 * defined here for special-purpose apps which want Zephyr running on
4752 * one core and to use others for design-specific processing.
4753 *
4754 * @param cpu_num Integer number of the CPU
4755 * @param stack Stack memory for the CPU
4756 * @param sz Stack buffer size, in bytes
4757 * @param fn Function to begin running on the CPU. First argument is
4758 * an irq_unlock() key.
4759 * @param arg Untyped argument to be passed to "fn"
4760 */
4761extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08004762 void (*fn)(int key, void *data), void *arg);
Andy Rosse7172672018-01-24 15:48:32 -08004763
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004764#ifdef __cplusplus
4765}
4766#endif
4767
Anas Nashifb6304e62018-07-04 08:03:03 -05004768#include <tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07004769#include <syscalls/kernel.h>
4770
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004771#endif /* !_ASMLANGUAGE */
4772
Flavio Ceolin67ca1762018-09-14 10:43:44 -07004773#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */