blob: 09c0b52e6b5254a5ad798c0e9056c1ab8e5644f0 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
Flavio Ceolin67ca1762018-09-14 10:43:44 -070013#ifndef ZEPHYR_INCLUDE_KERNEL_H_
14#define ZEPHYR_INCLUDE_KERNEL_H_
Benjamin Walsh456c6da2016-09-02 18:55:39 -040015
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Ioannis Glaropoulos92b8a412018-06-20 17:30:48 +020017#include <kernel_includes.h>
Kumar Gala8777ff12018-07-25 20:24:34 -050018#include <errno.h>
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -070019#include <stdbool.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020
21#ifdef __cplusplus
22extern "C" {
23#endif
24
Anas Nashifbbb157d2017-01-15 08:46:31 -050025/**
26 * @brief Kernel APIs
27 * @defgroup kernel_apis Kernel APIs
28 * @{
29 * @}
30 */
31
Anas Nashif61f4b242016-11-18 10:53:59 -050032#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040033#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
34#else
35#define K_DEBUG(fmt, ...)
36#endif
37
Benjamin Walsh2f280412017-01-14 19:23:46 -050038#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
39#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
40#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
41#elif defined(CONFIG_COOP_ENABLED)
42#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
43#define _NUM_PREEMPT_PRIO (0)
44#elif defined(CONFIG_PREEMPT_ENABLED)
45#define _NUM_COOP_PRIO (0)
46#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
47#else
48#error "invalid configuration"
49#endif
50
51#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040052#define K_PRIO_PREEMPT(x) (x)
53
Benjamin Walsh456c6da2016-09-02 18:55:39 -040054#define K_ANY NULL
55#define K_END NULL
56
Benjamin Walshedb35702017-01-14 18:47:22 -050057#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040058#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050059#elif defined(CONFIG_COOP_ENABLED)
60#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
61#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040062#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050063#else
64#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040065#endif
66
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050067#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040068#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
69#else
70#define K_LOWEST_THREAD_PRIO -1
71#endif
72
Benjamin Walshfab8d922016-11-08 15:36:36 -050073#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
74
Benjamin Walsh456c6da2016-09-02 18:55:39 -040075#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
76#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
77
Andy Ross225c74b2018-06-27 11:20:50 -070078#ifdef CONFIG_WAITQ_SCALABLE
Andy Ross1acd8c22018-05-03 14:51:49 -070079
80typedef struct {
81 struct _priq_rb waitq;
82} _wait_q_t;
83
Flavio Ceolin02ed85b2018-09-20 15:43:57 -070084extern bool _priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
Andy Ross1acd8c22018-05-03 14:51:49 -070085
86#define _WAIT_Q_INIT(wait_q) { { { .lessthan_fn = _priq_rb_lessthan } } }
87
88#else
89
Andy Rossccf3bf72018-05-10 11:10:34 -070090typedef struct {
91 sys_dlist_t waitq;
92} _wait_q_t;
93
94#define _WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
Benjamin Walsh456c6da2016-09-02 18:55:39 -040095
Andy Ross1acd8c22018-05-03 14:51:49 -070096#endif
97
Anas Nashif2f203c22016-12-18 06:57:45 -050098#ifdef CONFIG_OBJECT_TRACING
Flavio Ceolind1ed3362018-12-07 11:39:13 -080099#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next;
Anas Nashif2f203c22016-12-18 06:57:45 -0500100#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400101#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500102#define _OBJECT_TRACING_INIT
Flavio Ceolind1ed3362018-12-07 11:39:13 -0800103#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400104#endif
105
Benjamin Walshacc68c12017-01-29 18:57:45 -0500106#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300107#define _POLL_EVENT_OBJ_INIT(obj) \
108 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
109#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500110#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300111#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500112#define _POLL_EVENT
113#endif
114
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500115struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400116struct k_mutex;
117struct k_sem;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400118struct k_msgq;
119struct k_mbox;
120struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200121struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122struct k_fifo;
123struct k_lifo;
124struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400125struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400126struct k_mem_pool;
127struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500128struct k_poll_event;
129struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800130struct k_mem_domain;
131struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400132
Andrew Boie5bd891d2017-09-27 12:59:28 -0700133/* This enumeration needs to be kept in sync with the lists of kernel objects
134 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
135 * function in kernel/userspace.c
136 */
Andrew Boie945af952017-08-22 13:15:23 -0700137enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700138 K_OBJ_ANY,
139
Leandro Pereirac2003672018-04-04 13:50:32 -0700140 /** @cond
141 * Doxygen should ignore this build-time generated include file
142 * when genrating API documentation. Enumeration values are
143 * generated during build by gen_kobject_list.py. It includes
144 * basic kernel objects (e.g. pipes and mutexes) and driver types.
145 */
146#include <kobj-types-enum.h>
147 /** @endcond
148 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700149
Andrew Boie945af952017-08-22 13:15:23 -0700150 K_OBJ_LAST
151};
Anas Nashif4bcb2942019-01-23 23:06:29 -0500152/**
153 * @defgroup usermode_apis User Mode APIs
154 * @ingroup kernel_apis
155 * @{
156 */
Andrew Boie945af952017-08-22 13:15:23 -0700157
158#ifdef CONFIG_USERSPACE
159/* Table generated by gperf, these objects are retrieved via
160 * _k_object_find() */
161struct _k_object {
162 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700163 u8_t perms[CONFIG_MAX_THREAD_BYTES];
164 u8_t type;
165 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700166 u32_t data;
Andrew Boiedf555242018-05-25 07:28:54 -0700167} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700168
Andrew Boie877f82e2017-10-17 11:20:22 -0700169struct _k_object_assignment {
170 struct k_thread *thread;
171 void * const *objects;
172};
173
174/**
175 * @brief Grant a static thread access to a list of kernel objects
176 *
177 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
178 * a set of kernel objects. These objects do not need to be in an initialized
179 * state. The permissions will be granted when the threads are initialized
180 * in the early boot sequence.
181 *
182 * All arguments beyond the first must be pointers to kernel objects.
183 *
184 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
185 */
186#define K_THREAD_ACCESS_GRANT(name_, ...) \
187 static void * const _CONCAT(_object_list_, name_)[] = \
188 { __VA_ARGS__, NULL }; \
189 static __used __in_section_unique(object_access) \
190 const struct _k_object_assignment \
191 _CONCAT(_object_access_, name_) = \
192 { (&_k_thread_obj_ ## name_), \
193 (_CONCAT(_object_list_, name_)) }
194
Andrew Boie945af952017-08-22 13:15:23 -0700195#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700196#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700197#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie945af952017-08-22 13:15:23 -0700198
199/**
200 * Lookup a kernel object and init its metadata if it exists
201 *
202 * Calling this on an object will make it usable from userspace.
203 * Intended to be called as the last statement in kernel object init
204 * functions.
205 *
Anas Nashif50e3acd2018-12-08 13:26:18 -0500206 * @param obj Address of the kernel object
Andrew Boie945af952017-08-22 13:15:23 -0700207 */
208void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700209#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700210
211#define K_THREAD_ACCESS_GRANT(thread, ...)
212
Anas Nashif954d5502018-02-25 08:37:28 -0600213/**
214 * @internal
215 */
Andrew Boie743e4682017-10-04 12:25:50 -0700216static inline void _k_object_init(void *obj)
217{
218 ARG_UNUSED(obj);
219}
220
Anas Nashif954d5502018-02-25 08:37:28 -0600221/**
222 * @internal
223 */
Andrew Boie743e4682017-10-04 12:25:50 -0700224static inline void _impl_k_object_access_grant(void *object,
225 struct k_thread *thread)
226{
227 ARG_UNUSED(object);
228 ARG_UNUSED(thread);
229}
230
Anas Nashif954d5502018-02-25 08:37:28 -0600231/**
232 * @internal
233 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700234static inline void k_object_access_revoke(void *object,
235 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700236{
237 ARG_UNUSED(object);
238 ARG_UNUSED(thread);
239}
240
Andrew Boiee9cfc542018-04-13 13:15:28 -0700241/**
242 * @internal
243 */
244static inline void _impl_k_object_release(void *object)
245{
246 ARG_UNUSED(object);
247}
248
Andrew Boie41bab6e2017-10-14 14:42:23 -0700249static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700250{
251 ARG_UNUSED(object);
252}
253#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700254
255/**
256 * grant a thread access to a kernel object
257 *
258 * The thread will be granted access to the object if the caller is from
259 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700260 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700261 *
262 * @param object Address of kernel object
263 * @param thread Thread to grant access to the object
264 */
Andrew Boie743e4682017-10-04 12:25:50 -0700265__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700266
Andrew Boiea89bf012017-10-09 14:47:55 -0700267/**
268 * grant a thread access to a kernel object
269 *
270 * The thread will lose access to the object if the caller is from
271 * supervisor mode, or the caller is from user mode AND has permissions
272 * on both the object and the thread whose access is being revoked.
273 *
274 * @param object Address of kernel object
275 * @param thread Thread to remove access to the object
276 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700277void k_object_access_revoke(void *object, struct k_thread *thread);
278
279
280__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700281
282/**
283 * grant all present and future threads access to an object
284 *
285 * If the caller is from supervisor mode, or the caller is from user mode and
286 * have sufficient permissions on the object, then that object will have
287 * permissions granted to it for *all* current and future threads running in
288 * the system, effectively becoming a public kernel object.
289 *
290 * Use of this API should be avoided on systems that are running untrusted code
291 * as it is possible for such code to derive the addresses of kernel objects
292 * and perform unwanted operations on them.
293 *
Andrew Boie04caa672017-10-13 13:57:07 -0700294 * It is not possible to revoke permissions on public objects; once public,
295 * any thread may use it.
296 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700297 * @param object Address of kernel object
298 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700299void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700300
Andrew Boie31bdfc02017-11-08 16:38:03 -0800301/**
302 * Allocate a kernel object of a designated type
303 *
304 * This will instantiate at runtime a kernel object of the specified type,
305 * returning a pointer to it. The object will be returned in an uninitialized
306 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700307 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800308 *
309 * Currently, allocation of thread stacks is not supported.
310 *
311 * @param otype Requested kernel object type
312 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
313 * available
314 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700315__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800316
Andrew Boie97bf0012018-04-24 17:01:37 -0700317#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800318/**
319 * Free a kernel object previously allocated with k_object_alloc()
320 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700321 * This will return memory for a kernel object back to resource pool it was
322 * allocated from. Care must be exercised that the object will not be used
323 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800324 *
325 * @param obj Pointer to the kernel object memory address.
326 */
327void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700328#else
329static inline void *_impl_k_object_alloc(enum k_objects otype)
330{
Kumar Gala85699f72018-05-17 09:26:03 -0500331 ARG_UNUSED(otype);
332
Andrew Boie97bf0012018-04-24 17:01:37 -0700333 return NULL;
334}
335
336static inline void k_obj_free(void *obj)
337{
338 ARG_UNUSED(obj);
339}
Andrew Boie31bdfc02017-11-08 16:38:03 -0800340#endif /* CONFIG_DYNAMIC_OBJECTS */
341
Anas Nashif4bcb2942019-01-23 23:06:29 -0500342/** @} */
343
Andrew Boiebca15da2017-10-15 14:17:48 -0700344/* Using typedef deliberately here, this is quite intended to be an opaque
345 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
346 *
347 * The purpose of this data type is to clearly distinguish between the
348 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
349 * buffer which composes the stack data actually used by the underlying
350 * thread; they cannot be used interchangably as some arches precede the
351 * stack buffer region with guard areas that trigger a MPU or MMU fault
352 * if written to.
353 *
354 * APIs that want to work with the buffer inside should continue to use
355 * char *.
356 *
357 * Stacks should always be created with K_THREAD_STACK_DEFINE().
358 */
359struct __packed _k_thread_stack_element {
360 char data;
361};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700362typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700363
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700364/**
365 * @typedef k_thread_entry_t
366 * @brief Thread entry point function type.
367 *
368 * A thread's entry point function is invoked when the thread starts executing.
369 * Up to 3 argument values can be passed to the function.
370 *
371 * The thread terminates execution permanently if the entry point function
372 * returns. The thread is responsible for releasing any shared resources
373 * it may own (such as mutexes and dynamically allocated memory), prior to
374 * returning.
375 *
376 * @param p1 First argument.
377 * @param p2 Second argument.
378 * @param p3 Third argument.
379 *
380 * @return N/A
381 */
382typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700383
384#ifdef CONFIG_THREAD_MONITOR
385struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700386 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700387 void *parameter1;
388 void *parameter2;
389 void *parameter3;
390};
391#endif
392
393/* can be used for creating 'dummy' threads, e.g. for pending on objects */
394struct _thread_base {
395
396 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700397 union {
Peter A. Bigot82ad0d22019-01-03 23:49:28 -0600398 sys_dnode_t qnode_dlist;
Andy Ross1acd8c22018-05-03 14:51:49 -0700399 struct rbnode qnode_rb;
400 };
401
Andy Ross1acd8c22018-05-03 14:51:49 -0700402 /* wait queue on which the thread is pended (needed only for
403 * trees, not dumb lists)
404 */
405 _wait_q_t *pended_on;
Andrew Boie73abd322017-04-04 13:19:13 -0700406
407 /* user facing 'thread options'; values defined in include/kernel.h */
408 u8_t user_options;
409
410 /* thread state */
411 u8_t thread_state;
412
413 /*
414 * scheduler lock count and thread priority
415 *
416 * These two fields control the preemptibility of a thread.
417 *
418 * When the scheduler is locked, sched_locked is decremented, which
419 * means that the scheduler is locked for values from 0xff to 0x01. A
420 * thread is coop if its prio is negative, thus 0x80 to 0xff when
421 * looked at the value as unsigned.
422 *
423 * By putting them end-to-end, this means that a thread is
424 * non-preemptible if the bundled value is greater than or equal to
425 * 0x0080.
426 */
427 union {
428 struct {
429#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
430 u8_t sched_locked;
431 s8_t prio;
432#else /* LITTLE and PDP */
433 s8_t prio;
434 u8_t sched_locked;
435#endif
436 };
437 u16_t preempt;
438 };
439
Andy Ross4a2e50f2018-05-15 11:06:25 -0700440#ifdef CONFIG_SCHED_DEADLINE
441 int prio_deadline;
442#endif
443
Andy Ross1acd8c22018-05-03 14:51:49 -0700444 u32_t order_key;
445
Andy Ross2724fd12018-01-29 14:55:20 -0800446#ifdef CONFIG_SMP
447 /* True for the per-CPU idle threads */
448 u8_t is_idle;
449
Andy Ross2724fd12018-01-29 14:55:20 -0800450 /* CPU index on which thread was last run */
451 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700452
453 /* Recursive count of irq_lock() calls */
454 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800455#endif
456
Andrew Boie73abd322017-04-04 13:19:13 -0700457 /* data returned by APIs */
458 void *swap_data;
459
460#ifdef CONFIG_SYS_CLOCK_EXISTS
461 /* this thread's entry in a timeout queue */
462 struct _timeout timeout;
463#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700464};
465
466typedef struct _thread_base _thread_base_t;
467
468#if defined(CONFIG_THREAD_STACK_INFO)
469/* Contains the stack information of a thread */
470struct _thread_stack_info {
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700471 /* Stack Start - Identical to K_THREAD_STACK_BUFFER() on the stack
472 * object. Represents thread-writable stack area without any extras.
473 */
Andrew Boie73abd322017-04-04 13:19:13 -0700474 u32_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700475
476 /* Stack Size - Thread writable stack buffer size. Represents
477 * the size of the actual area, starting from the start member,
478 * that should be writable by the thread
479 */
Andrew Boie73abd322017-04-04 13:19:13 -0700480 u32_t size;
481};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700482
483typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700484#endif /* CONFIG_THREAD_STACK_INFO */
485
Chunlin Hane9c97022017-07-07 20:29:30 +0800486#if defined(CONFIG_USERSPACE)
487struct _mem_domain_info {
488 /* memory domain queue node */
489 sys_dnode_t mem_domain_q_node;
490 /* memory domain of the thread */
491 struct k_mem_domain *mem_domain;
492};
493
494#endif /* CONFIG_USERSPACE */
495
Daniel Leungfc182432018-08-16 15:42:28 -0700496#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
497struct _thread_userspace_local_data {
498 int errno_var;
499};
500#endif
501
Anas Nashifce78d162018-05-24 12:43:11 -0500502/**
503 * @ingroup thread_apis
504 * Thread Structure
505 */
Andrew Boie73abd322017-04-04 13:19:13 -0700506struct k_thread {
507
508 struct _thread_base base;
509
Anas Nashifce78d162018-05-24 12:43:11 -0500510 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700511 struct _caller_saved caller_saved;
Anas Nashifce78d162018-05-24 12:43:11 -0500512 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700513 struct _callee_saved callee_saved;
514
Anas Nashifce78d162018-05-24 12:43:11 -0500515 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700516 void *init_data;
517
Anas Nashifce78d162018-05-24 12:43:11 -0500518 /**
519 * abort function
520 * @req K-THREAD-002
521 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700522 void (*fn_abort)(void);
523
524#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500525 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700526 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700527
Anas Nashifce78d162018-05-24 12:43:11 -0500528 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700529 struct k_thread *next_thread;
530#endif
531
Anas Nashif57554052018-03-03 02:31:05 -0600532#if defined(CONFIG_THREAD_NAME)
533 /* Thread name */
534 const char *name;
535#endif
536
Andrew Boie73abd322017-04-04 13:19:13 -0700537#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500538 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700539 void *custom_data;
540#endif
541
Daniel Leungfc182432018-08-16 15:42:28 -0700542#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
543 struct _thread_userspace_local_data *userspace_local_data;
544#endif
545
Andrew Boie73abd322017-04-04 13:19:13 -0700546#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700547#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500548 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700549 int errno_var;
550#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700551#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700552
553#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500554 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700555 struct _thread_stack_info stack_info;
556#endif /* CONFIG_THREAD_STACK_INFO */
557
Chunlin Hane9c97022017-07-07 20:29:30 +0800558#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500559 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800560 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500561 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700562 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800563#endif /* CONFIG_USERSPACE */
564
Andy Ross042d8ec2017-12-09 08:37:20 -0800565#if defined(CONFIG_USE_SWITCH)
566 /* When using __switch() a few previously arch-specific items
567 * become part of the core OS
568 */
569
Anas Nashifce78d162018-05-24 12:43:11 -0500570 /** _Swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800571 int swap_retval;
572
Anas Nashifce78d162018-05-24 12:43:11 -0500573 /** Context handle returned via _arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800574 void *switch_handle;
575#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500576 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700577 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800578
Anas Nashifce78d162018-05-24 12:43:11 -0500579 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700580 struct _thread_arch arch;
581};
582
583typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400584typedef struct k_thread *k_tid_t;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400585
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400586enum execution_context_types {
587 K_ISR = 0,
588 K_COOP_THREAD,
589 K_PREEMPT_THREAD,
590};
591
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400592/**
Anas Nashif4bcb2942019-01-23 23:06:29 -0500593 * @addtogroup thread_apis
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100594 * @{
595 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530596typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
597 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100598
599/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530600 * @brief Iterate over all the threads in the system.
601 *
602 * This routine iterates over all the threads in the system and
603 * calls the user_cb function for each thread.
604 *
605 * @param user_cb Pointer to the user callback function.
606 * @param user_data Pointer to user data.
607 *
608 * @note CONFIG_THREAD_MONITOR must be set for this function
609 * to be effective. Also this API uses irq_lock to protect the
610 * _kernel.threads list which means creation of new threads and
611 * terminations of existing threads are blocked until this
612 * API returns.
613 *
614 * @return N/A
615 */
616extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
617
Anas Nashif166f5192018-02-25 08:02:36 -0600618/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100619
620/**
Allan Stephensc98da842016-11-11 15:45:03 -0500621 * @defgroup thread_apis Thread APIs
622 * @ingroup kernel_apis
623 * @{
624 */
625
Benjamin Walshed240f22017-01-22 13:05:08 -0500626#endif /* !_ASMLANGUAGE */
627
628
629/*
630 * Thread user options. May be needed by assembly code. Common part uses low
631 * bits, arch-specific use high bits.
632 */
633
Anas Nashifa541e932018-05-24 11:19:16 -0500634/**
635 * @brief system thread that must not abort
636 * @req K-THREAD-000
637 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700638#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500639
640#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500641/**
642 * @brief thread uses floating point registers
643 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700644#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500645#endif
646
Anas Nashifa541e932018-05-24 11:19:16 -0500647/**
648 * @brief user mode thread
649 *
650 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700651 * has additional restrictions
652 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700653#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700654
Anas Nashifa541e932018-05-24 11:19:16 -0500655/**
656 * @brief Inherit Permissions
657 *
658 * @details
659 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700660 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
661 * is not enabled.
662 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700663#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700664
Benjamin Walshed240f22017-01-22 13:05:08 -0500665#ifdef CONFIG_X86
666/* x86 Bitmask definitions for threads user options */
667
668#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
669/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700670#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500671#endif
672#endif
673
674/* end - thread options */
675
676#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400677/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700678 * @brief Create a thread.
679 *
680 * This routine initializes a thread, then schedules it for execution.
681 *
682 * The new thread may be scheduled for immediate execution or a delayed start.
683 * If the newly spawned thread does not have a delayed start the kernel
684 * scheduler may preempt the current thread to allow the new thread to
685 * execute.
686 *
687 * Thread options are architecture-specific, and can include K_ESSENTIAL,
688 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
689 * them using "|" (the logical OR operator).
690 *
691 * Historically, users often would use the beginning of the stack memory region
692 * to store the struct k_thread data, although corruption will occur if the
693 * stack overflows this region and stack protection features may not detect this
694 * situation.
695 *
696 * @param new_thread Pointer to uninitialized struct k_thread
697 * @param stack Pointer to the stack space.
698 * @param stack_size Stack size in bytes.
699 * @param entry Thread entry function.
700 * @param p1 1st entry point parameter.
701 * @param p2 2nd entry point parameter.
702 * @param p3 3rd entry point parameter.
703 * @param prio Thread priority.
704 * @param options Thread options.
705 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
706 *
707 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400708 *
709 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700710 */
Andrew Boie662c3452017-10-02 10:51:18 -0700711__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700712 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700713 size_t stack_size,
714 k_thread_entry_t entry,
715 void *p1, void *p2, void *p3,
716 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700717
Andrew Boie3f091b52017-08-30 14:34:14 -0700718/**
719 * @brief Drop a thread's privileges permanently to user mode
720 *
721 * @param entry Function to start executing from
722 * @param p1 1st entry point parameter
723 * @param p2 2nd entry point parameter
724 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400725 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700726 */
727extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
728 void *p1, void *p2,
729 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700730
Andrew Boied26cf2d2017-03-30 13:07:02 -0700731/**
Adithya Baglody392219e2019-01-02 14:40:39 +0530732 * @brief Grant a thread access to a set of kernel objects
Andrew Boiee12857a2017-10-17 11:38:26 -0700733 *
734 * This is a convenience function. For the provided thread, grant access to
735 * the remaining arguments, which must be pointers to kernel objects.
Andrew Boiee12857a2017-10-17 11:38:26 -0700736 *
737 * The thread object must be initialized (i.e. running). The objects don't
738 * need to be.
Adithya Baglody392219e2019-01-02 14:40:39 +0530739 * Note that NULL shouldn't be passed as an argument.
Andrew Boiee12857a2017-10-17 11:38:26 -0700740 *
741 * @param thread Thread to grant access to objects
Adithya Baglody392219e2019-01-02 14:40:39 +0530742 * @param ... list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400743 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700744 */
Adithya Baglody392219e2019-01-02 14:40:39 +0530745#define k_thread_access_grant(thread, ...) \
746 FOR_EACH_FIXED_ARG(k_object_access_grant, thread, __VA_ARGS__)
Andrew Boiee12857a2017-10-17 11:38:26 -0700747
748/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700749 * @brief Assign a resource memory pool to a thread
750 *
751 * By default, threads have no resource pool assigned unless their parent
752 * thread has a resource pool, in which case it is inherited. Multiple
753 * threads may be assigned to the same memory pool.
754 *
755 * Changing a thread's resource pool will not migrate allocations from the
756 * previous pool.
757 *
758 * @param thread Target thread to assign a memory pool for resource requests,
759 * or NULL if the thread should no longer have a memory pool.
760 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400761 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700762 */
763static inline void k_thread_resource_pool_assign(struct k_thread *thread,
764 struct k_mem_pool *pool)
765{
766 thread->resource_pool = pool;
767}
768
769#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
770/**
771 * @brief Assign the system heap as a thread's resource pool
772 *
773 * Similar to k_thread_resource_pool_assign(), but the thread will use
774 * the kernel heap to draw memory.
775 *
776 * Use with caution, as a malicious thread could perform DoS attacks on the
777 * kernel heap.
778 *
779 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400780 *
781 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700782 */
783void k_thread_system_pool_assign(struct k_thread *thread);
784#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
785
786/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500787 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400788 *
Allan Stephensc98da842016-11-11 15:45:03 -0500789 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500790 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400791 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500792 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400793 *
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200794 * @return Zero if the requested time has elapsed or the number of milliseconds
795 * left to sleep, if thread was woken up by \ref k_wakeup call.
796 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400797 */
Piotr Zięcik7700eb22018-10-25 17:45:08 +0200798__syscall s32_t k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400799
800/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500801 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400802 *
803 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500804 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400805 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400806 * @return N/A
807 */
Andrew Boie42cfd4f2018-11-14 14:29:24 -0800808__syscall void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400809
810/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500811 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500813 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400814 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500815 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400816 *
817 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400818 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819 */
Andrew Boie468190a2017-09-29 14:00:48 -0700820__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400821
822/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500823 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500825 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400826 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500827 * If @a thread is not currently sleeping, the routine has no effect.
828 *
829 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830 *
831 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400832 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400833 */
Andrew Boie468190a2017-09-29 14:00:48 -0700834__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400835
836/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500837 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500839 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400840 *
841 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400842 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700843__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844
845/**
Allan Stephensc98da842016-11-11 15:45:03 -0500846 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500848 * This routine permanently stops execution of @a thread. The thread is taken
849 * off all kernel queues it is part of (i.e. the ready queue, the timeout
850 * queue, or a kernel object wait queue). However, any kernel resources the
851 * thread might currently own (such as mutexes or memory blocks) are not
852 * released. It is the responsibility of the caller of this routine to ensure
853 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500855 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856 *
857 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400858 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400859 */
Andrew Boie468190a2017-09-29 14:00:48 -0700860__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400861
Andrew Boie7d627c52017-08-30 11:01:56 -0700862
863/**
864 * @brief Start an inactive thread
865 *
866 * If a thread was created with K_FOREVER in the delay parameter, it will
867 * not be added to the scheduling queue until this function is called
868 * on it.
869 *
870 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400871 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700872 */
Andrew Boie468190a2017-09-29 14:00:48 -0700873__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700874
Allan Stephensc98da842016-11-11 15:45:03 -0500875/**
876 * @cond INTERNAL_HIDDEN
877 */
878
Benjamin Walshd211a522016-12-06 11:44:01 -0500879/* timeout has timed out and is not on _timeout_q anymore */
880#define _EXPIRED (-2)
881
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400882struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700883 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700884 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400885 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700886 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500887 void *init_p1;
888 void *init_p2;
889 void *init_p3;
890 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500891 u32_t init_options;
892 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500893 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600894 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400895};
896
Andrew Boied26cf2d2017-03-30 13:07:02 -0700897#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400898 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600899 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500900 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700901 .init_thread = (thread), \
902 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500903 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700904 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400905 .init_p1 = (void *)p1, \
906 .init_p2 = (void *)p2, \
907 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500908 .init_prio = (prio), \
909 .init_options = (options), \
910 .init_delay = (delay), \
911 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600912 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400913 }
914
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400915/**
Allan Stephensc98da842016-11-11 15:45:03 -0500916 * INTERNAL_HIDDEN @endcond
917 */
918
919/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500920 * @brief Statically define and initialize a thread.
921 *
922 * The thread may be scheduled for immediate execution or a delayed start.
923 *
924 * Thread options are architecture-specific, and can include K_ESSENTIAL,
925 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
926 * them using "|" (the logical OR operator).
927 *
928 * The ID of the thread can be accessed using:
929 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500930 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500931 *
932 * @param name Name of the thread.
933 * @param stack_size Stack size in bytes.
934 * @param entry Thread entry function.
935 * @param p1 1st entry point parameter.
936 * @param p2 2nd entry point parameter.
937 * @param p3 3rd entry point parameter.
938 * @param prio Thread priority.
939 * @param options Thread options.
940 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400941 *
Anas Nashif47420d02018-05-24 14:20:56 -0400942 * @req K-THREAD-010
943 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400944 * @internal It has been observed that the x86 compiler by default aligns
945 * these _static_thread_data structures to 32-byte boundaries, thereby
946 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400947 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400948 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500949#define K_THREAD_DEFINE(name, stack_size, \
950 entry, p1, p2, p3, \
951 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700952 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700953 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500954 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500955 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700956 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
957 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500958 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -0600959 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700960 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400961
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400962/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500963 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400964 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500965 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400966 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500967 * @param thread ID of thread whose priority is needed.
968 *
969 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400970 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400971 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700972__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400973
974/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500975 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500977 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400978 *
979 * Rescheduling can occur immediately depending on the priority @a thread is
980 * set to:
981 *
982 * - If its priority is raised above the priority of the caller of this
983 * function, and the caller is preemptible, @a thread will be scheduled in.
984 *
985 * - If the caller operates on itself, it lowers its priority below that of
986 * other threads in the system, and the caller is preemptible, the thread of
987 * highest priority will be scheduled in.
988 *
989 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
990 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
991 * highest priority.
992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500993 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400994 * @param prio New priority.
995 *
996 * @warning Changing the priority of a thread currently involved in mutex
997 * priority inheritance may result in undefined behavior.
998 *
999 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001000 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001001 */
Andrew Boie468190a2017-09-29 14:00:48 -07001002__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001003
Andy Ross4a2e50f2018-05-15 11:06:25 -07001004
1005#ifdef CONFIG_SCHED_DEADLINE
1006/**
1007 * @brief Set deadline expiration time for scheduler
1008 *
1009 * This sets the "deadline" expiration as a time delta from the
1010 * current time, in the same units used by k_cycle_get_32(). The
1011 * scheduler (when deadline scheduling is enabled) will choose the
1012 * next expiring thread when selecting between threads at the same
1013 * static priority. Threads at different priorities will be scheduled
1014 * according to their static priority.
1015 *
1016 * @note Deadlines that are negative (i.e. in the past) are still seen
1017 * as higher priority than others, even if the thread has "finished"
1018 * its work. If you don't want it scheduled anymore, you have to
1019 * reset the deadline into the future, block/pend the thread, or
1020 * modify its priority with k_thread_priority_set().
1021 *
1022 * @note Despite the API naming, the scheduler makes no guarantees the
1023 * the thread WILL be scheduled within that deadline, nor does it take
1024 * extra metadata (like e.g. the "runtime" and "period" parameters in
1025 * Linux sched_setattr()) that allows the kernel to validate the
1026 * scheduling for achievability. Such features could be implemented
1027 * above this call, which is simply input to the priority selection
1028 * logic.
1029 *
1030 * @param thread A thread on which to set the deadline
1031 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001032 *
1033 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001034 */
1035__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1036#endif
1037
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001038/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001039 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001040 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001041 * This routine prevents the kernel scheduler from making @a thread the
1042 * current thread. All other internal operations on @a thread are still
1043 * performed; for example, any timeout it is waiting on keeps ticking,
1044 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001046 * If @a thread is already suspended, the routine has no effect.
1047 *
1048 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001049 *
1050 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001051 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001052 */
Andrew Boie468190a2017-09-29 14:00:48 -07001053__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001054
1055/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001056 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001058 * This routine allows the kernel scheduler to make @a thread the current
1059 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001060 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001061 * If @a thread is not currently suspended, the routine has no effect.
1062 *
1063 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001064 *
1065 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001066 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001067 */
Andrew Boie468190a2017-09-29 14:00:48 -07001068__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001069
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001070/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001071 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001073 * This routine specifies how the scheduler will perform time slicing of
1074 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001076 * To enable time slicing, @a slice must be non-zero. The scheduler
1077 * ensures that no thread runs for more than the specified time limit
1078 * before other threads of that priority are given a chance to execute.
1079 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001080 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001081 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001082 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001083 * execute. Once the scheduler selects a thread for execution, there is no
1084 * minimum guaranteed time the thread will execute before threads of greater or
1085 * equal priority are scheduled.
1086 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001087 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001088 * for execution, this routine has no effect; the thread is immediately
1089 * rescheduled after the slice period expires.
1090 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001091 * To disable timeslicing, set both @a slice and @a prio to zero.
1092 *
1093 * @param slice Maximum time slice length (in milliseconds).
1094 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001095 *
1096 * @return N/A
1097 */
Kumar Galacc334c72017-04-21 10:55:34 -05001098extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001099
Anas Nashif166f5192018-02-25 08:02:36 -06001100/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001101
1102/**
1103 * @addtogroup isr_apis
1104 * @{
1105 */
1106
1107/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001108 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001109 *
Allan Stephensc98da842016-11-11 15:45:03 -05001110 * This routine allows the caller to customize its actions, depending on
1111 * whether it is a thread or an ISR.
1112 *
1113 * @note Can be called by ISRs.
1114 *
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001115 * @return false if invoked by a thread.
1116 * @return true if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001117 */
Flavio Ceolin6a4a86e2018-12-17 12:40:22 -08001118extern bool k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001119
Benjamin Walsh445830d2016-11-10 15:54:27 -05001120/**
1121 * @brief Determine if code is running in a preemptible thread.
1122 *
Allan Stephensc98da842016-11-11 15:45:03 -05001123 * This routine allows the caller to customize its actions, depending on
1124 * whether it can be preempted by another thread. The routine returns a 'true'
1125 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001126 *
Allan Stephensc98da842016-11-11 15:45:03 -05001127 * - The code is running in a thread, not at ISR.
1128 * - The thread's priority is in the preemptible range.
1129 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001130 *
Allan Stephensc98da842016-11-11 15:45:03 -05001131 * @note Can be called by ISRs.
1132 *
1133 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001134 * @return Non-zero if invoked by a preemptible thread.
1135 */
Andrew Boie468190a2017-09-29 14:00:48 -07001136__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001137
Allan Stephensc98da842016-11-11 15:45:03 -05001138/**
Anas Nashif166f5192018-02-25 08:02:36 -06001139 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001140 */
1141
1142/**
1143 * @addtogroup thread_apis
1144 * @{
1145 */
1146
1147/**
1148 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001149 *
Allan Stephensc98da842016-11-11 15:45:03 -05001150 * This routine prevents the current thread from being preempted by another
1151 * thread by instructing the scheduler to treat it as a cooperative thread.
1152 * If the thread subsequently performs an operation that makes it unready,
1153 * it will be context switched out in the normal manner. When the thread
1154 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001155 *
Allan Stephensc98da842016-11-11 15:45:03 -05001156 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001157 *
Allan Stephensc98da842016-11-11 15:45:03 -05001158 * @note k_sched_lock() and k_sched_unlock() should normally be used
1159 * when the operation being performed can be safely interrupted by ISRs.
1160 * However, if the amount of processing involved is very small, better
1161 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001162 *
1163 * @return N/A
1164 */
1165extern void k_sched_lock(void);
1166
Allan Stephensc98da842016-11-11 15:45:03 -05001167/**
1168 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001169 *
Allan Stephensc98da842016-11-11 15:45:03 -05001170 * This routine reverses the effect of a previous call to k_sched_lock().
1171 * A thread must call the routine once for each time it called k_sched_lock()
1172 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001173 *
1174 * @return N/A
1175 */
1176extern void k_sched_unlock(void);
1177
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001178/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001179 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001181 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001183 * Custom data is not used by the kernel itself, and is freely available
1184 * for a thread to use as it sees fit. It can be used as a framework
1185 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001186 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001187 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001188 *
1189 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001190 *
1191 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001192 */
Andrew Boie468190a2017-09-29 14:00:48 -07001193__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001194
1195/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001196 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001197 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001198 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001199 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001200 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001201 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001202 */
Andrew Boie468190a2017-09-29 14:00:48 -07001203__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001204
1205/**
Anas Nashif57554052018-03-03 02:31:05 -06001206 * @brief Set current thread name
1207 *
1208 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1209 * tracing and debugging.
1210 *
1211 */
1212__syscall void k_thread_name_set(k_tid_t thread_id, const char *value);
1213
1214/**
1215 * @brief Get thread name
1216 *
1217 * Get the name of a thread
1218 *
1219 * @param thread_id Thread ID
1220 *
1221 */
1222__syscall const char *k_thread_name_get(k_tid_t thread_id);
1223
1224/**
Andy Rosscfe62032018-09-29 07:34:55 -07001225 * @}
1226 */
1227
1228/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001229 * @addtogroup clock_apis
1230 * @{
1231 */
1232
1233/**
1234 * @brief Generate null timeout delay.
1235 *
1236 * This macro generates a timeout delay that that instructs a kernel API
1237 * not to wait if the requested operation cannot be performed immediately.
1238 *
1239 * @return Timeout delay value.
1240 */
1241#define K_NO_WAIT 0
1242
1243/**
1244 * @brief Generate timeout delay from milliseconds.
1245 *
1246 * This macro generates a timeout delay that that instructs a kernel API
1247 * to wait up to @a ms milliseconds to perform the requested operation.
1248 *
1249 * @param ms Duration in milliseconds.
1250 *
1251 * @return Timeout delay value.
1252 */
Johan Hedberg14471692016-11-13 10:52:15 +02001253#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001254
1255/**
1256 * @brief Generate timeout delay from seconds.
1257 *
1258 * This macro generates a timeout delay that that instructs a kernel API
1259 * to wait up to @a s seconds to perform the requested operation.
1260 *
1261 * @param s Duration in seconds.
1262 *
1263 * @return Timeout delay value.
1264 */
Johan Hedberg14471692016-11-13 10:52:15 +02001265#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001266
1267/**
1268 * @brief Generate timeout delay from minutes.
1269 *
1270 * This macro generates a timeout delay that that instructs a kernel API
1271 * to wait up to @a m minutes to perform the requested operation.
1272 *
1273 * @param m Duration in minutes.
1274 *
1275 * @return Timeout delay value.
1276 */
Johan Hedberg14471692016-11-13 10:52:15 +02001277#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001278
1279/**
1280 * @brief Generate timeout delay from hours.
1281 *
1282 * This macro generates a timeout delay that that instructs a kernel API
1283 * to wait up to @a h hours to perform the requested operation.
1284 *
1285 * @param h Duration in hours.
1286 *
1287 * @return Timeout delay value.
1288 */
Johan Hedberg14471692016-11-13 10:52:15 +02001289#define K_HOURS(h) K_MINUTES((h) * 60)
1290
Allan Stephensc98da842016-11-11 15:45:03 -05001291/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001292 * @brief Generate infinite timeout delay.
1293 *
1294 * This macro generates a timeout delay that that instructs a kernel API
1295 * to wait as long as necessary to perform the requested operation.
1296 *
1297 * @return Timeout delay value.
1298 */
1299#define K_FOREVER (-1)
1300
1301/**
Anas Nashif166f5192018-02-25 08:02:36 -06001302 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001303 */
1304
1305/**
Allan Stephensc98da842016-11-11 15:45:03 -05001306 * @cond INTERNAL_HIDDEN
1307 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001308
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001309struct k_timer {
1310 /*
1311 * _timeout structure must be first here if we want to use
1312 * dynamic timer allocation. timeout.node is used in the double-linked
1313 * list of free timers
1314 */
1315 struct _timeout timeout;
1316
Allan Stephens45bfa372016-10-12 12:39:42 -05001317 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001318 _wait_q_t wait_q;
1319
1320 /* runs in ISR context */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001321 void (*expiry_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001322
1323 /* runs in the context of the thread that calls k_timer_stop() */
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08001324 void (*stop_fn)(struct k_timer *timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001325
1326 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001327 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001328
Allan Stephens45bfa372016-10-12 12:39:42 -05001329 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001330 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001331
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001332 /* user-specific data, also used to support legacy features */
1333 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001334
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001335 _OBJECT_TRACING_NEXT_PTR(k_timer)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001336};
1337
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001338#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001339 { \
Peter A. Bigotb4ece0a2019-01-02 08:29:43 -06001340 .timeout.dticks = 0, \
Andy Ross987c0e52018-09-27 16:50:00 -07001341 .timeout.fn = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001342 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001343 .expiry_fn = expiry, \
1344 .stop_fn = stop, \
1345 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001346 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001347 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001348 }
1349
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001350#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1351
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001352/**
Allan Stephensc98da842016-11-11 15:45:03 -05001353 * INTERNAL_HIDDEN @endcond
1354 */
1355
1356/**
1357 * @defgroup timer_apis Timer APIs
1358 * @ingroup kernel_apis
1359 * @{
1360 */
1361
1362/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001363 * @typedef k_timer_expiry_t
1364 * @brief Timer expiry function type.
1365 *
1366 * A timer's expiry function is executed by the system clock interrupt handler
1367 * each time the timer expires. The expiry function is optional, and is only
1368 * invoked if the timer has been initialized with one.
1369 *
1370 * @param timer Address of timer.
1371 *
1372 * @return N/A
1373 */
1374typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1375
1376/**
1377 * @typedef k_timer_stop_t
1378 * @brief Timer stop function type.
1379 *
1380 * A timer's stop function is executed if the timer is stopped prematurely.
1381 * The function runs in the context of the thread that stops the timer.
1382 * The stop function is optional, and is only invoked if the timer has been
1383 * initialized with one.
1384 *
1385 * @param timer Address of timer.
1386 *
1387 * @return N/A
1388 */
1389typedef void (*k_timer_stop_t)(struct k_timer *timer);
1390
1391/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001392 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001393 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001394 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001395 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001396 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001397 *
1398 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001399 * @param expiry_fn Function to invoke each time the timer expires.
1400 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001401 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001402#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001403 struct k_timer name \
1404 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001405 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001406
Allan Stephens45bfa372016-10-12 12:39:42 -05001407/**
1408 * @brief Initialize a timer.
1409 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001410 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001411 *
1412 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001413 * @param expiry_fn Function to invoke each time the timer expires.
1414 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001415 *
1416 * @return N/A
1417 */
1418extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001419 k_timer_expiry_t expiry_fn,
1420 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001421
Allan Stephens45bfa372016-10-12 12:39:42 -05001422/**
1423 * @brief Start a timer.
1424 *
1425 * This routine starts a timer, and resets its status to zero. The timer
1426 * begins counting down using the specified duration and period values.
1427 *
1428 * Attempting to start a timer that is already running is permitted.
1429 * The timer's status is reset to zero and the timer begins counting down
1430 * using the new duration and period values.
1431 *
1432 * @param timer Address of timer.
1433 * @param duration Initial timer duration (in milliseconds).
1434 * @param period Timer period (in milliseconds).
1435 *
1436 * @return N/A
1437 */
Andrew Boiea354d492017-09-29 16:22:28 -07001438__syscall void k_timer_start(struct k_timer *timer,
1439 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001440
1441/**
1442 * @brief Stop a timer.
1443 *
1444 * This routine stops a running timer prematurely. The timer's stop function,
1445 * if one exists, is invoked by the caller.
1446 *
1447 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001448 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001449 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001450 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1451 * if @a k_timer_stop is to be called from ISRs.
1452 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001453 * @param timer Address of timer.
1454 *
1455 * @return N/A
1456 */
Andrew Boiea354d492017-09-29 16:22:28 -07001457__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001458
1459/**
1460 * @brief Read timer status.
1461 *
1462 * This routine reads the timer's status, which indicates the number of times
1463 * it has expired since its status was last read.
1464 *
1465 * Calling this routine resets the timer's status to zero.
1466 *
1467 * @param timer Address of timer.
1468 *
1469 * @return Timer status.
1470 */
Andrew Boiea354d492017-09-29 16:22:28 -07001471__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001472
1473/**
1474 * @brief Synchronize thread to timer expiration.
1475 *
1476 * This routine blocks the calling thread until the timer's status is non-zero
1477 * (indicating that it has expired at least once since it was last examined)
1478 * or the timer is stopped. If the timer status is already non-zero,
1479 * or the timer is already stopped, the caller continues without waiting.
1480 *
1481 * Calling this routine resets the timer's status to zero.
1482 *
1483 * This routine must not be used by interrupt handlers, since they are not
1484 * allowed to block.
1485 *
1486 * @param timer Address of timer.
1487 *
1488 * @return Timer status.
1489 */
Andrew Boiea354d492017-09-29 16:22:28 -07001490__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001491
Andy Ross52e444b2018-09-28 09:06:37 -07001492extern s32_t z_timeout_remaining(struct _timeout *timeout);
1493
Allan Stephens45bfa372016-10-12 12:39:42 -05001494/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001495 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001496 *
1497 * This routine computes the (approximate) time remaining before a running
1498 * timer next expires. If the timer is not running, it returns zero.
1499 *
1500 * @param timer Address of timer.
1501 *
1502 * @return Remaining time (in milliseconds).
1503 */
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001504__syscall u32_t k_timer_remaining_get(struct k_timer *timer);
Andrew Boiea354d492017-09-29 16:22:28 -07001505
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001506static inline u32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001507{
Flavio Ceolinf1e53032018-12-04 16:03:13 -08001508 return (u32_t)__ticks_to_ms(z_timeout_remaining(&timer->timeout));
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001509}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001510
Allan Stephensc98da842016-11-11 15:45:03 -05001511/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001512 * @brief Associate user-specific data with a timer.
1513 *
1514 * This routine records the @a user_data with the @a timer, to be retrieved
1515 * later.
1516 *
1517 * It can be used e.g. in a timer handler shared across multiple subsystems to
1518 * retrieve data specific to the subsystem this timer is associated with.
1519 *
1520 * @param timer Address of timer.
1521 * @param user_data User data to associate with the timer.
1522 *
1523 * @return N/A
1524 */
Andrew Boiea354d492017-09-29 16:22:28 -07001525__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1526
Anas Nashif954d5502018-02-25 08:37:28 -06001527/**
1528 * @internal
1529 */
Andrew Boiea354d492017-09-29 16:22:28 -07001530static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1531 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001532{
1533 timer->user_data = user_data;
1534}
1535
1536/**
1537 * @brief Retrieve the user-specific data from a timer.
1538 *
1539 * @param timer Address of timer.
1540 *
1541 * @return The user data.
1542 */
Andrew Boiea354d492017-09-29 16:22:28 -07001543__syscall void *k_timer_user_data_get(struct k_timer *timer);
1544
1545static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001546{
1547 return timer->user_data;
1548}
1549
Anas Nashif166f5192018-02-25 08:02:36 -06001550/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001551
Allan Stephensc98da842016-11-11 15:45:03 -05001552/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001553 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001554 * @{
1555 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001556
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001557/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001558 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001559 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001560 * This routine returns the elapsed time since the system booted,
1561 * in milliseconds.
1562 *
1563 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001564 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001565__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001566
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001567/**
1568 * @brief Enable clock always on in tickless kernel
1569 *
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001570 * This routine enables keeping the clock running (that is, it always
1571 * keeps an active timer interrupt scheduled) when there are no timer
1572 * events programmed in tickless kernel scheduling. This is necessary
1573 * if the clock is used to track passage of time (e.g. via
1574 * k_uptime_get_32()), otherwise the internal hardware counter may
1575 * roll over between interrupts.
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001576 *
1577 * @retval prev_status Previous status of always on flag
1578 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001579int k_enable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001580
1581/**
1582 * @brief Disable clock always on in tickless kernel
1583 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001584 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001585 * there are no timer events programmed in tickless kernel
1586 * scheduling. To save power, this routine should be called
1587 * immediately when clock is not used to track time.
1588 */
Andy Rossb8ffd9a2018-09-19 13:14:32 -07001589void k_disable_sys_clock_always_on(void);
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001590
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001591/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001592 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001593 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001594 * This routine returns the lower 32-bits of the elapsed time since the system
1595 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001597 * This routine can be more efficient than k_uptime_get(), as it reduces the
1598 * need for interrupt locking and 64-bit math. However, the 32-bit result
1599 * cannot hold a system uptime time larger than approximately 50 days, so the
1600 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001601 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001602 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001603 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001604__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001605
1606/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001607 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001608 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001609 * This routine computes the elapsed time between the current system uptime
1610 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001612 * @param reftime Pointer to a reference time, which is updated to the current
1613 * uptime upon return.
1614 *
1615 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001616 */
Andy Ross987c0e52018-09-27 16:50:00 -07001617static inline s64_t k_uptime_delta(s64_t *reftime)
1618{
1619 s64_t uptime, delta;
1620
1621 uptime = k_uptime_get();
1622 delta = uptime - *reftime;
1623 *reftime = uptime;
1624
1625 return delta;
1626}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001627
1628/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001629 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001631 * This routine computes the elapsed time between the current system uptime
1632 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001633 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001634 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1635 * need for interrupt locking and 64-bit math. However, the 32-bit result
1636 * cannot hold an elapsed time larger than approximately 50 days, so the
1637 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001638 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001639 * @param reftime Pointer to a reference time, which is updated to the current
1640 * uptime upon return.
1641 *
1642 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001643 */
Andy Ross987c0e52018-09-27 16:50:00 -07001644static inline u32_t k_uptime_delta_32(s64_t *reftime)
1645{
1646 return (u32_t)k_uptime_delta(reftime);
1647}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001648
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001649/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001650 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001651 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001652 * This routine returns the current time, as measured by the system's hardware
1653 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001654 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001655 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001656 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001657#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001658
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001659/**
Anas Nashif166f5192018-02-25 08:02:36 -06001660 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001661 */
1662
Allan Stephensc98da842016-11-11 15:45:03 -05001663/**
1664 * @cond INTERNAL_HIDDEN
1665 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001666
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001667struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001668 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001669 union {
1670 _wait_q_t wait_q;
1671
1672 _POLL_EVENT;
1673 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001674
Flavio Ceolind1ed3362018-12-07 11:39:13 -08001675 _OBJECT_TRACING_NEXT_PTR(k_queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001676};
1677
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001678#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001679 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001680 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001681 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001682 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001683 _OBJECT_TRACING_INIT \
1684 }
1685
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001686#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1687
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001688extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1689
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001690/**
1691 * INTERNAL_HIDDEN @endcond
1692 */
1693
1694/**
1695 * @defgroup queue_apis Queue APIs
1696 * @ingroup kernel_apis
1697 * @{
1698 */
1699
1700/**
1701 * @brief Initialize a queue.
1702 *
1703 * This routine initializes a queue object, prior to its first use.
1704 *
1705 * @param queue Address of the queue.
1706 *
1707 * @return N/A
1708 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001709__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001710
1711/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001712 * @brief Cancel waiting on a queue.
1713 *
1714 * This routine causes first thread pending on @a queue, if any, to
1715 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001716 * If the queue is being waited on by k_poll(), it will return with
1717 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1718 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001719 *
1720 * @note Can be called by ISRs.
1721 *
1722 * @param queue Address of the queue.
1723 *
1724 * @return N/A
1725 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001726__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001727
1728/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001729 * @brief Append an element to the end of a queue.
1730 *
1731 * This routine appends a data item to @a queue. A queue data item must be
1732 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1733 * reserved for the kernel's use.
1734 *
1735 * @note Can be called by ISRs.
1736 *
1737 * @param queue Address of the queue.
1738 * @param data Address of the data item.
1739 *
1740 * @return N/A
1741 */
1742extern void k_queue_append(struct k_queue *queue, void *data);
1743
1744/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001745 * @brief Append an element to a queue.
1746 *
1747 * This routine appends a data item to @a queue. There is an implicit
1748 * memory allocation from the calling thread's resource pool, which is
1749 * automatically freed when the item is removed from the queue.
1750 *
1751 * @note Can be called by ISRs.
1752 *
1753 * @param queue Address of the queue.
1754 * @param data Address of the data item.
1755 *
1756 * @retval 0 on success
1757 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1758 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301759__syscall s32_t k_queue_alloc_append(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001760
1761/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001762 * @brief Prepend an element to a queue.
1763 *
1764 * This routine prepends a data item to @a queue. A queue data item must be
1765 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1766 * reserved for the kernel's use.
1767 *
1768 * @note Can be called by ISRs.
1769 *
1770 * @param queue Address of the queue.
1771 * @param data Address of the data item.
1772 *
1773 * @return N/A
1774 */
1775extern void k_queue_prepend(struct k_queue *queue, void *data);
1776
1777/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001778 * @brief Prepend an element to a queue.
1779 *
1780 * This routine prepends a data item to @a queue. There is an implicit
1781 * memory allocation from the calling thread's resource pool, which is
1782 * automatically freed when the item is removed from the queue.
1783 *
1784 * @note Can be called by ISRs.
1785 *
1786 * @param queue Address of the queue.
1787 * @param data Address of the data item.
1788 *
1789 * @retval 0 on success
1790 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1791 */
Adithya Baglody2a78b8d2018-10-25 12:09:04 +05301792__syscall s32_t k_queue_alloc_prepend(struct k_queue *queue, void *data);
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001793
1794/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001795 * @brief Inserts an element to a queue.
1796 *
1797 * This routine inserts a data item to @a queue after previous item. A queue
1798 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1799 * item are reserved for the kernel's use.
1800 *
1801 * @note Can be called by ISRs.
1802 *
1803 * @param queue Address of the queue.
1804 * @param prev Address of the previous data item.
1805 * @param data Address of the data item.
1806 *
1807 * @return N/A
1808 */
1809extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1810
1811/**
1812 * @brief Atomically append a list of elements to a queue.
1813 *
1814 * This routine adds a list of data items to @a queue in one operation.
1815 * The data items must be in a singly-linked list, with the first 32 bits
1816 * in each data item pointing to the next data item; the list must be
1817 * NULL-terminated.
1818 *
1819 * @note Can be called by ISRs.
1820 *
1821 * @param queue Address of the queue.
1822 * @param head Pointer to first node in singly-linked list.
1823 * @param tail Pointer to last node in singly-linked list.
1824 *
1825 * @return N/A
1826 */
1827extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1828
1829/**
1830 * @brief Atomically add a list of elements to a queue.
1831 *
1832 * This routine adds a list of data items to @a queue in one operation.
1833 * The data items must be in a singly-linked list implemented using a
1834 * sys_slist_t object. Upon completion, the original list is empty.
1835 *
1836 * @note Can be called by ISRs.
1837 *
1838 * @param queue Address of the queue.
1839 * @param list Pointer to sys_slist_t object.
1840 *
1841 * @return N/A
1842 */
1843extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1844
1845/**
1846 * @brief Get an element from a queue.
1847 *
1848 * This routine removes first data item from @a queue. The first 32 bits of the
1849 * data item are reserved for the kernel's use.
1850 *
1851 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1852 *
1853 * @param queue Address of the queue.
1854 * @param timeout Waiting period to obtain a data item (in milliseconds),
1855 * or one of the special values K_NO_WAIT and K_FOREVER.
1856 *
1857 * @return Address of the data item if successful; NULL if returned
1858 * without waiting, or waiting period timed out.
1859 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001860__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001861
1862/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001863 * @brief Remove an element from a queue.
1864 *
1865 * This routine removes data item from @a queue. The first 32 bits of the
1866 * data item are reserved for the kernel's use. Removing elements from k_queue
1867 * rely on sys_slist_find_and_remove which is not a constant time operation.
1868 *
1869 * @note Can be called by ISRs
1870 *
1871 * @param queue Address of the queue.
1872 * @param data Address of the data item.
1873 *
1874 * @return true if data item was removed
1875 */
1876static inline bool k_queue_remove(struct k_queue *queue, void *data)
1877{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001878 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001879}
1880
1881/**
Dhananjay Gundapu Jayakrishnan24bfa402018-08-22 12:33:00 +02001882 * @brief Append an element to a queue only if it's not present already.
1883 *
1884 * This routine appends data item to @a queue. The first 32 bits of the
1885 * data item are reserved for the kernel's use. Appending elements to k_queue
1886 * relies on sys_slist_is_node_in_list which is not a constant time operation.
1887 *
1888 * @note Can be called by ISRs
1889 *
1890 * @param queue Address of the queue.
1891 * @param data Address of the data item.
1892 *
1893 * @return true if data item was added, false if not
1894 */
1895static inline bool k_queue_unique_append(struct k_queue *queue, void *data)
1896{
1897 sys_sfnode_t *test;
1898
1899 SYS_SFLIST_FOR_EACH_NODE(&queue->data_q, test) {
1900 if (test == (sys_sfnode_t *) data) {
1901 return false;
1902 }
1903 }
1904
1905 k_queue_append(queue, data);
1906 return true;
1907}
1908
1909/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001910 * @brief Query a queue to see if it has data available.
1911 *
1912 * Note that the data might be already gone by the time this function returns
1913 * if other threads are also trying to read from the queue.
1914 *
1915 * @note Can be called by ISRs.
1916 *
1917 * @param queue Address of the queue.
1918 *
1919 * @return Non-zero if the queue is empty.
1920 * @return 0 if data is available.
1921 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001922__syscall int k_queue_is_empty(struct k_queue *queue);
1923
1924static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001925{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001926 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001927}
1928
1929/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001930 * @brief Peek element at the head of queue.
1931 *
1932 * Return element from the head of queue without removing it.
1933 *
1934 * @param queue Address of the queue.
1935 *
1936 * @return Head element, or NULL if queue is empty.
1937 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001938__syscall void *k_queue_peek_head(struct k_queue *queue);
1939
1940static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001941{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001942 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001943}
1944
1945/**
1946 * @brief Peek element at the tail of queue.
1947 *
1948 * Return element from the tail of queue without removing it.
1949 *
1950 * @param queue Address of the queue.
1951 *
1952 * @return Tail element, or NULL if queue is empty.
1953 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001954__syscall void *k_queue_peek_tail(struct k_queue *queue);
1955
1956static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001957{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001958 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001959}
1960
1961/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001962 * @brief Statically define and initialize a queue.
1963 *
1964 * The queue can be accessed outside the module where it is defined using:
1965 *
1966 * @code extern struct k_queue <name>; @endcode
1967 *
1968 * @param name Name of the queue.
1969 */
1970#define K_QUEUE_DEFINE(name) \
1971 struct k_queue name \
1972 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001973 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001974
Anas Nashif166f5192018-02-25 08:02:36 -06001975/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001976
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001977struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001978 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001979};
1980
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04001981/**
1982 * @cond INTERNAL_HIDDEN
1983 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001984#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001985 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001986 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001987 }
1988
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001989#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1990
Allan Stephensc98da842016-11-11 15:45:03 -05001991/**
1992 * INTERNAL_HIDDEN @endcond
1993 */
1994
1995/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001996 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001997 * @ingroup kernel_apis
1998 * @{
1999 */
2000
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002001/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002002 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002003 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002004 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002005 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002006 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002007 *
2008 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002009 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002010 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002011#define k_fifo_init(fifo) \
2012 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002013
2014/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002015 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002016 *
2017 * This routine causes first thread pending on @a fifo, if any, to
2018 * return from k_fifo_get() call with NULL value (as if timeout
2019 * expired).
2020 *
2021 * @note Can be called by ISRs.
2022 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002023 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002024 *
2025 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002026 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002027 */
2028#define k_fifo_cancel_wait(fifo) \
2029 k_queue_cancel_wait((struct k_queue *) fifo)
2030
2031/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002032 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002033 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002034 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002035 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2036 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002038 * @note Can be called by ISRs.
2039 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002040 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002041 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002042 *
2043 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002044 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002045 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002046#define k_fifo_put(fifo, data) \
2047 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002048
2049/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002050 * @brief Add an element to a FIFO queue.
2051 *
2052 * This routine adds a data item to @a fifo. There is an implicit
2053 * memory allocation from the calling thread's resource pool, which is
2054 * automatically freed when the item is removed.
2055 *
2056 * @note Can be called by ISRs.
2057 *
2058 * @param fifo Address of the FIFO.
2059 * @param data Address of the data item.
2060 *
2061 * @retval 0 on success
2062 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002063 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002064 */
2065#define k_fifo_alloc_put(fifo, data) \
2066 k_queue_alloc_append((struct k_queue *) fifo, data)
2067
2068/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002069 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002070 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002071 * This routine adds a list of data items to @a fifo in one operation.
2072 * The data items must be in a singly-linked list, with the first 32 bits
2073 * each data item pointing to the next data item; the list must be
2074 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002076 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002077 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002078 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002079 * @param head Pointer to first node in singly-linked list.
2080 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002081 *
2082 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002083 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002084 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002085#define k_fifo_put_list(fifo, head, tail) \
2086 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002087
2088/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002089 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002090 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002091 * This routine adds a list of data items to @a fifo in one operation.
2092 * The data items must be in a singly-linked list implemented using a
2093 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002094 * and must be re-initialized via sys_slist_init().
2095 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002096 * @note Can be called by ISRs.
2097 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002098 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002099 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002100 *
2101 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002102 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002103 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002104#define k_fifo_put_slist(fifo, list) \
2105 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002106
2107/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002108 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002110 * This routine removes a data item from @a fifo in a "first in, first out"
2111 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002112 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002113 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2114 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002115 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002116 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002117 * or one of the special values K_NO_WAIT and K_FOREVER.
2118 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002119 * @return Address of the data item if successful; NULL if returned
2120 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002121 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002122 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002123#define k_fifo_get(fifo, timeout) \
2124 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002125
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002126/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002127 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002128 *
2129 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002130 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002131 *
2132 * @note Can be called by ISRs.
2133 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002134 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002135 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002136 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002137 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002138 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002139 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002140#define k_fifo_is_empty(fifo) \
2141 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002142
2143/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002144 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002145 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002146 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302147 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002148 * on each iteration of processing, a head container will be peeked,
2149 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002150 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002151 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002152 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002153 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002154 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002155 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002156 */
2157#define k_fifo_peek_head(fifo) \
2158 k_queue_peek_head((struct k_queue *) fifo)
2159
2160/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002161 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002162 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002163 * Return element from the tail of FIFO queue (without removing it). A usecase
2164 * for this is if elements of the FIFO queue are themselves containers. Then
2165 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002166 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002167 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002168 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002169 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002170 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002171 */
2172#define k_fifo_peek_tail(fifo) \
2173 k_queue_peek_tail((struct k_queue *) fifo)
2174
2175/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002176 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002177 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002178 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002179 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002180 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002181 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002182 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002183 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002184 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002185#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002186 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002187 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002188 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002189
Anas Nashif166f5192018-02-25 08:02:36 -06002190/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002191
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002192struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002193 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002194};
2195
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002196/**
2197 * @cond INTERNAL_HIDDEN
2198 */
2199
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002200#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002201 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002202 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002203 }
2204
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002205#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2206
Allan Stephensc98da842016-11-11 15:45:03 -05002207/**
2208 * INTERNAL_HIDDEN @endcond
2209 */
2210
2211/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002212 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002213 * @ingroup kernel_apis
2214 * @{
2215 */
2216
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002217/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002218 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002219 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002220 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002221 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002222 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002223 *
2224 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002225 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002226 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002227#define k_lifo_init(lifo) \
2228 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002229
2230/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002231 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002232 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002233 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002234 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2235 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002236 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002237 * @note Can be called by ISRs.
2238 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002239 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002240 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002241 *
2242 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002243 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002244 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002245#define k_lifo_put(lifo, data) \
2246 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002247
2248/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002249 * @brief Add an element to a LIFO queue.
2250 *
2251 * This routine adds a data item to @a lifo. There is an implicit
2252 * memory allocation from the calling thread's resource pool, which is
2253 * automatically freed when the item is removed.
2254 *
2255 * @note Can be called by ISRs.
2256 *
2257 * @param lifo Address of the LIFO.
2258 * @param data Address of the data item.
2259 *
2260 * @retval 0 on success
2261 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002262 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002263 */
2264#define k_lifo_alloc_put(lifo, data) \
2265 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2266
2267/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002268 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002269 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002270 * This routine removes a data item from @a lifo in a "last in, first out"
2271 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002273 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2274 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002275 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002276 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002277 * or one of the special values K_NO_WAIT and K_FOREVER.
2278 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002279 * @return Address of the data item if successful; NULL if returned
2280 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002281 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002282 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002283#define k_lifo_get(lifo, timeout) \
2284 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002285
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002286/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002287 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002288 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002289 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002290 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002291 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002293 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002294 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002295 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002296#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002297 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002298 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002299 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002300
Anas Nashif166f5192018-02-25 08:02:36 -06002301/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002302
2303/**
2304 * @cond INTERNAL_HIDDEN
2305 */
Adithya Baglody28080d32018-10-15 11:48:51 +05302306#define K_STACK_FLAG_ALLOC ((u8_t)1) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002307
2308struct k_stack {
2309 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002310 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002311
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002312 _OBJECT_TRACING_NEXT_PTR(k_stack)
Andrew Boief3bee952018-05-02 17:44:39 -07002313 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002314};
2315
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002316#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002317 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002318 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002319 .base = stack_buffer, \
2320 .next = stack_buffer, \
2321 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002322 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002323 }
2324
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002325#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2326
Allan Stephensc98da842016-11-11 15:45:03 -05002327/**
2328 * INTERNAL_HIDDEN @endcond
2329 */
2330
2331/**
2332 * @defgroup stack_apis Stack APIs
2333 * @ingroup kernel_apis
2334 * @{
2335 */
2336
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002337/**
2338 * @brief Initialize a stack.
2339 *
2340 * This routine initializes a stack object, prior to its first use.
2341 *
2342 * @param stack Address of the stack.
2343 * @param buffer Address of array used to hold stacked values.
2344 * @param num_entries Maximum number of values that can be stacked.
2345 *
2346 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002347 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002348 */
Andrew Boief3bee952018-05-02 17:44:39 -07002349void k_stack_init(struct k_stack *stack,
Adithya Baglody28080d32018-10-15 11:48:51 +05302350 u32_t *buffer, u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002351
2352
2353/**
2354 * @brief Initialize a stack.
2355 *
2356 * This routine initializes a stack object, prior to its first use. Internal
2357 * buffers will be allocated from the calling thread's resource pool.
2358 * This memory will be released if k_stack_cleanup() is called, or
2359 * userspace is enabled and the stack object loses all references to it.
2360 *
2361 * @param stack Address of the stack.
2362 * @param num_entries Maximum number of values that can be stacked.
2363 *
2364 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002365 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002366 */
2367
Adithya Baglody28080d32018-10-15 11:48:51 +05302368__syscall s32_t k_stack_alloc_init(struct k_stack *stack,
2369 u32_t num_entries);
Andrew Boief3bee952018-05-02 17:44:39 -07002370
2371/**
2372 * @brief Release a stack's allocated buffer
2373 *
2374 * If a stack object was given a dynamically allocated buffer via
2375 * k_stack_alloc_init(), this will free it. This function does nothing
2376 * if the buffer wasn't dynamically allocated.
2377 *
2378 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002379 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002380 */
2381void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002382
2383/**
2384 * @brief Push an element onto a stack.
2385 *
2386 * This routine adds a 32-bit value @a data to @a stack.
2387 *
2388 * @note Can be called by ISRs.
2389 *
2390 * @param stack Address of the stack.
2391 * @param data Value to push onto the stack.
2392 *
2393 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002394 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002395 */
Andrew Boiee8734462017-09-29 16:42:07 -07002396__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002397
2398/**
2399 * @brief Pop an element from a stack.
2400 *
2401 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2402 * manner and stores the value in @a data.
2403 *
2404 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2405 *
2406 * @param stack Address of the stack.
2407 * @param data Address of area to hold the value popped from the stack.
2408 * @param timeout Waiting period to obtain a value (in milliseconds),
2409 * or one of the special values K_NO_WAIT and K_FOREVER.
2410 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002411 * @retval 0 Element popped from stack.
2412 * @retval -EBUSY Returned without waiting.
2413 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002414 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002415 */
Andrew Boiee8734462017-09-29 16:42:07 -07002416__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002417
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002418/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002419 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002420 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002421 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002422 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002423 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002424 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002425 * @param name Name of the stack.
2426 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002427 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002428 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002429#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002430 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002431 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002432 struct k_stack name \
2433 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002434 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002435 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002436
Anas Nashif166f5192018-02-25 08:02:36 -06002437/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002438
Allan Stephens6bba9b02016-11-16 14:56:54 -05002439struct k_work;
2440
Allan Stephensc98da842016-11-11 15:45:03 -05002441/**
Anas Nashif29f37f02019-01-21 14:30:35 -05002442 * @addtogroup thread_apis
Allan Stephensc98da842016-11-11 15:45:03 -05002443 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002444 */
2445
Allan Stephens6bba9b02016-11-16 14:56:54 -05002446/**
2447 * @typedef k_work_handler_t
2448 * @brief Work item handler function type.
2449 *
2450 * A work item's handler function is executed by a workqueue's thread
2451 * when the work item is processed by the workqueue.
2452 *
2453 * @param work Address of the work item.
2454 *
2455 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002456 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002457 */
2458typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002459
2460/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002461 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002462 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002463
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002464struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002465 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002466 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002467};
2468
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002469enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002470 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002471};
2472
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002473struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002474 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002475 k_work_handler_t handler;
2476 atomic_t flags[1];
2477};
2478
Allan Stephens6bba9b02016-11-16 14:56:54 -05002479struct k_delayed_work {
2480 struct k_work work;
2481 struct _timeout timeout;
2482 struct k_work_q *work_q;
2483};
2484
2485extern struct k_work_q k_sys_work_q;
2486
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002487/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002488 * INTERNAL_HIDDEN @endcond
2489 */
2490
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002491#define _K_WORK_INITIALIZER(work_handler) \
2492 { \
2493 ._reserved = NULL, \
2494 .handler = work_handler, \
2495 .flags = { 0 } \
2496 }
2497
2498#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2499
Allan Stephens6bba9b02016-11-16 14:56:54 -05002500/**
2501 * @brief Initialize a statically-defined work item.
2502 *
2503 * This macro can be used to initialize a statically-defined workqueue work
2504 * item, prior to its first use. For example,
2505 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002506 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002507 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002508 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002509 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002510 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002511 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002512#define K_WORK_DEFINE(work, work_handler) \
Andrew Boiec2e01df2018-11-12 15:16:54 -08002513 struct k_work work = _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002514
2515/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002516 * @brief Initialize a work item.
2517 *
2518 * This routine initializes a workqueue work item, prior to its first use.
2519 *
2520 * @param work Address of work item.
2521 * @param handler Function to invoke each time work item is processed.
2522 *
2523 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002524 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002525 */
2526static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2527{
Leandro Pereira0e23ad82018-05-29 10:42:17 -07002528 *work = (struct k_work)_K_WORK_INITIALIZER(handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002529}
2530
2531/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002532 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002533 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002534 * This routine submits work item @a work to be processed by workqueue
2535 * @a work_q. If the work item is already pending in the workqueue's queue
2536 * as a result of an earlier submission, this routine has no effect on the
2537 * work item. If the work item has already been processed, or is currently
2538 * being processed, its work is considered complete and the work item can be
2539 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002540 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002541 * @warning
2542 * A submitted work item must not be modified until it has been processed
2543 * by the workqueue.
2544 *
2545 * @note Can be called by ISRs.
2546 *
2547 * @param work_q Address of workqueue.
2548 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002549 *
2550 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002551 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002552 */
2553static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2554 struct k_work *work)
2555{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002556 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002557 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002558 }
2559}
2560
2561/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002562 * @brief Submit a work item to a user mode workqueue
2563 *
David B. Kinder06d78352018-12-17 14:32:40 -08002564 * Submits a work item to a workqueue that runs in user mode. A temporary
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002565 * memory allocation is made from the caller's resource pool which is freed
2566 * once the worker thread consumes the k_work item. The workqueue
2567 * thread must have memory access to the k_work item being submitted. The caller
2568 * must have permission granted on the work_q parameter's queue object.
2569 *
2570 * Otherwise this works the same as k_work_submit_to_queue().
2571 *
2572 * @note Can be called by ISRs.
2573 *
2574 * @param work_q Address of workqueue.
2575 * @param work Address of work item.
2576 *
2577 * @retval -EBUSY if the work item was already in some workqueue
2578 * @retval -ENOMEM if no memory for thread resource pool allocation
2579 * @retval 0 Success
2580 * @req K-WORK-001
2581 */
2582static inline int k_work_submit_to_user_queue(struct k_work_q *work_q,
2583 struct k_work *work)
2584{
2585 int ret = -EBUSY;
2586
2587 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
2588 ret = k_queue_alloc_append(&work_q->queue, work);
2589
2590 /* Couldn't insert into the queue. Clear the pending bit
2591 * so the work item can be submitted again
2592 */
Flavio Ceolin76b35182018-12-16 12:48:29 -08002593 if (ret != 0) {
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002594 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
2595 }
2596 }
2597
2598 return ret;
2599}
2600
2601/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002602 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002603 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002604 * This routine indicates if work item @a work is pending in a workqueue's
2605 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002606 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002607 * @note Can be called by ISRs.
2608 *
2609 * @param work Address of work item.
2610 *
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002611 * @return true if work item is pending, or false if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002612 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002613 */
Flavio Ceolin82ef4f82018-11-21 18:12:34 -08002614static inline bool k_work_pending(struct k_work *work)
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002615{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002616 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002617}
2618
2619/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002620 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002621 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002622 * This routine starts workqueue @a work_q. The workqueue spawns its work
2623 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002624 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002625 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002626 * @param stack Pointer to work queue thread's stack space, as defined by
2627 * K_THREAD_STACK_DEFINE()
2628 * @param stack_size Size of the work queue thread's stack (in bytes), which
2629 * should either be the same constant passed to
2630 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002631 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002632 *
2633 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002634 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002635 */
Andrew Boie507852a2017-07-25 18:47:07 -07002636extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002637 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002638 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002639
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002640/**
Andrew Boie2b1d54e2018-11-12 14:25:19 -08002641 * @brief Start a workqueue in user mode
2642 *
2643 * This works identically to k_work_q_start() except it is callable from user
2644 * mode, and the worker thread created will run in user mode.
2645 * The caller must have permissions granted on both the work_q parameter's
2646 * thread and queue objects, and the same restrictions on priority apply as
2647 * k_thread_create().
2648 *
2649 * @param work_q Address of workqueue.
2650 * @param stack Pointer to work queue thread's stack space, as defined by
2651 * K_THREAD_STACK_DEFINE()
2652 * @param stack_size Size of the work queue thread's stack (in bytes), which
2653 * should either be the same constant passed to
2654 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
2655 * @param prio Priority of the work queue's thread.
2656 *
2657 * @return N/A
2658 * @req K-WORK-001
2659 */
2660extern void k_work_q_user_start(struct k_work_q *work_q,
2661 k_thread_stack_t *stack,
2662 size_t stack_size, int prio);
2663
2664/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002665 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002666 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002667 * This routine initializes a workqueue delayed work item, prior to
2668 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002669 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002670 * @param work Address of delayed work item.
2671 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002672 *
2673 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002674 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002675 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002676extern void k_delayed_work_init(struct k_delayed_work *work,
2677 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002678
2679/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002680 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002681 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002682 * This routine schedules work item @a work to be processed by workqueue
2683 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002684 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002685 * Only when the countdown completes is the work item actually submitted to
2686 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002687 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002688 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002689 * counting down cancels the existing submission and restarts the
2690 * countdown using the new delay. Note that this behavior is
2691 * inherently subject to race conditions with the pre-existing
2692 * timeouts and work queue, so care must be taken to synchronize such
2693 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002694 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002695 * @warning
2696 * A delayed work item must not be modified until it has been processed
2697 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002698 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002699 * @note Can be called by ISRs.
2700 *
2701 * @param work_q Address of workqueue.
2702 * @param work Address of delayed work item.
2703 * @param delay Delay before submitting the work item (in milliseconds).
2704 *
2705 * @retval 0 Work item countdown started.
2706 * @retval -EINPROGRESS Work item is already pending.
2707 * @retval -EINVAL Work item is being processed or has completed its work.
2708 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002709 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002710 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002711extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2712 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002713 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002714
2715/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002716 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002717 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002718 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002719 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002720 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002721 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002722 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002723 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002724 * @param work Address of delayed work item.
2725 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002726 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002727 * @retval -EINPROGRESS Work item is already pending.
2728 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002729 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002730 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002731extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002732
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002733/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002734 * @brief Submit a work item to the system workqueue.
2735 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002736 * This routine submits work item @a work to be processed by the system
2737 * workqueue. If the work item is already pending in the workqueue's queue
2738 * as a result of an earlier submission, this routine has no effect on the
2739 * work item. If the work item has already been processed, or is currently
2740 * being processed, its work is considered complete and the work item can be
2741 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002742 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002743 * @warning
2744 * Work items submitted to the system workqueue should avoid using handlers
2745 * that block or yield since this may prevent the system workqueue from
2746 * processing other work items in a timely manner.
2747 *
2748 * @note Can be called by ISRs.
2749 *
2750 * @param work Address of work item.
2751 *
2752 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002753 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002754 */
2755static inline void k_work_submit(struct k_work *work)
2756{
2757 k_work_submit_to_queue(&k_sys_work_q, work);
2758}
2759
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002760/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002761 * @brief Submit a delayed work item to the system workqueue.
2762 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002763 * This routine schedules work item @a work to be processed by the system
2764 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002765 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002766 * Only when the countdown completes is the work item actually submitted to
2767 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002768 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002769 * Submitting a previously submitted delayed work item that is still
2770 * counting down cancels the existing submission and restarts the countdown
2771 * using the new delay. If the work item is currently pending on the
2772 * workqueue's queue because the countdown has completed it is too late to
2773 * resubmit the item, and resubmission fails without impacting the work item.
2774 * If the work item has already been processed, or is currently being processed,
2775 * its work is considered complete and the work item can be resubmitted.
2776 *
2777 * @warning
2778 * Work items submitted to the system workqueue should avoid using handlers
2779 * that block or yield since this may prevent the system workqueue from
2780 * processing other work items in a timely manner.
2781 *
2782 * @note Can be called by ISRs.
2783 *
2784 * @param work Address of delayed work item.
2785 * @param delay Delay before submitting the work item (in milliseconds).
2786 *
2787 * @retval 0 Work item countdown started.
2788 * @retval -EINPROGRESS Work item is already pending.
2789 * @retval -EINVAL Work item is being processed or has completed its work.
2790 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002791 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002792 */
2793static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002794 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002795{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002796 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002797}
2798
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002799/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002800 * @brief Get time remaining before a delayed work gets scheduled.
2801 *
2802 * This routine computes the (approximate) time remaining before a
2803 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002804 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002805 *
2806 * @param work Delayed work item.
2807 *
2808 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002809 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002810 */
Kumar Galacc334c72017-04-21 10:55:34 -05002811static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002812{
Andy Ross52e444b2018-09-28 09:06:37 -07002813 return __ticks_to_ms(z_timeout_remaining(&work->timeout));
Johan Hedbergc8201b22016-12-09 10:42:22 +02002814}
2815
Anas Nashif166f5192018-02-25 08:02:36 -06002816/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002817/**
Anas Nashifce78d162018-05-24 12:43:11 -05002818 * @defgroup mutex_apis Mutex APIs
2819 * @ingroup kernel_apis
2820 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002821 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002822
Anas Nashifce78d162018-05-24 12:43:11 -05002823/**
2824 * Mutex Structure
2825 * @ingroup mutex_apis
2826 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002827struct k_mutex {
2828 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002829 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002830 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002831 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002832 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002833
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002834 _OBJECT_TRACING_NEXT_PTR(k_mutex)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002835};
2836
Anas Nashifce78d162018-05-24 12:43:11 -05002837/**
2838 * @cond INTERNAL_HIDDEN
2839 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002840#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002841 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002842 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002843 .owner = NULL, \
2844 .lock_count = 0, \
2845 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002846 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002847 }
2848
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002849#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2850
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002851/**
Allan Stephensc98da842016-11-11 15:45:03 -05002852 * INTERNAL_HIDDEN @endcond
2853 */
2854
2855/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002856 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002858 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002859 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002860 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002862 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002863 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002864 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002865#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002866 struct k_mutex name \
2867 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002868 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002869
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002870/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002871 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002872 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002873 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002875 * Upon completion, the mutex is available and does not have an owner.
2876 *
2877 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002878 *
2879 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002880 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002881 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002882__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002883
2884/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002885 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002887 * This routine locks @a mutex. If the mutex is locked by another thread,
2888 * the calling thread waits until the mutex becomes available or until
2889 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002891 * A thread is permitted to lock a mutex it has already locked. The operation
2892 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002893 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002894 * @param mutex Address of the mutex.
2895 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002896 * or one of the special values K_NO_WAIT and K_FOREVER.
2897 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002898 * @retval 0 Mutex locked.
2899 * @retval -EBUSY Returned without waiting.
2900 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002901 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002902 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002903__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002904
2905/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002906 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002907 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002908 * This routine unlocks @a mutex. The mutex must already be locked by the
2909 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002910 *
2911 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002912 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002913 * thread.
2914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002915 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002916 *
2917 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002918 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002919 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002920__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002921
Allan Stephensc98da842016-11-11 15:45:03 -05002922/**
Anas Nashif166f5192018-02-25 08:02:36 -06002923 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002924 */
2925
2926/**
2927 * @cond INTERNAL_HIDDEN
2928 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002929
2930struct k_sem {
2931 _wait_q_t wait_q;
Adithya Baglody4b066212018-10-16 11:59:12 +05302932 u32_t count;
2933 u32_t limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002934 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002935
Flavio Ceolind1ed3362018-12-07 11:39:13 -08002936 _OBJECT_TRACING_NEXT_PTR(k_sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002937};
2938
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002939#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002940 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002941 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002942 .count = initial_count, \
2943 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002944 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002945 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002946 }
2947
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002948#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2949
Allan Stephensc98da842016-11-11 15:45:03 -05002950/**
2951 * INTERNAL_HIDDEN @endcond
2952 */
2953
2954/**
2955 * @defgroup semaphore_apis Semaphore APIs
2956 * @ingroup kernel_apis
2957 * @{
2958 */
2959
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002960/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002961 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002963 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002964 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002965 * @param sem Address of the semaphore.
2966 * @param initial_count Initial semaphore count.
2967 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002968 *
2969 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002970 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002971 */
Andrew Boie99280232017-09-29 14:17:47 -07002972__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2973 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002974
2975/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002976 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002977 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002978 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002979 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002980 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2981 *
2982 * @param sem Address of the semaphore.
2983 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002984 * or one of the special values K_NO_WAIT and K_FOREVER.
2985 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002986 * @note When porting code from the nanokernel legacy API to the new API, be
2987 * careful with the return value of this function. The return value is the
2988 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2989 * non-zero means failure, while the nano_sem_take family returns 1 for success
2990 * and 0 for failure.
2991 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002992 * @retval 0 Semaphore taken.
2993 * @retval -EBUSY Returned without waiting.
2994 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002995 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002996 */
Andrew Boie99280232017-09-29 14:17:47 -07002997__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002998
2999/**
3000 * @brief Give a semaphore.
3001 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003002 * This routine gives @a sem, unless the semaphore is already at its maximum
3003 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003005 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003007 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003008 *
3009 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003010 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003011 */
Andrew Boie99280232017-09-29 14:17:47 -07003012__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003013
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003014/**
3015 * @brief Reset a semaphore's count to zero.
3016 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003017 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003019 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003020 *
3021 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003022 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003023 */
Andrew Boie990bf162017-10-03 12:36:49 -07003024__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003025
Anas Nashif954d5502018-02-25 08:37:28 -06003026/**
3027 * @internal
3028 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003029static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003030{
3031 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003032}
3033
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003034/**
3035 * @brief Get a semaphore's count.
3036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003037 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003038 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003039 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003040 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003041 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003042 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003043 */
Andrew Boie990bf162017-10-03 12:36:49 -07003044__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003045
Anas Nashif954d5502018-02-25 08:37:28 -06003046/**
3047 * @internal
3048 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003049static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003050{
3051 return sem->count;
3052}
3053
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003054/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003055 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003056 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003057 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003058 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003059 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003060 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003061 * @param name Name of the semaphore.
3062 * @param initial_count Initial semaphore count.
3063 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003064 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003065 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003066#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003067 struct k_sem name \
3068 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003069 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303070 BUILD_ASSERT(((count_limit) != 0) && \
3071 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003072
Anas Nashif166f5192018-02-25 08:02:36 -06003073/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003074
3075/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003076 * @defgroup msgq_apis Message Queue APIs
3077 * @ingroup kernel_apis
3078 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003079 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003080
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003081/**
3082 * @brief Message Queue Structure
3083 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003084struct k_msgq {
3085 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003086 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003087 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003088 char *buffer_start;
3089 char *buffer_end;
3090 char *read_ptr;
3091 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003092 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003093
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003094 _OBJECT_TRACING_NEXT_PTR(k_msgq)
Andrew Boie0fe789f2018-04-12 18:35:56 -07003095 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003096};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003097/**
3098 * @cond INTERNAL_HIDDEN
3099 */
3100
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003101
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003102#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003103 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003104 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003105 .max_msgs = q_max_msgs, \
3106 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003107 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003108 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003109 .read_ptr = q_buffer, \
3110 .write_ptr = q_buffer, \
3111 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003112 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003113 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003114#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003115/**
3116 * INTERNAL_HIDDEN @endcond
3117 */
3118
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003119
Andrew Boie0fe789f2018-04-12 18:35:56 -07003120#define K_MSGQ_FLAG_ALLOC BIT(0)
3121
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003122/**
3123 * @brief Message Queue Attributes
3124 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303125struct k_msgq_attrs {
3126 size_t msg_size;
3127 u32_t max_msgs;
3128 u32_t used_msgs;
3129};
3130
Allan Stephensc98da842016-11-11 15:45:03 -05003131
3132/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003133 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003135 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3136 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003137 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3138 * message is similarly aligned to this boundary, @a q_msg_size must also be
3139 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003140 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003141 * The message queue can be accessed outside the module where it is defined
3142 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003143 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003144 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003145 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003146 * @param q_name Name of the message queue.
3147 * @param q_msg_size Message size (in bytes).
3148 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003149 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003150 *
3151 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003152 */
3153#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003154 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003155 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003156 struct k_msgq q_name \
3157 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003158 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003159 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003160
Peter Mitsisd7a37502016-10-13 11:37:40 -04003161/**
3162 * @brief Initialize a message queue.
3163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003164 * This routine initializes a message queue object, prior to its first use.
3165 *
Allan Stephensda827222016-11-09 14:23:58 -06003166 * The message queue's ring buffer must contain space for @a max_msgs messages,
3167 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3168 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3169 * that each message is similarly aligned to this boundary, @a q_msg_size
3170 * must also be a multiple of N.
3171 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003172 * @param q Address of the message queue.
3173 * @param buffer Pointer to ring buffer that holds queued messages.
3174 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003175 * @param max_msgs Maximum number of messages that can be queued.
3176 *
3177 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003178 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003179 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003180void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3181 u32_t max_msgs);
3182
3183/**
3184 * @brief Initialize a message queue.
3185 *
3186 * This routine initializes a message queue object, prior to its first use,
3187 * allocating its internal ring buffer from the calling thread's resource
3188 * pool.
3189 *
3190 * Memory allocated for the ring buffer can be released by calling
3191 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3192 * all of its references.
3193 *
3194 * @param q Address of the message queue.
3195 * @param msg_size Message size (in bytes).
3196 * @param max_msgs Maximum number of messages that can be queued.
3197 *
3198 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3199 * thread's resource pool, or -EINVAL if the size parameters cause
3200 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003201 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003202 */
3203__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3204 u32_t max_msgs);
3205
3206
3207void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003208
3209/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003210 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003211 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003212 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003213 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003214 * @note Can be called by ISRs.
3215 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003216 * @param q Address of the message queue.
3217 * @param data Pointer to the message.
3218 * @param timeout Waiting period to add the message (in milliseconds),
3219 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003220 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003221 * @retval 0 Message sent.
3222 * @retval -ENOMSG Returned without waiting or queue purged.
3223 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003224 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003225 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003226__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003227
3228/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003229 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003231 * This routine receives a message from message queue @a q in a "first in,
3232 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003233 *
Allan Stephensc98da842016-11-11 15:45:03 -05003234 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003236 * @param q Address of the message queue.
3237 * @param data Address of area to hold the received message.
3238 * @param timeout Waiting period to receive the message (in milliseconds),
3239 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003240 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003241 * @retval 0 Message received.
3242 * @retval -ENOMSG Returned without waiting.
3243 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003244 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003245 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003246__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003247
3248/**
Sathish Kuttan3efd8e12018-11-09 21:03:10 -08003249 * @brief Peek/read a message from a message queue.
3250 *
3251 * This routine reads a message from message queue @a q in a "first in,
3252 * first out" manner and leaves the message in the queue.
3253 *
3254 * @note Can be called by ISRs.
3255 *
3256 * @param q Address of the message queue.
3257 * @param data Address of area to hold the message read from the queue.
3258 *
3259 * @retval 0 Message read.
3260 * @retval -ENOMSG Returned when the queue has no message.
3261 * @req K-MSGQ-002
3262 */
3263__syscall int k_msgq_peek(struct k_msgq *q, void *data);
3264
3265/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003266 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003267 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003268 * This routine discards all unreceived messages in a message queue's ring
3269 * buffer. Any threads that are blocked waiting to send a message to the
3270 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003271 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003272 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003273 *
3274 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003275 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003276 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003277__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003278
Peter Mitsis67be2492016-10-07 11:44:34 -04003279/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003280 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003281 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003282 * This routine returns the number of unused entries in a message queue's
3283 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003285 * @param q Address of the message queue.
3286 *
3287 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003288 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003289 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003290__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3291
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303292/**
3293 * @brief Get basic attributes of a message queue.
3294 *
3295 * This routine fetches basic attributes of message queue into attr argument.
3296 *
3297 * @param q Address of the message queue.
3298 * @param attrs pointer to message queue attribute structure.
3299 *
3300 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003301 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303302 */
3303__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3304
3305
Andrew Boie82edb6e2017-10-02 10:53:06 -07003306static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003307{
3308 return q->max_msgs - q->used_msgs;
3309}
3310
Peter Mitsisd7a37502016-10-13 11:37:40 -04003311/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003312 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003313 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003314 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003315 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003316 * @param q Address of the message queue.
3317 *
3318 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003319 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003320 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003321__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3322
3323static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003324{
3325 return q->used_msgs;
3326}
3327
Anas Nashif166f5192018-02-25 08:02:36 -06003328/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003329
3330/**
3331 * @defgroup mem_pool_apis Memory Pool APIs
3332 * @ingroup kernel_apis
3333 * @{
3334 */
3335
Andy Ross73cb9582017-05-09 10:42:39 -07003336/* Note on sizing: the use of a 20 bit field for block means that,
3337 * assuming a reasonable minimum block size of 16 bytes, we're limited
3338 * to 16M of memory managed by a single pool. Long term it would be
3339 * good to move to a variable bit size based on configuration.
3340 */
3341struct k_mem_block_id {
3342 u32_t pool : 8;
3343 u32_t level : 4;
3344 u32_t block : 20;
3345};
3346
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003347struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003348 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003349 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003350};
3351
Anas Nashif166f5192018-02-25 08:02:36 -06003352/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003353
3354/**
3355 * @defgroup mailbox_apis Mailbox APIs
3356 * @ingroup kernel_apis
3357 * @{
3358 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003359
3360struct k_mbox_msg {
3361 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003362 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003363 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003364 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003365 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003366 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003367 /** sender's message data buffer */
3368 void *tx_data;
3369 /** internal use only - needed for legacy API support */
3370 void *_rx_data;
3371 /** message data block descriptor */
3372 struct k_mem_block tx_block;
3373 /** source thread id */
3374 k_tid_t rx_source_thread;
3375 /** target thread id */
3376 k_tid_t tx_target_thread;
3377 /** internal use only - thread waiting on send (may be a dummy) */
3378 k_tid_t _syncing_thread;
3379#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3380 /** internal use only - semaphore used during asynchronous send */
3381 struct k_sem *_async_sem;
3382#endif
3383};
3384
3385struct k_mbox {
3386 _wait_q_t tx_msg_queue;
3387 _wait_q_t rx_msg_queue;
3388
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003389 _OBJECT_TRACING_NEXT_PTR(k_mbox)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003390};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003391/**
3392 * @cond INTERNAL_HIDDEN
3393 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003394
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003395#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003396 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003397 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3398 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003399 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003400 }
3401
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003402#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3403
Peter Mitsis12092702016-10-14 12:57:23 -04003404/**
Allan Stephensc98da842016-11-11 15:45:03 -05003405 * INTERNAL_HIDDEN @endcond
3406 */
3407
3408/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003409 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003410 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003411 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003412 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003413 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003415 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003416 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003417 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003418#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003419 struct k_mbox name \
3420 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003421 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003422
Peter Mitsis12092702016-10-14 12:57:23 -04003423/**
3424 * @brief Initialize a mailbox.
3425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003426 * This routine initializes a mailbox object, prior to its first use.
3427 *
3428 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003429 *
3430 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003431 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003432 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003433extern void k_mbox_init(struct k_mbox *mbox);
3434
Peter Mitsis12092702016-10-14 12:57:23 -04003435/**
3436 * @brief Send a mailbox message in a synchronous manner.
3437 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003438 * This routine sends a message to @a mbox and waits for a receiver to both
3439 * receive and process it. The message data may be in a buffer, in a memory
3440 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003441 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003442 * @param mbox Address of the mailbox.
3443 * @param tx_msg Address of the transmit message descriptor.
3444 * @param timeout Waiting period for the message to be received (in
3445 * milliseconds), or one of the special values K_NO_WAIT
3446 * and K_FOREVER. Once the message has been received,
3447 * this routine waits as long as necessary for the message
3448 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003449 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003450 * @retval 0 Message sent.
3451 * @retval -ENOMSG Returned without waiting.
3452 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003453 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003454 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003455extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003456 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003457
Peter Mitsis12092702016-10-14 12:57:23 -04003458/**
3459 * @brief Send a mailbox message in an asynchronous manner.
3460 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003461 * This routine sends a message to @a mbox without waiting for a receiver
3462 * to process it. The message data may be in a buffer, in a memory pool block,
3463 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3464 * will be given when the message has been both received and completely
3465 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003466 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003467 * @param mbox Address of the mailbox.
3468 * @param tx_msg Address of the transmit message descriptor.
3469 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003470 *
3471 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003472 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003473 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003474extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003475 struct k_sem *sem);
3476
Peter Mitsis12092702016-10-14 12:57:23 -04003477/**
3478 * @brief Receive a mailbox message.
3479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003480 * This routine receives a message from @a mbox, then optionally retrieves
3481 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003483 * @param mbox Address of the mailbox.
3484 * @param rx_msg Address of the receive message descriptor.
3485 * @param buffer Address of the buffer to receive data, or NULL to defer data
3486 * retrieval and message disposal until later.
3487 * @param timeout Waiting period for a message to be received (in
3488 * milliseconds), or one of the special values K_NO_WAIT
3489 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003490 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003491 * @retval 0 Message received.
3492 * @retval -ENOMSG Returned without waiting.
3493 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003494 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003495 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003496extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003497 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003498
3499/**
3500 * @brief Retrieve mailbox message data into a buffer.
3501 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003502 * This routine completes the processing of a received message by retrieving
3503 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003504 *
3505 * Alternatively, this routine can be used to dispose of a received message
3506 * without retrieving its data.
3507 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003508 * @param rx_msg Address of the receive message descriptor.
3509 * @param buffer Address of the buffer to receive data, or NULL to discard
3510 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003511 *
3512 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003513 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003514 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003515extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003516
3517/**
3518 * @brief Retrieve mailbox message data into a memory pool block.
3519 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003520 * This routine completes the processing of a received message by retrieving
3521 * its data into a memory pool block, then disposing of the message.
3522 * The memory pool block that results from successful retrieval must be
3523 * returned to the pool once the data has been processed, even in cases
3524 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003525 *
3526 * Alternatively, this routine can be used to dispose of a received message
3527 * without retrieving its data. In this case there is no need to return a
3528 * memory pool block to the pool.
3529 *
3530 * This routine allocates a new memory pool block for the data only if the
3531 * data is not already in one. If a new block cannot be allocated, the routine
3532 * returns a failure code and the received message is left unchanged. This
3533 * permits the caller to reattempt data retrieval at a later time or to dispose
3534 * of the received message without retrieving its data.
3535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003536 * @param rx_msg Address of a receive message descriptor.
3537 * @param pool Address of memory pool, or NULL to discard data.
3538 * @param block Address of the area to hold memory pool block info.
3539 * @param timeout Waiting period to wait for a memory pool block (in
3540 * milliseconds), or one of the special values K_NO_WAIT
3541 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003542 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003543 * @retval 0 Data retrieved.
3544 * @retval -ENOMEM Returned without waiting.
3545 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003546 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003547 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003548extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003549 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003550 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003551
Anas Nashif166f5192018-02-25 08:02:36 -06003552/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003553
3554/**
Anas Nashifce78d162018-05-24 12:43:11 -05003555 * @defgroup pipe_apis Pipe APIs
3556 * @ingroup kernel_apis
3557 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003558 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003559
Anas Nashifce78d162018-05-24 12:43:11 -05003560/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003561struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003562 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3563 size_t size; /**< Buffer size */
3564 size_t bytes_used; /**< # bytes used in buffer */
3565 size_t read_index; /**< Where in buffer to read from */
3566 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003567
3568 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003569 _wait_q_t readers; /**< Reader wait queue */
3570 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003571 } wait_q;
3572
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003573 _OBJECT_TRACING_NEXT_PTR(k_pipe)
Anas Nashifce78d162018-05-24 12:43:11 -05003574 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003575};
3576
Anas Nashifce78d162018-05-24 12:43:11 -05003577/**
3578 * @cond INTERNAL_HIDDEN
3579 */
3580#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3581
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003582#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003583 { \
3584 .buffer = pipe_buffer, \
3585 .size = pipe_buffer_size, \
3586 .bytes_used = 0, \
3587 .read_index = 0, \
3588 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003589 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3590 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003591 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003592 }
3593
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003594#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3595
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003596/**
Allan Stephensc98da842016-11-11 15:45:03 -05003597 * INTERNAL_HIDDEN @endcond
3598 */
3599
3600/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003601 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003603 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003604 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003605 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003606 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003607 * @param name Name of the pipe.
3608 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3609 * or zero if no ring buffer is used.
3610 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003611 *
3612 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003613 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003614#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3615 static unsigned char __kernel_noinit __aligned(pipe_align) \
3616 _k_pipe_buf_##name[pipe_buffer_size]; \
3617 struct k_pipe name \
3618 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003619 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003620
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003621/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003622 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003624 * This routine initializes a pipe object, prior to its first use.
3625 *
3626 * @param pipe Address of the pipe.
3627 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3628 * is used.
3629 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3630 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003631 *
3632 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003633 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003634 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003635void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3636
3637/**
3638 * @brief Release a pipe's allocated buffer
3639 *
3640 * If a pipe object was given a dynamically allocated buffer via
3641 * k_pipe_alloc_init(), this will free it. This function does nothing
3642 * if the buffer wasn't dynamically allocated.
3643 *
3644 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003645 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003646 */
3647void k_pipe_cleanup(struct k_pipe *pipe);
3648
3649/**
3650 * @brief Initialize a pipe and allocate a buffer for it
3651 *
3652 * Storage for the buffer region will be allocated from the calling thread's
3653 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3654 * or userspace is enabled and the pipe object loses all references to it.
3655 *
3656 * This function should only be called on uninitialized pipe objects.
3657 *
3658 * @param pipe Address of the pipe.
3659 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3660 * buffer is used.
3661 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003662 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003663 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003664 */
3665__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003666
3667/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003668 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003670 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003672 * @param pipe Address of the pipe.
3673 * @param data Address of data to write.
3674 * @param bytes_to_write Size of data (in bytes).
3675 * @param bytes_written Address of area to hold the number of bytes written.
3676 * @param min_xfer Minimum number of bytes to write.
3677 * @param timeout Waiting period to wait for the data to be written (in
3678 * milliseconds), or one of the special values K_NO_WAIT
3679 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003680 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003681 * @retval 0 At least @a min_xfer bytes of data were written.
3682 * @retval -EIO Returned without waiting; zero data bytes were written.
3683 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003684 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003685 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003686 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003687__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3688 size_t bytes_to_write, size_t *bytes_written,
3689 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003690
3691/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003692 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003694 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003695 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003696 * @param pipe Address of the pipe.
3697 * @param data Address to place the data read from pipe.
3698 * @param bytes_to_read Maximum number of data bytes to read.
3699 * @param bytes_read Address of area to hold the number of bytes read.
3700 * @param min_xfer Minimum number of data bytes to read.
3701 * @param timeout Waiting period to wait for the data to be read (in
3702 * milliseconds), or one of the special values K_NO_WAIT
3703 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003704 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003705 * @retval 0 At least @a min_xfer bytes of data were read.
3706 * @retval -EIO Returned without waiting; zero data bytes were read.
3707 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003708 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003709 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003710 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003711__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3712 size_t bytes_to_read, size_t *bytes_read,
3713 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003714
3715/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003716 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003717 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003718 * This routine writes the data contained in a memory block to @a pipe.
3719 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003720 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003722 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003723 * @param block Memory block containing data to send
3724 * @param size Number of data bytes in memory block to send
3725 * @param sem Semaphore to signal upon completion (else NULL)
3726 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003727 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003728 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003729 */
3730extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3731 size_t size, struct k_sem *sem);
3732
Anas Nashif166f5192018-02-25 08:02:36 -06003733/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003734
Allan Stephensc98da842016-11-11 15:45:03 -05003735/**
3736 * @cond INTERNAL_HIDDEN
3737 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003738
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003739struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003740 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003741 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003742 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003743 char *buffer;
3744 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003745 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003746
Flavio Ceolind1ed3362018-12-07 11:39:13 -08003747 _OBJECT_TRACING_NEXT_PTR(k_mem_slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003748};
3749
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003750#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003751 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003752 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003753 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003754 .num_blocks = slab_num_blocks, \
3755 .block_size = slab_block_size, \
3756 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003757 .free_list = NULL, \
3758 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003759 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003760 }
3761
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003762#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3763
3764
Peter Mitsis578f9112016-10-07 13:50:31 -04003765/**
Allan Stephensc98da842016-11-11 15:45:03 -05003766 * INTERNAL_HIDDEN @endcond
3767 */
3768
3769/**
3770 * @defgroup mem_slab_apis Memory Slab APIs
3771 * @ingroup kernel_apis
3772 * @{
3773 */
3774
3775/**
Allan Stephensda827222016-11-09 14:23:58 -06003776 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003777 *
Allan Stephensda827222016-11-09 14:23:58 -06003778 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003779 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003780 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3781 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003782 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003783 *
Allan Stephensda827222016-11-09 14:23:58 -06003784 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003785 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003786 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003787 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003788 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003789 * @param name Name of the memory slab.
3790 * @param slab_block_size Size of each memory block (in bytes).
3791 * @param slab_num_blocks Number memory blocks.
3792 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003793 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003794 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003795#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3796 char __noinit __aligned(slab_align) \
3797 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3798 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003799 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003800 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003801 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003802
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003803/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003804 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003805 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003806 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003807 *
Allan Stephensda827222016-11-09 14:23:58 -06003808 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3809 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3810 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3811 * To ensure that each memory block is similarly aligned to this boundary,
3812 * @a slab_block_size must also be a multiple of N.
3813 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003814 * @param slab Address of the memory slab.
3815 * @param buffer Pointer to buffer used for the memory blocks.
3816 * @param block_size Size of each memory block (in bytes).
3817 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003818 *
3819 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003820 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003821 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003822extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003823 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003824
3825/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003826 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003828 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003829 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003830 * @param slab Address of the memory slab.
3831 * @param mem Pointer to block address area.
3832 * @param timeout Maximum time to wait for operation to complete
3833 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3834 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003835 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003836 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003837 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003838 * @retval -ENOMEM Returned without waiting.
3839 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003840 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003841 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003842extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003843 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003844
3845/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003846 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003848 * This routine releases a previously allocated memory block back to its
3849 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003850 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003851 * @param slab Address of the memory slab.
3852 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003853 *
3854 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003855 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003856 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003857extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003858
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003859/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003860 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003862 * This routine gets the number of memory blocks that are currently
3863 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003865 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003867 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003868 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003869 */
Kumar Galacc334c72017-04-21 10:55:34 -05003870static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003871{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003872 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003873}
3874
Peter Mitsisc001aa82016-10-13 13:53:37 -04003875/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003876 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003878 * This routine gets the number of memory blocks that are currently
3879 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003881 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003883 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003884 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04003885 */
Kumar Galacc334c72017-04-21 10:55:34 -05003886static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003887{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003888 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003889}
3890
Anas Nashif166f5192018-02-25 08:02:36 -06003891/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003892
3893/**
3894 * @cond INTERNAL_HIDDEN
3895 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003896
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003897struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003898 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003899 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003900};
3901
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003902/**
Allan Stephensc98da842016-11-11 15:45:03 -05003903 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003904 */
3905
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003906/**
Allan Stephensc98da842016-11-11 15:45:03 -05003907 * @addtogroup mem_pool_apis
3908 * @{
3909 */
3910
3911/**
3912 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003913 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003914 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3915 * long. The memory pool allows blocks to be repeatedly partitioned into
3916 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003917 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003918 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003919 * If the pool is to be accessed outside the module where it is defined, it
3920 * can be declared via
3921 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003922 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003923 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003924 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003925 * @param minsz Size of the smallest blocks in the pool (in bytes).
3926 * @param maxsz Size of the largest blocks in the pool (in bytes).
3927 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003928 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003929 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003930 */
Andy Ross73cb9582017-05-09 10:42:39 -07003931#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3932 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3933 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003934 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003935 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003936 .base = { \
3937 .buf = _mpool_buf_##name, \
3938 .max_sz = maxsz, \
3939 .n_max = nmax, \
3940 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3941 .levels = _mpool_lvls_##name, \
3942 .flags = SYS_MEM_POOL_KERNEL \
3943 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003944 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003945
Peter Mitsis937042c2016-10-13 13:18:26 -04003946/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003947 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003948 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003949 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003950 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003951 * @param pool Address of the memory pool.
3952 * @param block Pointer to block descriptor for the allocated memory.
3953 * @param size Amount of memory to allocate (in bytes).
3954 * @param timeout Maximum time to wait for operation to complete
3955 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3956 * or K_FOREVER to wait as long as necessary.
3957 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003958 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003959 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003960 * @retval -ENOMEM Returned without waiting.
3961 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003962 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04003963 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003964extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003965 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003966
3967/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07003968 * @brief Allocate memory from a memory pool with malloc() semantics
3969 *
3970 * Such memory must be released using k_free().
3971 *
3972 * @param pool Address of the memory pool.
3973 * @param size Amount of memory to allocate (in bytes).
3974 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003975 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07003976 */
3977extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
3978
3979/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003980 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003981 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003982 * This routine releases a previously allocated memory block back to its
3983 * memory pool.
3984 *
3985 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003986 *
3987 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003988 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04003989 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003990extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003991
3992/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003993 * @brief Free memory allocated from a memory pool.
3994 *
3995 * This routine releases a previously allocated memory block back to its
3996 * memory pool
3997 *
3998 * @param id Memory block identifier.
3999 *
4000 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004001 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004002 */
4003extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4004
4005/**
Anas Nashif166f5192018-02-25 08:02:36 -06004006 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004007 */
4008
4009/**
4010 * @defgroup heap_apis Heap Memory Pool APIs
4011 * @ingroup kernel_apis
4012 * @{
4013 */
4014
4015/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004016 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004018 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004019 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004021 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004022 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004023 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004024 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004025 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004026extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004027
4028/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004029 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004030 *
4031 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004032 * returned must have been allocated from the heap memory pool or
4033 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004034 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004035 * If @a ptr is NULL, no operation is performed.
4036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004037 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004038 *
4039 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004040 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004041 */
4042extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004043
Allan Stephensc98da842016-11-11 15:45:03 -05004044/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004045 * @brief Allocate memory from heap, array style
4046 *
4047 * This routine provides traditional calloc() semantics. Memory is
4048 * allocated from the heap memory pool and zeroed.
4049 *
4050 * @param nmemb Number of elements in the requested array
4051 * @param size Size of each array element (in bytes).
4052 *
4053 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004054 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004055 */
4056extern void *k_calloc(size_t nmemb, size_t size);
4057
Anas Nashif166f5192018-02-25 08:02:36 -06004058/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004059
Benjamin Walshacc68c12017-01-29 18:57:45 -05004060/* polling API - PRIVATE */
4061
Benjamin Walshb0179862017-02-02 16:39:57 -05004062#ifdef CONFIG_POLL
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004063#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004064#else
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004065#define _INIT_OBJ_POLL_EVENT(obj) do { } while (false)
Benjamin Walshb0179862017-02-02 16:39:57 -05004066#endif
4067
Benjamin Walshacc68c12017-01-29 18:57:45 -05004068/* private - implementation data created as needed, per-type */
4069struct _poller {
4070 struct k_thread *thread;
Flavio Ceolin76b35182018-12-16 12:48:29 -08004071 volatile bool is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004072};
4073
4074/* private - types bit positions */
4075enum _poll_types_bits {
4076 /* can be used to ignore an event */
4077 _POLL_TYPE_IGNORE,
4078
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004079 /* to be signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004080 _POLL_TYPE_SIGNAL,
4081
4082 /* semaphore availability */
4083 _POLL_TYPE_SEM_AVAILABLE,
4084
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004085 /* queue/fifo/lifo data availability */
4086 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004087
4088 _POLL_NUM_TYPES
4089};
4090
4091#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4092
4093/* private - states bit positions */
4094enum _poll_states_bits {
4095 /* default state when creating event */
4096 _POLL_STATE_NOT_READY,
4097
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004098 /* signaled by k_poll_signal_raise() */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004099 _POLL_STATE_SIGNALED,
4100
4101 /* semaphore is available */
4102 _POLL_STATE_SEM_AVAILABLE,
4103
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004104 /* data is available to read on queue/fifo/lifo */
4105 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004106
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004107 /* queue/fifo/lifo wait was cancelled */
4108 _POLL_STATE_CANCELLED,
4109
Benjamin Walshacc68c12017-01-29 18:57:45 -05004110 _POLL_NUM_STATES
4111};
4112
4113#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4114
4115#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004116 (32 - (0 \
4117 + 8 /* tag */ \
4118 + _POLL_NUM_TYPES \
4119 + _POLL_NUM_STATES \
4120 + 1 /* modes */ \
4121 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004122
Benjamin Walshacc68c12017-01-29 18:57:45 -05004123/* end of polling API - PRIVATE */
4124
4125
4126/**
4127 * @defgroup poll_apis Async polling APIs
4128 * @ingroup kernel_apis
4129 * @{
4130 */
4131
4132/* Public polling API */
4133
4134/* public - values for k_poll_event.type bitfield */
4135#define K_POLL_TYPE_IGNORE 0
4136#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4137#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004138#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4139#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004140
4141/* public - polling modes */
4142enum k_poll_modes {
4143 /* polling thread does not take ownership of objects when available */
4144 K_POLL_MODE_NOTIFY_ONLY = 0,
4145
4146 K_POLL_NUM_MODES
4147};
4148
4149/* public - values for k_poll_event.state bitfield */
4150#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004151#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4152#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004153#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4154#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004155#define K_POLL_STATE_CANCELLED _POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004156
4157/* public - poll signal object */
4158struct k_poll_signal {
4159 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004160 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004161
4162 /*
4163 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4164 * user resets it to 0.
4165 */
4166 unsigned int signaled;
4167
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004168 /* custom result value passed to k_poll_signal_raise() if needed */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004169 int result;
4170};
4171
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004172#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004173 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004174 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004175 .signaled = 0, \
4176 .result = 0, \
4177 }
4178
4179struct k_poll_event {
4180 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004181 sys_dnode_t _node;
4182
4183 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004184 struct _poller *poller;
4185
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004186 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004187 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004188
Benjamin Walshacc68c12017-01-29 18:57:45 -05004189 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004190 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004191
4192 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004193 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004194
4195 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004196 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004197
4198 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004199 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004200
4201 /* per-type data */
4202 union {
4203 void *obj;
4204 struct k_poll_signal *signal;
4205 struct k_sem *sem;
4206 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004207 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004208 };
4209};
4210
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004211#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004212 { \
4213 .poller = NULL, \
4214 .type = event_type, \
4215 .state = K_POLL_STATE_NOT_READY, \
4216 .mode = event_mode, \
4217 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004218 { .obj = event_obj }, \
4219 }
4220
4221#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4222 event_tag) \
4223 { \
4224 .type = event_type, \
4225 .tag = event_tag, \
4226 .state = K_POLL_STATE_NOT_READY, \
4227 .mode = event_mode, \
4228 .unused = 0, \
4229 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004230 }
4231
4232/**
4233 * @brief Initialize one struct k_poll_event instance
4234 *
4235 * After this routine is called on a poll event, the event it ready to be
4236 * placed in an event array to be passed to k_poll().
4237 *
4238 * @param event The event to initialize.
4239 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4240 * values. Only values that apply to the same object being polled
4241 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4242 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004243 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004244 * @param obj Kernel object or poll signal.
4245 *
4246 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004247 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004248 */
4249
Kumar Galacc334c72017-04-21 10:55:34 -05004250extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004251 int mode, void *obj);
4252
4253/**
4254 * @brief Wait for one or many of multiple poll events to occur
4255 *
4256 * This routine allows a thread to wait concurrently for one or many of
4257 * multiple poll events to have occurred. Such events can be a kernel object
4258 * being available, like a semaphore, or a poll signal event.
4259 *
4260 * When an event notifies that a kernel object is available, the kernel object
4261 * is not "given" to the thread calling k_poll(): it merely signals the fact
4262 * that the object was available when the k_poll() call was in effect. Also,
4263 * all threads trying to acquire an object the regular way, i.e. by pending on
4264 * the object, have precedence over the thread polling on the object. This
4265 * means that the polling thread will never get the poll event on an object
4266 * until the object becomes available and its pend queue is empty. For this
4267 * reason, the k_poll() call is more effective when the objects being polled
4268 * only have one thread, the polling thread, trying to acquire them.
4269 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004270 * When k_poll() returns 0, the caller should loop on all the events that were
4271 * passed to k_poll() and check the state field for the values that were
4272 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004273 *
4274 * Before being reused for another call to k_poll(), the user has to reset the
4275 * state field to K_POLL_STATE_NOT_READY.
4276 *
Andrew Boie3772f772018-05-07 16:52:57 -07004277 * When called from user mode, a temporary memory allocation is required from
4278 * the caller's resource pool.
4279 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004280 * @param events An array of pointers to events to be polled for.
4281 * @param num_events The number of events in the array.
4282 * @param timeout Waiting period for an event to be ready (in milliseconds),
4283 * or one of the special values K_NO_WAIT and K_FOREVER.
4284 *
4285 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004286 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004287 * @retval -EINTR Polling has been interrupted, e.g. with
4288 * k_queue_cancel_wait(). All output events are still set and valid,
4289 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4290 * words, -EINTR status means that at least one of output events is
4291 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004292 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4293 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004294 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004295 */
4296
Andrew Boie3772f772018-05-07 16:52:57 -07004297__syscall int k_poll(struct k_poll_event *events, int num_events,
4298 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004299
4300/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004301 * @brief Initialize a poll signal object.
4302 *
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004303 * Ready a poll signal object to be signaled via k_poll_signal_raise().
Benjamin Walsha304f162017-02-02 16:46:09 -05004304 *
4305 * @param signal A poll signal.
4306 *
4307 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004308 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004309 */
4310
Andrew Boie3772f772018-05-07 16:52:57 -07004311__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4312
4313/*
4314 * @brief Reset a poll signal object's state to unsignaled.
4315 *
4316 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004317 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004318 */
4319__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4320
4321static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4322{
4323 signal->signaled = 0;
4324}
4325
4326/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004327 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004328 *
4329 * @param signal A poll signal object
4330 * @param signaled An integer buffer which will be written nonzero if the
4331 * object was signaled
4332 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004333 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004334 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004335 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004336 */
4337__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4338 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004339
4340/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004341 * @brief Signal a poll signal object.
4342 *
4343 * This routine makes ready a poll signal, which is basically a poll event of
4344 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4345 * made ready to run. A @a result value can be specified.
4346 *
4347 * The poll signal contains a 'signaled' field that, when set by
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004348 * k_poll_signal_raise(), stays set until the user sets it back to 0 with
Andrew Boie3772f772018-05-07 16:52:57 -07004349 * k_poll_signal_reset(). It thus has to be reset by the user before being
4350 * passed again to k_poll() or k_poll() will consider it being signaled, and
4351 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004352 *
4353 * @param signal A poll signal.
4354 * @param result The value to store in the result field of the signal.
4355 *
4356 * @retval 0 The signal was delivered successfully.
4357 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004358 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004359 */
4360
Flavio Ceolinaecd4ec2018-11-02 12:35:30 -07004361__syscall int k_poll_signal_raise(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004362
Anas Nashif954d5502018-02-25 08:37:28 -06004363/**
4364 * @internal
4365 */
Andy Ross8606fab2018-03-26 10:54:40 -07004366extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004367
Anas Nashif166f5192018-02-25 08:02:36 -06004368/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004369
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004370/**
Anas Nashif30c3cff2019-01-22 08:18:13 -05004371 * @defgroup cpu_idle_apis CPU Idling APIs
4372 * @ingroup kernel_apis
4373 * @{
4374 */
4375
4376/**
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004377 * @brief Make the CPU idle.
4378 *
4379 * This function makes the CPU idle until an event wakes it up.
4380 *
4381 * In a regular system, the idle thread should be the only thread responsible
4382 * for making the CPU idle and triggering any type of power management.
4383 * However, in some more constrained systems, such as a single-threaded system,
4384 * the only thread would be responsible for this if needed.
4385 *
4386 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004387 * @req K-CPU-IDLE-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004388 */
4389extern void k_cpu_idle(void);
4390
4391/**
4392 * @brief Make the CPU idle in an atomic fashion.
4393 *
4394 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4395 * must be done atomically before making the CPU idle.
4396 *
4397 * @param key Interrupt locking key obtained from irq_lock().
4398 *
4399 * @return N/A
Anas Nashif30c3cff2019-01-22 08:18:13 -05004400 * @req K-CPU-IDLE-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004401 */
4402extern void k_cpu_atomic_idle(unsigned int key);
4403
Anas Nashif30c3cff2019-01-22 08:18:13 -05004404/**
4405 * @}
4406 */
Anas Nashif954d5502018-02-25 08:37:28 -06004407
4408/**
4409 * @internal
4410 */
Kumar Galacc334c72017-04-21 10:55:34 -05004411extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004412
Andrew Boiecdb94d62017-04-18 15:22:05 -07004413#ifdef _ARCH_EXCEPT
4414/* This archtecture has direct support for triggering a CPU exception */
4415#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4416#else
4417
Andrew Boiecdb94d62017-04-18 15:22:05 -07004418/* NOTE: This is the implementation for arches that do not implement
4419 * _ARCH_EXCEPT() to generate a real CPU exception.
4420 *
4421 * We won't have a real exception frame to determine the PC value when
4422 * the oops occurred, so print file and line number before we jump into
4423 * the fatal error handler.
4424 */
4425#define _k_except_reason(reason) do { \
4426 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4427 _NanoFatalErrorHandler(reason, &_default_esf); \
4428 CODE_UNREACHABLE; \
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004429 } while (false)
Andrew Boiecdb94d62017-04-18 15:22:05 -07004430
4431#endif /* _ARCH__EXCEPT */
4432
4433/**
4434 * @brief Fatally terminate a thread
4435 *
4436 * This should be called when a thread has encountered an unrecoverable
4437 * runtime condition and needs to terminate. What this ultimately
4438 * means is determined by the _fatal_error_handler() implementation, which
4439 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4440 *
4441 * If this is called from ISR context, the default system fatal error handler
4442 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004443 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004444 */
4445#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4446
4447/**
4448 * @brief Fatally terminate the system
4449 *
4450 * This should be called when the Zephyr kernel has encountered an
4451 * unrecoverable runtime condition and needs to terminate. What this ultimately
4452 * means is determined by the _fatal_error_handler() implementation, which
4453 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004454 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004455 */
4456#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4457
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004458/*
4459 * private APIs that are utilized by one or more public APIs
4460 */
4461
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004462#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004463/**
4464 * @internal
4465 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004466extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004467#else
Anas Nashif954d5502018-02-25 08:37:28 -06004468/**
4469 * @internal
4470 */
Flavio Ceolin6fdc56d2018-09-18 12:32:27 -07004471#define _init_static_threads() do { } while (false)
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004472#endif
4473
Anas Nashif954d5502018-02-25 08:37:28 -06004474/**
4475 * @internal
4476 */
Flavio Ceolin09e362e2018-12-17 12:34:05 -08004477extern bool _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004478/**
4479 * @internal
4480 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004481extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004482
Andrew Boiedc5d9352017-06-02 12:56:47 -07004483/* arch/cpu.h may declare an architecture or platform-specific macro
4484 * for properly declaring stacks, compatible with MMU/MPU constraints if
4485 * enabled
4486 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004487
4488/**
4489 * @brief Obtain an extern reference to a stack
4490 *
4491 * This macro properly brings the symbol of a thread stack declared
4492 * elsewhere into scope.
4493 *
4494 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004495 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004496 */
4497#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4498
Andrew Boiedc5d9352017-06-02 12:56:47 -07004499#ifdef _ARCH_THREAD_STACK_DEFINE
4500#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4501#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4502 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304503#define K_THREAD_STACK_LEN(size) _ARCH_THREAD_STACK_LEN(size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004504#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4505#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004506static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004507{
4508 return _ARCH_THREAD_STACK_BUFFER(sym);
4509}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004510#else
4511/**
4512 * @brief Declare a toplevel thread stack memory region
4513 *
4514 * This declares a region of memory suitable for use as a thread's stack.
4515 *
4516 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4517 * 'noinit' section so that it isn't zeroed at boot
4518 *
Andrew Boie507852a2017-07-25 18:47:07 -07004519 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004520 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004521 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004522 *
4523 * It is legal to precede this definition with the 'static' keyword.
4524 *
4525 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4526 * parameter of k_thread_create(), it may not be the same as the
4527 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4528 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004529 * Some arches may round the size of the usable stack region up to satisfy
4530 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4531 * size.
4532 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004533 * @param sym Thread stack symbol name
4534 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004535 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004536 */
4537#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004538 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004539
4540/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304541 * @brief Calculate size of stacks to be allocated in a stack array
4542 *
4543 * This macro calculates the size to be allocated for the stacks
4544 * inside a stack array. It accepts the indicated "size" as a parameter
4545 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4546 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4547 *
4548 * @param size Size of the stack memory region
4549 * @req K-TSTACK-001
4550 */
4551#define K_THREAD_STACK_LEN(size) (size)
4552
4553/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004554 * @brief Declare a toplevel array of thread stack memory regions
4555 *
4556 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4557 * definition for additional details and constraints.
4558 *
4559 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4560 * 'noinit' section so that it isn't zeroed at boot
4561 *
4562 * @param sym Thread stack symbol name
4563 * @param nmemb Number of stacks to declare
4564 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004565 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004566 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004567#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004568 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304569 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004570
4571/**
4572 * @brief Declare an embedded stack memory region
4573 *
4574 * Used for stacks embedded within other data structures. Use is highly
4575 * discouraged but in some cases necessary. For memory protection scenarios,
4576 * it is very important that any RAM preceding this member not be writable
4577 * by threads else a stack overflow will lead to silent corruption. In other
4578 * words, the containing data structure should live in RAM owned by the kernel.
4579 *
4580 * @param sym Thread stack symbol name
4581 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004582 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004583 */
4584#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004585 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004586
4587/**
4588 * @brief Return the size in bytes of a stack memory region
4589 *
4590 * Convenience macro for passing the desired stack size to k_thread_create()
4591 * since the underlying implementation may actually create something larger
4592 * (for instance a guard area).
4593 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004594 * The value returned here is not guaranteed to match the 'size' parameter
4595 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004596 *
4597 * @param sym Stack memory symbol
4598 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004599 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004600 */
4601#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4602
4603/**
4604 * @brief Get a pointer to the physical stack buffer
4605 *
4606 * Convenience macro to get at the real underlying stack buffer used by
4607 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4608 * This is really only intended for diagnostic tools which want to examine
4609 * stack memory contents.
4610 *
4611 * @param sym Declared stack symbol name
4612 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004613 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004614 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004615static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004616{
4617 return (char *)sym;
4618}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004619
4620#endif /* _ARCH_DECLARE_STACK */
4621
Chunlin Hane9c97022017-07-07 20:29:30 +08004622/**
4623 * @defgroup mem_domain_apis Memory domain APIs
4624 * @ingroup kernel_apis
4625 * @{
4626 */
4627
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004628/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004629 * @def K_MEM_PARTITION_DEFINE
4630 * @brief Used to declare a memory partition
4631 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004632 */
4633#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4634#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4635 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304636 __kernel struct k_mem_partition name =\
Ioannis Glaropoulos293247e2018-12-03 18:32:32 +01004637 { (u32_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004638#else
4639#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304640 __kernel struct k_mem_partition name =\
Ioannis Glaropoulos293247e2018-12-03 18:32:32 +01004641 { (u32_t)start, size, attr}
Chunlin Hane9c97022017-07-07 20:29:30 +08004642#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4643
Chunlin Hane9c97022017-07-07 20:29:30 +08004644/* memory partition */
4645struct k_mem_partition {
4646 /* start address of memory partition */
4647 u32_t start;
4648 /* size of memory partition */
4649 u32_t size;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004650#if defined(CONFIG_MEMORY_PROTECTION)
Chunlin Hane9c97022017-07-07 20:29:30 +08004651 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304652 k_mem_partition_attr_t attr;
Ioannis Glaropoulos39bf24a2018-11-27 15:45:36 +01004653#endif /* CONFIG_MEMORY_PROTECTION */
Chunlin Hane9c97022017-07-07 20:29:30 +08004654};
4655
Ioannis Glaropoulos12c02442018-09-25 16:05:24 +02004656/* memory domain
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304657 * Note: Always declare this structure with __kernel prefix
4658 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004659struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304660#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004661 /* partitions in the domain */
4662 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304663#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004664 /* domain q */
4665 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004666 /* number of partitions in the domain */
4667 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004668};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304669
Chunlin Hane9c97022017-07-07 20:29:30 +08004670
4671/**
4672 * @brief Initialize a memory domain.
4673 *
4674 * Initialize a memory domain with given name and memory partitions.
4675 *
4676 * @param domain The memory domain to be initialized.
4677 * @param num_parts The number of array items of "parts" parameter.
4678 * @param parts An array of pointers to the memory partitions. Can be NULL
4679 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004680 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004681 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004682extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004683 struct k_mem_partition *parts[]);
4684/**
4685 * @brief Destroy a memory domain.
4686 *
4687 * Destroy a memory domain.
4688 *
4689 * @param domain The memory domain to be destroyed.
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_destroy(struct k_mem_domain *domain);
4693
4694/**
4695 * @brief Add a memory partition into a memory domain.
4696 *
4697 * Add a memory partition into a memory domain.
4698 *
4699 * @param domain The memory domain to be added a memory partition.
4700 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004701 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004702 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004703extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4704 struct k_mem_partition *part);
4705
4706/**
4707 * @brief Remove a memory partition from a memory domain.
4708 *
4709 * Remove a memory partition from a memory domain.
4710 *
4711 * @param domain The memory domain to be removed a memory partition.
4712 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004713 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004714 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004715extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4716 struct k_mem_partition *part);
4717
4718/**
4719 * @brief Add a thread into a memory domain.
4720 *
4721 * Add a thread into a memory domain.
4722 *
4723 * @param domain The memory domain that the thread is going to be added into.
4724 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004725 *
4726 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004727 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004728extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4729 k_tid_t thread);
4730
4731/**
4732 * @brief Remove a thread from its memory domain.
4733 *
4734 * Remove a thread from its memory domain.
4735 *
4736 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004737 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004738 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004739extern void k_mem_domain_remove_thread(k_tid_t thread);
4740
Anas Nashif166f5192018-02-25 08:02:36 -06004741/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004742
Andrew Boie756f9072017-10-10 16:01:49 -07004743/**
4744 * @brief Emit a character buffer to the console device
4745 *
4746 * @param c String of characters to print
4747 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004748 *
4749 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004750 */
4751__syscall void k_str_out(char *c, size_t n);
4752
Andy Rosse7172672018-01-24 15:48:32 -08004753/**
4754 * @brief Start a numbered CPU on a MP-capable system
4755
4756 * This starts and initializes a specific CPU. The main thread on
4757 * startup is running on CPU zero, other processors are numbered
4758 * sequentially. On return from this function, the CPU is known to
4759 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004760 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004761 * with the provided key will work to enable them.
4762 *
4763 * Normally, in SMP mode this function will be called by the kernel
4764 * initialization and should not be used as a user API. But it is
4765 * defined here for special-purpose apps which want Zephyr running on
4766 * one core and to use others for design-specific processing.
4767 *
4768 * @param cpu_num Integer number of the CPU
4769 * @param stack Stack memory for the CPU
4770 * @param sz Stack buffer size, in bytes
4771 * @param fn Function to begin running on the CPU. First argument is
4772 * an irq_unlock() key.
4773 * @param arg Untyped argument to be passed to "fn"
4774 */
4775extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
Flavio Ceolin4b35dd22018-11-16 19:06:59 -08004776 void (*fn)(int key, void *data), void *arg);
Andy Rosse7172672018-01-24 15:48:32 -08004777
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004778#ifdef __cplusplus
4779}
4780#endif
4781
Anas Nashifb6304e62018-07-04 08:03:03 -05004782#include <tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07004783#include <syscalls/kernel.h>
4784
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004785#endif /* !_ASMLANGUAGE */
4786
Flavio Ceolin67ca1762018-09-14 10:43:44 -07004787#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */