blob: d2003d06095dbf9812d52b7e53ccce257fe579e6 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
13#ifndef _kernel__h_
14#define _kernel__h_
15
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Ioannis Glaropoulos92b8a412018-06-20 17:30:48 +020017#include <kernel_includes.h>
Kumar Gala8777ff12018-07-25 20:24:34 -050018#include <errno.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040019
20#ifdef __cplusplus
21extern "C" {
22#endif
23
Anas Nashifbbb157d2017-01-15 08:46:31 -050024/**
25 * @brief Kernel APIs
26 * @defgroup kernel_apis Kernel APIs
27 * @{
28 * @}
29 */
30
Anas Nashif61f4b242016-11-18 10:53:59 -050031#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040032#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
33#else
34#define K_DEBUG(fmt, ...)
35#endif
36
Benjamin Walsh2f280412017-01-14 19:23:46 -050037#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
38#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
39#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
40#elif defined(CONFIG_COOP_ENABLED)
41#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
42#define _NUM_PREEMPT_PRIO (0)
43#elif defined(CONFIG_PREEMPT_ENABLED)
44#define _NUM_COOP_PRIO (0)
45#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
46#else
47#error "invalid configuration"
48#endif
49
50#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040051#define K_PRIO_PREEMPT(x) (x)
52
Benjamin Walsh456c6da2016-09-02 18:55:39 -040053#define K_ANY NULL
54#define K_END NULL
55
Benjamin Walshedb35702017-01-14 18:47:22 -050056#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040057#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050058#elif defined(CONFIG_COOP_ENABLED)
59#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
60#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040061#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050062#else
63#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040064#endif
65
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050066#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040067#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
68#else
69#define K_LOWEST_THREAD_PRIO -1
70#endif
71
Benjamin Walshfab8d922016-11-08 15:36:36 -050072#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
73
Benjamin Walsh456c6da2016-09-02 18:55:39 -040074#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
75#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
76
Andy Ross225c74b2018-06-27 11:20:50 -070077#ifdef CONFIG_WAITQ_SCALABLE
Andy Ross1acd8c22018-05-03 14:51:49 -070078
79typedef struct {
80 struct _priq_rb waitq;
81} _wait_q_t;
82
83extern int _priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
84
85#define _WAIT_Q_INIT(wait_q) { { { .lessthan_fn = _priq_rb_lessthan } } }
86
87#else
88
Andy Rossccf3bf72018-05-10 11:10:34 -070089typedef struct {
90 sys_dlist_t waitq;
91} _wait_q_t;
92
93#define _WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
Benjamin Walsh456c6da2016-09-02 18:55:39 -040094
Andy Ross1acd8c22018-05-03 14:51:49 -070095#endif
96
Anas Nashif2f203c22016-12-18 06:57:45 -050097#ifdef CONFIG_OBJECT_TRACING
98#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
99#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400100#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500101#define _OBJECT_TRACING_INIT
102#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400103#endif
104
Benjamin Walshacc68c12017-01-29 18:57:45 -0500105#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300106#define _POLL_EVENT_OBJ_INIT(obj) \
107 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
108#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500109#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300110#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500111#define _POLL_EVENT
112#endif
113
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500114struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400115struct k_mutex;
116struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400117struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400118struct k_msgq;
119struct k_mbox;
120struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200121struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122struct k_fifo;
123struct k_lifo;
124struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400125struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400126struct k_mem_pool;
127struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500128struct k_poll_event;
129struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800130struct k_mem_domain;
131struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400132
Andrew Boie5bd891d2017-09-27 12:59:28 -0700133/* This enumeration needs to be kept in sync with the lists of kernel objects
134 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
135 * function in kernel/userspace.c
136 */
Andrew Boie945af952017-08-22 13:15:23 -0700137enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700138 K_OBJ_ANY,
139
Leandro Pereirac2003672018-04-04 13:50:32 -0700140 /** @cond
141 * Doxygen should ignore this build-time generated include file
142 * when genrating API documentation. Enumeration values are
143 * generated during build by gen_kobject_list.py. It includes
144 * basic kernel objects (e.g. pipes and mutexes) and driver types.
145 */
146#include <kobj-types-enum.h>
147 /** @endcond
148 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700149
Andrew Boie945af952017-08-22 13:15:23 -0700150 K_OBJ_LAST
151};
152
153#ifdef CONFIG_USERSPACE
154/* Table generated by gperf, these objects are retrieved via
155 * _k_object_find() */
156struct _k_object {
157 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700158 u8_t perms[CONFIG_MAX_THREAD_BYTES];
159 u8_t type;
160 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700161 u32_t data;
Andrew Boiedf555242018-05-25 07:28:54 -0700162} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700163
Andrew Boie877f82e2017-10-17 11:20:22 -0700164struct _k_object_assignment {
165 struct k_thread *thread;
166 void * const *objects;
167};
168
169/**
170 * @brief Grant a static thread access to a list of kernel objects
171 *
172 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
173 * a set of kernel objects. These objects do not need to be in an initialized
174 * state. The permissions will be granted when the threads are initialized
175 * in the early boot sequence.
176 *
177 * All arguments beyond the first must be pointers to kernel objects.
178 *
179 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
180 */
181#define K_THREAD_ACCESS_GRANT(name_, ...) \
182 static void * const _CONCAT(_object_list_, name_)[] = \
183 { __VA_ARGS__, NULL }; \
184 static __used __in_section_unique(object_access) \
185 const struct _k_object_assignment \
186 _CONCAT(_object_access_, name_) = \
187 { (&_k_thread_obj_ ## name_), \
188 (_CONCAT(_object_list_, name_)) }
189
Andrew Boie945af952017-08-22 13:15:23 -0700190#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700191#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700192#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie945af952017-08-22 13:15:23 -0700193
194/**
195 * Lookup a kernel object and init its metadata if it exists
196 *
197 * Calling this on an object will make it usable from userspace.
198 * Intended to be called as the last statement in kernel object init
199 * functions.
200 *
201 * @param object Address of the kernel object
202 */
203void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700204#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700205
206#define K_THREAD_ACCESS_GRANT(thread, ...)
207
Anas Nashif954d5502018-02-25 08:37:28 -0600208/**
209 * @internal
210 */
Andrew Boie743e4682017-10-04 12:25:50 -0700211static inline void _k_object_init(void *obj)
212{
213 ARG_UNUSED(obj);
214}
215
Anas Nashif954d5502018-02-25 08:37:28 -0600216/**
217 * @internal
218 */
Andrew Boie743e4682017-10-04 12:25:50 -0700219static inline void _impl_k_object_access_grant(void *object,
220 struct k_thread *thread)
221{
222 ARG_UNUSED(object);
223 ARG_UNUSED(thread);
224}
225
Anas Nashif954d5502018-02-25 08:37:28 -0600226/**
227 * @internal
228 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700229static inline void k_object_access_revoke(void *object,
230 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700231{
232 ARG_UNUSED(object);
233 ARG_UNUSED(thread);
234}
235
Andrew Boiee9cfc542018-04-13 13:15:28 -0700236/**
237 * @internal
238 */
239static inline void _impl_k_object_release(void *object)
240{
241 ARG_UNUSED(object);
242}
243
Andrew Boie41bab6e2017-10-14 14:42:23 -0700244static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700245{
246 ARG_UNUSED(object);
247}
248#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700249
250/**
251 * grant a thread access to a kernel object
252 *
253 * The thread will be granted access to the object if the caller is from
254 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700255 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700256 *
257 * @param object Address of kernel object
258 * @param thread Thread to grant access to the object
259 */
Andrew Boie743e4682017-10-04 12:25:50 -0700260__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700261
Andrew Boiea89bf012017-10-09 14:47:55 -0700262/**
263 * grant a thread access to a kernel object
264 *
265 * The thread will lose access to the object if the caller is from
266 * supervisor mode, or the caller is from user mode AND has permissions
267 * on both the object and the thread whose access is being revoked.
268 *
269 * @param object Address of kernel object
270 * @param thread Thread to remove access to the object
271 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700272void k_object_access_revoke(void *object, struct k_thread *thread);
273
274
275__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700276
277/**
278 * grant all present and future threads access to an object
279 *
280 * If the caller is from supervisor mode, or the caller is from user mode and
281 * have sufficient permissions on the object, then that object will have
282 * permissions granted to it for *all* current and future threads running in
283 * the system, effectively becoming a public kernel object.
284 *
285 * Use of this API should be avoided on systems that are running untrusted code
286 * as it is possible for such code to derive the addresses of kernel objects
287 * and perform unwanted operations on them.
288 *
Andrew Boie04caa672017-10-13 13:57:07 -0700289 * It is not possible to revoke permissions on public objects; once public,
290 * any thread may use it.
291 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700292 * @param object Address of kernel object
293 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700294void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700295
Andrew Boie31bdfc02017-11-08 16:38:03 -0800296/**
297 * Allocate a kernel object of a designated type
298 *
299 * This will instantiate at runtime a kernel object of the specified type,
300 * returning a pointer to it. The object will be returned in an uninitialized
301 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700302 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800303 *
304 * Currently, allocation of thread stacks is not supported.
305 *
306 * @param otype Requested kernel object type
307 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
308 * available
309 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700310__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800311
Andrew Boie97bf0012018-04-24 17:01:37 -0700312#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800313/**
314 * Free a kernel object previously allocated with k_object_alloc()
315 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700316 * This will return memory for a kernel object back to resource pool it was
317 * allocated from. Care must be exercised that the object will not be used
318 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800319 *
320 * @param obj Pointer to the kernel object memory address.
321 */
322void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700323#else
324static inline void *_impl_k_object_alloc(enum k_objects otype)
325{
Kumar Gala85699f72018-05-17 09:26:03 -0500326 ARG_UNUSED(otype);
327
Andrew Boie97bf0012018-04-24 17:01:37 -0700328 return NULL;
329}
330
331static inline void k_obj_free(void *obj)
332{
333 ARG_UNUSED(obj);
334}
Andrew Boie31bdfc02017-11-08 16:38:03 -0800335#endif /* CONFIG_DYNAMIC_OBJECTS */
336
Andrew Boiebca15da2017-10-15 14:17:48 -0700337/* Using typedef deliberately here, this is quite intended to be an opaque
338 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
339 *
340 * The purpose of this data type is to clearly distinguish between the
341 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
342 * buffer which composes the stack data actually used by the underlying
343 * thread; they cannot be used interchangably as some arches precede the
344 * stack buffer region with guard areas that trigger a MPU or MMU fault
345 * if written to.
346 *
347 * APIs that want to work with the buffer inside should continue to use
348 * char *.
349 *
350 * Stacks should always be created with K_THREAD_STACK_DEFINE().
351 */
352struct __packed _k_thread_stack_element {
353 char data;
354};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700355typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700356
Andrew Boie73abd322017-04-04 13:19:13 -0700357/* timeouts */
358
359struct _timeout;
360typedef void (*_timeout_func_t)(struct _timeout *t);
361
362struct _timeout {
363 sys_dnode_t node;
364 struct k_thread *thread;
365 sys_dlist_t *wait_q;
366 s32_t delta_ticks_from_prev;
367 _timeout_func_t func;
368};
369
370extern s32_t _timeout_remaining_get(struct _timeout *timeout);
371
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700372/**
373 * @typedef k_thread_entry_t
374 * @brief Thread entry point function type.
375 *
376 * A thread's entry point function is invoked when the thread starts executing.
377 * Up to 3 argument values can be passed to the function.
378 *
379 * The thread terminates execution permanently if the entry point function
380 * returns. The thread is responsible for releasing any shared resources
381 * it may own (such as mutexes and dynamically allocated memory), prior to
382 * returning.
383 *
384 * @param p1 First argument.
385 * @param p2 Second argument.
386 * @param p3 Third argument.
387 *
388 * @return N/A
389 */
390typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700391
392#ifdef CONFIG_THREAD_MONITOR
393struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700394 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700395 void *parameter1;
396 void *parameter2;
397 void *parameter3;
398};
399#endif
400
401/* can be used for creating 'dummy' threads, e.g. for pending on objects */
402struct _thread_base {
403
404 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700405 union {
406 sys_dlist_t qnode_dlist;
407 struct rbnode qnode_rb;
408 };
409
Andy Ross225c74b2018-06-27 11:20:50 -0700410#ifdef CONFIG_WAITQ_SCALABLE
Andy Ross1acd8c22018-05-03 14:51:49 -0700411 /* wait queue on which the thread is pended (needed only for
412 * trees, not dumb lists)
413 */
414 _wait_q_t *pended_on;
415#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700416
417 /* user facing 'thread options'; values defined in include/kernel.h */
418 u8_t user_options;
419
420 /* thread state */
421 u8_t thread_state;
422
423 /*
424 * scheduler lock count and thread priority
425 *
426 * These two fields control the preemptibility of a thread.
427 *
428 * When the scheduler is locked, sched_locked is decremented, which
429 * means that the scheduler is locked for values from 0xff to 0x01. A
430 * thread is coop if its prio is negative, thus 0x80 to 0xff when
431 * looked at the value as unsigned.
432 *
433 * By putting them end-to-end, this means that a thread is
434 * non-preemptible if the bundled value is greater than or equal to
435 * 0x0080.
436 */
437 union {
438 struct {
439#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
440 u8_t sched_locked;
441 s8_t prio;
442#else /* LITTLE and PDP */
443 s8_t prio;
444 u8_t sched_locked;
445#endif
446 };
447 u16_t preempt;
448 };
449
Andy Ross4a2e50f2018-05-15 11:06:25 -0700450#ifdef CONFIG_SCHED_DEADLINE
451 int prio_deadline;
452#endif
453
Andy Ross1acd8c22018-05-03 14:51:49 -0700454 u32_t order_key;
455
Andy Ross2724fd12018-01-29 14:55:20 -0800456#ifdef CONFIG_SMP
457 /* True for the per-CPU idle threads */
458 u8_t is_idle;
459
Andy Ross2724fd12018-01-29 14:55:20 -0800460 /* CPU index on which thread was last run */
461 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700462
463 /* Recursive count of irq_lock() calls */
464 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800465#endif
466
Andrew Boie73abd322017-04-04 13:19:13 -0700467 /* data returned by APIs */
468 void *swap_data;
469
470#ifdef CONFIG_SYS_CLOCK_EXISTS
471 /* this thread's entry in a timeout queue */
472 struct _timeout timeout;
473#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700474};
475
476typedef struct _thread_base _thread_base_t;
477
478#if defined(CONFIG_THREAD_STACK_INFO)
479/* Contains the stack information of a thread */
480struct _thread_stack_info {
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700481 /* Stack Start - Identical to K_THREAD_STACK_BUFFER() on the stack
482 * object. Represents thread-writable stack area without any extras.
483 */
Andrew Boie73abd322017-04-04 13:19:13 -0700484 u32_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700485
486 /* Stack Size - Thread writable stack buffer size. Represents
487 * the size of the actual area, starting from the start member,
488 * that should be writable by the thread
489 */
Andrew Boie73abd322017-04-04 13:19:13 -0700490 u32_t size;
491};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700492
493typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700494#endif /* CONFIG_THREAD_STACK_INFO */
495
Chunlin Hane9c97022017-07-07 20:29:30 +0800496#if defined(CONFIG_USERSPACE)
497struct _mem_domain_info {
498 /* memory domain queue node */
499 sys_dnode_t mem_domain_q_node;
500 /* memory domain of the thread */
501 struct k_mem_domain *mem_domain;
502};
503
504#endif /* CONFIG_USERSPACE */
505
Daniel Leungfc182432018-08-16 15:42:28 -0700506#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
507struct _thread_userspace_local_data {
508 int errno_var;
509};
510#endif
511
Anas Nashifce78d162018-05-24 12:43:11 -0500512/**
513 * @ingroup thread_apis
514 * Thread Structure
515 */
Andrew Boie73abd322017-04-04 13:19:13 -0700516struct k_thread {
517
518 struct _thread_base base;
519
Anas Nashifce78d162018-05-24 12:43:11 -0500520 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700521 struct _caller_saved caller_saved;
Anas Nashifce78d162018-05-24 12:43:11 -0500522 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700523 struct _callee_saved callee_saved;
524
Anas Nashifce78d162018-05-24 12:43:11 -0500525 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700526 void *init_data;
527
Anas Nashifce78d162018-05-24 12:43:11 -0500528 /**
529 * abort function
530 * @req K-THREAD-002
531 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700532 void (*fn_abort)(void);
533
534#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500535 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700536 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700537
Anas Nashifce78d162018-05-24 12:43:11 -0500538 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700539 struct k_thread *next_thread;
540#endif
541
542#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500543 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700544 void *custom_data;
545#endif
546
Daniel Leungfc182432018-08-16 15:42:28 -0700547#ifdef CONFIG_THREAD_USERSPACE_LOCAL_DATA
548 struct _thread_userspace_local_data *userspace_local_data;
549#endif
550
Andrew Boie73abd322017-04-04 13:19:13 -0700551#ifdef CONFIG_ERRNO
Daniel Leungfc182432018-08-16 15:42:28 -0700552#ifndef CONFIG_USERSPACE
Anas Nashifce78d162018-05-24 12:43:11 -0500553 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700554 int errno_var;
555#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700556#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700557
558#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500559 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700560 struct _thread_stack_info stack_info;
561#endif /* CONFIG_THREAD_STACK_INFO */
562
Chunlin Hane9c97022017-07-07 20:29:30 +0800563#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500564 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800565 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500566 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700567 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800568#endif /* CONFIG_USERSPACE */
569
Andy Ross042d8ec2017-12-09 08:37:20 -0800570#if defined(CONFIG_USE_SWITCH)
571 /* When using __switch() a few previously arch-specific items
572 * become part of the core OS
573 */
574
Anas Nashifce78d162018-05-24 12:43:11 -0500575 /** _Swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800576 int swap_retval;
577
Anas Nashifce78d162018-05-24 12:43:11 -0500578 /** Context handle returned via _arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800579 void *switch_handle;
580#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500581 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700582 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800583
Anas Nashifce78d162018-05-24 12:43:11 -0500584 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700585 struct _thread_arch arch;
586};
587
588typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400589typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700590#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400591
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400592enum execution_context_types {
593 K_ISR = 0,
594 K_COOP_THREAD,
595 K_PREEMPT_THREAD,
596};
597
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400598/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100599 * @defgroup profiling_apis Profiling APIs
600 * @ingroup kernel_apis
601 * @{
602 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530603typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
604 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100605
606/**
607 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
608 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700609 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100610 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
611 *
612 * CONFIG_MAIN_STACK_SIZE
613 * CONFIG_IDLE_STACK_SIZE
614 * CONFIG_ISR_STACK_SIZE
615 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
616 *
617 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
618 * produce output.
619 *
620 * @return N/A
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530621 *
622 * @deprecated This API is deprecated. Use k_thread_foreach().
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100623 */
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530624__deprecated extern void k_call_stacks_analyze(void);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100625
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530626/**
627 * @brief Iterate over all the threads in the system.
628 *
629 * This routine iterates over all the threads in the system and
630 * calls the user_cb function for each thread.
631 *
632 * @param user_cb Pointer to the user callback function.
633 * @param user_data Pointer to user data.
634 *
635 * @note CONFIG_THREAD_MONITOR must be set for this function
636 * to be effective. Also this API uses irq_lock to protect the
637 * _kernel.threads list which means creation of new threads and
638 * terminations of existing threads are blocked until this
639 * API returns.
640 *
641 * @return N/A
642 */
643extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
644
Anas Nashif166f5192018-02-25 08:02:36 -0600645/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100646
647/**
Allan Stephensc98da842016-11-11 15:45:03 -0500648 * @defgroup thread_apis Thread APIs
649 * @ingroup kernel_apis
650 * @{
651 */
652
Benjamin Walshed240f22017-01-22 13:05:08 -0500653#endif /* !_ASMLANGUAGE */
654
655
656/*
657 * Thread user options. May be needed by assembly code. Common part uses low
658 * bits, arch-specific use high bits.
659 */
660
Anas Nashifa541e932018-05-24 11:19:16 -0500661/**
662 * @brief system thread that must not abort
663 * @req K-THREAD-000
664 * */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700665#define K_ESSENTIAL (BIT(0))
Benjamin Walshed240f22017-01-22 13:05:08 -0500666
667#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500668/**
669 * @brief thread uses floating point registers
670 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700671#define K_FP_REGS (BIT(1))
Benjamin Walshed240f22017-01-22 13:05:08 -0500672#endif
673
Anas Nashifa541e932018-05-24 11:19:16 -0500674/**
675 * @brief user mode thread
676 *
677 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700678 * has additional restrictions
679 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700680#define K_USER (BIT(2))
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700681
Anas Nashifa541e932018-05-24 11:19:16 -0500682/**
683 * @brief Inherit Permissions
684 *
685 * @details
686 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700687 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
688 * is not enabled.
689 */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700690#define K_INHERIT_PERMS (BIT(3))
Andrew Boie47f8fd12017-10-05 11:11:02 -0700691
Benjamin Walshed240f22017-01-22 13:05:08 -0500692#ifdef CONFIG_X86
693/* x86 Bitmask definitions for threads user options */
694
695#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
696/* thread uses SSEx (and also FP) registers */
Flavio Ceolin8aec0872018-08-15 11:52:00 -0700697#define K_SSE_REGS (BIT(7))
Benjamin Walshed240f22017-01-22 13:05:08 -0500698#endif
699#endif
700
701/* end - thread options */
702
703#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400704/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700705 * @brief Create a thread.
706 *
707 * This routine initializes a thread, then schedules it for execution.
708 *
709 * The new thread may be scheduled for immediate execution or a delayed start.
710 * If the newly spawned thread does not have a delayed start the kernel
711 * scheduler may preempt the current thread to allow the new thread to
712 * execute.
713 *
714 * Thread options are architecture-specific, and can include K_ESSENTIAL,
715 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
716 * them using "|" (the logical OR operator).
717 *
718 * Historically, users often would use the beginning of the stack memory region
719 * to store the struct k_thread data, although corruption will occur if the
720 * stack overflows this region and stack protection features may not detect this
721 * situation.
722 *
723 * @param new_thread Pointer to uninitialized struct k_thread
724 * @param stack Pointer to the stack space.
725 * @param stack_size Stack size in bytes.
726 * @param entry Thread entry function.
727 * @param p1 1st entry point parameter.
728 * @param p2 2nd entry point parameter.
729 * @param p3 3rd entry point parameter.
730 * @param prio Thread priority.
731 * @param options Thread options.
732 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
733 *
734 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400735 *
736 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700737 */
Andrew Boie662c3452017-10-02 10:51:18 -0700738__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700739 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700740 size_t stack_size,
741 k_thread_entry_t entry,
742 void *p1, void *p2, void *p3,
743 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700744
Andrew Boie3f091b52017-08-30 14:34:14 -0700745/**
746 * @brief Drop a thread's privileges permanently to user mode
747 *
748 * @param entry Function to start executing from
749 * @param p1 1st entry point parameter
750 * @param p2 2nd entry point parameter
751 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400752 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700753 */
754extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
755 void *p1, void *p2,
756 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700757
Andrew Boied26cf2d2017-03-30 13:07:02 -0700758/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700759 * @brief Grant a thread access to a NULL-terminated set of kernel objects
760 *
761 * This is a convenience function. For the provided thread, grant access to
762 * the remaining arguments, which must be pointers to kernel objects.
763 * The final argument must be a NULL.
764 *
765 * The thread object must be initialized (i.e. running). The objects don't
766 * need to be.
767 *
768 * @param thread Thread to grant access to objects
769 * @param ... NULL-terminated list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400770 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700771 */
772extern void __attribute__((sentinel))
773 k_thread_access_grant(struct k_thread *thread, ...);
774
775/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700776 * @brief Assign a resource memory pool to a thread
777 *
778 * By default, threads have no resource pool assigned unless their parent
779 * thread has a resource pool, in which case it is inherited. Multiple
780 * threads may be assigned to the same memory pool.
781 *
782 * Changing a thread's resource pool will not migrate allocations from the
783 * previous pool.
784 *
785 * @param thread Target thread to assign a memory pool for resource requests,
786 * or NULL if the thread should no longer have a memory pool.
787 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400788 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700789 */
790static inline void k_thread_resource_pool_assign(struct k_thread *thread,
791 struct k_mem_pool *pool)
792{
793 thread->resource_pool = pool;
794}
795
796#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
797/**
798 * @brief Assign the system heap as a thread's resource pool
799 *
800 * Similar to k_thread_resource_pool_assign(), but the thread will use
801 * the kernel heap to draw memory.
802 *
803 * Use with caution, as a malicious thread could perform DoS attacks on the
804 * kernel heap.
805 *
806 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400807 *
808 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700809 */
810void k_thread_system_pool_assign(struct k_thread *thread);
811#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
812
813/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500814 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400815 *
Allan Stephensc98da842016-11-11 15:45:03 -0500816 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500817 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500819 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400820 *
821 * @return N/A
822 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700823__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400824
825/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500826 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827 *
828 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500829 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400831 * @return N/A
832 */
Kumar Galacc334c72017-04-21 10:55:34 -0500833extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400834
835/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500836 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500838 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400839 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500840 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400841 *
842 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400843 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844 */
Andrew Boie468190a2017-09-29 14:00:48 -0700845__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400846
847/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500848 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500850 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500852 * If @a thread is not currently sleeping, the routine has no effect.
853 *
854 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400855 *
856 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400857 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400858 */
Andrew Boie468190a2017-09-29 14:00:48 -0700859__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400860
861/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500862 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500864 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400865 *
866 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400867 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700868__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400869
870/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500871 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400872 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500873 * This routine prevents @a thread from executing if it has not yet started
874 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500876 * @param thread ID of thread to cancel.
877 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700878 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500879 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700880 *
881 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400882 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700883__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400884
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400885/**
Allan Stephensc98da842016-11-11 15:45:03 -0500886 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500888 * This routine permanently stops execution of @a thread. The thread is taken
889 * off all kernel queues it is part of (i.e. the ready queue, the timeout
890 * queue, or a kernel object wait queue). However, any kernel resources the
891 * thread might currently own (such as mutexes or memory blocks) are not
892 * released. It is the responsibility of the caller of this routine to ensure
893 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400894 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500895 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400896 *
897 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400898 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400899 */
Andrew Boie468190a2017-09-29 14:00:48 -0700900__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400901
Andrew Boie7d627c52017-08-30 11:01:56 -0700902
903/**
904 * @brief Start an inactive thread
905 *
906 * If a thread was created with K_FOREVER in the delay parameter, it will
907 * not be added to the scheduling queue until this function is called
908 * on it.
909 *
910 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400911 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700912 */
Andrew Boie468190a2017-09-29 14:00:48 -0700913__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700914
Allan Stephensc98da842016-11-11 15:45:03 -0500915/**
916 * @cond INTERNAL_HIDDEN
917 */
918
Benjamin Walshd211a522016-12-06 11:44:01 -0500919/* timeout has timed out and is not on _timeout_q anymore */
920#define _EXPIRED (-2)
921
922/* timeout is not in use */
923#define _INACTIVE (-1)
924
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400925struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700926 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700927 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400928 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700929 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500930 void *init_p1;
931 void *init_p2;
932 void *init_p3;
933 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500934 u32_t init_options;
935 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500936 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400937};
938
Andrew Boied26cf2d2017-03-30 13:07:02 -0700939#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400940 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500941 prio, options, delay, abort, groups) \
942 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700943 .init_thread = (thread), \
944 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500945 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700946 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400947 .init_p1 = (void *)p1, \
948 .init_p2 = (void *)p2, \
949 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500950 .init_prio = (prio), \
951 .init_options = (options), \
952 .init_delay = (delay), \
953 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400954 }
955
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400956/**
Allan Stephensc98da842016-11-11 15:45:03 -0500957 * INTERNAL_HIDDEN @endcond
958 */
959
960/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500961 * @brief Statically define and initialize a thread.
962 *
963 * The thread may be scheduled for immediate execution or a delayed start.
964 *
965 * Thread options are architecture-specific, and can include K_ESSENTIAL,
966 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
967 * them using "|" (the logical OR operator).
968 *
969 * The ID of the thread can be accessed using:
970 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500971 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500972 *
973 * @param name Name of the thread.
974 * @param stack_size Stack size in bytes.
975 * @param entry Thread entry function.
976 * @param p1 1st entry point parameter.
977 * @param p2 2nd entry point parameter.
978 * @param p3 3rd entry point parameter.
979 * @param prio Thread priority.
980 * @param options Thread options.
981 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400982 *
Anas Nashif47420d02018-05-24 14:20:56 -0400983 * @req K-THREAD-010
984 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400985 * @internal It has been observed that the x86 compiler by default aligns
986 * these _static_thread_data structures to 32-byte boundaries, thereby
987 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400988 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400989 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500990#define K_THREAD_DEFINE(name, stack_size, \
991 entry, p1, p2, p3, \
992 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700993 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700994 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500995 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500996 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700997 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
998 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500999 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -07001000 NULL, 0); \
1001 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001003/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001004 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001006 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001008 * @param thread ID of thread whose priority is needed.
1009 *
1010 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -04001011 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001012 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001013__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001014
1015/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001016 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001018 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001019 *
1020 * Rescheduling can occur immediately depending on the priority @a thread is
1021 * set to:
1022 *
1023 * - If its priority is raised above the priority of the caller of this
1024 * function, and the caller is preemptible, @a thread will be scheduled in.
1025 *
1026 * - If the caller operates on itself, it lowers its priority below that of
1027 * other threads in the system, and the caller is preemptible, the thread of
1028 * highest priority will be scheduled in.
1029 *
1030 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1031 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1032 * highest priority.
1033 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001034 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001035 * @param prio New priority.
1036 *
1037 * @warning Changing the priority of a thread currently involved in mutex
1038 * priority inheritance may result in undefined behavior.
1039 *
1040 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001041 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001042 */
Andrew Boie468190a2017-09-29 14:00:48 -07001043__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001044
Andy Ross4a2e50f2018-05-15 11:06:25 -07001045
1046#ifdef CONFIG_SCHED_DEADLINE
1047/**
1048 * @brief Set deadline expiration time for scheduler
1049 *
1050 * This sets the "deadline" expiration as a time delta from the
1051 * current time, in the same units used by k_cycle_get_32(). The
1052 * scheduler (when deadline scheduling is enabled) will choose the
1053 * next expiring thread when selecting between threads at the same
1054 * static priority. Threads at different priorities will be scheduled
1055 * according to their static priority.
1056 *
1057 * @note Deadlines that are negative (i.e. in the past) are still seen
1058 * as higher priority than others, even if the thread has "finished"
1059 * its work. If you don't want it scheduled anymore, you have to
1060 * reset the deadline into the future, block/pend the thread, or
1061 * modify its priority with k_thread_priority_set().
1062 *
1063 * @note Despite the API naming, the scheduler makes no guarantees the
1064 * the thread WILL be scheduled within that deadline, nor does it take
1065 * extra metadata (like e.g. the "runtime" and "period" parameters in
1066 * Linux sched_setattr()) that allows the kernel to validate the
1067 * scheduling for achievability. Such features could be implemented
1068 * above this call, which is simply input to the priority selection
1069 * logic.
1070 *
1071 * @param thread A thread on which to set the deadline
1072 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001073 *
1074 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001075 */
1076__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1077#endif
1078
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001079/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001080 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001081 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001082 * This routine prevents the kernel scheduler from making @a thread the
1083 * current thread. All other internal operations on @a thread are still
1084 * performed; for example, any timeout it is waiting on keeps ticking,
1085 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001086 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001087 * If @a thread is already suspended, the routine has no effect.
1088 *
1089 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001090 *
1091 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001092 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001093 */
Andrew Boie468190a2017-09-29 14:00:48 -07001094__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001095
1096/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001097 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001098 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001099 * This routine allows the kernel scheduler to make @a thread the current
1100 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001101 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001102 * If @a thread is not currently suspended, the routine has no effect.
1103 *
1104 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001105 *
1106 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001107 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001108 */
Andrew Boie468190a2017-09-29 14:00:48 -07001109__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001110
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001111/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001112 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001113 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001114 * This routine specifies how the scheduler will perform time slicing of
1115 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001117 * To enable time slicing, @a slice must be non-zero. The scheduler
1118 * ensures that no thread runs for more than the specified time limit
1119 * before other threads of that priority are given a chance to execute.
1120 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001121 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001123 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001124 * execute. Once the scheduler selects a thread for execution, there is no
1125 * minimum guaranteed time the thread will execute before threads of greater or
1126 * equal priority are scheduled.
1127 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001128 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001129 * for execution, this routine has no effect; the thread is immediately
1130 * rescheduled after the slice period expires.
1131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001132 * To disable timeslicing, set both @a slice and @a prio to zero.
1133 *
1134 * @param slice Maximum time slice length (in milliseconds).
1135 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001136 *
1137 * @return N/A
1138 */
Kumar Galacc334c72017-04-21 10:55:34 -05001139extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001140
Anas Nashif166f5192018-02-25 08:02:36 -06001141/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001142
1143/**
1144 * @addtogroup isr_apis
1145 * @{
1146 */
1147
1148/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001149 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001150 *
Allan Stephensc98da842016-11-11 15:45:03 -05001151 * This routine allows the caller to customize its actions, depending on
1152 * whether it is a thread or an ISR.
1153 *
1154 * @note Can be called by ISRs.
1155 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001156 * @return 0 if invoked by a thread.
1157 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001158 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001159extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001160
Benjamin Walsh445830d2016-11-10 15:54:27 -05001161/**
1162 * @brief Determine if code is running in a preemptible thread.
1163 *
Allan Stephensc98da842016-11-11 15:45:03 -05001164 * This routine allows the caller to customize its actions, depending on
1165 * whether it can be preempted by another thread. The routine returns a 'true'
1166 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001167 *
Allan Stephensc98da842016-11-11 15:45:03 -05001168 * - The code is running in a thread, not at ISR.
1169 * - The thread's priority is in the preemptible range.
1170 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001171 *
Allan Stephensc98da842016-11-11 15:45:03 -05001172 * @note Can be called by ISRs.
1173 *
1174 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001175 * @return Non-zero if invoked by a preemptible thread.
1176 */
Andrew Boie468190a2017-09-29 14:00:48 -07001177__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001178
Allan Stephensc98da842016-11-11 15:45:03 -05001179/**
Anas Nashif166f5192018-02-25 08:02:36 -06001180 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001181 */
1182
1183/**
1184 * @addtogroup thread_apis
1185 * @{
1186 */
1187
1188/**
1189 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001190 *
Allan Stephensc98da842016-11-11 15:45:03 -05001191 * This routine prevents the current thread from being preempted by another
1192 * thread by instructing the scheduler to treat it as a cooperative thread.
1193 * If the thread subsequently performs an operation that makes it unready,
1194 * it will be context switched out in the normal manner. When the thread
1195 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001196 *
Allan Stephensc98da842016-11-11 15:45:03 -05001197 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001198 *
Allan Stephensc98da842016-11-11 15:45:03 -05001199 * @note k_sched_lock() and k_sched_unlock() should normally be used
1200 * when the operation being performed can be safely interrupted by ISRs.
1201 * However, if the amount of processing involved is very small, better
1202 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001203 *
1204 * @return N/A
1205 */
1206extern void k_sched_lock(void);
1207
Allan Stephensc98da842016-11-11 15:45:03 -05001208/**
1209 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001210 *
Allan Stephensc98da842016-11-11 15:45:03 -05001211 * This routine reverses the effect of a previous call to k_sched_lock().
1212 * A thread must call the routine once for each time it called k_sched_lock()
1213 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001214 *
1215 * @return N/A
1216 */
1217extern void k_sched_unlock(void);
1218
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001219/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001220 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001221 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001222 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001223 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001224 * Custom data is not used by the kernel itself, and is freely available
1225 * for a thread to use as it sees fit. It can be used as a framework
1226 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001227 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001228 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001229 *
1230 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001231 *
1232 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001233 */
Andrew Boie468190a2017-09-29 14:00:48 -07001234__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001235
1236/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001237 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001239 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001240 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001241 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001242 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001243 */
Andrew Boie468190a2017-09-29 14:00:48 -07001244__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001245
1246/**
Anas Nashif166f5192018-02-25 08:02:36 -06001247 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001248 */
1249
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001250#include <sys_clock.h>
1251
Allan Stephensc2f15a42016-11-17 12:24:22 -05001252/**
1253 * @addtogroup clock_apis
1254 * @{
1255 */
1256
1257/**
1258 * @brief Generate null timeout delay.
1259 *
1260 * This macro generates a timeout delay that that instructs a kernel API
1261 * not to wait if the requested operation cannot be performed immediately.
1262 *
1263 * @return Timeout delay value.
1264 */
1265#define K_NO_WAIT 0
1266
1267/**
1268 * @brief Generate timeout delay from milliseconds.
1269 *
1270 * This macro generates a timeout delay that that instructs a kernel API
1271 * to wait up to @a ms milliseconds to perform the requested operation.
1272 *
1273 * @param ms Duration in milliseconds.
1274 *
1275 * @return Timeout delay value.
1276 */
Johan Hedberg14471692016-11-13 10:52:15 +02001277#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001278
1279/**
1280 * @brief Generate timeout delay from seconds.
1281 *
1282 * This macro generates a timeout delay that that instructs a kernel API
1283 * to wait up to @a s seconds to perform the requested operation.
1284 *
1285 * @param s Duration in seconds.
1286 *
1287 * @return Timeout delay value.
1288 */
Johan Hedberg14471692016-11-13 10:52:15 +02001289#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001290
1291/**
1292 * @brief Generate timeout delay from minutes.
1293 *
1294 * This macro generates a timeout delay that that instructs a kernel API
1295 * to wait up to @a m minutes to perform the requested operation.
1296 *
1297 * @param m Duration in minutes.
1298 *
1299 * @return Timeout delay value.
1300 */
Johan Hedberg14471692016-11-13 10:52:15 +02001301#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001302
1303/**
1304 * @brief Generate timeout delay from hours.
1305 *
1306 * This macro generates a timeout delay that that instructs a kernel API
1307 * to wait up to @a h hours to perform the requested operation.
1308 *
1309 * @param h Duration in hours.
1310 *
1311 * @return Timeout delay value.
1312 */
Johan Hedberg14471692016-11-13 10:52:15 +02001313#define K_HOURS(h) K_MINUTES((h) * 60)
1314
Allan Stephensc98da842016-11-11 15:45:03 -05001315/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001316 * @brief Generate infinite timeout delay.
1317 *
1318 * This macro generates a timeout delay that that instructs a kernel API
1319 * to wait as long as necessary to perform the requested operation.
1320 *
1321 * @return Timeout delay value.
1322 */
1323#define K_FOREVER (-1)
1324
1325/**
Anas Nashif166f5192018-02-25 08:02:36 -06001326 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001327 */
1328
1329/**
Allan Stephensc98da842016-11-11 15:45:03 -05001330 * @cond INTERNAL_HIDDEN
1331 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001332
Benjamin Walsh62092182016-12-20 14:39:08 -05001333/* kernel clocks */
1334
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001335#ifdef CONFIG_SYS_CLOCK_EXISTS
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001336
1337/*
1338 * If timer frequency is known at compile time, a simple (32-bit)
1339 * tick <-> ms conversion could be used for some combinations of
1340 * hardware timer frequency and tick rate. Otherwise precise
1341 * (64-bit) calculations are used.
1342 */
1343
1344#if !defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
1345#if (sys_clock_hw_cycles_per_sec % sys_clock_ticks_per_sec) != 0
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001346 #define _NEED_PRECISE_TICK_MS_CONVERSION
1347#elif (MSEC_PER_SEC % sys_clock_ticks_per_sec) != 0
Benjamin Walsh62092182016-12-20 14:39:08 -05001348 #define _NON_OPTIMIZED_TICKS_PER_SEC
1349#endif
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001350#endif
Benjamin Walsh62092182016-12-20 14:39:08 -05001351
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001352#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
1353 defined(_NON_OPTIMIZED_TICKS_PER_SEC)
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001354 #define _NEED_PRECISE_TICK_MS_CONVERSION
1355#endif
1356#endif
1357
Kumar Galacc334c72017-04-21 10:55:34 -05001358static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001359{
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001360#ifdef CONFIG_SYS_CLOCK_EXISTS
1361
1362#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
1363 /* use 64-bit math to keep precision */
Piotr Zięcik3c7f9902018-07-23 14:09:10 +02001364 return (s32_t)ceiling_fraction(
1365 (s64_t)ms * sys_clock_hw_cycles_per_sec,
1366 (s64_t)MSEC_PER_SEC * sys_clock_hw_cycles_per_tick);
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001367#else
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001368 /* simple division keeps precision */
1369 s32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
1370
1371 return (s32_t)ceiling_fraction(ms, ms_per_tick);
1372#endif
1373
1374#else
1375 __ASSERT(ms == 0, "ms not zero");
1376 return 0;
Benjamin Walsh62092182016-12-20 14:39:08 -05001377#endif
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001378}
Benjamin Walsh62092182016-12-20 14:39:08 -05001379
Kumar Galacc334c72017-04-21 10:55:34 -05001380static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001381{
Benjamin Walsh62092182016-12-20 14:39:08 -05001382#ifdef CONFIG_SYS_CLOCK_EXISTS
1383
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001384#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
1385 /* use 64-bit math to keep precision */
1386 return (u64_t)ticks * sys_clock_hw_cycles_per_tick * MSEC_PER_SEC /
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001387 sys_clock_hw_cycles_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001388#else
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001389 /* simple multiplication keeps precision */
1390 u32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
1391
1392 return (u64_t)ticks * ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001393#endif
1394
1395#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001396 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001397 return 0;
1398#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001399}
1400
Piotr Zięcik77f42f82018-06-11 14:26:29 +02001401/* added tick needed to account for tick in progress */
1402#ifdef CONFIG_TICKLESS_KERNEL
1403#define _TICK_ALIGN 0
1404#else
1405#define _TICK_ALIGN 1
1406#endif
1407
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001408struct k_timer {
1409 /*
1410 * _timeout structure must be first here if we want to use
1411 * dynamic timer allocation. timeout.node is used in the double-linked
1412 * list of free timers
1413 */
1414 struct _timeout timeout;
1415
Allan Stephens45bfa372016-10-12 12:39:42 -05001416 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001417 _wait_q_t wait_q;
1418
1419 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001420 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001421
1422 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001423 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001424
1425 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001426 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001427
Allan Stephens45bfa372016-10-12 12:39:42 -05001428 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001429 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001430
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001431 /* user-specific data, also used to support legacy features */
1432 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001433
Anas Nashif2f203c22016-12-18 06:57:45 -05001434 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001435};
1436
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001437#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001438 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001439 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001440 .timeout.wait_q = NULL, \
1441 .timeout.thread = NULL, \
1442 .timeout.func = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001443 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001444 .expiry_fn = expiry, \
1445 .stop_fn = stop, \
1446 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001447 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001448 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001449 }
1450
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001451#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1452
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001453/**
Allan Stephensc98da842016-11-11 15:45:03 -05001454 * INTERNAL_HIDDEN @endcond
1455 */
1456
1457/**
1458 * @defgroup timer_apis Timer APIs
1459 * @ingroup kernel_apis
1460 * @{
1461 */
1462
1463/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001464 * @typedef k_timer_expiry_t
1465 * @brief Timer expiry function type.
1466 *
1467 * A timer's expiry function is executed by the system clock interrupt handler
1468 * each time the timer expires. The expiry function is optional, and is only
1469 * invoked if the timer has been initialized with one.
1470 *
1471 * @param timer Address of timer.
1472 *
1473 * @return N/A
1474 */
1475typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1476
1477/**
1478 * @typedef k_timer_stop_t
1479 * @brief Timer stop function type.
1480 *
1481 * A timer's stop function is executed if the timer is stopped prematurely.
1482 * The function runs in the context of the thread that stops the timer.
1483 * The stop function is optional, and is only invoked if the timer has been
1484 * initialized with one.
1485 *
1486 * @param timer Address of timer.
1487 *
1488 * @return N/A
1489 */
1490typedef void (*k_timer_stop_t)(struct k_timer *timer);
1491
1492/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001493 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001495 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001496 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001497 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001498 *
1499 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001500 * @param expiry_fn Function to invoke each time the timer expires.
1501 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001502 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001503#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001504 struct k_timer name \
1505 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001506 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001507
Allan Stephens45bfa372016-10-12 12:39:42 -05001508/**
1509 * @brief Initialize a timer.
1510 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001511 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001512 *
1513 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001514 * @param expiry_fn Function to invoke each time the timer expires.
1515 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001516 *
1517 * @return N/A
1518 */
1519extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001520 k_timer_expiry_t expiry_fn,
1521 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001522
Allan Stephens45bfa372016-10-12 12:39:42 -05001523/**
1524 * @brief Start a timer.
1525 *
1526 * This routine starts a timer, and resets its status to zero. The timer
1527 * begins counting down using the specified duration and period values.
1528 *
1529 * Attempting to start a timer that is already running is permitted.
1530 * The timer's status is reset to zero and the timer begins counting down
1531 * using the new duration and period values.
1532 *
1533 * @param timer Address of timer.
1534 * @param duration Initial timer duration (in milliseconds).
1535 * @param period Timer period (in milliseconds).
1536 *
1537 * @return N/A
1538 */
Andrew Boiea354d492017-09-29 16:22:28 -07001539__syscall void k_timer_start(struct k_timer *timer,
1540 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001541
1542/**
1543 * @brief Stop a timer.
1544 *
1545 * This routine stops a running timer prematurely. The timer's stop function,
1546 * if one exists, is invoked by the caller.
1547 *
1548 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001549 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001550 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001551 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1552 * if @a k_timer_stop is to be called from ISRs.
1553 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001554 * @param timer Address of timer.
1555 *
1556 * @return N/A
1557 */
Andrew Boiea354d492017-09-29 16:22:28 -07001558__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001559
1560/**
1561 * @brief Read timer status.
1562 *
1563 * This routine reads the timer's status, which indicates the number of times
1564 * it has expired since its status was last read.
1565 *
1566 * Calling this routine resets the timer's status to zero.
1567 *
1568 * @param timer Address of timer.
1569 *
1570 * @return Timer status.
1571 */
Andrew Boiea354d492017-09-29 16:22:28 -07001572__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001573
1574/**
1575 * @brief Synchronize thread to timer expiration.
1576 *
1577 * This routine blocks the calling thread until the timer's status is non-zero
1578 * (indicating that it has expired at least once since it was last examined)
1579 * or the timer is stopped. If the timer status is already non-zero,
1580 * or the timer is already stopped, the caller continues without waiting.
1581 *
1582 * Calling this routine resets the timer's status to zero.
1583 *
1584 * This routine must not be used by interrupt handlers, since they are not
1585 * allowed to block.
1586 *
1587 * @param timer Address of timer.
1588 *
1589 * @return Timer status.
1590 */
Andrew Boiea354d492017-09-29 16:22:28 -07001591__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001592
1593/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001594 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001595 *
1596 * This routine computes the (approximate) time remaining before a running
1597 * timer next expires. If the timer is not running, it returns zero.
1598 *
1599 * @param timer Address of timer.
1600 *
1601 * @return Remaining time (in milliseconds).
1602 */
Andrew Boiea354d492017-09-29 16:22:28 -07001603__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1604
1605static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001606{
1607 return _timeout_remaining_get(&timer->timeout);
1608}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001609
Allan Stephensc98da842016-11-11 15:45:03 -05001610/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001611 * @brief Associate user-specific data with a timer.
1612 *
1613 * This routine records the @a user_data with the @a timer, to be retrieved
1614 * later.
1615 *
1616 * It can be used e.g. in a timer handler shared across multiple subsystems to
1617 * retrieve data specific to the subsystem this timer is associated with.
1618 *
1619 * @param timer Address of timer.
1620 * @param user_data User data to associate with the timer.
1621 *
1622 * @return N/A
1623 */
Andrew Boiea354d492017-09-29 16:22:28 -07001624__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1625
Anas Nashif954d5502018-02-25 08:37:28 -06001626/**
1627 * @internal
1628 */
Andrew Boiea354d492017-09-29 16:22:28 -07001629static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1630 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001631{
1632 timer->user_data = user_data;
1633}
1634
1635/**
1636 * @brief Retrieve the user-specific data from a timer.
1637 *
1638 * @param timer Address of timer.
1639 *
1640 * @return The user data.
1641 */
Andrew Boiea354d492017-09-29 16:22:28 -07001642__syscall void *k_timer_user_data_get(struct k_timer *timer);
1643
1644static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001645{
1646 return timer->user_data;
1647}
1648
Anas Nashif166f5192018-02-25 08:02:36 -06001649/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001650
Allan Stephensc98da842016-11-11 15:45:03 -05001651/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001652 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001653 * @{
1654 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001655
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001656/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001657 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001658 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001659 * This routine returns the elapsed time since the system booted,
1660 * in milliseconds.
1661 *
1662 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001663 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001664__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001665
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001666/**
1667 * @brief Enable clock always on in tickless kernel
1668 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001669 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001670 * there are no timer events programmed in tickless kernel
1671 * scheduling. This is necessary if the clock is used to track
1672 * passage of time.
1673 *
1674 * @retval prev_status Previous status of always on flag
1675 */
1676static inline int k_enable_sys_clock_always_on(void)
1677{
Flavio Ceolinf23a8cd2018-08-13 12:32:56 -07001678#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001679 int prev_status = _sys_clock_always_on;
1680
1681 _sys_clock_always_on = 1;
1682 _enable_sys_clock();
1683
1684 return prev_status;
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301685#else
Flavio Ceolinf23a8cd2018-08-13 12:32:56 -07001686 return -ENOTSUP;
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301687#endif
Flavio Ceolinf23a8cd2018-08-13 12:32:56 -07001688}
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001689
1690/**
1691 * @brief Disable clock always on in tickless kernel
1692 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001693 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001694 * there are no timer events programmed in tickless kernel
1695 * scheduling. To save power, this routine should be called
1696 * immediately when clock is not used to track time.
1697 */
1698static inline void k_disable_sys_clock_always_on(void)
1699{
Flavio Ceolinf23a8cd2018-08-13 12:32:56 -07001700#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001701 _sys_clock_always_on = 0;
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001702#endif
Flavio Ceolinf23a8cd2018-08-13 12:32:56 -07001703}
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001704
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001705/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001706 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001708 * This routine returns the lower 32-bits of the elapsed time since the system
1709 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001710 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001711 * This routine can be more efficient than k_uptime_get(), as it reduces the
1712 * need for interrupt locking and 64-bit math. However, the 32-bit result
1713 * cannot hold a system uptime time larger than approximately 50 days, so the
1714 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001716 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001717 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001718__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001719
1720/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001721 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001722 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001723 * This routine computes the elapsed time between the current system uptime
1724 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001725 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001726 * @param reftime Pointer to a reference time, which is updated to the current
1727 * uptime upon return.
1728 *
1729 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001730 */
Kumar Galacc334c72017-04-21 10:55:34 -05001731extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001732
1733/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001734 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001735 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001736 * This routine computes the elapsed time between the current system uptime
1737 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001738 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001739 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1740 * need for interrupt locking and 64-bit math. However, the 32-bit result
1741 * cannot hold an elapsed time larger than approximately 50 days, so the
1742 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001743 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001744 * @param reftime Pointer to a reference time, which is updated to the current
1745 * uptime upon return.
1746 *
1747 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001748 */
Kumar Galacc334c72017-04-21 10:55:34 -05001749extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001750
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001751/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001752 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001753 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001754 * This routine returns the current time, as measured by the system's hardware
1755 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001756 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001757 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001758 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001759#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001760
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001761/**
Anas Nashif166f5192018-02-25 08:02:36 -06001762 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001763 */
1764
Allan Stephensc98da842016-11-11 15:45:03 -05001765/**
1766 * @cond INTERNAL_HIDDEN
1767 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001768
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001769struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001770 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001771 union {
1772 _wait_q_t wait_q;
1773
1774 _POLL_EVENT;
1775 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001776
1777 _OBJECT_TRACING_NEXT_PTR(k_queue);
1778};
1779
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001780#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001781 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001782 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001783 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001784 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001785 _OBJECT_TRACING_INIT \
1786 }
1787
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001788#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1789
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001790extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1791
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001792/**
1793 * INTERNAL_HIDDEN @endcond
1794 */
1795
1796/**
1797 * @defgroup queue_apis Queue APIs
1798 * @ingroup kernel_apis
1799 * @{
1800 */
1801
1802/**
1803 * @brief Initialize a queue.
1804 *
1805 * This routine initializes a queue object, prior to its first use.
1806 *
1807 * @param queue Address of the queue.
1808 *
1809 * @return N/A
1810 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001811__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001812
1813/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001814 * @brief Cancel waiting on a queue.
1815 *
1816 * This routine causes first thread pending on @a queue, if any, to
1817 * return from k_queue_get() call with NULL value (as if timeout expired).
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03001818 * If the queue is being waited on by k_poll(), it will return with
1819 * -EINTR and K_POLL_STATE_CANCELLED state (and per above, subsequent
1820 * k_queue_get() will return NULL).
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001821 *
1822 * @note Can be called by ISRs.
1823 *
1824 * @param queue Address of the queue.
1825 *
1826 * @return N/A
1827 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001828__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001829
1830/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001831 * @brief Append an element to the end of a queue.
1832 *
1833 * This routine appends a data item to @a queue. A queue data item must be
1834 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1835 * reserved for the kernel's use.
1836 *
1837 * @note Can be called by ISRs.
1838 *
1839 * @param queue Address of the queue.
1840 * @param data Address of the data item.
1841 *
1842 * @return N/A
1843 */
1844extern void k_queue_append(struct k_queue *queue, void *data);
1845
1846/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001847 * @brief Append an element to a queue.
1848 *
1849 * This routine appends a data item to @a queue. There is an implicit
1850 * memory allocation from the calling thread's resource pool, which is
1851 * automatically freed when the item is removed from the queue.
1852 *
1853 * @note Can be called by ISRs.
1854 *
1855 * @param queue Address of the queue.
1856 * @param data Address of the data item.
1857 *
1858 * @retval 0 on success
1859 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1860 */
1861__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1862
1863/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001864 * @brief Prepend an element to a queue.
1865 *
1866 * This routine prepends a data item to @a queue. A queue data item must be
1867 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1868 * reserved for the kernel's use.
1869 *
1870 * @note Can be called by ISRs.
1871 *
1872 * @param queue Address of the queue.
1873 * @param data Address of the data item.
1874 *
1875 * @return N/A
1876 */
1877extern void k_queue_prepend(struct k_queue *queue, void *data);
1878
1879/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001880 * @brief Prepend an element to a queue.
1881 *
1882 * This routine prepends a data item to @a queue. There is an implicit
1883 * memory allocation from the calling thread's resource pool, which is
1884 * automatically freed when the item is removed from the queue.
1885 *
1886 * @note Can be called by ISRs.
1887 *
1888 * @param queue Address of the queue.
1889 * @param data Address of the data item.
1890 *
1891 * @retval 0 on success
1892 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1893 */
1894__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1895
1896/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001897 * @brief Inserts an element to a queue.
1898 *
1899 * This routine inserts a data item to @a queue after previous item. A queue
1900 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1901 * item are reserved for the kernel's use.
1902 *
1903 * @note Can be called by ISRs.
1904 *
1905 * @param queue Address of the queue.
1906 * @param prev Address of the previous data item.
1907 * @param data Address of the data item.
1908 *
1909 * @return N/A
1910 */
1911extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1912
1913/**
1914 * @brief Atomically append a list of elements to a queue.
1915 *
1916 * This routine adds a list of data items to @a queue in one operation.
1917 * The data items must be in a singly-linked list, with the first 32 bits
1918 * in each data item pointing to the next data item; the list must be
1919 * NULL-terminated.
1920 *
1921 * @note Can be called by ISRs.
1922 *
1923 * @param queue Address of the queue.
1924 * @param head Pointer to first node in singly-linked list.
1925 * @param tail Pointer to last node in singly-linked list.
1926 *
1927 * @return N/A
1928 */
1929extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1930
1931/**
1932 * @brief Atomically add a list of elements to a queue.
1933 *
1934 * This routine adds a list of data items to @a queue in one operation.
1935 * The data items must be in a singly-linked list implemented using a
1936 * sys_slist_t object. Upon completion, the original list is empty.
1937 *
1938 * @note Can be called by ISRs.
1939 *
1940 * @param queue Address of the queue.
1941 * @param list Pointer to sys_slist_t object.
1942 *
1943 * @return N/A
1944 */
1945extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1946
1947/**
1948 * @brief Get an element from a queue.
1949 *
1950 * This routine removes first data item from @a queue. The first 32 bits of the
1951 * data item are reserved for the kernel's use.
1952 *
1953 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1954 *
1955 * @param queue Address of the queue.
1956 * @param timeout Waiting period to obtain a data item (in milliseconds),
1957 * or one of the special values K_NO_WAIT and K_FOREVER.
1958 *
1959 * @return Address of the data item if successful; NULL if returned
1960 * without waiting, or waiting period timed out.
1961 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001962__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001963
1964/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001965 * @brief Remove an element from a queue.
1966 *
1967 * This routine removes data item from @a queue. The first 32 bits of the
1968 * data item are reserved for the kernel's use. Removing elements from k_queue
1969 * rely on sys_slist_find_and_remove which is not a constant time operation.
1970 *
1971 * @note Can be called by ISRs
1972 *
1973 * @param queue Address of the queue.
1974 * @param data Address of the data item.
1975 *
1976 * @return true if data item was removed
1977 */
1978static inline bool k_queue_remove(struct k_queue *queue, void *data)
1979{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001980 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001981}
1982
1983/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001984 * @brief Query a queue to see if it has data available.
1985 *
1986 * Note that the data might be already gone by the time this function returns
1987 * if other threads are also trying to read from the queue.
1988 *
1989 * @note Can be called by ISRs.
1990 *
1991 * @param queue Address of the queue.
1992 *
1993 * @return Non-zero if the queue is empty.
1994 * @return 0 if data is available.
1995 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001996__syscall int k_queue_is_empty(struct k_queue *queue);
1997
1998static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001999{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002000 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002001}
2002
2003/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002004 * @brief Peek element at the head of queue.
2005 *
2006 * Return element from the head of queue without removing it.
2007 *
2008 * @param queue Address of the queue.
2009 *
2010 * @return Head element, or NULL if queue is empty.
2011 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002012__syscall void *k_queue_peek_head(struct k_queue *queue);
2013
2014static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002015{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002016 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002017}
2018
2019/**
2020 * @brief Peek element at the tail of queue.
2021 *
2022 * Return element from the tail of queue without removing it.
2023 *
2024 * @param queue Address of the queue.
2025 *
2026 * @return Tail element, or NULL if queue is empty.
2027 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002028__syscall void *k_queue_peek_tail(struct k_queue *queue);
2029
2030static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002031{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002032 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002033}
2034
2035/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002036 * @brief Statically define and initialize a queue.
2037 *
2038 * The queue can be accessed outside the module where it is defined using:
2039 *
2040 * @code extern struct k_queue <name>; @endcode
2041 *
2042 * @param name Name of the queue.
2043 */
2044#define K_QUEUE_DEFINE(name) \
2045 struct k_queue name \
2046 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002047 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002048
Anas Nashif166f5192018-02-25 08:02:36 -06002049/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002050
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002051struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002052 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002053};
2054
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002055/**
2056 * @cond INTERNAL_HIDDEN
2057 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002058#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002059 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002060 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002061 }
2062
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002063#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
2064
Allan Stephensc98da842016-11-11 15:45:03 -05002065/**
2066 * INTERNAL_HIDDEN @endcond
2067 */
2068
2069/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002070 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002071 * @ingroup kernel_apis
2072 * @{
2073 */
2074
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002075/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002076 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002077 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002078 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002079 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002080 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002081 *
2082 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002083 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002084 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002085#define k_fifo_init(fifo) \
2086 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002087
2088/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002089 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002090 *
2091 * This routine causes first thread pending on @a fifo, if any, to
2092 * return from k_fifo_get() call with NULL value (as if timeout
2093 * expired).
2094 *
2095 * @note Can be called by ISRs.
2096 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002097 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002098 *
2099 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002100 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002101 */
2102#define k_fifo_cancel_wait(fifo) \
2103 k_queue_cancel_wait((struct k_queue *) fifo)
2104
2105/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002106 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002107 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002108 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002109 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2110 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002112 * @note Can be called by ISRs.
2113 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002114 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002115 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002116 *
2117 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002118 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002119 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002120#define k_fifo_put(fifo, data) \
2121 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002122
2123/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002124 * @brief Add an element to a FIFO queue.
2125 *
2126 * This routine adds a data item to @a fifo. There is an implicit
2127 * memory allocation from the calling thread's resource pool, which is
2128 * automatically freed when the item is removed.
2129 *
2130 * @note Can be called by ISRs.
2131 *
2132 * @param fifo Address of the FIFO.
2133 * @param data Address of the data item.
2134 *
2135 * @retval 0 on success
2136 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002137 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002138 */
2139#define k_fifo_alloc_put(fifo, data) \
2140 k_queue_alloc_append((struct k_queue *) fifo, data)
2141
2142/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002143 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002144 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002145 * This routine adds a list of data items to @a fifo in one operation.
2146 * The data items must be in a singly-linked list, with the first 32 bits
2147 * each data item pointing to the next data item; the list must be
2148 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002149 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002150 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002151 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002152 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002153 * @param head Pointer to first node in singly-linked list.
2154 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002155 *
2156 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002157 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002158 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002159#define k_fifo_put_list(fifo, head, tail) \
2160 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002161
2162/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002163 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002165 * This routine adds a list of data items to @a fifo in one operation.
2166 * The data items must be in a singly-linked list implemented using a
2167 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002168 * and must be re-initialized via sys_slist_init().
2169 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002170 * @note Can be called by ISRs.
2171 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002172 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002173 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002174 *
2175 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002176 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002177 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002178#define k_fifo_put_slist(fifo, list) \
2179 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002180
2181/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002182 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002183 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002184 * This routine removes a data item from @a fifo in a "first in, first out"
2185 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002186 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002187 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2188 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002189 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002190 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002191 * or one of the special values K_NO_WAIT and K_FOREVER.
2192 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002193 * @return Address of the data item if successful; NULL if returned
2194 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002195 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002196 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002197#define k_fifo_get(fifo, timeout) \
2198 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002199
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002200/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002201 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002202 *
2203 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002204 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002205 *
2206 * @note Can be called by ISRs.
2207 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002208 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002209 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002210 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002211 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002212 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002213 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002214#define k_fifo_is_empty(fifo) \
2215 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002216
2217/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002218 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002219 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002220 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302221 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002222 * on each iteration of processing, a head container will be peeked,
2223 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002224 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002225 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002226 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002227 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002228 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002229 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002230 */
2231#define k_fifo_peek_head(fifo) \
2232 k_queue_peek_head((struct k_queue *) fifo)
2233
2234/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002235 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002236 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002237 * Return element from the tail of FIFO queue (without removing it). A usecase
2238 * for this is if elements of the FIFO queue are themselves containers. Then
2239 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002240 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002241 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002242 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002243 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002244 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002245 */
2246#define k_fifo_peek_tail(fifo) \
2247 k_queue_peek_tail((struct k_queue *) fifo)
2248
2249/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002250 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002251 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002252 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002253 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002254 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002255 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002256 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002257 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002258 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002259#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002260 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002261 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002262 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002263
Anas Nashif166f5192018-02-25 08:02:36 -06002264/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002265
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002266struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002267 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002268};
2269
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002270/**
2271 * @cond INTERNAL_HIDDEN
2272 */
2273
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002274#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002275 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002276 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002277 }
2278
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002279#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2280
Allan Stephensc98da842016-11-11 15:45:03 -05002281/**
2282 * INTERNAL_HIDDEN @endcond
2283 */
2284
2285/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002286 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002287 * @ingroup kernel_apis
2288 * @{
2289 */
2290
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002291/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002292 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002293 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002294 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002295 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002296 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002297 *
2298 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002299 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002300 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002301#define k_lifo_init(lifo) \
2302 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002303
2304/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002305 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002306 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002307 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002308 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2309 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002310 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002311 * @note Can be called by ISRs.
2312 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002313 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002314 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002315 *
2316 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002317 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002318 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002319#define k_lifo_put(lifo, data) \
2320 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002321
2322/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002323 * @brief Add an element to a LIFO queue.
2324 *
2325 * This routine adds a data item to @a lifo. There is an implicit
2326 * memory allocation from the calling thread's resource pool, which is
2327 * automatically freed when the item is removed.
2328 *
2329 * @note Can be called by ISRs.
2330 *
2331 * @param lifo Address of the LIFO.
2332 * @param data Address of the data item.
2333 *
2334 * @retval 0 on success
2335 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002336 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002337 */
2338#define k_lifo_alloc_put(lifo, data) \
2339 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2340
2341/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002342 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002343 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002344 * This routine removes a data item from @a lifo in a "last in, first out"
2345 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002346 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002347 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2348 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002349 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002350 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002351 * or one of the special values K_NO_WAIT and K_FOREVER.
2352 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002353 * @return Address of the data item if successful; NULL if returned
2354 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002355 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002356 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002357#define k_lifo_get(lifo, timeout) \
2358 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002359
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002360/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002361 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002362 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002363 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002364 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002365 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002367 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002368 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002369 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002370#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002371 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002372 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002373 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002374
Anas Nashif166f5192018-02-25 08:02:36 -06002375/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002376
2377/**
2378 * @cond INTERNAL_HIDDEN
2379 */
Andrew Boief3bee952018-05-02 17:44:39 -07002380#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002381
2382struct k_stack {
2383 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002384 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002385
Anas Nashif2f203c22016-12-18 06:57:45 -05002386 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002387 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002388};
2389
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002390#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002391 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002392 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002393 .base = stack_buffer, \
2394 .next = stack_buffer, \
2395 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002396 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002397 }
2398
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002399#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2400
Allan Stephensc98da842016-11-11 15:45:03 -05002401/**
2402 * INTERNAL_HIDDEN @endcond
2403 */
2404
2405/**
2406 * @defgroup stack_apis Stack APIs
2407 * @ingroup kernel_apis
2408 * @{
2409 */
2410
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002411/**
2412 * @brief Initialize a stack.
2413 *
2414 * This routine initializes a stack object, prior to its first use.
2415 *
2416 * @param stack Address of the stack.
2417 * @param buffer Address of array used to hold stacked values.
2418 * @param num_entries Maximum number of values that can be stacked.
2419 *
2420 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002421 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002422 */
Andrew Boief3bee952018-05-02 17:44:39 -07002423void k_stack_init(struct k_stack *stack,
2424 u32_t *buffer, unsigned int num_entries);
2425
2426
2427/**
2428 * @brief Initialize a stack.
2429 *
2430 * This routine initializes a stack object, prior to its first use. Internal
2431 * buffers will be allocated from the calling thread's resource pool.
2432 * This memory will be released if k_stack_cleanup() is called, or
2433 * userspace is enabled and the stack object loses all references to it.
2434 *
2435 * @param stack Address of the stack.
2436 * @param num_entries Maximum number of values that can be stacked.
2437 *
2438 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002439 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002440 */
2441
2442__syscall int k_stack_alloc_init(struct k_stack *stack,
2443 unsigned int num_entries);
2444
2445/**
2446 * @brief Release a stack's allocated buffer
2447 *
2448 * If a stack object was given a dynamically allocated buffer via
2449 * k_stack_alloc_init(), this will free it. This function does nothing
2450 * if the buffer wasn't dynamically allocated.
2451 *
2452 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002453 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002454 */
2455void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002456
2457/**
2458 * @brief Push an element onto a stack.
2459 *
2460 * This routine adds a 32-bit value @a data to @a stack.
2461 *
2462 * @note Can be called by ISRs.
2463 *
2464 * @param stack Address of the stack.
2465 * @param data Value to push onto the stack.
2466 *
2467 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002468 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002469 */
Andrew Boiee8734462017-09-29 16:42:07 -07002470__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002471
2472/**
2473 * @brief Pop an element from a stack.
2474 *
2475 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2476 * manner and stores the value in @a data.
2477 *
2478 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2479 *
2480 * @param stack Address of the stack.
2481 * @param data Address of area to hold the value popped from the stack.
2482 * @param timeout Waiting period to obtain a value (in milliseconds),
2483 * or one of the special values K_NO_WAIT and K_FOREVER.
2484 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002485 * @retval 0 Element popped from stack.
2486 * @retval -EBUSY Returned without waiting.
2487 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002488 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002489 */
Andrew Boiee8734462017-09-29 16:42:07 -07002490__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002491
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002492/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002493 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002495 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002496 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002497 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002499 * @param name Name of the stack.
2500 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002501 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002502 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002503#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002504 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002505 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002506 struct k_stack name \
2507 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002508 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002509 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002510
Anas Nashif166f5192018-02-25 08:02:36 -06002511/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002512
Allan Stephens6bba9b02016-11-16 14:56:54 -05002513struct k_work;
2514
Allan Stephensc98da842016-11-11 15:45:03 -05002515/**
2516 * @defgroup workqueue_apis Workqueue Thread APIs
2517 * @ingroup kernel_apis
2518 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002519 */
2520
Allan Stephens6bba9b02016-11-16 14:56:54 -05002521/**
2522 * @typedef k_work_handler_t
2523 * @brief Work item handler function type.
2524 *
2525 * A work item's handler function is executed by a workqueue's thread
2526 * when the work item is processed by the workqueue.
2527 *
2528 * @param work Address of the work item.
2529 *
2530 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002531 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002532 */
2533typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002534
2535/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002536 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002537 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002538
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002539struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002540 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002541 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002542};
2543
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002544enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002545 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002546};
2547
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002548struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002549 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002550 k_work_handler_t handler;
2551 atomic_t flags[1];
2552};
2553
Allan Stephens6bba9b02016-11-16 14:56:54 -05002554struct k_delayed_work {
2555 struct k_work work;
2556 struct _timeout timeout;
2557 struct k_work_q *work_q;
2558};
2559
2560extern struct k_work_q k_sys_work_q;
2561
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002562/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002563 * INTERNAL_HIDDEN @endcond
2564 */
2565
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002566#define _K_WORK_INITIALIZER(work_handler) \
2567 { \
2568 ._reserved = NULL, \
2569 .handler = work_handler, \
2570 .flags = { 0 } \
2571 }
2572
2573#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2574
Allan Stephens6bba9b02016-11-16 14:56:54 -05002575/**
2576 * @brief Initialize a statically-defined work item.
2577 *
2578 * This macro can be used to initialize a statically-defined workqueue work
2579 * item, prior to its first use. For example,
2580 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002581 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002582 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002583 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002584 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002585 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002586 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002587#define K_WORK_DEFINE(work, work_handler) \
2588 struct k_work work \
2589 __in_section(_k_work, static, work) = \
2590 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002591
2592/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002593 * @brief Initialize a work item.
2594 *
2595 * This routine initializes a workqueue work item, prior to its first use.
2596 *
2597 * @param work Address of work item.
2598 * @param handler Function to invoke each time work item is processed.
2599 *
2600 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002601 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002602 */
2603static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2604{
Leandro Pereira0e23ad82018-05-29 10:42:17 -07002605 *work = (struct k_work)_K_WORK_INITIALIZER(handler);
Andrew Boie945af952017-08-22 13:15:23 -07002606 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002607}
2608
2609/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002610 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002611 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002612 * This routine submits work item @a work to be processed by workqueue
2613 * @a work_q. If the work item is already pending in the workqueue's queue
2614 * as a result of an earlier submission, this routine has no effect on the
2615 * work item. If the work item has already been processed, or is currently
2616 * being processed, its work is considered complete and the work item can be
2617 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002618 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002619 * @warning
2620 * A submitted work item must not be modified until it has been processed
2621 * by the workqueue.
2622 *
2623 * @note Can be called by ISRs.
2624 *
2625 * @param work_q Address of workqueue.
2626 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002627 *
2628 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002629 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002630 */
2631static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2632 struct k_work *work)
2633{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002634 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002635 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002636 }
2637}
2638
2639/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002640 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002641 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002642 * This routine indicates if work item @a work is pending in a workqueue's
2643 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002644 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002645 * @note Can be called by ISRs.
2646 *
2647 * @param work Address of work item.
2648 *
2649 * @return 1 if work item is pending, or 0 if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002650 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002651 */
2652static inline int k_work_pending(struct k_work *work)
2653{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002654 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002655}
2656
2657/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002658 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002659 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002660 * This routine starts workqueue @a work_q. The workqueue spawns its work
2661 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002662 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002663 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002664 * @param stack Pointer to work queue thread's stack space, as defined by
2665 * K_THREAD_STACK_DEFINE()
2666 * @param stack_size Size of the work queue thread's stack (in bytes), which
2667 * should either be the same constant passed to
2668 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002669 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002670 *
2671 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002672 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002673 */
Andrew Boie507852a2017-07-25 18:47:07 -07002674extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002675 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002676 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002677
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002678/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002679 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002680 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002681 * This routine initializes a workqueue delayed work item, prior to
2682 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002683 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002684 * @param work Address of delayed work item.
2685 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002686 *
2687 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002688 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002689 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002690extern void k_delayed_work_init(struct k_delayed_work *work,
2691 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002692
2693/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002694 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002695 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002696 * This routine schedules work item @a work to be processed by workqueue
2697 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002698 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002699 * Only when the countdown completes is the work item actually submitted to
2700 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002701 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002702 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002703 * counting down cancels the existing submission and restarts the
2704 * countdown using the new delay. Note that this behavior is
2705 * inherently subject to race conditions with the pre-existing
2706 * timeouts and work queue, so care must be taken to synchronize such
2707 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002708 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002709 * @warning
2710 * A delayed work item must not be modified until it has been processed
2711 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002712 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002713 * @note Can be called by ISRs.
2714 *
2715 * @param work_q Address of workqueue.
2716 * @param work Address of delayed work item.
2717 * @param delay Delay before submitting the work item (in milliseconds).
2718 *
2719 * @retval 0 Work item countdown started.
2720 * @retval -EINPROGRESS Work item is already pending.
2721 * @retval -EINVAL Work item is being processed or has completed its work.
2722 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002723 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002724 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002725extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2726 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002727 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002728
2729/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002730 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002731 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002732 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002733 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002734 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002735 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002736 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002737 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002738 * @param work Address of delayed work item.
2739 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002740 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002741 * @retval -EINPROGRESS Work item is already pending.
2742 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002743 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002744 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002745extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002746
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002747/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002748 * @brief Submit a work item to the system workqueue.
2749 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002750 * This routine submits work item @a work to be processed by the system
2751 * workqueue. If the work item is already pending in the workqueue's queue
2752 * as a result of an earlier submission, this routine has no effect on the
2753 * work item. If the work item has already been processed, or is currently
2754 * being processed, its work is considered complete and the work item can be
2755 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002756 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002757 * @warning
2758 * Work items submitted to the system workqueue should avoid using handlers
2759 * that block or yield since this may prevent the system workqueue from
2760 * processing other work items in a timely manner.
2761 *
2762 * @note Can be called by ISRs.
2763 *
2764 * @param work Address of work item.
2765 *
2766 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002767 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002768 */
2769static inline void k_work_submit(struct k_work *work)
2770{
2771 k_work_submit_to_queue(&k_sys_work_q, work);
2772}
2773
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002774/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002775 * @brief Submit a delayed work item to the system workqueue.
2776 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002777 * This routine schedules work item @a work to be processed by the system
2778 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002779 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002780 * Only when the countdown completes is the work item actually submitted to
2781 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002782 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002783 * Submitting a previously submitted delayed work item that is still
2784 * counting down cancels the existing submission and restarts the countdown
2785 * using the new delay. If the work item is currently pending on the
2786 * workqueue's queue because the countdown has completed it is too late to
2787 * resubmit the item, and resubmission fails without impacting the work item.
2788 * If the work item has already been processed, or is currently being processed,
2789 * its work is considered complete and the work item can be resubmitted.
2790 *
2791 * @warning
2792 * Work items submitted to the system workqueue should avoid using handlers
2793 * that block or yield since this may prevent the system workqueue from
2794 * processing other work items in a timely manner.
2795 *
2796 * @note Can be called by ISRs.
2797 *
2798 * @param work Address of delayed work item.
2799 * @param delay Delay before submitting the work item (in milliseconds).
2800 *
2801 * @retval 0 Work item countdown started.
2802 * @retval -EINPROGRESS Work item is already pending.
2803 * @retval -EINVAL Work item is being processed or has completed its work.
2804 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002805 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002806 */
2807static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002808 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002809{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002810 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002811}
2812
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002813/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002814 * @brief Get time remaining before a delayed work gets scheduled.
2815 *
2816 * This routine computes the (approximate) time remaining before a
2817 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002818 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002819 *
2820 * @param work Delayed work item.
2821 *
2822 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002823 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002824 */
Kumar Galacc334c72017-04-21 10:55:34 -05002825static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002826{
2827 return _timeout_remaining_get(&work->timeout);
2828}
2829
Anas Nashif166f5192018-02-25 08:02:36 -06002830/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002831/**
Anas Nashifce78d162018-05-24 12:43:11 -05002832 * @defgroup mutex_apis Mutex APIs
2833 * @ingroup kernel_apis
2834 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002835 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002836
Anas Nashifce78d162018-05-24 12:43:11 -05002837/**
2838 * Mutex Structure
2839 * @ingroup mutex_apis
2840 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002841struct k_mutex {
2842 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002843 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002844 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002845 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002846 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002847
Anas Nashif2f203c22016-12-18 06:57:45 -05002848 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002849};
2850
Anas Nashifce78d162018-05-24 12:43:11 -05002851/**
2852 * @cond INTERNAL_HIDDEN
2853 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002854#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002855 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002856 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002857 .owner = NULL, \
2858 .lock_count = 0, \
2859 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002860 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002861 }
2862
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002863#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2864
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002865/**
Allan Stephensc98da842016-11-11 15:45:03 -05002866 * INTERNAL_HIDDEN @endcond
2867 */
2868
2869/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002870 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002872 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002873 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002874 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002876 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002877 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002878 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002879#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002880 struct k_mutex name \
2881 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002882 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002883
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002884/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002885 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002887 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002888 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002889 * Upon completion, the mutex is available and does not have an owner.
2890 *
2891 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002892 *
2893 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002894 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002895 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002896__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002897
2898/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002899 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002900 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002901 * This routine locks @a mutex. If the mutex is locked by another thread,
2902 * the calling thread waits until the mutex becomes available or until
2903 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002905 * A thread is permitted to lock a mutex it has already locked. The operation
2906 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002907 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002908 * @param mutex Address of the mutex.
2909 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002910 * or one of the special values K_NO_WAIT and K_FOREVER.
2911 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002912 * @retval 0 Mutex locked.
2913 * @retval -EBUSY Returned without waiting.
2914 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002915 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002916 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002917__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002918
2919/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002920 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002921 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002922 * This routine unlocks @a mutex. The mutex must already be locked by the
2923 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002924 *
2925 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002926 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002927 * thread.
2928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002929 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002930 *
2931 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002932 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002933 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002934__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002935
Allan Stephensc98da842016-11-11 15:45:03 -05002936/**
Anas Nashif166f5192018-02-25 08:02:36 -06002937 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002938 */
2939
2940/**
2941 * @cond INTERNAL_HIDDEN
2942 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002943
2944struct k_sem {
2945 _wait_q_t wait_q;
2946 unsigned int count;
2947 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002948 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002949
Anas Nashif2f203c22016-12-18 06:57:45 -05002950 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002951};
2952
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002953#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002954 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002955 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002956 .count = initial_count, \
2957 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002958 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002959 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002960 }
2961
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002962#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2963
Allan Stephensc98da842016-11-11 15:45:03 -05002964/**
2965 * INTERNAL_HIDDEN @endcond
2966 */
2967
2968/**
2969 * @defgroup semaphore_apis Semaphore APIs
2970 * @ingroup kernel_apis
2971 * @{
2972 */
2973
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002974/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002975 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002977 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002979 * @param sem Address of the semaphore.
2980 * @param initial_count Initial semaphore count.
2981 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002982 *
2983 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002984 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002985 */
Andrew Boie99280232017-09-29 14:17:47 -07002986__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2987 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002988
2989/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002990 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002991 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002992 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002994 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2995 *
2996 * @param sem Address of the semaphore.
2997 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002998 * or one of the special values K_NO_WAIT and K_FOREVER.
2999 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05003000 * @note When porting code from the nanokernel legacy API to the new API, be
3001 * careful with the return value of this function. The return value is the
3002 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
3003 * non-zero means failure, while the nano_sem_take family returns 1 for success
3004 * and 0 for failure.
3005 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003006 * @retval 0 Semaphore taken.
3007 * @retval -EBUSY Returned without waiting.
3008 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003009 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003010 */
Andrew Boie99280232017-09-29 14:17:47 -07003011__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003012
3013/**
3014 * @brief Give a semaphore.
3015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003016 * This routine gives @a sem, unless the semaphore is already at its maximum
3017 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003019 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003021 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003022 *
3023 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003024 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003025 */
Andrew Boie99280232017-09-29 14:17:47 -07003026__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003027
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003028/**
3029 * @brief Reset a semaphore's count to zero.
3030 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003031 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003033 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003034 *
3035 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003036 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003037 */
Andrew Boie990bf162017-10-03 12:36:49 -07003038__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003039
Anas Nashif954d5502018-02-25 08:37:28 -06003040/**
3041 * @internal
3042 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003043static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003044{
3045 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003046}
3047
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003048/**
3049 * @brief Get a semaphore's count.
3050 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003051 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003052 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003053 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003055 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003056 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003057 */
Andrew Boie990bf162017-10-03 12:36:49 -07003058__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003059
Anas Nashif954d5502018-02-25 08:37:28 -06003060/**
3061 * @internal
3062 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003063static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003064{
3065 return sem->count;
3066}
3067
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003068/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003069 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003070 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003071 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003072 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003073 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003075 * @param name Name of the semaphore.
3076 * @param initial_count Initial semaphore count.
3077 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003078 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003079 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003080#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003081 struct k_sem name \
3082 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003083 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303084 BUILD_ASSERT(((count_limit) != 0) && \
3085 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003086
Anas Nashif166f5192018-02-25 08:02:36 -06003087/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003088
3089/**
3090 * @defgroup alert_apis Alert APIs
3091 * @ingroup kernel_apis
3092 * @{
3093 */
3094
Allan Stephens5eceb852016-11-16 10:16:30 -05003095/**
3096 * @typedef k_alert_handler_t
3097 * @brief Alert handler function type.
3098 *
3099 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07003100 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05003101 * and is only invoked if the alert has been initialized with one.
3102 *
3103 * @param alert Address of the alert.
3104 *
3105 * @return 0 if alert has been consumed; non-zero if alert should pend.
3106 */
3107typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05003108
Anas Nashif166f5192018-02-25 08:02:36 -06003109/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003110
3111/**
3112 * @cond INTERNAL_HIDDEN
3113 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003114
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003115#define K_ALERT_DEFAULT NULL
3116#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003117
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003118struct k_alert {
3119 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003120 atomic_t send_count;
3121 struct k_work work_item;
3122 struct k_sem sem;
3123
Anas Nashif2f203c22016-12-18 06:57:45 -05003124 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003125};
3126
Anas Nashif954d5502018-02-25 08:37:28 -06003127/**
3128 * @internal
3129 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003130extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003131
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003132#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003133 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003134 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003135 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003136 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
3137 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003138 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003139 }
3140
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003141#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
3142
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003143/**
Allan Stephensc98da842016-11-11 15:45:03 -05003144 * INTERNAL_HIDDEN @endcond
3145 */
3146
3147/**
3148 * @addtogroup alert_apis
3149 * @{
3150 */
3151
3152/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003153 * @def K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts)
3154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003155 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003156 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003157 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003158 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003159 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003160 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003161 * @param name Name of the alert.
3162 * @param alert_handler Action to take when alert is sent. Specify either
3163 * the address of a function to be invoked by the system workqueue
3164 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
3165 * K_ALERT_DEFAULT (which causes the alert to pend).
3166 * @param max_num_pending_alerts Maximum number of pending alerts.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003167 *
3168 * @req K-ALERT-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003169 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003170#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003171 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003172 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003173 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003174 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003175
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003176/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003177 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003179 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003181 * @param alert Address of the alert.
3182 * @param handler Action to take when alert is sent. Specify either the address
3183 * of a function to be invoked by the system workqueue thread,
3184 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3185 * K_ALERT_DEFAULT (which causes the alert to pend).
3186 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003187 *
3188 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003189 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003190 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003191extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3192 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003193
3194/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003195 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003197 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003199 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3200 *
3201 * @param alert Address of the alert.
3202 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003203 * or one of the special values K_NO_WAIT and K_FOREVER.
3204 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003205 * @retval 0 Alert received.
3206 * @retval -EBUSY Returned without waiting.
3207 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003208 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003209 */
Andrew Boie310e9872017-09-29 04:41:15 -07003210__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003211
3212/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003213 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003215 * This routine signals @a alert. The action specified for @a alert will
3216 * be taken, which may trigger the execution of an alert handler function
3217 * and/or cause the alert to pend (assuming the alert has not reached its
3218 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003219 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003220 * @note Can be called by ISRs.
3221 *
3222 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003223 *
3224 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003225 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003226 */
Andrew Boie310e9872017-09-29 04:41:15 -07003227__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003228
3229/**
Anas Nashif166f5192018-02-25 08:02:36 -06003230 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003231 */
3232
Allan Stephensc98da842016-11-11 15:45:03 -05003233/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003234 * @defgroup msgq_apis Message Queue APIs
3235 * @ingroup kernel_apis
3236 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003237 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003238
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003239/**
3240 * @brief Message Queue Structure
3241 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003242struct k_msgq {
3243 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003244 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003245 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003246 char *buffer_start;
3247 char *buffer_end;
3248 char *read_ptr;
3249 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003250 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003251
Anas Nashif2f203c22016-12-18 06:57:45 -05003252 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003253 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003254};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003255/**
3256 * @cond INTERNAL_HIDDEN
3257 */
3258
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003259
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003260#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003261 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003262 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003263 .max_msgs = q_max_msgs, \
3264 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003265 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003266 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003267 .read_ptr = q_buffer, \
3268 .write_ptr = q_buffer, \
3269 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003270 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003271 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003272#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003273/**
3274 * INTERNAL_HIDDEN @endcond
3275 */
3276
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003277
Andrew Boie0fe789f2018-04-12 18:35:56 -07003278#define K_MSGQ_FLAG_ALLOC BIT(0)
3279
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003280/**
3281 * @brief Message Queue Attributes
3282 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303283struct k_msgq_attrs {
3284 size_t msg_size;
3285 u32_t max_msgs;
3286 u32_t used_msgs;
3287};
3288
Allan Stephensc98da842016-11-11 15:45:03 -05003289
3290/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003291 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003293 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3294 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003295 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3296 * message is similarly aligned to this boundary, @a q_msg_size must also be
3297 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003298 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003299 * The message queue can be accessed outside the module where it is defined
3300 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003301 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003302 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003303 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003304 * @param q_name Name of the message queue.
3305 * @param q_msg_size Message size (in bytes).
3306 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003307 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003308 *
3309 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003310 */
3311#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003312 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003313 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003314 struct k_msgq q_name \
3315 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003316 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003317 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003318
Peter Mitsisd7a37502016-10-13 11:37:40 -04003319/**
3320 * @brief Initialize a message queue.
3321 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003322 * This routine initializes a message queue object, prior to its first use.
3323 *
Allan Stephensda827222016-11-09 14:23:58 -06003324 * The message queue's ring buffer must contain space for @a max_msgs messages,
3325 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3326 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3327 * that each message is similarly aligned to this boundary, @a q_msg_size
3328 * must also be a multiple of N.
3329 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003330 * @param q Address of the message queue.
3331 * @param buffer Pointer to ring buffer that holds queued messages.
3332 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003333 * @param max_msgs Maximum number of messages that can be queued.
3334 *
3335 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003336 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003337 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003338void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3339 u32_t max_msgs);
3340
3341/**
3342 * @brief Initialize a message queue.
3343 *
3344 * This routine initializes a message queue object, prior to its first use,
3345 * allocating its internal ring buffer from the calling thread's resource
3346 * pool.
3347 *
3348 * Memory allocated for the ring buffer can be released by calling
3349 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3350 * all of its references.
3351 *
3352 * @param q Address of the message queue.
3353 * @param msg_size Message size (in bytes).
3354 * @param max_msgs Maximum number of messages that can be queued.
3355 *
3356 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3357 * thread's resource pool, or -EINVAL if the size parameters cause
3358 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003359 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003360 */
3361__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3362 u32_t max_msgs);
3363
3364
3365void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003366
3367/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003368 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003369 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003370 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003371 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003372 * @note Can be called by ISRs.
3373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003374 * @param q Address of the message queue.
3375 * @param data Pointer to the message.
3376 * @param timeout Waiting period to add the message (in milliseconds),
3377 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003378 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003379 * @retval 0 Message sent.
3380 * @retval -ENOMSG Returned without waiting or queue purged.
3381 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003382 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003383 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003384__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003385
3386/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003387 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003389 * This routine receives a message from message queue @a q in a "first in,
3390 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003391 *
Allan Stephensc98da842016-11-11 15:45:03 -05003392 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003393 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003394 * @param q Address of the message queue.
3395 * @param data Address of area to hold the received message.
3396 * @param timeout Waiting period to receive the message (in milliseconds),
3397 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003398 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003399 * @retval 0 Message received.
3400 * @retval -ENOMSG Returned without waiting.
3401 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003402 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003403 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003404__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003405
3406/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003407 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003409 * This routine discards all unreceived messages in a message queue's ring
3410 * buffer. Any threads that are blocked waiting to send a message to the
3411 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003412 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003413 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003414 *
3415 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003416 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003417 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003418__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003419
Peter Mitsis67be2492016-10-07 11:44:34 -04003420/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003421 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003422 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003423 * This routine returns the number of unused entries in a message queue's
3424 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003426 * @param q Address of the message queue.
3427 *
3428 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003429 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003430 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003431__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3432
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303433/**
3434 * @brief Get basic attributes of a message queue.
3435 *
3436 * This routine fetches basic attributes of message queue into attr argument.
3437 *
3438 * @param q Address of the message queue.
3439 * @param attrs pointer to message queue attribute structure.
3440 *
3441 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003442 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303443 */
3444__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3445
3446
Andrew Boie82edb6e2017-10-02 10:53:06 -07003447static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003448{
3449 return q->max_msgs - q->used_msgs;
3450}
3451
Peter Mitsisd7a37502016-10-13 11:37:40 -04003452/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003453 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003454 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003455 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003456 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003457 * @param q Address of the message queue.
3458 *
3459 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003460 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003461 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003462__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3463
3464static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003465{
3466 return q->used_msgs;
3467}
3468
Anas Nashif166f5192018-02-25 08:02:36 -06003469/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003470
3471/**
3472 * @defgroup mem_pool_apis Memory Pool APIs
3473 * @ingroup kernel_apis
3474 * @{
3475 */
3476
Andy Ross73cb9582017-05-09 10:42:39 -07003477/* Note on sizing: the use of a 20 bit field for block means that,
3478 * assuming a reasonable minimum block size of 16 bytes, we're limited
3479 * to 16M of memory managed by a single pool. Long term it would be
3480 * good to move to a variable bit size based on configuration.
3481 */
3482struct k_mem_block_id {
3483 u32_t pool : 8;
3484 u32_t level : 4;
3485 u32_t block : 20;
3486};
3487
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003488struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003489 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003490 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003491};
3492
Anas Nashif166f5192018-02-25 08:02:36 -06003493/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003494
3495/**
3496 * @defgroup mailbox_apis Mailbox APIs
3497 * @ingroup kernel_apis
3498 * @{
3499 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003500
3501struct k_mbox_msg {
3502 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003503 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003504 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003505 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003506 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003507 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003508 /** sender's message data buffer */
3509 void *tx_data;
3510 /** internal use only - needed for legacy API support */
3511 void *_rx_data;
3512 /** message data block descriptor */
3513 struct k_mem_block tx_block;
3514 /** source thread id */
3515 k_tid_t rx_source_thread;
3516 /** target thread id */
3517 k_tid_t tx_target_thread;
3518 /** internal use only - thread waiting on send (may be a dummy) */
3519 k_tid_t _syncing_thread;
3520#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3521 /** internal use only - semaphore used during asynchronous send */
3522 struct k_sem *_async_sem;
3523#endif
3524};
3525
3526struct k_mbox {
3527 _wait_q_t tx_msg_queue;
3528 _wait_q_t rx_msg_queue;
3529
Anas Nashif2f203c22016-12-18 06:57:45 -05003530 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003531};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003532/**
3533 * @cond INTERNAL_HIDDEN
3534 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003535
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003536#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003537 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003538 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3539 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003540 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003541 }
3542
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003543#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3544
Peter Mitsis12092702016-10-14 12:57:23 -04003545/**
Allan Stephensc98da842016-11-11 15:45:03 -05003546 * INTERNAL_HIDDEN @endcond
3547 */
3548
3549/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003550 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003551 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003552 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003553 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003554 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003555 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003556 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003557 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003558 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003559#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003560 struct k_mbox name \
3561 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003562 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003563
Peter Mitsis12092702016-10-14 12:57:23 -04003564/**
3565 * @brief Initialize a mailbox.
3566 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003567 * This routine initializes a mailbox object, prior to its first use.
3568 *
3569 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003570 *
3571 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003572 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003573 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003574extern void k_mbox_init(struct k_mbox *mbox);
3575
Peter Mitsis12092702016-10-14 12:57:23 -04003576/**
3577 * @brief Send a mailbox message in a synchronous manner.
3578 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003579 * This routine sends a message to @a mbox and waits for a receiver to both
3580 * receive and process it. The message data may be in a buffer, in a memory
3581 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003583 * @param mbox Address of the mailbox.
3584 * @param tx_msg Address of the transmit message descriptor.
3585 * @param timeout Waiting period for the message to be received (in
3586 * milliseconds), or one of the special values K_NO_WAIT
3587 * and K_FOREVER. Once the message has been received,
3588 * this routine waits as long as necessary for the message
3589 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003590 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003591 * @retval 0 Message sent.
3592 * @retval -ENOMSG Returned without waiting.
3593 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003594 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003595 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003596extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003597 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003598
Peter Mitsis12092702016-10-14 12:57:23 -04003599/**
3600 * @brief Send a mailbox message in an asynchronous manner.
3601 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003602 * This routine sends a message to @a mbox without waiting for a receiver
3603 * to process it. The message data may be in a buffer, in a memory pool block,
3604 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3605 * will be given when the message has been both received and completely
3606 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003608 * @param mbox Address of the mailbox.
3609 * @param tx_msg Address of the transmit message descriptor.
3610 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003611 *
3612 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003613 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003614 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003615extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003616 struct k_sem *sem);
3617
Peter Mitsis12092702016-10-14 12:57:23 -04003618/**
3619 * @brief Receive a mailbox message.
3620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003621 * This routine receives a message from @a mbox, then optionally retrieves
3622 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003624 * @param mbox Address of the mailbox.
3625 * @param rx_msg Address of the receive message descriptor.
3626 * @param buffer Address of the buffer to receive data, or NULL to defer data
3627 * retrieval and message disposal until later.
3628 * @param timeout Waiting period for a message to be received (in
3629 * milliseconds), or one of the special values K_NO_WAIT
3630 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003631 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003632 * @retval 0 Message received.
3633 * @retval -ENOMSG Returned without waiting.
3634 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003635 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003636 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003637extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003638 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003639
3640/**
3641 * @brief Retrieve mailbox message data into a buffer.
3642 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003643 * This routine completes the processing of a received message by retrieving
3644 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003645 *
3646 * Alternatively, this routine can be used to dispose of a received message
3647 * without retrieving its data.
3648 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003649 * @param rx_msg Address of the receive message descriptor.
3650 * @param buffer Address of the buffer to receive data, or NULL to discard
3651 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003652 *
3653 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003654 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003655 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003656extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003657
3658/**
3659 * @brief Retrieve mailbox message data into a memory pool block.
3660 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003661 * This routine completes the processing of a received message by retrieving
3662 * its data into a memory pool block, then disposing of the message.
3663 * The memory pool block that results from successful retrieval must be
3664 * returned to the pool once the data has been processed, even in cases
3665 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003666 *
3667 * Alternatively, this routine can be used to dispose of a received message
3668 * without retrieving its data. In this case there is no need to return a
3669 * memory pool block to the pool.
3670 *
3671 * This routine allocates a new memory pool block for the data only if the
3672 * data is not already in one. If a new block cannot be allocated, the routine
3673 * returns a failure code and the received message is left unchanged. This
3674 * permits the caller to reattempt data retrieval at a later time or to dispose
3675 * of the received message without retrieving its data.
3676 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003677 * @param rx_msg Address of a receive message descriptor.
3678 * @param pool Address of memory pool, or NULL to discard data.
3679 * @param block Address of the area to hold memory pool block info.
3680 * @param timeout Waiting period to wait for a memory pool block (in
3681 * milliseconds), or one of the special values K_NO_WAIT
3682 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003683 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003684 * @retval 0 Data retrieved.
3685 * @retval -ENOMEM Returned without waiting.
3686 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003687 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003688 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003689extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003690 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003691 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003692
Anas Nashif166f5192018-02-25 08:02:36 -06003693/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003694
3695/**
Anas Nashifce78d162018-05-24 12:43:11 -05003696 * @defgroup pipe_apis Pipe APIs
3697 * @ingroup kernel_apis
3698 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003699 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003700
Anas Nashifce78d162018-05-24 12:43:11 -05003701/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003702struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003703 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3704 size_t size; /**< Buffer size */
3705 size_t bytes_used; /**< # bytes used in buffer */
3706 size_t read_index; /**< Where in buffer to read from */
3707 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003708
3709 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003710 _wait_q_t readers; /**< Reader wait queue */
3711 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003712 } wait_q;
3713
Anas Nashif2f203c22016-12-18 06:57:45 -05003714 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Anas Nashifce78d162018-05-24 12:43:11 -05003715 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003716};
3717
Anas Nashifce78d162018-05-24 12:43:11 -05003718/**
3719 * @cond INTERNAL_HIDDEN
3720 */
3721#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3722
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003723#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003724 { \
3725 .buffer = pipe_buffer, \
3726 .size = pipe_buffer_size, \
3727 .bytes_used = 0, \
3728 .read_index = 0, \
3729 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003730 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3731 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003732 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003733 }
3734
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003735#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3736
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003737/**
Allan Stephensc98da842016-11-11 15:45:03 -05003738 * INTERNAL_HIDDEN @endcond
3739 */
3740
3741/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003742 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003743 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003744 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003745 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003746 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003747 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003748 * @param name Name of the pipe.
3749 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3750 * or zero if no ring buffer is used.
3751 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003752 *
3753 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003754 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003755#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3756 static unsigned char __kernel_noinit __aligned(pipe_align) \
3757 _k_pipe_buf_##name[pipe_buffer_size]; \
3758 struct k_pipe name \
3759 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003760 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003761
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003762/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003763 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003764 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003765 * This routine initializes a pipe object, prior to its first use.
3766 *
3767 * @param pipe Address of the pipe.
3768 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3769 * is used.
3770 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3771 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003772 *
3773 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003774 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003775 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003776void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3777
3778/**
3779 * @brief Release a pipe's allocated buffer
3780 *
3781 * If a pipe object was given a dynamically allocated buffer via
3782 * k_pipe_alloc_init(), this will free it. This function does nothing
3783 * if the buffer wasn't dynamically allocated.
3784 *
3785 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003786 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003787 */
3788void k_pipe_cleanup(struct k_pipe *pipe);
3789
3790/**
3791 * @brief Initialize a pipe and allocate a buffer for it
3792 *
3793 * Storage for the buffer region will be allocated from the calling thread's
3794 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3795 * or userspace is enabled and the pipe object loses all references to it.
3796 *
3797 * This function should only be called on uninitialized pipe objects.
3798 *
3799 * @param pipe Address of the pipe.
3800 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3801 * buffer is used.
3802 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003803 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003804 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003805 */
3806__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003807
3808/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003809 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003810 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003811 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003813 * @param pipe Address of the pipe.
3814 * @param data Address of data to write.
3815 * @param bytes_to_write Size of data (in bytes).
3816 * @param bytes_written Address of area to hold the number of bytes written.
3817 * @param min_xfer Minimum number of bytes to write.
3818 * @param timeout Waiting period to wait for the data to be written (in
3819 * milliseconds), or one of the special values K_NO_WAIT
3820 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003821 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003822 * @retval 0 At least @a min_xfer bytes of data were written.
3823 * @retval -EIO Returned without waiting; zero data bytes were written.
3824 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003825 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003826 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003827 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003828__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3829 size_t bytes_to_write, size_t *bytes_written,
3830 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003831
3832/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003833 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003834 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003835 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003836 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003837 * @param pipe Address of the pipe.
3838 * @param data Address to place the data read from pipe.
3839 * @param bytes_to_read Maximum number of data bytes to read.
3840 * @param bytes_read Address of area to hold the number of bytes read.
3841 * @param min_xfer Minimum number of data bytes to read.
3842 * @param timeout Waiting period to wait for the data to be read (in
3843 * milliseconds), or one of the special values K_NO_WAIT
3844 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003845 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003846 * @retval 0 At least @a min_xfer bytes of data were read.
3847 * @retval -EIO Returned without waiting; zero data bytes were read.
3848 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003849 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003850 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003851 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003852__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3853 size_t bytes_to_read, size_t *bytes_read,
3854 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003855
3856/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003857 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003858 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003859 * This routine writes the data contained in a memory block to @a pipe.
3860 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003861 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003862 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003863 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003864 * @param block Memory block containing data to send
3865 * @param size Number of data bytes in memory block to send
3866 * @param sem Semaphore to signal upon completion (else NULL)
3867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003868 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003869 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003870 */
3871extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3872 size_t size, struct k_sem *sem);
3873
Anas Nashif166f5192018-02-25 08:02:36 -06003874/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003875
Allan Stephensc98da842016-11-11 15:45:03 -05003876/**
3877 * @cond INTERNAL_HIDDEN
3878 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003879
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003880struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003881 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003882 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003883 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003884 char *buffer;
3885 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003886 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003887
Anas Nashif2f203c22016-12-18 06:57:45 -05003888 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003889};
3890
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003891#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003892 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003893 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003894 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003895 .num_blocks = slab_num_blocks, \
3896 .block_size = slab_block_size, \
3897 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003898 .free_list = NULL, \
3899 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003900 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003901 }
3902
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003903#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3904
3905
Peter Mitsis578f9112016-10-07 13:50:31 -04003906/**
Allan Stephensc98da842016-11-11 15:45:03 -05003907 * INTERNAL_HIDDEN @endcond
3908 */
3909
3910/**
3911 * @defgroup mem_slab_apis Memory Slab APIs
3912 * @ingroup kernel_apis
3913 * @{
3914 */
3915
3916/**
Allan Stephensda827222016-11-09 14:23:58 -06003917 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003918 *
Allan Stephensda827222016-11-09 14:23:58 -06003919 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003920 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003921 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3922 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003923 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003924 *
Allan Stephensda827222016-11-09 14:23:58 -06003925 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003926 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003927 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003928 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003929 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003930 * @param name Name of the memory slab.
3931 * @param slab_block_size Size of each memory block (in bytes).
3932 * @param slab_num_blocks Number memory blocks.
3933 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003934 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003935 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003936#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3937 char __noinit __aligned(slab_align) \
3938 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3939 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003940 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003941 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003942 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003943
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003944/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003945 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003946 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003947 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003948 *
Allan Stephensda827222016-11-09 14:23:58 -06003949 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3950 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3951 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3952 * To ensure that each memory block is similarly aligned to this boundary,
3953 * @a slab_block_size must also be a multiple of N.
3954 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003955 * @param slab Address of the memory slab.
3956 * @param buffer Pointer to buffer used for the memory blocks.
3957 * @param block_size Size of each memory block (in bytes).
3958 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003959 *
3960 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003961 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003962 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003963extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003964 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003965
3966/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003967 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003969 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003971 * @param slab Address of the memory slab.
3972 * @param mem Pointer to block address area.
3973 * @param timeout Maximum time to wait for operation to complete
3974 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3975 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003976 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003977 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003978 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003979 * @retval -ENOMEM Returned without waiting.
3980 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003981 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003982 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003983extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003984 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003985
3986/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003987 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003989 * This routine releases a previously allocated memory block back to its
3990 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003991 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003992 * @param slab Address of the memory slab.
3993 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003994 *
3995 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003996 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003997 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003998extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003999
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004000/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004001 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004002 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004003 * This routine gets the number of memory blocks that are currently
4004 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004006 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004008 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004009 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004010 */
Kumar Galacc334c72017-04-21 10:55:34 -05004011static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004012{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004013 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004014}
4015
Peter Mitsisc001aa82016-10-13 13:53:37 -04004016/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004017 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004019 * This routine gets the number of memory blocks that are currently
4020 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004022 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004023 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004024 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004025 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004026 */
Kumar Galacc334c72017-04-21 10:55:34 -05004027static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004028{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004029 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004030}
4031
Anas Nashif166f5192018-02-25 08:02:36 -06004032/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004033
4034/**
4035 * @cond INTERNAL_HIDDEN
4036 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004037
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004038struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004039 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004040 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004041};
4042
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004043/**
Allan Stephensc98da842016-11-11 15:45:03 -05004044 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004045 */
4046
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004047/**
Allan Stephensc98da842016-11-11 15:45:03 -05004048 * @addtogroup mem_pool_apis
4049 * @{
4050 */
4051
4052/**
4053 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004055 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4056 * long. The memory pool allows blocks to be repeatedly partitioned into
4057 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004058 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004059 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004060 * If the pool is to be accessed outside the module where it is defined, it
4061 * can be declared via
4062 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004063 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004064 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004065 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004066 * @param minsz Size of the smallest blocks in the pool (in bytes).
4067 * @param maxsz Size of the largest blocks in the pool (in bytes).
4068 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004069 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004070 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004071 */
Andy Ross73cb9582017-05-09 10:42:39 -07004072#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4073 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4074 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08004075 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07004076 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004077 .base = { \
4078 .buf = _mpool_buf_##name, \
4079 .max_sz = maxsz, \
4080 .n_max = nmax, \
4081 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
4082 .levels = _mpool_lvls_##name, \
4083 .flags = SYS_MEM_POOL_KERNEL \
4084 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004085 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004086
Peter Mitsis937042c2016-10-13 13:18:26 -04004087/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004088 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004090 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004091 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004092 * @param pool Address of the memory pool.
4093 * @param block Pointer to block descriptor for the allocated memory.
4094 * @param size Amount of memory to allocate (in bytes).
4095 * @param timeout Maximum time to wait for operation to complete
4096 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4097 * or K_FOREVER to wait as long as necessary.
4098 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004099 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004100 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004101 * @retval -ENOMEM Returned without waiting.
4102 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004103 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004104 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004105extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004106 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004107
4108/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004109 * @brief Allocate memory from a memory pool with malloc() semantics
4110 *
4111 * Such memory must be released using k_free().
4112 *
4113 * @param pool Address of the memory pool.
4114 * @param size Amount of memory to allocate (in bytes).
4115 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004116 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004117 */
4118extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4119
4120/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004121 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004123 * This routine releases a previously allocated memory block back to its
4124 * memory pool.
4125 *
4126 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004127 *
4128 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004129 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004130 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004131extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004132
4133/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004134 * @brief Free memory allocated from a memory pool.
4135 *
4136 * This routine releases a previously allocated memory block back to its
4137 * memory pool
4138 *
4139 * @param id Memory block identifier.
4140 *
4141 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004142 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004143 */
4144extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4145
4146/**
Anas Nashif166f5192018-02-25 08:02:36 -06004147 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004148 */
4149
4150/**
4151 * @defgroup heap_apis Heap Memory Pool APIs
4152 * @ingroup kernel_apis
4153 * @{
4154 */
4155
4156/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004157 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004158 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004159 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004160 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004161 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004162 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004164 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004165 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004166 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004167extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004168
4169/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004170 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004171 *
4172 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004173 * returned must have been allocated from the heap memory pool or
4174 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004175 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004176 * If @a ptr is NULL, no operation is performed.
4177 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004178 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004179 *
4180 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004181 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004182 */
4183extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004184
Allan Stephensc98da842016-11-11 15:45:03 -05004185/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004186 * @brief Allocate memory from heap, array style
4187 *
4188 * This routine provides traditional calloc() semantics. Memory is
4189 * allocated from the heap memory pool and zeroed.
4190 *
4191 * @param nmemb Number of elements in the requested array
4192 * @param size Size of each array element (in bytes).
4193 *
4194 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004195 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004196 */
4197extern void *k_calloc(size_t nmemb, size_t size);
4198
Anas Nashif166f5192018-02-25 08:02:36 -06004199/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004200
Benjamin Walshacc68c12017-01-29 18:57:45 -05004201/* polling API - PRIVATE */
4202
Benjamin Walshb0179862017-02-02 16:39:57 -05004203#ifdef CONFIG_POLL
4204#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
4205#else
4206#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
4207#endif
4208
Benjamin Walshacc68c12017-01-29 18:57:45 -05004209/* private - implementation data created as needed, per-type */
4210struct _poller {
4211 struct k_thread *thread;
Andy Ross55a7e462018-05-31 11:58:09 -07004212 volatile int is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004213};
4214
4215/* private - types bit positions */
4216enum _poll_types_bits {
4217 /* can be used to ignore an event */
4218 _POLL_TYPE_IGNORE,
4219
4220 /* to be signaled by k_poll_signal() */
4221 _POLL_TYPE_SIGNAL,
4222
4223 /* semaphore availability */
4224 _POLL_TYPE_SEM_AVAILABLE,
4225
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004226 /* queue/fifo/lifo data availability */
4227 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004228
4229 _POLL_NUM_TYPES
4230};
4231
4232#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4233
4234/* private - states bit positions */
4235enum _poll_states_bits {
4236 /* default state when creating event */
4237 _POLL_STATE_NOT_READY,
4238
Benjamin Walshacc68c12017-01-29 18:57:45 -05004239 /* signaled by k_poll_signal() */
4240 _POLL_STATE_SIGNALED,
4241
4242 /* semaphore is available */
4243 _POLL_STATE_SEM_AVAILABLE,
4244
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004245 /* data is available to read on queue/fifo/lifo */
4246 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004247
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004248 /* queue/fifo/lifo wait was cancelled */
4249 _POLL_STATE_CANCELLED,
4250
Benjamin Walshacc68c12017-01-29 18:57:45 -05004251 _POLL_NUM_STATES
4252};
4253
4254#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4255
4256#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004257 (32 - (0 \
4258 + 8 /* tag */ \
4259 + _POLL_NUM_TYPES \
4260 + _POLL_NUM_STATES \
4261 + 1 /* modes */ \
4262 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004263
Benjamin Walshacc68c12017-01-29 18:57:45 -05004264/* end of polling API - PRIVATE */
4265
4266
4267/**
4268 * @defgroup poll_apis Async polling APIs
4269 * @ingroup kernel_apis
4270 * @{
4271 */
4272
4273/* Public polling API */
4274
4275/* public - values for k_poll_event.type bitfield */
4276#define K_POLL_TYPE_IGNORE 0
4277#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4278#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004279#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4280#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004281
4282/* public - polling modes */
4283enum k_poll_modes {
4284 /* polling thread does not take ownership of objects when available */
4285 K_POLL_MODE_NOTIFY_ONLY = 0,
4286
4287 K_POLL_NUM_MODES
4288};
4289
4290/* public - values for k_poll_event.state bitfield */
4291#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004292#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4293#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004294#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4295#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004296#define K_POLL_STATE_CANCELLED _POLL_STATE_BIT(_POLL_STATE_CANCELLED)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004297
4298/* public - poll signal object */
4299struct k_poll_signal {
4300 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004301 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004302
4303 /*
4304 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4305 * user resets it to 0.
4306 */
4307 unsigned int signaled;
4308
4309 /* custom result value passed to k_poll_signal() if needed */
4310 int result;
4311};
4312
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004313#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004314 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004315 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004316 .signaled = 0, \
4317 .result = 0, \
4318 }
4319
4320struct k_poll_event {
4321 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004322 sys_dnode_t _node;
4323
4324 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004325 struct _poller *poller;
4326
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004327 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004328 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004329
Benjamin Walshacc68c12017-01-29 18:57:45 -05004330 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004331 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004332
4333 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004334 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004335
4336 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004337 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004338
4339 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004340 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004341
4342 /* per-type data */
4343 union {
4344 void *obj;
4345 struct k_poll_signal *signal;
4346 struct k_sem *sem;
4347 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004348 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004349 };
4350};
4351
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004352#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004353 { \
4354 .poller = NULL, \
4355 .type = event_type, \
4356 .state = K_POLL_STATE_NOT_READY, \
4357 .mode = event_mode, \
4358 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004359 { .obj = event_obj }, \
4360 }
4361
4362#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4363 event_tag) \
4364 { \
4365 .type = event_type, \
4366 .tag = event_tag, \
4367 .state = K_POLL_STATE_NOT_READY, \
4368 .mode = event_mode, \
4369 .unused = 0, \
4370 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004371 }
4372
4373/**
4374 * @brief Initialize one struct k_poll_event instance
4375 *
4376 * After this routine is called on a poll event, the event it ready to be
4377 * placed in an event array to be passed to k_poll().
4378 *
4379 * @param event The event to initialize.
4380 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4381 * values. Only values that apply to the same object being polled
4382 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4383 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004384 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004385 * @param obj Kernel object or poll signal.
4386 *
4387 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004388 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004389 */
4390
Kumar Galacc334c72017-04-21 10:55:34 -05004391extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004392 int mode, void *obj);
4393
4394/**
4395 * @brief Wait for one or many of multiple poll events to occur
4396 *
4397 * This routine allows a thread to wait concurrently for one or many of
4398 * multiple poll events to have occurred. Such events can be a kernel object
4399 * being available, like a semaphore, or a poll signal event.
4400 *
4401 * When an event notifies that a kernel object is available, the kernel object
4402 * is not "given" to the thread calling k_poll(): it merely signals the fact
4403 * that the object was available when the k_poll() call was in effect. Also,
4404 * all threads trying to acquire an object the regular way, i.e. by pending on
4405 * the object, have precedence over the thread polling on the object. This
4406 * means that the polling thread will never get the poll event on an object
4407 * until the object becomes available and its pend queue is empty. For this
4408 * reason, the k_poll() call is more effective when the objects being polled
4409 * only have one thread, the polling thread, trying to acquire them.
4410 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004411 * When k_poll() returns 0, the caller should loop on all the events that were
4412 * passed to k_poll() and check the state field for the values that were
4413 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004414 *
4415 * Before being reused for another call to k_poll(), the user has to reset the
4416 * state field to K_POLL_STATE_NOT_READY.
4417 *
Andrew Boie3772f772018-05-07 16:52:57 -07004418 * When called from user mode, a temporary memory allocation is required from
4419 * the caller's resource pool.
4420 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004421 * @param events An array of pointers to events to be polled for.
4422 * @param num_events The number of events in the array.
4423 * @param timeout Waiting period for an event to be ready (in milliseconds),
4424 * or one of the special values K_NO_WAIT and K_FOREVER.
4425 *
4426 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004427 * @retval -EAGAIN Waiting period timed out.
Paul Sokolovsky45c0b202018-08-21 23:29:11 +03004428 * @retval -EINTR Polling has been interrupted, e.g. with
4429 * k_queue_cancel_wait(). All output events are still set and valid,
4430 * cancelled event(s) will be set to K_POLL_STATE_CANCELLED. In other
4431 * words, -EINTR status means that at least one of output events is
4432 * K_POLL_STATE_CANCELLED.
Andrew Boie3772f772018-05-07 16:52:57 -07004433 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4434 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004435 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004436 */
4437
Andrew Boie3772f772018-05-07 16:52:57 -07004438__syscall int k_poll(struct k_poll_event *events, int num_events,
4439 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004440
4441/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004442 * @brief Initialize a poll signal object.
4443 *
4444 * Ready a poll signal object to be signaled via k_poll_signal().
4445 *
4446 * @param signal A poll signal.
4447 *
4448 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004449 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004450 */
4451
Andrew Boie3772f772018-05-07 16:52:57 -07004452__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4453
4454/*
4455 * @brief Reset a poll signal object's state to unsignaled.
4456 *
4457 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004458 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004459 */
4460__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4461
4462static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4463{
4464 signal->signaled = 0;
4465}
4466
4467/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004468 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004469 *
4470 * @param signal A poll signal object
4471 * @param signaled An integer buffer which will be written nonzero if the
4472 * object was signaled
4473 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004474 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004475 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004476 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004477 */
4478__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4479 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004480
4481/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004482 * @brief Signal a poll signal object.
4483 *
4484 * This routine makes ready a poll signal, which is basically a poll event of
4485 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4486 * made ready to run. A @a result value can be specified.
4487 *
4488 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004489 * k_poll_signal(), stays set until the user sets it back to 0 with
4490 * k_poll_signal_reset(). It thus has to be reset by the user before being
4491 * passed again to k_poll() or k_poll() will consider it being signaled, and
4492 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004493 *
4494 * @param signal A poll signal.
4495 * @param result The value to store in the result field of the signal.
4496 *
4497 * @retval 0 The signal was delivered successfully.
4498 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004499 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004500 */
4501
Andrew Boie3772f772018-05-07 16:52:57 -07004502__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004503
Anas Nashif954d5502018-02-25 08:37:28 -06004504/**
4505 * @internal
4506 */
Andy Ross8606fab2018-03-26 10:54:40 -07004507extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004508
Anas Nashif166f5192018-02-25 08:02:36 -06004509/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004510
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004511/**
4512 * @brief Make the CPU idle.
4513 *
4514 * This function makes the CPU idle until an event wakes it up.
4515 *
4516 * In a regular system, the idle thread should be the only thread responsible
4517 * for making the CPU idle and triggering any type of power management.
4518 * However, in some more constrained systems, such as a single-threaded system,
4519 * the only thread would be responsible for this if needed.
4520 *
4521 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004522 * @req K-MISC-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004523 */
4524extern void k_cpu_idle(void);
4525
4526/**
4527 * @brief Make the CPU idle in an atomic fashion.
4528 *
4529 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4530 * must be done atomically before making the CPU idle.
4531 *
4532 * @param key Interrupt locking key obtained from irq_lock().
4533 *
4534 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004535 * @req K-MISC-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004536 */
4537extern void k_cpu_atomic_idle(unsigned int key);
4538
Anas Nashif954d5502018-02-25 08:37:28 -06004539
4540/**
4541 * @internal
4542 */
Kumar Galacc334c72017-04-21 10:55:34 -05004543extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004544
Andrew Boiecdb94d62017-04-18 15:22:05 -07004545#ifdef _ARCH_EXCEPT
4546/* This archtecture has direct support for triggering a CPU exception */
4547#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4548#else
4549
Andrew Boiecdb94d62017-04-18 15:22:05 -07004550/* NOTE: This is the implementation for arches that do not implement
4551 * _ARCH_EXCEPT() to generate a real CPU exception.
4552 *
4553 * We won't have a real exception frame to determine the PC value when
4554 * the oops occurred, so print file and line number before we jump into
4555 * the fatal error handler.
4556 */
4557#define _k_except_reason(reason) do { \
4558 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4559 _NanoFatalErrorHandler(reason, &_default_esf); \
4560 CODE_UNREACHABLE; \
4561 } while (0)
4562
4563#endif /* _ARCH__EXCEPT */
4564
4565/**
4566 * @brief Fatally terminate a thread
4567 *
4568 * This should be called when a thread has encountered an unrecoverable
4569 * runtime condition and needs to terminate. What this ultimately
4570 * means is determined by the _fatal_error_handler() implementation, which
4571 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4572 *
4573 * If this is called from ISR context, the default system fatal error handler
4574 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004575 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004576 */
4577#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4578
4579/**
4580 * @brief Fatally terminate the system
4581 *
4582 * This should be called when the Zephyr kernel has encountered an
4583 * unrecoverable runtime condition and needs to terminate. What this ultimately
4584 * means is determined by the _fatal_error_handler() implementation, which
4585 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004586 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004587 */
4588#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4589
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004590/*
4591 * private APIs that are utilized by one or more public APIs
4592 */
4593
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004594#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004595/**
4596 * @internal
4597 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004598extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004599#else
Anas Nashif954d5502018-02-25 08:37:28 -06004600/**
4601 * @internal
4602 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004603#define _init_static_threads() do { } while ((0))
4604#endif
4605
Anas Nashif954d5502018-02-25 08:37:28 -06004606/**
4607 * @internal
4608 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004609extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004610/**
4611 * @internal
4612 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004613extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004614
Andrew Boiedc5d9352017-06-02 12:56:47 -07004615/* arch/cpu.h may declare an architecture or platform-specific macro
4616 * for properly declaring stacks, compatible with MMU/MPU constraints if
4617 * enabled
4618 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004619
4620/**
4621 * @brief Obtain an extern reference to a stack
4622 *
4623 * This macro properly brings the symbol of a thread stack declared
4624 * elsewhere into scope.
4625 *
4626 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004627 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004628 */
4629#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4630
Andrew Boiedc5d9352017-06-02 12:56:47 -07004631#ifdef _ARCH_THREAD_STACK_DEFINE
4632#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4633#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4634 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304635#define K_THREAD_STACK_LEN(size) _ARCH_THREAD_STACK_LEN(size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004636#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4637#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004638static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004639{
4640 return _ARCH_THREAD_STACK_BUFFER(sym);
4641}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004642#else
4643/**
4644 * @brief Declare a toplevel thread stack memory region
4645 *
4646 * This declares a region of memory suitable for use as a thread's stack.
4647 *
4648 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4649 * 'noinit' section so that it isn't zeroed at boot
4650 *
Andrew Boie507852a2017-07-25 18:47:07 -07004651 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004652 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004653 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004654 *
4655 * It is legal to precede this definition with the 'static' keyword.
4656 *
4657 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4658 * parameter of k_thread_create(), it may not be the same as the
4659 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4660 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004661 * Some arches may round the size of the usable stack region up to satisfy
4662 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4663 * size.
4664 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004665 * @param sym Thread stack symbol name
4666 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004667 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004668 */
4669#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004670 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004671
4672/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304673 * @brief Calculate size of stacks to be allocated in a stack array
4674 *
4675 * This macro calculates the size to be allocated for the stacks
4676 * inside a stack array. It accepts the indicated "size" as a parameter
4677 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4678 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4679 *
4680 * @param size Size of the stack memory region
4681 * @req K-TSTACK-001
4682 */
4683#define K_THREAD_STACK_LEN(size) (size)
4684
4685/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004686 * @brief Declare a toplevel array of thread stack memory regions
4687 *
4688 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4689 * definition for additional details and constraints.
4690 *
4691 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4692 * 'noinit' section so that it isn't zeroed at boot
4693 *
4694 * @param sym Thread stack symbol name
4695 * @param nmemb Number of stacks to declare
4696 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004697 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004698 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004699#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004700 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304701 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004702
4703/**
4704 * @brief Declare an embedded stack memory region
4705 *
4706 * Used for stacks embedded within other data structures. Use is highly
4707 * discouraged but in some cases necessary. For memory protection scenarios,
4708 * it is very important that any RAM preceding this member not be writable
4709 * by threads else a stack overflow will lead to silent corruption. In other
4710 * words, the containing data structure should live in RAM owned by the kernel.
4711 *
4712 * @param sym Thread stack symbol name
4713 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004714 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004715 */
4716#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004717 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004718
4719/**
4720 * @brief Return the size in bytes of a stack memory region
4721 *
4722 * Convenience macro for passing the desired stack size to k_thread_create()
4723 * since the underlying implementation may actually create something larger
4724 * (for instance a guard area).
4725 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004726 * The value returned here is not guaranteed to match the 'size' parameter
4727 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004728 *
4729 * @param sym Stack memory symbol
4730 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004731 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004732 */
4733#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4734
4735/**
4736 * @brief Get a pointer to the physical stack buffer
4737 *
4738 * Convenience macro to get at the real underlying stack buffer used by
4739 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4740 * This is really only intended for diagnostic tools which want to examine
4741 * stack memory contents.
4742 *
4743 * @param sym Declared stack symbol name
4744 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004745 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004746 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004747static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004748{
4749 return (char *)sym;
4750}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004751
4752#endif /* _ARCH_DECLARE_STACK */
4753
Chunlin Hane9c97022017-07-07 20:29:30 +08004754/**
4755 * @defgroup mem_domain_apis Memory domain APIs
4756 * @ingroup kernel_apis
4757 * @{
4758 */
4759
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004760/**
4761 * @def MEM_PARTITION_ENTRY
4762 * @brief Used to declare a memory partition entry
4763 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004764 */
4765#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4766 {\
4767 .start = _start, \
4768 .size = _size, \
4769 .attr = _attr, \
4770 }
4771
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004772/**
4773 * @def K_MEM_PARTITION_DEFINE
4774 * @brief Used to declare a memory partition
4775 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004776 */
4777#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4778#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4779 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304780 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004781 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4782#else
4783#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304784 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004785 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4786#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4787
Chunlin Hane9c97022017-07-07 20:29:30 +08004788/* memory partition */
4789struct k_mem_partition {
4790 /* start address of memory partition */
4791 u32_t start;
4792 /* size of memory partition */
4793 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304794#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004795 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304796 k_mem_partition_attr_t attr;
4797#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004798};
4799
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304800/* memory domian
4801 * Note: Always declare this structure with __kernel prefix
4802 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004803struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304804#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004805 /* partitions in the domain */
4806 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304807#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004808 /* domain q */
4809 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004810 /* number of partitions in the domain */
4811 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004812};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304813
Chunlin Hane9c97022017-07-07 20:29:30 +08004814
4815/**
4816 * @brief Initialize a memory domain.
4817 *
4818 * Initialize a memory domain with given name and memory partitions.
4819 *
4820 * @param domain The memory domain to be initialized.
4821 * @param num_parts The number of array items of "parts" parameter.
4822 * @param parts An array of pointers to the memory partitions. Can be NULL
4823 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004824 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004825 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004826extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004827 struct k_mem_partition *parts[]);
4828/**
4829 * @brief Destroy a memory domain.
4830 *
4831 * Destroy a memory domain.
4832 *
4833 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004834 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004835 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004836extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4837
4838/**
4839 * @brief Add a memory partition into a memory domain.
4840 *
4841 * Add a memory partition into a memory domain.
4842 *
4843 * @param domain The memory domain to be added a memory partition.
4844 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004845 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004846 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004847extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4848 struct k_mem_partition *part);
4849
4850/**
4851 * @brief Remove a memory partition from a memory domain.
4852 *
4853 * Remove a memory partition from a memory domain.
4854 *
4855 * @param domain The memory domain to be removed a memory partition.
4856 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004857 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004858 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004859extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4860 struct k_mem_partition *part);
4861
4862/**
4863 * @brief Add a thread into a memory domain.
4864 *
4865 * Add a thread into a memory domain.
4866 *
4867 * @param domain The memory domain that the thread is going to be added into.
4868 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004869 *
4870 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004871 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004872extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4873 k_tid_t thread);
4874
4875/**
4876 * @brief Remove a thread from its memory domain.
4877 *
4878 * Remove a thread from its memory domain.
4879 *
4880 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004881 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004882 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004883extern void k_mem_domain_remove_thread(k_tid_t thread);
4884
Anas Nashif166f5192018-02-25 08:02:36 -06004885/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004886
Andrew Boie756f9072017-10-10 16:01:49 -07004887/**
4888 * @brief Emit a character buffer to the console device
4889 *
4890 * @param c String of characters to print
4891 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004892 *
4893 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004894 */
4895__syscall void k_str_out(char *c, size_t n);
4896
Andy Rosse7172672018-01-24 15:48:32 -08004897/**
4898 * @brief Start a numbered CPU on a MP-capable system
4899
4900 * This starts and initializes a specific CPU. The main thread on
4901 * startup is running on CPU zero, other processors are numbered
4902 * sequentially. On return from this function, the CPU is known to
4903 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004904 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004905 * with the provided key will work to enable them.
4906 *
4907 * Normally, in SMP mode this function will be called by the kernel
4908 * initialization and should not be used as a user API. But it is
4909 * defined here for special-purpose apps which want Zephyr running on
4910 * one core and to use others for design-specific processing.
4911 *
4912 * @param cpu_num Integer number of the CPU
4913 * @param stack Stack memory for the CPU
4914 * @param sz Stack buffer size, in bytes
4915 * @param fn Function to begin running on the CPU. First argument is
4916 * an irq_unlock() key.
4917 * @param arg Untyped argument to be passed to "fn"
4918 */
4919extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4920 void (*fn)(int, void *), void *arg);
4921
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004922#ifdef __cplusplus
4923}
4924#endif
4925
Andrew Boiee004dec2016-11-07 09:01:19 -08004926#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4927/*
4928 * Define new and delete operators.
4929 * At this moment, the operators do nothing since objects are supposed
4930 * to be statically allocated.
4931 */
4932inline void operator delete(void *ptr)
4933{
4934 (void)ptr;
4935}
4936
4937inline void operator delete[](void *ptr)
4938{
4939 (void)ptr;
4940}
4941
4942inline void *operator new(size_t size)
4943{
4944 (void)size;
4945 return NULL;
4946}
4947
4948inline void *operator new[](size_t size)
4949{
4950 (void)size;
4951 return NULL;
4952}
4953
4954/* Placement versions of operator new and delete */
4955inline void operator delete(void *ptr1, void *ptr2)
4956{
4957 (void)ptr1;
4958 (void)ptr2;
4959}
4960
4961inline void operator delete[](void *ptr1, void *ptr2)
4962{
4963 (void)ptr1;
4964 (void)ptr2;
4965}
4966
4967inline void *operator new(size_t size, void *ptr)
4968{
4969 (void)size;
4970 return ptr;
4971}
4972
4973inline void *operator new[](size_t size, void *ptr)
4974{
4975 (void)size;
4976 return ptr;
4977}
4978
4979#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4980
Anas Nashifb6304e62018-07-04 08:03:03 -05004981#include <tracing.h>
Andrew Boiefa94ee72017-09-28 16:54:35 -07004982#include <syscalls/kernel.h>
4983
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004984#endif /* !_ASMLANGUAGE */
4985
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004986#endif /* _kernel__h_ */