blob: bfa91552f5834e0edb05f6e3a9cba91da4714b87 [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/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002436 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002437 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438 */
2439
Allan Stephens6bba9b02016-11-16 14:56:54 -05002440/**
2441 * @typedef k_work_handler_t
2442 * @brief Work item handler function type.
2443 *
2444 * A work item's handler function is executed by a workqueue's thread
2445 * when the work item is processed by the workqueue.
2446 *
2447 * @param work Address of the work item.
2448 *
2449 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002450 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002451 */
2452typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002453
2454/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002455 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002456 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002457
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002458struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002459 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002460 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002461};
2462
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002463enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002464 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002465};
2466
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002467struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002468 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002469 k_work_handler_t handler;
2470 atomic_t flags[1];
2471};
2472
Allan Stephens6bba9b02016-11-16 14:56:54 -05002473struct k_delayed_work {
2474 struct k_work work;
2475 struct _timeout timeout;
2476 struct k_work_q *work_q;
2477};
2478
2479extern struct k_work_q k_sys_work_q;
2480
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002481/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002482 * INTERNAL_HIDDEN @endcond
2483 */
2484
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002485#define _K_WORK_INITIALIZER(work_handler) \
2486 { \
2487 ._reserved = NULL, \
2488 .handler = work_handler, \
2489 .flags = { 0 } \
2490 }
2491
2492#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2493
Allan Stephens6bba9b02016-11-16 14:56:54 -05002494/**
2495 * @brief Initialize a statically-defined work item.
2496 *
2497 * This macro can be used to initialize a statically-defined workqueue work
2498 * item, prior to its first use. For example,
2499 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002500 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002501 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002502 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002503 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002504 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002505 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002506#define K_WORK_DEFINE(work, work_handler) \
Andrew Boiec2e01df2018-11-12 15:16:54 -08002507 struct k_work work = _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002508
2509/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002510 * @brief Initialize a work item.
2511 *
2512 * This routine initializes a workqueue work item, prior to its first use.
2513 *
2514 * @param work Address of work item.
2515 * @param handler Function to invoke each time work item is processed.
2516 *
2517 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002518 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002519 */
2520static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2521{
Leandro Pereira0e23ad82018-05-29 10:42:17 -07002522 *work = (struct k_work)_K_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002523}
2524
2525/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002526 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002527 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002528 * This routine submits work item @a work to be processed by workqueue
2529 * @a work_q. If the work item is already pending in the workqueue's queue
2530 * as a result of an earlier submission, this routine has no effect on the
2531 * work item. If the work item has already been processed, or is currently
2532 * being processed, its work is considered complete and the work item can be
2533 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002534 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002535 * @warning
2536 * A submitted work item must not be modified until it has been processed
2537 * by the workqueue.
2538 *
2539 * @note Can be called by ISRs.
2540 *
2541 * @param work_q Address of workqueue.
2542 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002543 *
2544 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002545 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002546 */
2547static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2548 struct k_work *work)
2549{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002550 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002551 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002552 }
2553}
2554
2555/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002556 * @brief Submit a work item to a user mode workqueue
2557 *
David B. Kinder06d78352018-12-17 14:32:40 -08002558 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002559 * memory allocation is made from the caller's resource pool which is freed
2560 * once the worker thread consumes the k_work item. The workqueue
2561 * thread must have memory access to the k_work item being submitted. The caller
2562 * must have permission granted on the work_q parameter's queue object.
2563 *
2564 * Otherwise this works the same as k_work_submit_to_queue().
2565 *
2566 * @note Can be called by ISRs.
2567 *
2568 * @param work_q Address of workqueue.
2569 * @param work Address of work item.
2570 *
2571 * @retval -EBUSY if the work item was already in some workqueue
2572 * @retval -ENOMEM if no memory for thread resource pool allocation
2573 * @retval 0 Success
2574 * @req K-WORK-001
2575 */
2576static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2577 struct k_work *work)
2578{
2579 int ret = -EBUSY;
2580
2581 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2582 ret = k_queue_alloc_append(&work_q->queue, work);
2583
2584 /* Couldn't insert into the queue. Clear the pending bit
2585 * so the work item can be submitted again
2586 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002587 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002588 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2589 }
2590 }
2591
2592 return ret;
2593}
2594
2595/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002596 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002597 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002598 * This routine indicates if work item @a work is pending in a workqueue's
2599 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002600 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002601 * @note Can be called by ISRs.
2602 *
2603 * @param work Address of work item.
2604 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002605 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002606 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002607 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002608static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002609{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002610 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002611}
2612
2613/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002614 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002615 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002616 * This routine starts workqueue @a work_q. The workqueue spawns its work
2617 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002618 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002619 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002620 * @param stack Pointer to work queue thread's stack space, as defined by
2621 * K_THREAD_STACK_DEFINE()
2622 * @param stack_size Size of the work queue thread's stack (in bytes), which
2623 * should either be the same constant passed to
2624 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002625 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002626 *
2627 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002628 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002629 */
Andrew Boie507852a2017-07-25 18:47:07 -07002630extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002631 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002632 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002633
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002634/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002635 * @brief Start a workqueue in user mode
2636 *
2637 * This works identically to k_work_q_start() except it is callable from user
2638 * mode, and the worker thread created will run in user mode.
2639 * The caller must have permissions granted on both the work_q parameter's
2640 * thread and queue objects, and the same restrictions on priority apply as
2641 * k_thread_create().
2642 *
2643 * @param work_q Address of workqueue.
2644 * @param stack Pointer to work queue thread's stack space, as defined by
2645 * K_THREAD_STACK_DEFINE()
2646 * @param stack_size Size of the work queue thread's stack (in bytes), which
2647 * should either be the same constant passed to
2648 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2649 * @param prio Priority of the work queue's thread.
2650 *
2651 * @return N/A
2652 * @req K-WORK-001
2653 */
2654extern void k_work_q_user_start(struct k_work_q *work_q,
2655 k_thread_stack_t *stack,
2656 size_t stack_size, int prio);
2657
2658/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002659 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002660 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002661 * This routine initializes a workqueue delayed work item, prior to
2662 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002663 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002664 * @param work Address of delayed work item.
2665 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002666 *
2667 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002668 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002669 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002670extern void k_delayed_work_init(struct k_delayed_work *work,
2671 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002672
2673/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002674 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002675 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002676 * This routine schedules work item @a work to be processed by workqueue
2677 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002678 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002679 * Only when the countdown completes is the work item actually submitted to
2680 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002681 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002682 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002683 * counting down cancels the existing submission and restarts the
2684 * countdown using the new delay. Note that this behavior is
2685 * inherently subject to race conditions with the pre-existing
2686 * timeouts and work queue, so care must be taken to synchronize such
2687 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002688 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002689 * @warning
2690 * A delayed work item must not be modified until it has been processed
2691 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002692 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002693 * @note Can be called by ISRs.
2694 *
2695 * @param work_q Address of workqueue.
2696 * @param work Address of delayed work item.
2697 * @param delay Delay before submitting the work item (in milliseconds).
2698 *
2699 * @retval 0 Work item countdown started.
2700 * @retval -EINPROGRESS Work item is already pending.
2701 * @retval -EINVAL Work item is being processed or has completed its work.
2702 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002703 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002704 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002705extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2706 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002707 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002708
2709/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002710 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002711 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002712 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002713 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002714 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002715 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002716 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002717 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002718 * @param work Address of delayed work item.
2719 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002720 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002721 * @retval -EINPROGRESS Work item is already pending.
2722 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002723 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002724 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002725extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002726
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002727/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002728 * @brief Submit a work item to the system workqueue.
2729 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002730 * This routine submits work item @a work to be processed by the system
2731 * workqueue. If the work item is already pending in the workqueue's queue
2732 * as a result of an earlier submission, this routine has no effect on the
2733 * work item. If the work item has already been processed, or is currently
2734 * being processed, its work is considered complete and the work item can be
2735 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002736 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002737 * @warning
2738 * Work items submitted to the system workqueue should avoid using handlers
2739 * that block or yield since this may prevent the system workqueue from
2740 * processing other work items in a timely manner.
2741 *
2742 * @note Can be called by ISRs.
2743 *
2744 * @param work Address of work item.
2745 *
2746 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002747 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002748 */
2749static inline void k_work_submit(struct k_work *work)
2750{
2751 k_work_submit_to_queue(&k_sys_work_q, work);
2752}
2753
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002754/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002755 * @brief Submit a delayed work item to the system workqueue.
2756 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002757 * This routine schedules work item @a work to be processed by the system
2758 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002759 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002760 * Only when the countdown completes is the work item actually submitted to
2761 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002762 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002763 * Submitting a previously submitted delayed work item that is still
2764 * counting down cancels the existing submission and restarts the countdown
2765 * using the new delay. If the work item is currently pending on the
2766 * workqueue's queue because the countdown has completed it is too late to
2767 * resubmit the item, and resubmission fails without impacting the work item.
2768 * If the work item has already been processed, or is currently being processed,
2769 * its work is considered complete and the work item can be resubmitted.
2770 *
2771 * @warning
2772 * Work items submitted to the system workqueue should avoid using handlers
2773 * that block or yield since this may prevent the system workqueue from
2774 * processing other work items in a timely manner.
2775 *
2776 * @note Can be called by ISRs.
2777 *
2778 * @param work Address of delayed work item.
2779 * @param delay Delay before submitting the work item (in milliseconds).
2780 *
2781 * @retval 0 Work item countdown started.
2782 * @retval -EINPROGRESS Work item is already pending.
2783 * @retval -EINVAL Work item is being processed or has completed its work.
2784 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002785 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002786 */
2787static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002788 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002789{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002790 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002791}
2792
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002793/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002794 * @brief Get time remaining before a delayed work gets scheduled.
2795 *
2796 * This routine computes the (approximate) time remaining before a
2797 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002798 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002799 *
2800 * @param work Delayed work item.
2801 *
2802 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002803 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002804 */
Kumar Galacc334c72017-04-21 10:55:34 -05002805static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002806{
Andy Ross52e444b2018-09-28 09:06:37 -07002807 return __ticks_to_ms(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02002808}
2809
Anas Nashif166f5192018-02-25 08:02:36 -06002810/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002811/**
Anas Nashifce78d162018-05-24 12:43:11 -05002812 * @defgroup mutex_apis Mutex APIs
2813 * @ingroup kernel_apis
2814 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002815 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002816
Anas Nashifce78d162018-05-24 12:43:11 -05002817/**
2818 * Mutex Structure
2819 * @ingroup mutex_apis
2820 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002821struct k_mutex {
2822 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002823 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002824 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002825 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002826 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002827
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002828 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002829};
2830
Anas Nashifce78d162018-05-24 12:43:11 -05002831/**
2832 * @cond INTERNAL_HIDDEN
2833 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002834#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002835 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002836 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002837 .owner = NULL, \
2838 .lock_count = 0, \
2839 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002840 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002841 }
2842
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002843#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2844
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002845/**
Allan Stephensc98da842016-11-11 15:45:03 -05002846 * INTERNAL_HIDDEN @endcond
2847 */
2848
2849/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002850 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002852 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002853 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002854 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002856 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002857 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002858 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002859#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002860 struct k_mutex name \
2861 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002862 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002863
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002864/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002865 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002867 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002869 * Upon completion, the mutex is available and does not have an owner.
2870 *
2871 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002872 *
2873 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002874 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002875 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002876__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002877
2878/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002879 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002881 * This routine locks @a mutex. If the mutex is locked by another thread,
2882 * the calling thread waits until the mutex becomes available or until
2883 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002885 * A thread is permitted to lock a mutex it has already locked. The operation
2886 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002888 * @param mutex Address of the mutex.
2889 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002890 * or one of the special values K_NO_WAIT and K_FOREVER.
2891 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002892 * @retval 0 Mutex locked.
2893 * @retval -EBUSY Returned without waiting.
2894 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002895 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002896 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002897__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002898
2899/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002900 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002902 * This routine unlocks @a mutex. The mutex must already be locked by the
2903 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002904 *
2905 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002906 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002907 * thread.
2908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002909 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002910 *
2911 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002912 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002913 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002914__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002915
Allan Stephensc98da842016-11-11 15:45:03 -05002916/**
Anas Nashif166f5192018-02-25 08:02:36 -06002917 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002918 */
2919
2920/**
2921 * @cond INTERNAL_HIDDEN
2922 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002923
2924struct k_sem {
2925 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05302926 u32_t count;
2927 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002928 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002929
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002930 _OBJECT_TRACING_NEXT_PTR(k_sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002931};
2932
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002933#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002934 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002935 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002936 .count = initial_count, \
2937 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002938 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002939 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002940 }
2941
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002942#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2943
Allan Stephensc98da842016-11-11 15:45:03 -05002944/**
2945 * INTERNAL_HIDDEN @endcond
2946 */
2947
2948/**
2949 * @defgroup semaphore_apis Semaphore APIs
2950 * @ingroup kernel_apis
2951 * @{
2952 */
2953
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002954/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002955 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002957 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002958 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002959 * @param sem Address of the semaphore.
2960 * @param initial_count Initial semaphore count.
2961 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002962 *
2963 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002964 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002965 */
Andrew Boie99280232017-09-29 14:17:47 -07002966__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2967 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002968
2969/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002970 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002972 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002974 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2975 *
2976 * @param sem Address of the semaphore.
2977 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002978 * or one of the special values K_NO_WAIT and K_FOREVER.
2979 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002980 * @note When porting code from the nanokernel legacy API to the new API, be
2981 * careful with the return value of this function. The return value is the
2982 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2983 * non-zero means failure, while the nano_sem_take family returns 1 for success
2984 * and 0 for failure.
2985 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002986 * @retval 0 Semaphore taken.
2987 * @retval -EBUSY Returned without waiting.
2988 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002989 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002990 */
Andrew Boie99280232017-09-29 14:17:47 -07002991__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002992
2993/**
2994 * @brief Give a semaphore.
2995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002996 * This routine gives @a sem, unless the semaphore is already at its maximum
2997 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002999 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003001 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003002 *
3003 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003004 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003005 */
Andrew Boie99280232017-09-29 14:17:47 -07003006__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003007
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003008/**
3009 * @brief Reset a semaphore's count to zero.
3010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003011 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003013 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003014 *
3015 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003016 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003017 */
Andrew Boie990bf162017-10-03 12:36:49 -07003018__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003019
Anas Nashif954d5502018-02-25 08:37:28 -06003020/**
3021 * @internal
3022 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003023static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003024{
3025 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003026}
3027
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003028/**
3029 * @brief Get a semaphore's count.
3030 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003031 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003033 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003035 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003036 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003037 */
Andrew Boie990bf162017-10-03 12:36:49 -07003038__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003039
Anas Nashif954d5502018-02-25 08:37:28 -06003040/**
3041 * @internal
3042 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003043static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003044{
3045 return sem->count;
3046}
3047
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003048/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003049 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003050 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003051 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003052 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003053 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003055 * @param name Name of the semaphore.
3056 * @param initial_count Initial semaphore count.
3057 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003058 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003059 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003060#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003061 struct k_sem name \
3062 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003063 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303064 BUILD_ASSERT(((count_limit) != 0) && \
3065 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003066
Anas Nashif166f5192018-02-25 08:02:36 -06003067/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003068
3069/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003070 * @defgroup msgq_apis Message Queue APIs
3071 * @ingroup kernel_apis
3072 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003073 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003074
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003075/**
3076 * @brief Message Queue Structure
3077 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003078struct k_msgq {
3079 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003080 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003081 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003082 char *buffer_start;
3083 char *buffer_end;
3084 char *read_ptr;
3085 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003086 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003087
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003088 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Andrew Boie0fe789f2018-04-12 18:35:56 -07003089 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003090};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003091/**
3092 * @cond INTERNAL_HIDDEN
3093 */
3094
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003095
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003096#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003097 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003098 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003099 .max_msgs = q_max_msgs, \
3100 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003101 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003102 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003103 .read_ptr = q_buffer, \
3104 .write_ptr = q_buffer, \
3105 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003106 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003107 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003108#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003109/**
3110 * INTERNAL_HIDDEN @endcond
3111 */
3112
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003113
Andrew Boie0fe789f2018-04-12 18:35:56 -07003114#define K_MSGQ_FLAG_ALLOC BIT(0)
3115
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003116/**
3117 * @brief Message Queue Attributes
3118 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303119struct k_msgq_attrs {
3120 size_t msg_size;
3121 u32_t max_msgs;
3122 u32_t used_msgs;
3123};
3124
Allan Stephensc98da842016-11-11 15:45:03 -05003125
3126/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003127 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003128 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003129 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3130 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003131 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3132 * message is similarly aligned to this boundary, @a q_msg_size must also be
3133 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003135 * The message queue can be accessed outside the module where it is defined
3136 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003137 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003138 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003139 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003140 * @param q_name Name of the message queue.
3141 * @param q_msg_size Message size (in bytes).
3142 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003143 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003144 *
3145 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003146 */
3147#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003148 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003149 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003150 struct k_msgq q_name \
3151 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003152 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003153 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003154
Peter Mitsisd7a37502016-10-13 11:37:40 -04003155/**
3156 * @brief Initialize a message queue.
3157 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003158 * This routine initializes a message queue object, prior to its first use.
3159 *
Allan Stephensda827222016-11-09 14:23:58 -06003160 * The message queue's ring buffer must contain space for @a max_msgs messages,
3161 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3162 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3163 * that each message is similarly aligned to this boundary, @a q_msg_size
3164 * must also be a multiple of N.
3165 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003166 * @param q Address of the message queue.
3167 * @param buffer Pointer to ring buffer that holds queued messages.
3168 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003169 * @param max_msgs Maximum number of messages that can be queued.
3170 *
3171 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003172 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003173 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003174void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3175 u32_t max_msgs);
3176
3177/**
3178 * @brief Initialize a message queue.
3179 *
3180 * This routine initializes a message queue object, prior to its first use,
3181 * allocating its internal ring buffer from the calling thread's resource
3182 * pool.
3183 *
3184 * Memory allocated for the ring buffer can be released by calling
3185 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3186 * all of its references.
3187 *
3188 * @param q Address of the message queue.
3189 * @param msg_size Message size (in bytes).
3190 * @param max_msgs Maximum number of messages that can be queued.
3191 *
3192 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3193 * thread's resource pool, or -EINVAL if the size parameters cause
3194 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003195 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003196 */
3197__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3198 u32_t max_msgs);
3199
3200
3201void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003202
3203/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003204 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003205 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003206 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003207 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003208 * @note Can be called by ISRs.
3209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003210 * @param q Address of the message queue.
3211 * @param data Pointer to the message.
3212 * @param timeout Waiting period to add the message (in milliseconds),
3213 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003214 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003215 * @retval 0 Message sent.
3216 * @retval -ENOMSG Returned without waiting or queue purged.
3217 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003218 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003219 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003220__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003221
3222/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003223 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003224 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003225 * This routine receives a message from message queue @a q in a "first in,
3226 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003227 *
Allan Stephensc98da842016-11-11 15:45:03 -05003228 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003229 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003230 * @param q Address of the message queue.
3231 * @param data Address of area to hold the received message.
3232 * @param timeout Waiting period to receive the message (in milliseconds),
3233 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003234 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003235 * @retval 0 Message received.
3236 * @retval -ENOMSG Returned without waiting.
3237 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003238 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003239 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003240__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003241
3242/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003243 * @brief Peek/read a message from a message queue.
3244 *
3245 * This routine reads a message from message queue @a q in a "first in,
3246 * first out" manner and leaves the message in the queue.
3247 *
3248 * @note Can be called by ISRs.
3249 *
3250 * @param q Address of the message queue.
3251 * @param data Address of area to hold the message read from the queue.
3252 *
3253 * @retval 0 Message read.
3254 * @retval -ENOMSG Returned when the queue has no message.
3255 * @req K-MSGQ-002
3256 */
3257__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3258
3259/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003260 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003261 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003262 * This routine discards all unreceived messages in a message queue's ring
3263 * buffer. Any threads that are blocked waiting to send a message to the
3264 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003265 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003266 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003267 *
3268 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003269 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003270 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003271__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003272
Peter Mitsis67be2492016-10-07 11:44:34 -04003273/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003274 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003275 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003276 * This routine returns the number of unused entries in a message queue's
3277 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003279 * @param q Address of the message queue.
3280 *
3281 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003282 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003283 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003284__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3285
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303286/**
3287 * @brief Get basic attributes of a message queue.
3288 *
3289 * This routine fetches basic attributes of message queue into attr argument.
3290 *
3291 * @param q Address of the message queue.
3292 * @param attrs pointer to message queue attribute structure.
3293 *
3294 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003295 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303296 */
3297__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3298
3299
Andrew Boie82edb6e2017-10-02 10:53:06 -07003300static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003301{
3302 return q->max_msgs - q->used_msgs;
3303}
3304
Peter Mitsisd7a37502016-10-13 11:37:40 -04003305/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003306 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003307 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003308 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003309 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003310 * @param q Address of the message queue.
3311 *
3312 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003313 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003314 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003315__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3316
3317static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003318{
3319 return q->used_msgs;
3320}
3321
Anas Nashif166f5192018-02-25 08:02:36 -06003322/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003323
3324/**
3325 * @defgroup mem_pool_apis Memory Pool APIs
3326 * @ingroup kernel_apis
3327 * @{
3328 */
3329
Andy Ross73cb9582017-05-09 10:42:39 -07003330/* Note on sizing: the use of a 20 bit field for block means that,
3331 * assuming a reasonable minimum block size of 16 bytes, we're limited
3332 * to 16M of memory managed by a single pool. Long term it would be
3333 * good to move to a variable bit size based on configuration.
3334 */
3335struct k_mem_block_id {
3336 u32_t pool : 8;
3337 u32_t level : 4;
3338 u32_t block : 20;
3339};
3340
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003341struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003342 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003343 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003344};
3345
Anas Nashif166f5192018-02-25 08:02:36 -06003346/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003347
3348/**
3349 * @defgroup mailbox_apis Mailbox APIs
3350 * @ingroup kernel_apis
3351 * @{
3352 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003353
3354struct k_mbox_msg {
3355 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003356 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003357 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003358 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003359 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003360 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003361 /** sender's message data buffer */
3362 void *tx_data;
3363 /** internal use only - needed for legacy API support */
3364 void *_rx_data;
3365 /** message data block descriptor */
3366 struct k_mem_block tx_block;
3367 /** source thread id */
3368 k_tid_t rx_source_thread;
3369 /** target thread id */
3370 k_tid_t tx_target_thread;
3371 /** internal use only - thread waiting on send (may be a dummy) */
3372 k_tid_t _syncing_thread;
3373#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3374 /** internal use only - semaphore used during asynchronous send */
3375 struct k_sem *_async_sem;
3376#endif
3377};
3378
3379struct k_mbox {
3380 _wait_q_t tx_msg_queue;
3381 _wait_q_t rx_msg_queue;
3382
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003383 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003384};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003385/**
3386 * @cond INTERNAL_HIDDEN
3387 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003388
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003389#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003390 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003391 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3392 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003393 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003394 }
3395
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003396#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3397
Peter Mitsis12092702016-10-14 12:57:23 -04003398/**
Allan Stephensc98da842016-11-11 15:45:03 -05003399 * INTERNAL_HIDDEN @endcond
3400 */
3401
3402/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003403 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003404 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003405 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003406 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003407 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003409 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003410 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003411 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003412#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003413 struct k_mbox name \
3414 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003415 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003416
Peter Mitsis12092702016-10-14 12:57:23 -04003417/**
3418 * @brief Initialize a mailbox.
3419 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003420 * This routine initializes a mailbox object, prior to its first use.
3421 *
3422 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003423 *
3424 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003425 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003426 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003427extern void k_mbox_init(struct k_mbox *mbox);
3428
Peter Mitsis12092702016-10-14 12:57:23 -04003429/**
3430 * @brief Send a mailbox message in a synchronous manner.
3431 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003432 * This routine sends a message to @a mbox and waits for a receiver to both
3433 * receive and process it. The message data may be in a buffer, in a memory
3434 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003436 * @param mbox Address of the mailbox.
3437 * @param tx_msg Address of the transmit message descriptor.
3438 * @param timeout Waiting period for the message to be received (in
3439 * milliseconds), or one of the special values K_NO_WAIT
3440 * and K_FOREVER. Once the message has been received,
3441 * this routine waits as long as necessary for the message
3442 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003443 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003444 * @retval 0 Message sent.
3445 * @retval -ENOMSG Returned without waiting.
3446 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003447 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003448 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003449extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003450 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003451
Peter Mitsis12092702016-10-14 12:57:23 -04003452/**
3453 * @brief Send a mailbox message in an asynchronous manner.
3454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003455 * This routine sends a message to @a mbox without waiting for a receiver
3456 * to process it. The message data may be in a buffer, in a memory pool block,
3457 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3458 * will be given when the message has been both received and completely
3459 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003460 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003461 * @param mbox Address of the mailbox.
3462 * @param tx_msg Address of the transmit message descriptor.
3463 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003464 *
3465 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003466 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003467 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003468extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003469 struct k_sem *sem);
3470
Peter Mitsis12092702016-10-14 12:57:23 -04003471/**
3472 * @brief Receive a mailbox message.
3473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003474 * This routine receives a message from @a mbox, then optionally retrieves
3475 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003476 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003477 * @param mbox Address of the mailbox.
3478 * @param rx_msg Address of the receive message descriptor.
3479 * @param buffer Address of the buffer to receive data, or NULL to defer data
3480 * retrieval and message disposal until later.
3481 * @param timeout Waiting period for a message to be received (in
3482 * milliseconds), or one of the special values K_NO_WAIT
3483 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003484 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003485 * @retval 0 Message received.
3486 * @retval -ENOMSG Returned without waiting.
3487 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003488 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003489 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003490extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003491 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003492
3493/**
3494 * @brief Retrieve mailbox message data into a buffer.
3495 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003496 * This routine completes the processing of a received message by retrieving
3497 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003498 *
3499 * Alternatively, this routine can be used to dispose of a received message
3500 * without retrieving its data.
3501 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003502 * @param rx_msg Address of the receive message descriptor.
3503 * @param buffer Address of the buffer to receive data, or NULL to discard
3504 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003505 *
3506 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003507 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003508 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003509extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003510
3511/**
3512 * @brief Retrieve mailbox message data into a memory pool block.
3513 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003514 * This routine completes the processing of a received message by retrieving
3515 * its data into a memory pool block, then disposing of the message.
3516 * The memory pool block that results from successful retrieval must be
3517 * returned to the pool once the data has been processed, even in cases
3518 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003519 *
3520 * Alternatively, this routine can be used to dispose of a received message
3521 * without retrieving its data. In this case there is no need to return a
3522 * memory pool block to the pool.
3523 *
3524 * This routine allocates a new memory pool block for the data only if the
3525 * data is not already in one. If a new block cannot be allocated, the routine
3526 * returns a failure code and the received message is left unchanged. This
3527 * permits the caller to reattempt data retrieval at a later time or to dispose
3528 * of the received message without retrieving its data.
3529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003530 * @param rx_msg Address of a receive message descriptor.
3531 * @param pool Address of memory pool, or NULL to discard data.
3532 * @param block Address of the area to hold memory pool block info.
3533 * @param timeout Waiting period to wait for a memory pool block (in
3534 * milliseconds), or one of the special values K_NO_WAIT
3535 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003536 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003537 * @retval 0 Data retrieved.
3538 * @retval -ENOMEM Returned without waiting.
3539 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003540 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003541 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003542extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003543 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003544 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003545
Anas Nashif166f5192018-02-25 08:02:36 -06003546/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003547
3548/**
Anas Nashifce78d162018-05-24 12:43:11 -05003549 * @defgroup pipe_apis Pipe APIs
3550 * @ingroup kernel_apis
3551 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003552 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003553
Anas Nashifce78d162018-05-24 12:43:11 -05003554/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003555struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003556 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3557 size_t size; /**< Buffer size */
3558 size_t bytes_used; /**< # bytes used in buffer */
3559 size_t read_index; /**< Where in buffer to read from */
3560 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003561
3562 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003563 _wait_q_t readers; /**< Reader wait queue */
3564 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003565 } wait_q;
3566
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003567 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Anas Nashifce78d162018-05-24 12:43:11 -05003568 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003569};
3570
Anas Nashifce78d162018-05-24 12:43:11 -05003571/**
3572 * @cond INTERNAL_HIDDEN
3573 */
3574#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3575
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003576#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003577 { \
3578 .buffer = pipe_buffer, \
3579 .size = pipe_buffer_size, \
3580 .bytes_used = 0, \
3581 .read_index = 0, \
3582 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003583 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3584 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003585 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003586 }
3587
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003588#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3589
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003590/**
Allan Stephensc98da842016-11-11 15:45:03 -05003591 * INTERNAL_HIDDEN @endcond
3592 */
3593
3594/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003595 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003597 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003598 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003599 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003600 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003601 * @param name Name of the pipe.
3602 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3603 * or zero if no ring buffer is used.
3604 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003605 *
3606 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003607 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003608#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3609 static unsigned char __kernel_noinit __aligned(pipe_align) \
3610 _k_pipe_buf_##name[pipe_buffer_size]; \
3611 struct k_pipe name \
3612 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003613 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003614
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003615/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003616 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003617 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003618 * This routine initializes a pipe object, prior to its first use.
3619 *
3620 * @param pipe Address of the pipe.
3621 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3622 * is used.
3623 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3624 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003625 *
3626 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003627 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003628 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003629void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3630
3631/**
3632 * @brief Release a pipe's allocated buffer
3633 *
3634 * If a pipe object was given a dynamically allocated buffer via
3635 * k_pipe_alloc_init(), this will free it. This function does nothing
3636 * if the buffer wasn't dynamically allocated.
3637 *
3638 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003639 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003640 */
3641void k_pipe_cleanup(struct k_pipe *pipe);
3642
3643/**
3644 * @brief Initialize a pipe and allocate a buffer for it
3645 *
3646 * Storage for the buffer region will be allocated from the calling thread's
3647 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3648 * or userspace is enabled and the pipe object loses all references to it.
3649 *
3650 * This function should only be called on uninitialized pipe objects.
3651 *
3652 * @param pipe Address of the pipe.
3653 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3654 * buffer is used.
3655 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003656 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003657 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003658 */
3659__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003660
3661/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003662 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003663 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003664 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003665 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003666 * @param pipe Address of the pipe.
3667 * @param data Address of data to write.
3668 * @param bytes_to_write Size of data (in bytes).
3669 * @param bytes_written Address of area to hold the number of bytes written.
3670 * @param min_xfer Minimum number of bytes to write.
3671 * @param timeout Waiting period to wait for the data to be written (in
3672 * milliseconds), or one of the special values K_NO_WAIT
3673 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003674 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003675 * @retval 0 At least @a min_xfer bytes of data were written.
3676 * @retval -EIO Returned without waiting; zero data bytes were written.
3677 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003678 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003679 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003680 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003681__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3682 size_t bytes_to_write, size_t *bytes_written,
3683 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003684
3685/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003686 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003688 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003690 * @param pipe Address of the pipe.
3691 * @param data Address to place the data read from pipe.
3692 * @param bytes_to_read Maximum number of data bytes to read.
3693 * @param bytes_read Address of area to hold the number of bytes read.
3694 * @param min_xfer Minimum number of data bytes to read.
3695 * @param timeout Waiting period to wait for the data to be read (in
3696 * milliseconds), or one of the special values K_NO_WAIT
3697 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003698 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003699 * @retval 0 At least @a min_xfer bytes of data were read.
3700 * @retval -EIO Returned without waiting; zero data bytes were read.
3701 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003702 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003703 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003704 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003705__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3706 size_t bytes_to_read, size_t *bytes_read,
3707 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003708
3709/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003710 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003711 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003712 * This routine writes the data contained in a memory block to @a pipe.
3713 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003714 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003716 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003717 * @param block Memory block containing data to send
3718 * @param size Number of data bytes in memory block to send
3719 * @param sem Semaphore to signal upon completion (else NULL)
3720 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003721 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003722 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003723 */
3724extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3725 size_t size, struct k_sem *sem);
3726
Anas Nashif166f5192018-02-25 08:02:36 -06003727/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003728
Allan Stephensc98da842016-11-11 15:45:03 -05003729/**
3730 * @cond INTERNAL_HIDDEN
3731 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003732
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003733struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003734 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003735 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003736 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003737 char *buffer;
3738 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003739 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003740
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003741 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003742};
3743
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003744#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003745 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003746 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003747 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003748 .num_blocks = slab_num_blocks, \
3749 .block_size = slab_block_size, \
3750 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003751 .free_list = NULL, \
3752 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003753 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003754 }
3755
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003756#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3757
3758
Peter Mitsis578f9112016-10-07 13:50:31 -04003759/**
Allan Stephensc98da842016-11-11 15:45:03 -05003760 * INTERNAL_HIDDEN @endcond
3761 */
3762
3763/**
3764 * @defgroup mem_slab_apis Memory Slab APIs
3765 * @ingroup kernel_apis
3766 * @{
3767 */
3768
3769/**
Allan Stephensda827222016-11-09 14:23:58 -06003770 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003771 *
Allan Stephensda827222016-11-09 14:23:58 -06003772 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003773 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003774 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3775 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003776 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003777 *
Allan Stephensda827222016-11-09 14:23:58 -06003778 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003779 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003780 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003781 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003782 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003783 * @param name Name of the memory slab.
3784 * @param slab_block_size Size of each memory block (in bytes).
3785 * @param slab_num_blocks Number memory blocks.
3786 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003787 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003788 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003789#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3790 char __noinit __aligned(slab_align) \
3791 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3792 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003793 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003794 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003795 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003796
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003797/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003798 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003800 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003801 *
Allan Stephensda827222016-11-09 14:23:58 -06003802 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3803 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3804 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3805 * To ensure that each memory block is similarly aligned to this boundary,
3806 * @a slab_block_size must also be a multiple of N.
3807 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003808 * @param slab Address of the memory slab.
3809 * @param buffer Pointer to buffer used for the memory blocks.
3810 * @param block_size Size of each memory block (in bytes).
3811 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003812 *
3813 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003814 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003815 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003816extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003817 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003818
3819/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003820 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003821 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003822 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003824 * @param slab Address of the memory slab.
3825 * @param mem Pointer to block address area.
3826 * @param timeout Maximum time to wait for operation to complete
3827 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3828 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003829 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003830 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003831 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003832 * @retval -ENOMEM Returned without waiting.
3833 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003834 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003835 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003836extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003837 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003838
3839/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003840 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003842 * This routine releases a previously allocated memory block back to its
3843 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003845 * @param slab Address of the memory slab.
3846 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003847 *
3848 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003849 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003850 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003851extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003852
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003853/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003854 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003856 * This routine gets the number of memory blocks that are currently
3857 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003858 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003859 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003861 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003862 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003863 */
Kumar Galacc334c72017-04-21 10:55:34 -05003864static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003865{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003866 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003867}
3868
Peter Mitsisc001aa82016-10-13 13:53:37 -04003869/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003870 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003872 * This routine gets the number of memory blocks that are currently
3873 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003875 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003877 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003878 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04003879 */
Kumar Galacc334c72017-04-21 10:55:34 -05003880static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003881{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003882 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003883}
3884
Anas Nashif166f5192018-02-25 08:02:36 -06003885/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003886
3887/**
3888 * @cond INTERNAL_HIDDEN
3889 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003890
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003891struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003892 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003893 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003894};
3895
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003896/**
Allan Stephensc98da842016-11-11 15:45:03 -05003897 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003898 */
3899
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003900/**
Allan Stephensc98da842016-11-11 15:45:03 -05003901 * @addtogroup mem_pool_apis
3902 * @{
3903 */
3904
3905/**
3906 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003907 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003908 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3909 * long. The memory pool allows blocks to be repeatedly partitioned into
3910 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003911 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003912 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003913 * If the pool is to be accessed outside the module where it is defined, it
3914 * can be declared via
3915 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003916 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003917 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003918 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003919 * @param minsz Size of the smallest blocks in the pool (in bytes).
3920 * @param maxsz Size of the largest blocks in the pool (in bytes).
3921 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003922 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003923 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003924 */
Andy Ross73cb9582017-05-09 10:42:39 -07003925#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3926 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3927 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003928 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003929 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003930 .base = { \
3931 .buf = _mpool_buf_##name, \
3932 .max_sz = maxsz, \
3933 .n_max = nmax, \
3934 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3935 .levels = _mpool_lvls_##name, \
3936 .flags = SYS_MEM_POOL_KERNEL \
3937 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003938 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003939
Peter Mitsis937042c2016-10-13 13:18:26 -04003940/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003941 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003943 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003944 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003945 * @param pool Address of the memory pool.
3946 * @param block Pointer to block descriptor for the allocated memory.
3947 * @param size Amount of memory to allocate (in bytes).
3948 * @param timeout Maximum time to wait for operation to complete
3949 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3950 * or K_FOREVER to wait as long as necessary.
3951 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003952 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003953 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003954 * @retval -ENOMEM Returned without waiting.
3955 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003956 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04003957 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003958extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003959 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003960
3961/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07003962 * @brief Allocate memory from a memory pool with malloc() semantics
3963 *
3964 * Such memory must be released using k_free().
3965 *
3966 * @param pool Address of the memory pool.
3967 * @param size Amount of memory to allocate (in bytes).
3968 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003969 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07003970 */
3971extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
3972
3973/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003974 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003975 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003976 * This routine releases a previously allocated memory block back to its
3977 * memory pool.
3978 *
3979 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003980 *
3981 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003982 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04003983 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003984extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003985
3986/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003987 * @brief Free memory allocated from a memory pool.
3988 *
3989 * This routine releases a previously allocated memory block back to its
3990 * memory pool
3991 *
3992 * @param id Memory block identifier.
3993 *
3994 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003995 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003996 */
3997extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3998
3999/**
Anas Nashif166f5192018-02-25 08:02:36 -06004000 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004001 */
4002
4003/**
4004 * @defgroup heap_apis Heap Memory Pool APIs
4005 * @ingroup kernel_apis
4006 * @{
4007 */
4008
4009/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004010 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004011 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004012 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004013 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004015 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004016 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004017 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004018 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004019 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004020extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004021
4022/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004023 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004024 *
4025 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004026 * returned must have been allocated from the heap memory pool or
4027 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004028 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004029 * If @a ptr is NULL, no operation is performed.
4030 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004031 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004032 *
4033 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004034 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004035 */
4036extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004037
Allan Stephensc98da842016-11-11 15:45:03 -05004038/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004039 * @brief Allocate memory from heap, array style
4040 *
4041 * This routine provides traditional calloc() semantics. Memory is
4042 * allocated from the heap memory pool and zeroed.
4043 *
4044 * @param nmemb Number of elements in the requested array
4045 * @param size Size of each array element (in bytes).
4046 *
4047 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004048 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004049 */
4050extern void *k_calloc(size_t nmemb, size_t size);
4051
Anas Nashif166f5192018-02-25 08:02:36 -06004052/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004053
Benjamin Walshacc68c12017-01-29 18:57:45 -05004054/* polling API - PRIVATE */
4055
Benjamin Walshb0179862017-02-02 16:39:57 -05004056#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004057#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004058#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004059#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004060#endif
4061
Benjamin Walshacc68c12017-01-29 18:57:45 -05004062/* private - implementation data created as needed, per-type */
4063struct _poller {
4064 struct k_thread *thread;
Flavio Ceolin76b35182018-12-16 12:48:29 -08004065 volatile bool is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004066};
4067
4068/* private - types bit positions */
4069enum _poll_types_bits {
4070 /* can be used to ignore an event */
4071 _POLL_TYPE_IGNORE,
4072
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004073 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004074 _POLL_TYPE_SIGNAL,
4075
4076 /* semaphore availability */
4077 _POLL_TYPE_SEM_AVAILABLE,
4078
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004079 /* queue/fifo/lifo data availability */
4080 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004081
4082 _POLL_NUM_TYPES
4083};
4084
4085#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4086
4087/* private - states bit positions */
4088enum _poll_states_bits {
4089 /* default state when creating event */
4090 _POLL_STATE_NOT_READY,
4091
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004092 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004093 _POLL_STATE_SIGNALED,
4094
4095 /* semaphore is available */
4096 _POLL_STATE_SEM_AVAILABLE,
4097
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004098 /* data is available to read on queue/fifo/lifo */
4099 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004100
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004101 /* queue/fifo/lifo wait was cancelled */
4102 _POLL_STATE_CANCELLED,
4103
Benjamin Walshacc68c12017-01-29 18:57:45 -05004104 _POLL_NUM_STATES
4105};
4106
4107#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4108
4109#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004110 (32 - (0 \
4111 + 8 /* tag */ \
4112 + _POLL_NUM_TYPES \
4113 + _POLL_NUM_STATES \
4114 + 1 /* modes */ \
4115 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004116
Benjamin Walshacc68c12017-01-29 18:57:45 -05004117/* end of polling API - PRIVATE */
4118
4119
4120/**
4121 * @defgroup poll_apis Async polling APIs
4122 * @ingroup kernel_apis
4123 * @{
4124 */
4125
4126/* Public polling API */
4127
4128/* public - values for k_poll_event.type bitfield */
4129#define K_POLL_TYPE_IGNORE 0
4130#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4131#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004132#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4133#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004134
4135/* public - polling modes */
4136enum k_poll_modes {
4137 /* polling thread does not take ownership of objects when available */
4138 K_POLL_MODE_NOTIFY_ONLY = 0,
4139
4140 K_POLL_NUM_MODES
4141};
4142
4143/* public - values for k_poll_event.state bitfield */
4144#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004145#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4146#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004147#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4148#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004149#define K_POLL_STATE_CANCELLED _POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004150
4151/* public - poll signal object */
4152struct k_poll_signal {
4153 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004154 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004155
4156 /*
4157 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4158 * user resets it to 0.
4159 */
4160 unsigned int signaled;
4161
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004162 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004163 int result;
4164};
4165
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004166#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004167 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004168 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004169 .signaled = 0, \
4170 .result = 0, \
4171 }
4172
4173struct k_poll_event {
4174 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004175 sys_dnode_t _node;
4176
4177 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004178 struct _poller *poller;
4179
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004180 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004181 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004182
Benjamin Walshacc68c12017-01-29 18:57:45 -05004183 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004184 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004185
4186 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004187 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004188
4189 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004190 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004191
4192 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004193 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004194
4195 /* per-type data */
4196 union {
4197 void *obj;
4198 struct k_poll_signal *signal;
4199 struct k_sem *sem;
4200 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004201 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004202 };
4203};
4204
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004205#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004206 { \
4207 .poller = NULL, \
4208 .type = event_type, \
4209 .state = K_POLL_STATE_NOT_READY, \
4210 .mode = event_mode, \
4211 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004212 { .obj = event_obj }, \
4213 }
4214
4215#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4216 event_tag) \
4217 { \
4218 .type = event_type, \
4219 .tag = event_tag, \
4220 .state = K_POLL_STATE_NOT_READY, \
4221 .mode = event_mode, \
4222 .unused = 0, \
4223 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004224 }
4225
4226/**
4227 * @brief Initialize one struct k_poll_event instance
4228 *
4229 * After this routine is called on a poll event, the event it ready to be
4230 * placed in an event array to be passed to k_poll().
4231 *
4232 * @param event The event to initialize.
4233 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4234 * values. Only values that apply to the same object being polled
4235 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4236 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004237 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004238 * @param obj Kernel object or poll signal.
4239 *
4240 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004241 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004242 */
4243
Kumar Galacc334c72017-04-21 10:55:34 -05004244extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004245 int mode, void *obj);
4246
4247/**
4248 * @brief Wait for one or many of multiple poll events to occur
4249 *
4250 * This routine allows a thread to wait concurrently for one or many of
4251 * multiple poll events to have occurred. Such events can be a kernel object
4252 * being available, like a semaphore, or a poll signal event.
4253 *
4254 * When an event notifies that a kernel object is available, the kernel object
4255 * is not "given" to the thread calling k_poll(): it merely signals the fact
4256 * that the object was available when the k_poll() call was in effect. Also,
4257 * all threads trying to acquire an object the regular way, i.e. by pending on
4258 * the object, have precedence over the thread polling on the object. This
4259 * means that the polling thread will never get the poll event on an object
4260 * until the object becomes available and its pend queue is empty. For this
4261 * reason, the k_poll() call is more effective when the objects being polled
4262 * only have one thread, the polling thread, trying to acquire them.
4263 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004264 * When k_poll() returns 0, the caller should loop on all the events that were
4265 * passed to k_poll() and check the state field for the values that were
4266 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004267 *
4268 * Before being reused for another call to k_poll(), the user has to reset the
4269 * state field to K_POLL_STATE_NOT_READY.
4270 *
Andrew Boie3772f772018-05-07 16:52:57 -07004271 * When called from user mode, a temporary memory allocation is required from
4272 * the caller's resource pool.
4273 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004274 * @param events An array of pointers to events to be polled for.
4275 * @param num_events The number of events in the array.
4276 * @param timeout Waiting period for an event to be ready (in milliseconds),
4277 * or one of the special values K_NO_WAIT and K_FOREVER.
4278 *
4279 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004280 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004281 * @retval -EINTR Polling has been interrupted, e.g. with
4282 * k_queue_cancel_wait(). All output events are still set and valid,
4283 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4284 * words, -EINTR status means that at least one of output events is
4285 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004286 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4287 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004288 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004289 */
4290
Andrew Boie3772f772018-05-07 16:52:57 -07004291__syscall int k_poll(struct k_poll_event *events, int num_events,
4292 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004293
4294/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004295 * @brief Initialize a poll signal object.
4296 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004297 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004298 *
4299 * @param signal A poll signal.
4300 *
4301 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004302 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004303 */
4304
Andrew Boie3772f772018-05-07 16:52:57 -07004305__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4306
4307/*
4308 * @brief Reset a poll signal object's state to unsignaled.
4309 *
4310 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004311 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004312 */
4313__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4314
4315static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4316{
4317 signal->signaled = 0;
4318}
4319
4320/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004321 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004322 *
4323 * @param signal A poll signal object
4324 * @param signaled An integer buffer which will be written nonzero if the
4325 * object was signaled
4326 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004327 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004328 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004329 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004330 */
4331__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4332 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004333
4334/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004335 * @brief Signal a poll signal object.
4336 *
4337 * This routine makes ready a poll signal, which is basically a poll event of
4338 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4339 * made ready to run. A @a result value can be specified.
4340 *
4341 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004342 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004343 * k_poll_signal_reset(). It thus has to be reset by the user before being
4344 * passed again to k_poll() or k_poll() will consider it being signaled, and
4345 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004346 *
4347 * @param signal A poll signal.
4348 * @param result The value to store in the result field of the signal.
4349 *
4350 * @retval 0 The signal was delivered successfully.
4351 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004352 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004353 */
4354
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004355__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004356
Anas Nashif954d5502018-02-25 08:37:28 -06004357/**
4358 * @internal
4359 */
Andy Ross8606fab2018-03-26 10:54:40 -07004360extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004361
Anas Nashif166f5192018-02-25 08:02:36 -06004362/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004363
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004364/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004365 * @defgroup cpu_idle_apis CPU Idling APIs
4366 * @ingroup kernel_apis
4367 * @{
4368 */
4369
4370/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004371 * @brief Make the CPU idle.
4372 *
4373 * This function makes the CPU idle until an event wakes it up.
4374 *
4375 * In a regular system, the idle thread should be the only thread responsible
4376 * for making the CPU idle and triggering any type of power management.
4377 * However, in some more constrained systems, such as a single-threaded system,
4378 * the only thread would be responsible for this if needed.
4379 *
4380 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004381 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004382 */
4383extern void k_cpu_idle(void);
4384
4385/**
4386 * @brief Make the CPU idle in an atomic fashion.
4387 *
4388 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4389 * must be done atomically before making the CPU idle.
4390 *
4391 * @param key Interrupt locking key obtained from irq_lock().
4392 *
4393 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004394 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004395 */
4396extern void k_cpu_atomic_idle(unsigned int key);
4397
Anas Nashif30c3cff2019-01-22 08:18:13 -05004398/**
4399 * @}
4400 */
Anas Nashif954d5502018-02-25 08:37:28 -06004401
4402/**
4403 * @internal
4404 */
Kumar Galacc334c72017-04-21 10:55:34 -05004405extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004406
Andrew Boiecdb94d62017-04-18 15:22:05 -07004407#ifdef _ARCH_EXCEPT
4408/* This archtecture has direct support for triggering a CPU exception */
4409#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4410#else
4411
Andrew Boiecdb94d62017-04-18 15:22:05 -07004412/* NOTE: This is the implementation for arches that do not implement
4413 * _ARCH_EXCEPT() to generate a real CPU exception.
4414 *
4415 * We won't have a real exception frame to determine the PC value when
4416 * the oops occurred, so print file and line number before we jump into
4417 * the fatal error handler.
4418 */
4419#define _k_except_reason(reason) do { \
4420 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4421 _NanoFatalErrorHandler(reason, &_default_esf); \
4422 CODE_UNREACHABLE; \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004423 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004424
4425#endif /* _ARCH__EXCEPT */
4426
4427/**
4428 * @brief Fatally terminate a thread
4429 *
4430 * This should be called when a thread has encountered an unrecoverable
4431 * runtime condition and needs to terminate. What this ultimately
4432 * means is determined by the _fatal_error_handler() implementation, which
4433 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4434 *
4435 * If this is called from ISR context, the default system fatal error handler
4436 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004437 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004438 */
4439#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4440
4441/**
4442 * @brief Fatally terminate the system
4443 *
4444 * This should be called when the Zephyr kernel has encountered an
4445 * unrecoverable runtime condition and needs to terminate. What this ultimately
4446 * means is determined by the _fatal_error_handler() implementation, which
4447 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004448 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004449 */
4450#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4451
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004452/*
4453 * private APIs that are utilized by one or more public APIs
4454 */
4455
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004456#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004457/**
4458 * @internal
4459 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004460extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004461#else
Anas Nashif954d5502018-02-25 08:37:28 -06004462/**
4463 * @internal
4464 */
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004465#define _init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004466#endif
4467
Anas Nashif954d5502018-02-25 08:37:28 -06004468/**
4469 * @internal
4470 */
Flavio Ceolin09e362e2018-12-17 12:34:05 -08004471extern bool _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004472/**
4473 * @internal
4474 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004475extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004476
Andrew Boiedc5d9352017-06-02 12:56:47 -07004477/* arch/cpu.h may declare an architecture or platform-specific macro
4478 * for properly declaring stacks, compatible with MMU/MPU constraints if
4479 * enabled
4480 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004481
4482/**
4483 * @brief Obtain an extern reference to a stack
4484 *
4485 * This macro properly brings the symbol of a thread stack declared
4486 * elsewhere into scope.
4487 *
4488 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004489 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004490 */
4491#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4492
Andrew Boiedc5d9352017-06-02 12:56:47 -07004493#ifdef _ARCH_THREAD_STACK_DEFINE
4494#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4495#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4496 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304497#define K_THREAD_STACK_LEN(size) _ARCH_THREAD_STACK_LEN(size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004498#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4499#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004500static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004501{
4502 return _ARCH_THREAD_STACK_BUFFER(sym);
4503}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004504#else
4505/**
4506 * @brief Declare a toplevel thread stack memory region
4507 *
4508 * This declares a region of memory suitable for use as a thread's stack.
4509 *
4510 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4511 * 'noinit' section so that it isn't zeroed at boot
4512 *
Andrew Boie507852a2017-07-25 18:47:07 -07004513 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004514 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004515 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004516 *
4517 * It is legal to precede this definition with the 'static' keyword.
4518 *
4519 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4520 * parameter of k_thread_create(), it may not be the same as the
4521 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4522 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004523 * Some arches may round the size of the usable stack region up to satisfy
4524 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4525 * size.
4526 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004527 * @param sym Thread stack symbol name
4528 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004529 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004530 */
4531#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004532 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004533
4534/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304535 * @brief Calculate size of stacks to be allocated in a stack array
4536 *
4537 * This macro calculates the size to be allocated for the stacks
4538 * inside a stack array. It accepts the indicated "size" as a parameter
4539 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4540 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4541 *
4542 * @param size Size of the stack memory region
4543 * @req K-TSTACK-001
4544 */
4545#define K_THREAD_STACK_LEN(size) (size)
4546
4547/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004548 * @brief Declare a toplevel array of thread stack memory regions
4549 *
4550 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4551 * definition for additional details and constraints.
4552 *
4553 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4554 * 'noinit' section so that it isn't zeroed at boot
4555 *
4556 * @param sym Thread stack symbol name
4557 * @param nmemb Number of stacks to declare
4558 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004559 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004560 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004561#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004562 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304563 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004564
4565/**
4566 * @brief Declare an embedded stack memory region
4567 *
4568 * Used for stacks embedded within other data structures. Use is highly
4569 * discouraged but in some cases necessary. For memory protection scenarios,
4570 * it is very important that any RAM preceding this member not be writable
4571 * by threads else a stack overflow will lead to silent corruption. In other
4572 * words, the containing data structure should live in RAM owned by the kernel.
4573 *
4574 * @param sym Thread stack symbol name
4575 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004576 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004577 */
4578#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004579 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004580
4581/**
4582 * @brief Return the size in bytes of a stack memory region
4583 *
4584 * Convenience macro for passing the desired stack size to k_thread_create()
4585 * since the underlying implementation may actually create something larger
4586 * (for instance a guard area).
4587 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004588 * The value returned here is not guaranteed to match the 'size' parameter
4589 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004590 *
4591 * @param sym Stack memory symbol
4592 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004593 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004594 */
4595#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4596
4597/**
4598 * @brief Get a pointer to the physical stack buffer
4599 *
4600 * Convenience macro to get at the real underlying stack buffer used by
4601 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4602 * This is really only intended for diagnostic tools which want to examine
4603 * stack memory contents.
4604 *
4605 * @param sym Declared stack symbol name
4606 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004607 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004608 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004609static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004610{
4611 return (char *)sym;
4612}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004613
4614#endif /* _ARCH_DECLARE_STACK */
4615
Chunlin Hane9c97022017-07-07 20:29:30 +08004616/**
4617 * @defgroup mem_domain_apis Memory domain APIs
4618 * @ingroup kernel_apis
4619 * @{
4620 */
4621
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004622/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004623 * @def K_MEM_PARTITION_DEFINE
4624 * @brief Used to declare a memory partition
4625 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004626 */
4627#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4628#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4629 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304630 __kernel struct k_mem_partition name =\
Ioannis Glaropoulos293247e2018-12-03 18:32:32 +01004631 { (u32_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004632#else
4633#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304634 __kernel struct k_mem_partition name =\
Ioannis Glaropoulos293247e2018-12-03 18:32:32 +01004635 { (u32_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004636#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4637
Chunlin Hane9c97022017-07-07 20:29:30 +08004638/* memory partition */
4639struct k_mem_partition {
4640 /* start address of memory partition */
4641 u32_t start;
4642 /* size of memory partition */
4643 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004644#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08004645 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304646 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004647#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08004648};
4649
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02004650/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304651 * Note: Always declare this structure with __kernel prefix
4652 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004653struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304654#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004655 /* partitions in the domain */
4656 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304657#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004658 /* domain q */
4659 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004660 /* number of partitions in the domain */
4661 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004662};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304663
Chunlin Hane9c97022017-07-07 20:29:30 +08004664
4665/**
4666 * @brief Initialize a memory domain.
4667 *
4668 * Initialize a memory domain with given name and memory partitions.
4669 *
4670 * @param domain The memory domain to be initialized.
4671 * @param num_parts The number of array items of "parts" parameter.
4672 * @param parts An array of pointers to the memory partitions. Can be NULL
4673 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004674 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004675 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004676extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004677 struct k_mem_partition *parts[]);
4678/**
4679 * @brief Destroy a memory domain.
4680 *
4681 * Destroy a memory domain.
4682 *
4683 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004684 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004685 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004686extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4687
4688/**
4689 * @brief Add a memory partition into a memory domain.
4690 *
4691 * Add a memory partition into a memory domain.
4692 *
4693 * @param domain The memory domain to be added a memory partition.
4694 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004695 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004696 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004697extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4698 struct k_mem_partition *part);
4699
4700/**
4701 * @brief Remove a memory partition from a memory domain.
4702 *
4703 * Remove a memory partition from a memory domain.
4704 *
4705 * @param domain The memory domain to be removed a memory partition.
4706 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004707 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004708 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004709extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4710 struct k_mem_partition *part);
4711
4712/**
4713 * @brief Add a thread into a memory domain.
4714 *
4715 * Add a thread into a memory domain.
4716 *
4717 * @param domain The memory domain that the thread is going to be added into.
4718 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004719 *
4720 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004721 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004722extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4723 k_tid_t thread);
4724
4725/**
4726 * @brief Remove a thread from its memory domain.
4727 *
4728 * Remove a thread from its memory domain.
4729 *
4730 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004731 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004732 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004733extern void k_mem_domain_remove_thread(k_tid_t thread);
4734
Anas Nashif166f5192018-02-25 08:02:36 -06004735/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004736
Andrew Boie756f9072017-10-10 16:01:49 -07004737/**
4738 * @brief Emit a character buffer to the console device
4739 *
4740 * @param c String of characters to print
4741 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004742 *
4743 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004744 */
4745__syscall void k_str_out(char *c, size_t n);
4746
Andy Rosse7172672018-01-24 15:48:32 -08004747/**
4748 * @brief Start a numbered CPU on a MP-capable system
4749
4750 * This starts and initializes a specific CPU. The main thread on
4751 * startup is running on CPU zero, other processors are numbered
4752 * sequentially. On return from this function, the CPU is known to
4753 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004754 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004755 * with the provided key will work to enable them.
4756 *
4757 * Normally, in SMP mode this function will be called by the kernel
4758 * initialization and should not be used as a user API. But it is
4759 * defined here for special-purpose apps which want Zephyr running on
4760 * one core and to use others for design-specific processing.
4761 *
4762 * @param cpu_num Integer number of the CPU
4763 * @param stack Stack memory for the CPU
4764 * @param sz Stack buffer size, in bytes
4765 * @param fn Function to begin running on the CPU. First argument is
4766 * an irq_unlock() key.
4767 * @param arg Untyped argument to be passed to "fn"
4768 */
4769extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08004770 void (*fn)(int key, void *data), void *arg);
Andy Rosse7172672018-01-24 15:48:32 -08004771
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004772#ifdef __cplusplus
4773}
4774#endif
4775
Anas Nashifb6304e62018-07-04 08:03:03 -05004776#include <tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07004777#include <syscalls/kernel.h>
4778
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004779#endif /* !_ASMLANGUAGE */
4780
Flavio Ceolin67ca1762018-09-14 10:43:44 -07004781#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */