blob: ee6ef04947324d0942e20f3d5b86e817a411de1c [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
Anas Nashif57554052018-03-03 02:31:05 -0600542#if defined(CONFIG_THREAD_NAME)
543 /* Thread name */
544 const char *name;
545#endif
546
Andrew Boie73abd322017-04-04 13:19:13 -0700547#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500548 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700549 void *custom_data;
550#endif
551
Daniel Leungfc182432018-08-16 15:42:28 -0700552#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
553 struct _thread_userspace_local_data *userspace_local_data;
554#endif
555
Andrew Boie73abd322017-04-04 13:19:13 -0700556#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700557#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500558 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700559 int errno_var;
560#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700561#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700562
563#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500564 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700565 struct _thread_stack_info stack_info;
566#endif /* CONFIG_THREAD_STACK_INFO */
567
Chunlin Hane9c97022017-07-07 20:29:30 +0800568#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500569 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800570 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500571 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700572 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800573#endif /* CONFIG_USERSPACE */
574
Andy Ross042d8ec2017-12-09 08:37:20 -0800575#if defined(CONFIG_USE_SWITCH)
576 /* When using __switch() a few previously arch-specific items
577 * become part of the core OS
578 */
579
Anas Nashifce78d162018-05-24 12:43:11 -0500580 /** _Swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800581 int swap_retval;
582
Anas Nashifce78d162018-05-24 12:43:11 -0500583 /** Context handle returned via _arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800584 void *switch_handle;
585#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500586 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700587 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800588
Anas Nashifce78d162018-05-24 12:43:11 -0500589 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700590 struct _thread_arch arch;
591};
592
593typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400594typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700595#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400596
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400597enum execution_context_types {
598 K_ISR = 0,
599 K_COOP_THREAD,
600 K_PREEMPT_THREAD,
601};
602
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400603/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100604 * @defgroup profiling_apis Profiling APIs
605 * @ingroup kernel_apis
606 * @{
607 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530608typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
609 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100610
611/**
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530612 * @brief Iterate over all the threads in the system.
613 *
614 * This routine iterates over all the threads in the system and
615 * calls the user_cb function for each thread.
616 *
617 * @param user_cb Pointer to the user callback function.
618 * @param user_data Pointer to user data.
619 *
620 * @note CONFIG_THREAD_MONITOR must be set for this function
621 * to be effective. Also this API uses irq_lock to protect the
622 * _kernel.threads list which means creation of new threads and
623 * terminations of existing threads are blocked until this
624 * API returns.
625 *
626 * @return N/A
627 */
628extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
629
Anas Nashif166f5192018-02-25 08:02:36 -0600630/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100631
632/**
Allan Stephensc98da842016-11-11 15:45:03 -0500633 * @defgroup thread_apis Thread APIs
634 * @ingroup kernel_apis
635 * @{
636 */
637
Benjamin Walshed240f22017-01-22 13:05:08 -0500638#endif /* !_ASMLANGUAGE */
639
640
641/*
642 * Thread user options. May be needed by assembly code. Common part uses low
643 * bits, arch-specific use high bits.
644 */
645
Anas Nashifa541e932018-05-24 11:19:16 -0500646/**
647 * @brief system thread that must not abort
648 * @req K-THREAD-000
649 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700650#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500651
652#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500653/**
654 * @brief thread uses floating point registers
655 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700656#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500657#endif
658
Anas Nashifa541e932018-05-24 11:19:16 -0500659/**
660 * @brief user mode thread
661 *
662 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700663 * has additional restrictions
664 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700665#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700666
Anas Nashifa541e932018-05-24 11:19:16 -0500667/**
668 * @brief Inherit Permissions
669 *
670 * @details
671 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700672 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
673 * is not enabled.
674 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700675#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700676
Benjamin Walshed240f22017-01-22 13:05:08 -0500677#ifdef CONFIG_X86
678/* x86 Bitmask definitions for threads user options */
679
680#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
681/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700682#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500683#endif
684#endif
685
686/* end - thread options */
687
688#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400689/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700690 * @brief Create a thread.
691 *
692 * This routine initializes a thread, then schedules it for execution.
693 *
694 * The new thread may be scheduled for immediate execution or a delayed start.
695 * If the newly spawned thread does not have a delayed start the kernel
696 * scheduler may preempt the current thread to allow the new thread to
697 * execute.
698 *
699 * Thread options are architecture-specific, and can include K_ESSENTIAL,
700 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
701 * them using "|" (the logical OR operator).
702 *
703 * Historically, users often would use the beginning of the stack memory region
704 * to store the struct k_thread data, although corruption will occur if the
705 * stack overflows this region and stack protection features may not detect this
706 * situation.
707 *
708 * @param new_thread Pointer to uninitialized struct k_thread
709 * @param stack Pointer to the stack space.
710 * @param stack_size Stack size in bytes.
711 * @param entry Thread entry function.
712 * @param p1 1st entry point parameter.
713 * @param p2 2nd entry point parameter.
714 * @param p3 3rd entry point parameter.
715 * @param prio Thread priority.
716 * @param options Thread options.
717 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
718 *
719 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400720 *
721 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700722 */
Andrew Boie662c3452017-10-02 10:51:18 -0700723__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700724 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700725 size_t stack_size,
726 k_thread_entry_t entry,
727 void *p1, void *p2, void *p3,
728 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700729
Andrew Boie3f091b52017-08-30 14:34:14 -0700730/**
731 * @brief Drop a thread's privileges permanently to user mode
732 *
733 * @param entry Function to start executing from
734 * @param p1 1st entry point parameter
735 * @param p2 2nd entry point parameter
736 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400737 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700738 */
739extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
740 void *p1, void *p2,
741 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700742
Andrew Boied26cf2d2017-03-30 13:07:02 -0700743/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700744 * @brief Grant a thread access to a NULL-terminated set of kernel objects
745 *
746 * This is a convenience function. For the provided thread, grant access to
747 * the remaining arguments, which must be pointers to kernel objects.
748 * The final argument must be a NULL.
749 *
750 * The thread object must be initialized (i.e. running). The objects don't
751 * need to be.
752 *
753 * @param thread Thread to grant access to objects
754 * @param ... NULL-terminated list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400755 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700756 */
757extern void __attribute__((sentinel))
758 k_thread_access_grant(struct k_thread *thread, ...);
759
760/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700761 * @brief Assign a resource memory pool to a thread
762 *
763 * By default, threads have no resource pool assigned unless their parent
764 * thread has a resource pool, in which case it is inherited. Multiple
765 * threads may be assigned to the same memory pool.
766 *
767 * Changing a thread's resource pool will not migrate allocations from the
768 * previous pool.
769 *
770 * @param thread Target thread to assign a memory pool for resource requests,
771 * or NULL if the thread should no longer have a memory pool.
772 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400773 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700774 */
775static inline void k_thread_resource_pool_assign(struct k_thread *thread,
776 struct k_mem_pool *pool)
777{
778 thread->resource_pool = pool;
779}
780
781#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
782/**
783 * @brief Assign the system heap as a thread's resource pool
784 *
785 * Similar to k_thread_resource_pool_assign(), but the thread will use
786 * the kernel heap to draw memory.
787 *
788 * Use with caution, as a malicious thread could perform DoS attacks on the
789 * kernel heap.
790 *
791 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400792 *
793 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700794 */
795void k_thread_system_pool_assign(struct k_thread *thread);
796#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
797
798/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500799 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400800 *
Allan Stephensc98da842016-11-11 15:45:03 -0500801 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500802 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500804 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400805 *
806 * @return N/A
807 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700808__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400809
810/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500811 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400812 *
813 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500814 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400815 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400816 * @return N/A
817 */
Kumar Galacc334c72017-04-21 10:55:34 -0500818extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819
820/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500821 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500823 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400824 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500825 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400826 *
827 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400828 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829 */
Andrew Boie468190a2017-09-29 14:00:48 -0700830__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400831
832/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500833 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400834 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500835 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500837 * If @a thread is not currently sleeping, the routine has no effect.
838 *
839 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400840 *
841 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400842 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400843 */
Andrew Boie468190a2017-09-29 14:00:48 -0700844__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400845
846/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500847 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500849 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400850 *
851 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400852 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700853__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400854
855/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500856 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500858 * This routine prevents @a thread from executing if it has not yet started
859 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500861 * @param thread ID of thread to cancel.
862 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700863 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500864 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700865 *
866 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400867 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700868__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400869
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400870/**
Allan Stephensc98da842016-11-11 15:45:03 -0500871 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400872 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500873 * This routine permanently stops execution of @a thread. The thread is taken
874 * off all kernel queues it is part of (i.e. the ready queue, the timeout
875 * queue, or a kernel object wait queue). However, any kernel resources the
876 * thread might currently own (such as mutexes or memory blocks) are not
877 * released. It is the responsibility of the caller of this routine to ensure
878 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500880 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881 *
882 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400883 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400884 */
Andrew Boie468190a2017-09-29 14:00:48 -0700885__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400886
Andrew Boie7d627c52017-08-30 11:01:56 -0700887
888/**
889 * @brief Start an inactive thread
890 *
891 * If a thread was created with K_FOREVER in the delay parameter, it will
892 * not be added to the scheduling queue until this function is called
893 * on it.
894 *
895 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400896 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700897 */
Andrew Boie468190a2017-09-29 14:00:48 -0700898__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700899
Allan Stephensc98da842016-11-11 15:45:03 -0500900/**
901 * @cond INTERNAL_HIDDEN
902 */
903
Benjamin Walshd211a522016-12-06 11:44:01 -0500904/* timeout has timed out and is not on _timeout_q anymore */
905#define _EXPIRED (-2)
906
907/* timeout is not in use */
908#define _INACTIVE (-1)
909
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400910struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700911 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700912 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400913 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700914 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500915 void *init_p1;
916 void *init_p2;
917 void *init_p3;
918 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500919 u32_t init_options;
920 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500921 void (*init_abort)(void);
Anas Nashif57554052018-03-03 02:31:05 -0600922 const char *init_name;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400923};
924
Andrew Boied26cf2d2017-03-30 13:07:02 -0700925#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400926 entry, p1, p2, p3, \
Anas Nashif57554052018-03-03 02:31:05 -0600927 prio, options, delay, abort, tname) \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500928 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700929 .init_thread = (thread), \
930 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500931 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700932 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400933 .init_p1 = (void *)p1, \
934 .init_p2 = (void *)p2, \
935 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500936 .init_prio = (prio), \
937 .init_options = (options), \
938 .init_delay = (delay), \
939 .init_abort = (abort), \
Anas Nashif57554052018-03-03 02:31:05 -0600940 .init_name = STRINGIFY(tname), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400941 }
942
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400943/**
Allan Stephensc98da842016-11-11 15:45:03 -0500944 * INTERNAL_HIDDEN @endcond
945 */
946
947/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500948 * @brief Statically define and initialize a thread.
949 *
950 * The thread may be scheduled for immediate execution or a delayed start.
951 *
952 * Thread options are architecture-specific, and can include K_ESSENTIAL,
953 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
954 * them using "|" (the logical OR operator).
955 *
956 * The ID of the thread can be accessed using:
957 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500958 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500959 *
960 * @param name Name of the thread.
961 * @param stack_size Stack size in bytes.
962 * @param entry Thread entry function.
963 * @param p1 1st entry point parameter.
964 * @param p2 2nd entry point parameter.
965 * @param p3 3rd entry point parameter.
966 * @param prio Thread priority.
967 * @param options Thread options.
968 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400969 *
Anas Nashif47420d02018-05-24 14:20:56 -0400970 * @req K-THREAD-010
971 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400972 * @internal It has been observed that the x86 compiler by default aligns
973 * these _static_thread_data structures to 32-byte boundaries, thereby
974 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400975 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400976 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500977#define K_THREAD_DEFINE(name, stack_size, \
978 entry, p1, p2, p3, \
979 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700980 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700981 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500982 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500983 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700984 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
985 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500986 entry, p1, p2, p3, prio, options, delay, \
Anas Nashif57554052018-03-03 02:31:05 -0600987 NULL, name); \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700988 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400989
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400990/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500991 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500993 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400994 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500995 * @param thread ID of thread whose priority is needed.
996 *
997 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400998 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400999 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001000__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001001
1002/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001003 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001005 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001006 *
1007 * Rescheduling can occur immediately depending on the priority @a thread is
1008 * set to:
1009 *
1010 * - If its priority is raised above the priority of the caller of this
1011 * function, and the caller is preemptible, @a thread will be scheduled in.
1012 *
1013 * - If the caller operates on itself, it lowers its priority below that of
1014 * other threads in the system, and the caller is preemptible, the thread of
1015 * highest priority will be scheduled in.
1016 *
1017 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1018 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1019 * highest priority.
1020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001021 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001022 * @param prio New priority.
1023 *
1024 * @warning Changing the priority of a thread currently involved in mutex
1025 * priority inheritance may result in undefined behavior.
1026 *
1027 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001028 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001029 */
Andrew Boie468190a2017-09-29 14:00:48 -07001030__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001031
Andy Ross4a2e50f2018-05-15 11:06:25 -07001032
1033#ifdef CONFIG_SCHED_DEADLINE
1034/**
1035 * @brief Set deadline expiration time for scheduler
1036 *
1037 * This sets the "deadline" expiration as a time delta from the
1038 * current time, in the same units used by k_cycle_get_32(). The
1039 * scheduler (when deadline scheduling is enabled) will choose the
1040 * next expiring thread when selecting between threads at the same
1041 * static priority. Threads at different priorities will be scheduled
1042 * according to their static priority.
1043 *
1044 * @note Deadlines that are negative (i.e. in the past) are still seen
1045 * as higher priority than others, even if the thread has "finished"
1046 * its work. If you don't want it scheduled anymore, you have to
1047 * reset the deadline into the future, block/pend the thread, or
1048 * modify its priority with k_thread_priority_set().
1049 *
1050 * @note Despite the API naming, the scheduler makes no guarantees the
1051 * the thread WILL be scheduled within that deadline, nor does it take
1052 * extra metadata (like e.g. the "runtime" and "period" parameters in
1053 * Linux sched_setattr()) that allows the kernel to validate the
1054 * scheduling for achievability. Such features could be implemented
1055 * above this call, which is simply input to the priority selection
1056 * logic.
1057 *
1058 * @param thread A thread on which to set the deadline
1059 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001060 *
1061 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001062 */
1063__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1064#endif
1065
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001066/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001067 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001069 * This routine prevents the kernel scheduler from making @a thread the
1070 * current thread. All other internal operations on @a thread are still
1071 * performed; for example, any timeout it is waiting on keeps ticking,
1072 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001074 * If @a thread is already suspended, the routine has no effect.
1075 *
1076 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001077 *
1078 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001079 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001080 */
Andrew Boie468190a2017-09-29 14:00:48 -07001081__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001082
1083/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001084 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001085 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001086 * This routine allows the kernel scheduler to make @a thread the current
1087 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001089 * If @a thread is not currently suspended, the routine has no effect.
1090 *
1091 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001092 *
1093 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001094 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001095 */
Andrew Boie468190a2017-09-29 14:00:48 -07001096__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001097
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001098/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001099 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001101 * This routine specifies how the scheduler will perform time slicing of
1102 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001104 * To enable time slicing, @a slice must be non-zero. The scheduler
1105 * ensures that no thread runs for more than the specified time limit
1106 * before other threads of that priority are given a chance to execute.
1107 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001108 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001110 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001111 * execute. Once the scheduler selects a thread for execution, there is no
1112 * minimum guaranteed time the thread will execute before threads of greater or
1113 * equal priority are scheduled.
1114 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001115 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001116 * for execution, this routine has no effect; the thread is immediately
1117 * rescheduled after the slice period expires.
1118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001119 * To disable timeslicing, set both @a slice and @a prio to zero.
1120 *
1121 * @param slice Maximum time slice length (in milliseconds).
1122 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001123 *
1124 * @return N/A
1125 */
Kumar Galacc334c72017-04-21 10:55:34 -05001126extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001127
Anas Nashif166f5192018-02-25 08:02:36 -06001128/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001129
1130/**
1131 * @addtogroup isr_apis
1132 * @{
1133 */
1134
1135/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001136 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001137 *
Allan Stephensc98da842016-11-11 15:45:03 -05001138 * This routine allows the caller to customize its actions, depending on
1139 * whether it is a thread or an ISR.
1140 *
1141 * @note Can be called by ISRs.
1142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001143 * @return 0 if invoked by a thread.
1144 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001145 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001146extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001147
Benjamin Walsh445830d2016-11-10 15:54:27 -05001148/**
1149 * @brief Determine if code is running in a preemptible thread.
1150 *
Allan Stephensc98da842016-11-11 15:45:03 -05001151 * This routine allows the caller to customize its actions, depending on
1152 * whether it can be preempted by another thread. The routine returns a 'true'
1153 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001154 *
Allan Stephensc98da842016-11-11 15:45:03 -05001155 * - The code is running in a thread, not at ISR.
1156 * - The thread's priority is in the preemptible range.
1157 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001158 *
Allan Stephensc98da842016-11-11 15:45:03 -05001159 * @note Can be called by ISRs.
1160 *
1161 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001162 * @return Non-zero if invoked by a preemptible thread.
1163 */
Andrew Boie468190a2017-09-29 14:00:48 -07001164__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001165
Allan Stephensc98da842016-11-11 15:45:03 -05001166/**
Anas Nashif166f5192018-02-25 08:02:36 -06001167 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001168 */
1169
1170/**
1171 * @addtogroup thread_apis
1172 * @{
1173 */
1174
1175/**
1176 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001177 *
Allan Stephensc98da842016-11-11 15:45:03 -05001178 * This routine prevents the current thread from being preempted by another
1179 * thread by instructing the scheduler to treat it as a cooperative thread.
1180 * If the thread subsequently performs an operation that makes it unready,
1181 * it will be context switched out in the normal manner. When the thread
1182 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001183 *
Allan Stephensc98da842016-11-11 15:45:03 -05001184 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001185 *
Allan Stephensc98da842016-11-11 15:45:03 -05001186 * @note k_sched_lock() and k_sched_unlock() should normally be used
1187 * when the operation being performed can be safely interrupted by ISRs.
1188 * However, if the amount of processing involved is very small, better
1189 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001190 *
1191 * @return N/A
1192 */
1193extern void k_sched_lock(void);
1194
Allan Stephensc98da842016-11-11 15:45:03 -05001195/**
1196 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001197 *
Allan Stephensc98da842016-11-11 15:45:03 -05001198 * This routine reverses the effect of a previous call to k_sched_lock().
1199 * A thread must call the routine once for each time it called k_sched_lock()
1200 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001201 *
1202 * @return N/A
1203 */
1204extern void k_sched_unlock(void);
1205
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001206/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001207 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001208 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001209 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001210 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001211 * Custom data is not used by the kernel itself, and is freely available
1212 * for a thread to use as it sees fit. It can be used as a framework
1213 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001215 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001216 *
1217 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001218 *
1219 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001220 */
Andrew Boie468190a2017-09-29 14:00:48 -07001221__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001222
1223/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001224 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001225 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001226 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001227 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001228 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001229 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001230 */
Andrew Boie468190a2017-09-29 14:00:48 -07001231__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001232
1233/**
Anas Nashif57554052018-03-03 02:31:05 -06001234 * @brief Set current thread name
1235 *
1236 * Set the name of the thread to be used when THREAD_MONITOR is enabled for
1237 * tracing and debugging.
1238 *
1239 */
1240__syscall void k_thread_name_set(k_tid_t thread_id, const char *value);
1241
1242/**
1243 * @brief Get thread name
1244 *
1245 * Get the name of a thread
1246 *
1247 * @param thread_id Thread ID
1248 *
1249 */
1250__syscall const char *k_thread_name_get(k_tid_t thread_id);
1251
1252/**
Anas Nashif166f5192018-02-25 08:02:36 -06001253 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001254 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001255#include <sys_clock.h>
1256
Allan Stephensc2f15a42016-11-17 12:24:22 -05001257/**
1258 * @addtogroup clock_apis
1259 * @{
1260 */
1261
1262/**
1263 * @brief Generate null timeout delay.
1264 *
1265 * This macro generates a timeout delay that that instructs a kernel API
1266 * not to wait if the requested operation cannot be performed immediately.
1267 *
1268 * @return Timeout delay value.
1269 */
1270#define K_NO_WAIT 0
1271
1272/**
1273 * @brief Generate timeout delay from milliseconds.
1274 *
1275 * This macro generates a timeout delay that that instructs a kernel API
1276 * to wait up to @a ms milliseconds to perform the requested operation.
1277 *
1278 * @param ms Duration in milliseconds.
1279 *
1280 * @return Timeout delay value.
1281 */
Johan Hedberg14471692016-11-13 10:52:15 +02001282#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001283
1284/**
1285 * @brief Generate timeout delay from seconds.
1286 *
1287 * This macro generates a timeout delay that that instructs a kernel API
1288 * to wait up to @a s seconds to perform the requested operation.
1289 *
1290 * @param s Duration in seconds.
1291 *
1292 * @return Timeout delay value.
1293 */
Johan Hedberg14471692016-11-13 10:52:15 +02001294#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001295
1296/**
1297 * @brief Generate timeout delay from minutes.
1298 *
1299 * This macro generates a timeout delay that that instructs a kernel API
1300 * to wait up to @a m minutes to perform the requested operation.
1301 *
1302 * @param m Duration in minutes.
1303 *
1304 * @return Timeout delay value.
1305 */
Johan Hedberg14471692016-11-13 10:52:15 +02001306#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001307
1308/**
1309 * @brief Generate timeout delay from hours.
1310 *
1311 * This macro generates a timeout delay that that instructs a kernel API
1312 * to wait up to @a h hours to perform the requested operation.
1313 *
1314 * @param h Duration in hours.
1315 *
1316 * @return Timeout delay value.
1317 */
Johan Hedberg14471692016-11-13 10:52:15 +02001318#define K_HOURS(h) K_MINUTES((h) * 60)
1319
Allan Stephensc98da842016-11-11 15:45:03 -05001320/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001321 * @brief Generate infinite timeout delay.
1322 *
1323 * This macro generates a timeout delay that that instructs a kernel API
1324 * to wait as long as necessary to perform the requested operation.
1325 *
1326 * @return Timeout delay value.
1327 */
1328#define K_FOREVER (-1)
1329
1330/**
Anas Nashif166f5192018-02-25 08:02:36 -06001331 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001332 */
1333
1334/**
Allan Stephensc98da842016-11-11 15:45:03 -05001335 * @cond INTERNAL_HIDDEN
1336 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001337
Benjamin Walsh62092182016-12-20 14:39:08 -05001338/* kernel clocks */
1339
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001340#ifdef CONFIG_SYS_CLOCK_EXISTS
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001341
1342/*
1343 * If timer frequency is known at compile time, a simple (32-bit)
1344 * tick <-> ms conversion could be used for some combinations of
1345 * hardware timer frequency and tick rate. Otherwise precise
1346 * (64-bit) calculations are used.
1347 */
1348
1349#if !defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
1350#if (sys_clock_hw_cycles_per_sec % sys_clock_ticks_per_sec) != 0
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001351 #define _NEED_PRECISE_TICK_MS_CONVERSION
1352#elif (MSEC_PER_SEC % sys_clock_ticks_per_sec) != 0
Benjamin Walsh62092182016-12-20 14:39:08 -05001353 #define _NON_OPTIMIZED_TICKS_PER_SEC
1354#endif
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001355#endif
Benjamin Walsh62092182016-12-20 14:39:08 -05001356
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001357#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
1358 defined(_NON_OPTIMIZED_TICKS_PER_SEC)
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001359 #define _NEED_PRECISE_TICK_MS_CONVERSION
1360#endif
1361#endif
1362
Kumar Galacc334c72017-04-21 10:55:34 -05001363static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001364{
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001365#ifdef CONFIG_SYS_CLOCK_EXISTS
1366
1367#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
1368 /* use 64-bit math to keep precision */
Piotr Zięcik3c7f9902018-07-23 14:09:10 +02001369 return (s32_t)ceiling_fraction(
1370 (s64_t)ms * sys_clock_hw_cycles_per_sec,
Vinayak Kariappa Chettimadac7d27342018-08-31 08:58:59 +02001371 ((s64_t)MSEC_PER_SEC * sys_clock_hw_cycles_per_sec) /
1372 sys_clock_ticks_per_sec);
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001373#else
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001374 /* simple division keeps precision */
1375 s32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
1376
1377 return (s32_t)ceiling_fraction(ms, ms_per_tick);
1378#endif
1379
1380#else
1381 __ASSERT(ms == 0, "ms not zero");
1382 return 0;
Benjamin Walsh62092182016-12-20 14:39:08 -05001383#endif
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001384}
Benjamin Walsh62092182016-12-20 14:39:08 -05001385
Kumar Galacc334c72017-04-21 10:55:34 -05001386static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001387{
Benjamin Walsh62092182016-12-20 14:39:08 -05001388#ifdef CONFIG_SYS_CLOCK_EXISTS
1389
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001390#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
1391 /* use 64-bit math to keep precision */
Vinayak Kariappa Chettimadac7d27342018-08-31 08:58:59 +02001392 return (u64_t)ticks * MSEC_PER_SEC / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001393#else
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001394 /* simple multiplication keeps precision */
1395 u32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
1396
1397 return (u64_t)ticks * ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001398#endif
1399
1400#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001401 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001402 return 0;
1403#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001404}
1405
Piotr Zięcik77f42f82018-06-11 14:26:29 +02001406/* added tick needed to account for tick in progress */
1407#ifdef CONFIG_TICKLESS_KERNEL
1408#define _TICK_ALIGN 0
1409#else
1410#define _TICK_ALIGN 1
1411#endif
1412
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001413struct k_timer {
1414 /*
1415 * _timeout structure must be first here if we want to use
1416 * dynamic timer allocation. timeout.node is used in the double-linked
1417 * list of free timers
1418 */
1419 struct _timeout timeout;
1420
Allan Stephens45bfa372016-10-12 12:39:42 -05001421 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001422 _wait_q_t wait_q;
1423
1424 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001425 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001426
1427 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001428 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001429
1430 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001431 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001432
Allan Stephens45bfa372016-10-12 12:39:42 -05001433 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001434 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001435
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001436 /* user-specific data, also used to support legacy features */
1437 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001438
Anas Nashif2f203c22016-12-18 06:57:45 -05001439 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001440};
1441
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001442#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001443 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001444 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001445 .timeout.wait_q = NULL, \
1446 .timeout.thread = NULL, \
1447 .timeout.func = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001448 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001449 .expiry_fn = expiry, \
1450 .stop_fn = stop, \
1451 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001452 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001453 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001454 }
1455
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001456#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1457
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001458/**
Allan Stephensc98da842016-11-11 15:45:03 -05001459 * INTERNAL_HIDDEN @endcond
1460 */
1461
1462/**
1463 * @defgroup timer_apis Timer APIs
1464 * @ingroup kernel_apis
1465 * @{
1466 */
1467
1468/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001469 * @typedef k_timer_expiry_t
1470 * @brief Timer expiry function type.
1471 *
1472 * A timer's expiry function is executed by the system clock interrupt handler
1473 * each time the timer expires. The expiry function is optional, and is only
1474 * invoked if the timer has been initialized with one.
1475 *
1476 * @param timer Address of timer.
1477 *
1478 * @return N/A
1479 */
1480typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1481
1482/**
1483 * @typedef k_timer_stop_t
1484 * @brief Timer stop function type.
1485 *
1486 * A timer's stop function is executed if the timer is stopped prematurely.
1487 * The function runs in the context of the thread that stops the timer.
1488 * The stop function is optional, and is only invoked if the timer has been
1489 * initialized with one.
1490 *
1491 * @param timer Address of timer.
1492 *
1493 * @return N/A
1494 */
1495typedef void (*k_timer_stop_t)(struct k_timer *timer);
1496
1497/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001498 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001500 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001501 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001502 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001503 *
1504 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001505 * @param expiry_fn Function to invoke each time the timer expires.
1506 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001507 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001508#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001509 struct k_timer name \
1510 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001511 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001512
Allan Stephens45bfa372016-10-12 12:39:42 -05001513/**
1514 * @brief Initialize a timer.
1515 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001516 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001517 *
1518 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001519 * @param expiry_fn Function to invoke each time the timer expires.
1520 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001521 *
1522 * @return N/A
1523 */
1524extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001525 k_timer_expiry_t expiry_fn,
1526 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001527
Allan Stephens45bfa372016-10-12 12:39:42 -05001528/**
1529 * @brief Start a timer.
1530 *
1531 * This routine starts a timer, and resets its status to zero. The timer
1532 * begins counting down using the specified duration and period values.
1533 *
1534 * Attempting to start a timer that is already running is permitted.
1535 * The timer's status is reset to zero and the timer begins counting down
1536 * using the new duration and period values.
1537 *
1538 * @param timer Address of timer.
1539 * @param duration Initial timer duration (in milliseconds).
1540 * @param period Timer period (in milliseconds).
1541 *
1542 * @return N/A
1543 */
Andrew Boiea354d492017-09-29 16:22:28 -07001544__syscall void k_timer_start(struct k_timer *timer,
1545 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001546
1547/**
1548 * @brief Stop a timer.
1549 *
1550 * This routine stops a running timer prematurely. The timer's stop function,
1551 * if one exists, is invoked by the caller.
1552 *
1553 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001554 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001555 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001556 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1557 * if @a k_timer_stop is to be called from ISRs.
1558 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001559 * @param timer Address of timer.
1560 *
1561 * @return N/A
1562 */
Andrew Boiea354d492017-09-29 16:22:28 -07001563__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001564
1565/**
1566 * @brief Read timer status.
1567 *
1568 * This routine reads the timer's status, which indicates the number of times
1569 * it has expired since its status was last read.
1570 *
1571 * Calling this routine resets the timer's status to zero.
1572 *
1573 * @param timer Address of timer.
1574 *
1575 * @return Timer status.
1576 */
Andrew Boiea354d492017-09-29 16:22:28 -07001577__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001578
1579/**
1580 * @brief Synchronize thread to timer expiration.
1581 *
1582 * This routine blocks the calling thread until the timer's status is non-zero
1583 * (indicating that it has expired at least once since it was last examined)
1584 * or the timer is stopped. If the timer status is already non-zero,
1585 * or the timer is already stopped, the caller continues without waiting.
1586 *
1587 * Calling this routine resets the timer's status to zero.
1588 *
1589 * This routine must not be used by interrupt handlers, since they are not
1590 * allowed to block.
1591 *
1592 * @param timer Address of timer.
1593 *
1594 * @return Timer status.
1595 */
Andrew Boiea354d492017-09-29 16:22:28 -07001596__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001597
1598/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001599 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001600 *
1601 * This routine computes the (approximate) time remaining before a running
1602 * timer next expires. If the timer is not running, it returns zero.
1603 *
1604 * @param timer Address of timer.
1605 *
1606 * @return Remaining time (in milliseconds).
1607 */
Andrew Boiea354d492017-09-29 16:22:28 -07001608__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1609
1610static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001611{
1612 return _timeout_remaining_get(&timer->timeout);
1613}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001614
Allan Stephensc98da842016-11-11 15:45:03 -05001615/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001616 * @brief Associate user-specific data with a timer.
1617 *
1618 * This routine records the @a user_data with the @a timer, to be retrieved
1619 * later.
1620 *
1621 * It can be used e.g. in a timer handler shared across multiple subsystems to
1622 * retrieve data specific to the subsystem this timer is associated with.
1623 *
1624 * @param timer Address of timer.
1625 * @param user_data User data to associate with the timer.
1626 *
1627 * @return N/A
1628 */
Andrew Boiea354d492017-09-29 16:22:28 -07001629__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1630
Anas Nashif954d5502018-02-25 08:37:28 -06001631/**
1632 * @internal
1633 */
Andrew Boiea354d492017-09-29 16:22:28 -07001634static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1635 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001636{
1637 timer->user_data = user_data;
1638}
1639
1640/**
1641 * @brief Retrieve the user-specific data from a timer.
1642 *
1643 * @param timer Address of timer.
1644 *
1645 * @return The user data.
1646 */
Andrew Boiea354d492017-09-29 16:22:28 -07001647__syscall void *k_timer_user_data_get(struct k_timer *timer);
1648
1649static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001650{
1651 return timer->user_data;
1652}
1653
Anas Nashif166f5192018-02-25 08:02:36 -06001654/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001655
Allan Stephensc98da842016-11-11 15:45:03 -05001656/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001657 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001658 * @{
1659 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001660
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001661/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001662 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001663 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001664 * This routine returns the elapsed time since the system booted,
1665 * in milliseconds.
1666 *
1667 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001668 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001669__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001670
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001671/**
1672 * @brief Enable clock always on in tickless kernel
1673 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001674 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001675 * there are no timer events programmed in tickless kernel
1676 * scheduling. This is necessary if the clock is used to track
1677 * passage of time.
1678 *
1679 * @retval prev_status Previous status of always on flag
1680 */
1681static inline int k_enable_sys_clock_always_on(void)
1682{
Flavio Ceolinf23a8cd2018-08-13 12:32:56 -07001683#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001684 int prev_status = _sys_clock_always_on;
1685
1686 _sys_clock_always_on = 1;
1687 _enable_sys_clock();
1688
1689 return prev_status;
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301690#else
Flavio Ceolinf23a8cd2018-08-13 12:32:56 -07001691 return -ENOTSUP;
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301692#endif
Flavio Ceolinf23a8cd2018-08-13 12:32:56 -07001693}
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001694
1695/**
1696 * @brief Disable clock always on in tickless kernel
1697 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001698 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001699 * there are no timer events programmed in tickless kernel
1700 * scheduling. To save power, this routine should be called
1701 * immediately when clock is not used to track time.
1702 */
1703static inline void k_disable_sys_clock_always_on(void)
1704{
Flavio Ceolinf23a8cd2018-08-13 12:32:56 -07001705#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001706 _sys_clock_always_on = 0;
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001707#endif
Flavio Ceolinf23a8cd2018-08-13 12:32:56 -07001708}
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001709
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001710/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001711 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001712 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001713 * This routine returns the lower 32-bits of the elapsed time since the system
1714 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001716 * This routine can be more efficient than k_uptime_get(), as it reduces the
1717 * need for interrupt locking and 64-bit math. However, the 32-bit result
1718 * cannot hold a system uptime time larger than approximately 50 days, so the
1719 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001720 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001721 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001722 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001723__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001724
1725/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001726 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001728 * This routine computes the elapsed time between the current system uptime
1729 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001730 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001731 * @param reftime Pointer to a reference time, which is updated to the current
1732 * uptime upon return.
1733 *
1734 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001735 */
Kumar Galacc334c72017-04-21 10:55:34 -05001736extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001737
1738/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001739 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001741 * This routine computes the elapsed time between the current system uptime
1742 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001743 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001744 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1745 * need for interrupt locking and 64-bit math. However, the 32-bit result
1746 * cannot hold an elapsed time larger than approximately 50 days, so the
1747 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001748 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001749 * @param reftime Pointer to a reference time, which is updated to the current
1750 * uptime upon return.
1751 *
1752 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001753 */
Kumar Galacc334c72017-04-21 10:55:34 -05001754extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001755
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001756/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001757 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001758 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001759 * This routine returns the current time, as measured by the system's hardware
1760 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001761 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001762 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001763 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001764#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001765
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001766/**
Anas Nashif166f5192018-02-25 08:02:36 -06001767 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001768 */
1769
Allan Stephensc98da842016-11-11 15:45:03 -05001770/**
1771 * @cond INTERNAL_HIDDEN
1772 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001773
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001774struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001775 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001776 union {
1777 _wait_q_t wait_q;
1778
1779 _POLL_EVENT;
1780 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001781
1782 _OBJECT_TRACING_NEXT_PTR(k_queue);
1783};
1784
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001785#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001786 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001787 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001788 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001789 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001790 _OBJECT_TRACING_INIT \
1791 }
1792
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001793#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1794
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001795extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1796
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001797/**
1798 * INTERNAL_HIDDEN @endcond
1799 */
1800
1801/**
1802 * @defgroup queue_apis Queue APIs
1803 * @ingroup kernel_apis
1804 * @{
1805 */
1806
1807/**
1808 * @brief Initialize a queue.
1809 *
1810 * This routine initializes a queue object, prior to its first use.
1811 *
1812 * @param queue Address of the queue.
1813 *
1814 * @return N/A
1815 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001816__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001817
1818/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001819 * @brief Cancel waiting on a queue.
1820 *
1821 * This routine causes first thread pending on @a queue, if any, to
1822 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001823 * If the queue is being waited on by k_poll(), it will return with
1824 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1825 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001826 *
1827 * @note Can be called by ISRs.
1828 *
1829 * @param queue Address of the queue.
1830 *
1831 * @return N/A
1832 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001833__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001834
1835/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001836 * @brief Append an element to the end of a queue.
1837 *
1838 * This routine appends a data item to @a queue. A queue data item must be
1839 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1840 * reserved for the kernel's use.
1841 *
1842 * @note Can be called by ISRs.
1843 *
1844 * @param queue Address of the queue.
1845 * @param data Address of the data item.
1846 *
1847 * @return N/A
1848 */
1849extern void k_queue_append(struct k_queue *queue, void *data);
1850
1851/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001852 * @brief Append an element to a queue.
1853 *
1854 * This routine appends a data item to @a queue. There is an implicit
1855 * memory allocation from the calling thread's resource pool, which is
1856 * automatically freed when the item is removed from the queue.
1857 *
1858 * @note Can be called by ISRs.
1859 *
1860 * @param queue Address of the queue.
1861 * @param data Address of the data item.
1862 *
1863 * @retval 0 on success
1864 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1865 */
1866__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1867
1868/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001869 * @brief Prepend an element to a queue.
1870 *
1871 * This routine prepends a data item to @a queue. A queue data item must be
1872 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1873 * reserved for the kernel's use.
1874 *
1875 * @note Can be called by ISRs.
1876 *
1877 * @param queue Address of the queue.
1878 * @param data Address of the data item.
1879 *
1880 * @return N/A
1881 */
1882extern void k_queue_prepend(struct k_queue *queue, void *data);
1883
1884/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001885 * @brief Prepend an element to a queue.
1886 *
1887 * This routine prepends a data item to @a queue. There is an implicit
1888 * memory allocation from the calling thread's resource pool, which is
1889 * automatically freed when the item is removed from the queue.
1890 *
1891 * @note Can be called by ISRs.
1892 *
1893 * @param queue Address of the queue.
1894 * @param data Address of the data item.
1895 *
1896 * @retval 0 on success
1897 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1898 */
1899__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1900
1901/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001902 * @brief Inserts an element to a queue.
1903 *
1904 * This routine inserts a data item to @a queue after previous item. A queue
1905 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1906 * item are reserved for the kernel's use.
1907 *
1908 * @note Can be called by ISRs.
1909 *
1910 * @param queue Address of the queue.
1911 * @param prev Address of the previous data item.
1912 * @param data Address of the data item.
1913 *
1914 * @return N/A
1915 */
1916extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1917
1918/**
1919 * @brief Atomically append a list of elements to a queue.
1920 *
1921 * This routine adds a list of data items to @a queue in one operation.
1922 * The data items must be in a singly-linked list, with the first 32 bits
1923 * in each data item pointing to the next data item; the list must be
1924 * NULL-terminated.
1925 *
1926 * @note Can be called by ISRs.
1927 *
1928 * @param queue Address of the queue.
1929 * @param head Pointer to first node in singly-linked list.
1930 * @param tail Pointer to last node in singly-linked list.
1931 *
1932 * @return N/A
1933 */
1934extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1935
1936/**
1937 * @brief Atomically add a list of elements to a queue.
1938 *
1939 * This routine adds a list of data items to @a queue in one operation.
1940 * The data items must be in a singly-linked list implemented using a
1941 * sys_slist_t object. Upon completion, the original list is empty.
1942 *
1943 * @note Can be called by ISRs.
1944 *
1945 * @param queue Address of the queue.
1946 * @param list Pointer to sys_slist_t object.
1947 *
1948 * @return N/A
1949 */
1950extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1951
1952/**
1953 * @brief Get an element from a queue.
1954 *
1955 * This routine removes first data item from @a queue. The first 32 bits of the
1956 * data item are reserved for the kernel's use.
1957 *
1958 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1959 *
1960 * @param queue Address of the queue.
1961 * @param timeout Waiting period to obtain a data item (in milliseconds),
1962 * or one of the special values K_NO_WAIT and K_FOREVER.
1963 *
1964 * @return Address of the data item if successful; NULL if returned
1965 * without waiting, or waiting period timed out.
1966 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001967__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001968
1969/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001970 * @brief Remove an element from a queue.
1971 *
1972 * This routine removes data item from @a queue. The first 32 bits of the
1973 * data item are reserved for the kernel's use. Removing elements from k_queue
1974 * rely on sys_slist_find_and_remove which is not a constant time operation.
1975 *
1976 * @note Can be called by ISRs
1977 *
1978 * @param queue Address of the queue.
1979 * @param data Address of the data item.
1980 *
1981 * @return true if data item was removed
1982 */
1983static inline bool k_queue_remove(struct k_queue *queue, void *data)
1984{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001985 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001986}
1987
1988/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001989 * @brief Query a queue to see if it has data available.
1990 *
1991 * Note that the data might be already gone by the time this function returns
1992 * if other threads are also trying to read from the queue.
1993 *
1994 * @note Can be called by ISRs.
1995 *
1996 * @param queue Address of the queue.
1997 *
1998 * @return Non-zero if the queue is empty.
1999 * @return 0 if data is available.
2000 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002001__syscall int k_queue_is_empty(struct k_queue *queue);
2002
2003static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002004{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002005 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002006}
2007
2008/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002009 * @brief Peek element at the head of queue.
2010 *
2011 * Return element from the head of queue without removing it.
2012 *
2013 * @param queue Address of the queue.
2014 *
2015 * @return Head element, or NULL if queue is empty.
2016 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002017__syscall void *k_queue_peek_head(struct k_queue *queue);
2018
2019static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002020{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002021 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002022}
2023
2024/**
2025 * @brief Peek element at the tail of queue.
2026 *
2027 * Return element from the tail of queue without removing it.
2028 *
2029 * @param queue Address of the queue.
2030 *
2031 * @return Tail element, or NULL if queue is empty.
2032 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002033__syscall void *k_queue_peek_tail(struct k_queue *queue);
2034
2035static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002036{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002037 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002038}
2039
2040/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002041 * @brief Statically define and initialize a queue.
2042 *
2043 * The queue can be accessed outside the module where it is defined using:
2044 *
2045 * @code extern struct k_queue <name>; @endcode
2046 *
2047 * @param name Name of the queue.
2048 */
2049#define K_QUEUE_DEFINE(name) \
2050 struct k_queue name \
2051 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002052 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002053
Anas Nashif166f5192018-02-25 08:02:36 -06002054/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002055
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002056struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002057 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002058};
2059
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002060/**
2061 * @cond INTERNAL_HIDDEN
2062 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002063#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002064 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002065 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002066 }
2067
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002068#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
2069
Allan Stephensc98da842016-11-11 15:45:03 -05002070/**
2071 * INTERNAL_HIDDEN @endcond
2072 */
2073
2074/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002075 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002076 * @ingroup kernel_apis
2077 * @{
2078 */
2079
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002080/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002081 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002082 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002083 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002084 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002085 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002086 *
2087 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002088 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002089 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002090#define k_fifo_init(fifo) \
2091 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002092
2093/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002094 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002095 *
2096 * This routine causes first thread pending on @a fifo, if any, to
2097 * return from k_fifo_get() call with NULL value (as if timeout
2098 * expired).
2099 *
2100 * @note Can be called by ISRs.
2101 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002102 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002103 *
2104 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002105 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002106 */
2107#define k_fifo_cancel_wait(fifo) \
2108 k_queue_cancel_wait((struct k_queue *) fifo)
2109
2110/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002111 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002112 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002113 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002114 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2115 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002117 * @note Can be called by ISRs.
2118 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002119 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002120 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002121 *
2122 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002123 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002124 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002125#define k_fifo_put(fifo, data) \
2126 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002127
2128/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002129 * @brief Add an element to a FIFO queue.
2130 *
2131 * This routine adds a data item to @a fifo. There is an implicit
2132 * memory allocation from the calling thread's resource pool, which is
2133 * automatically freed when the item is removed.
2134 *
2135 * @note Can be called by ISRs.
2136 *
2137 * @param fifo Address of the FIFO.
2138 * @param data Address of the data item.
2139 *
2140 * @retval 0 on success
2141 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002142 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002143 */
2144#define k_fifo_alloc_put(fifo, data) \
2145 k_queue_alloc_append((struct k_queue *) fifo, data)
2146
2147/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002148 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002149 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002150 * This routine adds a list of data items to @a fifo in one operation.
2151 * The data items must be in a singly-linked list, with the first 32 bits
2152 * each data item pointing to the next data item; the list must be
2153 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002155 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002156 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002157 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002158 * @param head Pointer to first node in singly-linked list.
2159 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002160 *
2161 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002162 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002163 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002164#define k_fifo_put_list(fifo, head, tail) \
2165 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002166
2167/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002168 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002169 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002170 * This routine adds a list of data items to @a fifo in one operation.
2171 * The data items must be in a singly-linked list implemented using a
2172 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002173 * and must be re-initialized via sys_slist_init().
2174 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002175 * @note Can be called by ISRs.
2176 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002177 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002178 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002179 *
2180 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002181 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002182 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002183#define k_fifo_put_slist(fifo, list) \
2184 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002185
2186/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002187 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002188 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002189 * This routine removes a data item from @a fifo in a "first in, first out"
2190 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002191 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002192 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2193 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002194 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002195 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002196 * or one of the special values K_NO_WAIT and K_FOREVER.
2197 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002198 * @return Address of the data item if successful; NULL if returned
2199 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002200 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002201 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002202#define k_fifo_get(fifo, timeout) \
2203 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002204
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002205/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002206 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002207 *
2208 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002209 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002210 *
2211 * @note Can be called by ISRs.
2212 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002213 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002214 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002215 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002216 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002217 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002218 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002219#define k_fifo_is_empty(fifo) \
2220 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002221
2222/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002223 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002224 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002225 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302226 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002227 * on each iteration of processing, a head container will be peeked,
2228 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002229 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002230 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002231 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002232 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002233 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002234 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002235 */
2236#define k_fifo_peek_head(fifo) \
2237 k_queue_peek_head((struct k_queue *) fifo)
2238
2239/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002240 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002241 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002242 * Return element from the tail of FIFO queue (without removing it). A usecase
2243 * for this is if elements of the FIFO queue are themselves containers. Then
2244 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002245 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002246 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002247 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002248 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002249 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002250 */
2251#define k_fifo_peek_tail(fifo) \
2252 k_queue_peek_tail((struct k_queue *) fifo)
2253
2254/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002255 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002256 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002257 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002258 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002259 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002260 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002261 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002262 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002263 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002264#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002265 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002266 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002267 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002268
Anas Nashif166f5192018-02-25 08:02:36 -06002269/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002270
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002271struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002272 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002273};
2274
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002275/**
2276 * @cond INTERNAL_HIDDEN
2277 */
2278
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002279#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002280 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002281 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002282 }
2283
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002284#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2285
Allan Stephensc98da842016-11-11 15:45:03 -05002286/**
2287 * INTERNAL_HIDDEN @endcond
2288 */
2289
2290/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002291 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002292 * @ingroup kernel_apis
2293 * @{
2294 */
2295
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002296/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002297 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002298 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002299 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002300 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002301 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002302 *
2303 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002304 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002305 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002306#define k_lifo_init(lifo) \
2307 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002308
2309/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002310 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002311 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002312 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002313 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2314 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002315 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002316 * @note Can be called by ISRs.
2317 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002318 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002319 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002320 *
2321 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002322 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002323 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002324#define k_lifo_put(lifo, data) \
2325 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002326
2327/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002328 * @brief Add an element to a LIFO queue.
2329 *
2330 * This routine adds a data item to @a lifo. There is an implicit
2331 * memory allocation from the calling thread's resource pool, which is
2332 * automatically freed when the item is removed.
2333 *
2334 * @note Can be called by ISRs.
2335 *
2336 * @param lifo Address of the LIFO.
2337 * @param data Address of the data item.
2338 *
2339 * @retval 0 on success
2340 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002341 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002342 */
2343#define k_lifo_alloc_put(lifo, data) \
2344 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2345
2346/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002347 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002349 * This routine removes a data item from @a lifo in a "last in, first out"
2350 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002351 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002352 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2353 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002354 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002355 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002356 * or one of the special values K_NO_WAIT and K_FOREVER.
2357 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002358 * @return Address of the data item if successful; NULL if returned
2359 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002360 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002361 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002362#define k_lifo_get(lifo, timeout) \
2363 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002364
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002365/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002366 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002367 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002368 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002369 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002370 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002371 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002372 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002373 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002374 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002375#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002376 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002377 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002378 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002379
Anas Nashif166f5192018-02-25 08:02:36 -06002380/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002381
2382/**
2383 * @cond INTERNAL_HIDDEN
2384 */
Andrew Boief3bee952018-05-02 17:44:39 -07002385#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002386
2387struct k_stack {
2388 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002389 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002390
Anas Nashif2f203c22016-12-18 06:57:45 -05002391 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002392 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002393};
2394
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002395#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002396 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002397 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002398 .base = stack_buffer, \
2399 .next = stack_buffer, \
2400 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002401 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002402 }
2403
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002404#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2405
Allan Stephensc98da842016-11-11 15:45:03 -05002406/**
2407 * INTERNAL_HIDDEN @endcond
2408 */
2409
2410/**
2411 * @defgroup stack_apis Stack APIs
2412 * @ingroup kernel_apis
2413 * @{
2414 */
2415
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002416/**
2417 * @brief Initialize a stack.
2418 *
2419 * This routine initializes a stack object, prior to its first use.
2420 *
2421 * @param stack Address of the stack.
2422 * @param buffer Address of array used to hold stacked values.
2423 * @param num_entries Maximum number of values that can be stacked.
2424 *
2425 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002426 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002427 */
Andrew Boief3bee952018-05-02 17:44:39 -07002428void k_stack_init(struct k_stack *stack,
2429 u32_t *buffer, unsigned int num_entries);
2430
2431
2432/**
2433 * @brief Initialize a stack.
2434 *
2435 * This routine initializes a stack object, prior to its first use. Internal
2436 * buffers will be allocated from the calling thread's resource pool.
2437 * This memory will be released if k_stack_cleanup() is called, or
2438 * userspace is enabled and the stack object loses all references to it.
2439 *
2440 * @param stack Address of the stack.
2441 * @param num_entries Maximum number of values that can be stacked.
2442 *
2443 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002444 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002445 */
2446
2447__syscall int k_stack_alloc_init(struct k_stack *stack,
2448 unsigned int num_entries);
2449
2450/**
2451 * @brief Release a stack's allocated buffer
2452 *
2453 * If a stack object was given a dynamically allocated buffer via
2454 * k_stack_alloc_init(), this will free it. This function does nothing
2455 * if the buffer wasn't dynamically allocated.
2456 *
2457 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002458 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002459 */
2460void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002461
2462/**
2463 * @brief Push an element onto a stack.
2464 *
2465 * This routine adds a 32-bit value @a data to @a stack.
2466 *
2467 * @note Can be called by ISRs.
2468 *
2469 * @param stack Address of the stack.
2470 * @param data Value to push onto the stack.
2471 *
2472 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002473 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002474 */
Andrew Boiee8734462017-09-29 16:42:07 -07002475__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002476
2477/**
2478 * @brief Pop an element from a stack.
2479 *
2480 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2481 * manner and stores the value in @a data.
2482 *
2483 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2484 *
2485 * @param stack Address of the stack.
2486 * @param data Address of area to hold the value popped from the stack.
2487 * @param timeout Waiting period to obtain a value (in milliseconds),
2488 * or one of the special values K_NO_WAIT and K_FOREVER.
2489 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002490 * @retval 0 Element popped from stack.
2491 * @retval -EBUSY Returned without waiting.
2492 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002493 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002494 */
Andrew Boiee8734462017-09-29 16:42:07 -07002495__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002496
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002497/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002498 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002500 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002501 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002502 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002503 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002504 * @param name Name of the stack.
2505 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002506 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002507 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002508#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002509 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002510 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002511 struct k_stack name \
2512 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002513 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002514 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002515
Anas Nashif166f5192018-02-25 08:02:36 -06002516/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002517
Allan Stephens6bba9b02016-11-16 14:56:54 -05002518struct k_work;
2519
Allan Stephensc98da842016-11-11 15:45:03 -05002520/**
2521 * @defgroup workqueue_apis Workqueue Thread APIs
2522 * @ingroup kernel_apis
2523 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002524 */
2525
Allan Stephens6bba9b02016-11-16 14:56:54 -05002526/**
2527 * @typedef k_work_handler_t
2528 * @brief Work item handler function type.
2529 *
2530 * A work item's handler function is executed by a workqueue's thread
2531 * when the work item is processed by the workqueue.
2532 *
2533 * @param work Address of the work item.
2534 *
2535 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002536 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002537 */
2538typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002539
2540/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002541 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002542 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002543
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002544struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002545 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002546 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002547};
2548
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002549enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002550 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002551};
2552
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002553struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002554 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002555 k_work_handler_t handler;
2556 atomic_t flags[1];
2557};
2558
Allan Stephens6bba9b02016-11-16 14:56:54 -05002559struct k_delayed_work {
2560 struct k_work work;
2561 struct _timeout timeout;
2562 struct k_work_q *work_q;
2563};
2564
2565extern struct k_work_q k_sys_work_q;
2566
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002567/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002568 * INTERNAL_HIDDEN @endcond
2569 */
2570
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002571#define _K_WORK_INITIALIZER(work_handler) \
2572 { \
2573 ._reserved = NULL, \
2574 .handler = work_handler, \
2575 .flags = { 0 } \
2576 }
2577
2578#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2579
Allan Stephens6bba9b02016-11-16 14:56:54 -05002580/**
2581 * @brief Initialize a statically-defined work item.
2582 *
2583 * This macro can be used to initialize a statically-defined workqueue work
2584 * item, prior to its first use. For example,
2585 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002586 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002587 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002588 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002589 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002590 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002591 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002592#define K_WORK_DEFINE(work, work_handler) \
2593 struct k_work work \
2594 __in_section(_k_work, static, work) = \
2595 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002596
2597/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002598 * @brief Initialize a work item.
2599 *
2600 * This routine initializes a workqueue work item, prior to its first use.
2601 *
2602 * @param work Address of work item.
2603 * @param handler Function to invoke each time work item is processed.
2604 *
2605 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002606 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002607 */
2608static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2609{
Leandro Pereira0e23ad82018-05-29 10:42:17 -07002610 *work = (struct k_work)_K_WORK_INITIALIZER(handler);
Andrew Boie945af952017-08-22 13:15:23 -07002611 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002612}
2613
2614/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002615 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002616 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002617 * This routine submits work item @a work to be processed by workqueue
2618 * @a work_q. If the work item is already pending in the workqueue's queue
2619 * as a result of an earlier submission, this routine has no effect on the
2620 * work item. If the work item has already been processed, or is currently
2621 * being processed, its work is considered complete and the work item can be
2622 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002623 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002624 * @warning
2625 * A submitted work item must not be modified until it has been processed
2626 * by the workqueue.
2627 *
2628 * @note Can be called by ISRs.
2629 *
2630 * @param work_q Address of workqueue.
2631 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002632 *
2633 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002634 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002635 */
2636static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2637 struct k_work *work)
2638{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002639 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002640 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002641 }
2642}
2643
2644/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002645 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002646 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002647 * This routine indicates if work item @a work is pending in a workqueue's
2648 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002649 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002650 * @note Can be called by ISRs.
2651 *
2652 * @param work Address of work item.
2653 *
2654 * @return 1 if work item is pending, or 0 if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002655 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002656 */
2657static inline int k_work_pending(struct k_work *work)
2658{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002659 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002660}
2661
2662/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002663 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002664 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002665 * This routine starts workqueue @a work_q. The workqueue spawns its work
2666 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002667 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002668 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002669 * @param stack Pointer to work queue thread's stack space, as defined by
2670 * K_THREAD_STACK_DEFINE()
2671 * @param stack_size Size of the work queue thread's stack (in bytes), which
2672 * should either be the same constant passed to
2673 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002674 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002675 *
2676 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002677 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002678 */
Andrew Boie507852a2017-07-25 18:47:07 -07002679extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002680 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002681 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002682
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002683/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002684 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002685 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002686 * This routine initializes a workqueue delayed work item, prior to
2687 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002688 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002689 * @param work Address of delayed work item.
2690 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002691 *
2692 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002693 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002694 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002695extern void k_delayed_work_init(struct k_delayed_work *work,
2696 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002697
2698/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002699 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002700 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002701 * This routine schedules work item @a work to be processed by workqueue
2702 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002703 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002704 * Only when the countdown completes is the work item actually submitted to
2705 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002706 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002707 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002708 * counting down cancels the existing submission and restarts the
2709 * countdown using the new delay. Note that this behavior is
2710 * inherently subject to race conditions with the pre-existing
2711 * timeouts and work queue, so care must be taken to synchronize such
2712 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002713 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002714 * @warning
2715 * A delayed work item must not be modified until it has been processed
2716 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002717 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002718 * @note Can be called by ISRs.
2719 *
2720 * @param work_q Address of workqueue.
2721 * @param work Address of delayed work item.
2722 * @param delay Delay before submitting the work item (in milliseconds).
2723 *
2724 * @retval 0 Work item countdown started.
2725 * @retval -EINPROGRESS Work item is already pending.
2726 * @retval -EINVAL Work item is being processed or has completed its work.
2727 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002728 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002729 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002730extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2731 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002732 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002733
2734/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002735 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002736 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002737 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002738 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002739 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002740 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002741 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002742 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002743 * @param work Address of delayed work item.
2744 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002745 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002746 * @retval -EINPROGRESS Work item is already pending.
2747 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002748 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002749 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002750extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002751
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002752/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002753 * @brief Submit a work item to the system workqueue.
2754 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002755 * This routine submits work item @a work to be processed by the system
2756 * workqueue. If the work item is already pending in the workqueue's queue
2757 * as a result of an earlier submission, this routine has no effect on the
2758 * work item. If the work item has already been processed, or is currently
2759 * being processed, its work is considered complete and the work item can be
2760 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002761 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002762 * @warning
2763 * Work items submitted to the system workqueue should avoid using handlers
2764 * that block or yield since this may prevent the system workqueue from
2765 * processing other work items in a timely manner.
2766 *
2767 * @note Can be called by ISRs.
2768 *
2769 * @param work Address of work item.
2770 *
2771 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002772 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002773 */
2774static inline void k_work_submit(struct k_work *work)
2775{
2776 k_work_submit_to_queue(&k_sys_work_q, work);
2777}
2778
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002779/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002780 * @brief Submit a delayed work item to the system workqueue.
2781 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002782 * This routine schedules work item @a work to be processed by the system
2783 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002784 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002785 * Only when the countdown completes is the work item actually submitted to
2786 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002787 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002788 * Submitting a previously submitted delayed work item that is still
2789 * counting down cancels the existing submission and restarts the countdown
2790 * using the new delay. If the work item is currently pending on the
2791 * workqueue's queue because the countdown has completed it is too late to
2792 * resubmit the item, and resubmission fails without impacting the work item.
2793 * If the work item has already been processed, or is currently being processed,
2794 * its work is considered complete and the work item can be resubmitted.
2795 *
2796 * @warning
2797 * Work items submitted to the system workqueue should avoid using handlers
2798 * that block or yield since this may prevent the system workqueue from
2799 * processing other work items in a timely manner.
2800 *
2801 * @note Can be called by ISRs.
2802 *
2803 * @param work Address of delayed work item.
2804 * @param delay Delay before submitting the work item (in milliseconds).
2805 *
2806 * @retval 0 Work item countdown started.
2807 * @retval -EINPROGRESS Work item is already pending.
2808 * @retval -EINVAL Work item is being processed or has completed its work.
2809 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002810 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002811 */
2812static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002813 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002814{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002815 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002816}
2817
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002818/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002819 * @brief Get time remaining before a delayed work gets scheduled.
2820 *
2821 * This routine computes the (approximate) time remaining before a
2822 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002823 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002824 *
2825 * @param work Delayed work item.
2826 *
2827 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002828 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002829 */
Kumar Galacc334c72017-04-21 10:55:34 -05002830static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002831{
2832 return _timeout_remaining_get(&work->timeout);
2833}
2834
Anas Nashif166f5192018-02-25 08:02:36 -06002835/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002836/**
Anas Nashifce78d162018-05-24 12:43:11 -05002837 * @defgroup mutex_apis Mutex APIs
2838 * @ingroup kernel_apis
2839 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002840 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002841
Anas Nashifce78d162018-05-24 12:43:11 -05002842/**
2843 * Mutex Structure
2844 * @ingroup mutex_apis
2845 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002846struct k_mutex {
2847 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002848 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002849 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002850 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002851 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002852
Anas Nashif2f203c22016-12-18 06:57:45 -05002853 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002854};
2855
Anas Nashifce78d162018-05-24 12:43:11 -05002856/**
2857 * @cond INTERNAL_HIDDEN
2858 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002859#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002860 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002861 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002862 .owner = NULL, \
2863 .lock_count = 0, \
2864 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002865 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002866 }
2867
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002868#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2869
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002870/**
Allan Stephensc98da842016-11-11 15:45:03 -05002871 * INTERNAL_HIDDEN @endcond
2872 */
2873
2874/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002875 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002877 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002878 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002879 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002881 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002882 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002883 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002884#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002885 struct k_mutex name \
2886 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002887 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002888
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002889/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002890 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002892 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002893 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002894 * Upon completion, the mutex is available and does not have an owner.
2895 *
2896 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002897 *
2898 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002899 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002900 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002901__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002902
2903/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002904 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002905 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002906 * This routine locks @a mutex. If the mutex is locked by another thread,
2907 * the calling thread waits until the mutex becomes available or until
2908 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002909 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002910 * A thread is permitted to lock a mutex it has already locked. The operation
2911 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002912 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002913 * @param mutex Address of the mutex.
2914 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002915 * or one of the special values K_NO_WAIT and K_FOREVER.
2916 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002917 * @retval 0 Mutex locked.
2918 * @retval -EBUSY Returned without waiting.
2919 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002920 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002921 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002922__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002923
2924/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002925 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002927 * This routine unlocks @a mutex. The mutex must already be locked by the
2928 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002929 *
2930 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002931 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002932 * thread.
2933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002934 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002935 *
2936 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002937 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002938 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002939__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002940
Allan Stephensc98da842016-11-11 15:45:03 -05002941/**
Anas Nashif166f5192018-02-25 08:02:36 -06002942 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002943 */
2944
2945/**
2946 * @cond INTERNAL_HIDDEN
2947 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002948
2949struct k_sem {
2950 _wait_q_t wait_q;
2951 unsigned int count;
2952 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002953 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002954
Anas Nashif2f203c22016-12-18 06:57:45 -05002955 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002956};
2957
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002958#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002959 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002960 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002961 .count = initial_count, \
2962 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002963 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002964 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002965 }
2966
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002967#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2968
Allan Stephensc98da842016-11-11 15:45:03 -05002969/**
2970 * INTERNAL_HIDDEN @endcond
2971 */
2972
2973/**
2974 * @defgroup semaphore_apis Semaphore APIs
2975 * @ingroup kernel_apis
2976 * @{
2977 */
2978
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002979/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002980 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002981 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002982 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002983 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002984 * @param sem Address of the semaphore.
2985 * @param initial_count Initial semaphore count.
2986 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002987 *
2988 * @return N/A
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 void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2992 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002993
2994/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002995 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002996 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002997 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002999 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3000 *
3001 * @param sem Address of the semaphore.
3002 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003003 * or one of the special values K_NO_WAIT and K_FOREVER.
3004 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05003005 * @note When porting code from the nanokernel legacy API to the new API, be
3006 * careful with the return value of this function. The return value is the
3007 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
3008 * non-zero means failure, while the nano_sem_take family returns 1 for success
3009 * and 0 for failure.
3010 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003011 * @retval 0 Semaphore taken.
3012 * @retval -EBUSY Returned without waiting.
3013 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003014 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003015 */
Andrew Boie99280232017-09-29 14:17:47 -07003016__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003017
3018/**
3019 * @brief Give a semaphore.
3020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003021 * This routine gives @a sem, unless the semaphore is already at its maximum
3022 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003023 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003024 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003025 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003026 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003027 *
3028 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003029 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003030 */
Andrew Boie99280232017-09-29 14:17:47 -07003031__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003032
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003033/**
3034 * @brief Reset a semaphore's count to zero.
3035 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003036 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003038 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003039 *
3040 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003041 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003042 */
Andrew Boie990bf162017-10-03 12:36:49 -07003043__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003044
Anas Nashif954d5502018-02-25 08:37:28 -06003045/**
3046 * @internal
3047 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003048static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003049{
3050 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003051}
3052
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003053/**
3054 * @brief Get a semaphore's count.
3055 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003056 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003058 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003059 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003060 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003061 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003062 */
Andrew Boie990bf162017-10-03 12:36:49 -07003063__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003064
Anas Nashif954d5502018-02-25 08:37:28 -06003065/**
3066 * @internal
3067 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003068static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003069{
3070 return sem->count;
3071}
3072
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003073/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003074 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003076 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003077 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003078 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003079 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003080 * @param name Name of the semaphore.
3081 * @param initial_count Initial semaphore count.
3082 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003083 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003084 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003085#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003086 struct k_sem name \
3087 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003088 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303089 BUILD_ASSERT(((count_limit) != 0) && \
3090 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003091
Anas Nashif166f5192018-02-25 08:02:36 -06003092/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003093
3094/**
3095 * @defgroup alert_apis Alert APIs
3096 * @ingroup kernel_apis
3097 * @{
3098 */
3099
Allan Stephens5eceb852016-11-16 10:16:30 -05003100/**
3101 * @typedef k_alert_handler_t
3102 * @brief Alert handler function type.
3103 *
3104 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07003105 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05003106 * and is only invoked if the alert has been initialized with one.
3107 *
3108 * @param alert Address of the alert.
3109 *
3110 * @return 0 if alert has been consumed; non-zero if alert should pend.
3111 */
3112typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05003113
Anas Nashif166f5192018-02-25 08:02:36 -06003114/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003115
3116/**
3117 * @cond INTERNAL_HIDDEN
3118 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003119
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003120#define K_ALERT_DEFAULT NULL
3121#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003122
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003123struct k_alert {
3124 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003125 atomic_t send_count;
3126 struct k_work work_item;
3127 struct k_sem sem;
3128
Anas Nashif2f203c22016-12-18 06:57:45 -05003129 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003130};
3131
Anas Nashif954d5502018-02-25 08:37:28 -06003132/**
3133 * @internal
3134 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003135extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003136
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003137#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003138 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003139 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003140 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003141 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
3142 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003143 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003144 }
3145
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003146#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
3147
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003148/**
Allan Stephensc98da842016-11-11 15:45:03 -05003149 * INTERNAL_HIDDEN @endcond
3150 */
3151
3152/**
3153 * @addtogroup alert_apis
3154 * @{
3155 */
3156
3157/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003158 * @def K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts)
3159 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003160 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003161 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003162 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003163 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003164 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003165 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003166 * @param name Name of the alert.
3167 * @param alert_handler Action to take when alert is sent. Specify either
3168 * the address of a function to be invoked by the system workqueue
3169 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
3170 * K_ALERT_DEFAULT (which causes the alert to pend).
3171 * @param max_num_pending_alerts Maximum number of pending alerts.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003172 *
3173 * @req K-ALERT-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003174 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003175#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003176 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003177 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003178 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003179 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003180
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003181/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003182 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003183 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003184 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003185 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003186 * @param alert Address of the alert.
3187 * @param handler Action to take when alert is sent. Specify either the address
3188 * of a function to be invoked by the system workqueue thread,
3189 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3190 * K_ALERT_DEFAULT (which causes the alert to pend).
3191 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003192 *
3193 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003194 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003195 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003196extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3197 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003198
3199/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003200 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003201 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003202 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003203 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003204 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3205 *
3206 * @param alert Address of the alert.
3207 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003208 * or one of the special values K_NO_WAIT and K_FOREVER.
3209 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003210 * @retval 0 Alert received.
3211 * @retval -EBUSY Returned without waiting.
3212 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003213 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003214 */
Andrew Boie310e9872017-09-29 04:41:15 -07003215__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003216
3217/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003218 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003219 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003220 * This routine signals @a alert. The action specified for @a alert will
3221 * be taken, which may trigger the execution of an alert handler function
3222 * and/or cause the alert to pend (assuming the alert has not reached its
3223 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003224 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003225 * @note Can be called by ISRs.
3226 *
3227 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003228 *
3229 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003230 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003231 */
Andrew Boie310e9872017-09-29 04:41:15 -07003232__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003233
3234/**
Anas Nashif166f5192018-02-25 08:02:36 -06003235 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003236 */
3237
Allan Stephensc98da842016-11-11 15:45:03 -05003238/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003239 * @defgroup msgq_apis Message Queue APIs
3240 * @ingroup kernel_apis
3241 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003242 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003243
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003244/**
3245 * @brief Message Queue Structure
3246 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003247struct k_msgq {
3248 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003249 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003250 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003251 char *buffer_start;
3252 char *buffer_end;
3253 char *read_ptr;
3254 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003255 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003256
Anas Nashif2f203c22016-12-18 06:57:45 -05003257 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003258 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003259};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003260/**
3261 * @cond INTERNAL_HIDDEN
3262 */
3263
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003264
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003265#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003266 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003267 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003268 .max_msgs = q_max_msgs, \
3269 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003270 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003271 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003272 .read_ptr = q_buffer, \
3273 .write_ptr = q_buffer, \
3274 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003275 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003276 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003277#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003278/**
3279 * INTERNAL_HIDDEN @endcond
3280 */
3281
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003282
Andrew Boie0fe789f2018-04-12 18:35:56 -07003283#define K_MSGQ_FLAG_ALLOC BIT(0)
3284
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003285/**
3286 * @brief Message Queue Attributes
3287 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303288struct k_msgq_attrs {
3289 size_t msg_size;
3290 u32_t max_msgs;
3291 u32_t used_msgs;
3292};
3293
Allan Stephensc98da842016-11-11 15:45:03 -05003294
3295/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003296 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003297 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003298 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3299 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003300 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3301 * message is similarly aligned to this boundary, @a q_msg_size must also be
3302 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003303 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003304 * The message queue can be accessed outside the module where it is defined
3305 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003306 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003307 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003308 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003309 * @param q_name Name of the message queue.
3310 * @param q_msg_size Message size (in bytes).
3311 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003312 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003313 *
3314 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003315 */
3316#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003317 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003318 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003319 struct k_msgq q_name \
3320 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003321 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003322 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003323
Peter Mitsisd7a37502016-10-13 11:37:40 -04003324/**
3325 * @brief Initialize a message queue.
3326 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003327 * This routine initializes a message queue object, prior to its first use.
3328 *
Allan Stephensda827222016-11-09 14:23:58 -06003329 * The message queue's ring buffer must contain space for @a max_msgs messages,
3330 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3331 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3332 * that each message is similarly aligned to this boundary, @a q_msg_size
3333 * must also be a multiple of N.
3334 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003335 * @param q Address of the message queue.
3336 * @param buffer Pointer to ring buffer that holds queued messages.
3337 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003338 * @param max_msgs Maximum number of messages that can be queued.
3339 *
3340 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003341 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003342 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003343void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3344 u32_t max_msgs);
3345
3346/**
3347 * @brief Initialize a message queue.
3348 *
3349 * This routine initializes a message queue object, prior to its first use,
3350 * allocating its internal ring buffer from the calling thread's resource
3351 * pool.
3352 *
3353 * Memory allocated for the ring buffer can be released by calling
3354 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3355 * all of its references.
3356 *
3357 * @param q Address of the message queue.
3358 * @param msg_size Message size (in bytes).
3359 * @param max_msgs Maximum number of messages that can be queued.
3360 *
3361 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3362 * thread's resource pool, or -EINVAL if the size parameters cause
3363 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003364 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003365 */
3366__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3367 u32_t max_msgs);
3368
3369
3370void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003371
3372/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003373 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003374 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003375 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003376 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003377 * @note Can be called by ISRs.
3378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003379 * @param q Address of the message queue.
3380 * @param data Pointer to the message.
3381 * @param timeout Waiting period to add the message (in milliseconds),
3382 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003383 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003384 * @retval 0 Message sent.
3385 * @retval -ENOMSG Returned without waiting or queue purged.
3386 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003387 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003388 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003389__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003390
3391/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003392 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003393 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003394 * This routine receives a message from message queue @a q in a "first in,
3395 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003396 *
Allan Stephensc98da842016-11-11 15:45:03 -05003397 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003398 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003399 * @param q Address of the message queue.
3400 * @param data Address of area to hold the received message.
3401 * @param timeout Waiting period to receive the message (in milliseconds),
3402 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003403 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003404 * @retval 0 Message received.
3405 * @retval -ENOMSG Returned without waiting.
3406 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003407 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003408 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003409__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003410
3411/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003412 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003413 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003414 * This routine discards all unreceived messages in a message queue's ring
3415 * buffer. Any threads that are blocked waiting to send a message to the
3416 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003418 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003419 *
3420 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003421 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003422 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003423__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003424
Peter Mitsis67be2492016-10-07 11:44:34 -04003425/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003426 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003427 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003428 * This routine returns the number of unused entries in a message queue's
3429 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003431 * @param q Address of the message queue.
3432 *
3433 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003434 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003435 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003436__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3437
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303438/**
3439 * @brief Get basic attributes of a message queue.
3440 *
3441 * This routine fetches basic attributes of message queue into attr argument.
3442 *
3443 * @param q Address of the message queue.
3444 * @param attrs pointer to message queue attribute structure.
3445 *
3446 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003447 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303448 */
3449__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3450
3451
Andrew Boie82edb6e2017-10-02 10:53:06 -07003452static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003453{
3454 return q->max_msgs - q->used_msgs;
3455}
3456
Peter Mitsisd7a37502016-10-13 11:37:40 -04003457/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003458 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003459 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003460 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003461 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003462 * @param q Address of the message queue.
3463 *
3464 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003465 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003466 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003467__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3468
3469static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003470{
3471 return q->used_msgs;
3472}
3473
Anas Nashif166f5192018-02-25 08:02:36 -06003474/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003475
3476/**
3477 * @defgroup mem_pool_apis Memory Pool APIs
3478 * @ingroup kernel_apis
3479 * @{
3480 */
3481
Andy Ross73cb9582017-05-09 10:42:39 -07003482/* Note on sizing: the use of a 20 bit field for block means that,
3483 * assuming a reasonable minimum block size of 16 bytes, we're limited
3484 * to 16M of memory managed by a single pool. Long term it would be
3485 * good to move to a variable bit size based on configuration.
3486 */
3487struct k_mem_block_id {
3488 u32_t pool : 8;
3489 u32_t level : 4;
3490 u32_t block : 20;
3491};
3492
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003493struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003494 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003495 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003496};
3497
Anas Nashif166f5192018-02-25 08:02:36 -06003498/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003499
3500/**
3501 * @defgroup mailbox_apis Mailbox APIs
3502 * @ingroup kernel_apis
3503 * @{
3504 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003505
3506struct k_mbox_msg {
3507 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003508 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003509 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003510 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003511 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003512 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003513 /** sender's message data buffer */
3514 void *tx_data;
3515 /** internal use only - needed for legacy API support */
3516 void *_rx_data;
3517 /** message data block descriptor */
3518 struct k_mem_block tx_block;
3519 /** source thread id */
3520 k_tid_t rx_source_thread;
3521 /** target thread id */
3522 k_tid_t tx_target_thread;
3523 /** internal use only - thread waiting on send (may be a dummy) */
3524 k_tid_t _syncing_thread;
3525#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3526 /** internal use only - semaphore used during asynchronous send */
3527 struct k_sem *_async_sem;
3528#endif
3529};
3530
3531struct k_mbox {
3532 _wait_q_t tx_msg_queue;
3533 _wait_q_t rx_msg_queue;
3534
Anas Nashif2f203c22016-12-18 06:57:45 -05003535 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003536};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003537/**
3538 * @cond INTERNAL_HIDDEN
3539 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003540
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003541#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003542 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003543 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3544 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003545 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003546 }
3547
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003548#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3549
Peter Mitsis12092702016-10-14 12:57:23 -04003550/**
Allan Stephensc98da842016-11-11 15:45:03 -05003551 * INTERNAL_HIDDEN @endcond
3552 */
3553
3554/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003555 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003556 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003557 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003558 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003559 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003560 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003561 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003562 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003563 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003564#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003565 struct k_mbox name \
3566 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003567 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003568
Peter Mitsis12092702016-10-14 12:57:23 -04003569/**
3570 * @brief Initialize a mailbox.
3571 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003572 * This routine initializes a mailbox object, prior to its first use.
3573 *
3574 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003575 *
3576 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003577 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003578 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003579extern void k_mbox_init(struct k_mbox *mbox);
3580
Peter Mitsis12092702016-10-14 12:57:23 -04003581/**
3582 * @brief Send a mailbox message in a synchronous manner.
3583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003584 * This routine sends a message to @a mbox and waits for a receiver to both
3585 * receive and process it. The message data may be in a buffer, in a memory
3586 * pool block, or non-existent (i.e. an empty message).
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 timeout Waiting period for the message to be received (in
3591 * milliseconds), or one of the special values K_NO_WAIT
3592 * and K_FOREVER. Once the message has been received,
3593 * this routine waits as long as necessary for the message
3594 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003595 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003596 * @retval 0 Message sent.
3597 * @retval -ENOMSG Returned without waiting.
3598 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003599 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003600 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003601extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003602 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003603
Peter Mitsis12092702016-10-14 12:57:23 -04003604/**
3605 * @brief Send a mailbox message in an asynchronous manner.
3606 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003607 * This routine sends a message to @a mbox without waiting for a receiver
3608 * to process it. The message data may be in a buffer, in a memory pool block,
3609 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3610 * will be given when the message has been both received and completely
3611 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003612 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003613 * @param mbox Address of the mailbox.
3614 * @param tx_msg Address of the transmit message descriptor.
3615 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003616 *
3617 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003618 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003619 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003620extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003621 struct k_sem *sem);
3622
Peter Mitsis12092702016-10-14 12:57:23 -04003623/**
3624 * @brief Receive a mailbox message.
3625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003626 * This routine receives a message from @a mbox, then optionally retrieves
3627 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003628 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003629 * @param mbox Address of the mailbox.
3630 * @param rx_msg Address of the receive message descriptor.
3631 * @param buffer Address of the buffer to receive data, or NULL to defer data
3632 * retrieval and message disposal until later.
3633 * @param timeout Waiting period for a message to be received (in
3634 * milliseconds), or one of the special values K_NO_WAIT
3635 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003636 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003637 * @retval 0 Message received.
3638 * @retval -ENOMSG Returned without waiting.
3639 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003640 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003641 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003642extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003643 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003644
3645/**
3646 * @brief Retrieve mailbox message data into a buffer.
3647 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003648 * This routine completes the processing of a received message by retrieving
3649 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003650 *
3651 * Alternatively, this routine can be used to dispose of a received message
3652 * without retrieving its data.
3653 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003654 * @param rx_msg Address of the receive message descriptor.
3655 * @param buffer Address of the buffer to receive data, or NULL to discard
3656 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003657 *
3658 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003659 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003660 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003661extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003662
3663/**
3664 * @brief Retrieve mailbox message data into a memory pool block.
3665 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003666 * This routine completes the processing of a received message by retrieving
3667 * its data into a memory pool block, then disposing of the message.
3668 * The memory pool block that results from successful retrieval must be
3669 * returned to the pool once the data has been processed, even in cases
3670 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003671 *
3672 * Alternatively, this routine can be used to dispose of a received message
3673 * without retrieving its data. In this case there is no need to return a
3674 * memory pool block to the pool.
3675 *
3676 * This routine allocates a new memory pool block for the data only if the
3677 * data is not already in one. If a new block cannot be allocated, the routine
3678 * returns a failure code and the received message is left unchanged. This
3679 * permits the caller to reattempt data retrieval at a later time or to dispose
3680 * of the received message without retrieving its data.
3681 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003682 * @param rx_msg Address of a receive message descriptor.
3683 * @param pool Address of memory pool, or NULL to discard data.
3684 * @param block Address of the area to hold memory pool block info.
3685 * @param timeout Waiting period to wait for a memory pool block (in
3686 * milliseconds), or one of the special values K_NO_WAIT
3687 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003688 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003689 * @retval 0 Data retrieved.
3690 * @retval -ENOMEM Returned without waiting.
3691 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003692 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003693 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003694extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003695 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003696 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003697
Anas Nashif166f5192018-02-25 08:02:36 -06003698/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003699
3700/**
Anas Nashifce78d162018-05-24 12:43:11 -05003701 * @defgroup pipe_apis Pipe APIs
3702 * @ingroup kernel_apis
3703 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003704 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003705
Anas Nashifce78d162018-05-24 12:43:11 -05003706/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003707struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003708 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3709 size_t size; /**< Buffer size */
3710 size_t bytes_used; /**< # bytes used in buffer */
3711 size_t read_index; /**< Where in buffer to read from */
3712 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003713
3714 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003715 _wait_q_t readers; /**< Reader wait queue */
3716 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003717 } wait_q;
3718
Anas Nashif2f203c22016-12-18 06:57:45 -05003719 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Anas Nashifce78d162018-05-24 12:43:11 -05003720 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003721};
3722
Anas Nashifce78d162018-05-24 12:43:11 -05003723/**
3724 * @cond INTERNAL_HIDDEN
3725 */
3726#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3727
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003728#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003729 { \
3730 .buffer = pipe_buffer, \
3731 .size = pipe_buffer_size, \
3732 .bytes_used = 0, \
3733 .read_index = 0, \
3734 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003735 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3736 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003737 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003738 }
3739
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003740#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3741
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003742/**
Allan Stephensc98da842016-11-11 15:45:03 -05003743 * INTERNAL_HIDDEN @endcond
3744 */
3745
3746/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003747 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003748 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003749 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003750 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003751 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003752 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003753 * @param name Name of the pipe.
3754 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3755 * or zero if no ring buffer is used.
3756 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003757 *
3758 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003759 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003760#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3761 static unsigned char __kernel_noinit __aligned(pipe_align) \
3762 _k_pipe_buf_##name[pipe_buffer_size]; \
3763 struct k_pipe name \
3764 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003765 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003766
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003767/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003768 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003769 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003770 * This routine initializes a pipe object, prior to its first use.
3771 *
3772 * @param pipe Address of the pipe.
3773 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3774 * is used.
3775 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3776 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003777 *
3778 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003779 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003780 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003781void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3782
3783/**
3784 * @brief Release a pipe's allocated buffer
3785 *
3786 * If a pipe object was given a dynamically allocated buffer via
3787 * k_pipe_alloc_init(), this will free it. This function does nothing
3788 * if the buffer wasn't dynamically allocated.
3789 *
3790 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003791 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003792 */
3793void k_pipe_cleanup(struct k_pipe *pipe);
3794
3795/**
3796 * @brief Initialize a pipe and allocate a buffer for it
3797 *
3798 * Storage for the buffer region will be allocated from the calling thread's
3799 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3800 * or userspace is enabled and the pipe object loses all references to it.
3801 *
3802 * This function should only be called on uninitialized pipe objects.
3803 *
3804 * @param pipe Address of the pipe.
3805 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3806 * buffer is used.
3807 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003808 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003809 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003810 */
3811__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003812
3813/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003814 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003816 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003817 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003818 * @param pipe Address of the pipe.
3819 * @param data Address of data to write.
3820 * @param bytes_to_write Size of data (in bytes).
3821 * @param bytes_written Address of area to hold the number of bytes written.
3822 * @param min_xfer Minimum number of bytes to write.
3823 * @param timeout Waiting period to wait for the data to be written (in
3824 * milliseconds), or one of the special values K_NO_WAIT
3825 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003826 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003827 * @retval 0 At least @a min_xfer bytes of data were written.
3828 * @retval -EIO Returned without waiting; zero data bytes were written.
3829 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003830 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003831 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003832 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003833__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3834 size_t bytes_to_write, size_t *bytes_written,
3835 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003836
3837/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003838 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003840 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003842 * @param pipe Address of the pipe.
3843 * @param data Address to place the data read from pipe.
3844 * @param bytes_to_read Maximum number of data bytes to read.
3845 * @param bytes_read Address of area to hold the number of bytes read.
3846 * @param min_xfer Minimum number of data bytes to read.
3847 * @param timeout Waiting period to wait for the data to be read (in
3848 * milliseconds), or one of the special values K_NO_WAIT
3849 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003850 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003851 * @retval 0 At least @a min_xfer bytes of data were read.
3852 * @retval -EIO Returned without waiting; zero data bytes were read.
3853 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003854 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003855 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003856 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003857__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3858 size_t bytes_to_read, size_t *bytes_read,
3859 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003860
3861/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003862 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003864 * This routine writes the data contained in a memory block to @a pipe.
3865 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003866 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003868 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003869 * @param block Memory block containing data to send
3870 * @param size Number of data bytes in memory block to send
3871 * @param sem Semaphore to signal upon completion (else NULL)
3872 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003873 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003874 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003875 */
3876extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3877 size_t size, struct k_sem *sem);
3878
Anas Nashif166f5192018-02-25 08:02:36 -06003879/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003880
Allan Stephensc98da842016-11-11 15:45:03 -05003881/**
3882 * @cond INTERNAL_HIDDEN
3883 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003884
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003885struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003886 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003887 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003888 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003889 char *buffer;
3890 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003891 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003892
Anas Nashif2f203c22016-12-18 06:57:45 -05003893 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003894};
3895
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003896#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003897 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003898 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003899 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003900 .num_blocks = slab_num_blocks, \
3901 .block_size = slab_block_size, \
3902 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003903 .free_list = NULL, \
3904 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003905 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003906 }
3907
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003908#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3909
3910
Peter Mitsis578f9112016-10-07 13:50:31 -04003911/**
Allan Stephensc98da842016-11-11 15:45:03 -05003912 * INTERNAL_HIDDEN @endcond
3913 */
3914
3915/**
3916 * @defgroup mem_slab_apis Memory Slab APIs
3917 * @ingroup kernel_apis
3918 * @{
3919 */
3920
3921/**
Allan Stephensda827222016-11-09 14:23:58 -06003922 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003923 *
Allan Stephensda827222016-11-09 14:23:58 -06003924 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003925 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003926 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3927 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003928 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003929 *
Allan Stephensda827222016-11-09 14:23:58 -06003930 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003931 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003932 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003933 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003935 * @param name Name of the memory slab.
3936 * @param slab_block_size Size of each memory block (in bytes).
3937 * @param slab_num_blocks Number memory blocks.
3938 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003939 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003940 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003941#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3942 char __noinit __aligned(slab_align) \
3943 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3944 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003945 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003946 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003947 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003948
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003949/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003950 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003952 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003953 *
Allan Stephensda827222016-11-09 14:23:58 -06003954 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3955 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3956 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3957 * To ensure that each memory block is similarly aligned to this boundary,
3958 * @a slab_block_size must also be a multiple of N.
3959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003960 * @param slab Address of the memory slab.
3961 * @param buffer Pointer to buffer used for the memory blocks.
3962 * @param block_size Size of each memory block (in bytes).
3963 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003964 *
3965 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003966 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003967 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003968extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003969 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003970
3971/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003972 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003974 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003975 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003976 * @param slab Address of the memory slab.
3977 * @param mem Pointer to block address area.
3978 * @param timeout Maximum time to wait for operation to complete
3979 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3980 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003981 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003982 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003983 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003984 * @retval -ENOMEM Returned without waiting.
3985 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003986 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003987 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003988extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003989 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003990
3991/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003992 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003994 * This routine releases a previously allocated memory block back to its
3995 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003996 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003997 * @param slab Address of the memory slab.
3998 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003999 *
4000 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004001 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004002 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004003extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004004
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004005/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004006 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004008 * This routine gets the number of memory blocks that are currently
4009 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004011 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004013 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004014 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004015 */
Kumar Galacc334c72017-04-21 10:55:34 -05004016static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004017{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004018 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004019}
4020
Peter Mitsisc001aa82016-10-13 13:53:37 -04004021/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004022 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004023 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004024 * This routine gets the number of memory blocks that are currently
4025 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004026 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004027 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004028 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004029 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004030 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004031 */
Kumar Galacc334c72017-04-21 10:55:34 -05004032static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004033{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004034 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004035}
4036
Anas Nashif166f5192018-02-25 08:02:36 -06004037/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004038
4039/**
4040 * @cond INTERNAL_HIDDEN
4041 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004042
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004043struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004044 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004045 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004046};
4047
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004048/**
Allan Stephensc98da842016-11-11 15:45:03 -05004049 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004050 */
4051
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004052/**
Allan Stephensc98da842016-11-11 15:45:03 -05004053 * @addtogroup mem_pool_apis
4054 * @{
4055 */
4056
4057/**
4058 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004059 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004060 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4061 * long. The memory pool allows blocks to be repeatedly partitioned into
4062 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004063 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004064 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004065 * If the pool is to be accessed outside the module where it is defined, it
4066 * can be declared via
4067 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004068 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004069 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004070 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004071 * @param minsz Size of the smallest blocks in the pool (in bytes).
4072 * @param maxsz Size of the largest blocks in the pool (in bytes).
4073 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004074 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004075 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004076 */
Andy Ross73cb9582017-05-09 10:42:39 -07004077#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4078 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4079 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08004080 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07004081 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004082 .base = { \
4083 .buf = _mpool_buf_##name, \
4084 .max_sz = maxsz, \
4085 .n_max = nmax, \
4086 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
4087 .levels = _mpool_lvls_##name, \
4088 .flags = SYS_MEM_POOL_KERNEL \
4089 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004090 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004091
Peter Mitsis937042c2016-10-13 13:18:26 -04004092/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004093 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004095 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004096 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004097 * @param pool Address of the memory pool.
4098 * @param block Pointer to block descriptor for the allocated memory.
4099 * @param size Amount of memory to allocate (in bytes).
4100 * @param timeout Maximum time to wait for operation to complete
4101 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4102 * or K_FOREVER to wait as long as necessary.
4103 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004104 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004105 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004106 * @retval -ENOMEM Returned without waiting.
4107 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004108 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004109 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004110extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004111 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004112
4113/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004114 * @brief Allocate memory from a memory pool with malloc() semantics
4115 *
4116 * Such memory must be released using k_free().
4117 *
4118 * @param pool Address of the memory pool.
4119 * @param size Amount of memory to allocate (in bytes).
4120 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004121 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004122 */
4123extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4124
4125/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004126 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004127 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004128 * This routine releases a previously allocated memory block back to its
4129 * memory pool.
4130 *
4131 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004132 *
4133 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004134 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004135 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004136extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004137
4138/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004139 * @brief Free memory allocated from a memory pool.
4140 *
4141 * This routine releases a previously allocated memory block back to its
4142 * memory pool
4143 *
4144 * @param id Memory block identifier.
4145 *
4146 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004147 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004148 */
4149extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4150
4151/**
Anas Nashif166f5192018-02-25 08:02:36 -06004152 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004153 */
4154
4155/**
4156 * @defgroup heap_apis Heap Memory Pool APIs
4157 * @ingroup kernel_apis
4158 * @{
4159 */
4160
4161/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004162 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004164 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004165 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004167 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004168 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004169 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004170 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004171 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004172extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004173
4174/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004175 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004176 *
4177 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004178 * returned must have been allocated from the heap memory pool or
4179 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004180 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004181 * If @a ptr is NULL, no operation is performed.
4182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004183 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004184 *
4185 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004186 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004187 */
4188extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004189
Allan Stephensc98da842016-11-11 15:45:03 -05004190/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004191 * @brief Allocate memory from heap, array style
4192 *
4193 * This routine provides traditional calloc() semantics. Memory is
4194 * allocated from the heap memory pool and zeroed.
4195 *
4196 * @param nmemb Number of elements in the requested array
4197 * @param size Size of each array element (in bytes).
4198 *
4199 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004200 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004201 */
4202extern void *k_calloc(size_t nmemb, size_t size);
4203
Anas Nashif166f5192018-02-25 08:02:36 -06004204/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004205
Benjamin Walshacc68c12017-01-29 18:57:45 -05004206/* polling API - PRIVATE */
4207
Benjamin Walshb0179862017-02-02 16:39:57 -05004208#ifdef CONFIG_POLL
4209#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
4210#else
4211#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
4212#endif
4213
Benjamin Walshacc68c12017-01-29 18:57:45 -05004214/* private - implementation data created as needed, per-type */
4215struct _poller {
4216 struct k_thread *thread;
Andy Ross55a7e462018-05-31 11:58:09 -07004217 volatile int is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004218};
4219
4220/* private - types bit positions */
4221enum _poll_types_bits {
4222 /* can be used to ignore an event */
4223 _POLL_TYPE_IGNORE,
4224
4225 /* to be signaled by k_poll_signal() */
4226 _POLL_TYPE_SIGNAL,
4227
4228 /* semaphore availability */
4229 _POLL_TYPE_SEM_AVAILABLE,
4230
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004231 /* queue/fifo/lifo data availability */
4232 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004233
4234 _POLL_NUM_TYPES
4235};
4236
4237#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4238
4239/* private - states bit positions */
4240enum _poll_states_bits {
4241 /* default state when creating event */
4242 _POLL_STATE_NOT_READY,
4243
Benjamin Walshacc68c12017-01-29 18:57:45 -05004244 /* signaled by k_poll_signal() */
4245 _POLL_STATE_SIGNALED,
4246
4247 /* semaphore is available */
4248 _POLL_STATE_SEM_AVAILABLE,
4249
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004250 /* data is available to read on queue/fifo/lifo */
4251 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004252
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004253 /* queue/fifo/lifo wait was cancelled */
4254 _POLL_STATE_CANCELLED,
4255
Benjamin Walshacc68c12017-01-29 18:57:45 -05004256 _POLL_NUM_STATES
4257};
4258
4259#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4260
4261#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004262 (32 - (0 \
4263 + 8 /* tag */ \
4264 + _POLL_NUM_TYPES \
4265 + _POLL_NUM_STATES \
4266 + 1 /* modes */ \
4267 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004268
Benjamin Walshacc68c12017-01-29 18:57:45 -05004269/* end of polling API - PRIVATE */
4270
4271
4272/**
4273 * @defgroup poll_apis Async polling APIs
4274 * @ingroup kernel_apis
4275 * @{
4276 */
4277
4278/* Public polling API */
4279
4280/* public - values for k_poll_event.type bitfield */
4281#define K_POLL_TYPE_IGNORE 0
4282#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4283#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004284#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4285#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004286
4287/* public - polling modes */
4288enum k_poll_modes {
4289 /* polling thread does not take ownership of objects when available */
4290 K_POLL_MODE_NOTIFY_ONLY = 0,
4291
4292 K_POLL_NUM_MODES
4293};
4294
4295/* public - values for k_poll_event.state bitfield */
4296#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004297#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4298#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004299#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4300#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004301#define K_POLL_STATE_CANCELLED _POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004302
4303/* public - poll signal object */
4304struct k_poll_signal {
4305 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004306 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004307
4308 /*
4309 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4310 * user resets it to 0.
4311 */
4312 unsigned int signaled;
4313
4314 /* custom result value passed to k_poll_signal() if needed */
4315 int result;
4316};
4317
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004318#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004319 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004320 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004321 .signaled = 0, \
4322 .result = 0, \
4323 }
4324
4325struct k_poll_event {
4326 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004327 sys_dnode_t _node;
4328
4329 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004330 struct _poller *poller;
4331
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004332 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004333 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004334
Benjamin Walshacc68c12017-01-29 18:57:45 -05004335 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004336 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004337
4338 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004339 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004340
4341 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004342 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004343
4344 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004345 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004346
4347 /* per-type data */
4348 union {
4349 void *obj;
4350 struct k_poll_signal *signal;
4351 struct k_sem *sem;
4352 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004353 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004354 };
4355};
4356
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004357#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004358 { \
4359 .poller = NULL, \
4360 .type = event_type, \
4361 .state = K_POLL_STATE_NOT_READY, \
4362 .mode = event_mode, \
4363 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004364 { .obj = event_obj }, \
4365 }
4366
4367#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4368 event_tag) \
4369 { \
4370 .type = event_type, \
4371 .tag = event_tag, \
4372 .state = K_POLL_STATE_NOT_READY, \
4373 .mode = event_mode, \
4374 .unused = 0, \
4375 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004376 }
4377
4378/**
4379 * @brief Initialize one struct k_poll_event instance
4380 *
4381 * After this routine is called on a poll event, the event it ready to be
4382 * placed in an event array to be passed to k_poll().
4383 *
4384 * @param event The event to initialize.
4385 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4386 * values. Only values that apply to the same object being polled
4387 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4388 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004389 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004390 * @param obj Kernel object or poll signal.
4391 *
4392 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004393 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004394 */
4395
Kumar Galacc334c72017-04-21 10:55:34 -05004396extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004397 int mode, void *obj);
4398
4399/**
4400 * @brief Wait for one or many of multiple poll events to occur
4401 *
4402 * This routine allows a thread to wait concurrently for one or many of
4403 * multiple poll events to have occurred. Such events can be a kernel object
4404 * being available, like a semaphore, or a poll signal event.
4405 *
4406 * When an event notifies that a kernel object is available, the kernel object
4407 * is not "given" to the thread calling k_poll(): it merely signals the fact
4408 * that the object was available when the k_poll() call was in effect. Also,
4409 * all threads trying to acquire an object the regular way, i.e. by pending on
4410 * the object, have precedence over the thread polling on the object. This
4411 * means that the polling thread will never get the poll event on an object
4412 * until the object becomes available and its pend queue is empty. For this
4413 * reason, the k_poll() call is more effective when the objects being polled
4414 * only have one thread, the polling thread, trying to acquire them.
4415 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004416 * When k_poll() returns 0, the caller should loop on all the events that were
4417 * passed to k_poll() and check the state field for the values that were
4418 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004419 *
4420 * Before being reused for another call to k_poll(), the user has to reset the
4421 * state field to K_POLL_STATE_NOT_READY.
4422 *
Andrew Boie3772f772018-05-07 16:52:57 -07004423 * When called from user mode, a temporary memory allocation is required from
4424 * the caller's resource pool.
4425 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004426 * @param events An array of pointers to events to be polled for.
4427 * @param num_events The number of events in the array.
4428 * @param timeout Waiting period for an event to be ready (in milliseconds),
4429 * or one of the special values K_NO_WAIT and K_FOREVER.
4430 *
4431 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004432 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004433 * @retval -EINTR Polling has been interrupted, e.g. with
4434 * k_queue_cancel_wait(). All output events are still set and valid,
4435 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4436 * words, -EINTR status means that at least one of output events is
4437 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004438 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4439 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004440 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004441 */
4442
Andrew Boie3772f772018-05-07 16:52:57 -07004443__syscall int k_poll(struct k_poll_event *events, int num_events,
4444 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004445
4446/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004447 * @brief Initialize a poll signal object.
4448 *
4449 * Ready a poll signal object to be signaled via k_poll_signal().
4450 *
4451 * @param signal A poll signal.
4452 *
4453 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004454 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004455 */
4456
Andrew Boie3772f772018-05-07 16:52:57 -07004457__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4458
4459/*
4460 * @brief Reset a poll signal object's state to unsignaled.
4461 *
4462 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004463 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004464 */
4465__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4466
4467static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4468{
4469 signal->signaled = 0;
4470}
4471
4472/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004473 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004474 *
4475 * @param signal A poll signal object
4476 * @param signaled An integer buffer which will be written nonzero if the
4477 * object was signaled
4478 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004479 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004480 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004481 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004482 */
4483__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4484 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004485
4486/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004487 * @brief Signal a poll signal object.
4488 *
4489 * This routine makes ready a poll signal, which is basically a poll event of
4490 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4491 * made ready to run. A @a result value can be specified.
4492 *
4493 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004494 * k_poll_signal(), stays set until the user sets it back to 0 with
4495 * k_poll_signal_reset(). It thus has to be reset by the user before being
4496 * passed again to k_poll() or k_poll() will consider it being signaled, and
4497 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004498 *
4499 * @param signal A poll signal.
4500 * @param result The value to store in the result field of the signal.
4501 *
4502 * @retval 0 The signal was delivered successfully.
4503 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004504 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004505 */
4506
Andrew Boie3772f772018-05-07 16:52:57 -07004507__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004508
Anas Nashif954d5502018-02-25 08:37:28 -06004509/**
4510 * @internal
4511 */
Andy Ross8606fab2018-03-26 10:54:40 -07004512extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004513
Anas Nashif166f5192018-02-25 08:02:36 -06004514/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004515
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004516/**
4517 * @brief Make the CPU idle.
4518 *
4519 * This function makes the CPU idle until an event wakes it up.
4520 *
4521 * In a regular system, the idle thread should be the only thread responsible
4522 * for making the CPU idle and triggering any type of power management.
4523 * However, in some more constrained systems, such as a single-threaded system,
4524 * the only thread would be responsible for this if needed.
4525 *
4526 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004527 * @req K-MISC-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004528 */
4529extern void k_cpu_idle(void);
4530
4531/**
4532 * @brief Make the CPU idle in an atomic fashion.
4533 *
4534 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4535 * must be done atomically before making the CPU idle.
4536 *
4537 * @param key Interrupt locking key obtained from irq_lock().
4538 *
4539 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004540 * @req K-MISC-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004541 */
4542extern void k_cpu_atomic_idle(unsigned int key);
4543
Anas Nashif954d5502018-02-25 08:37:28 -06004544
4545/**
4546 * @internal
4547 */
Kumar Galacc334c72017-04-21 10:55:34 -05004548extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004549
Andrew Boiecdb94d62017-04-18 15:22:05 -07004550#ifdef _ARCH_EXCEPT
4551/* This archtecture has direct support for triggering a CPU exception */
4552#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4553#else
4554
Andrew Boiecdb94d62017-04-18 15:22:05 -07004555/* NOTE: This is the implementation for arches that do not implement
4556 * _ARCH_EXCEPT() to generate a real CPU exception.
4557 *
4558 * We won't have a real exception frame to determine the PC value when
4559 * the oops occurred, so print file and line number before we jump into
4560 * the fatal error handler.
4561 */
4562#define _k_except_reason(reason) do { \
4563 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4564 _NanoFatalErrorHandler(reason, &_default_esf); \
4565 CODE_UNREACHABLE; \
4566 } while (0)
4567
4568#endif /* _ARCH__EXCEPT */
4569
4570/**
4571 * @brief Fatally terminate a thread
4572 *
4573 * This should be called when a thread has encountered an unrecoverable
4574 * runtime condition and needs to terminate. What this ultimately
4575 * means is determined by the _fatal_error_handler() implementation, which
4576 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4577 *
4578 * If this is called from ISR context, the default system fatal error handler
4579 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004580 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004581 */
4582#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4583
4584/**
4585 * @brief Fatally terminate the system
4586 *
4587 * This should be called when the Zephyr kernel has encountered an
4588 * unrecoverable runtime condition and needs to terminate. What this ultimately
4589 * means is determined by the _fatal_error_handler() implementation, which
4590 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004591 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004592 */
4593#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4594
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004595/*
4596 * private APIs that are utilized by one or more public APIs
4597 */
4598
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004599#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004600/**
4601 * @internal
4602 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004603extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004604#else
Anas Nashif954d5502018-02-25 08:37:28 -06004605/**
4606 * @internal
4607 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004608#define _init_static_threads() do { } while ((0))
4609#endif
4610
Anas Nashif954d5502018-02-25 08:37:28 -06004611/**
4612 * @internal
4613 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004614extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004615/**
4616 * @internal
4617 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004618extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004619
Andrew Boiedc5d9352017-06-02 12:56:47 -07004620/* arch/cpu.h may declare an architecture or platform-specific macro
4621 * for properly declaring stacks, compatible with MMU/MPU constraints if
4622 * enabled
4623 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004624
4625/**
4626 * @brief Obtain an extern reference to a stack
4627 *
4628 * This macro properly brings the symbol of a thread stack declared
4629 * elsewhere into scope.
4630 *
4631 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004632 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004633 */
4634#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4635
Andrew Boiedc5d9352017-06-02 12:56:47 -07004636#ifdef _ARCH_THREAD_STACK_DEFINE
4637#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4638#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4639 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304640#define K_THREAD_STACK_LEN(size) _ARCH_THREAD_STACK_LEN(size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004641#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4642#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004643static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004644{
4645 return _ARCH_THREAD_STACK_BUFFER(sym);
4646}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004647#else
4648/**
4649 * @brief Declare a toplevel thread stack memory region
4650 *
4651 * This declares a region of memory suitable for use as a thread's stack.
4652 *
4653 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4654 * 'noinit' section so that it isn't zeroed at boot
4655 *
Andrew Boie507852a2017-07-25 18:47:07 -07004656 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004657 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004658 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004659 *
4660 * It is legal to precede this definition with the 'static' keyword.
4661 *
4662 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4663 * parameter of k_thread_create(), it may not be the same as the
4664 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4665 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004666 * Some arches may round the size of the usable stack region up to satisfy
4667 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4668 * size.
4669 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004670 * @param sym Thread stack symbol name
4671 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004672 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004673 */
4674#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004675 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004676
4677/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304678 * @brief Calculate size of stacks to be allocated in a stack array
4679 *
4680 * This macro calculates the size to be allocated for the stacks
4681 * inside a stack array. It accepts the indicated "size" as a parameter
4682 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4683 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4684 *
4685 * @param size Size of the stack memory region
4686 * @req K-TSTACK-001
4687 */
4688#define K_THREAD_STACK_LEN(size) (size)
4689
4690/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004691 * @brief Declare a toplevel array of thread stack memory regions
4692 *
4693 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4694 * definition for additional details and constraints.
4695 *
4696 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4697 * 'noinit' section so that it isn't zeroed at boot
4698 *
4699 * @param sym Thread stack symbol name
4700 * @param nmemb Number of stacks to declare
4701 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004702 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004703 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004704#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004705 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304706 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004707
4708/**
4709 * @brief Declare an embedded stack memory region
4710 *
4711 * Used for stacks embedded within other data structures. Use is highly
4712 * discouraged but in some cases necessary. For memory protection scenarios,
4713 * it is very important that any RAM preceding this member not be writable
4714 * by threads else a stack overflow will lead to silent corruption. In other
4715 * words, the containing data structure should live in RAM owned by the kernel.
4716 *
4717 * @param sym Thread stack symbol name
4718 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004719 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004720 */
4721#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004722 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004723
4724/**
4725 * @brief Return the size in bytes of a stack memory region
4726 *
4727 * Convenience macro for passing the desired stack size to k_thread_create()
4728 * since the underlying implementation may actually create something larger
4729 * (for instance a guard area).
4730 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004731 * The value returned here is not guaranteed to match the 'size' parameter
4732 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004733 *
4734 * @param sym Stack memory symbol
4735 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004736 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004737 */
4738#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4739
4740/**
4741 * @brief Get a pointer to the physical stack buffer
4742 *
4743 * Convenience macro to get at the real underlying stack buffer used by
4744 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4745 * This is really only intended for diagnostic tools which want to examine
4746 * stack memory contents.
4747 *
4748 * @param sym Declared stack symbol name
4749 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004750 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004751 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004752static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004753{
4754 return (char *)sym;
4755}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004756
4757#endif /* _ARCH_DECLARE_STACK */
4758
Chunlin Hane9c97022017-07-07 20:29:30 +08004759/**
4760 * @defgroup mem_domain_apis Memory domain APIs
4761 * @ingroup kernel_apis
4762 * @{
4763 */
4764
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004765/**
4766 * @def MEM_PARTITION_ENTRY
4767 * @brief Used to declare a memory partition entry
4768 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004769 */
4770#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4771 {\
4772 .start = _start, \
4773 .size = _size, \
4774 .attr = _attr, \
4775 }
4776
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004777/**
4778 * @def K_MEM_PARTITION_DEFINE
4779 * @brief Used to declare a memory partition
4780 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004781 */
4782#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4783#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4784 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304785 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004786 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4787#else
4788#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304789 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004790 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4791#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4792
Chunlin Hane9c97022017-07-07 20:29:30 +08004793/* memory partition */
4794struct k_mem_partition {
4795 /* start address of memory partition */
4796 u32_t start;
4797 /* size of memory partition */
4798 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304799#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004800 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304801 k_mem_partition_attr_t attr;
4802#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004803};
4804
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304805/* memory domian
4806 * Note: Always declare this structure with __kernel prefix
4807 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004808struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304809#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004810 /* partitions in the domain */
4811 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304812#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004813 /* domain q */
4814 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004815 /* number of partitions in the domain */
4816 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004817};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304818
Chunlin Hane9c97022017-07-07 20:29:30 +08004819
4820/**
4821 * @brief Initialize a memory domain.
4822 *
4823 * Initialize a memory domain with given name and memory partitions.
4824 *
4825 * @param domain The memory domain to be initialized.
4826 * @param num_parts The number of array items of "parts" parameter.
4827 * @param parts An array of pointers to the memory partitions. Can be NULL
4828 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004829 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004830 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004831extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004832 struct k_mem_partition *parts[]);
4833/**
4834 * @brief Destroy a memory domain.
4835 *
4836 * Destroy a memory domain.
4837 *
4838 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004839 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004840 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004841extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4842
4843/**
4844 * @brief Add a memory partition into a memory domain.
4845 *
4846 * Add a memory partition into a memory domain.
4847 *
4848 * @param domain The memory domain to be added a memory partition.
4849 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004850 * @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_partition(struct k_mem_domain *domain,
4853 struct k_mem_partition *part);
4854
4855/**
4856 * @brief Remove a memory partition from a memory domain.
4857 *
4858 * Remove a memory partition from a memory domain.
4859 *
4860 * @param domain The memory domain to be removed a memory partition.
4861 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004862 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004863 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004864extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4865 struct k_mem_partition *part);
4866
4867/**
4868 * @brief Add a thread into a memory domain.
4869 *
4870 * Add a thread into a memory domain.
4871 *
4872 * @param domain The memory domain that the thread is going to be added into.
4873 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004874 *
4875 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004876 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004877extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4878 k_tid_t thread);
4879
4880/**
4881 * @brief Remove a thread from its memory domain.
4882 *
4883 * Remove a thread from its memory domain.
4884 *
4885 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004886 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004887 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004888extern void k_mem_domain_remove_thread(k_tid_t thread);
4889
Anas Nashif166f5192018-02-25 08:02:36 -06004890/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004891
Andrew Boie756f9072017-10-10 16:01:49 -07004892/**
4893 * @brief Emit a character buffer to the console device
4894 *
4895 * @param c String of characters to print
4896 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004897 *
4898 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004899 */
4900__syscall void k_str_out(char *c, size_t n);
4901
Andy Rosse7172672018-01-24 15:48:32 -08004902/**
4903 * @brief Start a numbered CPU on a MP-capable system
4904
4905 * This starts and initializes a specific CPU. The main thread on
4906 * startup is running on CPU zero, other processors are numbered
4907 * sequentially. On return from this function, the CPU is known to
4908 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004909 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004910 * with the provided key will work to enable them.
4911 *
4912 * Normally, in SMP mode this function will be called by the kernel
4913 * initialization and should not be used as a user API. But it is
4914 * defined here for special-purpose apps which want Zephyr running on
4915 * one core and to use others for design-specific processing.
4916 *
4917 * @param cpu_num Integer number of the CPU
4918 * @param stack Stack memory for the CPU
4919 * @param sz Stack buffer size, in bytes
4920 * @param fn Function to begin running on the CPU. First argument is
4921 * an irq_unlock() key.
4922 * @param arg Untyped argument to be passed to "fn"
4923 */
4924extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4925 void (*fn)(int, void *), void *arg);
4926
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004927#ifdef __cplusplus
4928}
4929#endif
4930
Andrew Boiee004dec2016-11-07 09:01:19 -08004931#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4932/*
4933 * Define new and delete operators.
4934 * At this moment, the operators do nothing since objects are supposed
4935 * to be statically allocated.
4936 */
4937inline void operator delete(void *ptr)
4938{
4939 (void)ptr;
4940}
4941
4942inline void operator delete[](void *ptr)
4943{
4944 (void)ptr;
4945}
4946
4947inline void *operator new(size_t size)
4948{
4949 (void)size;
4950 return NULL;
4951}
4952
4953inline void *operator new[](size_t size)
4954{
4955 (void)size;
4956 return NULL;
4957}
4958
4959/* Placement versions of operator new and delete */
4960inline void operator delete(void *ptr1, void *ptr2)
4961{
4962 (void)ptr1;
4963 (void)ptr2;
4964}
4965
4966inline void operator delete[](void *ptr1, void *ptr2)
4967{
4968 (void)ptr1;
4969 (void)ptr2;
4970}
4971
4972inline void *operator new(size_t size, void *ptr)
4973{
4974 (void)size;
4975 return ptr;
4976}
4977
4978inline void *operator new[](size_t size, void *ptr)
4979{
4980 (void)size;
4981 return ptr;
4982}
4983
4984#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4985
Anas Nashifb6304e62018-07-04 08:03:03 -05004986#include <tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07004987#include <syscalls/kernel.h>
4988
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004989#endif /* !_ASMLANGUAGE */
4990
Flavio Ceolin67ca1762018-09-14 10:43:44 -07004991#endif /* ZEPHYR_INCLUDE_KERNEL_H_ */