blob: 67345898ffb2de5d6dc80c45c227921f50004b19 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
Flavio Ceolin67ca1762018-09-14 10:43:44 -070013#ifndef ZEPHYR_INCLUDE_KERNEL_H_
14#define ZEPHYR_INCLUDE_KERNEL_H_
Benjamin Walsh456c6da2016-09-02 18:55:39 -040015
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Ioannis Glaropoulos92b8a412018-06-20 17:30:48 +020017#include <kernel_includes.h>
Kumar Gala8777ff12018-07-25 20:24:34 -050018#include <errno.h>
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
Daniel Leungfc182432018-08-16 15:42:28 -0700506#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
507struct _thread_userspace_local_data {
508 int errno_var;
509};
510#endif
511
Anas Nashifce78d162018-05-24 12:43:11 -0500512/**
513 * @ingroup thread_apis
514 * Thread Structure
515 */
Andrew Boie73abd322017-04-04 13:19:13 -0700516struct k_thread {
517
518 struct _thread_base base;
519
Anas Nashifce78d162018-05-24 12:43:11 -0500520 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700521 struct _caller_saved caller_saved;
Anas Nashifce78d162018-05-24 12:43:11 -0500522 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700523 struct _callee_saved callee_saved;
524
Anas Nashifce78d162018-05-24 12:43:11 -0500525 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700526 void *init_data;
527
Anas Nashifce78d162018-05-24 12:43:11 -0500528 /**
529 * abort function
530 * @req K-THREAD-002
531 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700532 void (*fn_abort)(void);
533
534#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500535 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700536 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700537
Anas Nashifce78d162018-05-24 12:43:11 -0500538 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700539 struct k_thread *next_thread;
540#endif
541
542#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500543 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700544 void *custom_data;
545#endif
546
Daniel Leungfc182432018-08-16 15:42:28 -0700547#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
548 struct _thread_userspace_local_data *userspace_local_data;
549#endif
550
Andrew Boie73abd322017-04-04 13:19:13 -0700551#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700552#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500553 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700554 int errno_var;
555#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700556#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700557
558#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500559 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700560 struct _thread_stack_info stack_info;
561#endif /* CONFIG_THREAD_STACK_INFO */
562
Chunlin Hane9c97022017-07-07 20:29:30 +0800563#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500564 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800565 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500566 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700567 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800568#endif /* CONFIG_USERSPACE */
569
Andy Ross042d8ec2017-12-09 08:37:20 -0800570#if defined(CONFIG_USE_SWITCH)
571 /* When using __switch() a few previously arch-specific items
572 * become part of the core OS
573 */
574
Anas Nashifce78d162018-05-24 12:43:11 -0500575 /** _Swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800576 int swap_retval;
577
Anas Nashifce78d162018-05-24 12:43:11 -0500578 /** Context handle returned via _arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800579 void *switch_handle;
580#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500581 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700582 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800583
Anas Nashifce78d162018-05-24 12:43:11 -0500584 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700585 struct _thread_arch arch;
586};
587
588typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400589typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700590#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400591
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400592enum execution_context_types {
593 K_ISR = 0,
594 K_COOP_THREAD,
595 K_PREEMPT_THREAD,
596};
597
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400598/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100599 * @defgroup profiling_apis Profiling APIs
600 * @ingroup kernel_apis
601 * @{
602 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530603typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
604 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100605
606/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530607 * @brief Iterate over all the threads in the system.
608 *
609 * This routine iterates over all the threads in the system and
610 * calls the user_cb function for each thread.
611 *
612 * @param user_cb Pointer to the user callback function.
613 * @param user_data Pointer to user data.
614 *
615 * @note CONFIG_THREAD_MONITOR must be set for this function
616 * to be effective. Also this API uses irq_lock to protect the
617 * _kernel.threads list which means creation of new threads and
618 * terminations of existing threads are blocked until this
619 * API returns.
620 *
621 * @return N/A
622 */
623extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
624
Anas Nashif166f5192018-02-25 08:02:36 -0600625/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100626
627/**
Allan Stephensc98da842016-11-11 15:45:03 -0500628 * @defgroup thread_apis Thread APIs
629 * @ingroup kernel_apis
630 * @{
631 */
632
Benjamin Walshed240f22017-01-22 13:05:08 -0500633#endif /* !_ASMLANGUAGE */
634
635
636/*
637 * Thread user options. May be needed by assembly code. Common part uses low
638 * bits, arch-specific use high bits.
639 */
640
Anas Nashifa541e932018-05-24 11:19:16 -0500641/**
642 * @brief system thread that must not abort
643 * @req K-THREAD-000
644 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700645#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500646
647#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500648/**
649 * @brief thread uses floating point registers
650 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700651#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500652#endif
653
Anas Nashifa541e932018-05-24 11:19:16 -0500654/**
655 * @brief user mode thread
656 *
657 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700658 * has additional restrictions
659 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700660#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700661
Anas Nashifa541e932018-05-24 11:19:16 -0500662/**
663 * @brief Inherit Permissions
664 *
665 * @details
666 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700667 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
668 * is not enabled.
669 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700670#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700671
Benjamin Walshed240f22017-01-22 13:05:08 -0500672#ifdef CONFIG_X86
673/* x86 Bitmask definitions for threads user options */
674
675#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
676/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700677#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500678#endif
679#endif
680
681/* end - thread options */
682
683#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400684/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700685 * @brief Create a thread.
686 *
687 * This routine initializes a thread, then schedules it for execution.
688 *
689 * The new thread may be scheduled for immediate execution or a delayed start.
690 * If the newly spawned thread does not have a delayed start the kernel
691 * scheduler may preempt the current thread to allow the new thread to
692 * execute.
693 *
694 * Thread options are architecture-specific, and can include K_ESSENTIAL,
695 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
696 * them using "|" (the logical OR operator).
697 *
698 * Historically, users often would use the beginning of the stack memory region
699 * to store the struct k_thread data, although corruption will occur if the
700 * stack overflows this region and stack protection features may not detect this
701 * situation.
702 *
703 * @param new_thread Pointer to uninitialized struct k_thread
704 * @param stack Pointer to the stack space.
705 * @param stack_size Stack size in bytes.
706 * @param entry Thread entry function.
707 * @param p1 1st entry point parameter.
708 * @param p2 2nd entry point parameter.
709 * @param p3 3rd entry point parameter.
710 * @param prio Thread priority.
711 * @param options Thread options.
712 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
713 *
714 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400715 *
716 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700717 */
Andrew Boie662c3452017-10-02 10:51:18 -0700718__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700719 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700720 size_t stack_size,
721 k_thread_entry_t entry,
722 void *p1, void *p2, void *p3,
723 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700724
Andrew Boie3f091b52017-08-30 14:34:14 -0700725/**
726 * @brief Drop a thread's privileges permanently to user mode
727 *
728 * @param entry Function to start executing from
729 * @param p1 1st entry point parameter
730 * @param p2 2nd entry point parameter
731 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400732 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700733 */
734extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
735 void *p1, void *p2,
736 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700737
Andrew Boied26cf2d2017-03-30 13:07:02 -0700738/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700739 * @brief Grant a thread access to a NULL-terminated set of kernel objects
740 *
741 * This is a convenience function. For the provided thread, grant access to
742 * the remaining arguments, which must be pointers to kernel objects.
743 * The final argument must be a NULL.
744 *
745 * The thread object must be initialized (i.e. running). The objects don't
746 * need to be.
747 *
748 * @param thread Thread to grant access to objects
749 * @param ... NULL-terminated list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400750 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700751 */
752extern void __attribute__((sentinel))
753 k_thread_access_grant(struct k_thread *thread, ...);
754
755/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700756 * @brief Assign a resource memory pool to a thread
757 *
758 * By default, threads have no resource pool assigned unless their parent
759 * thread has a resource pool, in which case it is inherited. Multiple
760 * threads may be assigned to the same memory pool.
761 *
762 * Changing a thread's resource pool will not migrate allocations from the
763 * previous pool.
764 *
765 * @param thread Target thread to assign a memory pool for resource requests,
766 * or NULL if the thread should no longer have a memory pool.
767 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400768 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700769 */
770static inline void k_thread_resource_pool_assign(struct k_thread *thread,
771 struct k_mem_pool *pool)
772{
773 thread->resource_pool = pool;
774}
775
776#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
777/**
778 * @brief Assign the system heap as a thread's resource pool
779 *
780 * Similar to k_thread_resource_pool_assign(), but the thread will use
781 * the kernel heap to draw memory.
782 *
783 * Use with caution, as a malicious thread could perform DoS attacks on the
784 * kernel heap.
785 *
786 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400787 *
788 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700789 */
790void k_thread_system_pool_assign(struct k_thread *thread);
791#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
792
793/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500794 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400795 *
Allan Stephensc98da842016-11-11 15:45:03 -0500796 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500797 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500799 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400800 *
801 * @return N/A
802 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700803__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400804
805/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500806 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400807 *
808 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500809 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400811 * @return N/A
812 */
Kumar Galacc334c72017-04-21 10:55:34 -0500813extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400814
815/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500816 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400817 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500818 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500820 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400821 *
822 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400823 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400824 */
Andrew Boie468190a2017-09-29 14:00:48 -0700825__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400826
827/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500828 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500830 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400831 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500832 * If @a thread is not currently sleeping, the routine has no effect.
833 *
834 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400835 *
836 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400837 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838 */
Andrew Boie468190a2017-09-29 14:00:48 -0700839__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400840
841/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500842 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400843 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500844 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400845 *
846 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400847 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700848__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400849
850/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500851 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500853 * This routine prevents @a thread from executing if it has not yet started
854 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500856 * @param thread ID of thread to cancel.
857 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700858 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500859 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700860 *
861 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400862 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700863__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400864
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865/**
Allan Stephensc98da842016-11-11 15:45:03 -0500866 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500868 * This routine permanently stops execution of @a thread. The thread is taken
869 * off all kernel queues it is part of (i.e. the ready queue, the timeout
870 * queue, or a kernel object wait queue). However, any kernel resources the
871 * thread might currently own (such as mutexes or memory blocks) are not
872 * released. It is the responsibility of the caller of this routine to ensure
873 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500875 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400876 *
877 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400878 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400879 */
Andrew Boie468190a2017-09-29 14:00:48 -0700880__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400881
Andrew Boie7d627c52017-08-30 11:01:56 -0700882
883/**
884 * @brief Start an inactive thread
885 *
886 * If a thread was created with K_FOREVER in the delay parameter, it will
887 * not be added to the scheduling queue until this function is called
888 * on it.
889 *
890 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400891 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700892 */
Andrew Boie468190a2017-09-29 14:00:48 -0700893__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700894
Allan Stephensc98da842016-11-11 15:45:03 -0500895/**
896 * @cond INTERNAL_HIDDEN
897 */
898
Benjamin Walshd211a522016-12-06 11:44:01 -0500899/* timeout has timed out and is not on _timeout_q anymore */
900#define _EXPIRED (-2)
901
902/* timeout is not in use */
903#define _INACTIVE (-1)
904
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400905struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700906 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700907 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400908 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700909 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500910 void *init_p1;
911 void *init_p2;
912 void *init_p3;
913 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500914 u32_t init_options;
915 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500916 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400917};
918
Andrew Boied26cf2d2017-03-30 13:07:02 -0700919#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400920 entry, p1, p2, p3, \
Anas Nashif3a117c22018-03-02 13:59:58 +0100921 prio, options, delay, abort) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500922 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700923 .init_thread = (thread), \
924 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500925 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700926 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400927 .init_p1 = (void *)p1, \
928 .init_p2 = (void *)p2, \
929 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500930 .init_prio = (prio), \
931 .init_options = (options), \
932 .init_delay = (delay), \
933 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400934 }
935
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400936/**
Allan Stephensc98da842016-11-11 15:45:03 -0500937 * INTERNAL_HIDDEN @endcond
938 */
939
940/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500941 * @brief Statically define and initialize a thread.
942 *
943 * The thread may be scheduled for immediate execution or a delayed start.
944 *
945 * Thread options are architecture-specific, and can include K_ESSENTIAL,
946 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
947 * them using "|" (the logical OR operator).
948 *
949 * The ID of the thread can be accessed using:
950 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500951 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500952 *
953 * @param name Name of the thread.
954 * @param stack_size Stack size in bytes.
955 * @param entry Thread entry function.
956 * @param p1 1st entry point parameter.
957 * @param p2 2nd entry point parameter.
958 * @param p3 3rd entry point parameter.
959 * @param prio Thread priority.
960 * @param options Thread options.
961 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400962 *
Anas Nashif47420d02018-05-24 14:20:56 -0400963 * @req K-THREAD-010
964 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400965 * @internal It has been observed that the x86 compiler by default aligns
966 * these _static_thread_data structures to 32-byte boundaries, thereby
967 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400968 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400969 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500970#define K_THREAD_DEFINE(name, stack_size, \
971 entry, p1, p2, p3, \
972 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700973 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700974 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500975 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500976 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700977 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
978 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500979 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif3a117c22018-03-02 13:59:58 +0100980 NULL); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700981 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400982
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400983/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500984 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500986 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400987 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500988 * @param thread ID of thread whose priority is needed.
989 *
990 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400991 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400992 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700993__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400994
995/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500996 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400997 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500998 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400999 *
1000 * Rescheduling can occur immediately depending on the priority @a thread is
1001 * set to:
1002 *
1003 * - If its priority is raised above the priority of the caller of this
1004 * function, and the caller is preemptible, @a thread will be scheduled in.
1005 *
1006 * - If the caller operates on itself, it lowers its priority below that of
1007 * other threads in the system, and the caller is preemptible, the thread of
1008 * highest priority will be scheduled in.
1009 *
1010 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1011 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1012 * highest priority.
1013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001014 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001015 * @param prio New priority.
1016 *
1017 * @warning Changing the priority of a thread currently involved in mutex
1018 * priority inheritance may result in undefined behavior.
1019 *
1020 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001021 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001022 */
Andrew Boie468190a2017-09-29 14:00:48 -07001023__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001024
Andy Ross4a2e50f2018-05-15 11:06:25 -07001025
1026#ifdef CONFIG_SCHED_DEADLINE
1027/**
1028 * @brief Set deadline expiration time for scheduler
1029 *
1030 * This sets the "deadline" expiration as a time delta from the
1031 * current time, in the same units used by k_cycle_get_32(). The
1032 * scheduler (when deadline scheduling is enabled) will choose the
1033 * next expiring thread when selecting between threads at the same
1034 * static priority. Threads at different priorities will be scheduled
1035 * according to their static priority.
1036 *
1037 * @note Deadlines that are negative (i.e. in the past) are still seen
1038 * as higher priority than others, even if the thread has "finished"
1039 * its work. If you don't want it scheduled anymore, you have to
1040 * reset the deadline into the future, block/pend the thread, or
1041 * modify its priority with k_thread_priority_set().
1042 *
1043 * @note Despite the API naming, the scheduler makes no guarantees the
1044 * the thread WILL be scheduled within that deadline, nor does it take
1045 * extra metadata (like e.g. the "runtime" and "period" parameters in
1046 * Linux sched_setattr()) that allows the kernel to validate the
1047 * scheduling for achievability. Such features could be implemented
1048 * above this call, which is simply input to the priority selection
1049 * logic.
1050 *
1051 * @param thread A thread on which to set the deadline
1052 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001053 *
1054 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001055 */
1056__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1057#endif
1058
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001059/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001060 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001061 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001062 * This routine prevents the kernel scheduler from making @a thread the
1063 * current thread. All other internal operations on @a thread are still
1064 * performed; for example, any timeout it is waiting on keeps ticking,
1065 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001066 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001067 * If @a thread is already suspended, the routine has no effect.
1068 *
1069 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001070 *
1071 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001072 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001073 */
Andrew Boie468190a2017-09-29 14:00:48 -07001074__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001075
1076/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001077 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001078 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001079 * This routine allows the kernel scheduler to make @a thread the current
1080 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001081 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001082 * If @a thread is not currently suspended, the routine has no effect.
1083 *
1084 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001085 *
1086 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001087 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001088 */
Andrew Boie468190a2017-09-29 14:00:48 -07001089__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001090
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001091/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001092 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001094 * This routine specifies how the scheduler will perform time slicing of
1095 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001096 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001097 * To enable time slicing, @a slice must be non-zero. The scheduler
1098 * ensures that no thread runs for more than the specified time limit
1099 * before other threads of that priority are given a chance to execute.
1100 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001101 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001102 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001103 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001104 * execute. Once the scheduler selects a thread for execution, there is no
1105 * minimum guaranteed time the thread will execute before threads of greater or
1106 * equal priority are scheduled.
1107 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001108 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001109 * for execution, this routine has no effect; the thread is immediately
1110 * rescheduled after the slice period expires.
1111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001112 * To disable timeslicing, set both @a slice and @a prio to zero.
1113 *
1114 * @param slice Maximum time slice length (in milliseconds).
1115 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001116 *
1117 * @return N/A
1118 */
Kumar Galacc334c72017-04-21 10:55:34 -05001119extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001120
Anas Nashif166f5192018-02-25 08:02:36 -06001121/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001122
1123/**
1124 * @addtogroup isr_apis
1125 * @{
1126 */
1127
1128/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001129 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001130 *
Allan Stephensc98da842016-11-11 15:45:03 -05001131 * This routine allows the caller to customize its actions, depending on
1132 * whether it is a thread or an ISR.
1133 *
1134 * @note Can be called by ISRs.
1135 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001136 * @return 0 if invoked by a thread.
1137 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001138 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001139extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001140
Benjamin Walsh445830d2016-11-10 15:54:27 -05001141/**
1142 * @brief Determine if code is running in a preemptible thread.
1143 *
Allan Stephensc98da842016-11-11 15:45:03 -05001144 * This routine allows the caller to customize its actions, depending on
1145 * whether it can be preempted by another thread. The routine returns a 'true'
1146 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001147 *
Allan Stephensc98da842016-11-11 15:45:03 -05001148 * - The code is running in a thread, not at ISR.
1149 * - The thread's priority is in the preemptible range.
1150 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001151 *
Allan Stephensc98da842016-11-11 15:45:03 -05001152 * @note Can be called by ISRs.
1153 *
1154 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001155 * @return Non-zero if invoked by a preemptible thread.
1156 */
Andrew Boie468190a2017-09-29 14:00:48 -07001157__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001158
Allan Stephensc98da842016-11-11 15:45:03 -05001159/**
Anas Nashif166f5192018-02-25 08:02:36 -06001160 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001161 */
1162
1163/**
1164 * @addtogroup thread_apis
1165 * @{
1166 */
1167
1168/**
1169 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001170 *
Allan Stephensc98da842016-11-11 15:45:03 -05001171 * This routine prevents the current thread from being preempted by another
1172 * thread by instructing the scheduler to treat it as a cooperative thread.
1173 * If the thread subsequently performs an operation that makes it unready,
1174 * it will be context switched out in the normal manner. When the thread
1175 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001176 *
Allan Stephensc98da842016-11-11 15:45:03 -05001177 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001178 *
Allan Stephensc98da842016-11-11 15:45:03 -05001179 * @note k_sched_lock() and k_sched_unlock() should normally be used
1180 * when the operation being performed can be safely interrupted by ISRs.
1181 * However, if the amount of processing involved is very small, better
1182 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001183 *
1184 * @return N/A
1185 */
1186extern void k_sched_lock(void);
1187
Allan Stephensc98da842016-11-11 15:45:03 -05001188/**
1189 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001190 *
Allan Stephensc98da842016-11-11 15:45:03 -05001191 * This routine reverses the effect of a previous call to k_sched_lock().
1192 * A thread must call the routine once for each time it called k_sched_lock()
1193 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001194 *
1195 * @return N/A
1196 */
1197extern void k_sched_unlock(void);
1198
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001199/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001200 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001201 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001202 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001203 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001204 * Custom data is not used by the kernel itself, and is freely available
1205 * for a thread to use as it sees fit. It can be used as a framework
1206 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001207 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001208 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001209 *
1210 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001211 *
1212 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001213 */
Andrew Boie468190a2017-09-29 14:00:48 -07001214__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001215
1216/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001217 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001218 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001219 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001220 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001221 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001222 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001223 */
Andrew Boie468190a2017-09-29 14:00:48 -07001224__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001225
1226/**
Anas Nashif166f5192018-02-25 08:02:36 -06001227 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001228 */
1229
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001230#include <sys_clock.h>
1231
Allan Stephensc2f15a42016-11-17 12:24:22 -05001232/**
1233 * @addtogroup clock_apis
1234 * @{
1235 */
1236
1237/**
1238 * @brief Generate null timeout delay.
1239 *
1240 * This macro generates a timeout delay that that instructs a kernel API
1241 * not to wait if the requested operation cannot be performed immediately.
1242 *
1243 * @return Timeout delay value.
1244 */
1245#define K_NO_WAIT 0
1246
1247/**
1248 * @brief Generate timeout delay from milliseconds.
1249 *
1250 * This macro generates a timeout delay that that instructs a kernel API
1251 * to wait up to @a ms milliseconds to perform the requested operation.
1252 *
1253 * @param ms Duration in milliseconds.
1254 *
1255 * @return Timeout delay value.
1256 */
Johan Hedberg14471692016-11-13 10:52:15 +02001257#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001258
1259/**
1260 * @brief Generate timeout delay from seconds.
1261 *
1262 * This macro generates a timeout delay that that instructs a kernel API
1263 * to wait up to @a s seconds to perform the requested operation.
1264 *
1265 * @param s Duration in seconds.
1266 *
1267 * @return Timeout delay value.
1268 */
Johan Hedberg14471692016-11-13 10:52:15 +02001269#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001270
1271/**
1272 * @brief Generate timeout delay from minutes.
1273 *
1274 * This macro generates a timeout delay that that instructs a kernel API
1275 * to wait up to @a m minutes to perform the requested operation.
1276 *
1277 * @param m Duration in minutes.
1278 *
1279 * @return Timeout delay value.
1280 */
Johan Hedberg14471692016-11-13 10:52:15 +02001281#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001282
1283/**
1284 * @brief Generate timeout delay from hours.
1285 *
1286 * This macro generates a timeout delay that that instructs a kernel API
1287 * to wait up to @a h hours to perform the requested operation.
1288 *
1289 * @param h Duration in hours.
1290 *
1291 * @return Timeout delay value.
1292 */
Johan Hedberg14471692016-11-13 10:52:15 +02001293#define K_HOURS(h) K_MINUTES((h) * 60)
1294
Allan Stephensc98da842016-11-11 15:45:03 -05001295/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001296 * @brief Generate infinite timeout delay.
1297 *
1298 * This macro generates a timeout delay that that instructs a kernel API
1299 * to wait as long as necessary to perform the requested operation.
1300 *
1301 * @return Timeout delay value.
1302 */
1303#define K_FOREVER (-1)
1304
1305/**
Anas Nashif166f5192018-02-25 08:02:36 -06001306 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001307 */
1308
1309/**
Allan Stephensc98da842016-11-11 15:45:03 -05001310 * @cond INTERNAL_HIDDEN
1311 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001312
Benjamin Walsh62092182016-12-20 14:39:08 -05001313/* kernel clocks */
1314
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001315#ifdef CONFIG_SYS_CLOCK_EXISTS
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001316
1317/*
1318 * If timer frequency is known at compile time, a simple (32-bit)
1319 * tick <-> ms conversion could be used for some combinations of
1320 * hardware timer frequency and tick rate. Otherwise precise
1321 * (64-bit) calculations are used.
1322 */
1323
1324#if !defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
1325#if (sys_clock_hw_cycles_per_sec % sys_clock_ticks_per_sec) != 0
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001326 #define _NEED_PRECISE_TICK_MS_CONVERSION
1327#elif (MSEC_PER_SEC % sys_clock_ticks_per_sec) != 0
Benjamin Walsh62092182016-12-20 14:39:08 -05001328 #define _NON_OPTIMIZED_TICKS_PER_SEC
1329#endif
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001330#endif
Benjamin Walsh62092182016-12-20 14:39:08 -05001331
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001332#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
1333 defined(_NON_OPTIMIZED_TICKS_PER_SEC)
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001334 #define _NEED_PRECISE_TICK_MS_CONVERSION
1335#endif
1336#endif
1337
Kumar Galacc334c72017-04-21 10:55:34 -05001338static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001339{
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001340#ifdef CONFIG_SYS_CLOCK_EXISTS
1341
1342#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
1343 /* use 64-bit math to keep precision */
Piotr Zięcik3c7f9902018-07-23 14:09:10 +02001344 return (s32_t)ceiling_fraction(
1345 (s64_t)ms * sys_clock_hw_cycles_per_sec,
Vinayak Kariappa Chettimadac7d27342018-08-31 08:58:59 +02001346 ((s64_t)MSEC_PER_SEC * sys_clock_hw_cycles_per_sec) /
1347 sys_clock_ticks_per_sec);
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001348#else
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001349 /* simple division keeps precision */
1350 s32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
1351
1352 return (s32_t)ceiling_fraction(ms, ms_per_tick);
1353#endif
1354
1355#else
1356 __ASSERT(ms == 0, "ms not zero");
1357 return 0;
Benjamin Walsh62092182016-12-20 14:39:08 -05001358#endif
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001359}
Benjamin Walsh62092182016-12-20 14:39:08 -05001360
Kumar Galacc334c72017-04-21 10:55:34 -05001361static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001362{
Benjamin Walsh62092182016-12-20 14:39:08 -05001363#ifdef CONFIG_SYS_CLOCK_EXISTS
1364
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001365#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
1366 /* use 64-bit math to keep precision */
Vinayak Kariappa Chettimadac7d27342018-08-31 08:58:59 +02001367 return (u64_t)ticks * MSEC_PER_SEC / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001368#else
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001369 /* simple multiplication keeps precision */
1370 u32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
1371
1372 return (u64_t)ticks * ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001373#endif
1374
1375#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001376 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001377 return 0;
1378#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001379}
1380
Piotr Zięcik77f42f82018-06-11 14:26:29 +02001381/* added tick needed to account for tick in progress */
1382#ifdef CONFIG_TICKLESS_KERNEL
1383#define _TICK_ALIGN 0
1384#else
1385#define _TICK_ALIGN 1
1386#endif
1387
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001388struct k_timer {
1389 /*
1390 * _timeout structure must be first here if we want to use
1391 * dynamic timer allocation. timeout.node is used in the double-linked
1392 * list of free timers
1393 */
1394 struct _timeout timeout;
1395
Allan Stephens45bfa372016-10-12 12:39:42 -05001396 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001397 _wait_q_t wait_q;
1398
1399 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001400 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001401
1402 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001403 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001404
1405 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001406 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001407
Allan Stephens45bfa372016-10-12 12:39:42 -05001408 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001409 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001410
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001411 /* user-specific data, also used to support legacy features */
1412 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001413
Anas Nashif2f203c22016-12-18 06:57:45 -05001414 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001415};
1416
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001417#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001418 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001419 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001420 .timeout.wait_q = NULL, \
1421 .timeout.thread = NULL, \
1422 .timeout.func = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001423 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001424 .expiry_fn = expiry, \
1425 .stop_fn = stop, \
1426 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001427 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001428 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001429 }
1430
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001431#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1432
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001433/**
Allan Stephensc98da842016-11-11 15:45:03 -05001434 * INTERNAL_HIDDEN @endcond
1435 */
1436
1437/**
1438 * @defgroup timer_apis Timer APIs
1439 * @ingroup kernel_apis
1440 * @{
1441 */
1442
1443/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001444 * @typedef k_timer_expiry_t
1445 * @brief Timer expiry function type.
1446 *
1447 * A timer's expiry function is executed by the system clock interrupt handler
1448 * each time the timer expires. The expiry function is optional, and is only
1449 * invoked if the timer has been initialized with one.
1450 *
1451 * @param timer Address of timer.
1452 *
1453 * @return N/A
1454 */
1455typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1456
1457/**
1458 * @typedef k_timer_stop_t
1459 * @brief Timer stop function type.
1460 *
1461 * A timer's stop function is executed if the timer is stopped prematurely.
1462 * The function runs in the context of the thread that stops the timer.
1463 * The stop function is optional, and is only invoked if the timer has been
1464 * initialized with one.
1465 *
1466 * @param timer Address of timer.
1467 *
1468 * @return N/A
1469 */
1470typedef void (*k_timer_stop_t)(struct k_timer *timer);
1471
1472/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001473 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001475 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001476 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001477 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001478 *
1479 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001480 * @param expiry_fn Function to invoke each time the timer expires.
1481 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001482 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001483#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001484 struct k_timer name \
1485 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001486 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001487
Allan Stephens45bfa372016-10-12 12:39:42 -05001488/**
1489 * @brief Initialize a timer.
1490 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001491 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001492 *
1493 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001494 * @param expiry_fn Function to invoke each time the timer expires.
1495 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001496 *
1497 * @return N/A
1498 */
1499extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001500 k_timer_expiry_t expiry_fn,
1501 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001502
Allan Stephens45bfa372016-10-12 12:39:42 -05001503/**
1504 * @brief Start a timer.
1505 *
1506 * This routine starts a timer, and resets its status to zero. The timer
1507 * begins counting down using the specified duration and period values.
1508 *
1509 * Attempting to start a timer that is already running is permitted.
1510 * The timer's status is reset to zero and the timer begins counting down
1511 * using the new duration and period values.
1512 *
1513 * @param timer Address of timer.
1514 * @param duration Initial timer duration (in milliseconds).
1515 * @param period Timer period (in milliseconds).
1516 *
1517 * @return N/A
1518 */
Andrew Boiea354d492017-09-29 16:22:28 -07001519__syscall void k_timer_start(struct k_timer *timer,
1520 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001521
1522/**
1523 * @brief Stop a timer.
1524 *
1525 * This routine stops a running timer prematurely. The timer's stop function,
1526 * if one exists, is invoked by the caller.
1527 *
1528 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001529 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001530 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001531 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1532 * if @a k_timer_stop is to be called from ISRs.
1533 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001534 * @param timer Address of timer.
1535 *
1536 * @return N/A
1537 */
Andrew Boiea354d492017-09-29 16:22:28 -07001538__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001539
1540/**
1541 * @brief Read timer status.
1542 *
1543 * This routine reads the timer's status, which indicates the number of times
1544 * it has expired since its status was last read.
1545 *
1546 * Calling this routine resets the timer's status to zero.
1547 *
1548 * @param timer Address of timer.
1549 *
1550 * @return Timer status.
1551 */
Andrew Boiea354d492017-09-29 16:22:28 -07001552__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001553
1554/**
1555 * @brief Synchronize thread to timer expiration.
1556 *
1557 * This routine blocks the calling thread until the timer's status is non-zero
1558 * (indicating that it has expired at least once since it was last examined)
1559 * or the timer is stopped. If the timer status is already non-zero,
1560 * or the timer is already stopped, the caller continues without waiting.
1561 *
1562 * Calling this routine resets the timer's status to zero.
1563 *
1564 * This routine must not be used by interrupt handlers, since they are not
1565 * allowed to block.
1566 *
1567 * @param timer Address of timer.
1568 *
1569 * @return Timer status.
1570 */
Andrew Boiea354d492017-09-29 16:22:28 -07001571__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001572
1573/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001574 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001575 *
1576 * This routine computes the (approximate) time remaining before a running
1577 * timer next expires. If the timer is not running, it returns zero.
1578 *
1579 * @param timer Address of timer.
1580 *
1581 * @return Remaining time (in milliseconds).
1582 */
Andrew Boiea354d492017-09-29 16:22:28 -07001583__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1584
1585static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001586{
1587 return _timeout_remaining_get(&timer->timeout);
1588}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001589
Allan Stephensc98da842016-11-11 15:45:03 -05001590/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001591 * @brief Associate user-specific data with a timer.
1592 *
1593 * This routine records the @a user_data with the @a timer, to be retrieved
1594 * later.
1595 *
1596 * It can be used e.g. in a timer handler shared across multiple subsystems to
1597 * retrieve data specific to the subsystem this timer is associated with.
1598 *
1599 * @param timer Address of timer.
1600 * @param user_data User data to associate with the timer.
1601 *
1602 * @return N/A
1603 */
Andrew Boiea354d492017-09-29 16:22:28 -07001604__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1605
Anas Nashif954d5502018-02-25 08:37:28 -06001606/**
1607 * @internal
1608 */
Andrew Boiea354d492017-09-29 16:22:28 -07001609static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1610 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001611{
1612 timer->user_data = user_data;
1613}
1614
1615/**
1616 * @brief Retrieve the user-specific data from a timer.
1617 *
1618 * @param timer Address of timer.
1619 *
1620 * @return The user data.
1621 */
Andrew Boiea354d492017-09-29 16:22:28 -07001622__syscall void *k_timer_user_data_get(struct k_timer *timer);
1623
1624static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001625{
1626 return timer->user_data;
1627}
1628
Anas Nashif166f5192018-02-25 08:02:36 -06001629/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001630
Allan Stephensc98da842016-11-11 15:45:03 -05001631/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001632 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001633 * @{
1634 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001635
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001636/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001637 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001638 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001639 * This routine returns the elapsed time since the system booted,
1640 * in milliseconds.
1641 *
1642 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001643 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001644__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001645
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001646/**
1647 * @brief Enable clock always on in tickless kernel
1648 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001649 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001650 * there are no timer events programmed in tickless kernel
1651 * scheduling. This is necessary if the clock is used to track
1652 * passage of time.
1653 *
1654 * @retval prev_status Previous status of always on flag
1655 */
1656static inline int k_enable_sys_clock_always_on(void)
1657{
Flavio Ceolinf23a8cd2018-08-13 12:32:56 -07001658#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001659 int prev_status = _sys_clock_always_on;
1660
1661 _sys_clock_always_on = 1;
1662 _enable_sys_clock();
1663
1664 return prev_status;
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301665#else
Flavio Ceolinf23a8cd2018-08-13 12:32:56 -07001666 return -ENOTSUP;
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301667#endif
Flavio Ceolinf23a8cd2018-08-13 12:32:56 -07001668}
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001669
1670/**
1671 * @brief Disable clock always on in tickless kernel
1672 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001673 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001674 * there are no timer events programmed in tickless kernel
1675 * scheduling. To save power, this routine should be called
1676 * immediately when clock is not used to track time.
1677 */
1678static inline void k_disable_sys_clock_always_on(void)
1679{
Flavio Ceolinf23a8cd2018-08-13 12:32:56 -07001680#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001681 _sys_clock_always_on = 0;
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001682#endif
Flavio Ceolinf23a8cd2018-08-13 12:32:56 -07001683}
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001684
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001685/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001686 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001688 * This routine returns the lower 32-bits of the elapsed time since the system
1689 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001691 * This routine can be more efficient than k_uptime_get(), as it reduces the
1692 * need for interrupt locking and 64-bit math. However, the 32-bit result
1693 * cannot hold a system uptime time larger than approximately 50 days, so the
1694 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001695 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001696 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001697 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001698__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001699
1700/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001701 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001702 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001703 * This routine computes the elapsed time between the current system uptime
1704 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001706 * @param reftime Pointer to a reference time, which is updated to the current
1707 * uptime upon return.
1708 *
1709 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001710 */
Kumar Galacc334c72017-04-21 10:55:34 -05001711extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001712
1713/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001714 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001716 * This routine computes the elapsed time between the current system uptime
1717 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001718 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001719 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1720 * need for interrupt locking and 64-bit math. However, the 32-bit result
1721 * cannot hold an elapsed time larger than approximately 50 days, so the
1722 * caller must handle possible rollovers.
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 u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001730
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001731/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001732 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001733 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001734 * This routine returns the current time, as measured by the system's hardware
1735 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001737 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001738 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001739#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001740
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001741/**
Anas Nashif166f5192018-02-25 08:02:36 -06001742 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001743 */
1744
Allan Stephensc98da842016-11-11 15:45:03 -05001745/**
1746 * @cond INTERNAL_HIDDEN
1747 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001748
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001749struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001750 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001751 union {
1752 _wait_q_t wait_q;
1753
1754 _POLL_EVENT;
1755 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001756
1757 _OBJECT_TRACING_NEXT_PTR(k_queue);
1758};
1759
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001760#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001761 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001762 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001763 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001764 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001765 _OBJECT_TRACING_INIT \
1766 }
1767
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001768#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1769
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001770extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1771
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001772/**
1773 * INTERNAL_HIDDEN @endcond
1774 */
1775
1776/**
1777 * @defgroup queue_apis Queue APIs
1778 * @ingroup kernel_apis
1779 * @{
1780 */
1781
1782/**
1783 * @brief Initialize a queue.
1784 *
1785 * This routine initializes a queue object, prior to its first use.
1786 *
1787 * @param queue Address of the queue.
1788 *
1789 * @return N/A
1790 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001791__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001792
1793/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001794 * @brief Cancel waiting on a queue.
1795 *
1796 * This routine causes first thread pending on @a queue, if any, to
1797 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001798 * If the queue is being waited on by k_poll(), it will return with
1799 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1800 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001801 *
1802 * @note Can be called by ISRs.
1803 *
1804 * @param queue Address of the queue.
1805 *
1806 * @return N/A
1807 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001808__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001809
1810/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001811 * @brief Append an element to the end of a queue.
1812 *
1813 * This routine appends a data item to @a queue. A queue data item must be
1814 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1815 * reserved for the kernel's use.
1816 *
1817 * @note Can be called by ISRs.
1818 *
1819 * @param queue Address of the queue.
1820 * @param data Address of the data item.
1821 *
1822 * @return N/A
1823 */
1824extern void k_queue_append(struct k_queue *queue, void *data);
1825
1826/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001827 * @brief Append an element to a queue.
1828 *
1829 * This routine appends a data item to @a queue. There is an implicit
1830 * memory allocation from the calling thread's resource pool, which is
1831 * automatically freed when the item is removed from the queue.
1832 *
1833 * @note Can be called by ISRs.
1834 *
1835 * @param queue Address of the queue.
1836 * @param data Address of the data item.
1837 *
1838 * @retval 0 on success
1839 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1840 */
1841__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1842
1843/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001844 * @brief Prepend an element to a queue.
1845 *
1846 * This routine prepends a data item to @a queue. A queue data item must be
1847 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1848 * reserved for the kernel's use.
1849 *
1850 * @note Can be called by ISRs.
1851 *
1852 * @param queue Address of the queue.
1853 * @param data Address of the data item.
1854 *
1855 * @return N/A
1856 */
1857extern void k_queue_prepend(struct k_queue *queue, void *data);
1858
1859/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001860 * @brief Prepend an element to a queue.
1861 *
1862 * This routine prepends a data item to @a queue. There is an implicit
1863 * memory allocation from the calling thread's resource pool, which is
1864 * automatically freed when the item is removed from the queue.
1865 *
1866 * @note Can be called by ISRs.
1867 *
1868 * @param queue Address of the queue.
1869 * @param data Address of the data item.
1870 *
1871 * @retval 0 on success
1872 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1873 */
1874__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1875
1876/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001877 * @brief Inserts an element to a queue.
1878 *
1879 * This routine inserts a data item to @a queue after previous item. A queue
1880 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1881 * item are reserved for the kernel's use.
1882 *
1883 * @note Can be called by ISRs.
1884 *
1885 * @param queue Address of the queue.
1886 * @param prev Address of the previous data item.
1887 * @param data Address of the data item.
1888 *
1889 * @return N/A
1890 */
1891extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1892
1893/**
1894 * @brief Atomically append a list of elements to a queue.
1895 *
1896 * This routine adds a list of data items to @a queue in one operation.
1897 * The data items must be in a singly-linked list, with the first 32 bits
1898 * in each data item pointing to the next data item; the list must be
1899 * NULL-terminated.
1900 *
1901 * @note Can be called by ISRs.
1902 *
1903 * @param queue Address of the queue.
1904 * @param head Pointer to first node in singly-linked list.
1905 * @param tail Pointer to last node in singly-linked list.
1906 *
1907 * @return N/A
1908 */
1909extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1910
1911/**
1912 * @brief Atomically add a list of elements to a queue.
1913 *
1914 * This routine adds a list of data items to @a queue in one operation.
1915 * The data items must be in a singly-linked list implemented using a
1916 * sys_slist_t object. Upon completion, the original list is empty.
1917 *
1918 * @note Can be called by ISRs.
1919 *
1920 * @param queue Address of the queue.
1921 * @param list Pointer to sys_slist_t object.
1922 *
1923 * @return N/A
1924 */
1925extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1926
1927/**
1928 * @brief Get an element from a queue.
1929 *
1930 * This routine removes first data item from @a queue. The first 32 bits of the
1931 * data item are reserved for the kernel's use.
1932 *
1933 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1934 *
1935 * @param queue Address of the queue.
1936 * @param timeout Waiting period to obtain a data item (in milliseconds),
1937 * or one of the special values K_NO_WAIT and K_FOREVER.
1938 *
1939 * @return Address of the data item if successful; NULL if returned
1940 * without waiting, or waiting period timed out.
1941 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001942__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001943
1944/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001945 * @brief Remove an element from a queue.
1946 *
1947 * This routine removes data item from @a queue. The first 32 bits of the
1948 * data item are reserved for the kernel's use. Removing elements from k_queue
1949 * rely on sys_slist_find_and_remove which is not a constant time operation.
1950 *
1951 * @note Can be called by ISRs
1952 *
1953 * @param queue Address of the queue.
1954 * @param data Address of the data item.
1955 *
1956 * @return true if data item was removed
1957 */
1958static inline bool k_queue_remove(struct k_queue *queue, void *data)
1959{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001960 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001961}
1962
1963/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001964 * @brief Query a queue to see if it has data available.
1965 *
1966 * Note that the data might be already gone by the time this function returns
1967 * if other threads are also trying to read from the queue.
1968 *
1969 * @note Can be called by ISRs.
1970 *
1971 * @param queue Address of the queue.
1972 *
1973 * @return Non-zero if the queue is empty.
1974 * @return 0 if data is available.
1975 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001976__syscall int k_queue_is_empty(struct k_queue *queue);
1977
1978static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001979{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001980 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001981}
1982
1983/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001984 * @brief Peek element at the head of queue.
1985 *
1986 * Return element from the head of queue without removing it.
1987 *
1988 * @param queue Address of the queue.
1989 *
1990 * @return Head element, or NULL if queue is empty.
1991 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001992__syscall void *k_queue_peek_head(struct k_queue *queue);
1993
1994static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001995{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001996 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001997}
1998
1999/**
2000 * @brief Peek element at the tail of queue.
2001 *
2002 * Return element from the tail of queue without removing it.
2003 *
2004 * @param queue Address of the queue.
2005 *
2006 * @return Tail element, or NULL if queue is empty.
2007 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002008__syscall void *k_queue_peek_tail(struct k_queue *queue);
2009
2010static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002011{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002012 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002013}
2014
2015/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002016 * @brief Statically define and initialize a queue.
2017 *
2018 * The queue can be accessed outside the module where it is defined using:
2019 *
2020 * @code extern struct k_queue <name>; @endcode
2021 *
2022 * @param name Name of the queue.
2023 */
2024#define K_QUEUE_DEFINE(name) \
2025 struct k_queue name \
2026 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002027 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002028
Anas Nashif166f5192018-02-25 08:02:36 -06002029/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002030
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002031struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002032 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002033};
2034
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002035/**
2036 * @cond INTERNAL_HIDDEN
2037 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002038#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002039 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002040 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002041 }
2042
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002043#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
2044
Allan Stephensc98da842016-11-11 15:45:03 -05002045/**
2046 * INTERNAL_HIDDEN @endcond
2047 */
2048
2049/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002050 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002051 * @ingroup kernel_apis
2052 * @{
2053 */
2054
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002055/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002056 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002057 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002058 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002059 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002060 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002061 *
2062 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002063 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002064 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002065#define k_fifo_init(fifo) \
2066 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002067
2068/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002069 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002070 *
2071 * This routine causes first thread pending on @a fifo, if any, to
2072 * return from k_fifo_get() call with NULL value (as if timeout
2073 * expired).
2074 *
2075 * @note Can be called by ISRs.
2076 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002077 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002078 *
2079 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002080 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002081 */
2082#define k_fifo_cancel_wait(fifo) \
2083 k_queue_cancel_wait((struct k_queue *) fifo)
2084
2085/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002086 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002087 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002088 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002089 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2090 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002092 * @note Can be called by ISRs.
2093 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002094 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002095 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002096 *
2097 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002098 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002099 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002100#define k_fifo_put(fifo, data) \
2101 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002102
2103/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002104 * @brief Add an element to a FIFO queue.
2105 *
2106 * This routine adds a data item to @a fifo. There is an implicit
2107 * memory allocation from the calling thread's resource pool, which is
2108 * automatically freed when the item is removed.
2109 *
2110 * @note Can be called by ISRs.
2111 *
2112 * @param fifo Address of the FIFO.
2113 * @param data Address of the data item.
2114 *
2115 * @retval 0 on success
2116 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002117 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002118 */
2119#define k_fifo_alloc_put(fifo, data) \
2120 k_queue_alloc_append((struct k_queue *) fifo, data)
2121
2122/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002123 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002125 * This routine adds a list of data items to @a fifo in one operation.
2126 * The data items must be in a singly-linked list, with the first 32 bits
2127 * each data item pointing to the next data item; the list must be
2128 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002130 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002131 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002132 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002133 * @param head Pointer to first node in singly-linked list.
2134 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002135 *
2136 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002137 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002138 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002139#define k_fifo_put_list(fifo, head, tail) \
2140 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002141
2142/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002143 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002144 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002145 * This routine adds a list of data items to @a fifo in one operation.
2146 * The data items must be in a singly-linked list implemented using a
2147 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002148 * and must be re-initialized via sys_slist_init().
2149 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002150 * @note Can be called by ISRs.
2151 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002152 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002153 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002154 *
2155 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002156 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002157 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002158#define k_fifo_put_slist(fifo, list) \
2159 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002160
2161/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002162 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002164 * This routine removes a data item from @a fifo in a "first in, first out"
2165 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002167 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2168 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002169 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002170 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002171 * or one of the special values K_NO_WAIT and K_FOREVER.
2172 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002173 * @return Address of the data item if successful; NULL if returned
2174 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002175 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002176 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002177#define k_fifo_get(fifo, timeout) \
2178 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002179
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002180/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002181 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002182 *
2183 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002184 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002185 *
2186 * @note Can be called by ISRs.
2187 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002188 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002189 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002190 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002191 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002192 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002193 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002194#define k_fifo_is_empty(fifo) \
2195 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002196
2197/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002198 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002199 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002200 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302201 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002202 * on each iteration of processing, a head container will be peeked,
2203 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002204 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002205 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002206 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002207 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002208 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002209 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002210 */
2211#define k_fifo_peek_head(fifo) \
2212 k_queue_peek_head((struct k_queue *) fifo)
2213
2214/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002215 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002216 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002217 * Return element from the tail of FIFO queue (without removing it). A usecase
2218 * for this is if elements of the FIFO queue are themselves containers. Then
2219 * it may be useful to add more data to the last container in a 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 Tail element, or NULL if a 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_tail(fifo) \
2227 k_queue_peek_tail((struct k_queue *) fifo)
2228
2229/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002230 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002231 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002232 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002233 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002234 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002235 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002236 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002237 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002238 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002239#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002240 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002241 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002242 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002243
Anas Nashif166f5192018-02-25 08:02:36 -06002244/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002245
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002246struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002247 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002248};
2249
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002250/**
2251 * @cond INTERNAL_HIDDEN
2252 */
2253
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002254#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002255 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002256 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002257 }
2258
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002259#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2260
Allan Stephensc98da842016-11-11 15:45:03 -05002261/**
2262 * INTERNAL_HIDDEN @endcond
2263 */
2264
2265/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002266 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002267 * @ingroup kernel_apis
2268 * @{
2269 */
2270
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002271/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002272 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002273 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002274 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002275 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002276 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002277 *
2278 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002279 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002280 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002281#define k_lifo_init(lifo) \
2282 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002283
2284/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002285 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002286 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002287 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002288 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2289 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002290 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002291 * @note Can be called by ISRs.
2292 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002293 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002294 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002295 *
2296 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002297 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002298 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002299#define k_lifo_put(lifo, data) \
2300 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002301
2302/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002303 * @brief Add an element to a LIFO queue.
2304 *
2305 * This routine adds a data item to @a lifo. There is an implicit
2306 * memory allocation from the calling thread's resource pool, which is
2307 * automatically freed when the item is removed.
2308 *
2309 * @note Can be called by ISRs.
2310 *
2311 * @param lifo Address of the LIFO.
2312 * @param data Address of the data item.
2313 *
2314 * @retval 0 on success
2315 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002316 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002317 */
2318#define k_lifo_alloc_put(lifo, data) \
2319 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2320
2321/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002322 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002323 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002324 * This routine removes a data item from @a lifo in a "last in, first out"
2325 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002326 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002327 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2328 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002329 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002330 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002331 * or one of the special values K_NO_WAIT and K_FOREVER.
2332 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002333 * @return Address of the data item if successful; NULL if returned
2334 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002335 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002336 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002337#define k_lifo_get(lifo, timeout) \
2338 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002339
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002340/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002341 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002342 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002343 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002344 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002345 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002346 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002347 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002348 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002349 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002350#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002351 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002352 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002353 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002354
Anas Nashif166f5192018-02-25 08:02:36 -06002355/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002356
2357/**
2358 * @cond INTERNAL_HIDDEN
2359 */
Andrew Boief3bee952018-05-02 17:44:39 -07002360#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002361
2362struct k_stack {
2363 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002364 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002365
Anas Nashif2f203c22016-12-18 06:57:45 -05002366 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002367 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002368};
2369
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002370#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002371 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002372 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002373 .base = stack_buffer, \
2374 .next = stack_buffer, \
2375 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002376 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002377 }
2378
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002379#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2380
Allan Stephensc98da842016-11-11 15:45:03 -05002381/**
2382 * INTERNAL_HIDDEN @endcond
2383 */
2384
2385/**
2386 * @defgroup stack_apis Stack APIs
2387 * @ingroup kernel_apis
2388 * @{
2389 */
2390
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002391/**
2392 * @brief Initialize a stack.
2393 *
2394 * This routine initializes a stack object, prior to its first use.
2395 *
2396 * @param stack Address of the stack.
2397 * @param buffer Address of array used to hold stacked values.
2398 * @param num_entries Maximum number of values that can be stacked.
2399 *
2400 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002401 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002402 */
Andrew Boief3bee952018-05-02 17:44:39 -07002403void k_stack_init(struct k_stack *stack,
2404 u32_t *buffer, unsigned int num_entries);
2405
2406
2407/**
2408 * @brief Initialize a stack.
2409 *
2410 * This routine initializes a stack object, prior to its first use. Internal
2411 * buffers will be allocated from the calling thread's resource pool.
2412 * This memory will be released if k_stack_cleanup() is called, or
2413 * userspace is enabled and the stack object loses all references to it.
2414 *
2415 * @param stack Address of the stack.
2416 * @param num_entries Maximum number of values that can be stacked.
2417 *
2418 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002419 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002420 */
2421
2422__syscall int k_stack_alloc_init(struct k_stack *stack,
2423 unsigned int num_entries);
2424
2425/**
2426 * @brief Release a stack's allocated buffer
2427 *
2428 * If a stack object was given a dynamically allocated buffer via
2429 * k_stack_alloc_init(), this will free it. This function does nothing
2430 * if the buffer wasn't dynamically allocated.
2431 *
2432 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002433 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002434 */
2435void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002436
2437/**
2438 * @brief Push an element onto a stack.
2439 *
2440 * This routine adds a 32-bit value @a data to @a stack.
2441 *
2442 * @note Can be called by ISRs.
2443 *
2444 * @param stack Address of the stack.
2445 * @param data Value to push onto the stack.
2446 *
2447 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002448 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002449 */
Andrew Boiee8734462017-09-29 16:42:07 -07002450__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002451
2452/**
2453 * @brief Pop an element from a stack.
2454 *
2455 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2456 * manner and stores the value in @a data.
2457 *
2458 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2459 *
2460 * @param stack Address of the stack.
2461 * @param data Address of area to hold the value popped from the stack.
2462 * @param timeout Waiting period to obtain a value (in milliseconds),
2463 * or one of the special values K_NO_WAIT and K_FOREVER.
2464 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002465 * @retval 0 Element popped from stack.
2466 * @retval -EBUSY Returned without waiting.
2467 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002468 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002469 */
Andrew Boiee8734462017-09-29 16:42:07 -07002470__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002471
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002472/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002473 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002475 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002476 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002477 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002479 * @param name Name of the stack.
2480 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002481 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002482 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002483#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002484 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002485 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002486 struct k_stack name \
2487 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002488 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002489 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002490
Anas Nashif166f5192018-02-25 08:02:36 -06002491/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002492
Allan Stephens6bba9b02016-11-16 14:56:54 -05002493struct k_work;
2494
Allan Stephensc98da842016-11-11 15:45:03 -05002495/**
2496 * @defgroup workqueue_apis Workqueue Thread APIs
2497 * @ingroup kernel_apis
2498 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002499 */
2500
Allan Stephens6bba9b02016-11-16 14:56:54 -05002501/**
2502 * @typedef k_work_handler_t
2503 * @brief Work item handler function type.
2504 *
2505 * A work item's handler function is executed by a workqueue's thread
2506 * when the work item is processed by the workqueue.
2507 *
2508 * @param work Address of the work item.
2509 *
2510 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002511 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002512 */
2513typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002514
2515/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002516 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002517 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002518
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002519struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002520 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002521 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002522};
2523
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002524enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002525 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002526};
2527
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002528struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002529 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002530 k_work_handler_t handler;
2531 atomic_t flags[1];
2532};
2533
Allan Stephens6bba9b02016-11-16 14:56:54 -05002534struct k_delayed_work {
2535 struct k_work work;
2536 struct _timeout timeout;
2537 struct k_work_q *work_q;
2538};
2539
2540extern struct k_work_q k_sys_work_q;
2541
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002542/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002543 * INTERNAL_HIDDEN @endcond
2544 */
2545
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002546#define _K_WORK_INITIALIZER(work_handler) \
2547 { \
2548 ._reserved = NULL, \
2549 .handler = work_handler, \
2550 .flags = { 0 } \
2551 }
2552
2553#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2554
Allan Stephens6bba9b02016-11-16 14:56:54 -05002555/**
2556 * @brief Initialize a statically-defined work item.
2557 *
2558 * This macro can be used to initialize a statically-defined workqueue work
2559 * item, prior to its first use. For example,
2560 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002561 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002562 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002563 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002564 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002565 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002566 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002567#define K_WORK_DEFINE(work, work_handler) \
2568 struct k_work work \
2569 __in_section(_k_work, static, work) = \
2570 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002571
2572/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002573 * @brief Initialize a work item.
2574 *
2575 * This routine initializes a workqueue work item, prior to its first use.
2576 *
2577 * @param work Address of work item.
2578 * @param handler Function to invoke each time work item is processed.
2579 *
2580 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002581 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002582 */
2583static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2584{
Leandro Pereira0e23ad82018-05-29 10:42:17 -07002585 *work = (struct k_work)_K_WORK_INITIALIZER(handler);
Andrew Boie945af952017-08-22 13:15:23 -07002586 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002587}
2588
2589/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002590 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002591 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002592 * This routine submits work item @a work to be processed by workqueue
2593 * @a work_q. If the work item is already pending in the workqueue's queue
2594 * as a result of an earlier submission, this routine has no effect on the
2595 * work item. If the work item has already been processed, or is currently
2596 * being processed, its work is considered complete and the work item can be
2597 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002598 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002599 * @warning
2600 * A submitted work item must not be modified until it has been processed
2601 * by the workqueue.
2602 *
2603 * @note Can be called by ISRs.
2604 *
2605 * @param work_q Address of workqueue.
2606 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002607 *
2608 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002609 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002610 */
2611static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2612 struct k_work *work)
2613{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002614 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002615 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002616 }
2617}
2618
2619/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002620 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002621 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002622 * This routine indicates if work item @a work is pending in a workqueue's
2623 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002624 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002625 * @note Can be called by ISRs.
2626 *
2627 * @param work Address of work item.
2628 *
2629 * @return 1 if work item is pending, or 0 if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002630 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002631 */
2632static inline int k_work_pending(struct k_work *work)
2633{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002634 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002635}
2636
2637/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002638 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002639 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002640 * This routine starts workqueue @a work_q. The workqueue spawns its work
2641 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002642 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002643 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002644 * @param stack Pointer to work queue thread's stack space, as defined by
2645 * K_THREAD_STACK_DEFINE()
2646 * @param stack_size Size of the work queue thread's stack (in bytes), which
2647 * should either be the same constant passed to
2648 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002649 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002650 *
2651 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002652 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002653 */
Andrew Boie507852a2017-07-25 18:47:07 -07002654extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002655 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002656 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002657
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002658/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002659 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002660 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002661 * This routine initializes a workqueue delayed work item, prior to
2662 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002663 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002664 * @param work Address of delayed work item.
2665 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002666 *
2667 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002668 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002669 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002670extern void k_delayed_work_init(struct k_delayed_work *work,
2671 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002672
2673/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002674 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002675 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002676 * This routine schedules work item @a work to be processed by workqueue
2677 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002678 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002679 * Only when the countdown completes is the work item actually submitted to
2680 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002681 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002682 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002683 * counting down cancels the existing submission and restarts the
2684 * countdown using the new delay. Note that this behavior is
2685 * inherently subject to race conditions with the pre-existing
2686 * timeouts and work queue, so care must be taken to synchronize such
2687 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002688 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002689 * @warning
2690 * A delayed work item must not be modified until it has been processed
2691 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002692 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002693 * @note Can be called by ISRs.
2694 *
2695 * @param work_q Address of workqueue.
2696 * @param work Address of delayed work item.
2697 * @param delay Delay before submitting the work item (in milliseconds).
2698 *
2699 * @retval 0 Work item countdown started.
2700 * @retval -EINPROGRESS Work item is already pending.
2701 * @retval -EINVAL Work item is being processed or has completed its work.
2702 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002703 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002704 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002705extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2706 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002707 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002708
2709/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002710 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002711 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002712 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002713 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002714 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002715 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002716 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002717 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002718 * @param work Address of delayed work item.
2719 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002720 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002721 * @retval -EINPROGRESS Work item is already pending.
2722 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002723 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002724 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002725extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002726
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002727/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002728 * @brief Submit a work item to the system workqueue.
2729 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002730 * This routine submits work item @a work to be processed by the system
2731 * workqueue. If the work item is already pending in the workqueue's queue
2732 * as a result of an earlier submission, this routine has no effect on the
2733 * work item. If the work item has already been processed, or is currently
2734 * being processed, its work is considered complete and the work item can be
2735 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002736 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002737 * @warning
2738 * Work items submitted to the system workqueue should avoid using handlers
2739 * that block or yield since this may prevent the system workqueue from
2740 * processing other work items in a timely manner.
2741 *
2742 * @note Can be called by ISRs.
2743 *
2744 * @param work Address of work item.
2745 *
2746 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002747 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002748 */
2749static inline void k_work_submit(struct k_work *work)
2750{
2751 k_work_submit_to_queue(&k_sys_work_q, work);
2752}
2753
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002754/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002755 * @brief Submit a delayed work item to the system workqueue.
2756 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002757 * This routine schedules work item @a work to be processed by the system
2758 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002759 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002760 * Only when the countdown completes is the work item actually submitted to
2761 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002762 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002763 * Submitting a previously submitted delayed work item that is still
2764 * counting down cancels the existing submission and restarts the countdown
2765 * using the new delay. If the work item is currently pending on the
2766 * workqueue's queue because the countdown has completed it is too late to
2767 * resubmit the item, and resubmission fails without impacting the work item.
2768 * If the work item has already been processed, or is currently being processed,
2769 * its work is considered complete and the work item can be resubmitted.
2770 *
2771 * @warning
2772 * Work items submitted to the system workqueue should avoid using handlers
2773 * that block or yield since this may prevent the system workqueue from
2774 * processing other work items in a timely manner.
2775 *
2776 * @note Can be called by ISRs.
2777 *
2778 * @param work Address of delayed work item.
2779 * @param delay Delay before submitting the work item (in milliseconds).
2780 *
2781 * @retval 0 Work item countdown started.
2782 * @retval -EINPROGRESS Work item is already pending.
2783 * @retval -EINVAL Work item is being processed or has completed its work.
2784 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002785 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002786 */
2787static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002788 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002789{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002790 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002791}
2792
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002793/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002794 * @brief Get time remaining before a delayed work gets scheduled.
2795 *
2796 * This routine computes the (approximate) time remaining before a
2797 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002798 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002799 *
2800 * @param work Delayed work item.
2801 *
2802 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002803 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002804 */
Kumar Galacc334c72017-04-21 10:55:34 -05002805static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002806{
2807 return _timeout_remaining_get(&work->timeout);
2808}
2809
Anas Nashif166f5192018-02-25 08:02:36 -06002810/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002811/**
Anas Nashifce78d162018-05-24 12:43:11 -05002812 * @defgroup mutex_apis Mutex APIs
2813 * @ingroup kernel_apis
2814 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002815 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002816
Anas Nashifce78d162018-05-24 12:43:11 -05002817/**
2818 * Mutex Structure
2819 * @ingroup mutex_apis
2820 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002821struct k_mutex {
2822 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002823 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002824 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002825 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002826 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002827
Anas Nashif2f203c22016-12-18 06:57:45 -05002828 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002829};
2830
Anas Nashifce78d162018-05-24 12:43:11 -05002831/**
2832 * @cond INTERNAL_HIDDEN
2833 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002834#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002835 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002836 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002837 .owner = NULL, \
2838 .lock_count = 0, \
2839 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002840 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002841 }
2842
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002843#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2844
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002845/**
Allan Stephensc98da842016-11-11 15:45:03 -05002846 * INTERNAL_HIDDEN @endcond
2847 */
2848
2849/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002850 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002852 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002853 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002854 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002856 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002857 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002858 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002859#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002860 struct k_mutex name \
2861 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002862 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002863
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002864/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002865 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002867 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002869 * Upon completion, the mutex is available and does not have an owner.
2870 *
2871 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002872 *
2873 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002874 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002875 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002876__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002877
2878/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002879 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002881 * This routine locks @a mutex. If the mutex is locked by another thread,
2882 * the calling thread waits until the mutex becomes available or until
2883 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002885 * A thread is permitted to lock a mutex it has already locked. The operation
2886 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002888 * @param mutex Address of the mutex.
2889 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002890 * or one of the special values K_NO_WAIT and K_FOREVER.
2891 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002892 * @retval 0 Mutex locked.
2893 * @retval -EBUSY Returned without waiting.
2894 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002895 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002896 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002897__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002898
2899/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002900 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002902 * This routine unlocks @a mutex. The mutex must already be locked by the
2903 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002904 *
2905 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002906 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002907 * thread.
2908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002909 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002910 *
2911 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002912 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002913 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002914__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002915
Allan Stephensc98da842016-11-11 15:45:03 -05002916/**
Anas Nashif166f5192018-02-25 08:02:36 -06002917 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002918 */
2919
2920/**
2921 * @cond INTERNAL_HIDDEN
2922 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002923
2924struct k_sem {
2925 _wait_q_t wait_q;
2926 unsigned int count;
2927 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002928 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002929
Anas Nashif2f203c22016-12-18 06:57:45 -05002930 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002931};
2932
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002933#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002934 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002935 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002936 .count = initial_count, \
2937 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002938 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002939 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002940 }
2941
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002942#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2943
Allan Stephensc98da842016-11-11 15:45:03 -05002944/**
2945 * INTERNAL_HIDDEN @endcond
2946 */
2947
2948/**
2949 * @defgroup semaphore_apis Semaphore APIs
2950 * @ingroup kernel_apis
2951 * @{
2952 */
2953
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002954/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002955 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002957 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002958 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002959 * @param sem Address of the semaphore.
2960 * @param initial_count Initial semaphore count.
2961 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002962 *
2963 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002964 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002965 */
Andrew Boie99280232017-09-29 14:17:47 -07002966__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2967 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002968
2969/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002970 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002972 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002974 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2975 *
2976 * @param sem Address of the semaphore.
2977 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002978 * or one of the special values K_NO_WAIT and K_FOREVER.
2979 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002980 * @note When porting code from the nanokernel legacy API to the new API, be
2981 * careful with the return value of this function. The return value is the
2982 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2983 * non-zero means failure, while the nano_sem_take family returns 1 for success
2984 * and 0 for failure.
2985 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002986 * @retval 0 Semaphore taken.
2987 * @retval -EBUSY Returned without waiting.
2988 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002989 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002990 */
Andrew Boie99280232017-09-29 14:17:47 -07002991__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002992
2993/**
2994 * @brief Give a semaphore.
2995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002996 * This routine gives @a sem, unless the semaphore is already at its maximum
2997 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002999 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003001 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003002 *
3003 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003004 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003005 */
Andrew Boie99280232017-09-29 14:17:47 -07003006__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003007
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003008/**
3009 * @brief Reset a semaphore's count to zero.
3010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003011 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003013 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003014 *
3015 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003016 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003017 */
Andrew Boie990bf162017-10-03 12:36:49 -07003018__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003019
Anas Nashif954d5502018-02-25 08:37:28 -06003020/**
3021 * @internal
3022 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003023static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003024{
3025 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003026}
3027
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003028/**
3029 * @brief Get a semaphore's count.
3030 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003031 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003033 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003035 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003036 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003037 */
Andrew Boie990bf162017-10-03 12:36:49 -07003038__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003039
Anas Nashif954d5502018-02-25 08:37:28 -06003040/**
3041 * @internal
3042 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003043static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003044{
3045 return sem->count;
3046}
3047
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003048/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003049 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003050 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003051 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003052 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003053 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003055 * @param name Name of the semaphore.
3056 * @param initial_count Initial semaphore count.
3057 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003058 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003059 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003060#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003061 struct k_sem name \
3062 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003063 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303064 BUILD_ASSERT(((count_limit) != 0) && \
3065 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003066
Anas Nashif166f5192018-02-25 08:02:36 -06003067/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003068
3069/**
3070 * @defgroup alert_apis Alert APIs
3071 * @ingroup kernel_apis
3072 * @{
3073 */
3074
Allan Stephens5eceb852016-11-16 10:16:30 -05003075/**
3076 * @typedef k_alert_handler_t
3077 * @brief Alert handler function type.
3078 *
3079 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07003080 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05003081 * and is only invoked if the alert has been initialized with one.
3082 *
3083 * @param alert Address of the alert.
3084 *
3085 * @return 0 if alert has been consumed; non-zero if alert should pend.
3086 */
3087typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05003088
Anas Nashif166f5192018-02-25 08:02:36 -06003089/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003090
3091/**
3092 * @cond INTERNAL_HIDDEN
3093 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003094
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003095#define K_ALERT_DEFAULT NULL
3096#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003097
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003098struct k_alert {
3099 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003100 atomic_t send_count;
3101 struct k_work work_item;
3102 struct k_sem sem;
3103
Anas Nashif2f203c22016-12-18 06:57:45 -05003104 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003105};
3106
Anas Nashif954d5502018-02-25 08:37:28 -06003107/**
3108 * @internal
3109 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003110extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003111
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003112#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003113 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003114 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003115 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003116 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
3117 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003118 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003119 }
3120
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003121#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
3122
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003123/**
Allan Stephensc98da842016-11-11 15:45:03 -05003124 * INTERNAL_HIDDEN @endcond
3125 */
3126
3127/**
3128 * @addtogroup alert_apis
3129 * @{
3130 */
3131
3132/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003133 * @def K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts)
3134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003135 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003136 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003137 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003138 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003139 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003140 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003141 * @param name Name of the alert.
3142 * @param alert_handler Action to take when alert is sent. Specify either
3143 * the address of a function to be invoked by the system workqueue
3144 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
3145 * K_ALERT_DEFAULT (which causes the alert to pend).
3146 * @param max_num_pending_alerts Maximum number of pending alerts.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003147 *
3148 * @req K-ALERT-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003149 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003150#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003151 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003152 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003153 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003154 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003155
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003156/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003157 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003158 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003159 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003160 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003161 * @param alert Address of the alert.
3162 * @param handler Action to take when alert is sent. Specify either the address
3163 * of a function to be invoked by the system workqueue thread,
3164 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3165 * K_ALERT_DEFAULT (which causes the alert to pend).
3166 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003167 *
3168 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003169 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003170 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003171extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3172 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003173
3174/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003175 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003176 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003177 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003179 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3180 *
3181 * @param alert Address of the alert.
3182 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003183 * or one of the special values K_NO_WAIT and K_FOREVER.
3184 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003185 * @retval 0 Alert received.
3186 * @retval -EBUSY Returned without waiting.
3187 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003188 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003189 */
Andrew Boie310e9872017-09-29 04:41:15 -07003190__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003191
3192/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003193 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003195 * This routine signals @a alert. The action specified for @a alert will
3196 * be taken, which may trigger the execution of an alert handler function
3197 * and/or cause the alert to pend (assuming the alert has not reached its
3198 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003199 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003200 * @note Can be called by ISRs.
3201 *
3202 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003203 *
3204 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003205 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003206 */
Andrew Boie310e9872017-09-29 04:41:15 -07003207__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003208
3209/**
Anas Nashif166f5192018-02-25 08:02:36 -06003210 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003211 */
3212
Allan Stephensc98da842016-11-11 15:45:03 -05003213/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003214 * @defgroup msgq_apis Message Queue APIs
3215 * @ingroup kernel_apis
3216 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003217 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003218
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003219/**
3220 * @brief Message Queue Structure
3221 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003222struct k_msgq {
3223 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003224 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003225 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003226 char *buffer_start;
3227 char *buffer_end;
3228 char *read_ptr;
3229 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003230 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003231
Anas Nashif2f203c22016-12-18 06:57:45 -05003232 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003233 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003234};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003235/**
3236 * @cond INTERNAL_HIDDEN
3237 */
3238
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003239
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003240#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003241 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003242 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003243 .max_msgs = q_max_msgs, \
3244 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003245 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003246 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003247 .read_ptr = q_buffer, \
3248 .write_ptr = q_buffer, \
3249 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003250 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003251 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003252#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003253/**
3254 * INTERNAL_HIDDEN @endcond
3255 */
3256
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003257
Andrew Boie0fe789f2018-04-12 18:35:56 -07003258#define K_MSGQ_FLAG_ALLOC BIT(0)
3259
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003260/**
3261 * @brief Message Queue Attributes
3262 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303263struct k_msgq_attrs {
3264 size_t msg_size;
3265 u32_t max_msgs;
3266 u32_t used_msgs;
3267};
3268
Allan Stephensc98da842016-11-11 15:45:03 -05003269
3270/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003271 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003273 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3274 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003275 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3276 * message is similarly aligned to this boundary, @a q_msg_size must also be
3277 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003279 * The message queue can be accessed outside the module where it is defined
3280 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003281 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003282 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003283 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003284 * @param q_name Name of the message queue.
3285 * @param q_msg_size Message size (in bytes).
3286 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003287 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003288 *
3289 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003290 */
3291#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003292 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003293 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003294 struct k_msgq q_name \
3295 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003296 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003297 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003298
Peter Mitsisd7a37502016-10-13 11:37:40 -04003299/**
3300 * @brief Initialize a message queue.
3301 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003302 * This routine initializes a message queue object, prior to its first use.
3303 *
Allan Stephensda827222016-11-09 14:23:58 -06003304 * The message queue's ring buffer must contain space for @a max_msgs messages,
3305 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3306 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3307 * that each message is similarly aligned to this boundary, @a q_msg_size
3308 * must also be a multiple of N.
3309 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003310 * @param q Address of the message queue.
3311 * @param buffer Pointer to ring buffer that holds queued messages.
3312 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003313 * @param max_msgs Maximum number of messages that can be queued.
3314 *
3315 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003316 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003317 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003318void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3319 u32_t max_msgs);
3320
3321/**
3322 * @brief Initialize a message queue.
3323 *
3324 * This routine initializes a message queue object, prior to its first use,
3325 * allocating its internal ring buffer from the calling thread's resource
3326 * pool.
3327 *
3328 * Memory allocated for the ring buffer can be released by calling
3329 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3330 * all of its references.
3331 *
3332 * @param q Address of the message queue.
3333 * @param msg_size Message size (in bytes).
3334 * @param max_msgs Maximum number of messages that can be queued.
3335 *
3336 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3337 * thread's resource pool, or -EINVAL if the size parameters cause
3338 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003339 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003340 */
3341__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3342 u32_t max_msgs);
3343
3344
3345void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003346
3347/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003348 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003350 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003351 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003352 * @note Can be called by ISRs.
3353 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003354 * @param q Address of the message queue.
3355 * @param data Pointer to the message.
3356 * @param timeout Waiting period to add the message (in milliseconds),
3357 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003358 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003359 * @retval 0 Message sent.
3360 * @retval -ENOMSG Returned without waiting or queue purged.
3361 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003362 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003363 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003364__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003365
3366/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003367 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003368 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003369 * This routine receives a message from message queue @a q in a "first in,
3370 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003371 *
Allan Stephensc98da842016-11-11 15:45:03 -05003372 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003374 * @param q Address of the message queue.
3375 * @param data Address of area to hold the received message.
3376 * @param timeout Waiting period to receive the message (in milliseconds),
3377 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003378 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003379 * @retval 0 Message received.
3380 * @retval -ENOMSG Returned without waiting.
3381 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003382 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003383 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003384__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003385
3386/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003387 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003389 * This routine discards all unreceived messages in a message queue's ring
3390 * buffer. Any threads that are blocked waiting to send a message to the
3391 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003392 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003393 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003394 *
3395 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003396 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003397 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003398__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003399
Peter Mitsis67be2492016-10-07 11:44:34 -04003400/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003401 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003403 * This routine returns the number of unused entries in a message queue's
3404 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003406 * @param q Address of the message queue.
3407 *
3408 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003409 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003410 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003411__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3412
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303413/**
3414 * @brief Get basic attributes of a message queue.
3415 *
3416 * This routine fetches basic attributes of message queue into attr argument.
3417 *
3418 * @param q Address of the message queue.
3419 * @param attrs pointer to message queue attribute structure.
3420 *
3421 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003422 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303423 */
3424__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3425
3426
Andrew Boie82edb6e2017-10-02 10:53:06 -07003427static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003428{
3429 return q->max_msgs - q->used_msgs;
3430}
3431
Peter Mitsisd7a37502016-10-13 11:37:40 -04003432/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003433 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003434 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003435 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003436 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003437 * @param q Address of the message queue.
3438 *
3439 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003440 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003441 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003442__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3443
3444static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003445{
3446 return q->used_msgs;
3447}
3448
Anas Nashif166f5192018-02-25 08:02:36 -06003449/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003450
3451/**
3452 * @defgroup mem_pool_apis Memory Pool APIs
3453 * @ingroup kernel_apis
3454 * @{
3455 */
3456
Andy Ross73cb9582017-05-09 10:42:39 -07003457/* Note on sizing: the use of a 20 bit field for block means that,
3458 * assuming a reasonable minimum block size of 16 bytes, we're limited
3459 * to 16M of memory managed by a single pool. Long term it would be
3460 * good to move to a variable bit size based on configuration.
3461 */
3462struct k_mem_block_id {
3463 u32_t pool : 8;
3464 u32_t level : 4;
3465 u32_t block : 20;
3466};
3467
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003468struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003469 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003470 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003471};
3472
Anas Nashif166f5192018-02-25 08:02:36 -06003473/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003474
3475/**
3476 * @defgroup mailbox_apis Mailbox APIs
3477 * @ingroup kernel_apis
3478 * @{
3479 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003480
3481struct k_mbox_msg {
3482 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003483 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003484 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003485 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003486 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003487 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003488 /** sender's message data buffer */
3489 void *tx_data;
3490 /** internal use only - needed for legacy API support */
3491 void *_rx_data;
3492 /** message data block descriptor */
3493 struct k_mem_block tx_block;
3494 /** source thread id */
3495 k_tid_t rx_source_thread;
3496 /** target thread id */
3497 k_tid_t tx_target_thread;
3498 /** internal use only - thread waiting on send (may be a dummy) */
3499 k_tid_t _syncing_thread;
3500#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3501 /** internal use only - semaphore used during asynchronous send */
3502 struct k_sem *_async_sem;
3503#endif
3504};
3505
3506struct k_mbox {
3507 _wait_q_t tx_msg_queue;
3508 _wait_q_t rx_msg_queue;
3509
Anas Nashif2f203c22016-12-18 06:57:45 -05003510 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003511};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003512/**
3513 * @cond INTERNAL_HIDDEN
3514 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003515
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003516#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003517 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003518 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3519 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003520 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003521 }
3522
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003523#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3524
Peter Mitsis12092702016-10-14 12:57:23 -04003525/**
Allan Stephensc98da842016-11-11 15:45:03 -05003526 * INTERNAL_HIDDEN @endcond
3527 */
3528
3529/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003530 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003531 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003532 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003533 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003534 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003535 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003536 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003537 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003538 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003539#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003540 struct k_mbox name \
3541 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003542 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003543
Peter Mitsis12092702016-10-14 12:57:23 -04003544/**
3545 * @brief Initialize a mailbox.
3546 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003547 * This routine initializes a mailbox object, prior to its first use.
3548 *
3549 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003550 *
3551 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003552 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003553 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003554extern void k_mbox_init(struct k_mbox *mbox);
3555
Peter Mitsis12092702016-10-14 12:57:23 -04003556/**
3557 * @brief Send a mailbox message in a synchronous manner.
3558 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003559 * This routine sends a message to @a mbox and waits for a receiver to both
3560 * receive and process it. The message data may be in a buffer, in a memory
3561 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003562 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003563 * @param mbox Address of the mailbox.
3564 * @param tx_msg Address of the transmit message descriptor.
3565 * @param timeout Waiting period for the message to be received (in
3566 * milliseconds), or one of the special values K_NO_WAIT
3567 * and K_FOREVER. Once the message has been received,
3568 * this routine waits as long as necessary for the message
3569 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003570 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003571 * @retval 0 Message sent.
3572 * @retval -ENOMSG Returned without waiting.
3573 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003574 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003575 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003576extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003577 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003578
Peter Mitsis12092702016-10-14 12:57:23 -04003579/**
3580 * @brief Send a mailbox message in an asynchronous manner.
3581 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003582 * This routine sends a message to @a mbox without waiting for a receiver
3583 * to process it. The message data may be in a buffer, in a memory pool block,
3584 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3585 * will be given when the message has been both received and completely
3586 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003587 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003588 * @param mbox Address of the mailbox.
3589 * @param tx_msg Address of the transmit message descriptor.
3590 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003591 *
3592 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003593 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003594 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003595extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003596 struct k_sem *sem);
3597
Peter Mitsis12092702016-10-14 12:57:23 -04003598/**
3599 * @brief Receive a mailbox message.
3600 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003601 * This routine receives a message from @a mbox, then optionally retrieves
3602 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003604 * @param mbox Address of the mailbox.
3605 * @param rx_msg Address of the receive message descriptor.
3606 * @param buffer Address of the buffer to receive data, or NULL to defer data
3607 * retrieval and message disposal until later.
3608 * @param timeout Waiting period for a message to be received (in
3609 * milliseconds), or one of the special values K_NO_WAIT
3610 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003611 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003612 * @retval 0 Message received.
3613 * @retval -ENOMSG Returned without waiting.
3614 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003615 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003616 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003617extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003618 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003619
3620/**
3621 * @brief Retrieve mailbox message data into a buffer.
3622 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003623 * This routine completes the processing of a received message by retrieving
3624 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003625 *
3626 * Alternatively, this routine can be used to dispose of a received message
3627 * without retrieving its data.
3628 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003629 * @param rx_msg Address of the receive message descriptor.
3630 * @param buffer Address of the buffer to receive data, or NULL to discard
3631 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003632 *
3633 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003634 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003635 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003636extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003637
3638/**
3639 * @brief Retrieve mailbox message data into a memory pool block.
3640 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003641 * This routine completes the processing of a received message by retrieving
3642 * its data into a memory pool block, then disposing of the message.
3643 * The memory pool block that results from successful retrieval must be
3644 * returned to the pool once the data has been processed, even in cases
3645 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003646 *
3647 * Alternatively, this routine can be used to dispose of a received message
3648 * without retrieving its data. In this case there is no need to return a
3649 * memory pool block to the pool.
3650 *
3651 * This routine allocates a new memory pool block for the data only if the
3652 * data is not already in one. If a new block cannot be allocated, the routine
3653 * returns a failure code and the received message is left unchanged. This
3654 * permits the caller to reattempt data retrieval at a later time or to dispose
3655 * of the received message without retrieving its data.
3656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003657 * @param rx_msg Address of a receive message descriptor.
3658 * @param pool Address of memory pool, or NULL to discard data.
3659 * @param block Address of the area to hold memory pool block info.
3660 * @param timeout Waiting period to wait for a memory pool block (in
3661 * milliseconds), or one of the special values K_NO_WAIT
3662 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003663 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003664 * @retval 0 Data retrieved.
3665 * @retval -ENOMEM Returned without waiting.
3666 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003667 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003668 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003669extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003670 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003671 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003672
Anas Nashif166f5192018-02-25 08:02:36 -06003673/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003674
3675/**
Anas Nashifce78d162018-05-24 12:43:11 -05003676 * @defgroup pipe_apis Pipe APIs
3677 * @ingroup kernel_apis
3678 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003679 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003680
Anas Nashifce78d162018-05-24 12:43:11 -05003681/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003682struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003683 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3684 size_t size; /**< Buffer size */
3685 size_t bytes_used; /**< # bytes used in buffer */
3686 size_t read_index; /**< Where in buffer to read from */
3687 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003688
3689 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003690 _wait_q_t readers; /**< Reader wait queue */
3691 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003692 } wait_q;
3693
Anas Nashif2f203c22016-12-18 06:57:45 -05003694 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Anas Nashifce78d162018-05-24 12:43:11 -05003695 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003696};
3697
Anas Nashifce78d162018-05-24 12:43:11 -05003698/**
3699 * @cond INTERNAL_HIDDEN
3700 */
3701#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3702
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003703#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003704 { \
3705 .buffer = pipe_buffer, \
3706 .size = pipe_buffer_size, \
3707 .bytes_used = 0, \
3708 .read_index = 0, \
3709 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003710 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3711 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003712 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003713 }
3714
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003715#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3716
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003717/**
Allan Stephensc98da842016-11-11 15:45:03 -05003718 * INTERNAL_HIDDEN @endcond
3719 */
3720
3721/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003722 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003723 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003724 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003725 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003726 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003728 * @param name Name of the pipe.
3729 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3730 * or zero if no ring buffer is used.
3731 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003732 *
3733 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003734 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003735#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3736 static unsigned char __kernel_noinit __aligned(pipe_align) \
3737 _k_pipe_buf_##name[pipe_buffer_size]; \
3738 struct k_pipe name \
3739 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003740 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003741
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003742/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003743 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003744 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003745 * This routine initializes a pipe object, prior to its first use.
3746 *
3747 * @param pipe Address of the pipe.
3748 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3749 * is used.
3750 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3751 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003752 *
3753 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003754 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003755 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003756void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3757
3758/**
3759 * @brief Release a pipe's allocated buffer
3760 *
3761 * If a pipe object was given a dynamically allocated buffer via
3762 * k_pipe_alloc_init(), this will free it. This function does nothing
3763 * if the buffer wasn't dynamically allocated.
3764 *
3765 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003766 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003767 */
3768void k_pipe_cleanup(struct k_pipe *pipe);
3769
3770/**
3771 * @brief Initialize a pipe and allocate a buffer for it
3772 *
3773 * Storage for the buffer region will be allocated from the calling thread's
3774 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3775 * or userspace is enabled and the pipe object loses all references to it.
3776 *
3777 * This function should only be called on uninitialized pipe objects.
3778 *
3779 * @param pipe Address of the pipe.
3780 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3781 * buffer is used.
3782 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003783 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003784 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003785 */
3786__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003787
3788/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003789 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003791 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003792 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003793 * @param pipe Address of the pipe.
3794 * @param data Address of data to write.
3795 * @param bytes_to_write Size of data (in bytes).
3796 * @param bytes_written Address of area to hold the number of bytes written.
3797 * @param min_xfer Minimum number of bytes to write.
3798 * @param timeout Waiting period to wait for the data to be written (in
3799 * milliseconds), or one of the special values K_NO_WAIT
3800 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003801 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003802 * @retval 0 At least @a min_xfer bytes of data were written.
3803 * @retval -EIO Returned without waiting; zero data bytes were written.
3804 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003805 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003806 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003807 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003808__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3809 size_t bytes_to_write, size_t *bytes_written,
3810 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003811
3812/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003813 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003815 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003817 * @param pipe Address of the pipe.
3818 * @param data Address to place the data read from pipe.
3819 * @param bytes_to_read Maximum number of data bytes to read.
3820 * @param bytes_read Address of area to hold the number of bytes read.
3821 * @param min_xfer Minimum number of data bytes to read.
3822 * @param timeout Waiting period to wait for the data to be read (in
3823 * milliseconds), or one of the special values K_NO_WAIT
3824 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003825 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003826 * @retval 0 At least @a min_xfer bytes of data were read.
3827 * @retval -EIO Returned without waiting; zero data bytes were read.
3828 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003829 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003830 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003831 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003832__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3833 size_t bytes_to_read, size_t *bytes_read,
3834 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003835
3836/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003837 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003838 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003839 * This routine writes the data contained in a memory block to @a pipe.
3840 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003841 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003842 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003843 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003844 * @param block Memory block containing data to send
3845 * @param size Number of data bytes in memory block to send
3846 * @param sem Semaphore to signal upon completion (else NULL)
3847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003848 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003849 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003850 */
3851extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3852 size_t size, struct k_sem *sem);
3853
Anas Nashif166f5192018-02-25 08:02:36 -06003854/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003855
Allan Stephensc98da842016-11-11 15:45:03 -05003856/**
3857 * @cond INTERNAL_HIDDEN
3858 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003859
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003860struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003861 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003862 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003863 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003864 char *buffer;
3865 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003866 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003867
Anas Nashif2f203c22016-12-18 06:57:45 -05003868 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003869};
3870
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003871#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003872 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003873 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003874 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003875 .num_blocks = slab_num_blocks, \
3876 .block_size = slab_block_size, \
3877 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003878 .free_list = NULL, \
3879 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003880 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003881 }
3882
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003883#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3884
3885
Peter Mitsis578f9112016-10-07 13:50:31 -04003886/**
Allan Stephensc98da842016-11-11 15:45:03 -05003887 * INTERNAL_HIDDEN @endcond
3888 */
3889
3890/**
3891 * @defgroup mem_slab_apis Memory Slab APIs
3892 * @ingroup kernel_apis
3893 * @{
3894 */
3895
3896/**
Allan Stephensda827222016-11-09 14:23:58 -06003897 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003898 *
Allan Stephensda827222016-11-09 14:23:58 -06003899 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003900 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003901 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3902 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003903 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003904 *
Allan Stephensda827222016-11-09 14:23:58 -06003905 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003906 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003907 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003908 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003909 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003910 * @param name Name of the memory slab.
3911 * @param slab_block_size Size of each memory block (in bytes).
3912 * @param slab_num_blocks Number memory blocks.
3913 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003914 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003915 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003916#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3917 char __noinit __aligned(slab_align) \
3918 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3919 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003920 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003921 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003922 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003923
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003924/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003925 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003927 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003928 *
Allan Stephensda827222016-11-09 14:23:58 -06003929 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3930 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3931 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3932 * To ensure that each memory block is similarly aligned to this boundary,
3933 * @a slab_block_size must also be a multiple of N.
3934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003935 * @param slab Address of the memory slab.
3936 * @param buffer Pointer to buffer used for the memory blocks.
3937 * @param block_size Size of each memory block (in bytes).
3938 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003939 *
3940 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003941 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003942 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003943extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003944 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003945
3946/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003947 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003948 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003949 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003950 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003951 * @param slab Address of the memory slab.
3952 * @param mem Pointer to block address area.
3953 * @param timeout Maximum time to wait for operation to complete
3954 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3955 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003956 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003957 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003958 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003959 * @retval -ENOMEM Returned without waiting.
3960 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003961 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003962 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003963extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003964 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003965
3966/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003967 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003969 * This routine releases a previously allocated memory block back to its
3970 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003972 * @param slab Address of the memory slab.
3973 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003974 *
3975 * @return N/A
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 void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003979
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003980/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003981 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003983 * This routine gets the number of memory blocks that are currently
3984 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003986 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003987 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003988 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003989 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003990 */
Kumar Galacc334c72017-04-21 10:55:34 -05003991static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003992{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003993 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003994}
3995
Peter Mitsisc001aa82016-10-13 13:53:37 -04003996/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003997 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003999 * This routine gets the number of memory blocks that are currently
4000 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004001 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004002 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004004 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004005 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004006 */
Kumar Galacc334c72017-04-21 10:55:34 -05004007static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004008{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004009 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004010}
4011
Anas Nashif166f5192018-02-25 08:02:36 -06004012/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004013
4014/**
4015 * @cond INTERNAL_HIDDEN
4016 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004017
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004018struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004019 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004020 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004021};
4022
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004023/**
Allan Stephensc98da842016-11-11 15:45:03 -05004024 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004025 */
4026
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004027/**
Allan Stephensc98da842016-11-11 15:45:03 -05004028 * @addtogroup mem_pool_apis
4029 * @{
4030 */
4031
4032/**
4033 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004035 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4036 * long. The memory pool allows blocks to be repeatedly partitioned into
4037 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004038 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004039 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004040 * If the pool is to be accessed outside the module where it is defined, it
4041 * can be declared via
4042 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004043 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004044 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004045 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004046 * @param minsz Size of the smallest blocks in the pool (in bytes).
4047 * @param maxsz Size of the largest blocks in the pool (in bytes).
4048 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004049 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004050 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004051 */
Andy Ross73cb9582017-05-09 10:42:39 -07004052#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4053 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4054 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08004055 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07004056 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004057 .base = { \
4058 .buf = _mpool_buf_##name, \
4059 .max_sz = maxsz, \
4060 .n_max = nmax, \
4061 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
4062 .levels = _mpool_lvls_##name, \
4063 .flags = SYS_MEM_POOL_KERNEL \
4064 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004065 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004066
Peter Mitsis937042c2016-10-13 13:18:26 -04004067/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004068 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004069 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004070 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004071 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004072 * @param pool Address of the memory pool.
4073 * @param block Pointer to block descriptor for the allocated memory.
4074 * @param size Amount of memory to allocate (in bytes).
4075 * @param timeout Maximum time to wait for operation to complete
4076 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4077 * or K_FOREVER to wait as long as necessary.
4078 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004079 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004080 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004081 * @retval -ENOMEM Returned without waiting.
4082 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004083 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004084 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004085extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004086 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004087
4088/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004089 * @brief Allocate memory from a memory pool with malloc() semantics
4090 *
4091 * Such memory must be released using k_free().
4092 *
4093 * @param pool Address of the memory pool.
4094 * @param size Amount of memory to allocate (in bytes).
4095 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004096 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004097 */
4098extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4099
4100/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004101 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004102 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004103 * This routine releases a previously allocated memory block back to its
4104 * memory pool.
4105 *
4106 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004107 *
4108 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004109 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004110 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004111extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004112
4113/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004114 * @brief Free memory allocated from a memory pool.
4115 *
4116 * This routine releases a previously allocated memory block back to its
4117 * memory pool
4118 *
4119 * @param id Memory block identifier.
4120 *
4121 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004122 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004123 */
4124extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4125
4126/**
Anas Nashif166f5192018-02-25 08:02:36 -06004127 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004128 */
4129
4130/**
4131 * @defgroup heap_apis Heap Memory Pool APIs
4132 * @ingroup kernel_apis
4133 * @{
4134 */
4135
4136/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004137 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004139 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004140 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004141 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004142 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004144 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004145 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004146 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004147extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004148
4149/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004150 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004151 *
4152 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004153 * returned must have been allocated from the heap memory pool or
4154 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004155 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004156 * If @a ptr is NULL, no operation is performed.
4157 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004158 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004159 *
4160 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004161 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004162 */
4163extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004164
Allan Stephensc98da842016-11-11 15:45:03 -05004165/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004166 * @brief Allocate memory from heap, array style
4167 *
4168 * This routine provides traditional calloc() semantics. Memory is
4169 * allocated from the heap memory pool and zeroed.
4170 *
4171 * @param nmemb Number of elements in the requested array
4172 * @param size Size of each array element (in bytes).
4173 *
4174 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004175 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004176 */
4177extern void *k_calloc(size_t nmemb, size_t size);
4178
Anas Nashif166f5192018-02-25 08:02:36 -06004179/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004180
Benjamin Walshacc68c12017-01-29 18:57:45 -05004181/* polling API - PRIVATE */
4182
Benjamin Walshb0179862017-02-02 16:39:57 -05004183#ifdef CONFIG_POLL
4184#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
4185#else
4186#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
4187#endif
4188
Benjamin Walshacc68c12017-01-29 18:57:45 -05004189/* private - implementation data created as needed, per-type */
4190struct _poller {
4191 struct k_thread *thread;
Andy Ross55a7e462018-05-31 11:58:09 -07004192 volatile int is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004193};
4194
4195/* private - types bit positions */
4196enum _poll_types_bits {
4197 /* can be used to ignore an event */
4198 _POLL_TYPE_IGNORE,
4199
4200 /* to be signaled by k_poll_signal() */
4201 _POLL_TYPE_SIGNAL,
4202
4203 /* semaphore availability */
4204 _POLL_TYPE_SEM_AVAILABLE,
4205
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004206 /* queue/fifo/lifo data availability */
4207 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004208
4209 _POLL_NUM_TYPES
4210};
4211
4212#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4213
4214/* private - states bit positions */
4215enum _poll_states_bits {
4216 /* default state when creating event */
4217 _POLL_STATE_NOT_READY,
4218
Benjamin Walshacc68c12017-01-29 18:57:45 -05004219 /* signaled by k_poll_signal() */
4220 _POLL_STATE_SIGNALED,
4221
4222 /* semaphore is available */
4223 _POLL_STATE_SEM_AVAILABLE,
4224
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004225 /* data is available to read on queue/fifo/lifo */
4226 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004227
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004228 /* queue/fifo/lifo wait was cancelled */
4229 _POLL_STATE_CANCELLED,
4230
Benjamin Walshacc68c12017-01-29 18:57:45 -05004231 _POLL_NUM_STATES
4232};
4233
4234#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4235
4236#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004237 (32 - (0 \
4238 + 8 /* tag */ \
4239 + _POLL_NUM_TYPES \
4240 + _POLL_NUM_STATES \
4241 + 1 /* modes */ \
4242 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004243
Benjamin Walshacc68c12017-01-29 18:57:45 -05004244/* end of polling API - PRIVATE */
4245
4246
4247/**
4248 * @defgroup poll_apis Async polling APIs
4249 * @ingroup kernel_apis
4250 * @{
4251 */
4252
4253/* Public polling API */
4254
4255/* public - values for k_poll_event.type bitfield */
4256#define K_POLL_TYPE_IGNORE 0
4257#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4258#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004259#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4260#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004261
4262/* public - polling modes */
4263enum k_poll_modes {
4264 /* polling thread does not take ownership of objects when available */
4265 K_POLL_MODE_NOTIFY_ONLY = 0,
4266
4267 K_POLL_NUM_MODES
4268};
4269
4270/* public - values for k_poll_event.state bitfield */
4271#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004272#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4273#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004274#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4275#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004276#define K_POLL_STATE_CANCELLED _POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004277
4278/* public - poll signal object */
4279struct k_poll_signal {
4280 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004281 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004282
4283 /*
4284 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4285 * user resets it to 0.
4286 */
4287 unsigned int signaled;
4288
4289 /* custom result value passed to k_poll_signal() if needed */
4290 int result;
4291};
4292
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004293#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004294 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004295 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004296 .signaled = 0, \
4297 .result = 0, \
4298 }
4299
4300struct k_poll_event {
4301 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004302 sys_dnode_t _node;
4303
4304 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004305 struct _poller *poller;
4306
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004307 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004308 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004309
Benjamin Walshacc68c12017-01-29 18:57:45 -05004310 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004311 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004312
4313 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004314 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004315
4316 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004317 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004318
4319 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004320 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004321
4322 /* per-type data */
4323 union {
4324 void *obj;
4325 struct k_poll_signal *signal;
4326 struct k_sem *sem;
4327 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004328 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004329 };
4330};
4331
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004332#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004333 { \
4334 .poller = NULL, \
4335 .type = event_type, \
4336 .state = K_POLL_STATE_NOT_READY, \
4337 .mode = event_mode, \
4338 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004339 { .obj = event_obj }, \
4340 }
4341
4342#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4343 event_tag) \
4344 { \
4345 .type = event_type, \
4346 .tag = event_tag, \
4347 .state = K_POLL_STATE_NOT_READY, \
4348 .mode = event_mode, \
4349 .unused = 0, \
4350 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004351 }
4352
4353/**
4354 * @brief Initialize one struct k_poll_event instance
4355 *
4356 * After this routine is called on a poll event, the event it ready to be
4357 * placed in an event array to be passed to k_poll().
4358 *
4359 * @param event The event to initialize.
4360 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4361 * values. Only values that apply to the same object being polled
4362 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4363 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004364 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004365 * @param obj Kernel object or poll signal.
4366 *
4367 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004368 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004369 */
4370
Kumar Galacc334c72017-04-21 10:55:34 -05004371extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004372 int mode, void *obj);
4373
4374/**
4375 * @brief Wait for one or many of multiple poll events to occur
4376 *
4377 * This routine allows a thread to wait concurrently for one or many of
4378 * multiple poll events to have occurred. Such events can be a kernel object
4379 * being available, like a semaphore, or a poll signal event.
4380 *
4381 * When an event notifies that a kernel object is available, the kernel object
4382 * is not "given" to the thread calling k_poll(): it merely signals the fact
4383 * that the object was available when the k_poll() call was in effect. Also,
4384 * all threads trying to acquire an object the regular way, i.e. by pending on
4385 * the object, have precedence over the thread polling on the object. This
4386 * means that the polling thread will never get the poll event on an object
4387 * until the object becomes available and its pend queue is empty. For this
4388 * reason, the k_poll() call is more effective when the objects being polled
4389 * only have one thread, the polling thread, trying to acquire them.
4390 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004391 * When k_poll() returns 0, the caller should loop on all the events that were
4392 * passed to k_poll() and check the state field for the values that were
4393 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004394 *
4395 * Before being reused for another call to k_poll(), the user has to reset the
4396 * state field to K_POLL_STATE_NOT_READY.
4397 *
Andrew Boie3772f772018-05-07 16:52:57 -07004398 * When called from user mode, a temporary memory allocation is required from
4399 * the caller's resource pool.
4400 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004401 * @param events An array of pointers to events to be polled for.
4402 * @param num_events The number of events in the array.
4403 * @param timeout Waiting period for an event to be ready (in milliseconds),
4404 * or one of the special values K_NO_WAIT and K_FOREVER.
4405 *
4406 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004407 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004408 * @retval -EINTR Polling has been interrupted, e.g. with
4409 * k_queue_cancel_wait(). All output events are still set and valid,
4410 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4411 * words, -EINTR status means that at least one of output events is
4412 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004413 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4414 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004415 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004416 */
4417
Andrew Boie3772f772018-05-07 16:52:57 -07004418__syscall int k_poll(struct k_poll_event *events, int num_events,
4419 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004420
4421/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004422 * @brief Initialize a poll signal object.
4423 *
4424 * Ready a poll signal object to be signaled via k_poll_signal().
4425 *
4426 * @param signal A poll signal.
4427 *
4428 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004429 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004430 */
4431
Andrew Boie3772f772018-05-07 16:52:57 -07004432__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4433
4434/*
4435 * @brief Reset a poll signal object's state to unsignaled.
4436 *
4437 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004438 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004439 */
4440__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4441
4442static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4443{
4444 signal->signaled = 0;
4445}
4446
4447/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004448 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004449 *
4450 * @param signal A poll signal object
4451 * @param signaled An integer buffer which will be written nonzero if the
4452 * object was signaled
4453 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004454 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004455 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004456 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004457 */
4458__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4459 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004460
4461/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004462 * @brief Signal a poll signal object.
4463 *
4464 * This routine makes ready a poll signal, which is basically a poll event of
4465 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4466 * made ready to run. A @a result value can be specified.
4467 *
4468 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004469 * k_poll_signal(), stays set until the user sets it back to 0 with
4470 * k_poll_signal_reset(). It thus has to be reset by the user before being
4471 * passed again to k_poll() or k_poll() will consider it being signaled, and
4472 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004473 *
4474 * @param signal A poll signal.
4475 * @param result The value to store in the result field of the signal.
4476 *
4477 * @retval 0 The signal was delivered successfully.
4478 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004479 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004480 */
4481
Andrew Boie3772f772018-05-07 16:52:57 -07004482__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004483
Anas Nashif954d5502018-02-25 08:37:28 -06004484/**
4485 * @internal
4486 */
Andy Ross8606fab2018-03-26 10:54:40 -07004487extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004488
Anas Nashif166f5192018-02-25 08:02:36 -06004489/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004490
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004491/**
4492 * @brief Make the CPU idle.
4493 *
4494 * This function makes the CPU idle until an event wakes it up.
4495 *
4496 * In a regular system, the idle thread should be the only thread responsible
4497 * for making the CPU idle and triggering any type of power management.
4498 * However, in some more constrained systems, such as a single-threaded system,
4499 * the only thread would be responsible for this if needed.
4500 *
4501 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004502 * @req K-MISC-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004503 */
4504extern void k_cpu_idle(void);
4505
4506/**
4507 * @brief Make the CPU idle in an atomic fashion.
4508 *
4509 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4510 * must be done atomically before making the CPU idle.
4511 *
4512 * @param key Interrupt locking key obtained from irq_lock().
4513 *
4514 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004515 * @req K-MISC-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004516 */
4517extern void k_cpu_atomic_idle(unsigned int key);
4518
Anas Nashif954d5502018-02-25 08:37:28 -06004519
4520/**
4521 * @internal
4522 */
Kumar Galacc334c72017-04-21 10:55:34 -05004523extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004524
Andrew Boiecdb94d62017-04-18 15:22:05 -07004525#ifdef _ARCH_EXCEPT
4526/* This archtecture has direct support for triggering a CPU exception */
4527#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4528#else
4529
Andrew Boiecdb94d62017-04-18 15:22:05 -07004530/* NOTE: This is the implementation for arches that do not implement
4531 * _ARCH_EXCEPT() to generate a real CPU exception.
4532 *
4533 * We won't have a real exception frame to determine the PC value when
4534 * the oops occurred, so print file and line number before we jump into
4535 * the fatal error handler.
4536 */
4537#define _k_except_reason(reason) do { \
4538 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4539 _NanoFatalErrorHandler(reason, &_default_esf); \
4540 CODE_UNREACHABLE; \
4541 } while (0)
4542
4543#endif /* _ARCH__EXCEPT */
4544
4545/**
4546 * @brief Fatally terminate a thread
4547 *
4548 * This should be called when a thread has encountered an unrecoverable
4549 * runtime condition and needs to terminate. What this ultimately
4550 * means is determined by the _fatal_error_handler() implementation, which
4551 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4552 *
4553 * If this is called from ISR context, the default system fatal error handler
4554 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004555 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004556 */
4557#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4558
4559/**
4560 * @brief Fatally terminate the system
4561 *
4562 * This should be called when the Zephyr kernel has encountered an
4563 * unrecoverable runtime condition and needs to terminate. What this ultimately
4564 * means is determined by the _fatal_error_handler() implementation, which
4565 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004566 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004567 */
4568#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4569
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004570/*
4571 * private APIs that are utilized by one or more public APIs
4572 */
4573
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004574#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004575/**
4576 * @internal
4577 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004578extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004579#else
Anas Nashif954d5502018-02-25 08:37:28 -06004580/**
4581 * @internal
4582 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004583#define _init_static_threads() do { } while ((0))
4584#endif
4585
Anas Nashif954d5502018-02-25 08:37:28 -06004586/**
4587 * @internal
4588 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004589extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004590/**
4591 * @internal
4592 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004593extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004594
Andrew Boiedc5d9352017-06-02 12:56:47 -07004595/* arch/cpu.h may declare an architecture or platform-specific macro
4596 * for properly declaring stacks, compatible with MMU/MPU constraints if
4597 * enabled
4598 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004599
4600/**
4601 * @brief Obtain an extern reference to a stack
4602 *
4603 * This macro properly brings the symbol of a thread stack declared
4604 * elsewhere into scope.
4605 *
4606 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004607 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004608 */
4609#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4610
Andrew Boiedc5d9352017-06-02 12:56:47 -07004611#ifdef _ARCH_THREAD_STACK_DEFINE
4612#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4613#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4614 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304615#define K_THREAD_STACK_LEN(size) _ARCH_THREAD_STACK_LEN(size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004616#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4617#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004618static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004619{
4620 return _ARCH_THREAD_STACK_BUFFER(sym);
4621}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004622#else
4623/**
4624 * @brief Declare a toplevel thread stack memory region
4625 *
4626 * This declares a region of memory suitable for use as a thread's stack.
4627 *
4628 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4629 * 'noinit' section so that it isn't zeroed at boot
4630 *
Andrew Boie507852a2017-07-25 18:47:07 -07004631 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004632 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004633 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004634 *
4635 * It is legal to precede this definition with the 'static' keyword.
4636 *
4637 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4638 * parameter of k_thread_create(), it may not be the same as the
4639 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4640 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004641 * Some arches may round the size of the usable stack region up to satisfy
4642 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4643 * size.
4644 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004645 * @param sym Thread stack symbol name
4646 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004647 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004648 */
4649#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004650 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004651
4652/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304653 * @brief Calculate size of stacks to be allocated in a stack array
4654 *
4655 * This macro calculates the size to be allocated for the stacks
4656 * inside a stack array. It accepts the indicated "size" as a parameter
4657 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4658 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4659 *
4660 * @param size Size of the stack memory region
4661 * @req K-TSTACK-001
4662 */
4663#define K_THREAD_STACK_LEN(size) (size)
4664
4665/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004666 * @brief Declare a toplevel array of thread stack memory regions
4667 *
4668 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4669 * definition for additional details and constraints.
4670 *
4671 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4672 * 'noinit' section so that it isn't zeroed at boot
4673 *
4674 * @param sym Thread stack symbol name
4675 * @param nmemb Number of stacks to declare
4676 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004677 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004678 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004679#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004680 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304681 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004682
4683/**
4684 * @brief Declare an embedded stack memory region
4685 *
4686 * Used for stacks embedded within other data structures. Use is highly
4687 * discouraged but in some cases necessary. For memory protection scenarios,
4688 * it is very important that any RAM preceding this member not be writable
4689 * by threads else a stack overflow will lead to silent corruption. In other
4690 * words, the containing data structure should live in RAM owned by the kernel.
4691 *
4692 * @param sym Thread stack symbol name
4693 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004694 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004695 */
4696#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004697 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004698
4699/**
4700 * @brief Return the size in bytes of a stack memory region
4701 *
4702 * Convenience macro for passing the desired stack size to k_thread_create()
4703 * since the underlying implementation may actually create something larger
4704 * (for instance a guard area).
4705 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004706 * The value returned here is not guaranteed to match the 'size' parameter
4707 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004708 *
4709 * @param sym Stack memory symbol
4710 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004711 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004712 */
4713#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4714
4715/**
4716 * @brief Get a pointer to the physical stack buffer
4717 *
4718 * Convenience macro to get at the real underlying stack buffer used by
4719 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4720 * This is really only intended for diagnostic tools which want to examine
4721 * stack memory contents.
4722 *
4723 * @param sym Declared stack symbol name
4724 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004725 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004726 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004727static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004728{
4729 return (char *)sym;
4730}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004731
4732#endif /* _ARCH_DECLARE_STACK */
4733
Chunlin Hane9c97022017-07-07 20:29:30 +08004734/**
4735 * @defgroup mem_domain_apis Memory domain APIs
4736 * @ingroup kernel_apis
4737 * @{
4738 */
4739
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004740/**
4741 * @def MEM_PARTITION_ENTRY
4742 * @brief Used to declare a memory partition entry
4743 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004744 */
4745#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4746 {\
4747 .start = _start, \
4748 .size = _size, \
4749 .attr = _attr, \
4750 }
4751
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004752/**
4753 * @def K_MEM_PARTITION_DEFINE
4754 * @brief Used to declare a memory partition
4755 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004756 */
4757#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4758#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4759 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304760 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004761 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4762#else
4763#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304764 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004765 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4766#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4767
Chunlin Hane9c97022017-07-07 20:29:30 +08004768/* memory partition */
4769struct k_mem_partition {
4770 /* start address of memory partition */
4771 u32_t start;
4772 /* size of memory partition */
4773 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304774#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004775 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304776 k_mem_partition_attr_t attr;
4777#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004778};
4779
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304780/* memory domian
4781 * Note: Always declare this structure with __kernel prefix
4782 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004783struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304784#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004785 /* partitions in the domain */
4786 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304787#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004788 /* domain q */
4789 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004790 /* number of partitions in the domain */
4791 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004792};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304793
Chunlin Hane9c97022017-07-07 20:29:30 +08004794
4795/**
4796 * @brief Initialize a memory domain.
4797 *
4798 * Initialize a memory domain with given name and memory partitions.
4799 *
4800 * @param domain The memory domain to be initialized.
4801 * @param num_parts The number of array items of "parts" parameter.
4802 * @param parts An array of pointers to the memory partitions. Can be NULL
4803 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004804 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004805 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004806extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004807 struct k_mem_partition *parts[]);
4808/**
4809 * @brief Destroy a memory domain.
4810 *
4811 * Destroy a memory domain.
4812 *
4813 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004814 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004815 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004816extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4817
4818/**
4819 * @brief Add a memory partition into a memory domain.
4820 *
4821 * Add a memory partition into a memory domain.
4822 *
4823 * @param domain The memory domain to be added a memory partition.
4824 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004825 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004826 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004827extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4828 struct k_mem_partition *part);
4829
4830/**
4831 * @brief Remove a memory partition from a memory domain.
4832 *
4833 * Remove a memory partition from a memory domain.
4834 *
4835 * @param domain The memory domain to be removed a memory partition.
4836 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004837 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004838 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004839extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4840 struct k_mem_partition *part);
4841
4842/**
4843 * @brief Add a thread into a memory domain.
4844 *
4845 * Add a thread into a memory domain.
4846 *
4847 * @param domain The memory domain that the thread is going to be added into.
4848 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004849 *
4850 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004851 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004852extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4853 k_tid_t thread);
4854
4855/**
4856 * @brief Remove a thread from its memory domain.
4857 *
4858 * Remove a thread from its memory domain.
4859 *
4860 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004861 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004862 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004863extern void k_mem_domain_remove_thread(k_tid_t thread);
4864
Anas Nashif166f5192018-02-25 08:02:36 -06004865/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004866
Andrew Boie756f9072017-10-10 16:01:49 -07004867/**
4868 * @brief Emit a character buffer to the console device
4869 *
4870 * @param c String of characters to print
4871 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004872 *
4873 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004874 */
4875__syscall void k_str_out(char *c, size_t n);
4876
Andy Rosse7172672018-01-24 15:48:32 -08004877/**
4878 * @brief Start a numbered CPU on a MP-capable system
4879
4880 * This starts and initializes a specific CPU. The main thread on
4881 * startup is running on CPU zero, other processors are numbered
4882 * sequentially. On return from this function, the CPU is known to
4883 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004884 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004885 * with the provided key will work to enable them.
4886 *
4887 * Normally, in SMP mode this function will be called by the kernel
4888 * initialization and should not be used as a user API. But it is
4889 * defined here for special-purpose apps which want Zephyr running on
4890 * one core and to use others for design-specific processing.
4891 *
4892 * @param cpu_num Integer number of the CPU
4893 * @param stack Stack memory for the CPU
4894 * @param sz Stack buffer size, in bytes
4895 * @param fn Function to begin running on the CPU. First argument is
4896 * an irq_unlock() key.
4897 * @param arg Untyped argument to be passed to "fn"
4898 */
4899extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4900 void (*fn)(int, void *), void *arg);
4901
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004902#ifdef __cplusplus
4903}
4904#endif
4905
Andrew Boiee004dec2016-11-07 09:01:19 -08004906#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4907/*
4908 * Define new and delete operators.
4909 * At this moment, the operators do nothing since objects are supposed
4910 * to be statically allocated.
4911 */
4912inline void operator delete(void *ptr)
4913{
4914 (void)ptr;
4915}
4916
4917inline void operator delete[](void *ptr)
4918{
4919 (void)ptr;
4920}
4921
4922inline void *operator new(size_t size)
4923{
4924 (void)size;
4925 return NULL;
4926}
4927
4928inline void *operator new[](size_t size)
4929{
4930 (void)size;
4931 return NULL;
4932}
4933
4934/* Placement versions of operator new and delete */
4935inline void operator delete(void *ptr1, void *ptr2)
4936{
4937 (void)ptr1;
4938 (void)ptr2;
4939}
4940
4941inline void operator delete[](void *ptr1, void *ptr2)
4942{
4943 (void)ptr1;
4944 (void)ptr2;
4945}
4946
4947inline void *operator new(size_t size, void *ptr)
4948{
4949 (void)size;
4950 return ptr;
4951}
4952
4953inline void *operator new[](size_t size, void *ptr)
4954{
4955 (void)size;
4956 return ptr;
4957}
4958
4959#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4960
Anas Nashifb6304e62018-07-04 08:03:03 -05004961#include <tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07004962#include <syscalls/kernel.h>
4963
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004964#endif /* !_ASMLANGUAGE */
4965
Flavio Ceolin67ca1762018-09-14 10:43:44 -07004966#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */