blob: ae8e8e6b719640d20a59d8ce4607b0d4dd838e0a [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
13#ifndef _kernel__h_
14#define _kernel__h_
15
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>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040019
20#ifdef __cplusplus
21extern "C" {
22#endif
23
Anas Nashifbbb157d2017-01-15 08:46:31 -050024/**
25 * @brief Kernel APIs
26 * @defgroup kernel_apis Kernel APIs
27 * @{
28 * @}
29 */
30
Anas Nashif61f4b242016-11-18 10:53:59 -050031#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040032#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
33#else
34#define K_DEBUG(fmt, ...)
35#endif
36
Benjamin Walsh2f280412017-01-14 19:23:46 -050037#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
38#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
39#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
40#elif defined(CONFIG_COOP_ENABLED)
41#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
42#define _NUM_PREEMPT_PRIO (0)
43#elif defined(CONFIG_PREEMPT_ENABLED)
44#define _NUM_COOP_PRIO (0)
45#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
46#else
47#error "invalid configuration"
48#endif
49
50#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040051#define K_PRIO_PREEMPT(x) (x)
52
Benjamin Walsh456c6da2016-09-02 18:55:39 -040053#define K_ANY NULL
54#define K_END NULL
55
Benjamin Walshedb35702017-01-14 18:47:22 -050056#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040057#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050058#elif defined(CONFIG_COOP_ENABLED)
59#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
60#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040061#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050062#else
63#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040064#endif
65
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050066#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040067#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
68#else
69#define K_LOWEST_THREAD_PRIO -1
70#endif
71
Benjamin Walshfab8d922016-11-08 15:36:36 -050072#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
73
Benjamin Walsh456c6da2016-09-02 18:55:39 -040074#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
75#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
76
Andy Ross225c74b2018-06-27 11:20:50 -070077#ifdef CONFIG_WAITQ_SCALABLE
Andy Ross1acd8c22018-05-03 14:51:49 -070078
79typedef struct {
80 struct _priq_rb waitq;
81} _wait_q_t;
82
83extern int _priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
84
85#define _WAIT_Q_INIT(wait_q) { { { .lessthan_fn = _priq_rb_lessthan } } }
86
87#else
88
Andy Rossccf3bf72018-05-10 11:10:34 -070089typedef struct {
90 sys_dlist_t waitq;
91} _wait_q_t;
92
93#define _WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
Benjamin Walsh456c6da2016-09-02 18:55:39 -040094
Andy Ross1acd8c22018-05-03 14:51:49 -070095#endif
96
Anas Nashif2f203c22016-12-18 06:57:45 -050097#ifdef CONFIG_OBJECT_TRACING
98#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
99#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400100#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500101#define _OBJECT_TRACING_INIT
102#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400103#endif
104
Benjamin Walshacc68c12017-01-29 18:57:45 -0500105#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300106#define _POLL_EVENT_OBJ_INIT(obj) \
107 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
108#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500109#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300110#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500111#define _POLL_EVENT
112#endif
113
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500114struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400115struct k_mutex;
116struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400117struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400118struct k_msgq;
119struct k_mbox;
120struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200121struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122struct k_fifo;
123struct k_lifo;
124struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400125struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400126struct k_mem_pool;
127struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500128struct k_poll_event;
129struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800130struct k_mem_domain;
131struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400132
Andrew Boie5bd891d2017-09-27 12:59:28 -0700133/* This enumeration needs to be kept in sync with the lists of kernel objects
134 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
135 * function in kernel/userspace.c
136 */
Andrew Boie945af952017-08-22 13:15:23 -0700137enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700138 K_OBJ_ANY,
139
Leandro Pereirac2003672018-04-04 13:50:32 -0700140 /** @cond
141 * Doxygen should ignore this build-time generated include file
142 * when genrating API documentation. Enumeration values are
143 * generated during build by gen_kobject_list.py. It includes
144 * basic kernel objects (e.g. pipes and mutexes) and driver types.
145 */
146#include <kobj-types-enum.h>
147 /** @endcond
148 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700149
Andrew Boie945af952017-08-22 13:15:23 -0700150 K_OBJ_LAST
151};
152
153#ifdef CONFIG_USERSPACE
154/* Table generated by gperf, these objects are retrieved via
155 * _k_object_find() */
156struct _k_object {
157 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700158 u8_t perms[CONFIG_MAX_THREAD_BYTES];
159 u8_t type;
160 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700161 u32_t data;
Andrew Boiedf555242018-05-25 07:28:54 -0700162} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700163
Andrew Boie877f82e2017-10-17 11:20:22 -0700164struct _k_object_assignment {
165 struct k_thread *thread;
166 void * const *objects;
167};
168
169/**
170 * @brief Grant a static thread access to a list of kernel objects
171 *
172 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
173 * a set of kernel objects. These objects do not need to be in an initialized
174 * state. The permissions will be granted when the threads are initialized
175 * in the early boot sequence.
176 *
177 * All arguments beyond the first must be pointers to kernel objects.
178 *
179 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
180 */
181#define K_THREAD_ACCESS_GRANT(name_, ...) \
182 static void * const _CONCAT(_object_list_, name_)[] = \
183 { __VA_ARGS__, NULL }; \
184 static __used __in_section_unique(object_access) \
185 const struct _k_object_assignment \
186 _CONCAT(_object_access_, name_) = \
187 { (&_k_thread_obj_ ## name_), \
188 (_CONCAT(_object_list_, name_)) }
189
Andrew Boie945af952017-08-22 13:15:23 -0700190#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700191#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700192#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie945af952017-08-22 13:15:23 -0700193
194/**
195 * Lookup a kernel object and init its metadata if it exists
196 *
197 * Calling this on an object will make it usable from userspace.
198 * Intended to be called as the last statement in kernel object init
199 * functions.
200 *
201 * @param object Address of the kernel object
202 */
203void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700204#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700205
206#define K_THREAD_ACCESS_GRANT(thread, ...)
207
Anas Nashif954d5502018-02-25 08:37:28 -0600208/**
209 * @internal
210 */
Andrew Boie743e4682017-10-04 12:25:50 -0700211static inline void _k_object_init(void *obj)
212{
213 ARG_UNUSED(obj);
214}
215
Anas Nashif954d5502018-02-25 08:37:28 -0600216/**
217 * @internal
218 */
Andrew Boie743e4682017-10-04 12:25:50 -0700219static inline void _impl_k_object_access_grant(void *object,
220 struct k_thread *thread)
221{
222 ARG_UNUSED(object);
223 ARG_UNUSED(thread);
224}
225
Anas Nashif954d5502018-02-25 08:37:28 -0600226/**
227 * @internal
228 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700229static inline void k_object_access_revoke(void *object,
230 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700231{
232 ARG_UNUSED(object);
233 ARG_UNUSED(thread);
234}
235
Andrew Boiee9cfc542018-04-13 13:15:28 -0700236/**
237 * @internal
238 */
239static inline void _impl_k_object_release(void *object)
240{
241 ARG_UNUSED(object);
242}
243
Andrew Boie41bab6e2017-10-14 14:42:23 -0700244static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700245{
246 ARG_UNUSED(object);
247}
248#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700249
250/**
251 * grant a thread access to a kernel object
252 *
253 * The thread will be granted access to the object if the caller is from
254 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700255 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700256 *
257 * @param object Address of kernel object
258 * @param thread Thread to grant access to the object
259 */
Andrew Boie743e4682017-10-04 12:25:50 -0700260__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700261
Andrew Boiea89bf012017-10-09 14:47:55 -0700262/**
263 * grant a thread access to a kernel object
264 *
265 * The thread will lose access to the object if the caller is from
266 * supervisor mode, or the caller is from user mode AND has permissions
267 * on both the object and the thread whose access is being revoked.
268 *
269 * @param object Address of kernel object
270 * @param thread Thread to remove access to the object
271 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700272void k_object_access_revoke(void *object, struct k_thread *thread);
273
274
275__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700276
277/**
278 * grant all present and future threads access to an object
279 *
280 * If the caller is from supervisor mode, or the caller is from user mode and
281 * have sufficient permissions on the object, then that object will have
282 * permissions granted to it for *all* current and future threads running in
283 * the system, effectively becoming a public kernel object.
284 *
285 * Use of this API should be avoided on systems that are running untrusted code
286 * as it is possible for such code to derive the addresses of kernel objects
287 * and perform unwanted operations on them.
288 *
Andrew Boie04caa672017-10-13 13:57:07 -0700289 * It is not possible to revoke permissions on public objects; once public,
290 * any thread may use it.
291 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700292 * @param object Address of kernel object
293 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700294void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700295
Andrew Boie31bdfc02017-11-08 16:38:03 -0800296/**
297 * Allocate a kernel object of a designated type
298 *
299 * This will instantiate at runtime a kernel object of the specified type,
300 * returning a pointer to it. The object will be returned in an uninitialized
301 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700302 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800303 *
304 * Currently, allocation of thread stacks is not supported.
305 *
306 * @param otype Requested kernel object type
307 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
308 * available
309 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700310__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800311
Andrew Boie97bf0012018-04-24 17:01:37 -0700312#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800313/**
314 * Free a kernel object previously allocated with k_object_alloc()
315 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700316 * This will return memory for a kernel object back to resource pool it was
317 * allocated from. Care must be exercised that the object will not be used
318 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800319 *
320 * @param obj Pointer to the kernel object memory address.
321 */
322void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700323#else
324static inline void *_impl_k_object_alloc(enum k_objects otype)
325{
Kumar Gala85699f72018-05-17 09:26:03 -0500326 ARG_UNUSED(otype);
327
Andrew Boie97bf0012018-04-24 17:01:37 -0700328 return NULL;
329}
330
331static inline void k_obj_free(void *obj)
332{
333 ARG_UNUSED(obj);
334}
Andrew Boie31bdfc02017-11-08 16:38:03 -0800335#endif /* CONFIG_DYNAMIC_OBJECTS */
336
Andrew Boiebca15da2017-10-15 14:17:48 -0700337/* Using typedef deliberately here, this is quite intended to be an opaque
338 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
339 *
340 * The purpose of this data type is to clearly distinguish between the
341 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
342 * buffer which composes the stack data actually used by the underlying
343 * thread; they cannot be used interchangably as some arches precede the
344 * stack buffer region with guard areas that trigger a MPU or MMU fault
345 * if written to.
346 *
347 * APIs that want to work with the buffer inside should continue to use
348 * char *.
349 *
350 * Stacks should always be created with K_THREAD_STACK_DEFINE().
351 */
352struct __packed _k_thread_stack_element {
353 char data;
354};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700355typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700356
Andrew Boie73abd322017-04-04 13:19:13 -0700357/* timeouts */
358
359struct _timeout;
360typedef void (*_timeout_func_t)(struct _timeout *t);
361
362struct _timeout {
363 sys_dnode_t node;
364 struct k_thread *thread;
365 sys_dlist_t *wait_q;
366 s32_t delta_ticks_from_prev;
367 _timeout_func_t func;
368};
369
370extern s32_t _timeout_remaining_get(struct _timeout *timeout);
371
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700372/**
373 * @typedef k_thread_entry_t
374 * @brief Thread entry point function type.
375 *
376 * A thread's entry point function is invoked when the thread starts executing.
377 * Up to 3 argument values can be passed to the function.
378 *
379 * The thread terminates execution permanently if the entry point function
380 * returns. The thread is responsible for releasing any shared resources
381 * it may own (such as mutexes and dynamically allocated memory), prior to
382 * returning.
383 *
384 * @param p1 First argument.
385 * @param p2 Second argument.
386 * @param p3 Third argument.
387 *
388 * @return N/A
389 */
390typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700391
392#ifdef CONFIG_THREAD_MONITOR
393struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700394 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700395 void *parameter1;
396 void *parameter2;
397 void *parameter3;
398};
399#endif
400
401/* can be used for creating 'dummy' threads, e.g. for pending on objects */
402struct _thread_base {
403
404 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700405 union {
406 sys_dlist_t qnode_dlist;
407 struct rbnode qnode_rb;
408 };
409
Andy Ross225c74b2018-06-27 11:20:50 -0700410#ifdef CONFIG_WAITQ_SCALABLE
Andy Ross1acd8c22018-05-03 14:51:49 -0700411 /* wait queue on which the thread is pended (needed only for
412 * trees, not dumb lists)
413 */
414 _wait_q_t *pended_on;
415#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700416
417 /* user facing 'thread options'; values defined in include/kernel.h */
418 u8_t user_options;
419
420 /* thread state */
421 u8_t thread_state;
422
423 /*
424 * scheduler lock count and thread priority
425 *
426 * These two fields control the preemptibility of a thread.
427 *
428 * When the scheduler is locked, sched_locked is decremented, which
429 * means that the scheduler is locked for values from 0xff to 0x01. A
430 * thread is coop if its prio is negative, thus 0x80 to 0xff when
431 * looked at the value as unsigned.
432 *
433 * By putting them end-to-end, this means that a thread is
434 * non-preemptible if the bundled value is greater than or equal to
435 * 0x0080.
436 */
437 union {
438 struct {
439#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
440 u8_t sched_locked;
441 s8_t prio;
442#else /* LITTLE and PDP */
443 s8_t prio;
444 u8_t sched_locked;
445#endif
446 };
447 u16_t preempt;
448 };
449
Andy Ross4a2e50f2018-05-15 11:06:25 -0700450#ifdef CONFIG_SCHED_DEADLINE
451 int prio_deadline;
452#endif
453
Andy Ross1acd8c22018-05-03 14:51:49 -0700454 u32_t order_key;
455
Andy Ross2724fd12018-01-29 14:55:20 -0800456#ifdef CONFIG_SMP
457 /* True for the per-CPU idle threads */
458 u8_t is_idle;
459
Andy Ross2724fd12018-01-29 14:55:20 -0800460 /* CPU index on which thread was last run */
461 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700462
463 /* Recursive count of irq_lock() calls */
464 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800465#endif
466
Andrew Boie73abd322017-04-04 13:19:13 -0700467 /* data returned by APIs */
468 void *swap_data;
469
470#ifdef CONFIG_SYS_CLOCK_EXISTS
471 /* this thread's entry in a timeout queue */
472 struct _timeout timeout;
473#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700474};
475
476typedef struct _thread_base _thread_base_t;
477
478#if defined(CONFIG_THREAD_STACK_INFO)
479/* Contains the stack information of a thread */
480struct _thread_stack_info {
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700481 /* Stack Start - Identical to K_THREAD_STACK_BUFFER() on the stack
482 * object. Represents thread-writable stack area without any extras.
483 */
Andrew Boie73abd322017-04-04 13:19:13 -0700484 u32_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700485
486 /* Stack Size - Thread writable stack buffer size. Represents
487 * the size of the actual area, starting from the start member,
488 * that should be writable by the thread
489 */
Andrew Boie73abd322017-04-04 13:19:13 -0700490 u32_t size;
491};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700492
493typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700494#endif /* CONFIG_THREAD_STACK_INFO */
495
Chunlin Hane9c97022017-07-07 20:29:30 +0800496#if defined(CONFIG_USERSPACE)
497struct _mem_domain_info {
498 /* memory domain queue node */
499 sys_dnode_t mem_domain_q_node;
500 /* memory domain of the thread */
501 struct k_mem_domain *mem_domain;
502};
503
504#endif /* CONFIG_USERSPACE */
505
Anas Nashifce78d162018-05-24 12:43:11 -0500506/**
507 * @ingroup thread_apis
508 * Thread Structure
509 */
Andrew Boie73abd322017-04-04 13:19:13 -0700510struct k_thread {
511
512 struct _thread_base base;
513
Anas Nashifce78d162018-05-24 12:43:11 -0500514 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700515 struct _caller_saved caller_saved;
Anas Nashifce78d162018-05-24 12:43:11 -0500516 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700517 struct _callee_saved callee_saved;
518
Anas Nashifce78d162018-05-24 12:43:11 -0500519 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700520 void *init_data;
521
Anas Nashifce78d162018-05-24 12:43:11 -0500522 /**
523 * abort function
524 * @req K-THREAD-002
525 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700526 void (*fn_abort)(void);
527
528#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500529 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700530 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700531
Anas Nashifce78d162018-05-24 12:43:11 -0500532 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700533 struct k_thread *next_thread;
534#endif
535
536#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500537 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700538 void *custom_data;
539#endif
540
541#ifdef CONFIG_ERRNO
Andrew Boie7f4d0062018-07-19 11:09:33 -0700542#ifdef CONFIG_USERSPACE
543 /* Set to the lowest area in the thread stack since this needs to
544 * be directly read/writable by user mode. Not ideal, but best we
545 * can do until we have thread-local storage
546 */
547 int *errno_location;
548#else
Anas Nashifce78d162018-05-24 12:43:11 -0500549 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700550 int errno_var;
551#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700552#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700553
554#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500555 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700556 struct _thread_stack_info stack_info;
557#endif /* CONFIG_THREAD_STACK_INFO */
558
Chunlin Hane9c97022017-07-07 20:29:30 +0800559#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500560 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800561 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500562 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700563 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800564#endif /* CONFIG_USERSPACE */
565
Andy Ross042d8ec2017-12-09 08:37:20 -0800566#if defined(CONFIG_USE_SWITCH)
567 /* When using __switch() a few previously arch-specific items
568 * become part of the core OS
569 */
570
Anas Nashifce78d162018-05-24 12:43:11 -0500571 /** _Swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800572 int swap_retval;
573
Anas Nashifce78d162018-05-24 12:43:11 -0500574 /** Context handle returned via _arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800575 void *switch_handle;
576#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500577 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700578 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800579
Anas Nashifce78d162018-05-24 12:43:11 -0500580 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700581 struct _thread_arch arch;
582};
583
584typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400585typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700586#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400587
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400588enum execution_context_types {
589 K_ISR = 0,
590 K_COOP_THREAD,
591 K_PREEMPT_THREAD,
592};
593
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400594/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100595 * @defgroup profiling_apis Profiling APIs
596 * @ingroup kernel_apis
597 * @{
598 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530599typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
600 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100601
602/**
603 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
604 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700605 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100606 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
607 *
608 * CONFIG_MAIN_STACK_SIZE
609 * CONFIG_IDLE_STACK_SIZE
610 * CONFIG_ISR_STACK_SIZE
611 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
612 *
613 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
614 * produce output.
615 *
616 * @return N/A
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530617 *
618 * @deprecated This API is deprecated. Use k_thread_foreach().
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100619 */
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530620__deprecated extern void k_call_stacks_analyze(void);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100621
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530622/**
623 * @brief Iterate over all the threads in the system.
624 *
625 * This routine iterates over all the threads in the system and
626 * calls the user_cb function for each thread.
627 *
628 * @param user_cb Pointer to the user callback function.
629 * @param user_data Pointer to user data.
630 *
631 * @note CONFIG_THREAD_MONITOR must be set for this function
632 * to be effective. Also this API uses irq_lock to protect the
633 * _kernel.threads list which means creation of new threads and
634 * terminations of existing threads are blocked until this
635 * API returns.
636 *
637 * @return N/A
638 */
639extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
640
Anas Nashif166f5192018-02-25 08:02:36 -0600641/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100642
643/**
Allan Stephensc98da842016-11-11 15:45:03 -0500644 * @defgroup thread_apis Thread APIs
645 * @ingroup kernel_apis
646 * @{
647 */
648
Benjamin Walshed240f22017-01-22 13:05:08 -0500649#endif /* !_ASMLANGUAGE */
650
651
652/*
653 * Thread user options. May be needed by assembly code. Common part uses low
654 * bits, arch-specific use high bits.
655 */
656
Anas Nashifa541e932018-05-24 11:19:16 -0500657/**
658 * @brief system thread that must not abort
659 * @req K-THREAD-000
660 * */
Benjamin Walshed240f22017-01-22 13:05:08 -0500661#define K_ESSENTIAL (1 << 0)
662
663#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500664/**
665 * @brief thread uses floating point registers
666 */
Benjamin Walshed240f22017-01-22 13:05:08 -0500667#define K_FP_REGS (1 << 1)
668#endif
669
Anas Nashifa541e932018-05-24 11:19:16 -0500670/**
671 * @brief user mode thread
672 *
673 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700674 * has additional restrictions
675 */
676#define K_USER (1 << 2)
677
Anas Nashifa541e932018-05-24 11:19:16 -0500678/**
679 * @brief Inherit Permissions
680 *
681 * @details
682 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700683 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
684 * is not enabled.
685 */
686#define K_INHERIT_PERMS (1 << 3)
687
Benjamin Walshed240f22017-01-22 13:05:08 -0500688#ifdef CONFIG_X86
689/* x86 Bitmask definitions for threads user options */
690
691#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
692/* thread uses SSEx (and also FP) registers */
693#define K_SSE_REGS (1 << 7)
694#endif
695#endif
696
697/* end - thread options */
698
699#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400700/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700701 * @brief Create a thread.
702 *
703 * This routine initializes a thread, then schedules it for execution.
704 *
705 * The new thread may be scheduled for immediate execution or a delayed start.
706 * If the newly spawned thread does not have a delayed start the kernel
707 * scheduler may preempt the current thread to allow the new thread to
708 * execute.
709 *
710 * Thread options are architecture-specific, and can include K_ESSENTIAL,
711 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
712 * them using "|" (the logical OR operator).
713 *
714 * Historically, users often would use the beginning of the stack memory region
715 * to store the struct k_thread data, although corruption will occur if the
716 * stack overflows this region and stack protection features may not detect this
717 * situation.
718 *
719 * @param new_thread Pointer to uninitialized struct k_thread
720 * @param stack Pointer to the stack space.
721 * @param stack_size Stack size in bytes.
722 * @param entry Thread entry function.
723 * @param p1 1st entry point parameter.
724 * @param p2 2nd entry point parameter.
725 * @param p3 3rd entry point parameter.
726 * @param prio Thread priority.
727 * @param options Thread options.
728 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
729 *
730 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400731 *
732 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700733 */
Andrew Boie662c3452017-10-02 10:51:18 -0700734__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700735 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700736 size_t stack_size,
737 k_thread_entry_t entry,
738 void *p1, void *p2, void *p3,
739 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700740
Andrew Boie3f091b52017-08-30 14:34:14 -0700741/**
742 * @brief Drop a thread's privileges permanently to user mode
743 *
744 * @param entry Function to start executing from
745 * @param p1 1st entry point parameter
746 * @param p2 2nd entry point parameter
747 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400748 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700749 */
750extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
751 void *p1, void *p2,
752 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700753
Andrew Boied26cf2d2017-03-30 13:07:02 -0700754/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700755 * @brief Grant a thread access to a NULL-terminated set of kernel objects
756 *
757 * This is a convenience function. For the provided thread, grant access to
758 * the remaining arguments, which must be pointers to kernel objects.
759 * The final argument must be a NULL.
760 *
761 * The thread object must be initialized (i.e. running). The objects don't
762 * need to be.
763 *
764 * @param thread Thread to grant access to objects
765 * @param ... NULL-terminated list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400766 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700767 */
768extern void __attribute__((sentinel))
769 k_thread_access_grant(struct k_thread *thread, ...);
770
771/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700772 * @brief Assign a resource memory pool to a thread
773 *
774 * By default, threads have no resource pool assigned unless their parent
775 * thread has a resource pool, in which case it is inherited. Multiple
776 * threads may be assigned to the same memory pool.
777 *
778 * Changing a thread's resource pool will not migrate allocations from the
779 * previous pool.
780 *
781 * @param thread Target thread to assign a memory pool for resource requests,
782 * or NULL if the thread should no longer have a memory pool.
783 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400784 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700785 */
786static inline void k_thread_resource_pool_assign(struct k_thread *thread,
787 struct k_mem_pool *pool)
788{
789 thread->resource_pool = pool;
790}
791
792#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
793/**
794 * @brief Assign the system heap as a thread's resource pool
795 *
796 * Similar to k_thread_resource_pool_assign(), but the thread will use
797 * the kernel heap to draw memory.
798 *
799 * Use with caution, as a malicious thread could perform DoS attacks on the
800 * kernel heap.
801 *
802 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400803 *
804 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700805 */
806void k_thread_system_pool_assign(struct k_thread *thread);
807#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
808
809/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500810 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400811 *
Allan Stephensc98da842016-11-11 15:45:03 -0500812 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500813 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500815 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400816 *
817 * @return N/A
818 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700819__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400820
821/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500822 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400823 *
824 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500825 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400826 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827 * @return N/A
828 */
Kumar Galacc334c72017-04-21 10:55:34 -0500829extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830
831/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500832 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400833 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500834 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400835 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500836 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400837 *
838 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400839 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400840 */
Andrew Boie468190a2017-09-29 14:00:48 -0700841__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400842
843/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500844 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500846 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500848 * If @a thread is not currently sleeping, the routine has no effect.
849 *
850 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 *
852 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400853 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400854 */
Andrew Boie468190a2017-09-29 14:00:48 -0700855__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856
857/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500858 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500860 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400861 *
862 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400863 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700864__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865
866/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500867 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500869 * This routine prevents @a thread from executing if it has not yet started
870 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500872 * @param thread ID of thread to cancel.
873 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700874 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500875 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700876 *
877 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400878 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700879__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400880
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881/**
Allan Stephensc98da842016-11-11 15:45:03 -0500882 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500884 * This routine permanently stops execution of @a thread. The thread is taken
885 * off all kernel queues it is part of (i.e. the ready queue, the timeout
886 * queue, or a kernel object wait queue). However, any kernel resources the
887 * thread might currently own (such as mutexes or memory blocks) are not
888 * released. It is the responsibility of the caller of this routine to ensure
889 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500891 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400892 *
893 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400894 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400895 */
Andrew Boie468190a2017-09-29 14:00:48 -0700896__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400897
Andrew Boie7d627c52017-08-30 11:01:56 -0700898
899/**
900 * @brief Start an inactive thread
901 *
902 * If a thread was created with K_FOREVER in the delay parameter, it will
903 * not be added to the scheduling queue until this function is called
904 * on it.
905 *
906 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400907 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700908 */
Andrew Boie468190a2017-09-29 14:00:48 -0700909__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700910
Allan Stephensc98da842016-11-11 15:45:03 -0500911/**
912 * @cond INTERNAL_HIDDEN
913 */
914
Benjamin Walshd211a522016-12-06 11:44:01 -0500915/* timeout has timed out and is not on _timeout_q anymore */
916#define _EXPIRED (-2)
917
918/* timeout is not in use */
919#define _INACTIVE (-1)
920
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400921struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700922 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700923 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400924 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700925 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500926 void *init_p1;
927 void *init_p2;
928 void *init_p3;
929 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500930 u32_t init_options;
931 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500932 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400933};
934
Andrew Boied26cf2d2017-03-30 13:07:02 -0700935#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400936 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500937 prio, options, delay, abort, groups) \
938 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700939 .init_thread = (thread), \
940 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500941 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700942 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400943 .init_p1 = (void *)p1, \
944 .init_p2 = (void *)p2, \
945 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500946 .init_prio = (prio), \
947 .init_options = (options), \
948 .init_delay = (delay), \
949 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400950 }
951
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400952/**
Allan Stephensc98da842016-11-11 15:45:03 -0500953 * INTERNAL_HIDDEN @endcond
954 */
955
956/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500957 * @brief Statically define and initialize a thread.
958 *
959 * The thread may be scheduled for immediate execution or a delayed start.
960 *
961 * Thread options are architecture-specific, and can include K_ESSENTIAL,
962 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
963 * them using "|" (the logical OR operator).
964 *
965 * The ID of the thread can be accessed using:
966 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500967 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500968 *
969 * @param name Name of the thread.
970 * @param stack_size Stack size in bytes.
971 * @param entry Thread entry function.
972 * @param p1 1st entry point parameter.
973 * @param p2 2nd entry point parameter.
974 * @param p3 3rd entry point parameter.
975 * @param prio Thread priority.
976 * @param options Thread options.
977 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400978 *
Anas Nashif47420d02018-05-24 14:20:56 -0400979 * @req K-THREAD-010
980 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400981 * @internal It has been observed that the x86 compiler by default aligns
982 * these _static_thread_data structures to 32-byte boundaries, thereby
983 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400984 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400985 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500986#define K_THREAD_DEFINE(name, stack_size, \
987 entry, p1, p2, p3, \
988 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700989 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700990 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500991 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500992 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700993 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
994 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500995 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700996 NULL, 0); \
997 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400998
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400999/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001000 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001001 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001002 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001004 * @param thread ID of thread whose priority is needed.
1005 *
1006 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -04001007 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001008 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001009__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001010
1011/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001012 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001014 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001015 *
1016 * Rescheduling can occur immediately depending on the priority @a thread is
1017 * set to:
1018 *
1019 * - If its priority is raised above the priority of the caller of this
1020 * function, and the caller is preemptible, @a thread will be scheduled in.
1021 *
1022 * - If the caller operates on itself, it lowers its priority below that of
1023 * other threads in the system, and the caller is preemptible, the thread of
1024 * highest priority will be scheduled in.
1025 *
1026 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1027 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1028 * highest priority.
1029 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001030 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001031 * @param prio New priority.
1032 *
1033 * @warning Changing the priority of a thread currently involved in mutex
1034 * priority inheritance may result in undefined behavior.
1035 *
1036 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001037 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001038 */
Andrew Boie468190a2017-09-29 14:00:48 -07001039__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001040
Andy Ross4a2e50f2018-05-15 11:06:25 -07001041
1042#ifdef CONFIG_SCHED_DEADLINE
1043/**
1044 * @brief Set deadline expiration time for scheduler
1045 *
1046 * This sets the "deadline" expiration as a time delta from the
1047 * current time, in the same units used by k_cycle_get_32(). The
1048 * scheduler (when deadline scheduling is enabled) will choose the
1049 * next expiring thread when selecting between threads at the same
1050 * static priority. Threads at different priorities will be scheduled
1051 * according to their static priority.
1052 *
1053 * @note Deadlines that are negative (i.e. in the past) are still seen
1054 * as higher priority than others, even if the thread has "finished"
1055 * its work. If you don't want it scheduled anymore, you have to
1056 * reset the deadline into the future, block/pend the thread, or
1057 * modify its priority with k_thread_priority_set().
1058 *
1059 * @note Despite the API naming, the scheduler makes no guarantees the
1060 * the thread WILL be scheduled within that deadline, nor does it take
1061 * extra metadata (like e.g. the "runtime" and "period" parameters in
1062 * Linux sched_setattr()) that allows the kernel to validate the
1063 * scheduling for achievability. Such features could be implemented
1064 * above this call, which is simply input to the priority selection
1065 * logic.
1066 *
1067 * @param thread A thread on which to set the deadline
1068 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001069 *
1070 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001071 */
1072__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1073#endif
1074
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001075/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001076 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001077 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001078 * This routine prevents the kernel scheduler from making @a thread the
1079 * current thread. All other internal operations on @a thread are still
1080 * performed; for example, any timeout it is waiting on keeps ticking,
1081 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001082 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001083 * If @a thread is already suspended, the routine has no effect.
1084 *
1085 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001086 *
1087 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001088 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001089 */
Andrew Boie468190a2017-09-29 14:00:48 -07001090__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001091
1092/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001093 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001095 * This routine allows the kernel scheduler to make @a thread the current
1096 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001097 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001098 * If @a thread is not currently suspended, the routine has no effect.
1099 *
1100 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001101 *
1102 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001103 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001104 */
Andrew Boie468190a2017-09-29 14:00:48 -07001105__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001106
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001107/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001108 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001110 * This routine specifies how the scheduler will perform time slicing of
1111 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001112 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001113 * To enable time slicing, @a slice must be non-zero. The scheduler
1114 * ensures that no thread runs for more than the specified time limit
1115 * before other threads of that priority are given a chance to execute.
1116 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001117 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001119 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001120 * execute. Once the scheduler selects a thread for execution, there is no
1121 * minimum guaranteed time the thread will execute before threads of greater or
1122 * equal priority are scheduled.
1123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001124 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001125 * for execution, this routine has no effect; the thread is immediately
1126 * rescheduled after the slice period expires.
1127 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001128 * To disable timeslicing, set both @a slice and @a prio to zero.
1129 *
1130 * @param slice Maximum time slice length (in milliseconds).
1131 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001132 *
1133 * @return N/A
1134 */
Kumar Galacc334c72017-04-21 10:55:34 -05001135extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001136
Anas Nashif166f5192018-02-25 08:02:36 -06001137/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001138
1139/**
1140 * @addtogroup isr_apis
1141 * @{
1142 */
1143
1144/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001145 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001146 *
Allan Stephensc98da842016-11-11 15:45:03 -05001147 * This routine allows the caller to customize its actions, depending on
1148 * whether it is a thread or an ISR.
1149 *
1150 * @note Can be called by ISRs.
1151 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001152 * @return 0 if invoked by a thread.
1153 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001154 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001155extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001156
Benjamin Walsh445830d2016-11-10 15:54:27 -05001157/**
1158 * @brief Determine if code is running in a preemptible thread.
1159 *
Allan Stephensc98da842016-11-11 15:45:03 -05001160 * This routine allows the caller to customize its actions, depending on
1161 * whether it can be preempted by another thread. The routine returns a 'true'
1162 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001163 *
Allan Stephensc98da842016-11-11 15:45:03 -05001164 * - The code is running in a thread, not at ISR.
1165 * - The thread's priority is in the preemptible range.
1166 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001167 *
Allan Stephensc98da842016-11-11 15:45:03 -05001168 * @note Can be called by ISRs.
1169 *
1170 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001171 * @return Non-zero if invoked by a preemptible thread.
1172 */
Andrew Boie468190a2017-09-29 14:00:48 -07001173__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001174
Allan Stephensc98da842016-11-11 15:45:03 -05001175/**
Anas Nashif166f5192018-02-25 08:02:36 -06001176 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001177 */
1178
1179/**
1180 * @addtogroup thread_apis
1181 * @{
1182 */
1183
1184/**
1185 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001186 *
Allan Stephensc98da842016-11-11 15:45:03 -05001187 * This routine prevents the current thread from being preempted by another
1188 * thread by instructing the scheduler to treat it as a cooperative thread.
1189 * If the thread subsequently performs an operation that makes it unready,
1190 * it will be context switched out in the normal manner. When the thread
1191 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001192 *
Allan Stephensc98da842016-11-11 15:45:03 -05001193 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001194 *
Allan Stephensc98da842016-11-11 15:45:03 -05001195 * @note k_sched_lock() and k_sched_unlock() should normally be used
1196 * when the operation being performed can be safely interrupted by ISRs.
1197 * However, if the amount of processing involved is very small, better
1198 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001199 *
1200 * @return N/A
1201 */
1202extern void k_sched_lock(void);
1203
Allan Stephensc98da842016-11-11 15:45:03 -05001204/**
1205 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001206 *
Allan Stephensc98da842016-11-11 15:45:03 -05001207 * This routine reverses the effect of a previous call to k_sched_lock().
1208 * A thread must call the routine once for each time it called k_sched_lock()
1209 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001210 *
1211 * @return N/A
1212 */
1213extern void k_sched_unlock(void);
1214
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001215/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001216 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001218 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001219 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001220 * Custom data is not used by the kernel itself, and is freely available
1221 * for a thread to use as it sees fit. It can be used as a framework
1222 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001223 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001224 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001225 *
1226 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001227 *
1228 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001229 */
Andrew Boie468190a2017-09-29 14:00:48 -07001230__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001231
1232/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001233 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001235 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001236 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001237 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001238 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001239 */
Andrew Boie468190a2017-09-29 14:00:48 -07001240__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001241
1242/**
Anas Nashif166f5192018-02-25 08:02:36 -06001243 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001244 */
1245
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001246#include <sys_clock.h>
1247
Allan Stephensc2f15a42016-11-17 12:24:22 -05001248/**
1249 * @addtogroup clock_apis
1250 * @{
1251 */
1252
1253/**
1254 * @brief Generate null timeout delay.
1255 *
1256 * This macro generates a timeout delay that that instructs a kernel API
1257 * not to wait if the requested operation cannot be performed immediately.
1258 *
1259 * @return Timeout delay value.
1260 */
1261#define K_NO_WAIT 0
1262
1263/**
1264 * @brief Generate timeout delay from milliseconds.
1265 *
1266 * This macro generates a timeout delay that that instructs a kernel API
1267 * to wait up to @a ms milliseconds to perform the requested operation.
1268 *
1269 * @param ms Duration in milliseconds.
1270 *
1271 * @return Timeout delay value.
1272 */
Johan Hedberg14471692016-11-13 10:52:15 +02001273#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001274
1275/**
1276 * @brief Generate timeout delay from seconds.
1277 *
1278 * This macro generates a timeout delay that that instructs a kernel API
1279 * to wait up to @a s seconds to perform the requested operation.
1280 *
1281 * @param s Duration in seconds.
1282 *
1283 * @return Timeout delay value.
1284 */
Johan Hedberg14471692016-11-13 10:52:15 +02001285#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001286
1287/**
1288 * @brief Generate timeout delay from minutes.
1289 *
1290 * This macro generates a timeout delay that that instructs a kernel API
1291 * to wait up to @a m minutes to perform the requested operation.
1292 *
1293 * @param m Duration in minutes.
1294 *
1295 * @return Timeout delay value.
1296 */
Johan Hedberg14471692016-11-13 10:52:15 +02001297#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001298
1299/**
1300 * @brief Generate timeout delay from hours.
1301 *
1302 * This macro generates a timeout delay that that instructs a kernel API
1303 * to wait up to @a h hours to perform the requested operation.
1304 *
1305 * @param h Duration in hours.
1306 *
1307 * @return Timeout delay value.
1308 */
Johan Hedberg14471692016-11-13 10:52:15 +02001309#define K_HOURS(h) K_MINUTES((h) * 60)
1310
Allan Stephensc98da842016-11-11 15:45:03 -05001311/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001312 * @brief Generate infinite timeout delay.
1313 *
1314 * This macro generates a timeout delay that that instructs a kernel API
1315 * to wait as long as necessary to perform the requested operation.
1316 *
1317 * @return Timeout delay value.
1318 */
1319#define K_FOREVER (-1)
1320
1321/**
Anas Nashif166f5192018-02-25 08:02:36 -06001322 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001323 */
1324
1325/**
Allan Stephensc98da842016-11-11 15:45:03 -05001326 * @cond INTERNAL_HIDDEN
1327 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001328
Benjamin Walsh62092182016-12-20 14:39:08 -05001329/* kernel clocks */
1330
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001331#ifdef CONFIG_SYS_CLOCK_EXISTS
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001332
1333/*
1334 * If timer frequency is known at compile time, a simple (32-bit)
1335 * tick <-> ms conversion could be used for some combinations of
1336 * hardware timer frequency and tick rate. Otherwise precise
1337 * (64-bit) calculations are used.
1338 */
1339
1340#if !defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
1341#if (sys_clock_hw_cycles_per_sec % sys_clock_ticks_per_sec) != 0
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001342 #define _NEED_PRECISE_TICK_MS_CONVERSION
1343#elif (MSEC_PER_SEC % sys_clock_ticks_per_sec) != 0
Benjamin Walsh62092182016-12-20 14:39:08 -05001344 #define _NON_OPTIMIZED_TICKS_PER_SEC
1345#endif
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001346#endif
Benjamin Walsh62092182016-12-20 14:39:08 -05001347
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001348#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
1349 defined(_NON_OPTIMIZED_TICKS_PER_SEC)
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001350 #define _NEED_PRECISE_TICK_MS_CONVERSION
1351#endif
1352#endif
1353
Kumar Galacc334c72017-04-21 10:55:34 -05001354static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001355{
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001356#ifdef CONFIG_SYS_CLOCK_EXISTS
1357
1358#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
1359 /* use 64-bit math to keep precision */
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001360 s64_t ms_ticks_per_sec = (s64_t)ms * sys_clock_ticks_per_sec;
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001361
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001362 return (s32_t)ceiling_fraction(ms_ticks_per_sec, MSEC_PER_SEC);
1363#else
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001364 /* simple division keeps precision */
1365 s32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
1366
1367 return (s32_t)ceiling_fraction(ms, ms_per_tick);
1368#endif
1369
1370#else
1371 __ASSERT(ms == 0, "ms not zero");
1372 return 0;
Benjamin Walsh62092182016-12-20 14:39:08 -05001373#endif
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001374}
Benjamin Walsh62092182016-12-20 14:39:08 -05001375
Kumar Galacc334c72017-04-21 10:55:34 -05001376static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001377{
Benjamin Walsh62092182016-12-20 14:39:08 -05001378#ifdef CONFIG_SYS_CLOCK_EXISTS
1379
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001380#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
1381 /* use 64-bit math to keep precision */
1382 return (u64_t)ticks * sys_clock_hw_cycles_per_tick * MSEC_PER_SEC /
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001383 sys_clock_hw_cycles_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001384#else
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001385 /* simple multiplication keeps precision */
1386 u32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
1387
1388 return (u64_t)ticks * ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001389#endif
1390
1391#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001392 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001393 return 0;
1394#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001395}
1396
Piotr Zięcik77f42f82018-06-11 14:26:29 +02001397/* added tick needed to account for tick in progress */
1398#ifdef CONFIG_TICKLESS_KERNEL
1399#define _TICK_ALIGN 0
1400#else
1401#define _TICK_ALIGN 1
1402#endif
1403
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001404struct k_timer {
1405 /*
1406 * _timeout structure must be first here if we want to use
1407 * dynamic timer allocation. timeout.node is used in the double-linked
1408 * list of free timers
1409 */
1410 struct _timeout timeout;
1411
Allan Stephens45bfa372016-10-12 12:39:42 -05001412 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001413 _wait_q_t wait_q;
1414
1415 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001416 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001417
1418 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001419 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001420
1421 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001422 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001423
Allan Stephens45bfa372016-10-12 12:39:42 -05001424 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001425 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001426
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001427 /* user-specific data, also used to support legacy features */
1428 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001429
Anas Nashif2f203c22016-12-18 06:57:45 -05001430 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001431};
1432
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001433#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001434 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001435 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001436 .timeout.wait_q = NULL, \
1437 .timeout.thread = NULL, \
1438 .timeout.func = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001439 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001440 .expiry_fn = expiry, \
1441 .stop_fn = stop, \
1442 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001443 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001444 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001445 }
1446
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001447#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1448
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001449/**
Allan Stephensc98da842016-11-11 15:45:03 -05001450 * INTERNAL_HIDDEN @endcond
1451 */
1452
1453/**
1454 * @defgroup timer_apis Timer APIs
1455 * @ingroup kernel_apis
1456 * @{
1457 */
1458
1459/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001460 * @typedef k_timer_expiry_t
1461 * @brief Timer expiry function type.
1462 *
1463 * A timer's expiry function is executed by the system clock interrupt handler
1464 * each time the timer expires. The expiry function is optional, and is only
1465 * invoked if the timer has been initialized with one.
1466 *
1467 * @param timer Address of timer.
1468 *
1469 * @return N/A
1470 */
1471typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1472
1473/**
1474 * @typedef k_timer_stop_t
1475 * @brief Timer stop function type.
1476 *
1477 * A timer's stop function is executed if the timer is stopped prematurely.
1478 * The function runs in the context of the thread that stops the timer.
1479 * The stop function is optional, and is only invoked if the timer has been
1480 * initialized with one.
1481 *
1482 * @param timer Address of timer.
1483 *
1484 * @return N/A
1485 */
1486typedef void (*k_timer_stop_t)(struct k_timer *timer);
1487
1488/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001489 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001490 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001491 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001492 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001493 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001494 *
1495 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001496 * @param expiry_fn Function to invoke each time the timer expires.
1497 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001498 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001499#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001500 struct k_timer name \
1501 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001502 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001503
Allan Stephens45bfa372016-10-12 12:39:42 -05001504/**
1505 * @brief Initialize a timer.
1506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001507 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001508 *
1509 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001510 * @param expiry_fn Function to invoke each time the timer expires.
1511 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001512 *
1513 * @return N/A
1514 */
1515extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001516 k_timer_expiry_t expiry_fn,
1517 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001518
Allan Stephens45bfa372016-10-12 12:39:42 -05001519/**
1520 * @brief Start a timer.
1521 *
1522 * This routine starts a timer, and resets its status to zero. The timer
1523 * begins counting down using the specified duration and period values.
1524 *
1525 * Attempting to start a timer that is already running is permitted.
1526 * The timer's status is reset to zero and the timer begins counting down
1527 * using the new duration and period values.
1528 *
1529 * @param timer Address of timer.
1530 * @param duration Initial timer duration (in milliseconds).
1531 * @param period Timer period (in milliseconds).
1532 *
1533 * @return N/A
1534 */
Andrew Boiea354d492017-09-29 16:22:28 -07001535__syscall void k_timer_start(struct k_timer *timer,
1536 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001537
1538/**
1539 * @brief Stop a timer.
1540 *
1541 * This routine stops a running timer prematurely. The timer's stop function,
1542 * if one exists, is invoked by the caller.
1543 *
1544 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001545 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001546 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001547 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1548 * if @a k_timer_stop is to be called from ISRs.
1549 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001550 * @param timer Address of timer.
1551 *
1552 * @return N/A
1553 */
Andrew Boiea354d492017-09-29 16:22:28 -07001554__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001555
1556/**
1557 * @brief Read timer status.
1558 *
1559 * This routine reads the timer's status, which indicates the number of times
1560 * it has expired since its status was last read.
1561 *
1562 * Calling this routine resets the timer's status to zero.
1563 *
1564 * @param timer Address of timer.
1565 *
1566 * @return Timer status.
1567 */
Andrew Boiea354d492017-09-29 16:22:28 -07001568__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001569
1570/**
1571 * @brief Synchronize thread to timer expiration.
1572 *
1573 * This routine blocks the calling thread until the timer's status is non-zero
1574 * (indicating that it has expired at least once since it was last examined)
1575 * or the timer is stopped. If the timer status is already non-zero,
1576 * or the timer is already stopped, the caller continues without waiting.
1577 *
1578 * Calling this routine resets the timer's status to zero.
1579 *
1580 * This routine must not be used by interrupt handlers, since they are not
1581 * allowed to block.
1582 *
1583 * @param timer Address of timer.
1584 *
1585 * @return Timer status.
1586 */
Andrew Boiea354d492017-09-29 16:22:28 -07001587__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001588
1589/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001590 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001591 *
1592 * This routine computes the (approximate) time remaining before a running
1593 * timer next expires. If the timer is not running, it returns zero.
1594 *
1595 * @param timer Address of timer.
1596 *
1597 * @return Remaining time (in milliseconds).
1598 */
Andrew Boiea354d492017-09-29 16:22:28 -07001599__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1600
1601static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001602{
1603 return _timeout_remaining_get(&timer->timeout);
1604}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001605
Allan Stephensc98da842016-11-11 15:45:03 -05001606/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001607 * @brief Associate user-specific data with a timer.
1608 *
1609 * This routine records the @a user_data with the @a timer, to be retrieved
1610 * later.
1611 *
1612 * It can be used e.g. in a timer handler shared across multiple subsystems to
1613 * retrieve data specific to the subsystem this timer is associated with.
1614 *
1615 * @param timer Address of timer.
1616 * @param user_data User data to associate with the timer.
1617 *
1618 * @return N/A
1619 */
Andrew Boiea354d492017-09-29 16:22:28 -07001620__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1621
Anas Nashif954d5502018-02-25 08:37:28 -06001622/**
1623 * @internal
1624 */
Andrew Boiea354d492017-09-29 16:22:28 -07001625static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1626 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001627{
1628 timer->user_data = user_data;
1629}
1630
1631/**
1632 * @brief Retrieve the user-specific data from a timer.
1633 *
1634 * @param timer Address of timer.
1635 *
1636 * @return The user data.
1637 */
Andrew Boiea354d492017-09-29 16:22:28 -07001638__syscall void *k_timer_user_data_get(struct k_timer *timer);
1639
1640static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001641{
1642 return timer->user_data;
1643}
1644
Anas Nashif166f5192018-02-25 08:02:36 -06001645/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001646
Allan Stephensc98da842016-11-11 15:45:03 -05001647/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001648 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001649 * @{
1650 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001651
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001652/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001653 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001654 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001655 * This routine returns the elapsed time since the system booted,
1656 * in milliseconds.
1657 *
1658 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001659 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001660__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001661
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001662/**
1663 * @brief Enable clock always on in tickless kernel
1664 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001665 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001666 * there are no timer events programmed in tickless kernel
1667 * scheduling. This is necessary if the clock is used to track
1668 * passage of time.
1669 *
1670 * @retval prev_status Previous status of always on flag
1671 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301672#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001673static inline int k_enable_sys_clock_always_on(void)
1674{
1675 int prev_status = _sys_clock_always_on;
1676
1677 _sys_clock_always_on = 1;
1678 _enable_sys_clock();
1679
1680 return prev_status;
1681}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301682#else
1683#define k_enable_sys_clock_always_on() do { } while ((0))
1684#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001685
1686/**
1687 * @brief Disable clock always on in tickless kernel
1688 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001689 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001690 * there are no timer events programmed in tickless kernel
1691 * scheduling. To save power, this routine should be called
1692 * immediately when clock is not used to track time.
1693 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301694#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001695static inline void k_disable_sys_clock_always_on(void)
1696{
1697 _sys_clock_always_on = 0;
1698}
1699#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001700#define k_disable_sys_clock_always_on() do { } while ((0))
1701#endif
1702
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001703/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001704 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001706 * This routine returns the lower 32-bits of the elapsed time since the system
1707 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001708 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001709 * This routine can be more efficient than k_uptime_get(), as it reduces the
1710 * need for interrupt locking and 64-bit math. However, the 32-bit result
1711 * cannot hold a system uptime time larger than approximately 50 days, so the
1712 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001713 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001714 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001715 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001716__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001717
1718/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001719 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001720 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001721 * This routine computes the elapsed time between the current system uptime
1722 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001723 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001724 * @param reftime Pointer to a reference time, which is updated to the current
1725 * uptime upon return.
1726 *
1727 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001728 */
Kumar Galacc334c72017-04-21 10:55:34 -05001729extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001730
1731/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001732 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001734 * This routine computes the elapsed time between the current system uptime
1735 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001737 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1738 * need for interrupt locking and 64-bit math. However, the 32-bit result
1739 * cannot hold an elapsed time larger than approximately 50 days, so the
1740 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001742 * @param reftime Pointer to a reference time, which is updated to the current
1743 * uptime upon return.
1744 *
1745 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001746 */
Kumar Galacc334c72017-04-21 10:55:34 -05001747extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001748
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001749/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001750 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001752 * This routine returns the current time, as measured by the system's hardware
1753 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001755 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001756 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001757#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001758
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001759/**
Anas Nashif166f5192018-02-25 08:02:36 -06001760 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001761 */
1762
Allan Stephensc98da842016-11-11 15:45:03 -05001763/**
1764 * @cond INTERNAL_HIDDEN
1765 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001766
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001767struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001768 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001769 union {
1770 _wait_q_t wait_q;
1771
1772 _POLL_EVENT;
1773 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001774
1775 _OBJECT_TRACING_NEXT_PTR(k_queue);
1776};
1777
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001778#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001779 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001780 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001781 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001782 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001783 _OBJECT_TRACING_INIT \
1784 }
1785
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001786#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1787
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001788extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1789
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001790/**
1791 * INTERNAL_HIDDEN @endcond
1792 */
1793
1794/**
1795 * @defgroup queue_apis Queue APIs
1796 * @ingroup kernel_apis
1797 * @{
1798 */
1799
1800/**
1801 * @brief Initialize a queue.
1802 *
1803 * This routine initializes a queue object, prior to its first use.
1804 *
1805 * @param queue Address of the queue.
1806 *
1807 * @return N/A
1808 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001809__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001810
1811/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001812 * @brief Cancel waiting on a queue.
1813 *
1814 * This routine causes first thread pending on @a queue, if any, to
1815 * return from k_queue_get() call with NULL value (as if timeout expired).
1816 *
1817 * @note Can be called by ISRs.
1818 *
1819 * @param queue Address of the queue.
1820 *
1821 * @return N/A
1822 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001823__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001824
1825/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001826 * @brief Append an element to the end of a queue.
1827 *
1828 * This routine appends a data item to @a queue. A queue data item must be
1829 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1830 * reserved for the kernel's use.
1831 *
1832 * @note Can be called by ISRs.
1833 *
1834 * @param queue Address of the queue.
1835 * @param data Address of the data item.
1836 *
1837 * @return N/A
1838 */
1839extern void k_queue_append(struct k_queue *queue, void *data);
1840
1841/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001842 * @brief Append an element to a queue.
1843 *
1844 * This routine appends a data item to @a queue. There is an implicit
1845 * memory allocation from the calling thread's resource pool, which is
1846 * automatically freed when the item is removed from the queue.
1847 *
1848 * @note Can be called by ISRs.
1849 *
1850 * @param queue Address of the queue.
1851 * @param data Address of the data item.
1852 *
1853 * @retval 0 on success
1854 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1855 */
1856__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1857
1858/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001859 * @brief Prepend an element to a queue.
1860 *
1861 * This routine prepends a data item to @a queue. A queue data item must be
1862 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1863 * reserved for the kernel's use.
1864 *
1865 * @note Can be called by ISRs.
1866 *
1867 * @param queue Address of the queue.
1868 * @param data Address of the data item.
1869 *
1870 * @return N/A
1871 */
1872extern void k_queue_prepend(struct k_queue *queue, void *data);
1873
1874/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001875 * @brief Prepend an element to a queue.
1876 *
1877 * This routine prepends a data item to @a queue. There is an implicit
1878 * memory allocation from the calling thread's resource pool, which is
1879 * automatically freed when the item is removed from the queue.
1880 *
1881 * @note Can be called by ISRs.
1882 *
1883 * @param queue Address of the queue.
1884 * @param data Address of the data item.
1885 *
1886 * @retval 0 on success
1887 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1888 */
1889__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1890
1891/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001892 * @brief Inserts an element to a queue.
1893 *
1894 * This routine inserts a data item to @a queue after previous item. A queue
1895 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1896 * item are reserved for the kernel's use.
1897 *
1898 * @note Can be called by ISRs.
1899 *
1900 * @param queue Address of the queue.
1901 * @param prev Address of the previous data item.
1902 * @param data Address of the data item.
1903 *
1904 * @return N/A
1905 */
1906extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1907
1908/**
1909 * @brief Atomically append a list of elements to a queue.
1910 *
1911 * This routine adds a list of data items to @a queue in one operation.
1912 * The data items must be in a singly-linked list, with the first 32 bits
1913 * in each data item pointing to the next data item; the list must be
1914 * NULL-terminated.
1915 *
1916 * @note Can be called by ISRs.
1917 *
1918 * @param queue Address of the queue.
1919 * @param head Pointer to first node in singly-linked list.
1920 * @param tail Pointer to last node in singly-linked list.
1921 *
1922 * @return N/A
1923 */
1924extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1925
1926/**
1927 * @brief Atomically add a list of elements to a queue.
1928 *
1929 * This routine adds a list of data items to @a queue in one operation.
1930 * The data items must be in a singly-linked list implemented using a
1931 * sys_slist_t object. Upon completion, the original list is empty.
1932 *
1933 * @note Can be called by ISRs.
1934 *
1935 * @param queue Address of the queue.
1936 * @param list Pointer to sys_slist_t object.
1937 *
1938 * @return N/A
1939 */
1940extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1941
1942/**
1943 * @brief Get an element from a queue.
1944 *
1945 * This routine removes first data item from @a queue. The first 32 bits of the
1946 * data item are reserved for the kernel's use.
1947 *
1948 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1949 *
1950 * @param queue Address of the queue.
1951 * @param timeout Waiting period to obtain a data item (in milliseconds),
1952 * or one of the special values K_NO_WAIT and K_FOREVER.
1953 *
1954 * @return Address of the data item if successful; NULL if returned
1955 * without waiting, or waiting period timed out.
1956 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001957__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001958
1959/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001960 * @brief Remove an element from a queue.
1961 *
1962 * This routine removes data item from @a queue. The first 32 bits of the
1963 * data item are reserved for the kernel's use. Removing elements from k_queue
1964 * rely on sys_slist_find_and_remove which is not a constant time operation.
1965 *
1966 * @note Can be called by ISRs
1967 *
1968 * @param queue Address of the queue.
1969 * @param data Address of the data item.
1970 *
1971 * @return true if data item was removed
1972 */
1973static inline bool k_queue_remove(struct k_queue *queue, void *data)
1974{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001975 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001976}
1977
1978/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001979 * @brief Query a queue to see if it has data available.
1980 *
1981 * Note that the data might be already gone by the time this function returns
1982 * if other threads are also trying to read from the queue.
1983 *
1984 * @note Can be called by ISRs.
1985 *
1986 * @param queue Address of the queue.
1987 *
1988 * @return Non-zero if the queue is empty.
1989 * @return 0 if data is available.
1990 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001991__syscall int k_queue_is_empty(struct k_queue *queue);
1992
1993static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001994{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001995 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001996}
1997
1998/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001999 * @brief Peek element at the head of queue.
2000 *
2001 * Return element from the head of queue without removing it.
2002 *
2003 * @param queue Address of the queue.
2004 *
2005 * @return Head element, or NULL if queue is empty.
2006 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002007__syscall void *k_queue_peek_head(struct k_queue *queue);
2008
2009static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002010{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002011 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002012}
2013
2014/**
2015 * @brief Peek element at the tail of queue.
2016 *
2017 * Return element from the tail of queue without removing it.
2018 *
2019 * @param queue Address of the queue.
2020 *
2021 * @return Tail element, or NULL if queue is empty.
2022 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002023__syscall void *k_queue_peek_tail(struct k_queue *queue);
2024
2025static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002026{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002027 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002028}
2029
2030/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002031 * @brief Statically define and initialize a queue.
2032 *
2033 * The queue can be accessed outside the module where it is defined using:
2034 *
2035 * @code extern struct k_queue <name>; @endcode
2036 *
2037 * @param name Name of the queue.
2038 */
2039#define K_QUEUE_DEFINE(name) \
2040 struct k_queue name \
2041 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002042 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002043
Anas Nashif166f5192018-02-25 08:02:36 -06002044/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002045
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002046struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002047 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002048};
2049
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002050/**
2051 * @cond INTERNAL_HIDDEN
2052 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002053#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002054 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002055 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002056 }
2057
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002058#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
2059
Allan Stephensc98da842016-11-11 15:45:03 -05002060/**
2061 * INTERNAL_HIDDEN @endcond
2062 */
2063
2064/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002065 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002066 * @ingroup kernel_apis
2067 * @{
2068 */
2069
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002070/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002071 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002072 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002073 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002074 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002075 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002076 *
2077 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002078 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002079 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002080#define k_fifo_init(fifo) \
2081 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002082
2083/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002084 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002085 *
2086 * This routine causes first thread pending on @a fifo, if any, to
2087 * return from k_fifo_get() call with NULL value (as if timeout
2088 * expired).
2089 *
2090 * @note Can be called by ISRs.
2091 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002092 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002093 *
2094 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002095 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002096 */
2097#define k_fifo_cancel_wait(fifo) \
2098 k_queue_cancel_wait((struct k_queue *) fifo)
2099
2100/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002101 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002102 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002103 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002104 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2105 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002107 * @note Can be called by ISRs.
2108 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002109 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002110 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002111 *
2112 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002113 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002114 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002115#define k_fifo_put(fifo, data) \
2116 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002117
2118/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002119 * @brief Add an element to a FIFO queue.
2120 *
2121 * This routine adds a data item to @a fifo. There is an implicit
2122 * memory allocation from the calling thread's resource pool, which is
2123 * automatically freed when the item is removed.
2124 *
2125 * @note Can be called by ISRs.
2126 *
2127 * @param fifo Address of the FIFO.
2128 * @param data Address of the data item.
2129 *
2130 * @retval 0 on success
2131 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002132 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002133 */
2134#define k_fifo_alloc_put(fifo, data) \
2135 k_queue_alloc_append((struct k_queue *) fifo, data)
2136
2137/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002138 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002139 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002140 * This routine adds a list of data items to @a fifo in one operation.
2141 * The data items must be in a singly-linked list, with the first 32 bits
2142 * each data item pointing to the next data item; the list must be
2143 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002144 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002145 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002146 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002147 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002148 * @param head Pointer to first node in singly-linked list.
2149 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002150 *
2151 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002152 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002153 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002154#define k_fifo_put_list(fifo, head, tail) \
2155 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002156
2157/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002158 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002159 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002160 * This routine adds a list of data items to @a fifo in one operation.
2161 * The data items must be in a singly-linked list implemented using a
2162 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002163 * and must be re-initialized via sys_slist_init().
2164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002165 * @note Can be called by ISRs.
2166 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002167 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002168 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002169 *
2170 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002171 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002172 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002173#define k_fifo_put_slist(fifo, list) \
2174 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002175
2176/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002177 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002179 * This routine removes a data item from @a fifo in a "first in, first out"
2180 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002182 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2183 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002184 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002185 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002186 * or one of the special values K_NO_WAIT and K_FOREVER.
2187 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002188 * @return Address of the data item if successful; NULL if returned
2189 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002190 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002191 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002192#define k_fifo_get(fifo, timeout) \
2193 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002194
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002195/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002196 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002197 *
2198 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002199 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002200 *
2201 * @note Can be called by ISRs.
2202 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002203 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002204 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002205 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002206 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002207 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002208 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002209#define k_fifo_is_empty(fifo) \
2210 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002211
2212/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002213 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002214 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002215 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302216 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002217 * on each iteration of processing, a head container will be peeked,
2218 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002219 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002220 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002221 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002222 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002223 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002224 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002225 */
2226#define k_fifo_peek_head(fifo) \
2227 k_queue_peek_head((struct k_queue *) fifo)
2228
2229/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002230 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002231 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002232 * Return element from the tail of FIFO queue (without removing it). A usecase
2233 * for this is if elements of the FIFO queue are themselves containers. Then
2234 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002235 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002236 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002237 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002238 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002239 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002240 */
2241#define k_fifo_peek_tail(fifo) \
2242 k_queue_peek_tail((struct k_queue *) fifo)
2243
2244/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002245 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002246 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002247 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002248 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002249 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002250 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002251 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002252 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002253 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002254#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002255 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002256 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002257 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002258
Anas Nashif166f5192018-02-25 08:02:36 -06002259/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002260
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002261struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002262 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002263};
2264
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002265/**
2266 * @cond INTERNAL_HIDDEN
2267 */
2268
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002269#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002270 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002271 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002272 }
2273
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002274#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2275
Allan Stephensc98da842016-11-11 15:45:03 -05002276/**
2277 * INTERNAL_HIDDEN @endcond
2278 */
2279
2280/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002281 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002282 * @ingroup kernel_apis
2283 * @{
2284 */
2285
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002286/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002287 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002288 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002289 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002290 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002291 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002292 *
2293 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002294 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002295 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002296#define k_lifo_init(lifo) \
2297 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002298
2299/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002300 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002301 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002302 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002303 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2304 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002305 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002306 * @note Can be called by ISRs.
2307 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002308 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002309 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002310 *
2311 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002312 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002313 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002314#define k_lifo_put(lifo, data) \
2315 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002316
2317/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002318 * @brief Add an element to a LIFO queue.
2319 *
2320 * This routine adds a data item to @a lifo. There is an implicit
2321 * memory allocation from the calling thread's resource pool, which is
2322 * automatically freed when the item is removed.
2323 *
2324 * @note Can be called by ISRs.
2325 *
2326 * @param lifo Address of the LIFO.
2327 * @param data Address of the data item.
2328 *
2329 * @retval 0 on success
2330 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002331 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002332 */
2333#define k_lifo_alloc_put(lifo, data) \
2334 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2335
2336/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002337 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002338 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002339 * This routine removes a data item from @a lifo in a "last in, first out"
2340 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002341 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002342 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2343 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002344 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002345 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002346 * or one of the special values K_NO_WAIT and K_FOREVER.
2347 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002348 * @return Address of the data item if successful; NULL if returned
2349 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002350 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002351 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002352#define k_lifo_get(lifo, timeout) \
2353 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002354
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002355/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002356 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002357 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002358 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002359 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002360 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002361 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002362 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002363 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002364 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002365#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002366 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002367 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002368 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002369
Anas Nashif166f5192018-02-25 08:02:36 -06002370/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002371
2372/**
2373 * @cond INTERNAL_HIDDEN
2374 */
Andrew Boief3bee952018-05-02 17:44:39 -07002375#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002376
2377struct k_stack {
2378 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002379 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002380
Anas Nashif2f203c22016-12-18 06:57:45 -05002381 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002382 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002383};
2384
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002385#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002386 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002387 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002388 .base = stack_buffer, \
2389 .next = stack_buffer, \
2390 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002391 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002392 }
2393
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002394#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2395
Allan Stephensc98da842016-11-11 15:45:03 -05002396/**
2397 * INTERNAL_HIDDEN @endcond
2398 */
2399
2400/**
2401 * @defgroup stack_apis Stack APIs
2402 * @ingroup kernel_apis
2403 * @{
2404 */
2405
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002406/**
2407 * @brief Initialize a stack.
2408 *
2409 * This routine initializes a stack object, prior to its first use.
2410 *
2411 * @param stack Address of the stack.
2412 * @param buffer Address of array used to hold stacked values.
2413 * @param num_entries Maximum number of values that can be stacked.
2414 *
2415 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002416 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002417 */
Andrew Boief3bee952018-05-02 17:44:39 -07002418void k_stack_init(struct k_stack *stack,
2419 u32_t *buffer, unsigned int num_entries);
2420
2421
2422/**
2423 * @brief Initialize a stack.
2424 *
2425 * This routine initializes a stack object, prior to its first use. Internal
2426 * buffers will be allocated from the calling thread's resource pool.
2427 * This memory will be released if k_stack_cleanup() is called, or
2428 * userspace is enabled and the stack object loses all references to it.
2429 *
2430 * @param stack Address of the stack.
2431 * @param num_entries Maximum number of values that can be stacked.
2432 *
2433 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002434 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002435 */
2436
2437__syscall int k_stack_alloc_init(struct k_stack *stack,
2438 unsigned int num_entries);
2439
2440/**
2441 * @brief Release a stack's allocated buffer
2442 *
2443 * If a stack object was given a dynamically allocated buffer via
2444 * k_stack_alloc_init(), this will free it. This function does nothing
2445 * if the buffer wasn't dynamically allocated.
2446 *
2447 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002448 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002449 */
2450void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002451
2452/**
2453 * @brief Push an element onto a stack.
2454 *
2455 * This routine adds a 32-bit value @a data to @a stack.
2456 *
2457 * @note Can be called by ISRs.
2458 *
2459 * @param stack Address of the stack.
2460 * @param data Value to push onto the stack.
2461 *
2462 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002463 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002464 */
Andrew Boiee8734462017-09-29 16:42:07 -07002465__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002466
2467/**
2468 * @brief Pop an element from a stack.
2469 *
2470 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2471 * manner and stores the value in @a data.
2472 *
2473 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2474 *
2475 * @param stack Address of the stack.
2476 * @param data Address of area to hold the value popped from the stack.
2477 * @param timeout Waiting period to obtain a value (in milliseconds),
2478 * or one of the special values K_NO_WAIT and K_FOREVER.
2479 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002480 * @retval 0 Element popped from stack.
2481 * @retval -EBUSY Returned without waiting.
2482 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002483 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002484 */
Andrew Boiee8734462017-09-29 16:42:07 -07002485__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002486
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002487/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002488 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002489 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002490 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002491 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002492 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002493 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002494 * @param name Name of the stack.
2495 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002496 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002497 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002498#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002499 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002500 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002501 struct k_stack name \
2502 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002503 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002504 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002505
Anas Nashif166f5192018-02-25 08:02:36 -06002506/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002507
Allan Stephens6bba9b02016-11-16 14:56:54 -05002508struct k_work;
2509
Allan Stephensc98da842016-11-11 15:45:03 -05002510/**
2511 * @defgroup workqueue_apis Workqueue Thread APIs
2512 * @ingroup kernel_apis
2513 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002514 */
2515
Allan Stephens6bba9b02016-11-16 14:56:54 -05002516/**
2517 * @typedef k_work_handler_t
2518 * @brief Work item handler function type.
2519 *
2520 * A work item's handler function is executed by a workqueue's thread
2521 * when the work item is processed by the workqueue.
2522 *
2523 * @param work Address of the work item.
2524 *
2525 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002526 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002527 */
2528typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002529
2530/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002531 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002532 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002533
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002534struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002535 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002536 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002537};
2538
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002539enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002540 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002541};
2542
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002543struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002544 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002545 k_work_handler_t handler;
2546 atomic_t flags[1];
2547};
2548
Allan Stephens6bba9b02016-11-16 14:56:54 -05002549struct k_delayed_work {
2550 struct k_work work;
2551 struct _timeout timeout;
2552 struct k_work_q *work_q;
2553};
2554
2555extern struct k_work_q k_sys_work_q;
2556
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002557/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002558 * INTERNAL_HIDDEN @endcond
2559 */
2560
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002561#define _K_WORK_INITIALIZER(work_handler) \
2562 { \
2563 ._reserved = NULL, \
2564 .handler = work_handler, \
2565 .flags = { 0 } \
2566 }
2567
2568#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2569
Allan Stephens6bba9b02016-11-16 14:56:54 -05002570/**
2571 * @brief Initialize a statically-defined work item.
2572 *
2573 * This macro can be used to initialize a statically-defined workqueue work
2574 * item, prior to its first use. For example,
2575 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002576 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002577 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002578 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002579 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002580 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002581 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002582#define K_WORK_DEFINE(work, work_handler) \
2583 struct k_work work \
2584 __in_section(_k_work, static, work) = \
2585 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002586
2587/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002588 * @brief Initialize a work item.
2589 *
2590 * This routine initializes a workqueue work item, prior to its first use.
2591 *
2592 * @param work Address of work item.
2593 * @param handler Function to invoke each time work item is processed.
2594 *
2595 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002596 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002597 */
2598static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2599{
Leandro Pereira0e23ad82018-05-29 10:42:17 -07002600 *work = (struct k_work)_K_WORK_INITIALIZER(handler);
Andrew Boie945af952017-08-22 13:15:23 -07002601 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002602}
2603
2604/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002605 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002606 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002607 * This routine submits work item @a work to be processed by workqueue
2608 * @a work_q. If the work item is already pending in the workqueue's queue
2609 * as a result of an earlier submission, this routine has no effect on the
2610 * work item. If the work item has already been processed, or is currently
2611 * being processed, its work is considered complete and the work item can be
2612 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002613 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002614 * @warning
2615 * A submitted work item must not be modified until it has been processed
2616 * by the workqueue.
2617 *
2618 * @note Can be called by ISRs.
2619 *
2620 * @param work_q Address of workqueue.
2621 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002622 *
2623 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002624 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002625 */
2626static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2627 struct k_work *work)
2628{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002629 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002630 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002631 }
2632}
2633
2634/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002635 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002636 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002637 * This routine indicates if work item @a work is pending in a workqueue's
2638 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002639 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002640 * @note Can be called by ISRs.
2641 *
2642 * @param work Address of work item.
2643 *
2644 * @return 1 if work item is pending, or 0 if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002645 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002646 */
2647static inline int k_work_pending(struct k_work *work)
2648{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002649 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002650}
2651
2652/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002653 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002654 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002655 * This routine starts workqueue @a work_q. The workqueue spawns its work
2656 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002657 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002658 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002659 * @param stack Pointer to work queue thread's stack space, as defined by
2660 * K_THREAD_STACK_DEFINE()
2661 * @param stack_size Size of the work queue thread's stack (in bytes), which
2662 * should either be the same constant passed to
2663 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002664 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002665 *
2666 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002667 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002668 */
Andrew Boie507852a2017-07-25 18:47:07 -07002669extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002670 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002671 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002672
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002673/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002674 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002675 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002676 * This routine initializes a workqueue delayed work item, prior to
2677 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002678 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002679 * @param work Address of delayed work item.
2680 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002681 *
2682 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002683 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002684 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002685extern void k_delayed_work_init(struct k_delayed_work *work,
2686 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002687
2688/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002689 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002690 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002691 * This routine schedules work item @a work to be processed by workqueue
2692 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002693 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002694 * Only when the countdown completes is the work item actually submitted to
2695 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002696 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002697 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002698 * counting down cancels the existing submission and restarts the
2699 * countdown using the new delay. Note that this behavior is
2700 * inherently subject to race conditions with the pre-existing
2701 * timeouts and work queue, so care must be taken to synchronize such
2702 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002703 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002704 * @warning
2705 * A delayed work item must not be modified until it has been processed
2706 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002707 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002708 * @note Can be called by ISRs.
2709 *
2710 * @param work_q Address of workqueue.
2711 * @param work Address of delayed work item.
2712 * @param delay Delay before submitting the work item (in milliseconds).
2713 *
2714 * @retval 0 Work item countdown started.
2715 * @retval -EINPROGRESS Work item is already pending.
2716 * @retval -EINVAL Work item is being processed or has completed its work.
2717 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002718 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002719 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002720extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2721 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002722 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002723
2724/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002725 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002726 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002727 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002728 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002729 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002730 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002731 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002732 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002733 * @param work Address of delayed work item.
2734 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002735 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002736 * @retval -EINPROGRESS Work item is already pending.
2737 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002738 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002739 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002740extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002741
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002742/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002743 * @brief Submit a work item to the system workqueue.
2744 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002745 * This routine submits work item @a work to be processed by the system
2746 * workqueue. If the work item is already pending in the workqueue's queue
2747 * as a result of an earlier submission, this routine has no effect on the
2748 * work item. If the work item has already been processed, or is currently
2749 * being processed, its work is considered complete and the work item can be
2750 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002751 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002752 * @warning
2753 * Work items submitted to the system workqueue should avoid using handlers
2754 * that block or yield since this may prevent the system workqueue from
2755 * processing other work items in a timely manner.
2756 *
2757 * @note Can be called by ISRs.
2758 *
2759 * @param work Address of work item.
2760 *
2761 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002762 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002763 */
2764static inline void k_work_submit(struct k_work *work)
2765{
2766 k_work_submit_to_queue(&k_sys_work_q, work);
2767}
2768
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002769/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002770 * @brief Submit a delayed work item to the system workqueue.
2771 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002772 * This routine schedules work item @a work to be processed by the system
2773 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002774 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002775 * Only when the countdown completes is the work item actually submitted to
2776 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002777 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002778 * Submitting a previously submitted delayed work item that is still
2779 * counting down cancels the existing submission and restarts the countdown
2780 * using the new delay. If the work item is currently pending on the
2781 * workqueue's queue because the countdown has completed it is too late to
2782 * resubmit the item, and resubmission fails without impacting the work item.
2783 * If the work item has already been processed, or is currently being processed,
2784 * its work is considered complete and the work item can be resubmitted.
2785 *
2786 * @warning
2787 * Work items submitted to the system workqueue should avoid using handlers
2788 * that block or yield since this may prevent the system workqueue from
2789 * processing other work items in a timely manner.
2790 *
2791 * @note Can be called by ISRs.
2792 *
2793 * @param work Address of delayed work item.
2794 * @param delay Delay before submitting the work item (in milliseconds).
2795 *
2796 * @retval 0 Work item countdown started.
2797 * @retval -EINPROGRESS Work item is already pending.
2798 * @retval -EINVAL Work item is being processed or has completed its work.
2799 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002800 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002801 */
2802static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002803 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002804{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002805 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002806}
2807
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002808/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002809 * @brief Get time remaining before a delayed work gets scheduled.
2810 *
2811 * This routine computes the (approximate) time remaining before a
2812 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002813 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002814 *
2815 * @param work Delayed work item.
2816 *
2817 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002818 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002819 */
Kumar Galacc334c72017-04-21 10:55:34 -05002820static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002821{
2822 return _timeout_remaining_get(&work->timeout);
2823}
2824
Anas Nashif166f5192018-02-25 08:02:36 -06002825/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002826/**
Anas Nashifce78d162018-05-24 12:43:11 -05002827 * @defgroup mutex_apis Mutex APIs
2828 * @ingroup kernel_apis
2829 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002830 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002831
Anas Nashifce78d162018-05-24 12:43:11 -05002832/**
2833 * Mutex Structure
2834 * @ingroup mutex_apis
2835 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002836struct k_mutex {
2837 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002838 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002839 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002840 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002841 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002842
Anas Nashif2f203c22016-12-18 06:57:45 -05002843 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002844};
2845
Anas Nashifce78d162018-05-24 12:43:11 -05002846/**
2847 * @cond INTERNAL_HIDDEN
2848 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002849#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002850 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002851 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002852 .owner = NULL, \
2853 .lock_count = 0, \
2854 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002855 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002856 }
2857
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002858#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2859
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002860/**
Allan Stephensc98da842016-11-11 15:45:03 -05002861 * INTERNAL_HIDDEN @endcond
2862 */
2863
2864/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002865 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002867 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002868 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002869 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002871 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002872 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002873 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002874#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002875 struct k_mutex name \
2876 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002877 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002878
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002879/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002880 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002882 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002884 * Upon completion, the mutex is available and does not have an owner.
2885 *
2886 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002887 *
2888 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002889 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002890 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002891__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002892
2893/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002894 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002895 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002896 * This routine locks @a mutex. If the mutex is locked by another thread,
2897 * the calling thread waits until the mutex becomes available or until
2898 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002900 * A thread is permitted to lock a mutex it has already locked. The operation
2901 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002902 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002903 * @param mutex Address of the mutex.
2904 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002905 * or one of the special values K_NO_WAIT and K_FOREVER.
2906 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002907 * @retval 0 Mutex locked.
2908 * @retval -EBUSY Returned without waiting.
2909 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002910 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002911 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002912__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002913
2914/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002915 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002917 * This routine unlocks @a mutex. The mutex must already be locked by the
2918 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002919 *
2920 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002921 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002922 * thread.
2923 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002924 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002925 *
2926 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002927 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002928 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002929__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002930
Allan Stephensc98da842016-11-11 15:45:03 -05002931/**
Anas Nashif166f5192018-02-25 08:02:36 -06002932 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002933 */
2934
2935/**
2936 * @cond INTERNAL_HIDDEN
2937 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002938
2939struct k_sem {
2940 _wait_q_t wait_q;
2941 unsigned int count;
2942 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002943 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002944
Anas Nashif2f203c22016-12-18 06:57:45 -05002945 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002946};
2947
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002948#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002949 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002950 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002951 .count = initial_count, \
2952 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002953 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002954 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002955 }
2956
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002957#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2958
Allan Stephensc98da842016-11-11 15:45:03 -05002959/**
2960 * INTERNAL_HIDDEN @endcond
2961 */
2962
2963/**
2964 * @defgroup semaphore_apis Semaphore APIs
2965 * @ingroup kernel_apis
2966 * @{
2967 */
2968
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002969/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002970 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002972 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002974 * @param sem Address of the semaphore.
2975 * @param initial_count Initial semaphore count.
2976 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002977 *
2978 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002979 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002980 */
Andrew Boie99280232017-09-29 14:17:47 -07002981__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2982 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002983
2984/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002985 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002987 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002989 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2990 *
2991 * @param sem Address of the semaphore.
2992 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002993 * or one of the special values K_NO_WAIT and K_FOREVER.
2994 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002995 * @note When porting code from the nanokernel legacy API to the new API, be
2996 * careful with the return value of this function. The return value is the
2997 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2998 * non-zero means failure, while the nano_sem_take family returns 1 for success
2999 * and 0 for failure.
3000 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003001 * @retval 0 Semaphore taken.
3002 * @retval -EBUSY Returned without waiting.
3003 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003004 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003005 */
Andrew Boie99280232017-09-29 14:17:47 -07003006__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003007
3008/**
3009 * @brief Give a semaphore.
3010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003011 * This routine gives @a sem, unless the semaphore is already at its maximum
3012 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003014 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003016 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003017 *
3018 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003019 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003020 */
Andrew Boie99280232017-09-29 14:17:47 -07003021__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003022
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003023/**
3024 * @brief Reset a semaphore's count to zero.
3025 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003026 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003028 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003029 *
3030 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003031 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003032 */
Andrew Boie990bf162017-10-03 12:36:49 -07003033__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003034
Anas Nashif954d5502018-02-25 08:37:28 -06003035/**
3036 * @internal
3037 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003038static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003039{
3040 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003041}
3042
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003043/**
3044 * @brief Get a semaphore's count.
3045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003046 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003048 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003049 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003050 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003051 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003052 */
Andrew Boie990bf162017-10-03 12:36:49 -07003053__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003054
Anas Nashif954d5502018-02-25 08:37:28 -06003055/**
3056 * @internal
3057 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003058static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003059{
3060 return sem->count;
3061}
3062
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003063/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003064 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003065 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003066 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003067 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003068 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003069 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003070 * @param name Name of the semaphore.
3071 * @param initial_count Initial semaphore count.
3072 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003073 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003074 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003075#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003076 struct k_sem name \
3077 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003078 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303079 BUILD_ASSERT(((count_limit) != 0) && \
3080 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003081
Anas Nashif166f5192018-02-25 08:02:36 -06003082/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003083
3084/**
3085 * @defgroup alert_apis Alert APIs
3086 * @ingroup kernel_apis
3087 * @{
3088 */
3089
Allan Stephens5eceb852016-11-16 10:16:30 -05003090/**
3091 * @typedef k_alert_handler_t
3092 * @brief Alert handler function type.
3093 *
3094 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07003095 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05003096 * and is only invoked if the alert has been initialized with one.
3097 *
3098 * @param alert Address of the alert.
3099 *
3100 * @return 0 if alert has been consumed; non-zero if alert should pend.
3101 */
3102typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05003103
Anas Nashif166f5192018-02-25 08:02:36 -06003104/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003105
3106/**
3107 * @cond INTERNAL_HIDDEN
3108 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003109
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003110#define K_ALERT_DEFAULT NULL
3111#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003112
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003113struct k_alert {
3114 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003115 atomic_t send_count;
3116 struct k_work work_item;
3117 struct k_sem sem;
3118
Anas Nashif2f203c22016-12-18 06:57:45 -05003119 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003120};
3121
Anas Nashif954d5502018-02-25 08:37:28 -06003122/**
3123 * @internal
3124 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003125extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003126
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003127#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003128 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003129 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003130 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003131 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
3132 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003133 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003134 }
3135
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003136#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
3137
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003138/**
Allan Stephensc98da842016-11-11 15:45:03 -05003139 * INTERNAL_HIDDEN @endcond
3140 */
3141
3142/**
3143 * @addtogroup alert_apis
3144 * @{
3145 */
3146
3147/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003148 * @def K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts)
3149 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003150 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003151 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003152 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003153 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003154 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003155 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003156 * @param name Name of the alert.
3157 * @param alert_handler Action to take when alert is sent. Specify either
3158 * the address of a function to be invoked by the system workqueue
3159 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
3160 * K_ALERT_DEFAULT (which causes the alert to pend).
3161 * @param max_num_pending_alerts Maximum number of pending alerts.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003162 *
3163 * @req K-ALERT-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003164 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003165#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003166 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003167 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003168 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003169 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003170
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003171/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003172 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003174 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003175 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003176 * @param alert Address of the alert.
3177 * @param handler Action to take when alert is sent. Specify either the address
3178 * of a function to be invoked by the system workqueue thread,
3179 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3180 * K_ALERT_DEFAULT (which causes the alert to pend).
3181 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003182 *
3183 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003184 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003185 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003186extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3187 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003188
3189/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003190 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003191 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003192 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003193 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003194 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3195 *
3196 * @param alert Address of the alert.
3197 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003198 * or one of the special values K_NO_WAIT and K_FOREVER.
3199 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003200 * @retval 0 Alert received.
3201 * @retval -EBUSY Returned without waiting.
3202 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003203 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003204 */
Andrew Boie310e9872017-09-29 04:41:15 -07003205__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003206
3207/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003208 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003210 * This routine signals @a alert. The action specified for @a alert will
3211 * be taken, which may trigger the execution of an alert handler function
3212 * and/or cause the alert to pend (assuming the alert has not reached its
3213 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003215 * @note Can be called by ISRs.
3216 *
3217 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003218 *
3219 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003220 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003221 */
Andrew Boie310e9872017-09-29 04:41:15 -07003222__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003223
3224/**
Anas Nashif166f5192018-02-25 08:02:36 -06003225 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003226 */
3227
Allan Stephensc98da842016-11-11 15:45:03 -05003228/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003229 * @defgroup msgq_apis Message Queue APIs
3230 * @ingroup kernel_apis
3231 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003232 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003233
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003234/**
3235 * @brief Message Queue Structure
3236 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003237struct k_msgq {
3238 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003239 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003240 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003241 char *buffer_start;
3242 char *buffer_end;
3243 char *read_ptr;
3244 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003245 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003246
Anas Nashif2f203c22016-12-18 06:57:45 -05003247 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003248 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003249};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003250/**
3251 * @cond INTERNAL_HIDDEN
3252 */
3253
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003254
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003255#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003256 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003257 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003258 .max_msgs = q_max_msgs, \
3259 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003260 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003261 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003262 .read_ptr = q_buffer, \
3263 .write_ptr = q_buffer, \
3264 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003265 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003266 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003267#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003268/**
3269 * INTERNAL_HIDDEN @endcond
3270 */
3271
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003272
Andrew Boie0fe789f2018-04-12 18:35:56 -07003273#define K_MSGQ_FLAG_ALLOC BIT(0)
3274
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003275/**
3276 * @brief Message Queue Attributes
3277 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303278struct k_msgq_attrs {
3279 size_t msg_size;
3280 u32_t max_msgs;
3281 u32_t used_msgs;
3282};
3283
Allan Stephensc98da842016-11-11 15:45:03 -05003284
3285/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003286 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003287 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003288 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3289 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003290 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3291 * message is similarly aligned to this boundary, @a q_msg_size must also be
3292 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003293 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003294 * The message queue can be accessed outside the module where it is defined
3295 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003296 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003297 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003298 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003299 * @param q_name Name of the message queue.
3300 * @param q_msg_size Message size (in bytes).
3301 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003302 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003303 *
3304 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003305 */
3306#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003307 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003308 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003309 struct k_msgq q_name \
3310 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003311 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003312 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003313
Peter Mitsisd7a37502016-10-13 11:37:40 -04003314/**
3315 * @brief Initialize a message queue.
3316 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003317 * This routine initializes a message queue object, prior to its first use.
3318 *
Allan Stephensda827222016-11-09 14:23:58 -06003319 * The message queue's ring buffer must contain space for @a max_msgs messages,
3320 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3321 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3322 * that each message is similarly aligned to this boundary, @a q_msg_size
3323 * must also be a multiple of N.
3324 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003325 * @param q Address of the message queue.
3326 * @param buffer Pointer to ring buffer that holds queued messages.
3327 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003328 * @param max_msgs Maximum number of messages that can be queued.
3329 *
3330 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003331 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003332 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003333void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3334 u32_t max_msgs);
3335
3336/**
3337 * @brief Initialize a message queue.
3338 *
3339 * This routine initializes a message queue object, prior to its first use,
3340 * allocating its internal ring buffer from the calling thread's resource
3341 * pool.
3342 *
3343 * Memory allocated for the ring buffer can be released by calling
3344 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3345 * all of its references.
3346 *
3347 * @param q Address of the message queue.
3348 * @param msg_size Message size (in bytes).
3349 * @param max_msgs Maximum number of messages that can be queued.
3350 *
3351 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3352 * thread's resource pool, or -EINVAL if the size parameters cause
3353 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003354 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003355 */
3356__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3357 u32_t max_msgs);
3358
3359
3360void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003361
3362/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003363 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003365 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003366 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003367 * @note Can be called by ISRs.
3368 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003369 * @param q Address of the message queue.
3370 * @param data Pointer to the message.
3371 * @param timeout Waiting period to add the message (in milliseconds),
3372 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003373 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003374 * @retval 0 Message sent.
3375 * @retval -ENOMSG Returned without waiting or queue purged.
3376 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003377 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003378 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003379__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003380
3381/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003382 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003383 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003384 * This routine receives a message from message queue @a q in a "first in,
3385 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003386 *
Allan Stephensc98da842016-11-11 15:45:03 -05003387 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003389 * @param q Address of the message queue.
3390 * @param data Address of area to hold the received message.
3391 * @param timeout Waiting period to receive the message (in milliseconds),
3392 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003393 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003394 * @retval 0 Message received.
3395 * @retval -ENOMSG Returned without waiting.
3396 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003397 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003398 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003399__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003400
3401/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003402 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003404 * This routine discards all unreceived messages in a message queue's ring
3405 * buffer. Any threads that are blocked waiting to send a message to the
3406 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003407 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003408 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003409 *
3410 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003411 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003412 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003413__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003414
Peter Mitsis67be2492016-10-07 11:44:34 -04003415/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003416 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003418 * This routine returns the number of unused entries in a message queue's
3419 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003420 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003421 * @param q Address of the message queue.
3422 *
3423 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003424 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003425 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003426__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3427
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303428/**
3429 * @brief Get basic attributes of a message queue.
3430 *
3431 * This routine fetches basic attributes of message queue into attr argument.
3432 *
3433 * @param q Address of the message queue.
3434 * @param attrs pointer to message queue attribute structure.
3435 *
3436 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003437 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303438 */
3439__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3440
3441
Andrew Boie82edb6e2017-10-02 10:53:06 -07003442static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003443{
3444 return q->max_msgs - q->used_msgs;
3445}
3446
Peter Mitsisd7a37502016-10-13 11:37:40 -04003447/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003448 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003450 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003451 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003452 * @param q Address of the message queue.
3453 *
3454 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003455 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003456 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003457__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3458
3459static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003460{
3461 return q->used_msgs;
3462}
3463
Anas Nashif166f5192018-02-25 08:02:36 -06003464/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003465
3466/**
3467 * @defgroup mem_pool_apis Memory Pool APIs
3468 * @ingroup kernel_apis
3469 * @{
3470 */
3471
Andy Ross73cb9582017-05-09 10:42:39 -07003472/* Note on sizing: the use of a 20 bit field for block means that,
3473 * assuming a reasonable minimum block size of 16 bytes, we're limited
3474 * to 16M of memory managed by a single pool. Long term it would be
3475 * good to move to a variable bit size based on configuration.
3476 */
3477struct k_mem_block_id {
3478 u32_t pool : 8;
3479 u32_t level : 4;
3480 u32_t block : 20;
3481};
3482
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003483struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003484 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003485 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003486};
3487
Anas Nashif166f5192018-02-25 08:02:36 -06003488/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003489
3490/**
3491 * @defgroup mailbox_apis Mailbox APIs
3492 * @ingroup kernel_apis
3493 * @{
3494 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003495
3496struct k_mbox_msg {
3497 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003498 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003499 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003500 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003501 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003502 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003503 /** sender's message data buffer */
3504 void *tx_data;
3505 /** internal use only - needed for legacy API support */
3506 void *_rx_data;
3507 /** message data block descriptor */
3508 struct k_mem_block tx_block;
3509 /** source thread id */
3510 k_tid_t rx_source_thread;
3511 /** target thread id */
3512 k_tid_t tx_target_thread;
3513 /** internal use only - thread waiting on send (may be a dummy) */
3514 k_tid_t _syncing_thread;
3515#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3516 /** internal use only - semaphore used during asynchronous send */
3517 struct k_sem *_async_sem;
3518#endif
3519};
3520
3521struct k_mbox {
3522 _wait_q_t tx_msg_queue;
3523 _wait_q_t rx_msg_queue;
3524
Anas Nashif2f203c22016-12-18 06:57:45 -05003525 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003526};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003527/**
3528 * @cond INTERNAL_HIDDEN
3529 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003530
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003531#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003532 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003533 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3534 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003535 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003536 }
3537
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003538#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3539
Peter Mitsis12092702016-10-14 12:57:23 -04003540/**
Allan Stephensc98da842016-11-11 15:45:03 -05003541 * INTERNAL_HIDDEN @endcond
3542 */
3543
3544/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003545 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003546 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003547 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003548 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003549 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003550 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003551 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003552 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003553 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003554#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003555 struct k_mbox name \
3556 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003557 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003558
Peter Mitsis12092702016-10-14 12:57:23 -04003559/**
3560 * @brief Initialize a mailbox.
3561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003562 * This routine initializes a mailbox object, prior to its first use.
3563 *
3564 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003565 *
3566 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003567 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003568 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003569extern void k_mbox_init(struct k_mbox *mbox);
3570
Peter Mitsis12092702016-10-14 12:57:23 -04003571/**
3572 * @brief Send a mailbox message in a synchronous manner.
3573 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003574 * This routine sends a message to @a mbox and waits for a receiver to both
3575 * receive and process it. The message data may be in a buffer, in a memory
3576 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003577 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003578 * @param mbox Address of the mailbox.
3579 * @param tx_msg Address of the transmit message descriptor.
3580 * @param timeout Waiting period for the message to be received (in
3581 * milliseconds), or one of the special values K_NO_WAIT
3582 * and K_FOREVER. Once the message has been received,
3583 * this routine waits as long as necessary for the message
3584 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003585 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003586 * @retval 0 Message sent.
3587 * @retval -ENOMSG Returned without waiting.
3588 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003589 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003590 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003591extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003592 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003593
Peter Mitsis12092702016-10-14 12:57:23 -04003594/**
3595 * @brief Send a mailbox message in an asynchronous manner.
3596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003597 * This routine sends a message to @a mbox without waiting for a receiver
3598 * to process it. The message data may be in a buffer, in a memory pool block,
3599 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3600 * will be given when the message has been both received and completely
3601 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003603 * @param mbox Address of the mailbox.
3604 * @param tx_msg Address of the transmit message descriptor.
3605 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003606 *
3607 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003608 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003609 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003610extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003611 struct k_sem *sem);
3612
Peter Mitsis12092702016-10-14 12:57:23 -04003613/**
3614 * @brief Receive a mailbox message.
3615 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003616 * This routine receives a message from @a mbox, then optionally retrieves
3617 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003619 * @param mbox Address of the mailbox.
3620 * @param rx_msg Address of the receive message descriptor.
3621 * @param buffer Address of the buffer to receive data, or NULL to defer data
3622 * retrieval and message disposal until later.
3623 * @param timeout Waiting period for a message to be received (in
3624 * milliseconds), or one of the special values K_NO_WAIT
3625 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003626 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003627 * @retval 0 Message received.
3628 * @retval -ENOMSG Returned without waiting.
3629 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003630 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003631 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003632extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003633 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003634
3635/**
3636 * @brief Retrieve mailbox message data into a buffer.
3637 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003638 * This routine completes the processing of a received message by retrieving
3639 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003640 *
3641 * Alternatively, this routine can be used to dispose of a received message
3642 * without retrieving its data.
3643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003644 * @param rx_msg Address of the receive message descriptor.
3645 * @param buffer Address of the buffer to receive data, or NULL to discard
3646 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003647 *
3648 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003649 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003650 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003651extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003652
3653/**
3654 * @brief Retrieve mailbox message data into a memory pool block.
3655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003656 * This routine completes the processing of a received message by retrieving
3657 * its data into a memory pool block, then disposing of the message.
3658 * The memory pool block that results from successful retrieval must be
3659 * returned to the pool once the data has been processed, even in cases
3660 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003661 *
3662 * Alternatively, this routine can be used to dispose of a received message
3663 * without retrieving its data. In this case there is no need to return a
3664 * memory pool block to the pool.
3665 *
3666 * This routine allocates a new memory pool block for the data only if the
3667 * data is not already in one. If a new block cannot be allocated, the routine
3668 * returns a failure code and the received message is left unchanged. This
3669 * permits the caller to reattempt data retrieval at a later time or to dispose
3670 * of the received message without retrieving its data.
3671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003672 * @param rx_msg Address of a receive message descriptor.
3673 * @param pool Address of memory pool, or NULL to discard data.
3674 * @param block Address of the area to hold memory pool block info.
3675 * @param timeout Waiting period to wait for a memory pool block (in
3676 * milliseconds), or one of the special values K_NO_WAIT
3677 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003678 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003679 * @retval 0 Data retrieved.
3680 * @retval -ENOMEM Returned without waiting.
3681 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003682 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003683 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003684extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003685 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003686 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003687
Anas Nashif166f5192018-02-25 08:02:36 -06003688/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003689
3690/**
Anas Nashifce78d162018-05-24 12:43:11 -05003691 * @defgroup pipe_apis Pipe APIs
3692 * @ingroup kernel_apis
3693 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003694 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003695
Anas Nashifce78d162018-05-24 12:43:11 -05003696/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003697struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003698 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3699 size_t size; /**< Buffer size */
3700 size_t bytes_used; /**< # bytes used in buffer */
3701 size_t read_index; /**< Where in buffer to read from */
3702 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003703
3704 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003705 _wait_q_t readers; /**< Reader wait queue */
3706 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003707 } wait_q;
3708
Anas Nashif2f203c22016-12-18 06:57:45 -05003709 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Anas Nashifce78d162018-05-24 12:43:11 -05003710 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003711};
3712
Anas Nashifce78d162018-05-24 12:43:11 -05003713/**
3714 * @cond INTERNAL_HIDDEN
3715 */
3716#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3717
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003718#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003719 { \
3720 .buffer = pipe_buffer, \
3721 .size = pipe_buffer_size, \
3722 .bytes_used = 0, \
3723 .read_index = 0, \
3724 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003725 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3726 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003727 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003728 }
3729
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003730#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3731
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003732/**
Allan Stephensc98da842016-11-11 15:45:03 -05003733 * INTERNAL_HIDDEN @endcond
3734 */
3735
3736/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003737 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003738 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003739 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003740 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003741 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003742 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003743 * @param name Name of the pipe.
3744 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3745 * or zero if no ring buffer is used.
3746 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003747 *
3748 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003749 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003750#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3751 static unsigned char __kernel_noinit __aligned(pipe_align) \
3752 _k_pipe_buf_##name[pipe_buffer_size]; \
3753 struct k_pipe name \
3754 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003755 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003756
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003757/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003758 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003760 * This routine initializes a pipe object, prior to its first use.
3761 *
3762 * @param pipe Address of the pipe.
3763 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3764 * is used.
3765 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3766 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003767 *
3768 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003769 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003770 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003771void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3772
3773/**
3774 * @brief Release a pipe's allocated buffer
3775 *
3776 * If a pipe object was given a dynamically allocated buffer via
3777 * k_pipe_alloc_init(), this will free it. This function does nothing
3778 * if the buffer wasn't dynamically allocated.
3779 *
3780 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003781 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003782 */
3783void k_pipe_cleanup(struct k_pipe *pipe);
3784
3785/**
3786 * @brief Initialize a pipe and allocate a buffer for it
3787 *
3788 * Storage for the buffer region will be allocated from the calling thread's
3789 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3790 * or userspace is enabled and the pipe object loses all references to it.
3791 *
3792 * This function should only be called on uninitialized pipe objects.
3793 *
3794 * @param pipe Address of the pipe.
3795 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3796 * buffer is used.
3797 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003798 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003799 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003800 */
3801__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003802
3803/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003804 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003805 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003806 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003807 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003808 * @param pipe Address of the pipe.
3809 * @param data Address of data to write.
3810 * @param bytes_to_write Size of data (in bytes).
3811 * @param bytes_written Address of area to hold the number of bytes written.
3812 * @param min_xfer Minimum number of bytes to write.
3813 * @param timeout Waiting period to wait for the data to be written (in
3814 * milliseconds), or one of the special values K_NO_WAIT
3815 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003816 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003817 * @retval 0 At least @a min_xfer bytes of data were written.
3818 * @retval -EIO Returned without waiting; zero data bytes were written.
3819 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003820 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003821 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003822 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003823__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3824 size_t bytes_to_write, size_t *bytes_written,
3825 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003826
3827/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003828 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003829 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003830 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003831 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003832 * @param pipe Address of the pipe.
3833 * @param data Address to place the data read from pipe.
3834 * @param bytes_to_read Maximum number of data bytes to read.
3835 * @param bytes_read Address of area to hold the number of bytes read.
3836 * @param min_xfer Minimum number of data bytes to read.
3837 * @param timeout Waiting period to wait for the data to be read (in
3838 * milliseconds), or one of the special values K_NO_WAIT
3839 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003840 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003841 * @retval 0 At least @a min_xfer bytes of data were read.
3842 * @retval -EIO Returned without waiting; zero data bytes were read.
3843 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003844 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003845 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003846 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003847__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3848 size_t bytes_to_read, size_t *bytes_read,
3849 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003850
3851/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003852 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003854 * This routine writes the data contained in a memory block to @a pipe.
3855 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003856 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003858 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003859 * @param block Memory block containing data to send
3860 * @param size Number of data bytes in memory block to send
3861 * @param sem Semaphore to signal upon completion (else NULL)
3862 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003863 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003864 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003865 */
3866extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3867 size_t size, struct k_sem *sem);
3868
Anas Nashif166f5192018-02-25 08:02:36 -06003869/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003870
Allan Stephensc98da842016-11-11 15:45:03 -05003871/**
3872 * @cond INTERNAL_HIDDEN
3873 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003874
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003875struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003876 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003877 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003878 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003879 char *buffer;
3880 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003881 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003882
Anas Nashif2f203c22016-12-18 06:57:45 -05003883 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003884};
3885
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003886#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003887 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003888 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003889 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003890 .num_blocks = slab_num_blocks, \
3891 .block_size = slab_block_size, \
3892 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003893 .free_list = NULL, \
3894 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003895 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003896 }
3897
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003898#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3899
3900
Peter Mitsis578f9112016-10-07 13:50:31 -04003901/**
Allan Stephensc98da842016-11-11 15:45:03 -05003902 * INTERNAL_HIDDEN @endcond
3903 */
3904
3905/**
3906 * @defgroup mem_slab_apis Memory Slab APIs
3907 * @ingroup kernel_apis
3908 * @{
3909 */
3910
3911/**
Allan Stephensda827222016-11-09 14:23:58 -06003912 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003913 *
Allan Stephensda827222016-11-09 14:23:58 -06003914 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003915 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003916 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3917 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003918 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003919 *
Allan Stephensda827222016-11-09 14:23:58 -06003920 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003921 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003922 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003923 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003924 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003925 * @param name Name of the memory slab.
3926 * @param slab_block_size Size of each memory block (in bytes).
3927 * @param slab_num_blocks Number memory blocks.
3928 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003929 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003930 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003931#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3932 char __noinit __aligned(slab_align) \
3933 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3934 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003935 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003936 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003937 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003938
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003939/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003940 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003941 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003942 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003943 *
Allan Stephensda827222016-11-09 14:23:58 -06003944 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3945 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3946 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3947 * To ensure that each memory block is similarly aligned to this boundary,
3948 * @a slab_block_size must also be a multiple of N.
3949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003950 * @param slab Address of the memory slab.
3951 * @param buffer Pointer to buffer used for the memory blocks.
3952 * @param block_size Size of each memory block (in bytes).
3953 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003954 *
3955 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003956 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003957 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003958extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003959 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003960
3961/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003962 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003963 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003964 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003965 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003966 * @param slab Address of the memory slab.
3967 * @param mem Pointer to block address area.
3968 * @param timeout Maximum time to wait for operation to complete
3969 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3970 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003971 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003972 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003973 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003974 * @retval -ENOMEM Returned without waiting.
3975 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003976 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003977 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003978extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003979 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003980
3981/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003982 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003983 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003984 * This routine releases a previously allocated memory block back to its
3985 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003987 * @param slab Address of the memory slab.
3988 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003989 *
3990 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003991 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003992 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003993extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003994
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003995/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003996 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003997 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003998 * This routine gets the number of memory blocks that are currently
3999 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004001 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004002 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004003 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004004 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004005 */
Kumar Galacc334c72017-04-21 10:55:34 -05004006static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004007{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004008 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004009}
4010
Peter Mitsisc001aa82016-10-13 13:53:37 -04004011/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004012 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004014 * This routine gets the number of memory blocks that are currently
4015 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004016 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004017 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004019 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004020 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004021 */
Kumar Galacc334c72017-04-21 10:55:34 -05004022static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004023{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004024 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004025}
4026
Anas Nashif166f5192018-02-25 08:02:36 -06004027/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004028
4029/**
4030 * @cond INTERNAL_HIDDEN
4031 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004032
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004033struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004034 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004035 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004036};
4037
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004038/**
Allan Stephensc98da842016-11-11 15:45:03 -05004039 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004040 */
4041
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004042/**
Allan Stephensc98da842016-11-11 15:45:03 -05004043 * @addtogroup mem_pool_apis
4044 * @{
4045 */
4046
4047/**
4048 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004049 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004050 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4051 * long. The memory pool allows blocks to be repeatedly partitioned into
4052 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004053 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004054 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004055 * If the pool is to be accessed outside the module where it is defined, it
4056 * can be declared via
4057 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004058 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004059 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004060 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004061 * @param minsz Size of the smallest blocks in the pool (in bytes).
4062 * @param maxsz Size of the largest blocks in the pool (in bytes).
4063 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004064 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004065 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004066 */
Andy Ross73cb9582017-05-09 10:42:39 -07004067#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4068 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4069 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08004070 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07004071 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004072 .base = { \
4073 .buf = _mpool_buf_##name, \
4074 .max_sz = maxsz, \
4075 .n_max = nmax, \
4076 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
4077 .levels = _mpool_lvls_##name, \
4078 .flags = SYS_MEM_POOL_KERNEL \
4079 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004080 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004081
Peter Mitsis937042c2016-10-13 13:18:26 -04004082/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004083 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004084 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004085 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004086 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004087 * @param pool Address of the memory pool.
4088 * @param block Pointer to block descriptor for the allocated memory.
4089 * @param size Amount of memory to allocate (in bytes).
4090 * @param timeout Maximum time to wait for operation to complete
4091 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4092 * or K_FOREVER to wait as long as necessary.
4093 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004094 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004095 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004096 * @retval -ENOMEM Returned without waiting.
4097 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004098 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004099 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004100extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004101 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004102
4103/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004104 * @brief Allocate memory from a memory pool with malloc() semantics
4105 *
4106 * Such memory must be released using k_free().
4107 *
4108 * @param pool Address of the memory pool.
4109 * @param size Amount of memory to allocate (in bytes).
4110 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004111 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004112 */
4113extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4114
4115/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004116 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004117 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004118 * This routine releases a previously allocated memory block back to its
4119 * memory pool.
4120 *
4121 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004122 *
4123 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004124 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004125 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004126extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004127
4128/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004129 * @brief Free memory allocated from a memory pool.
4130 *
4131 * This routine releases a previously allocated memory block back to its
4132 * memory pool
4133 *
4134 * @param id Memory block identifier.
4135 *
4136 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004137 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004138 */
4139extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4140
4141/**
Anas Nashif166f5192018-02-25 08:02:36 -06004142 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004143 */
4144
4145/**
4146 * @defgroup heap_apis Heap Memory Pool APIs
4147 * @ingroup kernel_apis
4148 * @{
4149 */
4150
4151/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004152 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004153 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004154 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004155 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004156 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004157 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004158 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004159 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004160 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004161 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004162extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004163
4164/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004165 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004166 *
4167 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004168 * returned must have been allocated from the heap memory pool or
4169 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004170 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004171 * If @a ptr is NULL, no operation is performed.
4172 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004173 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004174 *
4175 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004176 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004177 */
4178extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004179
Allan Stephensc98da842016-11-11 15:45:03 -05004180/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004181 * @brief Allocate memory from heap, array style
4182 *
4183 * This routine provides traditional calloc() semantics. Memory is
4184 * allocated from the heap memory pool and zeroed.
4185 *
4186 * @param nmemb Number of elements in the requested array
4187 * @param size Size of each array element (in bytes).
4188 *
4189 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004190 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004191 */
4192extern void *k_calloc(size_t nmemb, size_t size);
4193
Anas Nashif166f5192018-02-25 08:02:36 -06004194/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004195
Benjamin Walshacc68c12017-01-29 18:57:45 -05004196/* polling API - PRIVATE */
4197
Benjamin Walshb0179862017-02-02 16:39:57 -05004198#ifdef CONFIG_POLL
4199#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
4200#else
4201#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
4202#endif
4203
Benjamin Walshacc68c12017-01-29 18:57:45 -05004204/* private - implementation data created as needed, per-type */
4205struct _poller {
4206 struct k_thread *thread;
Andy Ross55a7e462018-05-31 11:58:09 -07004207 volatile int is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004208};
4209
4210/* private - types bit positions */
4211enum _poll_types_bits {
4212 /* can be used to ignore an event */
4213 _POLL_TYPE_IGNORE,
4214
4215 /* to be signaled by k_poll_signal() */
4216 _POLL_TYPE_SIGNAL,
4217
4218 /* semaphore availability */
4219 _POLL_TYPE_SEM_AVAILABLE,
4220
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004221 /* queue/fifo/lifo data availability */
4222 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004223
4224 _POLL_NUM_TYPES
4225};
4226
4227#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4228
4229/* private - states bit positions */
4230enum _poll_states_bits {
4231 /* default state when creating event */
4232 _POLL_STATE_NOT_READY,
4233
Benjamin Walshacc68c12017-01-29 18:57:45 -05004234 /* signaled by k_poll_signal() */
4235 _POLL_STATE_SIGNALED,
4236
4237 /* semaphore is available */
4238 _POLL_STATE_SEM_AVAILABLE,
4239
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004240 /* data is available to read on queue/fifo/lifo */
4241 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004242
4243 _POLL_NUM_STATES
4244};
4245
4246#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4247
4248#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004249 (32 - (0 \
4250 + 8 /* tag */ \
4251 + _POLL_NUM_TYPES \
4252 + _POLL_NUM_STATES \
4253 + 1 /* modes */ \
4254 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004255
Benjamin Walshacc68c12017-01-29 18:57:45 -05004256/* end of polling API - PRIVATE */
4257
4258
4259/**
4260 * @defgroup poll_apis Async polling APIs
4261 * @ingroup kernel_apis
4262 * @{
4263 */
4264
4265/* Public polling API */
4266
4267/* public - values for k_poll_event.type bitfield */
4268#define K_POLL_TYPE_IGNORE 0
4269#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4270#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004271#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4272#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004273
4274/* public - polling modes */
4275enum k_poll_modes {
4276 /* polling thread does not take ownership of objects when available */
4277 K_POLL_MODE_NOTIFY_ONLY = 0,
4278
4279 K_POLL_NUM_MODES
4280};
4281
4282/* public - values for k_poll_event.state bitfield */
4283#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004284#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4285#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004286#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4287#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004288
4289/* public - poll signal object */
4290struct k_poll_signal {
4291 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004292 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004293
4294 /*
4295 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4296 * user resets it to 0.
4297 */
4298 unsigned int signaled;
4299
4300 /* custom result value passed to k_poll_signal() if needed */
4301 int result;
4302};
4303
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004304#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004305 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004306 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004307 .signaled = 0, \
4308 .result = 0, \
4309 }
4310
4311struct k_poll_event {
4312 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004313 sys_dnode_t _node;
4314
4315 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004316 struct _poller *poller;
4317
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004318 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004319 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004320
Benjamin Walshacc68c12017-01-29 18:57:45 -05004321 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004322 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004323
4324 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004325 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004326
4327 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004328 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004329
4330 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004331 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004332
4333 /* per-type data */
4334 union {
4335 void *obj;
4336 struct k_poll_signal *signal;
4337 struct k_sem *sem;
4338 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004339 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004340 };
4341};
4342
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004343#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004344 { \
4345 .poller = NULL, \
4346 .type = event_type, \
4347 .state = K_POLL_STATE_NOT_READY, \
4348 .mode = event_mode, \
4349 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004350 { .obj = event_obj }, \
4351 }
4352
4353#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4354 event_tag) \
4355 { \
4356 .type = event_type, \
4357 .tag = event_tag, \
4358 .state = K_POLL_STATE_NOT_READY, \
4359 .mode = event_mode, \
4360 .unused = 0, \
4361 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004362 }
4363
4364/**
4365 * @brief Initialize one struct k_poll_event instance
4366 *
4367 * After this routine is called on a poll event, the event it ready to be
4368 * placed in an event array to be passed to k_poll().
4369 *
4370 * @param event The event to initialize.
4371 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4372 * values. Only values that apply to the same object being polled
4373 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4374 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004375 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004376 * @param obj Kernel object or poll signal.
4377 *
4378 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004379 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004380 */
4381
Kumar Galacc334c72017-04-21 10:55:34 -05004382extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004383 int mode, void *obj);
4384
4385/**
4386 * @brief Wait for one or many of multiple poll events to occur
4387 *
4388 * This routine allows a thread to wait concurrently for one or many of
4389 * multiple poll events to have occurred. Such events can be a kernel object
4390 * being available, like a semaphore, or a poll signal event.
4391 *
4392 * When an event notifies that a kernel object is available, the kernel object
4393 * is not "given" to the thread calling k_poll(): it merely signals the fact
4394 * that the object was available when the k_poll() call was in effect. Also,
4395 * all threads trying to acquire an object the regular way, i.e. by pending on
4396 * the object, have precedence over the thread polling on the object. This
4397 * means that the polling thread will never get the poll event on an object
4398 * until the object becomes available and its pend queue is empty. For this
4399 * reason, the k_poll() call is more effective when the objects being polled
4400 * only have one thread, the polling thread, trying to acquire them.
4401 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004402 * When k_poll() returns 0, the caller should loop on all the events that were
4403 * passed to k_poll() and check the state field for the values that were
4404 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004405 *
4406 * Before being reused for another call to k_poll(), the user has to reset the
4407 * state field to K_POLL_STATE_NOT_READY.
4408 *
Andrew Boie3772f772018-05-07 16:52:57 -07004409 * When called from user mode, a temporary memory allocation is required from
4410 * the caller's resource pool.
4411 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004412 * @param events An array of pointers to events to be polled for.
4413 * @param num_events The number of events in the array.
4414 * @param timeout Waiting period for an event to be ready (in milliseconds),
4415 * or one of the special values K_NO_WAIT and K_FOREVER.
4416 *
4417 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004418 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02004419 * @retval -EINTR Poller thread has been interrupted.
Andrew Boie3772f772018-05-07 16:52:57 -07004420 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4421 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004422 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004423 */
4424
Andrew Boie3772f772018-05-07 16:52:57 -07004425__syscall int k_poll(struct k_poll_event *events, int num_events,
4426 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004427
4428/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004429 * @brief Initialize a poll signal object.
4430 *
4431 * Ready a poll signal object to be signaled via k_poll_signal().
4432 *
4433 * @param signal A poll signal.
4434 *
4435 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004436 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004437 */
4438
Andrew Boie3772f772018-05-07 16:52:57 -07004439__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4440
4441/*
4442 * @brief Reset a poll signal object's state to unsignaled.
4443 *
4444 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004445 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004446 */
4447__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4448
4449static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4450{
4451 signal->signaled = 0;
4452}
4453
4454/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004455 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004456 *
4457 * @param signal A poll signal object
4458 * @param signaled An integer buffer which will be written nonzero if the
4459 * object was signaled
4460 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004461 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004462 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004463 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004464 */
4465__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4466 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004467
4468/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004469 * @brief Signal a poll signal object.
4470 *
4471 * This routine makes ready a poll signal, which is basically a poll event of
4472 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4473 * made ready to run. A @a result value can be specified.
4474 *
4475 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004476 * k_poll_signal(), stays set until the user sets it back to 0 with
4477 * k_poll_signal_reset(). It thus has to be reset by the user before being
4478 * passed again to k_poll() or k_poll() will consider it being signaled, and
4479 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004480 *
4481 * @param signal A poll signal.
4482 * @param result The value to store in the result field of the signal.
4483 *
4484 * @retval 0 The signal was delivered successfully.
4485 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004486 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004487 */
4488
Andrew Boie3772f772018-05-07 16:52:57 -07004489__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004490
Anas Nashif954d5502018-02-25 08:37:28 -06004491/**
4492 * @internal
4493 */
Andy Ross8606fab2018-03-26 10:54:40 -07004494extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004495
Anas Nashif166f5192018-02-25 08:02:36 -06004496/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004497
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004498/**
4499 * @brief Make the CPU idle.
4500 *
4501 * This function makes the CPU idle until an event wakes it up.
4502 *
4503 * In a regular system, the idle thread should be the only thread responsible
4504 * for making the CPU idle and triggering any type of power management.
4505 * However, in some more constrained systems, such as a single-threaded system,
4506 * the only thread would be responsible for this if needed.
4507 *
4508 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004509 * @req K-MISC-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004510 */
4511extern void k_cpu_idle(void);
4512
4513/**
4514 * @brief Make the CPU idle in an atomic fashion.
4515 *
4516 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4517 * must be done atomically before making the CPU idle.
4518 *
4519 * @param key Interrupt locking key obtained from irq_lock().
4520 *
4521 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004522 * @req K-MISC-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004523 */
4524extern void k_cpu_atomic_idle(unsigned int key);
4525
Anas Nashif954d5502018-02-25 08:37:28 -06004526
4527/**
4528 * @internal
4529 */
Kumar Galacc334c72017-04-21 10:55:34 -05004530extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004531
Andrew Boiecdb94d62017-04-18 15:22:05 -07004532#ifdef _ARCH_EXCEPT
4533/* This archtecture has direct support for triggering a CPU exception */
4534#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4535#else
4536
Andrew Boiecdb94d62017-04-18 15:22:05 -07004537/* NOTE: This is the implementation for arches that do not implement
4538 * _ARCH_EXCEPT() to generate a real CPU exception.
4539 *
4540 * We won't have a real exception frame to determine the PC value when
4541 * the oops occurred, so print file and line number before we jump into
4542 * the fatal error handler.
4543 */
4544#define _k_except_reason(reason) do { \
4545 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4546 _NanoFatalErrorHandler(reason, &_default_esf); \
4547 CODE_UNREACHABLE; \
4548 } while (0)
4549
4550#endif /* _ARCH__EXCEPT */
4551
4552/**
4553 * @brief Fatally terminate a thread
4554 *
4555 * This should be called when a thread has encountered an unrecoverable
4556 * runtime condition and needs to terminate. What this ultimately
4557 * means is determined by the _fatal_error_handler() implementation, which
4558 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4559 *
4560 * If this is called from ISR context, the default system fatal error handler
4561 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004562 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004563 */
4564#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4565
4566/**
4567 * @brief Fatally terminate the system
4568 *
4569 * This should be called when the Zephyr kernel has encountered an
4570 * unrecoverable runtime condition and needs to terminate. What this ultimately
4571 * means is determined by the _fatal_error_handler() implementation, which
4572 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004573 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004574 */
4575#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4576
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004577/*
4578 * private APIs that are utilized by one or more public APIs
4579 */
4580
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004581#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004582/**
4583 * @internal
4584 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004585extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004586#else
Anas Nashif954d5502018-02-25 08:37:28 -06004587/**
4588 * @internal
4589 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004590#define _init_static_threads() do { } while ((0))
4591#endif
4592
Anas Nashif954d5502018-02-25 08:37:28 -06004593/**
4594 * @internal
4595 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004596extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004597/**
4598 * @internal
4599 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004600extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004601
Andrew Boiedc5d9352017-06-02 12:56:47 -07004602/* arch/cpu.h may declare an architecture or platform-specific macro
4603 * for properly declaring stacks, compatible with MMU/MPU constraints if
4604 * enabled
4605 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004606
4607/**
4608 * @brief Obtain an extern reference to a stack
4609 *
4610 * This macro properly brings the symbol of a thread stack declared
4611 * elsewhere into scope.
4612 *
4613 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004614 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004615 */
4616#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4617
Andrew Boiedc5d9352017-06-02 12:56:47 -07004618#ifdef _ARCH_THREAD_STACK_DEFINE
4619#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4620#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4621 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304622#define K_THREAD_STACK_LEN(size) _ARCH_THREAD_STACK_LEN(size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004623#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4624#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004625static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004626{
4627 return _ARCH_THREAD_STACK_BUFFER(sym);
4628}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004629#else
4630/**
4631 * @brief Declare a toplevel thread stack memory region
4632 *
4633 * This declares a region of memory suitable for use as a thread's stack.
4634 *
4635 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4636 * 'noinit' section so that it isn't zeroed at boot
4637 *
Andrew Boie507852a2017-07-25 18:47:07 -07004638 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004639 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004640 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004641 *
4642 * It is legal to precede this definition with the 'static' keyword.
4643 *
4644 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4645 * parameter of k_thread_create(), it may not be the same as the
4646 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4647 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004648 * Some arches may round the size of the usable stack region up to satisfy
4649 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4650 * size.
4651 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004652 * @param sym Thread stack symbol name
4653 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004654 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004655 */
4656#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004657 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004658
4659/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304660 * @brief Calculate size of stacks to be allocated in a stack array
4661 *
4662 * This macro calculates the size to be allocated for the stacks
4663 * inside a stack array. It accepts the indicated "size" as a parameter
4664 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4665 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4666 *
4667 * @param size Size of the stack memory region
4668 * @req K-TSTACK-001
4669 */
4670#define K_THREAD_STACK_LEN(size) (size)
4671
4672/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004673 * @brief Declare a toplevel array of thread stack memory regions
4674 *
4675 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4676 * definition for additional details and constraints.
4677 *
4678 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4679 * 'noinit' section so that it isn't zeroed at boot
4680 *
4681 * @param sym Thread stack symbol name
4682 * @param nmemb Number of stacks to declare
4683 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004684 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004685 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004686#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004687 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304688 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004689
4690/**
4691 * @brief Declare an embedded stack memory region
4692 *
4693 * Used for stacks embedded within other data structures. Use is highly
4694 * discouraged but in some cases necessary. For memory protection scenarios,
4695 * it is very important that any RAM preceding this member not be writable
4696 * by threads else a stack overflow will lead to silent corruption. In other
4697 * words, the containing data structure should live in RAM owned by the kernel.
4698 *
4699 * @param sym Thread stack symbol name
4700 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004701 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004702 */
4703#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004704 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004705
4706/**
4707 * @brief Return the size in bytes of a stack memory region
4708 *
4709 * Convenience macro for passing the desired stack size to k_thread_create()
4710 * since the underlying implementation may actually create something larger
4711 * (for instance a guard area).
4712 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004713 * The value returned here is not guaranteed to match the 'size' parameter
4714 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004715 *
4716 * @param sym Stack memory symbol
4717 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004718 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004719 */
4720#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4721
4722/**
4723 * @brief Get a pointer to the physical stack buffer
4724 *
4725 * Convenience macro to get at the real underlying stack buffer used by
4726 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4727 * This is really only intended for diagnostic tools which want to examine
4728 * stack memory contents.
4729 *
4730 * @param sym Declared stack symbol name
4731 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004732 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004733 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004734static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004735{
4736 return (char *)sym;
4737}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004738
4739#endif /* _ARCH_DECLARE_STACK */
4740
Chunlin Hane9c97022017-07-07 20:29:30 +08004741/**
4742 * @defgroup mem_domain_apis Memory domain APIs
4743 * @ingroup kernel_apis
4744 * @{
4745 */
4746
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004747/**
4748 * @def MEM_PARTITION_ENTRY
4749 * @brief Used to declare a memory partition entry
4750 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004751 */
4752#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4753 {\
4754 .start = _start, \
4755 .size = _size, \
4756 .attr = _attr, \
4757 }
4758
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004759/**
4760 * @def K_MEM_PARTITION_DEFINE
4761 * @brief Used to declare a memory partition
4762 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004763 */
4764#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4765#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4766 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304767 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004768 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4769#else
4770#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304771 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004772 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4773#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4774
Chunlin Hane9c97022017-07-07 20:29:30 +08004775/* memory partition */
4776struct k_mem_partition {
4777 /* start address of memory partition */
4778 u32_t start;
4779 /* size of memory partition */
4780 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304781#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004782 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304783 k_mem_partition_attr_t attr;
4784#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004785};
4786
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304787/* memory domian
4788 * Note: Always declare this structure with __kernel prefix
4789 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004790struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304791#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004792 /* partitions in the domain */
4793 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304794#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004795 /* domain q */
4796 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004797 /* number of partitions in the domain */
4798 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004799};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304800
Chunlin Hane9c97022017-07-07 20:29:30 +08004801
4802/**
4803 * @brief Initialize a memory domain.
4804 *
4805 * Initialize a memory domain with given name and memory partitions.
4806 *
4807 * @param domain The memory domain to be initialized.
4808 * @param num_parts The number of array items of "parts" parameter.
4809 * @param parts An array of pointers to the memory partitions. Can be NULL
4810 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004811 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004812 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004813extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004814 struct k_mem_partition *parts[]);
4815/**
4816 * @brief Destroy a memory domain.
4817 *
4818 * Destroy a memory domain.
4819 *
4820 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004821 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004822 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004823extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4824
4825/**
4826 * @brief Add a memory partition into a memory domain.
4827 *
4828 * Add a memory partition into a memory domain.
4829 *
4830 * @param domain The memory domain to be added a memory partition.
4831 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004832 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004833 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004834extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4835 struct k_mem_partition *part);
4836
4837/**
4838 * @brief Remove a memory partition from a memory domain.
4839 *
4840 * Remove a memory partition from a memory domain.
4841 *
4842 * @param domain The memory domain to be removed a memory partition.
4843 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004844 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004845 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004846extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4847 struct k_mem_partition *part);
4848
4849/**
4850 * @brief Add a thread into a memory domain.
4851 *
4852 * Add a thread into a memory domain.
4853 *
4854 * @param domain The memory domain that the thread is going to be added into.
4855 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004856 *
4857 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004858 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004859extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4860 k_tid_t thread);
4861
4862/**
4863 * @brief Remove a thread from its memory domain.
4864 *
4865 * Remove a thread from its memory domain.
4866 *
4867 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004868 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004869 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004870extern void k_mem_domain_remove_thread(k_tid_t thread);
4871
Anas Nashif166f5192018-02-25 08:02:36 -06004872/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004873
Andrew Boie756f9072017-10-10 16:01:49 -07004874/**
4875 * @brief Emit a character buffer to the console device
4876 *
4877 * @param c String of characters to print
4878 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004879 *
4880 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004881 */
4882__syscall void k_str_out(char *c, size_t n);
4883
Andy Rosse7172672018-01-24 15:48:32 -08004884/**
4885 * @brief Start a numbered CPU on a MP-capable system
4886
4887 * This starts and initializes a specific CPU. The main thread on
4888 * startup is running on CPU zero, other processors are numbered
4889 * sequentially. On return from this function, the CPU is known to
4890 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004891 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004892 * with the provided key will work to enable them.
4893 *
4894 * Normally, in SMP mode this function will be called by the kernel
4895 * initialization and should not be used as a user API. But it is
4896 * defined here for special-purpose apps which want Zephyr running on
4897 * one core and to use others for design-specific processing.
4898 *
4899 * @param cpu_num Integer number of the CPU
4900 * @param stack Stack memory for the CPU
4901 * @param sz Stack buffer size, in bytes
4902 * @param fn Function to begin running on the CPU. First argument is
4903 * an irq_unlock() key.
4904 * @param arg Untyped argument to be passed to "fn"
4905 */
4906extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4907 void (*fn)(int, void *), void *arg);
4908
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004909#ifdef __cplusplus
4910}
4911#endif
4912
Andrew Boiee004dec2016-11-07 09:01:19 -08004913#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4914/*
4915 * Define new and delete operators.
4916 * At this moment, the operators do nothing since objects are supposed
4917 * to be statically allocated.
4918 */
4919inline void operator delete(void *ptr)
4920{
4921 (void)ptr;
4922}
4923
4924inline void operator delete[](void *ptr)
4925{
4926 (void)ptr;
4927}
4928
4929inline void *operator new(size_t size)
4930{
4931 (void)size;
4932 return NULL;
4933}
4934
4935inline void *operator new[](size_t size)
4936{
4937 (void)size;
4938 return NULL;
4939}
4940
4941/* Placement versions of operator new and delete */
4942inline void operator delete(void *ptr1, void *ptr2)
4943{
4944 (void)ptr1;
4945 (void)ptr2;
4946}
4947
4948inline void operator delete[](void *ptr1, void *ptr2)
4949{
4950 (void)ptr1;
4951 (void)ptr2;
4952}
4953
4954inline void *operator new(size_t size, void *ptr)
4955{
4956 (void)size;
4957 return ptr;
4958}
4959
4960inline void *operator new[](size_t size, void *ptr)
4961{
4962 (void)size;
4963 return ptr;
4964}
4965
4966#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4967
Andrew Boiefa94ee72017-09-28 16:54:35 -07004968#include <syscalls/kernel.h>
4969
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004970#endif /* !_ASMLANGUAGE */
4971
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004972#endif /* _kernel__h_ */