blob: 6050e1cf8e892bba64aa5816e6d45747f01aa963 [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 {
391 sys_dlist_t qnode_dlist;
392 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
876/* timeout is not in use */
877#define _INACTIVE (-1)
878
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400879struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700880 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700881 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400882 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700883 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500884 void *init_p1;
885 void *init_p2;
886 void *init_p3;
887 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500888 u32_t init_options;
889 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500890 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600891 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400892};
893
Andrew Boied26cf2d2017-03-30 13:07:02 -0700894#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400895 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600896 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500897 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700898 .init_thread = (thread), \
899 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500900 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700901 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400902 .init_p1 = (void *)p1, \
903 .init_p2 = (void *)p2, \
904 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500905 .init_prio = (prio), \
906 .init_options = (options), \
907 .init_delay = (delay), \
908 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600909 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400910 }
911
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400912/**
Allan Stephensc98da842016-11-11 15:45:03 -0500913 * INTERNAL_HIDDEN @endcond
914 */
915
916/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500917 * @brief Statically define and initialize a thread.
918 *
919 * The thread may be scheduled for immediate execution or a delayed start.
920 *
921 * Thread options are architecture-specific, and can include K_ESSENTIAL,
922 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
923 * them using "|" (the logical OR operator).
924 *
925 * The ID of the thread can be accessed using:
926 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500927 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500928 *
929 * @param name Name of the thread.
930 * @param stack_size Stack size in bytes.
931 * @param entry Thread entry function.
932 * @param p1 1st entry point parameter.
933 * @param p2 2nd entry point parameter.
934 * @param p3 3rd entry point parameter.
935 * @param prio Thread priority.
936 * @param options Thread options.
937 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400938 *
Anas Nashif47420d02018-05-24 14:20:56 -0400939 * @req K-THREAD-010
940 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400941 * @internal It has been observed that the x86 compiler by default aligns
942 * these _static_thread_data structures to 32-byte boundaries, thereby
943 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400944 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400945 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500946#define K_THREAD_DEFINE(name, stack_size, \
947 entry, p1, p2, p3, \
948 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700949 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700950 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500951 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500952 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700953 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
954 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500955 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -0600956 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700957 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400958
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400959/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500960 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500962 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400963 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500964 * @param thread ID of thread whose priority is needed.
965 *
966 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400967 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400968 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700969__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400970
971/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500972 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500974 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400975 *
976 * Rescheduling can occur immediately depending on the priority @a thread is
977 * set to:
978 *
979 * - If its priority is raised above the priority of the caller of this
980 * function, and the caller is preemptible, @a thread will be scheduled in.
981 *
982 * - If the caller operates on itself, it lowers its priority below that of
983 * other threads in the system, and the caller is preemptible, the thread of
984 * highest priority will be scheduled in.
985 *
986 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
987 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
988 * highest priority.
989 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500990 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400991 * @param prio New priority.
992 *
993 * @warning Changing the priority of a thread currently involved in mutex
994 * priority inheritance may result in undefined behavior.
995 *
996 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400997 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400998 */
Andrew Boie468190a2017-09-29 14:00:48 -0700999__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001000
Andy Ross4a2e50f2018-05-15 11:06:25 -07001001
1002#ifdef CONFIG_SCHED_DEADLINE
1003/**
1004 * @brief Set deadline expiration time for scheduler
1005 *
1006 * This sets the "deadline" expiration as a time delta from the
1007 * current time, in the same units used by k_cycle_get_32(). The
1008 * scheduler (when deadline scheduling is enabled) will choose the
1009 * next expiring thread when selecting between threads at the same
1010 * static priority. Threads at different priorities will be scheduled
1011 * according to their static priority.
1012 *
1013 * @note Deadlines that are negative (i.e. in the past) are still seen
1014 * as higher priority than others, even if the thread has "finished"
1015 * its work. If you don't want it scheduled anymore, you have to
1016 * reset the deadline into the future, block/pend the thread, or
1017 * modify its priority with k_thread_priority_set().
1018 *
1019 * @note Despite the API naming, the scheduler makes no guarantees the
1020 * the thread WILL be scheduled within that deadline, nor does it take
1021 * extra metadata (like e.g. the "runtime" and "period" parameters in
1022 * Linux sched_setattr()) that allows the kernel to validate the
1023 * scheduling for achievability. Such features could be implemented
1024 * above this call, which is simply input to the priority selection
1025 * logic.
1026 *
1027 * @param thread A thread on which to set the deadline
1028 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001029 *
1030 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001031 */
1032__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1033#endif
1034
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001035/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001036 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001038 * This routine prevents the kernel scheduler from making @a thread the
1039 * current thread. All other internal operations on @a thread are still
1040 * performed; for example, any timeout it is waiting on keeps ticking,
1041 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001042 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001043 * If @a thread is already suspended, the routine has no effect.
1044 *
1045 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001046 *
1047 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001048 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001049 */
Andrew Boie468190a2017-09-29 14:00:48 -07001050__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001051
1052/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001053 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001055 * This routine allows the kernel scheduler to make @a thread the current
1056 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001058 * If @a thread is not currently suspended, the routine has no effect.
1059 *
1060 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001061 *
1062 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001063 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001064 */
Andrew Boie468190a2017-09-29 14:00:48 -07001065__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001066
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001067/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001068 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001069 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001070 * This routine specifies how the scheduler will perform time slicing of
1071 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001073 * To enable time slicing, @a slice must be non-zero. The scheduler
1074 * ensures that no thread runs for more than the specified time limit
1075 * before other threads of that priority are given a chance to execute.
1076 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001077 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001078 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001079 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001080 * execute. Once the scheduler selects a thread for execution, there is no
1081 * minimum guaranteed time the thread will execute before threads of greater or
1082 * equal priority are scheduled.
1083 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001084 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001085 * for execution, this routine has no effect; the thread is immediately
1086 * rescheduled after the slice period expires.
1087 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001088 * To disable timeslicing, set both @a slice and @a prio to zero.
1089 *
1090 * @param slice Maximum time slice length (in milliseconds).
1091 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001092 *
1093 * @return N/A
1094 */
Kumar Galacc334c72017-04-21 10:55:34 -05001095extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001096
Anas Nashif166f5192018-02-25 08:02:36 -06001097/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001098
1099/**
1100 * @addtogroup isr_apis
1101 * @{
1102 */
1103
1104/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001105 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001106 *
Allan Stephensc98da842016-11-11 15:45:03 -05001107 * This routine allows the caller to customize its actions, depending on
1108 * whether it is a thread or an ISR.
1109 *
1110 * @note Can be called by ISRs.
1111 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001112 * @return false if invoked by a thread.
1113 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001114 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001115extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001116
Benjamin Walsh445830d2016-11-10 15:54:27 -05001117/**
1118 * @brief Determine if code is running in a preemptible thread.
1119 *
Allan Stephensc98da842016-11-11 15:45:03 -05001120 * This routine allows the caller to customize its actions, depending on
1121 * whether it can be preempted by another thread. The routine returns a 'true'
1122 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001123 *
Allan Stephensc98da842016-11-11 15:45:03 -05001124 * - The code is running in a thread, not at ISR.
1125 * - The thread's priority is in the preemptible range.
1126 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001127 *
Allan Stephensc98da842016-11-11 15:45:03 -05001128 * @note Can be called by ISRs.
1129 *
1130 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001131 * @return Non-zero if invoked by a preemptible thread.
1132 */
Andrew Boie468190a2017-09-29 14:00:48 -07001133__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001134
Allan Stephensc98da842016-11-11 15:45:03 -05001135/**
Anas Nashif166f5192018-02-25 08:02:36 -06001136 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001137 */
1138
1139/**
1140 * @addtogroup thread_apis
1141 * @{
1142 */
1143
1144/**
1145 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001146 *
Allan Stephensc98da842016-11-11 15:45:03 -05001147 * This routine prevents the current thread from being preempted by another
1148 * thread by instructing the scheduler to treat it as a cooperative thread.
1149 * If the thread subsequently performs an operation that makes it unready,
1150 * it will be context switched out in the normal manner. When the thread
1151 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001152 *
Allan Stephensc98da842016-11-11 15:45:03 -05001153 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001154 *
Allan Stephensc98da842016-11-11 15:45:03 -05001155 * @note k_sched_lock() and k_sched_unlock() should normally be used
1156 * when the operation being performed can be safely interrupted by ISRs.
1157 * However, if the amount of processing involved is very small, better
1158 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001159 *
1160 * @return N/A
1161 */
1162extern void k_sched_lock(void);
1163
Allan Stephensc98da842016-11-11 15:45:03 -05001164/**
1165 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001166 *
Allan Stephensc98da842016-11-11 15:45:03 -05001167 * This routine reverses the effect of a previous call to k_sched_lock().
1168 * A thread must call the routine once for each time it called k_sched_lock()
1169 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001170 *
1171 * @return N/A
1172 */
1173extern void k_sched_unlock(void);
1174
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001175/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001176 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001177 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001178 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001180 * Custom data is not used by the kernel itself, and is freely available
1181 * for a thread to use as it sees fit. It can be used as a framework
1182 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001183 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001184 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001185 *
1186 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001187 *
1188 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001189 */
Andrew Boie468190a2017-09-29 14:00:48 -07001190__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001191
1192/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001193 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001195 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001197 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001198 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001199 */
Andrew Boie468190a2017-09-29 14:00:48 -07001200__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001201
1202/**
Anas Nashif57554052018-03-03 02:31:05 -06001203 * @brief Set current thread name
1204 *
1205 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1206 * tracing and debugging.
1207 *
1208 */
1209__syscall void k_thread_name_set(k_tid_t thread_id, const char *value);
1210
1211/**
1212 * @brief Get thread name
1213 *
1214 * Get the name of a thread
1215 *
1216 * @param thread_id Thread ID
1217 *
1218 */
1219__syscall const char *k_thread_name_get(k_tid_t thread_id);
1220
1221/**
Andy Rosscfe62032018-09-29 07:34:55 -07001222 * @}
1223 */
1224
1225/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001226 * @addtogroup clock_apis
1227 * @{
1228 */
1229
1230/**
1231 * @brief Generate null timeout delay.
1232 *
1233 * This macro generates a timeout delay that that instructs a kernel API
1234 * not to wait if the requested operation cannot be performed immediately.
1235 *
1236 * @return Timeout delay value.
1237 */
1238#define K_NO_WAIT 0
1239
1240/**
1241 * @brief Generate timeout delay from milliseconds.
1242 *
1243 * This macro generates a timeout delay that that instructs a kernel API
1244 * to wait up to @a ms milliseconds to perform the requested operation.
1245 *
1246 * @param ms Duration in milliseconds.
1247 *
1248 * @return Timeout delay value.
1249 */
Johan Hedberg14471692016-11-13 10:52:15 +02001250#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001251
1252/**
1253 * @brief Generate timeout delay from seconds.
1254 *
1255 * This macro generates a timeout delay that that instructs a kernel API
1256 * to wait up to @a s seconds to perform the requested operation.
1257 *
1258 * @param s Duration in seconds.
1259 *
1260 * @return Timeout delay value.
1261 */
Johan Hedberg14471692016-11-13 10:52:15 +02001262#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001263
1264/**
1265 * @brief Generate timeout delay from minutes.
1266 *
1267 * This macro generates a timeout delay that that instructs a kernel API
1268 * to wait up to @a m minutes to perform the requested operation.
1269 *
1270 * @param m Duration in minutes.
1271 *
1272 * @return Timeout delay value.
1273 */
Johan Hedberg14471692016-11-13 10:52:15 +02001274#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001275
1276/**
1277 * @brief Generate timeout delay from hours.
1278 *
1279 * This macro generates a timeout delay that that instructs a kernel API
1280 * to wait up to @a h hours to perform the requested operation.
1281 *
1282 * @param h Duration in hours.
1283 *
1284 * @return Timeout delay value.
1285 */
Johan Hedberg14471692016-11-13 10:52:15 +02001286#define K_HOURS(h) K_MINUTES((h) * 60)
1287
Allan Stephensc98da842016-11-11 15:45:03 -05001288/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001289 * @brief Generate infinite timeout delay.
1290 *
1291 * This macro generates a timeout delay that that instructs a kernel API
1292 * to wait as long as necessary to perform the requested operation.
1293 *
1294 * @return Timeout delay value.
1295 */
1296#define K_FOREVER (-1)
1297
1298/**
Anas Nashif166f5192018-02-25 08:02:36 -06001299 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001300 */
1301
1302/**
Allan Stephensc98da842016-11-11 15:45:03 -05001303 * @cond INTERNAL_HIDDEN
1304 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001305
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001306struct k_timer {
1307 /*
1308 * _timeout structure must be first here if we want to use
1309 * dynamic timer allocation. timeout.node is used in the double-linked
1310 * list of free timers
1311 */
1312 struct _timeout timeout;
1313
Allan Stephens45bfa372016-10-12 12:39:42 -05001314 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001315 _wait_q_t wait_q;
1316
1317 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001318 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001319
1320 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001321 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001322
1323 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001324 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001325
Allan Stephens45bfa372016-10-12 12:39:42 -05001326 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001327 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001328
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001329 /* user-specific data, also used to support legacy features */
1330 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001331
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001332 _OBJECT_TRACING_NEXT_PTR(k_timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001333};
1334
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001335#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001336 { \
Andy Ross987c0e52018-09-27 16:50:00 -07001337 .timeout.dticks = _INACTIVE, \
1338 .timeout.fn = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001339 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001340 .expiry_fn = expiry, \
1341 .stop_fn = stop, \
1342 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001343 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001344 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001345 }
1346
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001347#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1348
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001349/**
Allan Stephensc98da842016-11-11 15:45:03 -05001350 * INTERNAL_HIDDEN @endcond
1351 */
1352
1353/**
1354 * @defgroup timer_apis Timer APIs
1355 * @ingroup kernel_apis
1356 * @{
1357 */
1358
1359/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001360 * @typedef k_timer_expiry_t
1361 * @brief Timer expiry function type.
1362 *
1363 * A timer's expiry function is executed by the system clock interrupt handler
1364 * each time the timer expires. The expiry function is optional, and is only
1365 * invoked if the timer has been initialized with one.
1366 *
1367 * @param timer Address of timer.
1368 *
1369 * @return N/A
1370 */
1371typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1372
1373/**
1374 * @typedef k_timer_stop_t
1375 * @brief Timer stop function type.
1376 *
1377 * A timer's stop function is executed if the timer is stopped prematurely.
1378 * The function runs in the context of the thread that stops the timer.
1379 * The stop function is optional, and is only invoked if the timer has been
1380 * initialized with one.
1381 *
1382 * @param timer Address of timer.
1383 *
1384 * @return N/A
1385 */
1386typedef void (*k_timer_stop_t)(struct k_timer *timer);
1387
1388/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001389 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001391 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001392 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001393 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001394 *
1395 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001396 * @param expiry_fn Function to invoke each time the timer expires.
1397 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001398 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001399#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001400 struct k_timer name \
1401 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001402 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001403
Allan Stephens45bfa372016-10-12 12:39:42 -05001404/**
1405 * @brief Initialize a timer.
1406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001407 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001408 *
1409 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001410 * @param expiry_fn Function to invoke each time the timer expires.
1411 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001412 *
1413 * @return N/A
1414 */
1415extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001416 k_timer_expiry_t expiry_fn,
1417 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001418
Allan Stephens45bfa372016-10-12 12:39:42 -05001419/**
1420 * @brief Start a timer.
1421 *
1422 * This routine starts a timer, and resets its status to zero. The timer
1423 * begins counting down using the specified duration and period values.
1424 *
1425 * Attempting to start a timer that is already running is permitted.
1426 * The timer's status is reset to zero and the timer begins counting down
1427 * using the new duration and period values.
1428 *
1429 * @param timer Address of timer.
1430 * @param duration Initial timer duration (in milliseconds).
1431 * @param period Timer period (in milliseconds).
1432 *
1433 * @return N/A
1434 */
Andrew Boiea354d492017-09-29 16:22:28 -07001435__syscall void k_timer_start(struct k_timer *timer,
1436 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001437
1438/**
1439 * @brief Stop a timer.
1440 *
1441 * This routine stops a running timer prematurely. The timer's stop function,
1442 * if one exists, is invoked by the caller.
1443 *
1444 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001445 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001446 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001447 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1448 * if @a k_timer_stop is to be called from ISRs.
1449 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001450 * @param timer Address of timer.
1451 *
1452 * @return N/A
1453 */
Andrew Boiea354d492017-09-29 16:22:28 -07001454__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001455
1456/**
1457 * @brief Read timer status.
1458 *
1459 * This routine reads the timer's status, which indicates the number of times
1460 * it has expired since its status was last read.
1461 *
1462 * Calling this routine resets the timer's status to zero.
1463 *
1464 * @param timer Address of timer.
1465 *
1466 * @return Timer status.
1467 */
Andrew Boiea354d492017-09-29 16:22:28 -07001468__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001469
1470/**
1471 * @brief Synchronize thread to timer expiration.
1472 *
1473 * This routine blocks the calling thread until the timer's status is non-zero
1474 * (indicating that it has expired at least once since it was last examined)
1475 * or the timer is stopped. If the timer status is already non-zero,
1476 * or the timer is already stopped, the caller continues without waiting.
1477 *
1478 * Calling this routine resets the timer's status to zero.
1479 *
1480 * This routine must not be used by interrupt handlers, since they are not
1481 * allowed to block.
1482 *
1483 * @param timer Address of timer.
1484 *
1485 * @return Timer status.
1486 */
Andrew Boiea354d492017-09-29 16:22:28 -07001487__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001488
Andy Ross52e444b2018-09-28 09:06:37 -07001489extern s32_t z_timeout_remaining(struct _timeout *timeout);
1490
Allan Stephens45bfa372016-10-12 12:39:42 -05001491/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001492 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001493 *
1494 * This routine computes the (approximate) time remaining before a running
1495 * timer next expires. If the timer is not running, it returns zero.
1496 *
1497 * @param timer Address of timer.
1498 *
1499 * @return Remaining time (in milliseconds).
1500 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001501__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001502
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001503static inline u32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001504{
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001505 return (u32_t)__ticks_to_ms(z_timeout_remaining(&timer->timeout));
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001506}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001507
Allan Stephensc98da842016-11-11 15:45:03 -05001508/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001509 * @brief Associate user-specific data with a timer.
1510 *
1511 * This routine records the @a user_data with the @a timer, to be retrieved
1512 * later.
1513 *
1514 * It can be used e.g. in a timer handler shared across multiple subsystems to
1515 * retrieve data specific to the subsystem this timer is associated with.
1516 *
1517 * @param timer Address of timer.
1518 * @param user_data User data to associate with the timer.
1519 *
1520 * @return N/A
1521 */
Andrew Boiea354d492017-09-29 16:22:28 -07001522__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1523
Anas Nashif954d5502018-02-25 08:37:28 -06001524/**
1525 * @internal
1526 */
Andrew Boiea354d492017-09-29 16:22:28 -07001527static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1528 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001529{
1530 timer->user_data = user_data;
1531}
1532
1533/**
1534 * @brief Retrieve the user-specific data from a timer.
1535 *
1536 * @param timer Address of timer.
1537 *
1538 * @return The user data.
1539 */
Andrew Boiea354d492017-09-29 16:22:28 -07001540__syscall void *k_timer_user_data_get(struct k_timer *timer);
1541
1542static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001543{
1544 return timer->user_data;
1545}
1546
Anas Nashif166f5192018-02-25 08:02:36 -06001547/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001548
Allan Stephensc98da842016-11-11 15:45:03 -05001549/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001550 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001551 * @{
1552 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001553
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001554/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001555 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001557 * This routine returns the elapsed time since the system booted,
1558 * in milliseconds.
1559 *
1560 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001561 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001562__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001563
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001564/**
1565 * @brief Enable clock always on in tickless kernel
1566 *
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001567 * This routine enables keeping the clock running (that is, it always
1568 * keeps an active timer interrupt scheduled) when there are no timer
1569 * events programmed in tickless kernel scheduling. This is necessary
1570 * if the clock is used to track passage of time (e.g. via
1571 * k_uptime_get_32()), otherwise the internal hardware counter may
1572 * roll over between interrupts.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001573 *
1574 * @retval prev_status Previous status of always on flag
1575 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001576int k_enable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001577
1578/**
1579 * @brief Disable clock always on in tickless kernel
1580 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001581 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001582 * there are no timer events programmed in tickless kernel
1583 * scheduling. To save power, this routine should be called
1584 * immediately when clock is not used to track time.
1585 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001586void k_disable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001587
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001588/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001589 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001591 * This routine returns the lower 32-bits of the elapsed time since the system
1592 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001593 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001594 * This routine can be more efficient than k_uptime_get(), as it reduces the
1595 * need for interrupt locking and 64-bit math. However, the 32-bit result
1596 * cannot hold a system uptime time larger than approximately 50 days, so the
1597 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001598 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001599 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001600 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001601__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001602
1603/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001604 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001606 * This routine computes the elapsed time between the current system uptime
1607 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001608 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001609 * @param reftime Pointer to a reference time, which is updated to the current
1610 * uptime upon return.
1611 *
1612 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001613 */
Andy Ross987c0e52018-09-27 16:50:00 -07001614static inline s64_t k_uptime_delta(s64_t *reftime)
1615{
1616 s64_t uptime, delta;
1617
1618 uptime = k_uptime_get();
1619 delta = uptime - *reftime;
1620 *reftime = uptime;
1621
1622 return delta;
1623}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001624
1625/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001626 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001627 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001628 * This routine computes the elapsed time between the current system uptime
1629 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001631 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1632 * need for interrupt locking and 64-bit math. However, the 32-bit result
1633 * cannot hold an elapsed time larger than approximately 50 days, so the
1634 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001635 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001636 * @param reftime Pointer to a reference time, which is updated to the current
1637 * uptime upon return.
1638 *
1639 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001640 */
Andy Ross987c0e52018-09-27 16:50:00 -07001641static inline u32_t k_uptime_delta_32(s64_t *reftime)
1642{
1643 return (u32_t)k_uptime_delta(reftime);
1644}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001645
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001646/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001647 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001648 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001649 * This routine returns the current time, as measured by the system's hardware
1650 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001651 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001652 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001653 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001654#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001655
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001656/**
Anas Nashif166f5192018-02-25 08:02:36 -06001657 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001658 */
1659
Allan Stephensc98da842016-11-11 15:45:03 -05001660/**
1661 * @cond INTERNAL_HIDDEN
1662 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001663
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001664struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001665 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001666 union {
1667 _wait_q_t wait_q;
1668
1669 _POLL_EVENT;
1670 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001671
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001672 _OBJECT_TRACING_NEXT_PTR(k_queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001673};
1674
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001675#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001676 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001677 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001678 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001679 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001680 _OBJECT_TRACING_INIT \
1681 }
1682
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001683#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1684
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001685extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1686
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001687/**
1688 * INTERNAL_HIDDEN @endcond
1689 */
1690
1691/**
1692 * @defgroup queue_apis Queue APIs
1693 * @ingroup kernel_apis
1694 * @{
1695 */
1696
1697/**
1698 * @brief Initialize a queue.
1699 *
1700 * This routine initializes a queue object, prior to its first use.
1701 *
1702 * @param queue Address of the queue.
1703 *
1704 * @return N/A
1705 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001706__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001707
1708/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001709 * @brief Cancel waiting on a queue.
1710 *
1711 * This routine causes first thread pending on @a queue, if any, to
1712 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001713 * If the queue is being waited on by k_poll(), it will return with
1714 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1715 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001716 *
1717 * @note Can be called by ISRs.
1718 *
1719 * @param queue Address of the queue.
1720 *
1721 * @return N/A
1722 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001723__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001724
1725/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001726 * @brief Append an element to the end of a queue.
1727 *
1728 * This routine appends a data item to @a queue. A queue data item must be
1729 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1730 * reserved for the kernel's use.
1731 *
1732 * @note Can be called by ISRs.
1733 *
1734 * @param queue Address of the queue.
1735 * @param data Address of the data item.
1736 *
1737 * @return N/A
1738 */
1739extern void k_queue_append(struct k_queue *queue, void *data);
1740
1741/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001742 * @brief Append an element to a queue.
1743 *
1744 * This routine appends a data item to @a queue. There is an implicit
1745 * memory allocation from the calling thread's resource pool, which is
1746 * automatically freed when the item is removed from the queue.
1747 *
1748 * @note Can be called by ISRs.
1749 *
1750 * @param queue Address of the queue.
1751 * @param data Address of the data item.
1752 *
1753 * @retval 0 on success
1754 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1755 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301756__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001757
1758/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001759 * @brief Prepend an element to a queue.
1760 *
1761 * This routine prepends a data item to @a queue. A queue data item must be
1762 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1763 * reserved for the kernel's use.
1764 *
1765 * @note Can be called by ISRs.
1766 *
1767 * @param queue Address of the queue.
1768 * @param data Address of the data item.
1769 *
1770 * @return N/A
1771 */
1772extern void k_queue_prepend(struct k_queue *queue, void *data);
1773
1774/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001775 * @brief Prepend an element to a queue.
1776 *
1777 * This routine prepends a data item to @a queue. There is an implicit
1778 * memory allocation from the calling thread's resource pool, which is
1779 * automatically freed when the item is removed from the queue.
1780 *
1781 * @note Can be called by ISRs.
1782 *
1783 * @param queue Address of the queue.
1784 * @param data Address of the data item.
1785 *
1786 * @retval 0 on success
1787 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1788 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301789__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001790
1791/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001792 * @brief Inserts an element to a queue.
1793 *
1794 * This routine inserts a data item to @a queue after previous item. A queue
1795 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1796 * item are reserved for the kernel's use.
1797 *
1798 * @note Can be called by ISRs.
1799 *
1800 * @param queue Address of the queue.
1801 * @param prev Address of the previous data item.
1802 * @param data Address of the data item.
1803 *
1804 * @return N/A
1805 */
1806extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1807
1808/**
1809 * @brief Atomically append a list of elements to a queue.
1810 *
1811 * This routine adds a list of data items to @a queue in one operation.
1812 * The data items must be in a singly-linked list, with the first 32 bits
1813 * in each data item pointing to the next data item; the list must be
1814 * NULL-terminated.
1815 *
1816 * @note Can be called by ISRs.
1817 *
1818 * @param queue Address of the queue.
1819 * @param head Pointer to first node in singly-linked list.
1820 * @param tail Pointer to last node in singly-linked list.
1821 *
1822 * @return N/A
1823 */
1824extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1825
1826/**
1827 * @brief Atomically add a list of elements to a queue.
1828 *
1829 * This routine adds a list of data items to @a queue in one operation.
1830 * The data items must be in a singly-linked list implemented using a
1831 * sys_slist_t object. Upon completion, the original list is empty.
1832 *
1833 * @note Can be called by ISRs.
1834 *
1835 * @param queue Address of the queue.
1836 * @param list Pointer to sys_slist_t object.
1837 *
1838 * @return N/A
1839 */
1840extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1841
1842/**
1843 * @brief Get an element from a queue.
1844 *
1845 * This routine removes first data item from @a queue. The first 32 bits of the
1846 * data item are reserved for the kernel's use.
1847 *
1848 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1849 *
1850 * @param queue Address of the queue.
1851 * @param timeout Waiting period to obtain a data item (in milliseconds),
1852 * or one of the special values K_NO_WAIT and K_FOREVER.
1853 *
1854 * @return Address of the data item if successful; NULL if returned
1855 * without waiting, or waiting period timed out.
1856 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001857__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001858
1859/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001860 * @brief Remove an element from a queue.
1861 *
1862 * This routine removes data item from @a queue. The first 32 bits of the
1863 * data item are reserved for the kernel's use. Removing elements from k_queue
1864 * rely on sys_slist_find_and_remove which is not a constant time operation.
1865 *
1866 * @note Can be called by ISRs
1867 *
1868 * @param queue Address of the queue.
1869 * @param data Address of the data item.
1870 *
1871 * @return true if data item was removed
1872 */
1873static inline bool k_queue_remove(struct k_queue *queue, void *data)
1874{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001875 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001876}
1877
1878/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02001879 * @brief Append an element to a queue only if it's not present already.
1880 *
1881 * This routine appends data item to @a queue. The first 32 bits of the
1882 * data item are reserved for the kernel's use. Appending elements to k_queue
1883 * relies on sys_slist_is_node_in_list which is not a constant time operation.
1884 *
1885 * @note Can be called by ISRs
1886 *
1887 * @param queue Address of the queue.
1888 * @param data Address of the data item.
1889 *
1890 * @return true if data item was added, false if not
1891 */
1892static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
1893{
1894 sys_sfnode_t *test;
1895
1896 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
1897 if (test == (sys_sfnode_t *) data) {
1898 return false;
1899 }
1900 }
1901
1902 k_queue_append(queue, data);
1903 return true;
1904}
1905
1906/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001907 * @brief Query a queue to see if it has data available.
1908 *
1909 * Note that the data might be already gone by the time this function returns
1910 * if other threads are also trying to read from the queue.
1911 *
1912 * @note Can be called by ISRs.
1913 *
1914 * @param queue Address of the queue.
1915 *
1916 * @return Non-zero if the queue is empty.
1917 * @return 0 if data is available.
1918 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001919__syscall int k_queue_is_empty(struct k_queue *queue);
1920
1921static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001922{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001923 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001924}
1925
1926/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001927 * @brief Peek element at the head of queue.
1928 *
1929 * Return element from the head of queue without removing it.
1930 *
1931 * @param queue Address of the queue.
1932 *
1933 * @return Head element, or NULL if queue is empty.
1934 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001935__syscall void *k_queue_peek_head(struct k_queue *queue);
1936
1937static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001938{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001939 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001940}
1941
1942/**
1943 * @brief Peek element at the tail of queue.
1944 *
1945 * Return element from the tail of queue without removing it.
1946 *
1947 * @param queue Address of the queue.
1948 *
1949 * @return Tail element, or NULL if queue is empty.
1950 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001951__syscall void *k_queue_peek_tail(struct k_queue *queue);
1952
1953static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001954{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001955 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001956}
1957
1958/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001959 * @brief Statically define and initialize a queue.
1960 *
1961 * The queue can be accessed outside the module where it is defined using:
1962 *
1963 * @code extern struct k_queue <name>; @endcode
1964 *
1965 * @param name Name of the queue.
1966 */
1967#define K_QUEUE_DEFINE(name) \
1968 struct k_queue name \
1969 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001970 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001971
Anas Nashif166f5192018-02-25 08:02:36 -06001972/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001973
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001974struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001975 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001976};
1977
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04001978/**
1979 * @cond INTERNAL_HIDDEN
1980 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001981#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001982 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001983 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001984 }
1985
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001986#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1987
Allan Stephensc98da842016-11-11 15:45:03 -05001988/**
1989 * INTERNAL_HIDDEN @endcond
1990 */
1991
1992/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001993 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001994 * @ingroup kernel_apis
1995 * @{
1996 */
1997
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001998/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001999 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002000 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002001 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002002 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002003 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002004 *
2005 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002006 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002007 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002008#define k_fifo_init(fifo) \
2009 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002010
2011/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002012 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002013 *
2014 * This routine causes first thread pending on @a fifo, if any, to
2015 * return from k_fifo_get() call with NULL value (as if timeout
2016 * expired).
2017 *
2018 * @note Can be called by ISRs.
2019 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002020 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002021 *
2022 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002023 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002024 */
2025#define k_fifo_cancel_wait(fifo) \
2026 k_queue_cancel_wait((struct k_queue *) fifo)
2027
2028/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002029 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002030 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002031 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002032 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2033 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002035 * @note Can be called by ISRs.
2036 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002037 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002038 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002039 *
2040 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002041 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002042 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002043#define k_fifo_put(fifo, data) \
2044 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002045
2046/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002047 * @brief Add an element to a FIFO queue.
2048 *
2049 * This routine adds a data item to @a fifo. There is an implicit
2050 * memory allocation from the calling thread's resource pool, which is
2051 * automatically freed when the item is removed.
2052 *
2053 * @note Can be called by ISRs.
2054 *
2055 * @param fifo Address of the FIFO.
2056 * @param data Address of the data item.
2057 *
2058 * @retval 0 on success
2059 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002060 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002061 */
2062#define k_fifo_alloc_put(fifo, data) \
2063 k_queue_alloc_append((struct k_queue *) fifo, data)
2064
2065/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002066 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002067 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002068 * This routine adds a list of data items to @a fifo in one operation.
2069 * The data items must be in a singly-linked list, with the first 32 bits
2070 * each data item pointing to the next data item; the list must be
2071 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002073 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002074 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002075 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002076 * @param head Pointer to first node in singly-linked list.
2077 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002078 *
2079 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002080 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002081 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002082#define k_fifo_put_list(fifo, head, tail) \
2083 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002084
2085/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002086 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002087 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002088 * This routine adds a list of data items to @a fifo in one operation.
2089 * The data items must be in a singly-linked list implemented using a
2090 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002091 * and must be re-initialized via sys_slist_init().
2092 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002093 * @note Can be called by ISRs.
2094 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002095 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002096 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002097 *
2098 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002099 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002100 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002101#define k_fifo_put_slist(fifo, list) \
2102 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002103
2104/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002105 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002107 * This routine removes a data item from @a fifo in a "first in, first out"
2108 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002110 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2111 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002112 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002113 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002114 * or one of the special values K_NO_WAIT and K_FOREVER.
2115 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002116 * @return Address of the data item if successful; NULL if returned
2117 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002118 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002119 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002120#define k_fifo_get(fifo, timeout) \
2121 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002122
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002123/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002124 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002125 *
2126 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002127 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002128 *
2129 * @note Can be called by ISRs.
2130 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002131 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002132 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002133 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002134 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002135 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002136 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002137#define k_fifo_is_empty(fifo) \
2138 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002139
2140/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002141 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002142 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002143 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302144 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002145 * on each iteration of processing, a head container will be peeked,
2146 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002147 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002148 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002149 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002150 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002151 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002152 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002153 */
2154#define k_fifo_peek_head(fifo) \
2155 k_queue_peek_head((struct k_queue *) fifo)
2156
2157/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002158 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002159 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002160 * Return element from the tail of FIFO queue (without removing it). A usecase
2161 * for this is if elements of the FIFO queue are themselves containers. Then
2162 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002163 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002164 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002165 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002166 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002167 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002168 */
2169#define k_fifo_peek_tail(fifo) \
2170 k_queue_peek_tail((struct k_queue *) fifo)
2171
2172/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002173 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002174 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002175 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002176 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002177 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002178 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002179 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002180 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002181 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002182#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002183 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002184 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002185 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002186
Anas Nashif166f5192018-02-25 08:02:36 -06002187/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002188
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002189struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002190 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002191};
2192
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002193/**
2194 * @cond INTERNAL_HIDDEN
2195 */
2196
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002197#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002198 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002199 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002200 }
2201
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002202#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2203
Allan Stephensc98da842016-11-11 15:45:03 -05002204/**
2205 * INTERNAL_HIDDEN @endcond
2206 */
2207
2208/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002209 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002210 * @ingroup kernel_apis
2211 * @{
2212 */
2213
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002214/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002215 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002216 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002217 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002218 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002219 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002220 *
2221 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002222 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002223 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002224#define k_lifo_init(lifo) \
2225 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002226
2227/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002228 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002229 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002230 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002231 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2232 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002233 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002234 * @note Can be called by ISRs.
2235 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002236 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002237 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002238 *
2239 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002240 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002241 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002242#define k_lifo_put(lifo, data) \
2243 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002244
2245/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002246 * @brief Add an element to a LIFO queue.
2247 *
2248 * This routine adds a data item to @a lifo. There is an implicit
2249 * memory allocation from the calling thread's resource pool, which is
2250 * automatically freed when the item is removed.
2251 *
2252 * @note Can be called by ISRs.
2253 *
2254 * @param lifo Address of the LIFO.
2255 * @param data Address of the data item.
2256 *
2257 * @retval 0 on success
2258 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002259 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002260 */
2261#define k_lifo_alloc_put(lifo, data) \
2262 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2263
2264/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002265 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002267 * This routine removes a data item from @a lifo in a "last in, first out"
2268 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002269 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002270 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2271 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002272 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002273 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002274 * or one of the special values K_NO_WAIT and K_FOREVER.
2275 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002276 * @return Address of the data item if successful; NULL if returned
2277 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002278 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002279 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002280#define k_lifo_get(lifo, timeout) \
2281 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002282
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002283/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002284 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002285 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002286 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002287 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002288 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002290 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002291 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002292 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002293#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002294 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002295 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002296 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002297
Anas Nashif166f5192018-02-25 08:02:36 -06002298/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002299
2300/**
2301 * @cond INTERNAL_HIDDEN
2302 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302303#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002304
2305struct k_stack {
2306 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002307 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002308
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002309 _OBJECT_TRACING_NEXT_PTR(k_stack)
Andrew Boief3bee952018-05-02 17:44:39 -07002310 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002311};
2312
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002313#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002314 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002315 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002316 .base = stack_buffer, \
2317 .next = stack_buffer, \
2318 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002319 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002320 }
2321
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002322#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2323
Allan Stephensc98da842016-11-11 15:45:03 -05002324/**
2325 * INTERNAL_HIDDEN @endcond
2326 */
2327
2328/**
2329 * @defgroup stack_apis Stack APIs
2330 * @ingroup kernel_apis
2331 * @{
2332 */
2333
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002334/**
2335 * @brief Initialize a stack.
2336 *
2337 * This routine initializes a stack object, prior to its first use.
2338 *
2339 * @param stack Address of the stack.
2340 * @param buffer Address of array used to hold stacked values.
2341 * @param num_entries Maximum number of values that can be stacked.
2342 *
2343 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002344 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002345 */
Andrew Boief3bee952018-05-02 17:44:39 -07002346void k_stack_init(struct k_stack *stack,
Adithya Baglody28080d32018-10-15 11:48:51 +05302347 u32_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002348
2349
2350/**
2351 * @brief Initialize a stack.
2352 *
2353 * This routine initializes a stack object, prior to its first use. Internal
2354 * buffers will be allocated from the calling thread's resource pool.
2355 * This memory will be released if k_stack_cleanup() is called, or
2356 * userspace is enabled and the stack object loses all references to it.
2357 *
2358 * @param stack Address of the stack.
2359 * @param num_entries Maximum number of values that can be stacked.
2360 *
2361 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002362 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002363 */
2364
Adithya Baglody28080d32018-10-15 11:48:51 +05302365__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2366 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002367
2368/**
2369 * @brief Release a stack's allocated buffer
2370 *
2371 * If a stack object was given a dynamically allocated buffer via
2372 * k_stack_alloc_init(), this will free it. This function does nothing
2373 * if the buffer wasn't dynamically allocated.
2374 *
2375 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002376 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002377 */
2378void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002379
2380/**
2381 * @brief Push an element onto a stack.
2382 *
2383 * This routine adds a 32-bit value @a data to @a stack.
2384 *
2385 * @note Can be called by ISRs.
2386 *
2387 * @param stack Address of the stack.
2388 * @param data Value to push onto the stack.
2389 *
2390 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002391 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002392 */
Andrew Boiee8734462017-09-29 16:42:07 -07002393__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002394
2395/**
2396 * @brief Pop an element from a stack.
2397 *
2398 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2399 * manner and stores the value in @a data.
2400 *
2401 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2402 *
2403 * @param stack Address of the stack.
2404 * @param data Address of area to hold the value popped from the stack.
2405 * @param timeout Waiting period to obtain a value (in milliseconds),
2406 * or one of the special values K_NO_WAIT and K_FOREVER.
2407 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002408 * @retval 0 Element popped from stack.
2409 * @retval -EBUSY Returned without waiting.
2410 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002411 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002412 */
Andrew Boiee8734462017-09-29 16:42:07 -07002413__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002414
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002415/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002416 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002418 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002419 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002420 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002422 * @param name Name of the stack.
2423 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002424 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002425 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002426#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002427 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002428 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002429 struct k_stack name \
2430 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002431 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002432 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002433
Anas Nashif166f5192018-02-25 08:02:36 -06002434/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002435
Allan Stephens6bba9b02016-11-16 14:56:54 -05002436struct k_work;
2437
Allan Stephensc98da842016-11-11 15:45:03 -05002438/**
2439 * @defgroup workqueue_apis Workqueue Thread APIs
2440 * @ingroup kernel_apis
2441 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002442 */
2443
Allan Stephens6bba9b02016-11-16 14:56:54 -05002444/**
2445 * @typedef k_work_handler_t
2446 * @brief Work item handler function type.
2447 *
2448 * A work item's handler function is executed by a workqueue's thread
2449 * when the work item is processed by the workqueue.
2450 *
2451 * @param work Address of the work item.
2452 *
2453 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002454 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002455 */
2456typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002457
2458/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002459 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002460 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002461
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002462struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002463 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002464 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002465};
2466
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002467enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002468 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002469};
2470
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002471struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002472 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002473 k_work_handler_t handler;
2474 atomic_t flags[1];
2475};
2476
Allan Stephens6bba9b02016-11-16 14:56:54 -05002477struct k_delayed_work {
2478 struct k_work work;
2479 struct _timeout timeout;
2480 struct k_work_q *work_q;
2481};
2482
2483extern struct k_work_q k_sys_work_q;
2484
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002485/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002486 * INTERNAL_HIDDEN @endcond
2487 */
2488
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002489#define _K_WORK_INITIALIZER(work_handler) \
2490 { \
2491 ._reserved = NULL, \
2492 .handler = work_handler, \
2493 .flags = { 0 } \
2494 }
2495
2496#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2497
Allan Stephens6bba9b02016-11-16 14:56:54 -05002498/**
2499 * @brief Initialize a statically-defined work item.
2500 *
2501 * This macro can be used to initialize a statically-defined workqueue work
2502 * item, prior to its first use. For example,
2503 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002504 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002505 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002506 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002507 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002508 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002509 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002510#define K_WORK_DEFINE(work, work_handler) \
Andrew Boiec2e01df2018-11-12 15:16:54 -08002511 struct k_work work = _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002512
2513/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002514 * @brief Initialize a work item.
2515 *
2516 * This routine initializes a workqueue work item, prior to its first use.
2517 *
2518 * @param work Address of work item.
2519 * @param handler Function to invoke each time work item is processed.
2520 *
2521 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002522 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002523 */
2524static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2525{
Leandro Pereira0e23ad82018-05-29 10:42:17 -07002526 *work = (struct k_work)_K_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002527}
2528
2529/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002530 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002531 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002532 * This routine submits work item @a work to be processed by workqueue
2533 * @a work_q. If the work item is already pending in the workqueue's queue
2534 * as a result of an earlier submission, this routine has no effect on the
2535 * work item. If the work item has already been processed, or is currently
2536 * being processed, its work is considered complete and the work item can be
2537 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002538 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002539 * @warning
2540 * A submitted work item must not be modified until it has been processed
2541 * by the workqueue.
2542 *
2543 * @note Can be called by ISRs.
2544 *
2545 * @param work_q Address of workqueue.
2546 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002547 *
2548 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002549 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002550 */
2551static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2552 struct k_work *work)
2553{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002554 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002555 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556 }
2557}
2558
2559/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002560 * @brief Submit a work item to a user mode workqueue
2561 *
David B. Kinder06d78352018-12-17 14:32:40 -08002562 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002563 * memory allocation is made from the caller's resource pool which is freed
2564 * once the worker thread consumes the k_work item. The workqueue
2565 * thread must have memory access to the k_work item being submitted. The caller
2566 * must have permission granted on the work_q parameter's queue object.
2567 *
2568 * Otherwise this works the same as k_work_submit_to_queue().
2569 *
2570 * @note Can be called by ISRs.
2571 *
2572 * @param work_q Address of workqueue.
2573 * @param work Address of work item.
2574 *
2575 * @retval -EBUSY if the work item was already in some workqueue
2576 * @retval -ENOMEM if no memory for thread resource pool allocation
2577 * @retval 0 Success
2578 * @req K-WORK-001
2579 */
2580static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2581 struct k_work *work)
2582{
2583 int ret = -EBUSY;
2584
2585 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2586 ret = k_queue_alloc_append(&work_q->queue, work);
2587
2588 /* Couldn't insert into the queue. Clear the pending bit
2589 * so the work item can be submitted again
2590 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002591 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002592 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2593 }
2594 }
2595
2596 return ret;
2597}
2598
2599/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002600 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002601 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002602 * This routine indicates if work item @a work is pending in a workqueue's
2603 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002604 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002605 * @note Can be called by ISRs.
2606 *
2607 * @param work Address of work item.
2608 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002609 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002610 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002611 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002612static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002613{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002614 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002615}
2616
2617/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002618 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002619 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002620 * This routine starts workqueue @a work_q. The workqueue spawns its work
2621 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002622 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002623 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002624 * @param stack Pointer to work queue thread's stack space, as defined by
2625 * K_THREAD_STACK_DEFINE()
2626 * @param stack_size Size of the work queue thread's stack (in bytes), which
2627 * should either be the same constant passed to
2628 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002629 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002630 *
2631 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002632 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002633 */
Andrew Boie507852a2017-07-25 18:47:07 -07002634extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002635 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002636 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002637
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002638/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002639 * @brief Start a workqueue in user mode
2640 *
2641 * This works identically to k_work_q_start() except it is callable from user
2642 * mode, and the worker thread created will run in user mode.
2643 * The caller must have permissions granted on both the work_q parameter's
2644 * thread and queue objects, and the same restrictions on priority apply as
2645 * k_thread_create().
2646 *
2647 * @param work_q Address of workqueue.
2648 * @param stack Pointer to work queue thread's stack space, as defined by
2649 * K_THREAD_STACK_DEFINE()
2650 * @param stack_size Size of the work queue thread's stack (in bytes), which
2651 * should either be the same constant passed to
2652 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2653 * @param prio Priority of the work queue's thread.
2654 *
2655 * @return N/A
2656 * @req K-WORK-001
2657 */
2658extern void k_work_q_user_start(struct k_work_q *work_q,
2659 k_thread_stack_t *stack,
2660 size_t stack_size, int prio);
2661
2662/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002663 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002664 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002665 * This routine initializes a workqueue delayed work item, prior to
2666 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002667 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002668 * @param work Address of delayed work item.
2669 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002670 *
2671 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002672 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002673 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002674extern void k_delayed_work_init(struct k_delayed_work *work,
2675 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002676
2677/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002678 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002679 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002680 * This routine schedules work item @a work to be processed by workqueue
2681 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002682 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002683 * Only when the countdown completes is the work item actually submitted to
2684 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002685 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002686 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002687 * counting down cancels the existing submission and restarts the
2688 * countdown using the new delay. Note that this behavior is
2689 * inherently subject to race conditions with the pre-existing
2690 * timeouts and work queue, so care must be taken to synchronize such
2691 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002692 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002693 * @warning
2694 * A delayed work item must not be modified until it has been processed
2695 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002696 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002697 * @note Can be called by ISRs.
2698 *
2699 * @param work_q Address of workqueue.
2700 * @param work Address of delayed work item.
2701 * @param delay Delay before submitting the work item (in milliseconds).
2702 *
2703 * @retval 0 Work item countdown started.
2704 * @retval -EINPROGRESS Work item is already pending.
2705 * @retval -EINVAL Work item is being processed or has completed its work.
2706 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002707 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002708 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002709extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2710 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002711 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002712
2713/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002714 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002715 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002716 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002717 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002718 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002719 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002720 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002721 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002722 * @param work Address of delayed work item.
2723 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002724 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002725 * @retval -EINPROGRESS Work item is already pending.
2726 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002727 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002728 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002729extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002730
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002731/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002732 * @brief Submit a work item to the system workqueue.
2733 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002734 * This routine submits work item @a work to be processed by the system
2735 * workqueue. If the work item is already pending in the workqueue's queue
2736 * as a result of an earlier submission, this routine has no effect on the
2737 * work item. If the work item has already been processed, or is currently
2738 * being processed, its work is considered complete and the work item can be
2739 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002740 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002741 * @warning
2742 * Work items submitted to the system workqueue should avoid using handlers
2743 * that block or yield since this may prevent the system workqueue from
2744 * processing other work items in a timely manner.
2745 *
2746 * @note Can be called by ISRs.
2747 *
2748 * @param work Address of work item.
2749 *
2750 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002751 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002752 */
2753static inline void k_work_submit(struct k_work *work)
2754{
2755 k_work_submit_to_queue(&k_sys_work_q, work);
2756}
2757
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002758/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002759 * @brief Submit a delayed work item to the system workqueue.
2760 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002761 * This routine schedules work item @a work to be processed by the system
2762 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002763 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002764 * Only when the countdown completes is the work item actually submitted to
2765 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002766 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002767 * Submitting a previously submitted delayed work item that is still
2768 * counting down cancels the existing submission and restarts the countdown
2769 * using the new delay. If the work item is currently pending on the
2770 * workqueue's queue because the countdown has completed it is too late to
2771 * resubmit the item, and resubmission fails without impacting the work item.
2772 * If the work item has already been processed, or is currently being processed,
2773 * its work is considered complete and the work item can be resubmitted.
2774 *
2775 * @warning
2776 * Work items submitted to the system workqueue should avoid using handlers
2777 * that block or yield since this may prevent the system workqueue from
2778 * processing other work items in a timely manner.
2779 *
2780 * @note Can be called by ISRs.
2781 *
2782 * @param work Address of delayed work item.
2783 * @param delay Delay before submitting the work item (in milliseconds).
2784 *
2785 * @retval 0 Work item countdown started.
2786 * @retval -EINPROGRESS Work item is already pending.
2787 * @retval -EINVAL Work item is being processed or has completed its work.
2788 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002789 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002790 */
2791static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002792 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002793{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002794 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002795}
2796
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002797/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002798 * @brief Get time remaining before a delayed work gets scheduled.
2799 *
2800 * This routine computes the (approximate) time remaining before a
2801 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002802 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002803 *
2804 * @param work Delayed work item.
2805 *
2806 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002807 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002808 */
Kumar Galacc334c72017-04-21 10:55:34 -05002809static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002810{
Andy Ross52e444b2018-09-28 09:06:37 -07002811 return __ticks_to_ms(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02002812}
2813
Anas Nashif166f5192018-02-25 08:02:36 -06002814/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002815/**
Anas Nashifce78d162018-05-24 12:43:11 -05002816 * @defgroup mutex_apis Mutex APIs
2817 * @ingroup kernel_apis
2818 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002819 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002820
Anas Nashifce78d162018-05-24 12:43:11 -05002821/**
2822 * Mutex Structure
2823 * @ingroup mutex_apis
2824 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002825struct k_mutex {
2826 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002827 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002828 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002829 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002830 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002831
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002832 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002833};
2834
Anas Nashifce78d162018-05-24 12:43:11 -05002835/**
2836 * @cond INTERNAL_HIDDEN
2837 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002838#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002839 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002840 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002841 .owner = NULL, \
2842 .lock_count = 0, \
2843 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002844 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002845 }
2846
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002847#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2848
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002849/**
Allan Stephensc98da842016-11-11 15:45:03 -05002850 * INTERNAL_HIDDEN @endcond
2851 */
2852
2853/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002854 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002856 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002857 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002858 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002860 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002861 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002862 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002863#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002864 struct k_mutex name \
2865 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002866 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002867
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002868/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002869 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002871 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002872 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002873 * Upon completion, the mutex is available and does not have an owner.
2874 *
2875 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002876 *
2877 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002878 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002879 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002880__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002881
2882/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002883 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002885 * This routine locks @a mutex. If the mutex is locked by another thread,
2886 * the calling thread waits until the mutex becomes available or until
2887 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002888 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002889 * A thread is permitted to lock a mutex it has already locked. The operation
2890 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002892 * @param mutex Address of the mutex.
2893 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002894 * or one of the special values K_NO_WAIT and K_FOREVER.
2895 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002896 * @retval 0 Mutex locked.
2897 * @retval -EBUSY Returned without waiting.
2898 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002899 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002900 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002901__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002902
2903/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002904 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002905 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002906 * This routine unlocks @a mutex. The mutex must already be locked by the
2907 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002908 *
2909 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002910 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002911 * thread.
2912 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002913 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002914 *
2915 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002916 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002917 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002918__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002919
Allan Stephensc98da842016-11-11 15:45:03 -05002920/**
Anas Nashif166f5192018-02-25 08:02:36 -06002921 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002922 */
2923
2924/**
2925 * @cond INTERNAL_HIDDEN
2926 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002927
2928struct k_sem {
2929 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05302930 u32_t count;
2931 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002932 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002933
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002934 _OBJECT_TRACING_NEXT_PTR(k_sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002935};
2936
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002937#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002938 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002939 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002940 .count = initial_count, \
2941 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002942 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002943 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002944 }
2945
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002946#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2947
Allan Stephensc98da842016-11-11 15:45:03 -05002948/**
2949 * INTERNAL_HIDDEN @endcond
2950 */
2951
2952/**
2953 * @defgroup semaphore_apis Semaphore APIs
2954 * @ingroup kernel_apis
2955 * @{
2956 */
2957
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002958/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002959 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002960 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002961 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002963 * @param sem Address of the semaphore.
2964 * @param initial_count Initial semaphore count.
2965 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002966 *
2967 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002968 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002969 */
Andrew Boie99280232017-09-29 14:17:47 -07002970__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2971 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002972
2973/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002974 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002975 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002976 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002977 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002978 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2979 *
2980 * @param sem Address of the semaphore.
2981 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002982 * or one of the special values K_NO_WAIT and K_FOREVER.
2983 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002984 * @note When porting code from the nanokernel legacy API to the new API, be
2985 * careful with the return value of this function. The return value is the
2986 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2987 * non-zero means failure, while the nano_sem_take family returns 1 for success
2988 * and 0 for failure.
2989 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002990 * @retval 0 Semaphore taken.
2991 * @retval -EBUSY Returned without waiting.
2992 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002993 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002994 */
Andrew Boie99280232017-09-29 14:17:47 -07002995__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002996
2997/**
2998 * @brief Give a semaphore.
2999 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003000 * This routine gives @a sem, unless the semaphore is already at its maximum
3001 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003002 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003003 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003005 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003006 *
3007 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003008 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003009 */
Andrew Boie99280232017-09-29 14:17:47 -07003010__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003011
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003012/**
3013 * @brief Reset a semaphore's count to zero.
3014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003015 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003016 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003017 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003018 *
3019 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003020 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003021 */
Andrew Boie990bf162017-10-03 12:36:49 -07003022__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003023
Anas Nashif954d5502018-02-25 08:37:28 -06003024/**
3025 * @internal
3026 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003027static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003028{
3029 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003030}
3031
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003032/**
3033 * @brief Get a semaphore's count.
3034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003035 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003037 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003038 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003039 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003040 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003041 */
Andrew Boie990bf162017-10-03 12:36:49 -07003042__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003043
Anas Nashif954d5502018-02-25 08:37:28 -06003044/**
3045 * @internal
3046 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003047static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003048{
3049 return sem->count;
3050}
3051
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003052/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003053 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003055 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003056 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003057 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003058 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003059 * @param name Name of the semaphore.
3060 * @param initial_count Initial semaphore count.
3061 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003062 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003063 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003064#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003065 struct k_sem name \
3066 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003067 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303068 BUILD_ASSERT(((count_limit) != 0) && \
3069 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003070
Anas Nashif166f5192018-02-25 08:02:36 -06003071/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003072
3073/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003074 * @defgroup msgq_apis Message Queue APIs
3075 * @ingroup kernel_apis
3076 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003077 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003078
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003079/**
3080 * @brief Message Queue Structure
3081 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003082struct k_msgq {
3083 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003084 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003085 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003086 char *buffer_start;
3087 char *buffer_end;
3088 char *read_ptr;
3089 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003090 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003091
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003092 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Andrew Boie0fe789f2018-04-12 18:35:56 -07003093 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003094};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003095/**
3096 * @cond INTERNAL_HIDDEN
3097 */
3098
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003099
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003100#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003101 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003102 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003103 .max_msgs = q_max_msgs, \
3104 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003105 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003106 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003107 .read_ptr = q_buffer, \
3108 .write_ptr = q_buffer, \
3109 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003110 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003111 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003112#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003113/**
3114 * INTERNAL_HIDDEN @endcond
3115 */
3116
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003117
Andrew Boie0fe789f2018-04-12 18:35:56 -07003118#define K_MSGQ_FLAG_ALLOC BIT(0)
3119
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003120/**
3121 * @brief Message Queue Attributes
3122 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303123struct k_msgq_attrs {
3124 size_t msg_size;
3125 u32_t max_msgs;
3126 u32_t used_msgs;
3127};
3128
Allan Stephensc98da842016-11-11 15:45:03 -05003129
3130/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003131 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003133 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3134 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003135 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3136 * message is similarly aligned to this boundary, @a q_msg_size must also be
3137 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003139 * The message queue can be accessed outside the module where it is defined
3140 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003141 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003142 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003144 * @param q_name Name of the message queue.
3145 * @param q_msg_size Message size (in bytes).
3146 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003147 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003148 *
3149 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003150 */
3151#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003152 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003153 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003154 struct k_msgq q_name \
3155 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003156 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003157 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003158
Peter Mitsisd7a37502016-10-13 11:37:40 -04003159/**
3160 * @brief Initialize a message queue.
3161 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003162 * This routine initializes a message queue object, prior to its first use.
3163 *
Allan Stephensda827222016-11-09 14:23:58 -06003164 * The message queue's ring buffer must contain space for @a max_msgs messages,
3165 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3166 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3167 * that each message is similarly aligned to this boundary, @a q_msg_size
3168 * must also be a multiple of N.
3169 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003170 * @param q Address of the message queue.
3171 * @param buffer Pointer to ring buffer that holds queued messages.
3172 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003173 * @param max_msgs Maximum number of messages that can be queued.
3174 *
3175 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003176 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003177 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003178void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3179 u32_t max_msgs);
3180
3181/**
3182 * @brief Initialize a message queue.
3183 *
3184 * This routine initializes a message queue object, prior to its first use,
3185 * allocating its internal ring buffer from the calling thread's resource
3186 * pool.
3187 *
3188 * Memory allocated for the ring buffer can be released by calling
3189 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3190 * all of its references.
3191 *
3192 * @param q Address of the message queue.
3193 * @param msg_size Message size (in bytes).
3194 * @param max_msgs Maximum number of messages that can be queued.
3195 *
3196 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3197 * thread's resource pool, or -EINVAL if the size parameters cause
3198 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003199 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003200 */
3201__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3202 u32_t max_msgs);
3203
3204
3205void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003206
3207/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003208 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003210 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003211 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003212 * @note Can be called by ISRs.
3213 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003214 * @param q Address of the message queue.
3215 * @param data Pointer to the message.
3216 * @param timeout Waiting period to add the message (in milliseconds),
3217 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003218 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003219 * @retval 0 Message sent.
3220 * @retval -ENOMSG Returned without waiting or queue purged.
3221 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003222 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003223 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003224__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003225
3226/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003227 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003229 * This routine receives a message from message queue @a q in a "first in,
3230 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003231 *
Allan Stephensc98da842016-11-11 15:45:03 -05003232 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003233 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003234 * @param q Address of the message queue.
3235 * @param data Address of area to hold the received message.
3236 * @param timeout Waiting period to receive the message (in milliseconds),
3237 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003238 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003239 * @retval 0 Message received.
3240 * @retval -ENOMSG Returned without waiting.
3241 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003242 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003243 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003244__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003245
3246/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003247 * @brief Peek/read a message from a message queue.
3248 *
3249 * This routine reads a message from message queue @a q in a "first in,
3250 * first out" manner and leaves the message in the queue.
3251 *
3252 * @note Can be called by ISRs.
3253 *
3254 * @param q Address of the message queue.
3255 * @param data Address of area to hold the message read from the queue.
3256 *
3257 * @retval 0 Message read.
3258 * @retval -ENOMSG Returned when the queue has no message.
3259 * @req K-MSGQ-002
3260 */
3261__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3262
3263/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003264 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003265 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003266 * This routine discards all unreceived messages in a message queue's ring
3267 * buffer. Any threads that are blocked waiting to send a message to the
3268 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003269 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003270 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003271 *
3272 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003273 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003274 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003275__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003276
Peter Mitsis67be2492016-10-07 11:44:34 -04003277/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003278 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003279 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003280 * This routine returns the number of unused entries in a message queue's
3281 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003282 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003283 * @param q Address of the message queue.
3284 *
3285 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003286 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003287 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003288__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3289
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303290/**
3291 * @brief Get basic attributes of a message queue.
3292 *
3293 * This routine fetches basic attributes of message queue into attr argument.
3294 *
3295 * @param q Address of the message queue.
3296 * @param attrs pointer to message queue attribute structure.
3297 *
3298 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003299 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303300 */
3301__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3302
3303
Andrew Boie82edb6e2017-10-02 10:53:06 -07003304static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003305{
3306 return q->max_msgs - q->used_msgs;
3307}
3308
Peter Mitsisd7a37502016-10-13 11:37:40 -04003309/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003310 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003311 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003312 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003313 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003314 * @param q Address of the message queue.
3315 *
3316 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003317 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003318 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003319__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3320
3321static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003322{
3323 return q->used_msgs;
3324}
3325
Anas Nashif166f5192018-02-25 08:02:36 -06003326/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003327
3328/**
3329 * @defgroup mem_pool_apis Memory Pool APIs
3330 * @ingroup kernel_apis
3331 * @{
3332 */
3333
Andy Ross73cb9582017-05-09 10:42:39 -07003334/* Note on sizing: the use of a 20 bit field for block means that,
3335 * assuming a reasonable minimum block size of 16 bytes, we're limited
3336 * to 16M of memory managed by a single pool. Long term it would be
3337 * good to move to a variable bit size based on configuration.
3338 */
3339struct k_mem_block_id {
3340 u32_t pool : 8;
3341 u32_t level : 4;
3342 u32_t block : 20;
3343};
3344
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003345struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003346 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003347 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003348};
3349
Anas Nashif166f5192018-02-25 08:02:36 -06003350/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003351
3352/**
3353 * @defgroup mailbox_apis Mailbox APIs
3354 * @ingroup kernel_apis
3355 * @{
3356 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003357
3358struct k_mbox_msg {
3359 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003360 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003361 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003362 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003363 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003364 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003365 /** sender's message data buffer */
3366 void *tx_data;
3367 /** internal use only - needed for legacy API support */
3368 void *_rx_data;
3369 /** message data block descriptor */
3370 struct k_mem_block tx_block;
3371 /** source thread id */
3372 k_tid_t rx_source_thread;
3373 /** target thread id */
3374 k_tid_t tx_target_thread;
3375 /** internal use only - thread waiting on send (may be a dummy) */
3376 k_tid_t _syncing_thread;
3377#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3378 /** internal use only - semaphore used during asynchronous send */
3379 struct k_sem *_async_sem;
3380#endif
3381};
3382
3383struct k_mbox {
3384 _wait_q_t tx_msg_queue;
3385 _wait_q_t rx_msg_queue;
3386
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003387 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003388};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003389/**
3390 * @cond INTERNAL_HIDDEN
3391 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003392
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003393#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003394 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003395 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3396 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003397 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003398 }
3399
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003400#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3401
Peter Mitsis12092702016-10-14 12:57:23 -04003402/**
Allan Stephensc98da842016-11-11 15:45:03 -05003403 * INTERNAL_HIDDEN @endcond
3404 */
3405
3406/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003407 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003409 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003410 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003411 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003412 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003413 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003414 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003415 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003416#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003417 struct k_mbox name \
3418 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003419 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003420
Peter Mitsis12092702016-10-14 12:57:23 -04003421/**
3422 * @brief Initialize a mailbox.
3423 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003424 * This routine initializes a mailbox object, prior to its first use.
3425 *
3426 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003427 *
3428 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003429 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003430 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003431extern void k_mbox_init(struct k_mbox *mbox);
3432
Peter Mitsis12092702016-10-14 12:57:23 -04003433/**
3434 * @brief Send a mailbox message in a synchronous manner.
3435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003436 * This routine sends a message to @a mbox and waits for a receiver to both
3437 * receive and process it. The message data may be in a buffer, in a memory
3438 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003439 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003440 * @param mbox Address of the mailbox.
3441 * @param tx_msg Address of the transmit message descriptor.
3442 * @param timeout Waiting period for the message to be received (in
3443 * milliseconds), or one of the special values K_NO_WAIT
3444 * and K_FOREVER. Once the message has been received,
3445 * this routine waits as long as necessary for the message
3446 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003447 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003448 * @retval 0 Message sent.
3449 * @retval -ENOMSG Returned without waiting.
3450 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003451 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003452 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003453extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003454 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003455
Peter Mitsis12092702016-10-14 12:57:23 -04003456/**
3457 * @brief Send a mailbox message in an asynchronous manner.
3458 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003459 * This routine sends a message to @a mbox without waiting for a receiver
3460 * to process it. The message data may be in a buffer, in a memory pool block,
3461 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3462 * will be given when the message has been both received and completely
3463 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003464 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003465 * @param mbox Address of the mailbox.
3466 * @param tx_msg Address of the transmit message descriptor.
3467 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003468 *
3469 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003470 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003471 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003472extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003473 struct k_sem *sem);
3474
Peter Mitsis12092702016-10-14 12:57:23 -04003475/**
3476 * @brief Receive a mailbox message.
3477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003478 * This routine receives a message from @a mbox, then optionally retrieves
3479 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003480 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003481 * @param mbox Address of the mailbox.
3482 * @param rx_msg Address of the receive message descriptor.
3483 * @param buffer Address of the buffer to receive data, or NULL to defer data
3484 * retrieval and message disposal until later.
3485 * @param timeout Waiting period for a message to be received (in
3486 * milliseconds), or one of the special values K_NO_WAIT
3487 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003488 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003489 * @retval 0 Message received.
3490 * @retval -ENOMSG Returned without waiting.
3491 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003492 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003493 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003494extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003495 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003496
3497/**
3498 * @brief Retrieve mailbox message data into a buffer.
3499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003500 * This routine completes the processing of a received message by retrieving
3501 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003502 *
3503 * Alternatively, this routine can be used to dispose of a received message
3504 * without retrieving its data.
3505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003506 * @param rx_msg Address of the receive message descriptor.
3507 * @param buffer Address of the buffer to receive data, or NULL to discard
3508 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003509 *
3510 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003511 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003512 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003513extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003514
3515/**
3516 * @brief Retrieve mailbox message data into a memory pool block.
3517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003518 * This routine completes the processing of a received message by retrieving
3519 * its data into a memory pool block, then disposing of the message.
3520 * The memory pool block that results from successful retrieval must be
3521 * returned to the pool once the data has been processed, even in cases
3522 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003523 *
3524 * Alternatively, this routine can be used to dispose of a received message
3525 * without retrieving its data. In this case there is no need to return a
3526 * memory pool block to the pool.
3527 *
3528 * This routine allocates a new memory pool block for the data only if the
3529 * data is not already in one. If a new block cannot be allocated, the routine
3530 * returns a failure code and the received message is left unchanged. This
3531 * permits the caller to reattempt data retrieval at a later time or to dispose
3532 * of the received message without retrieving its data.
3533 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003534 * @param rx_msg Address of a receive message descriptor.
3535 * @param pool Address of memory pool, or NULL to discard data.
3536 * @param block Address of the area to hold memory pool block info.
3537 * @param timeout Waiting period to wait for a memory pool block (in
3538 * milliseconds), or one of the special values K_NO_WAIT
3539 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003540 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003541 * @retval 0 Data retrieved.
3542 * @retval -ENOMEM Returned without waiting.
3543 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003544 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003545 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003546extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003547 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003548 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003549
Anas Nashif166f5192018-02-25 08:02:36 -06003550/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003551
3552/**
Anas Nashifce78d162018-05-24 12:43:11 -05003553 * @defgroup pipe_apis Pipe APIs
3554 * @ingroup kernel_apis
3555 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003556 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003557
Anas Nashifce78d162018-05-24 12:43:11 -05003558/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003559struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003560 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3561 size_t size; /**< Buffer size */
3562 size_t bytes_used; /**< # bytes used in buffer */
3563 size_t read_index; /**< Where in buffer to read from */
3564 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003565
3566 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003567 _wait_q_t readers; /**< Reader wait queue */
3568 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003569 } wait_q;
3570
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003571 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Anas Nashifce78d162018-05-24 12:43:11 -05003572 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003573};
3574
Anas Nashifce78d162018-05-24 12:43:11 -05003575/**
3576 * @cond INTERNAL_HIDDEN
3577 */
3578#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3579
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003580#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003581 { \
3582 .buffer = pipe_buffer, \
3583 .size = pipe_buffer_size, \
3584 .bytes_used = 0, \
3585 .read_index = 0, \
3586 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003587 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3588 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003589 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003590 }
3591
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003592#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3593
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003594/**
Allan Stephensc98da842016-11-11 15:45:03 -05003595 * INTERNAL_HIDDEN @endcond
3596 */
3597
3598/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003599 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003600 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003601 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003602 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003603 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003605 * @param name Name of the pipe.
3606 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3607 * or zero if no ring buffer is used.
3608 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003609 *
3610 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003611 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003612#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3613 static unsigned char __kernel_noinit __aligned(pipe_align) \
3614 _k_pipe_buf_##name[pipe_buffer_size]; \
3615 struct k_pipe name \
3616 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003617 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003618
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003619/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003620 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003622 * This routine initializes a pipe object, prior to its first use.
3623 *
3624 * @param pipe Address of the pipe.
3625 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3626 * is used.
3627 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3628 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003629 *
3630 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003631 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003632 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003633void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3634
3635/**
3636 * @brief Release a pipe's allocated buffer
3637 *
3638 * If a pipe object was given a dynamically allocated buffer via
3639 * k_pipe_alloc_init(), this will free it. This function does nothing
3640 * if the buffer wasn't dynamically allocated.
3641 *
3642 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003643 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003644 */
3645void k_pipe_cleanup(struct k_pipe *pipe);
3646
3647/**
3648 * @brief Initialize a pipe and allocate a buffer for it
3649 *
3650 * Storage for the buffer region will be allocated from the calling thread's
3651 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3652 * or userspace is enabled and the pipe object loses all references to it.
3653 *
3654 * This function should only be called on uninitialized pipe objects.
3655 *
3656 * @param pipe Address of the pipe.
3657 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3658 * buffer is used.
3659 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003660 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003661 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003662 */
3663__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003664
3665/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003666 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003667 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003668 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003670 * @param pipe Address of the pipe.
3671 * @param data Address of data to write.
3672 * @param bytes_to_write Size of data (in bytes).
3673 * @param bytes_written Address of area to hold the number of bytes written.
3674 * @param min_xfer Minimum number of bytes to write.
3675 * @param timeout Waiting period to wait for the data to be written (in
3676 * milliseconds), or one of the special values K_NO_WAIT
3677 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003678 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003679 * @retval 0 At least @a min_xfer bytes of data were written.
3680 * @retval -EIO Returned without waiting; zero data bytes were written.
3681 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003682 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003683 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003684 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003685__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3686 size_t bytes_to_write, size_t *bytes_written,
3687 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003688
3689/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003690 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003691 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003692 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003694 * @param pipe Address of the pipe.
3695 * @param data Address to place the data read from pipe.
3696 * @param bytes_to_read Maximum number of data bytes to read.
3697 * @param bytes_read Address of area to hold the number of bytes read.
3698 * @param min_xfer Minimum number of data bytes to read.
3699 * @param timeout Waiting period to wait for the data to be read (in
3700 * milliseconds), or one of the special values K_NO_WAIT
3701 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003702 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003703 * @retval 0 At least @a min_xfer bytes of data were read.
3704 * @retval -EIO Returned without waiting; zero data bytes were read.
3705 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003706 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003707 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003708 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003709__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3710 size_t bytes_to_read, size_t *bytes_read,
3711 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003712
3713/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003714 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003716 * This routine writes the data contained in a memory block to @a pipe.
3717 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003718 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003719 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003720 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003721 * @param block Memory block containing data to send
3722 * @param size Number of data bytes in memory block to send
3723 * @param sem Semaphore to signal upon completion (else NULL)
3724 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003725 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003726 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003727 */
3728extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3729 size_t size, struct k_sem *sem);
3730
Anas Nashif166f5192018-02-25 08:02:36 -06003731/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003732
Allan Stephensc98da842016-11-11 15:45:03 -05003733/**
3734 * @cond INTERNAL_HIDDEN
3735 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003736
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003737struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003738 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003739 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003740 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003741 char *buffer;
3742 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003743 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003744
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003745 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003746};
3747
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003748#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003749 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003750 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003751 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003752 .num_blocks = slab_num_blocks, \
3753 .block_size = slab_block_size, \
3754 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003755 .free_list = NULL, \
3756 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003757 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003758 }
3759
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003760#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3761
3762
Peter Mitsis578f9112016-10-07 13:50:31 -04003763/**
Allan Stephensc98da842016-11-11 15:45:03 -05003764 * INTERNAL_HIDDEN @endcond
3765 */
3766
3767/**
3768 * @defgroup mem_slab_apis Memory Slab APIs
3769 * @ingroup kernel_apis
3770 * @{
3771 */
3772
3773/**
Allan Stephensda827222016-11-09 14:23:58 -06003774 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003775 *
Allan Stephensda827222016-11-09 14:23:58 -06003776 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003777 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003778 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3779 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003780 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003781 *
Allan Stephensda827222016-11-09 14:23:58 -06003782 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003783 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003784 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003785 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003786 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003787 * @param name Name of the memory slab.
3788 * @param slab_block_size Size of each memory block (in bytes).
3789 * @param slab_num_blocks Number memory blocks.
3790 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003791 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003792 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003793#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3794 char __noinit __aligned(slab_align) \
3795 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3796 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003797 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003798 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003799 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003800
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003801/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003802 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003804 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003805 *
Allan Stephensda827222016-11-09 14:23:58 -06003806 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3807 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3808 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3809 * To ensure that each memory block is similarly aligned to this boundary,
3810 * @a slab_block_size must also be a multiple of N.
3811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003812 * @param slab Address of the memory slab.
3813 * @param buffer Pointer to buffer used for the memory blocks.
3814 * @param block_size Size of each memory block (in bytes).
3815 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003816 *
3817 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003818 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003819 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003820extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003821 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003822
3823/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003824 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003825 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003826 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003828 * @param slab Address of the memory slab.
3829 * @param mem Pointer to block address area.
3830 * @param timeout Maximum time to wait for operation to complete
3831 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3832 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003833 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003834 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003835 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003836 * @retval -ENOMEM Returned without waiting.
3837 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003838 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003839 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003840extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003841 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003842
3843/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003844 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003846 * This routine releases a previously allocated memory block back to its
3847 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003849 * @param slab Address of the memory slab.
3850 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003851 *
3852 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003853 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003854 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003855extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003856
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003857/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003858 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003860 * This routine gets the number of memory blocks that are currently
3861 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003862 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003863 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003865 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003866 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003867 */
Kumar Galacc334c72017-04-21 10:55:34 -05003868static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003869{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003870 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003871}
3872
Peter Mitsisc001aa82016-10-13 13:53:37 -04003873/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003874 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003876 * This routine gets the number of memory blocks that are currently
3877 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003878 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003879 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003881 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003882 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04003883 */
Kumar Galacc334c72017-04-21 10:55:34 -05003884static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003885{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003886 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003887}
3888
Anas Nashif166f5192018-02-25 08:02:36 -06003889/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003890
3891/**
3892 * @cond INTERNAL_HIDDEN
3893 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003894
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003895struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003896 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003897 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003898};
3899
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003900/**
Allan Stephensc98da842016-11-11 15:45:03 -05003901 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003902 */
3903
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003904/**
Allan Stephensc98da842016-11-11 15:45:03 -05003905 * @addtogroup mem_pool_apis
3906 * @{
3907 */
3908
3909/**
3910 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003911 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003912 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3913 * long. The memory pool allows blocks to be repeatedly partitioned into
3914 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003915 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003916 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003917 * If the pool is to be accessed outside the module where it is defined, it
3918 * can be declared via
3919 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003920 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003921 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003922 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003923 * @param minsz Size of the smallest blocks in the pool (in bytes).
3924 * @param maxsz Size of the largest blocks in the pool (in bytes).
3925 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003926 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003927 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003928 */
Andy Ross73cb9582017-05-09 10:42:39 -07003929#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3930 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3931 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003932 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003933 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003934 .base = { \
3935 .buf = _mpool_buf_##name, \
3936 .max_sz = maxsz, \
3937 .n_max = nmax, \
3938 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3939 .levels = _mpool_lvls_##name, \
3940 .flags = SYS_MEM_POOL_KERNEL \
3941 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003942 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003943
Peter Mitsis937042c2016-10-13 13:18:26 -04003944/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003945 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003946 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003947 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003948 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003949 * @param pool Address of the memory pool.
3950 * @param block Pointer to block descriptor for the allocated memory.
3951 * @param size Amount of memory to allocate (in bytes).
3952 * @param timeout Maximum time to wait for operation to complete
3953 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3954 * or K_FOREVER to wait as long as necessary.
3955 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003956 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003957 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003958 * @retval -ENOMEM Returned without waiting.
3959 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003960 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04003961 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003962extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003963 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003964
3965/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07003966 * @brief Allocate memory from a memory pool with malloc() semantics
3967 *
3968 * Such memory must be released using k_free().
3969 *
3970 * @param pool Address of the memory pool.
3971 * @param size Amount of memory to allocate (in bytes).
3972 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003973 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07003974 */
3975extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
3976
3977/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003978 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003979 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003980 * This routine releases a previously allocated memory block back to its
3981 * memory pool.
3982 *
3983 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003984 *
3985 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003986 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04003987 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003988extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003989
3990/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003991 * @brief Free memory allocated from a memory pool.
3992 *
3993 * This routine releases a previously allocated memory block back to its
3994 * memory pool
3995 *
3996 * @param id Memory block identifier.
3997 *
3998 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003999 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004000 */
4001extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4002
4003/**
Anas Nashif166f5192018-02-25 08:02:36 -06004004 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004005 */
4006
4007/**
4008 * @defgroup heap_apis Heap Memory Pool APIs
4009 * @ingroup kernel_apis
4010 * @{
4011 */
4012
4013/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004014 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004016 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004017 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004019 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004021 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004022 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004023 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004024extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004025
4026/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004027 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004028 *
4029 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004030 * returned must have been allocated from the heap memory pool or
4031 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004032 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004033 * If @a ptr is NULL, no operation is performed.
4034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004035 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004036 *
4037 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004038 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004039 */
4040extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004041
Allan Stephensc98da842016-11-11 15:45:03 -05004042/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004043 * @brief Allocate memory from heap, array style
4044 *
4045 * This routine provides traditional calloc() semantics. Memory is
4046 * allocated from the heap memory pool and zeroed.
4047 *
4048 * @param nmemb Number of elements in the requested array
4049 * @param size Size of each array element (in bytes).
4050 *
4051 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004052 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004053 */
4054extern void *k_calloc(size_t nmemb, size_t size);
4055
Anas Nashif166f5192018-02-25 08:02:36 -06004056/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004057
Benjamin Walshacc68c12017-01-29 18:57:45 -05004058/* polling API - PRIVATE */
4059
Benjamin Walshb0179862017-02-02 16:39:57 -05004060#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004061#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004062#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004063#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004064#endif
4065
Benjamin Walshacc68c12017-01-29 18:57:45 -05004066/* private - implementation data created as needed, per-type */
4067struct _poller {
4068 struct k_thread *thread;
Flavio Ceolin76b35182018-12-16 12:48:29 -08004069 volatile bool is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004070};
4071
4072/* private - types bit positions */
4073enum _poll_types_bits {
4074 /* can be used to ignore an event */
4075 _POLL_TYPE_IGNORE,
4076
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004077 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004078 _POLL_TYPE_SIGNAL,
4079
4080 /* semaphore availability */
4081 _POLL_TYPE_SEM_AVAILABLE,
4082
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004083 /* queue/fifo/lifo data availability */
4084 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004085
4086 _POLL_NUM_TYPES
4087};
4088
4089#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4090
4091/* private - states bit positions */
4092enum _poll_states_bits {
4093 /* default state when creating event */
4094 _POLL_STATE_NOT_READY,
4095
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004096 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004097 _POLL_STATE_SIGNALED,
4098
4099 /* semaphore is available */
4100 _POLL_STATE_SEM_AVAILABLE,
4101
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004102 /* data is available to read on queue/fifo/lifo */
4103 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004104
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004105 /* queue/fifo/lifo wait was cancelled */
4106 _POLL_STATE_CANCELLED,
4107
Benjamin Walshacc68c12017-01-29 18:57:45 -05004108 _POLL_NUM_STATES
4109};
4110
4111#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4112
4113#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004114 (32 - (0 \
4115 + 8 /* tag */ \
4116 + _POLL_NUM_TYPES \
4117 + _POLL_NUM_STATES \
4118 + 1 /* modes */ \
4119 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004120
Benjamin Walshacc68c12017-01-29 18:57:45 -05004121/* end of polling API - PRIVATE */
4122
4123
4124/**
4125 * @defgroup poll_apis Async polling APIs
4126 * @ingroup kernel_apis
4127 * @{
4128 */
4129
4130/* Public polling API */
4131
4132/* public - values for k_poll_event.type bitfield */
4133#define K_POLL_TYPE_IGNORE 0
4134#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4135#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004136#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4137#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004138
4139/* public - polling modes */
4140enum k_poll_modes {
4141 /* polling thread does not take ownership of objects when available */
4142 K_POLL_MODE_NOTIFY_ONLY = 0,
4143
4144 K_POLL_NUM_MODES
4145};
4146
4147/* public - values for k_poll_event.state bitfield */
4148#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004149#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4150#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004151#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4152#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004153#define K_POLL_STATE_CANCELLED _POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004154
4155/* public - poll signal object */
4156struct k_poll_signal {
4157 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004158 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004159
4160 /*
4161 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4162 * user resets it to 0.
4163 */
4164 unsigned int signaled;
4165
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004166 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004167 int result;
4168};
4169
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004170#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004171 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004172 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004173 .signaled = 0, \
4174 .result = 0, \
4175 }
4176
4177struct k_poll_event {
4178 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004179 sys_dnode_t _node;
4180
4181 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004182 struct _poller *poller;
4183
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004184 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004185 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004186
Benjamin Walshacc68c12017-01-29 18:57:45 -05004187 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004188 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004189
4190 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004191 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004192
4193 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004194 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004195
4196 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004197 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004198
4199 /* per-type data */
4200 union {
4201 void *obj;
4202 struct k_poll_signal *signal;
4203 struct k_sem *sem;
4204 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004205 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004206 };
4207};
4208
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004209#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004210 { \
4211 .poller = NULL, \
4212 .type = event_type, \
4213 .state = K_POLL_STATE_NOT_READY, \
4214 .mode = event_mode, \
4215 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004216 { .obj = event_obj }, \
4217 }
4218
4219#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4220 event_tag) \
4221 { \
4222 .type = event_type, \
4223 .tag = event_tag, \
4224 .state = K_POLL_STATE_NOT_READY, \
4225 .mode = event_mode, \
4226 .unused = 0, \
4227 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004228 }
4229
4230/**
4231 * @brief Initialize one struct k_poll_event instance
4232 *
4233 * After this routine is called on a poll event, the event it ready to be
4234 * placed in an event array to be passed to k_poll().
4235 *
4236 * @param event The event to initialize.
4237 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4238 * values. Only values that apply to the same object being polled
4239 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4240 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004241 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004242 * @param obj Kernel object or poll signal.
4243 *
4244 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004245 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004246 */
4247
Kumar Galacc334c72017-04-21 10:55:34 -05004248extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004249 int mode, void *obj);
4250
4251/**
4252 * @brief Wait for one or many of multiple poll events to occur
4253 *
4254 * This routine allows a thread to wait concurrently for one or many of
4255 * multiple poll events to have occurred. Such events can be a kernel object
4256 * being available, like a semaphore, or a poll signal event.
4257 *
4258 * When an event notifies that a kernel object is available, the kernel object
4259 * is not "given" to the thread calling k_poll(): it merely signals the fact
4260 * that the object was available when the k_poll() call was in effect. Also,
4261 * all threads trying to acquire an object the regular way, i.e. by pending on
4262 * the object, have precedence over the thread polling on the object. This
4263 * means that the polling thread will never get the poll event on an object
4264 * until the object becomes available and its pend queue is empty. For this
4265 * reason, the k_poll() call is more effective when the objects being polled
4266 * only have one thread, the polling thread, trying to acquire them.
4267 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004268 * When k_poll() returns 0, the caller should loop on all the events that were
4269 * passed to k_poll() and check the state field for the values that were
4270 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004271 *
4272 * Before being reused for another call to k_poll(), the user has to reset the
4273 * state field to K_POLL_STATE_NOT_READY.
4274 *
Andrew Boie3772f772018-05-07 16:52:57 -07004275 * When called from user mode, a temporary memory allocation is required from
4276 * the caller's resource pool.
4277 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004278 * @param events An array of pointers to events to be polled for.
4279 * @param num_events The number of events in the array.
4280 * @param timeout Waiting period for an event to be ready (in milliseconds),
4281 * or one of the special values K_NO_WAIT and K_FOREVER.
4282 *
4283 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004284 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004285 * @retval -EINTR Polling has been interrupted, e.g. with
4286 * k_queue_cancel_wait(). All output events are still set and valid,
4287 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4288 * words, -EINTR status means that at least one of output events is
4289 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004290 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4291 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004292 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004293 */
4294
Andrew Boie3772f772018-05-07 16:52:57 -07004295__syscall int k_poll(struct k_poll_event *events, int num_events,
4296 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004297
4298/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004299 * @brief Initialize a poll signal object.
4300 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004301 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004302 *
4303 * @param signal A poll signal.
4304 *
4305 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004306 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004307 */
4308
Andrew Boie3772f772018-05-07 16:52:57 -07004309__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4310
4311/*
4312 * @brief Reset a poll signal object's state to unsignaled.
4313 *
4314 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004315 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004316 */
4317__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4318
4319static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4320{
4321 signal->signaled = 0;
4322}
4323
4324/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004325 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004326 *
4327 * @param signal A poll signal object
4328 * @param signaled An integer buffer which will be written nonzero if the
4329 * object was signaled
4330 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004331 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004332 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004333 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004334 */
4335__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4336 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004337
4338/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004339 * @brief Signal a poll signal object.
4340 *
4341 * This routine makes ready a poll signal, which is basically a poll event of
4342 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4343 * made ready to run. A @a result value can be specified.
4344 *
4345 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004346 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004347 * k_poll_signal_reset(). It thus has to be reset by the user before being
4348 * passed again to k_poll() or k_poll() will consider it being signaled, and
4349 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004350 *
4351 * @param signal A poll signal.
4352 * @param result The value to store in the result field of the signal.
4353 *
4354 * @retval 0 The signal was delivered successfully.
4355 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004356 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004357 */
4358
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004359__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004360
Anas Nashif954d5502018-02-25 08:37:28 -06004361/**
4362 * @internal
4363 */
Andy Ross8606fab2018-03-26 10:54:40 -07004364extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004365
Anas Nashif166f5192018-02-25 08:02:36 -06004366/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004367
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004368/**
4369 * @brief Make the CPU idle.
4370 *
4371 * This function makes the CPU idle until an event wakes it up.
4372 *
4373 * In a regular system, the idle thread should be the only thread responsible
4374 * for making the CPU idle and triggering any type of power management.
4375 * However, in some more constrained systems, such as a single-threaded system,
4376 * the only thread would be responsible for this if needed.
4377 *
4378 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004379 * @req K-MISC-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004380 */
4381extern void k_cpu_idle(void);
4382
4383/**
4384 * @brief Make the CPU idle in an atomic fashion.
4385 *
4386 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4387 * must be done atomically before making the CPU idle.
4388 *
4389 * @param key Interrupt locking key obtained from irq_lock().
4390 *
4391 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004392 * @req K-MISC-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004393 */
4394extern void k_cpu_atomic_idle(unsigned int key);
4395
Anas Nashif954d5502018-02-25 08:37:28 -06004396
4397/**
4398 * @internal
4399 */
Kumar Galacc334c72017-04-21 10:55:34 -05004400extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004401
Andrew Boiecdb94d62017-04-18 15:22:05 -07004402#ifdef _ARCH_EXCEPT
4403/* This archtecture has direct support for triggering a CPU exception */
4404#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4405#else
4406
Andrew Boiecdb94d62017-04-18 15:22:05 -07004407/* NOTE: This is the implementation for arches that do not implement
4408 * _ARCH_EXCEPT() to generate a real CPU exception.
4409 *
4410 * We won't have a real exception frame to determine the PC value when
4411 * the oops occurred, so print file and line number before we jump into
4412 * the fatal error handler.
4413 */
4414#define _k_except_reason(reason) do { \
4415 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4416 _NanoFatalErrorHandler(reason, &_default_esf); \
4417 CODE_UNREACHABLE; \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004418 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004419
4420#endif /* _ARCH__EXCEPT */
4421
4422/**
4423 * @brief Fatally terminate a thread
4424 *
4425 * This should be called when a thread has encountered an unrecoverable
4426 * runtime condition and needs to terminate. What this ultimately
4427 * means is determined by the _fatal_error_handler() implementation, which
4428 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4429 *
4430 * If this is called from ISR context, the default system fatal error handler
4431 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004432 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004433 */
4434#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4435
4436/**
4437 * @brief Fatally terminate the system
4438 *
4439 * This should be called when the Zephyr kernel has encountered an
4440 * unrecoverable runtime condition and needs to terminate. What this ultimately
4441 * means is determined by the _fatal_error_handler() implementation, which
4442 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004443 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004444 */
4445#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4446
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004447/*
4448 * private APIs that are utilized by one or more public APIs
4449 */
4450
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004451#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004452/**
4453 * @internal
4454 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004455extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004456#else
Anas Nashif954d5502018-02-25 08:37:28 -06004457/**
4458 * @internal
4459 */
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004460#define _init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004461#endif
4462
Anas Nashif954d5502018-02-25 08:37:28 -06004463/**
4464 * @internal
4465 */
Flavio Ceolin09e362e2018-12-17 12:34:05 -08004466extern bool _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004467/**
4468 * @internal
4469 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004470extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004471
Andrew Boiedc5d9352017-06-02 12:56:47 -07004472/* arch/cpu.h may declare an architecture or platform-specific macro
4473 * for properly declaring stacks, compatible with MMU/MPU constraints if
4474 * enabled
4475 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004476
4477/**
4478 * @brief Obtain an extern reference to a stack
4479 *
4480 * This macro properly brings the symbol of a thread stack declared
4481 * elsewhere into scope.
4482 *
4483 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004484 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004485 */
4486#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4487
Andrew Boiedc5d9352017-06-02 12:56:47 -07004488#ifdef _ARCH_THREAD_STACK_DEFINE
4489#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4490#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4491 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304492#define K_THREAD_STACK_LEN(size) _ARCH_THREAD_STACK_LEN(size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004493#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4494#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004495static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004496{
4497 return _ARCH_THREAD_STACK_BUFFER(sym);
4498}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004499#else
4500/**
4501 * @brief Declare a toplevel thread stack memory region
4502 *
4503 * This declares a region of memory suitable for use as a thread's stack.
4504 *
4505 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4506 * 'noinit' section so that it isn't zeroed at boot
4507 *
Andrew Boie507852a2017-07-25 18:47:07 -07004508 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004509 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004510 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004511 *
4512 * It is legal to precede this definition with the 'static' keyword.
4513 *
4514 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4515 * parameter of k_thread_create(), it may not be the same as the
4516 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4517 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004518 * Some arches may round the size of the usable stack region up to satisfy
4519 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4520 * size.
4521 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004522 * @param sym Thread stack symbol name
4523 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004524 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004525 */
4526#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004527 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004528
4529/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304530 * @brief Calculate size of stacks to be allocated in a stack array
4531 *
4532 * This macro calculates the size to be allocated for the stacks
4533 * inside a stack array. It accepts the indicated "size" as a parameter
4534 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4535 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4536 *
4537 * @param size Size of the stack memory region
4538 * @req K-TSTACK-001
4539 */
4540#define K_THREAD_STACK_LEN(size) (size)
4541
4542/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004543 * @brief Declare a toplevel array of thread stack memory regions
4544 *
4545 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4546 * definition for additional details and constraints.
4547 *
4548 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4549 * 'noinit' section so that it isn't zeroed at boot
4550 *
4551 * @param sym Thread stack symbol name
4552 * @param nmemb Number of stacks to declare
4553 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004554 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004555 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004556#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004557 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304558 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004559
4560/**
4561 * @brief Declare an embedded stack memory region
4562 *
4563 * Used for stacks embedded within other data structures. Use is highly
4564 * discouraged but in some cases necessary. For memory protection scenarios,
4565 * it is very important that any RAM preceding this member not be writable
4566 * by threads else a stack overflow will lead to silent corruption. In other
4567 * words, the containing data structure should live in RAM owned by the kernel.
4568 *
4569 * @param sym Thread stack symbol name
4570 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004571 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004572 */
4573#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004574 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004575
4576/**
4577 * @brief Return the size in bytes of a stack memory region
4578 *
4579 * Convenience macro for passing the desired stack size to k_thread_create()
4580 * since the underlying implementation may actually create something larger
4581 * (for instance a guard area).
4582 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004583 * The value returned here is not guaranteed to match the 'size' parameter
4584 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004585 *
4586 * @param sym Stack memory symbol
4587 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004588 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004589 */
4590#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4591
4592/**
4593 * @brief Get a pointer to the physical stack buffer
4594 *
4595 * Convenience macro to get at the real underlying stack buffer used by
4596 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4597 * This is really only intended for diagnostic tools which want to examine
4598 * stack memory contents.
4599 *
4600 * @param sym Declared stack symbol name
4601 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004602 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004603 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004604static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004605{
4606 return (char *)sym;
4607}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004608
4609#endif /* _ARCH_DECLARE_STACK */
4610
Chunlin Hane9c97022017-07-07 20:29:30 +08004611/**
4612 * @defgroup mem_domain_apis Memory domain APIs
4613 * @ingroup kernel_apis
4614 * @{
4615 */
4616
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004617/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004618 * @def K_MEM_PARTITION_DEFINE
4619 * @brief Used to declare a memory partition
4620 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004621 */
4622#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4623#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4624 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304625 __kernel struct k_mem_partition name =\
Ioannis Glaropoulos293247e2018-12-03 18:32:32 +01004626 { (u32_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004627#else
4628#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304629 __kernel struct k_mem_partition name =\
Ioannis Glaropoulos293247e2018-12-03 18:32:32 +01004630 { (u32_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004631#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4632
Chunlin Hane9c97022017-07-07 20:29:30 +08004633/* memory partition */
4634struct k_mem_partition {
4635 /* start address of memory partition */
4636 u32_t start;
4637 /* size of memory partition */
4638 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004639#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08004640 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304641 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004642#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08004643};
4644
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02004645/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304646 * Note: Always declare this structure with __kernel prefix
4647 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004648struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304649#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004650 /* partitions in the domain */
4651 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304652#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004653 /* domain q */
4654 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004655 /* number of partitions in the domain */
4656 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004657};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304658
Chunlin Hane9c97022017-07-07 20:29:30 +08004659
4660/**
4661 * @brief Initialize a memory domain.
4662 *
4663 * Initialize a memory domain with given name and memory partitions.
4664 *
4665 * @param domain The memory domain to be initialized.
4666 * @param num_parts The number of array items of "parts" parameter.
4667 * @param parts An array of pointers to the memory partitions. Can be NULL
4668 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004669 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004670 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004671extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004672 struct k_mem_partition *parts[]);
4673/**
4674 * @brief Destroy a memory domain.
4675 *
4676 * Destroy a memory domain.
4677 *
4678 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004679 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004680 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004681extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4682
4683/**
4684 * @brief Add a memory partition into a memory domain.
4685 *
4686 * Add a memory partition into a memory domain.
4687 *
4688 * @param domain The memory domain to be added a memory partition.
4689 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004690 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004691 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004692extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4693 struct k_mem_partition *part);
4694
4695/**
4696 * @brief Remove a memory partition from a memory domain.
4697 *
4698 * Remove a memory partition from a memory domain.
4699 *
4700 * @param domain The memory domain to be removed a memory partition.
4701 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004702 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004703 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004704extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4705 struct k_mem_partition *part);
4706
4707/**
4708 * @brief Add a thread into a memory domain.
4709 *
4710 * Add a thread into a memory domain.
4711 *
4712 * @param domain The memory domain that the thread is going to be added into.
4713 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004714 *
4715 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004716 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004717extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4718 k_tid_t thread);
4719
4720/**
4721 * @brief Remove a thread from its memory domain.
4722 *
4723 * Remove a thread from its memory domain.
4724 *
4725 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004726 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004727 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004728extern void k_mem_domain_remove_thread(k_tid_t thread);
4729
Anas Nashif166f5192018-02-25 08:02:36 -06004730/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004731
Andrew Boie756f9072017-10-10 16:01:49 -07004732/**
4733 * @brief Emit a character buffer to the console device
4734 *
4735 * @param c String of characters to print
4736 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004737 *
4738 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004739 */
4740__syscall void k_str_out(char *c, size_t n);
4741
Andy Rosse7172672018-01-24 15:48:32 -08004742/**
4743 * @brief Start a numbered CPU on a MP-capable system
4744
4745 * This starts and initializes a specific CPU. The main thread on
4746 * startup is running on CPU zero, other processors are numbered
4747 * sequentially. On return from this function, the CPU is known to
4748 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004749 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004750 * with the provided key will work to enable them.
4751 *
4752 * Normally, in SMP mode this function will be called by the kernel
4753 * initialization and should not be used as a user API. But it is
4754 * defined here for special-purpose apps which want Zephyr running on
4755 * one core and to use others for design-specific processing.
4756 *
4757 * @param cpu_num Integer number of the CPU
4758 * @param stack Stack memory for the CPU
4759 * @param sz Stack buffer size, in bytes
4760 * @param fn Function to begin running on the CPU. First argument is
4761 * an irq_unlock() key.
4762 * @param arg Untyped argument to be passed to "fn"
4763 */
4764extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08004765 void (*fn)(int key, void *data), void *arg);
Andy Rosse7172672018-01-24 15:48:32 -08004766
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004767#ifdef __cplusplus
4768}
4769#endif
4770
Anas Nashifb6304e62018-07-04 08:03:03 -05004771#include <tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07004772#include <syscalls/kernel.h>
4773
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004774#endif /* !_ASMLANGUAGE */
4775
Flavio Ceolin67ca1762018-09-14 10:43:44 -07004776#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */