blob: d099d754d5660ba19e93780616991e69c9582ddb [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>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040018
19#ifdef __cplusplus
20extern "C" {
21#endif
22
Anas Nashifbbb157d2017-01-15 08:46:31 -050023/**
24 * @brief Kernel APIs
25 * @defgroup kernel_apis Kernel APIs
26 * @{
27 * @}
28 */
29
Anas Nashif61f4b242016-11-18 10:53:59 -050030#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040031#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
32#else
33#define K_DEBUG(fmt, ...)
34#endif
35
Benjamin Walsh2f280412017-01-14 19:23:46 -050036#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
37#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
38#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
39#elif defined(CONFIG_COOP_ENABLED)
40#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
41#define _NUM_PREEMPT_PRIO (0)
42#elif defined(CONFIG_PREEMPT_ENABLED)
43#define _NUM_COOP_PRIO (0)
44#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
45#else
46#error "invalid configuration"
47#endif
48
49#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040050#define K_PRIO_PREEMPT(x) (x)
51
Benjamin Walsh456c6da2016-09-02 18:55:39 -040052#define K_ANY NULL
53#define K_END NULL
54
Benjamin Walshedb35702017-01-14 18:47:22 -050055#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040056#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050057#elif defined(CONFIG_COOP_ENABLED)
58#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
59#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040060#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050061#else
62#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040063#endif
64
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050065#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040066#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
67#else
68#define K_LOWEST_THREAD_PRIO -1
69#endif
70
Benjamin Walshfab8d922016-11-08 15:36:36 -050071#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
72
Benjamin Walsh456c6da2016-09-02 18:55:39 -040073#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
74#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
75
Andy Ross225c74b2018-06-27 11:20:50 -070076#ifdef CONFIG_WAITQ_SCALABLE
Andy Ross1acd8c22018-05-03 14:51:49 -070077
78typedef struct {
79 struct _priq_rb waitq;
80} _wait_q_t;
81
82extern int _priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
83
84#define _WAIT_Q_INIT(wait_q) { { { .lessthan_fn = _priq_rb_lessthan } } }
85
86#else
87
Andy Rossccf3bf72018-05-10 11:10:34 -070088typedef struct {
89 sys_dlist_t waitq;
90} _wait_q_t;
91
92#define _WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
Benjamin Walsh456c6da2016-09-02 18:55:39 -040093
Andy Ross1acd8c22018-05-03 14:51:49 -070094#endif
95
Anas Nashif2f203c22016-12-18 06:57:45 -050096#ifdef CONFIG_OBJECT_TRACING
97#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
98#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040099#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500100#define _OBJECT_TRACING_INIT
101#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400102#endif
103
Benjamin Walshacc68c12017-01-29 18:57:45 -0500104#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300105#define _POLL_EVENT_OBJ_INIT(obj) \
106 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
107#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500108#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300109#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500110#define _POLL_EVENT
111#endif
112
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500113struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400114struct k_mutex;
115struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400116struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400117struct k_msgq;
118struct k_mbox;
119struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200120struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400121struct k_fifo;
122struct k_lifo;
123struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400124struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400125struct k_mem_pool;
126struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500127struct k_poll_event;
128struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800129struct k_mem_domain;
130struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400131
Andrew Boie5bd891d2017-09-27 12:59:28 -0700132/* This enumeration needs to be kept in sync with the lists of kernel objects
133 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
134 * function in kernel/userspace.c
135 */
Andrew Boie945af952017-08-22 13:15:23 -0700136enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700137 K_OBJ_ANY,
138
Leandro Pereirac2003672018-04-04 13:50:32 -0700139 /** @cond
140 * Doxygen should ignore this build-time generated include file
141 * when genrating API documentation. Enumeration values are
142 * generated during build by gen_kobject_list.py. It includes
143 * basic kernel objects (e.g. pipes and mutexes) and driver types.
144 */
145#include <kobj-types-enum.h>
146 /** @endcond
147 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700148
Andrew Boie945af952017-08-22 13:15:23 -0700149 K_OBJ_LAST
150};
151
152#ifdef CONFIG_USERSPACE
153/* Table generated by gperf, these objects are retrieved via
154 * _k_object_find() */
155struct _k_object {
156 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700157 u8_t perms[CONFIG_MAX_THREAD_BYTES];
158 u8_t type;
159 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700160 u32_t data;
Andrew Boiedf555242018-05-25 07:28:54 -0700161} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700162
Andrew Boie877f82e2017-10-17 11:20:22 -0700163struct _k_object_assignment {
164 struct k_thread *thread;
165 void * const *objects;
166};
167
168/**
169 * @brief Grant a static thread access to a list of kernel objects
170 *
171 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
172 * a set of kernel objects. These objects do not need to be in an initialized
173 * state. The permissions will be granted when the threads are initialized
174 * in the early boot sequence.
175 *
176 * All arguments beyond the first must be pointers to kernel objects.
177 *
178 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
179 */
180#define K_THREAD_ACCESS_GRANT(name_, ...) \
181 static void * const _CONCAT(_object_list_, name_)[] = \
182 { __VA_ARGS__, NULL }; \
183 static __used __in_section_unique(object_access) \
184 const struct _k_object_assignment \
185 _CONCAT(_object_access_, name_) = \
186 { (&_k_thread_obj_ ## name_), \
187 (_CONCAT(_object_list_, name_)) }
188
Andrew Boie945af952017-08-22 13:15:23 -0700189#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700190#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700191#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie945af952017-08-22 13:15:23 -0700192
193/**
194 * Lookup a kernel object and init its metadata if it exists
195 *
196 * Calling this on an object will make it usable from userspace.
197 * Intended to be called as the last statement in kernel object init
198 * functions.
199 *
200 * @param object Address of the kernel object
201 */
202void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700203#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700204
205#define K_THREAD_ACCESS_GRANT(thread, ...)
206
Anas Nashif954d5502018-02-25 08:37:28 -0600207/**
208 * @internal
209 */
Andrew Boie743e4682017-10-04 12:25:50 -0700210static inline void _k_object_init(void *obj)
211{
212 ARG_UNUSED(obj);
213}
214
Anas Nashif954d5502018-02-25 08:37:28 -0600215/**
216 * @internal
217 */
Andrew Boie743e4682017-10-04 12:25:50 -0700218static inline void _impl_k_object_access_grant(void *object,
219 struct k_thread *thread)
220{
221 ARG_UNUSED(object);
222 ARG_UNUSED(thread);
223}
224
Anas Nashif954d5502018-02-25 08:37:28 -0600225/**
226 * @internal
227 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700228static inline void k_object_access_revoke(void *object,
229 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700230{
231 ARG_UNUSED(object);
232 ARG_UNUSED(thread);
233}
234
Andrew Boiee9cfc542018-04-13 13:15:28 -0700235/**
236 * @internal
237 */
238static inline void _impl_k_object_release(void *object)
239{
240 ARG_UNUSED(object);
241}
242
Andrew Boie41bab6e2017-10-14 14:42:23 -0700243static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700244{
245 ARG_UNUSED(object);
246}
247#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700248
249/**
250 * grant a thread access to a kernel object
251 *
252 * The thread will be granted access to the object if the caller is from
253 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700254 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700255 *
256 * @param object Address of kernel object
257 * @param thread Thread to grant access to the object
258 */
Andrew Boie743e4682017-10-04 12:25:50 -0700259__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700260
Andrew Boiea89bf012017-10-09 14:47:55 -0700261/**
262 * grant a thread access to a kernel object
263 *
264 * The thread will lose access to the object if the caller is from
265 * supervisor mode, or the caller is from user mode AND has permissions
266 * on both the object and the thread whose access is being revoked.
267 *
268 * @param object Address of kernel object
269 * @param thread Thread to remove access to the object
270 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700271void k_object_access_revoke(void *object, struct k_thread *thread);
272
273
274__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700275
276/**
277 * grant all present and future threads access to an object
278 *
279 * If the caller is from supervisor mode, or the caller is from user mode and
280 * have sufficient permissions on the object, then that object will have
281 * permissions granted to it for *all* current and future threads running in
282 * the system, effectively becoming a public kernel object.
283 *
284 * Use of this API should be avoided on systems that are running untrusted code
285 * as it is possible for such code to derive the addresses of kernel objects
286 * and perform unwanted operations on them.
287 *
Andrew Boie04caa672017-10-13 13:57:07 -0700288 * It is not possible to revoke permissions on public objects; once public,
289 * any thread may use it.
290 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700291 * @param object Address of kernel object
292 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700293void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700294
Andrew Boie31bdfc02017-11-08 16:38:03 -0800295/**
296 * Allocate a kernel object of a designated type
297 *
298 * This will instantiate at runtime a kernel object of the specified type,
299 * returning a pointer to it. The object will be returned in an uninitialized
300 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700301 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800302 *
303 * Currently, allocation of thread stacks is not supported.
304 *
305 * @param otype Requested kernel object type
306 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
307 * available
308 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700309__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800310
Andrew Boie97bf0012018-04-24 17:01:37 -0700311#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800312/**
313 * Free a kernel object previously allocated with k_object_alloc()
314 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700315 * This will return memory for a kernel object back to resource pool it was
316 * allocated from. Care must be exercised that the object will not be used
317 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800318 *
319 * @param obj Pointer to the kernel object memory address.
320 */
321void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700322#else
323static inline void *_impl_k_object_alloc(enum k_objects otype)
324{
Kumar Gala85699f72018-05-17 09:26:03 -0500325 ARG_UNUSED(otype);
326
Andrew Boie97bf0012018-04-24 17:01:37 -0700327 return NULL;
328}
329
330static inline void k_obj_free(void *obj)
331{
332 ARG_UNUSED(obj);
333}
Andrew Boie31bdfc02017-11-08 16:38:03 -0800334#endif /* CONFIG_DYNAMIC_OBJECTS */
335
Andrew Boiebca15da2017-10-15 14:17:48 -0700336/* Using typedef deliberately here, this is quite intended to be an opaque
337 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
338 *
339 * The purpose of this data type is to clearly distinguish between the
340 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
341 * buffer which composes the stack data actually used by the underlying
342 * thread; they cannot be used interchangably as some arches precede the
343 * stack buffer region with guard areas that trigger a MPU or MMU fault
344 * if written to.
345 *
346 * APIs that want to work with the buffer inside should continue to use
347 * char *.
348 *
349 * Stacks should always be created with K_THREAD_STACK_DEFINE().
350 */
351struct __packed _k_thread_stack_element {
352 char data;
353};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700354typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700355
Andrew Boie73abd322017-04-04 13:19:13 -0700356/* timeouts */
357
358struct _timeout;
359typedef void (*_timeout_func_t)(struct _timeout *t);
360
361struct _timeout {
362 sys_dnode_t node;
363 struct k_thread *thread;
364 sys_dlist_t *wait_q;
365 s32_t delta_ticks_from_prev;
366 _timeout_func_t func;
367};
368
369extern s32_t _timeout_remaining_get(struct _timeout *timeout);
370
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700371/**
372 * @typedef k_thread_entry_t
373 * @brief Thread entry point function type.
374 *
375 * A thread's entry point function is invoked when the thread starts executing.
376 * Up to 3 argument values can be passed to the function.
377 *
378 * The thread terminates execution permanently if the entry point function
379 * returns. The thread is responsible for releasing any shared resources
380 * it may own (such as mutexes and dynamically allocated memory), prior to
381 * returning.
382 *
383 * @param p1 First argument.
384 * @param p2 Second argument.
385 * @param p3 Third argument.
386 *
387 * @return N/A
388 */
389typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700390
391#ifdef CONFIG_THREAD_MONITOR
392struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700393 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700394 void *parameter1;
395 void *parameter2;
396 void *parameter3;
397};
398#endif
399
400/* can be used for creating 'dummy' threads, e.g. for pending on objects */
401struct _thread_base {
402
403 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700404 union {
405 sys_dlist_t qnode_dlist;
406 struct rbnode qnode_rb;
407 };
408
Andy Ross225c74b2018-06-27 11:20:50 -0700409#ifdef CONFIG_WAITQ_SCALABLE
Andy Ross1acd8c22018-05-03 14:51:49 -0700410 /* wait queue on which the thread is pended (needed only for
411 * trees, not dumb lists)
412 */
413 _wait_q_t *pended_on;
414#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700415
416 /* user facing 'thread options'; values defined in include/kernel.h */
417 u8_t user_options;
418
419 /* thread state */
420 u8_t thread_state;
421
422 /*
423 * scheduler lock count and thread priority
424 *
425 * These two fields control the preemptibility of a thread.
426 *
427 * When the scheduler is locked, sched_locked is decremented, which
428 * means that the scheduler is locked for values from 0xff to 0x01. A
429 * thread is coop if its prio is negative, thus 0x80 to 0xff when
430 * looked at the value as unsigned.
431 *
432 * By putting them end-to-end, this means that a thread is
433 * non-preemptible if the bundled value is greater than or equal to
434 * 0x0080.
435 */
436 union {
437 struct {
438#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
439 u8_t sched_locked;
440 s8_t prio;
441#else /* LITTLE and PDP */
442 s8_t prio;
443 u8_t sched_locked;
444#endif
445 };
446 u16_t preempt;
447 };
448
Andy Ross4a2e50f2018-05-15 11:06:25 -0700449#ifdef CONFIG_SCHED_DEADLINE
450 int prio_deadline;
451#endif
452
Andy Ross1acd8c22018-05-03 14:51:49 -0700453 u32_t order_key;
454
Andy Ross2724fd12018-01-29 14:55:20 -0800455#ifdef CONFIG_SMP
456 /* True for the per-CPU idle threads */
457 u8_t is_idle;
458
Andy Ross2724fd12018-01-29 14:55:20 -0800459 /* CPU index on which thread was last run */
460 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700461
462 /* Recursive count of irq_lock() calls */
463 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800464#endif
465
Andrew Boie73abd322017-04-04 13:19:13 -0700466 /* data returned by APIs */
467 void *swap_data;
468
469#ifdef CONFIG_SYS_CLOCK_EXISTS
470 /* this thread's entry in a timeout queue */
471 struct _timeout timeout;
472#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700473};
474
475typedef struct _thread_base _thread_base_t;
476
477#if defined(CONFIG_THREAD_STACK_INFO)
478/* Contains the stack information of a thread */
479struct _thread_stack_info {
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700480 /* Stack Start - Identical to K_THREAD_STACK_BUFFER() on the stack
481 * object. Represents thread-writable stack area without any extras.
482 */
Andrew Boie73abd322017-04-04 13:19:13 -0700483 u32_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700484
485 /* Stack Size - Thread writable stack buffer size. Represents
486 * the size of the actual area, starting from the start member,
487 * that should be writable by the thread
488 */
Andrew Boie73abd322017-04-04 13:19:13 -0700489 u32_t size;
490};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700491
492typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700493#endif /* CONFIG_THREAD_STACK_INFO */
494
Chunlin Hane9c97022017-07-07 20:29:30 +0800495#if defined(CONFIG_USERSPACE)
496struct _mem_domain_info {
497 /* memory domain queue node */
498 sys_dnode_t mem_domain_q_node;
499 /* memory domain of the thread */
500 struct k_mem_domain *mem_domain;
501};
502
503#endif /* CONFIG_USERSPACE */
504
Anas Nashifce78d162018-05-24 12:43:11 -0500505/**
506 * @ingroup thread_apis
507 * Thread Structure
508 */
Andrew Boie73abd322017-04-04 13:19:13 -0700509struct k_thread {
510
511 struct _thread_base base;
512
Anas Nashifce78d162018-05-24 12:43:11 -0500513 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700514 struct _caller_saved caller_saved;
Anas Nashifce78d162018-05-24 12:43:11 -0500515 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700516 struct _callee_saved callee_saved;
517
Anas Nashifce78d162018-05-24 12:43:11 -0500518 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700519 void *init_data;
520
Anas Nashifce78d162018-05-24 12:43:11 -0500521 /**
522 * abort function
523 * @req K-THREAD-002
524 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700525 void (*fn_abort)(void);
526
527#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500528 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700529 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700530
Anas Nashifce78d162018-05-24 12:43:11 -0500531 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700532 struct k_thread *next_thread;
533#endif
534
535#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500536 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700537 void *custom_data;
538#endif
539
540#ifdef CONFIG_ERRNO
Anas Nashifce78d162018-05-24 12:43:11 -0500541 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700542 int errno_var;
543#endif
544
545#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500546 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700547 struct _thread_stack_info stack_info;
548#endif /* CONFIG_THREAD_STACK_INFO */
549
Chunlin Hane9c97022017-07-07 20:29:30 +0800550#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500551 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800552 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500553 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700554 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800555#endif /* CONFIG_USERSPACE */
556
Andy Ross042d8ec2017-12-09 08:37:20 -0800557#if defined(CONFIG_USE_SWITCH)
558 /* When using __switch() a few previously arch-specific items
559 * become part of the core OS
560 */
561
Anas Nashifce78d162018-05-24 12:43:11 -0500562 /** _Swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800563 int swap_retval;
564
Anas Nashifce78d162018-05-24 12:43:11 -0500565 /** Context handle returned via _arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800566 void *switch_handle;
567#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500568 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700569 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800570
Anas Nashifce78d162018-05-24 12:43:11 -0500571 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700572 struct _thread_arch arch;
573};
574
575typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400576typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700577#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400578
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400579enum execution_context_types {
580 K_ISR = 0,
581 K_COOP_THREAD,
582 K_PREEMPT_THREAD,
583};
584
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400585/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100586 * @defgroup profiling_apis Profiling APIs
587 * @ingroup kernel_apis
588 * @{
589 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530590typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
591 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100592
593/**
594 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
595 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700596 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100597 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
598 *
599 * CONFIG_MAIN_STACK_SIZE
600 * CONFIG_IDLE_STACK_SIZE
601 * CONFIG_ISR_STACK_SIZE
602 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
603 *
604 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
605 * produce output.
606 *
607 * @return N/A
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530608 *
609 * @deprecated This API is deprecated. Use k_thread_foreach().
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100610 */
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530611__deprecated extern void k_call_stacks_analyze(void);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100612
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530613/**
614 * @brief Iterate over all the threads in the system.
615 *
616 * This routine iterates over all the threads in the system and
617 * calls the user_cb function for each thread.
618 *
619 * @param user_cb Pointer to the user callback function.
620 * @param user_data Pointer to user data.
621 *
622 * @note CONFIG_THREAD_MONITOR must be set for this function
623 * to be effective. Also this API uses irq_lock to protect the
624 * _kernel.threads list which means creation of new threads and
625 * terminations of existing threads are blocked until this
626 * API returns.
627 *
628 * @return N/A
629 */
630extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
631
Anas Nashif166f5192018-02-25 08:02:36 -0600632/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100633
634/**
Allan Stephensc98da842016-11-11 15:45:03 -0500635 * @defgroup thread_apis Thread APIs
636 * @ingroup kernel_apis
637 * @{
638 */
639
Benjamin Walshed240f22017-01-22 13:05:08 -0500640#endif /* !_ASMLANGUAGE */
641
642
643/*
644 * Thread user options. May be needed by assembly code. Common part uses low
645 * bits, arch-specific use high bits.
646 */
647
Anas Nashifa541e932018-05-24 11:19:16 -0500648/**
649 * @brief system thread that must not abort
650 * @req K-THREAD-000
651 * */
Benjamin Walshed240f22017-01-22 13:05:08 -0500652#define K_ESSENTIAL (1 << 0)
653
654#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500655/**
656 * @brief thread uses floating point registers
657 */
Benjamin Walshed240f22017-01-22 13:05:08 -0500658#define K_FP_REGS (1 << 1)
659#endif
660
Anas Nashifa541e932018-05-24 11:19:16 -0500661/**
662 * @brief user mode thread
663 *
664 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700665 * has additional restrictions
666 */
667#define K_USER (1 << 2)
668
Anas Nashifa541e932018-05-24 11:19:16 -0500669/**
670 * @brief Inherit Permissions
671 *
672 * @details
673 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700674 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
675 * is not enabled.
676 */
677#define K_INHERIT_PERMS (1 << 3)
678
Benjamin Walshed240f22017-01-22 13:05:08 -0500679#ifdef CONFIG_X86
680/* x86 Bitmask definitions for threads user options */
681
682#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
683/* thread uses SSEx (and also FP) registers */
684#define K_SSE_REGS (1 << 7)
685#endif
686#endif
687
688/* end - thread options */
689
690#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400691/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700692 * @brief Create a thread.
693 *
694 * This routine initializes a thread, then schedules it for execution.
695 *
696 * The new thread may be scheduled for immediate execution or a delayed start.
697 * If the newly spawned thread does not have a delayed start the kernel
698 * scheduler may preempt the current thread to allow the new thread to
699 * execute.
700 *
701 * Thread options are architecture-specific, and can include K_ESSENTIAL,
702 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
703 * them using "|" (the logical OR operator).
704 *
705 * Historically, users often would use the beginning of the stack memory region
706 * to store the struct k_thread data, although corruption will occur if the
707 * stack overflows this region and stack protection features may not detect this
708 * situation.
709 *
710 * @param new_thread Pointer to uninitialized struct k_thread
711 * @param stack Pointer to the stack space.
712 * @param stack_size Stack size in bytes.
713 * @param entry Thread entry function.
714 * @param p1 1st entry point parameter.
715 * @param p2 2nd entry point parameter.
716 * @param p3 3rd entry point parameter.
717 * @param prio Thread priority.
718 * @param options Thread options.
719 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
720 *
721 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400722 *
723 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700724 */
Andrew Boie662c3452017-10-02 10:51:18 -0700725__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700726 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700727 size_t stack_size,
728 k_thread_entry_t entry,
729 void *p1, void *p2, void *p3,
730 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700731
Andrew Boie3f091b52017-08-30 14:34:14 -0700732/**
733 * @brief Drop a thread's privileges permanently to user mode
734 *
735 * @param entry Function to start executing from
736 * @param p1 1st entry point parameter
737 * @param p2 2nd entry point parameter
738 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400739 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700740 */
741extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
742 void *p1, void *p2,
743 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700744
Andrew Boied26cf2d2017-03-30 13:07:02 -0700745/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700746 * @brief Grant a thread access to a NULL-terminated set of kernel objects
747 *
748 * This is a convenience function. For the provided thread, grant access to
749 * the remaining arguments, which must be pointers to kernel objects.
750 * The final argument must be a NULL.
751 *
752 * The thread object must be initialized (i.e. running). The objects don't
753 * need to be.
754 *
755 * @param thread Thread to grant access to objects
756 * @param ... NULL-terminated list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400757 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700758 */
759extern void __attribute__((sentinel))
760 k_thread_access_grant(struct k_thread *thread, ...);
761
762/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700763 * @brief Assign a resource memory pool to a thread
764 *
765 * By default, threads have no resource pool assigned unless their parent
766 * thread has a resource pool, in which case it is inherited. Multiple
767 * threads may be assigned to the same memory pool.
768 *
769 * Changing a thread's resource pool will not migrate allocations from the
770 * previous pool.
771 *
772 * @param thread Target thread to assign a memory pool for resource requests,
773 * or NULL if the thread should no longer have a memory pool.
774 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400775 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700776 */
777static inline void k_thread_resource_pool_assign(struct k_thread *thread,
778 struct k_mem_pool *pool)
779{
780 thread->resource_pool = pool;
781}
782
783#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
784/**
785 * @brief Assign the system heap as a thread's resource pool
786 *
787 * Similar to k_thread_resource_pool_assign(), but the thread will use
788 * the kernel heap to draw memory.
789 *
790 * Use with caution, as a malicious thread could perform DoS attacks on the
791 * kernel heap.
792 *
793 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400794 *
795 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700796 */
797void k_thread_system_pool_assign(struct k_thread *thread);
798#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
799
800/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500801 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400802 *
Allan Stephensc98da842016-11-11 15:45:03 -0500803 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500804 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400805 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500806 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400807 *
808 * @return N/A
809 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700810__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400811
812/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500813 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400814 *
815 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500816 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400817 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400818 * @return N/A
819 */
Kumar Galacc334c72017-04-21 10:55:34 -0500820extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400821
822/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500823 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500825 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400826 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500827 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400828 *
829 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400830 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400831 */
Andrew Boie468190a2017-09-29 14:00:48 -0700832__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400833
834/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500835 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500837 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500839 * If @a thread is not currently sleeping, the routine has no effect.
840 *
841 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400842 *
843 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400844 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400845 */
Andrew Boie468190a2017-09-29 14:00:48 -0700846__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400847
848/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500849 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400850 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500851 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400852 *
853 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400854 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700855__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856
857/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500858 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500860 * This routine prevents @a thread from executing if it has not yet started
861 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400862 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500863 * @param thread ID of thread to cancel.
864 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700865 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500866 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700867 *
868 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400869 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700870__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400871
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400872/**
Allan Stephensc98da842016-11-11 15:45:03 -0500873 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500875 * This routine permanently stops execution of @a thread. The thread is taken
876 * off all kernel queues it is part of (i.e. the ready queue, the timeout
877 * queue, or a kernel object wait queue). However, any kernel resources the
878 * thread might currently own (such as mutexes or memory blocks) are not
879 * released. It is the responsibility of the caller of this routine to ensure
880 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500882 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400883 *
884 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400885 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400886 */
Andrew Boie468190a2017-09-29 14:00:48 -0700887__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400888
Andrew Boie7d627c52017-08-30 11:01:56 -0700889
890/**
891 * @brief Start an inactive thread
892 *
893 * If a thread was created with K_FOREVER in the delay parameter, it will
894 * not be added to the scheduling queue until this function is called
895 * on it.
896 *
897 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400898 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700899 */
Andrew Boie468190a2017-09-29 14:00:48 -0700900__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700901
Allan Stephensc98da842016-11-11 15:45:03 -0500902/**
903 * @cond INTERNAL_HIDDEN
904 */
905
Benjamin Walshd211a522016-12-06 11:44:01 -0500906/* timeout has timed out and is not on _timeout_q anymore */
907#define _EXPIRED (-2)
908
909/* timeout is not in use */
910#define _INACTIVE (-1)
911
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400912struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700913 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700914 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400915 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700916 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500917 void *init_p1;
918 void *init_p2;
919 void *init_p3;
920 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500921 u32_t init_options;
922 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500923 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400924};
925
Andrew Boied26cf2d2017-03-30 13:07:02 -0700926#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400927 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500928 prio, options, delay, abort, groups) \
929 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700930 .init_thread = (thread), \
931 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500932 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700933 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400934 .init_p1 = (void *)p1, \
935 .init_p2 = (void *)p2, \
936 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500937 .init_prio = (prio), \
938 .init_options = (options), \
939 .init_delay = (delay), \
940 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400941 }
942
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400943/**
Allan Stephensc98da842016-11-11 15:45:03 -0500944 * INTERNAL_HIDDEN @endcond
945 */
946
947/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500948 * @brief Statically define and initialize a thread.
949 *
950 * The thread may be scheduled for immediate execution or a delayed start.
951 *
952 * Thread options are architecture-specific, and can include K_ESSENTIAL,
953 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
954 * them using "|" (the logical OR operator).
955 *
956 * The ID of the thread can be accessed using:
957 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500958 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500959 *
960 * @param name Name of the thread.
961 * @param stack_size Stack size in bytes.
962 * @param entry Thread entry function.
963 * @param p1 1st entry point parameter.
964 * @param p2 2nd entry point parameter.
965 * @param p3 3rd entry point parameter.
966 * @param prio Thread priority.
967 * @param options Thread options.
968 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400969 *
Anas Nashif47420d02018-05-24 14:20:56 -0400970 * @req K-THREAD-010
971 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400972 * @internal It has been observed that the x86 compiler by default aligns
973 * these _static_thread_data structures to 32-byte boundaries, thereby
974 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400975 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400976 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500977#define K_THREAD_DEFINE(name, stack_size, \
978 entry, p1, p2, p3, \
979 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700980 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700981 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500982 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500983 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700984 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
985 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500986 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700987 NULL, 0); \
988 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400989
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400990/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500991 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500993 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400994 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500995 * @param thread ID of thread whose priority is needed.
996 *
997 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400998 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400999 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001000__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001001
1002/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001003 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001005 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001006 *
1007 * Rescheduling can occur immediately depending on the priority @a thread is
1008 * set to:
1009 *
1010 * - If its priority is raised above the priority of the caller of this
1011 * function, and the caller is preemptible, @a thread will be scheduled in.
1012 *
1013 * - If the caller operates on itself, it lowers its priority below that of
1014 * other threads in the system, and the caller is preemptible, the thread of
1015 * highest priority will be scheduled in.
1016 *
1017 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1018 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1019 * highest priority.
1020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001021 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001022 * @param prio New priority.
1023 *
1024 * @warning Changing the priority of a thread currently involved in mutex
1025 * priority inheritance may result in undefined behavior.
1026 *
1027 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001028 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001029 */
Andrew Boie468190a2017-09-29 14:00:48 -07001030__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001031
Andy Ross4a2e50f2018-05-15 11:06:25 -07001032
1033#ifdef CONFIG_SCHED_DEADLINE
1034/**
1035 * @brief Set deadline expiration time for scheduler
1036 *
1037 * This sets the "deadline" expiration as a time delta from the
1038 * current time, in the same units used by k_cycle_get_32(). The
1039 * scheduler (when deadline scheduling is enabled) will choose the
1040 * next expiring thread when selecting between threads at the same
1041 * static priority. Threads at different priorities will be scheduled
1042 * according to their static priority.
1043 *
1044 * @note Deadlines that are negative (i.e. in the past) are still seen
1045 * as higher priority than others, even if the thread has "finished"
1046 * its work. If you don't want it scheduled anymore, you have to
1047 * reset the deadline into the future, block/pend the thread, or
1048 * modify its priority with k_thread_priority_set().
1049 *
1050 * @note Despite the API naming, the scheduler makes no guarantees the
1051 * the thread WILL be scheduled within that deadline, nor does it take
1052 * extra metadata (like e.g. the "runtime" and "period" parameters in
1053 * Linux sched_setattr()) that allows the kernel to validate the
1054 * scheduling for achievability. Such features could be implemented
1055 * above this call, which is simply input to the priority selection
1056 * logic.
1057 *
1058 * @param thread A thread on which to set the deadline
1059 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001060 *
1061 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001062 */
1063__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1064#endif
1065
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001066/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001067 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001069 * This routine prevents the kernel scheduler from making @a thread the
1070 * current thread. All other internal operations on @a thread are still
1071 * performed; for example, any timeout it is waiting on keeps ticking,
1072 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001074 * If @a thread is already suspended, the routine has no effect.
1075 *
1076 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001077 *
1078 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001079 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001080 */
Andrew Boie468190a2017-09-29 14:00:48 -07001081__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001082
1083/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001084 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001085 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001086 * This routine allows the kernel scheduler to make @a thread the current
1087 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001089 * If @a thread is not currently suspended, the routine has no effect.
1090 *
1091 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001092 *
1093 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001094 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001095 */
Andrew Boie468190a2017-09-29 14:00:48 -07001096__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001097
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001098/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001099 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001101 * This routine specifies how the scheduler will perform time slicing of
1102 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001104 * To enable time slicing, @a slice must be non-zero. The scheduler
1105 * ensures that no thread runs for more than the specified time limit
1106 * before other threads of that priority are given a chance to execute.
1107 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001108 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001110 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001111 * execute. Once the scheduler selects a thread for execution, there is no
1112 * minimum guaranteed time the thread will execute before threads of greater or
1113 * equal priority are scheduled.
1114 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001115 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001116 * for execution, this routine has no effect; the thread is immediately
1117 * rescheduled after the slice period expires.
1118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001119 * To disable timeslicing, set both @a slice and @a prio to zero.
1120 *
1121 * @param slice Maximum time slice length (in milliseconds).
1122 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001123 *
1124 * @return N/A
1125 */
Kumar Galacc334c72017-04-21 10:55:34 -05001126extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001127
Anas Nashif166f5192018-02-25 08:02:36 -06001128/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001129
1130/**
1131 * @addtogroup isr_apis
1132 * @{
1133 */
1134
1135/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001136 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001137 *
Allan Stephensc98da842016-11-11 15:45:03 -05001138 * This routine allows the caller to customize its actions, depending on
1139 * whether it is a thread or an ISR.
1140 *
1141 * @note Can be called by ISRs.
1142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001143 * @return 0 if invoked by a thread.
1144 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001145 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001146extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001147
Benjamin Walsh445830d2016-11-10 15:54:27 -05001148/**
1149 * @brief Determine if code is running in a preemptible thread.
1150 *
Allan Stephensc98da842016-11-11 15:45:03 -05001151 * This routine allows the caller to customize its actions, depending on
1152 * whether it can be preempted by another thread. The routine returns a 'true'
1153 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001154 *
Allan Stephensc98da842016-11-11 15:45:03 -05001155 * - The code is running in a thread, not at ISR.
1156 * - The thread's priority is in the preemptible range.
1157 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001158 *
Allan Stephensc98da842016-11-11 15:45:03 -05001159 * @note Can be called by ISRs.
1160 *
1161 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001162 * @return Non-zero if invoked by a preemptible thread.
1163 */
Andrew Boie468190a2017-09-29 14:00:48 -07001164__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001165
Allan Stephensc98da842016-11-11 15:45:03 -05001166/**
Anas Nashif166f5192018-02-25 08:02:36 -06001167 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001168 */
1169
1170/**
1171 * @addtogroup thread_apis
1172 * @{
1173 */
1174
1175/**
1176 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001177 *
Allan Stephensc98da842016-11-11 15:45:03 -05001178 * This routine prevents the current thread from being preempted by another
1179 * thread by instructing the scheduler to treat it as a cooperative thread.
1180 * If the thread subsequently performs an operation that makes it unready,
1181 * it will be context switched out in the normal manner. When the thread
1182 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001183 *
Allan Stephensc98da842016-11-11 15:45:03 -05001184 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001185 *
Allan Stephensc98da842016-11-11 15:45:03 -05001186 * @note k_sched_lock() and k_sched_unlock() should normally be used
1187 * when the operation being performed can be safely interrupted by ISRs.
1188 * However, if the amount of processing involved is very small, better
1189 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001190 *
1191 * @return N/A
1192 */
1193extern void k_sched_lock(void);
1194
Allan Stephensc98da842016-11-11 15:45:03 -05001195/**
1196 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001197 *
Allan Stephensc98da842016-11-11 15:45:03 -05001198 * This routine reverses the effect of a previous call to k_sched_lock().
1199 * A thread must call the routine once for each time it called k_sched_lock()
1200 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001201 *
1202 * @return N/A
1203 */
1204extern void k_sched_unlock(void);
1205
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001206/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001207 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001208 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001209 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001210 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001211 * Custom data is not used by the kernel itself, and is freely available
1212 * for a thread to use as it sees fit. It can be used as a framework
1213 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001215 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001216 *
1217 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001218 *
1219 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001220 */
Andrew Boie468190a2017-09-29 14:00:48 -07001221__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001222
1223/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001224 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001225 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001226 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001227 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001228 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001229 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001230 */
Andrew Boie468190a2017-09-29 14:00:48 -07001231__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001232
1233/**
Anas Nashif166f5192018-02-25 08:02:36 -06001234 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001235 */
1236
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001237#include <sys_clock.h>
1238
Allan Stephensc2f15a42016-11-17 12:24:22 -05001239/**
1240 * @addtogroup clock_apis
1241 * @{
1242 */
1243
1244/**
1245 * @brief Generate null timeout delay.
1246 *
1247 * This macro generates a timeout delay that that instructs a kernel API
1248 * not to wait if the requested operation cannot be performed immediately.
1249 *
1250 * @return Timeout delay value.
1251 */
1252#define K_NO_WAIT 0
1253
1254/**
1255 * @brief Generate timeout delay from milliseconds.
1256 *
1257 * This macro generates a timeout delay that that instructs a kernel API
1258 * to wait up to @a ms milliseconds to perform the requested operation.
1259 *
1260 * @param ms Duration in milliseconds.
1261 *
1262 * @return Timeout delay value.
1263 */
Johan Hedberg14471692016-11-13 10:52:15 +02001264#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001265
1266/**
1267 * @brief Generate timeout delay from seconds.
1268 *
1269 * This macro generates a timeout delay that that instructs a kernel API
1270 * to wait up to @a s seconds to perform the requested operation.
1271 *
1272 * @param s Duration in seconds.
1273 *
1274 * @return Timeout delay value.
1275 */
Johan Hedberg14471692016-11-13 10:52:15 +02001276#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001277
1278/**
1279 * @brief Generate timeout delay from minutes.
1280 *
1281 * This macro generates a timeout delay that that instructs a kernel API
1282 * to wait up to @a m minutes to perform the requested operation.
1283 *
1284 * @param m Duration in minutes.
1285 *
1286 * @return Timeout delay value.
1287 */
Johan Hedberg14471692016-11-13 10:52:15 +02001288#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001289
1290/**
1291 * @brief Generate timeout delay from hours.
1292 *
1293 * This macro generates a timeout delay that that instructs a kernel API
1294 * to wait up to @a h hours to perform the requested operation.
1295 *
1296 * @param h Duration in hours.
1297 *
1298 * @return Timeout delay value.
1299 */
Johan Hedberg14471692016-11-13 10:52:15 +02001300#define K_HOURS(h) K_MINUTES((h) * 60)
1301
Allan Stephensc98da842016-11-11 15:45:03 -05001302/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001303 * @brief Generate infinite timeout delay.
1304 *
1305 * This macro generates a timeout delay that that instructs a kernel API
1306 * to wait as long as necessary to perform the requested operation.
1307 *
1308 * @return Timeout delay value.
1309 */
1310#define K_FOREVER (-1)
1311
1312/**
Anas Nashif166f5192018-02-25 08:02:36 -06001313 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001314 */
1315
1316/**
Allan Stephensc98da842016-11-11 15:45:03 -05001317 * @cond INTERNAL_HIDDEN
1318 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001319
Benjamin Walsh62092182016-12-20 14:39:08 -05001320/* kernel clocks */
1321
1322#if (sys_clock_ticks_per_sec == 1000) || \
1323 (sys_clock_ticks_per_sec == 500) || \
1324 (sys_clock_ticks_per_sec == 250) || \
1325 (sys_clock_ticks_per_sec == 125) || \
1326 (sys_clock_ticks_per_sec == 100) || \
1327 (sys_clock_ticks_per_sec == 50) || \
1328 (sys_clock_ticks_per_sec == 25) || \
1329 (sys_clock_ticks_per_sec == 20) || \
1330 (sys_clock_ticks_per_sec == 10) || \
1331 (sys_clock_ticks_per_sec == 1)
1332
1333 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1334#else
1335 /* yields horrible 64-bit math on many architectures: try to avoid */
1336 #define _NON_OPTIMIZED_TICKS_PER_SEC
1337#endif
1338
Kumar Galacc334c72017-04-21 10:55:34 -05001339static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001340{
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001341#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
1342 s64_t ms_ticks_per_sec = (s64_t)ms * sys_clock_ticks_per_sec;
1343 return (s32_t)ceiling_fraction(ms_ticks_per_sec, MSEC_PER_SEC);
1344#else
Kumar Galacc334c72017-04-21 10:55:34 -05001345 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001346#endif
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001347}
Benjamin Walsh62092182016-12-20 14:39:08 -05001348
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001349/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001350#ifdef CONFIG_TICKLESS_KERNEL
1351#define _TICK_ALIGN 0
1352#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001353#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001354#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001355
Kumar Galacc334c72017-04-21 10:55:34 -05001356static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001357{
Benjamin Walsh62092182016-12-20 14:39:08 -05001358#ifdef CONFIG_SYS_CLOCK_EXISTS
1359
1360#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001361 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001362#else
Kumar Galacc334c72017-04-21 10:55:34 -05001363 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001364#endif
1365
1366#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001367 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001368 return 0;
1369#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001370}
1371
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001372struct k_timer {
1373 /*
1374 * _timeout structure must be first here if we want to use
1375 * dynamic timer allocation. timeout.node is used in the double-linked
1376 * list of free timers
1377 */
1378 struct _timeout timeout;
1379
Allan Stephens45bfa372016-10-12 12:39:42 -05001380 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001381 _wait_q_t wait_q;
1382
1383 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001384 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001385
1386 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001387 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001388
1389 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001390 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001391
Allan Stephens45bfa372016-10-12 12:39:42 -05001392 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001393 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001394
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001395 /* user-specific data, also used to support legacy features */
1396 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001397
Anas Nashif2f203c22016-12-18 06:57:45 -05001398 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001399};
1400
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001401#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001402 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001403 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001404 .timeout.wait_q = NULL, \
1405 .timeout.thread = NULL, \
1406 .timeout.func = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001407 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001408 .expiry_fn = expiry, \
1409 .stop_fn = stop, \
1410 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001411 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001412 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001413 }
1414
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001415#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1416
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001417/**
Allan Stephensc98da842016-11-11 15:45:03 -05001418 * INTERNAL_HIDDEN @endcond
1419 */
1420
1421/**
1422 * @defgroup timer_apis Timer APIs
1423 * @ingroup kernel_apis
1424 * @{
1425 */
1426
1427/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001428 * @typedef k_timer_expiry_t
1429 * @brief Timer expiry function type.
1430 *
1431 * A timer's expiry function is executed by the system clock interrupt handler
1432 * each time the timer expires. The expiry function is optional, and is only
1433 * invoked if the timer has been initialized with one.
1434 *
1435 * @param timer Address of timer.
1436 *
1437 * @return N/A
1438 */
1439typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1440
1441/**
1442 * @typedef k_timer_stop_t
1443 * @brief Timer stop function type.
1444 *
1445 * A timer's stop function is executed if the timer is stopped prematurely.
1446 * The function runs in the context of the thread that stops the timer.
1447 * The stop function is optional, and is only invoked if the timer has been
1448 * initialized with one.
1449 *
1450 * @param timer Address of timer.
1451 *
1452 * @return N/A
1453 */
1454typedef void (*k_timer_stop_t)(struct k_timer *timer);
1455
1456/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001457 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001458 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001459 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001460 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001461 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001462 *
1463 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001464 * @param expiry_fn Function to invoke each time the timer expires.
1465 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001466 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001467#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001468 struct k_timer name \
1469 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001470 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001471
Allan Stephens45bfa372016-10-12 12:39:42 -05001472/**
1473 * @brief Initialize a timer.
1474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001475 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001476 *
1477 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001478 * @param expiry_fn Function to invoke each time the timer expires.
1479 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001480 *
1481 * @return N/A
1482 */
1483extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001484 k_timer_expiry_t expiry_fn,
1485 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001486
Allan Stephens45bfa372016-10-12 12:39:42 -05001487/**
1488 * @brief Start a timer.
1489 *
1490 * This routine starts a timer, and resets its status to zero. The timer
1491 * begins counting down using the specified duration and period values.
1492 *
1493 * Attempting to start a timer that is already running is permitted.
1494 * The timer's status is reset to zero and the timer begins counting down
1495 * using the new duration and period values.
1496 *
1497 * @param timer Address of timer.
1498 * @param duration Initial timer duration (in milliseconds).
1499 * @param period Timer period (in milliseconds).
1500 *
1501 * @return N/A
1502 */
Andrew Boiea354d492017-09-29 16:22:28 -07001503__syscall void k_timer_start(struct k_timer *timer,
1504 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001505
1506/**
1507 * @brief Stop a timer.
1508 *
1509 * This routine stops a running timer prematurely. The timer's stop function,
1510 * if one exists, is invoked by the caller.
1511 *
1512 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001513 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001514 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001515 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1516 * if @a k_timer_stop is to be called from ISRs.
1517 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001518 * @param timer Address of timer.
1519 *
1520 * @return N/A
1521 */
Andrew Boiea354d492017-09-29 16:22:28 -07001522__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001523
1524/**
1525 * @brief Read timer status.
1526 *
1527 * This routine reads the timer's status, which indicates the number of times
1528 * it has expired since its status was last read.
1529 *
1530 * Calling this routine resets the timer's status to zero.
1531 *
1532 * @param timer Address of timer.
1533 *
1534 * @return Timer status.
1535 */
Andrew Boiea354d492017-09-29 16:22:28 -07001536__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001537
1538/**
1539 * @brief Synchronize thread to timer expiration.
1540 *
1541 * This routine blocks the calling thread until the timer's status is non-zero
1542 * (indicating that it has expired at least once since it was last examined)
1543 * or the timer is stopped. If the timer status is already non-zero,
1544 * or the timer is already stopped, the caller continues without waiting.
1545 *
1546 * Calling this routine resets the timer's status to zero.
1547 *
1548 * This routine must not be used by interrupt handlers, since they are not
1549 * allowed to block.
1550 *
1551 * @param timer Address of timer.
1552 *
1553 * @return Timer status.
1554 */
Andrew Boiea354d492017-09-29 16:22:28 -07001555__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001556
1557/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001558 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001559 *
1560 * This routine computes the (approximate) time remaining before a running
1561 * timer next expires. If the timer is not running, it returns zero.
1562 *
1563 * @param timer Address of timer.
1564 *
1565 * @return Remaining time (in milliseconds).
1566 */
Andrew Boiea354d492017-09-29 16:22:28 -07001567__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1568
1569static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001570{
1571 return _timeout_remaining_get(&timer->timeout);
1572}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001573
Allan Stephensc98da842016-11-11 15:45:03 -05001574/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001575 * @brief Associate user-specific data with a timer.
1576 *
1577 * This routine records the @a user_data with the @a timer, to be retrieved
1578 * later.
1579 *
1580 * It can be used e.g. in a timer handler shared across multiple subsystems to
1581 * retrieve data specific to the subsystem this timer is associated with.
1582 *
1583 * @param timer Address of timer.
1584 * @param user_data User data to associate with the timer.
1585 *
1586 * @return N/A
1587 */
Andrew Boiea354d492017-09-29 16:22:28 -07001588__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1589
Anas Nashif954d5502018-02-25 08:37:28 -06001590/**
1591 * @internal
1592 */
Andrew Boiea354d492017-09-29 16:22:28 -07001593static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1594 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001595{
1596 timer->user_data = user_data;
1597}
1598
1599/**
1600 * @brief Retrieve the user-specific data from a timer.
1601 *
1602 * @param timer Address of timer.
1603 *
1604 * @return The user data.
1605 */
Andrew Boiea354d492017-09-29 16:22:28 -07001606__syscall void *k_timer_user_data_get(struct k_timer *timer);
1607
1608static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001609{
1610 return timer->user_data;
1611}
1612
Anas Nashif166f5192018-02-25 08:02:36 -06001613/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001614
Allan Stephensc98da842016-11-11 15:45:03 -05001615/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001616 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001617 * @{
1618 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001619
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001620/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001621 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001622 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001623 * This routine returns the elapsed time since the system booted,
1624 * in milliseconds.
1625 *
1626 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001627 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001628__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001629
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001630/**
1631 * @brief Enable clock always on in tickless kernel
1632 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001633 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001634 * there are no timer events programmed in tickless kernel
1635 * scheduling. This is necessary if the clock is used to track
1636 * passage of time.
1637 *
1638 * @retval prev_status Previous status of always on flag
1639 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301640#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001641static inline int k_enable_sys_clock_always_on(void)
1642{
1643 int prev_status = _sys_clock_always_on;
1644
1645 _sys_clock_always_on = 1;
1646 _enable_sys_clock();
1647
1648 return prev_status;
1649}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301650#else
1651#define k_enable_sys_clock_always_on() do { } while ((0))
1652#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001653
1654/**
1655 * @brief Disable clock always on in tickless kernel
1656 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001657 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001658 * there are no timer events programmed in tickless kernel
1659 * scheduling. To save power, this routine should be called
1660 * immediately when clock is not used to track time.
1661 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301662#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001663static inline void k_disable_sys_clock_always_on(void)
1664{
1665 _sys_clock_always_on = 0;
1666}
1667#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001668#define k_disable_sys_clock_always_on() do { } while ((0))
1669#endif
1670
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001671/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001672 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001673 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001674 * This routine returns the lower 32-bits of the elapsed time since the system
1675 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001676 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001677 * This routine can be more efficient than k_uptime_get(), as it reduces the
1678 * need for interrupt locking and 64-bit math. However, the 32-bit result
1679 * cannot hold a system uptime time larger than approximately 50 days, so the
1680 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001681 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001682 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001683 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001684__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001685
1686/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001687 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001688 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001689 * This routine computes the elapsed time between the current system uptime
1690 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001691 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001692 * @param reftime Pointer to a reference time, which is updated to the current
1693 * uptime upon return.
1694 *
1695 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001696 */
Kumar Galacc334c72017-04-21 10:55:34 -05001697extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001698
1699/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001700 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001702 * This routine computes the elapsed time between the current system uptime
1703 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001705 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1706 * need for interrupt locking and 64-bit math. However, the 32-bit result
1707 * cannot hold an elapsed time larger than approximately 50 days, so the
1708 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001709 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001710 * @param reftime Pointer to a reference time, which is updated to the current
1711 * uptime upon return.
1712 *
1713 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001714 */
Kumar Galacc334c72017-04-21 10:55:34 -05001715extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001716
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001717/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001718 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001719 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001720 * This routine returns the current time, as measured by the system's hardware
1721 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001722 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001723 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001724 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001725#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001726
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001727/**
Anas Nashif166f5192018-02-25 08:02:36 -06001728 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001729 */
1730
Allan Stephensc98da842016-11-11 15:45:03 -05001731/**
1732 * @cond INTERNAL_HIDDEN
1733 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001734
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001735struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001736 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001737 union {
1738 _wait_q_t wait_q;
1739
1740 _POLL_EVENT;
1741 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001742
1743 _OBJECT_TRACING_NEXT_PTR(k_queue);
1744};
1745
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001746#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001747 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001748 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001749 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001750 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001751 _OBJECT_TRACING_INIT \
1752 }
1753
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001754#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1755
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001756extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1757
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001758/**
1759 * INTERNAL_HIDDEN @endcond
1760 */
1761
1762/**
1763 * @defgroup queue_apis Queue APIs
1764 * @ingroup kernel_apis
1765 * @{
1766 */
1767
1768/**
1769 * @brief Initialize a queue.
1770 *
1771 * This routine initializes a queue object, prior to its first use.
1772 *
1773 * @param queue Address of the queue.
1774 *
1775 * @return N/A
1776 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001777__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001778
1779/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001780 * @brief Cancel waiting on a queue.
1781 *
1782 * This routine causes first thread pending on @a queue, if any, to
1783 * return from k_queue_get() call with NULL value (as if timeout expired).
1784 *
1785 * @note Can be called by ISRs.
1786 *
1787 * @param queue Address of the queue.
1788 *
1789 * @return N/A
1790 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001791__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001792
1793/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001794 * @brief Append an element to the end of a queue.
1795 *
1796 * This routine appends a data item to @a queue. A queue data item must be
1797 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1798 * reserved for the kernel's use.
1799 *
1800 * @note Can be called by ISRs.
1801 *
1802 * @param queue Address of the queue.
1803 * @param data Address of the data item.
1804 *
1805 * @return N/A
1806 */
1807extern void k_queue_append(struct k_queue *queue, void *data);
1808
1809/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001810 * @brief Append an element to a queue.
1811 *
1812 * This routine appends a data item to @a queue. There is an implicit
1813 * memory allocation from the calling thread's resource pool, which is
1814 * automatically freed when the item is removed from the queue.
1815 *
1816 * @note Can be called by ISRs.
1817 *
1818 * @param queue Address of the queue.
1819 * @param data Address of the data item.
1820 *
1821 * @retval 0 on success
1822 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1823 */
1824__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1825
1826/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001827 * @brief Prepend an element to a queue.
1828 *
1829 * This routine prepends a data item to @a queue. A queue data item must be
1830 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1831 * reserved for the kernel's use.
1832 *
1833 * @note Can be called by ISRs.
1834 *
1835 * @param queue Address of the queue.
1836 * @param data Address of the data item.
1837 *
1838 * @return N/A
1839 */
1840extern void k_queue_prepend(struct k_queue *queue, void *data);
1841
1842/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001843 * @brief Prepend an element to a queue.
1844 *
1845 * This routine prepends a data item to @a queue. There is an implicit
1846 * memory allocation from the calling thread's resource pool, which is
1847 * automatically freed when the item is removed from the queue.
1848 *
1849 * @note Can be called by ISRs.
1850 *
1851 * @param queue Address of the queue.
1852 * @param data Address of the data item.
1853 *
1854 * @retval 0 on success
1855 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1856 */
1857__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1858
1859/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001860 * @brief Inserts an element to a queue.
1861 *
1862 * This routine inserts a data item to @a queue after previous item. A queue
1863 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1864 * item are reserved for the kernel's use.
1865 *
1866 * @note Can be called by ISRs.
1867 *
1868 * @param queue Address of the queue.
1869 * @param prev Address of the previous data item.
1870 * @param data Address of the data item.
1871 *
1872 * @return N/A
1873 */
1874extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1875
1876/**
1877 * @brief Atomically append a list of elements to a queue.
1878 *
1879 * This routine adds a list of data items to @a queue in one operation.
1880 * The data items must be in a singly-linked list, with the first 32 bits
1881 * in each data item pointing to the next data item; the list must be
1882 * NULL-terminated.
1883 *
1884 * @note Can be called by ISRs.
1885 *
1886 * @param queue Address of the queue.
1887 * @param head Pointer to first node in singly-linked list.
1888 * @param tail Pointer to last node in singly-linked list.
1889 *
1890 * @return N/A
1891 */
1892extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1893
1894/**
1895 * @brief Atomically add a list of elements to a queue.
1896 *
1897 * This routine adds a list of data items to @a queue in one operation.
1898 * The data items must be in a singly-linked list implemented using a
1899 * sys_slist_t object. Upon completion, the original list is empty.
1900 *
1901 * @note Can be called by ISRs.
1902 *
1903 * @param queue Address of the queue.
1904 * @param list Pointer to sys_slist_t object.
1905 *
1906 * @return N/A
1907 */
1908extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1909
1910/**
1911 * @brief Get an element from a queue.
1912 *
1913 * This routine removes first data item from @a queue. The first 32 bits of the
1914 * data item are reserved for the kernel's use.
1915 *
1916 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1917 *
1918 * @param queue Address of the queue.
1919 * @param timeout Waiting period to obtain a data item (in milliseconds),
1920 * or one of the special values K_NO_WAIT and K_FOREVER.
1921 *
1922 * @return Address of the data item if successful; NULL if returned
1923 * without waiting, or waiting period timed out.
1924 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001925__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001926
1927/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001928 * @brief Remove an element from a queue.
1929 *
1930 * This routine removes data item from @a queue. The first 32 bits of the
1931 * data item are reserved for the kernel's use. Removing elements from k_queue
1932 * rely on sys_slist_find_and_remove which is not a constant time operation.
1933 *
1934 * @note Can be called by ISRs
1935 *
1936 * @param queue Address of the queue.
1937 * @param data Address of the data item.
1938 *
1939 * @return true if data item was removed
1940 */
1941static inline bool k_queue_remove(struct k_queue *queue, void *data)
1942{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001943 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001944}
1945
1946/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001947 * @brief Query a queue to see if it has data available.
1948 *
1949 * Note that the data might be already gone by the time this function returns
1950 * if other threads are also trying to read from the queue.
1951 *
1952 * @note Can be called by ISRs.
1953 *
1954 * @param queue Address of the queue.
1955 *
1956 * @return Non-zero if the queue is empty.
1957 * @return 0 if data is available.
1958 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001959__syscall int k_queue_is_empty(struct k_queue *queue);
1960
1961static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001962{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001963 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001964}
1965
1966/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001967 * @brief Peek element at the head of queue.
1968 *
1969 * Return element from the head of queue without removing it.
1970 *
1971 * @param queue Address of the queue.
1972 *
1973 * @return Head element, or NULL if queue is empty.
1974 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001975__syscall void *k_queue_peek_head(struct k_queue *queue);
1976
1977static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001978{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001979 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001980}
1981
1982/**
1983 * @brief Peek element at the tail of queue.
1984 *
1985 * Return element from the tail of queue without removing it.
1986 *
1987 * @param queue Address of the queue.
1988 *
1989 * @return Tail element, or NULL if queue is empty.
1990 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001991__syscall void *k_queue_peek_tail(struct k_queue *queue);
1992
1993static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001994{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001995 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001996}
1997
1998/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001999 * @brief Statically define and initialize a queue.
2000 *
2001 * The queue can be accessed outside the module where it is defined using:
2002 *
2003 * @code extern struct k_queue <name>; @endcode
2004 *
2005 * @param name Name of the queue.
2006 */
2007#define K_QUEUE_DEFINE(name) \
2008 struct k_queue name \
2009 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002010 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002011
Anas Nashif166f5192018-02-25 08:02:36 -06002012/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002013
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002014struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002015 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002016};
2017
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002018/**
2019 * @cond INTERNAL_HIDDEN
2020 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002021#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002022 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002023 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002024 }
2025
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002026#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
2027
Allan Stephensc98da842016-11-11 15:45:03 -05002028/**
2029 * INTERNAL_HIDDEN @endcond
2030 */
2031
2032/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002033 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002034 * @ingroup kernel_apis
2035 * @{
2036 */
2037
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002038/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002039 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002040 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002041 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002042 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002043 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002044 *
2045 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002046 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002047 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002048#define k_fifo_init(fifo) \
2049 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002050
2051/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002052 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002053 *
2054 * This routine causes first thread pending on @a fifo, if any, to
2055 * return from k_fifo_get() call with NULL value (as if timeout
2056 * expired).
2057 *
2058 * @note Can be called by ISRs.
2059 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002060 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002061 *
2062 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002063 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002064 */
2065#define k_fifo_cancel_wait(fifo) \
2066 k_queue_cancel_wait((struct k_queue *) fifo)
2067
2068/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002069 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002070 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002071 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002072 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2073 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002075 * @note Can be called by ISRs.
2076 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002077 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002078 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002079 *
2080 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002081 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002082 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002083#define k_fifo_put(fifo, data) \
2084 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002085
2086/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002087 * @brief Add an element to a FIFO queue.
2088 *
2089 * This routine adds a data item to @a fifo. There is an implicit
2090 * memory allocation from the calling thread's resource pool, which is
2091 * automatically freed when the item is removed.
2092 *
2093 * @note Can be called by ISRs.
2094 *
2095 * @param fifo Address of the FIFO.
2096 * @param data Address of the data item.
2097 *
2098 * @retval 0 on success
2099 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002100 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002101 */
2102#define k_fifo_alloc_put(fifo, data) \
2103 k_queue_alloc_append((struct k_queue *) fifo, data)
2104
2105/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002106 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002107 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002108 * This routine adds a list of data items to @a fifo in one operation.
2109 * The data items must be in a singly-linked list, with the first 32 bits
2110 * each data item pointing to the next data item; the list must be
2111 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002112 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002113 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002114 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002115 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002116 * @param head Pointer to first node in singly-linked list.
2117 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002118 *
2119 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002120 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002121 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002122#define k_fifo_put_list(fifo, head, tail) \
2123 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002124
2125/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002126 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002127 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002128 * This routine adds a list of data items to @a fifo in one operation.
2129 * The data items must be in a singly-linked list implemented using a
2130 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002131 * and must be re-initialized via sys_slist_init().
2132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002133 * @note Can be called by ISRs.
2134 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002135 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002136 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002137 *
2138 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002139 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002140 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002141#define k_fifo_put_slist(fifo, list) \
2142 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002143
2144/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002145 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002146 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002147 * This routine removes a data item from @a fifo in a "first in, first out"
2148 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002149 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002150 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2151 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002152 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002153 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002154 * or one of the special values K_NO_WAIT and K_FOREVER.
2155 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002156 * @return Address of the data item if successful; NULL if returned
2157 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002158 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002159 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002160#define k_fifo_get(fifo, timeout) \
2161 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002162
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002163/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002164 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002165 *
2166 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002167 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002168 *
2169 * @note Can be called by ISRs.
2170 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002171 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002172 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002173 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002174 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002175 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002176 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002177#define k_fifo_is_empty(fifo) \
2178 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002179
2180/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002181 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002182 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002183 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302184 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002185 * on each iteration of processing, a head container will be peeked,
2186 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002187 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002188 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002189 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002190 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002191 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002192 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002193 */
2194#define k_fifo_peek_head(fifo) \
2195 k_queue_peek_head((struct k_queue *) fifo)
2196
2197/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002198 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002199 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002200 * Return element from the tail of FIFO queue (without removing it). A usecase
2201 * for this is if elements of the FIFO queue are themselves containers. Then
2202 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002203 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002204 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002205 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002206 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002207 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002208 */
2209#define k_fifo_peek_tail(fifo) \
2210 k_queue_peek_tail((struct k_queue *) fifo)
2211
2212/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002213 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002214 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002215 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002216 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002217 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002218 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002219 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002220 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002221 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002222#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002223 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002224 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002225 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002226
Anas Nashif166f5192018-02-25 08:02:36 -06002227/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002228
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002229struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002230 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002231};
2232
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002233/**
2234 * @cond INTERNAL_HIDDEN
2235 */
2236
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002237#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002238 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002239 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002240 }
2241
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002242#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2243
Allan Stephensc98da842016-11-11 15:45:03 -05002244/**
2245 * INTERNAL_HIDDEN @endcond
2246 */
2247
2248/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002249 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002250 * @ingroup kernel_apis
2251 * @{
2252 */
2253
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002254/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002255 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002256 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002257 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002258 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002259 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002260 *
2261 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002262 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002263 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002264#define k_lifo_init(lifo) \
2265 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002266
2267/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002268 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002269 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002270 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002271 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2272 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002273 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002274 * @note Can be called by ISRs.
2275 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002276 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002277 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002278 *
2279 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002280 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002281 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002282#define k_lifo_put(lifo, data) \
2283 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002284
2285/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002286 * @brief Add an element to a LIFO queue.
2287 *
2288 * This routine adds a data item to @a lifo. There is an implicit
2289 * memory allocation from the calling thread's resource pool, which is
2290 * automatically freed when the item is removed.
2291 *
2292 * @note Can be called by ISRs.
2293 *
2294 * @param lifo Address of the LIFO.
2295 * @param data Address of the data item.
2296 *
2297 * @retval 0 on success
2298 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002299 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002300 */
2301#define k_lifo_alloc_put(lifo, data) \
2302 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2303
2304/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002305 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002306 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002307 * This routine removes a data item from @a lifo in a "last in, first out"
2308 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002309 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002310 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2311 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002312 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002313 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002314 * or one of the special values K_NO_WAIT and K_FOREVER.
2315 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002316 * @return Address of the data item if successful; NULL if returned
2317 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002318 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002319 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002320#define k_lifo_get(lifo, timeout) \
2321 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002322
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002323/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002324 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002325 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002326 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002327 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002328 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002329 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002330 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002331 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002332 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002333#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002334 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002335 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002336 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002337
Anas Nashif166f5192018-02-25 08:02:36 -06002338/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002339
2340/**
2341 * @cond INTERNAL_HIDDEN
2342 */
Andrew Boief3bee952018-05-02 17:44:39 -07002343#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002344
2345struct k_stack {
2346 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002347 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002348
Anas Nashif2f203c22016-12-18 06:57:45 -05002349 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002350 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002351};
2352
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002353#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002354 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002355 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002356 .base = stack_buffer, \
2357 .next = stack_buffer, \
2358 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002359 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002360 }
2361
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002362#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2363
Allan Stephensc98da842016-11-11 15:45:03 -05002364/**
2365 * INTERNAL_HIDDEN @endcond
2366 */
2367
2368/**
2369 * @defgroup stack_apis Stack APIs
2370 * @ingroup kernel_apis
2371 * @{
2372 */
2373
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002374/**
2375 * @brief Initialize a stack.
2376 *
2377 * This routine initializes a stack object, prior to its first use.
2378 *
2379 * @param stack Address of the stack.
2380 * @param buffer Address of array used to hold stacked values.
2381 * @param num_entries Maximum number of values that can be stacked.
2382 *
2383 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002384 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002385 */
Andrew Boief3bee952018-05-02 17:44:39 -07002386void k_stack_init(struct k_stack *stack,
2387 u32_t *buffer, unsigned int num_entries);
2388
2389
2390/**
2391 * @brief Initialize a stack.
2392 *
2393 * This routine initializes a stack object, prior to its first use. Internal
2394 * buffers will be allocated from the calling thread's resource pool.
2395 * This memory will be released if k_stack_cleanup() is called, or
2396 * userspace is enabled and the stack object loses all references to it.
2397 *
2398 * @param stack Address of the stack.
2399 * @param num_entries Maximum number of values that can be stacked.
2400 *
2401 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002402 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002403 */
2404
2405__syscall int k_stack_alloc_init(struct k_stack *stack,
2406 unsigned int num_entries);
2407
2408/**
2409 * @brief Release a stack's allocated buffer
2410 *
2411 * If a stack object was given a dynamically allocated buffer via
2412 * k_stack_alloc_init(), this will free it. This function does nothing
2413 * if the buffer wasn't dynamically allocated.
2414 *
2415 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002416 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002417 */
2418void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002419
2420/**
2421 * @brief Push an element onto a stack.
2422 *
2423 * This routine adds a 32-bit value @a data to @a stack.
2424 *
2425 * @note Can be called by ISRs.
2426 *
2427 * @param stack Address of the stack.
2428 * @param data Value to push onto the stack.
2429 *
2430 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002431 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002432 */
Andrew Boiee8734462017-09-29 16:42:07 -07002433__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002434
2435/**
2436 * @brief Pop an element from a stack.
2437 *
2438 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2439 * manner and stores the value in @a data.
2440 *
2441 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2442 *
2443 * @param stack Address of the stack.
2444 * @param data Address of area to hold the value popped from the stack.
2445 * @param timeout Waiting period to obtain a value (in milliseconds),
2446 * or one of the special values K_NO_WAIT and K_FOREVER.
2447 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002448 * @retval 0 Element popped from stack.
2449 * @retval -EBUSY Returned without waiting.
2450 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002451 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002452 */
Andrew Boiee8734462017-09-29 16:42:07 -07002453__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002454
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002455/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002456 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002457 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002458 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002459 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002460 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002461 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002462 * @param name Name of the stack.
2463 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002464 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002465 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002466#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002467 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002468 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002469 struct k_stack name \
2470 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002471 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002472 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002473
Anas Nashif166f5192018-02-25 08:02:36 -06002474/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002475
Allan Stephens6bba9b02016-11-16 14:56:54 -05002476struct k_work;
2477
Allan Stephensc98da842016-11-11 15:45:03 -05002478/**
2479 * @defgroup workqueue_apis Workqueue Thread APIs
2480 * @ingroup kernel_apis
2481 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002482 */
2483
Allan Stephens6bba9b02016-11-16 14:56:54 -05002484/**
2485 * @typedef k_work_handler_t
2486 * @brief Work item handler function type.
2487 *
2488 * A work item's handler function is executed by a workqueue's thread
2489 * when the work item is processed by the workqueue.
2490 *
2491 * @param work Address of the work item.
2492 *
2493 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002494 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002495 */
2496typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002497
2498/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002499 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002500 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002501
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002502struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002503 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002504 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002505};
2506
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002507enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002508 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002509};
2510
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002511struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002512 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002513 k_work_handler_t handler;
2514 atomic_t flags[1];
2515};
2516
Allan Stephens6bba9b02016-11-16 14:56:54 -05002517struct k_delayed_work {
2518 struct k_work work;
2519 struct _timeout timeout;
2520 struct k_work_q *work_q;
2521};
2522
2523extern struct k_work_q k_sys_work_q;
2524
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002525/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002526 * INTERNAL_HIDDEN @endcond
2527 */
2528
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002529#define _K_WORK_INITIALIZER(work_handler) \
2530 { \
2531 ._reserved = NULL, \
2532 .handler = work_handler, \
2533 .flags = { 0 } \
2534 }
2535
2536#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2537
Allan Stephens6bba9b02016-11-16 14:56:54 -05002538/**
2539 * @brief Initialize a statically-defined work item.
2540 *
2541 * This macro can be used to initialize a statically-defined workqueue work
2542 * item, prior to its first use. For example,
2543 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002544 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002545 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002546 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002547 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002548 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002549 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002550#define K_WORK_DEFINE(work, work_handler) \
2551 struct k_work work \
2552 __in_section(_k_work, static, work) = \
2553 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002554
2555/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002556 * @brief Initialize a work item.
2557 *
2558 * This routine initializes a workqueue work item, prior to its first use.
2559 *
2560 * @param work Address of work item.
2561 * @param handler Function to invoke each time work item is processed.
2562 *
2563 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002564 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002565 */
2566static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2567{
Leandro Pereira0e23ad82018-05-29 10:42:17 -07002568 *work = (struct k_work)_K_WORK_INITIALIZER(handler);
Andrew Boie945af952017-08-22 13:15:23 -07002569 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002570}
2571
2572/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002573 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002574 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002575 * This routine submits work item @a work to be processed by workqueue
2576 * @a work_q. If the work item is already pending in the workqueue's queue
2577 * as a result of an earlier submission, this routine has no effect on the
2578 * work item. If the work item has already been processed, or is currently
2579 * being processed, its work is considered complete and the work item can be
2580 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002581 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002582 * @warning
2583 * A submitted work item must not be modified until it has been processed
2584 * by the workqueue.
2585 *
2586 * @note Can be called by ISRs.
2587 *
2588 * @param work_q Address of workqueue.
2589 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002590 *
2591 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002592 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002593 */
2594static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2595 struct k_work *work)
2596{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002597 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002598 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002599 }
2600}
2601
2602/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002603 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002604 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002605 * This routine indicates if work item @a work is pending in a workqueue's
2606 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002607 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002608 * @note Can be called by ISRs.
2609 *
2610 * @param work Address of work item.
2611 *
2612 * @return 1 if work item is pending, or 0 if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002613 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002614 */
2615static inline int k_work_pending(struct k_work *work)
2616{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002617 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002618}
2619
2620/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002621 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002622 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002623 * This routine starts workqueue @a work_q. The workqueue spawns its work
2624 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002625 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002626 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002627 * @param stack Pointer to work queue thread's stack space, as defined by
2628 * K_THREAD_STACK_DEFINE()
2629 * @param stack_size Size of the work queue thread's stack (in bytes), which
2630 * should either be the same constant passed to
2631 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002632 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002633 *
2634 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002635 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002636 */
Andrew Boie507852a2017-07-25 18:47:07 -07002637extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002638 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002639 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002640
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002641/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002642 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002643 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002644 * This routine initializes a workqueue delayed work item, prior to
2645 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002646 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002647 * @param work Address of delayed work item.
2648 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002649 *
2650 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002651 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002652 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002653extern void k_delayed_work_init(struct k_delayed_work *work,
2654 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002655
2656/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002657 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002658 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002659 * This routine schedules work item @a work to be processed by workqueue
2660 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002661 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002662 * Only when the countdown completes is the work item actually submitted to
2663 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002664 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002665 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002666 * counting down cancels the existing submission and restarts the
2667 * countdown using the new delay. Note that this behavior is
2668 * inherently subject to race conditions with the pre-existing
2669 * timeouts and work queue, so care must be taken to synchronize such
2670 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002671 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002672 * @warning
2673 * A delayed work item must not be modified until it has been processed
2674 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002675 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002676 * @note Can be called by ISRs.
2677 *
2678 * @param work_q Address of workqueue.
2679 * @param work Address of delayed work item.
2680 * @param delay Delay before submitting the work item (in milliseconds).
2681 *
2682 * @retval 0 Work item countdown started.
2683 * @retval -EINPROGRESS Work item is already pending.
2684 * @retval -EINVAL Work item is being processed or has completed its work.
2685 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002686 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002687 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002688extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2689 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002690 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002691
2692/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002693 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002694 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002695 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002696 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002697 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002698 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002699 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002700 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002701 * @param work Address of delayed work item.
2702 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002703 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002704 * @retval -EINPROGRESS Work item is already pending.
2705 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002706 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002707 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002708extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002709
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002710/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002711 * @brief Submit a work item to the system workqueue.
2712 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002713 * This routine submits work item @a work to be processed by the system
2714 * workqueue. If the work item is already pending in the workqueue's queue
2715 * as a result of an earlier submission, this routine has no effect on the
2716 * work item. If the work item has already been processed, or is currently
2717 * being processed, its work is considered complete and the work item can be
2718 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002719 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002720 * @warning
2721 * Work items submitted to the system workqueue should avoid using handlers
2722 * that block or yield since this may prevent the system workqueue from
2723 * processing other work items in a timely manner.
2724 *
2725 * @note Can be called by ISRs.
2726 *
2727 * @param work Address of work item.
2728 *
2729 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002730 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002731 */
2732static inline void k_work_submit(struct k_work *work)
2733{
2734 k_work_submit_to_queue(&k_sys_work_q, work);
2735}
2736
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002737/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002738 * @brief Submit a delayed work item to the system workqueue.
2739 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002740 * This routine schedules work item @a work to be processed by the system
2741 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002742 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002743 * Only when the countdown completes is the work item actually submitted to
2744 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002745 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002746 * Submitting a previously submitted delayed work item that is still
2747 * counting down cancels the existing submission and restarts the countdown
2748 * using the new delay. If the work item is currently pending on the
2749 * workqueue's queue because the countdown has completed it is too late to
2750 * resubmit the item, and resubmission fails without impacting the work item.
2751 * If the work item has already been processed, or is currently being processed,
2752 * its work is considered complete and the work item can be resubmitted.
2753 *
2754 * @warning
2755 * Work items submitted to the system workqueue should avoid using handlers
2756 * that block or yield since this may prevent the system workqueue from
2757 * processing other work items in a timely manner.
2758 *
2759 * @note Can be called by ISRs.
2760 *
2761 * @param work Address of delayed work item.
2762 * @param delay Delay before submitting the work item (in milliseconds).
2763 *
2764 * @retval 0 Work item countdown started.
2765 * @retval -EINPROGRESS Work item is already pending.
2766 * @retval -EINVAL Work item is being processed or has completed its work.
2767 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002768 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002769 */
2770static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002771 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002772{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002773 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002774}
2775
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002776/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002777 * @brief Get time remaining before a delayed work gets scheduled.
2778 *
2779 * This routine computes the (approximate) time remaining before a
2780 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002781 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002782 *
2783 * @param work Delayed work item.
2784 *
2785 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002786 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002787 */
Kumar Galacc334c72017-04-21 10:55:34 -05002788static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002789{
2790 return _timeout_remaining_get(&work->timeout);
2791}
2792
Anas Nashif166f5192018-02-25 08:02:36 -06002793/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002794/**
Anas Nashifce78d162018-05-24 12:43:11 -05002795 * @defgroup mutex_apis Mutex APIs
2796 * @ingroup kernel_apis
2797 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002798 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002799
Anas Nashifce78d162018-05-24 12:43:11 -05002800/**
2801 * Mutex Structure
2802 * @ingroup mutex_apis
2803 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002804struct k_mutex {
2805 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002806 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002807 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002808 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002809 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002810
Anas Nashif2f203c22016-12-18 06:57:45 -05002811 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002812};
2813
Anas Nashifce78d162018-05-24 12:43:11 -05002814/**
2815 * @cond INTERNAL_HIDDEN
2816 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002817#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002818 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002819 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002820 .owner = NULL, \
2821 .lock_count = 0, \
2822 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002823 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002824 }
2825
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002826#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2827
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002828/**
Allan Stephensc98da842016-11-11 15:45:03 -05002829 * INTERNAL_HIDDEN @endcond
2830 */
2831
2832/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002833 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002834 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002835 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002836 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002837 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002838 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002839 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002840 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002841 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002842#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002843 struct k_mutex name \
2844 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002845 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002846
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002847/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002848 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002850 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002852 * Upon completion, the mutex is available and does not have an owner.
2853 *
2854 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002855 *
2856 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002857 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002858 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002859__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002860
2861/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002862 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002864 * This routine locks @a mutex. If the mutex is locked by another thread,
2865 * the calling thread waits until the mutex becomes available or until
2866 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002868 * A thread is permitted to lock a mutex it has already locked. The operation
2869 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002871 * @param mutex Address of the mutex.
2872 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002873 * or one of the special values K_NO_WAIT and K_FOREVER.
2874 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002875 * @retval 0 Mutex locked.
2876 * @retval -EBUSY Returned without waiting.
2877 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002878 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002879 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002880__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002881
2882/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002883 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002885 * This routine unlocks @a mutex. The mutex must already be locked by the
2886 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002887 *
2888 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002889 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002890 * thread.
2891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002892 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002893 *
2894 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002895 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002896 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002897__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002898
Allan Stephensc98da842016-11-11 15:45:03 -05002899/**
Anas Nashif166f5192018-02-25 08:02:36 -06002900 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002901 */
2902
2903/**
2904 * @cond INTERNAL_HIDDEN
2905 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002906
2907struct k_sem {
2908 _wait_q_t wait_q;
2909 unsigned int count;
2910 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002911 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002912
Anas Nashif2f203c22016-12-18 06:57:45 -05002913 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002914};
2915
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002916#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002917 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002918 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002919 .count = initial_count, \
2920 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002921 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002922 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002923 }
2924
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002925#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2926
Allan Stephensc98da842016-11-11 15:45:03 -05002927/**
2928 * INTERNAL_HIDDEN @endcond
2929 */
2930
2931/**
2932 * @defgroup semaphore_apis Semaphore APIs
2933 * @ingroup kernel_apis
2934 * @{
2935 */
2936
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002937/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002938 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002939 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002940 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002941 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002942 * @param sem Address of the semaphore.
2943 * @param initial_count Initial semaphore count.
2944 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002945 *
2946 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002947 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002948 */
Andrew Boie99280232017-09-29 14:17:47 -07002949__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2950 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002951
2952/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002953 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002954 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002955 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002957 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2958 *
2959 * @param sem Address of the semaphore.
2960 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002961 * or one of the special values K_NO_WAIT and K_FOREVER.
2962 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002963 * @note When porting code from the nanokernel legacy API to the new API, be
2964 * careful with the return value of this function. The return value is the
2965 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2966 * non-zero means failure, while the nano_sem_take family returns 1 for success
2967 * and 0 for failure.
2968 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002969 * @retval 0 Semaphore taken.
2970 * @retval -EBUSY Returned without waiting.
2971 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002972 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002973 */
Andrew Boie99280232017-09-29 14:17:47 -07002974__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002975
2976/**
2977 * @brief Give a semaphore.
2978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002979 * This routine gives @a sem, unless the semaphore is already at its maximum
2980 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002981 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002982 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002983 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002984 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002985 *
2986 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002987 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002988 */
Andrew Boie99280232017-09-29 14:17:47 -07002989__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002990
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002991/**
2992 * @brief Reset a semaphore's count to zero.
2993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002994 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002996 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002997 *
2998 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002999 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003000 */
Andrew Boie990bf162017-10-03 12:36:49 -07003001__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003002
Anas Nashif954d5502018-02-25 08:37:28 -06003003/**
3004 * @internal
3005 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003006static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003007{
3008 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003009}
3010
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003011/**
3012 * @brief Get a semaphore's count.
3013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003014 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003016 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003018 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003019 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003020 */
Andrew Boie990bf162017-10-03 12:36:49 -07003021__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003022
Anas Nashif954d5502018-02-25 08:37:28 -06003023/**
3024 * @internal
3025 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003026static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003027{
3028 return sem->count;
3029}
3030
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003031/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003032 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003033 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003034 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003035 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003036 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003038 * @param name Name of the semaphore.
3039 * @param initial_count Initial semaphore count.
3040 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003041 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003042 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003043#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003044 struct k_sem name \
3045 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003046 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303047 BUILD_ASSERT(((count_limit) != 0) && \
3048 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003049
Anas Nashif166f5192018-02-25 08:02:36 -06003050/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003051
3052/**
3053 * @defgroup alert_apis Alert APIs
3054 * @ingroup kernel_apis
3055 * @{
3056 */
3057
Allan Stephens5eceb852016-11-16 10:16:30 -05003058/**
3059 * @typedef k_alert_handler_t
3060 * @brief Alert handler function type.
3061 *
3062 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07003063 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05003064 * and is only invoked if the alert has been initialized with one.
3065 *
3066 * @param alert Address of the alert.
3067 *
3068 * @return 0 if alert has been consumed; non-zero if alert should pend.
3069 */
3070typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05003071
Anas Nashif166f5192018-02-25 08:02:36 -06003072/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003073
3074/**
3075 * @cond INTERNAL_HIDDEN
3076 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003077
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003078#define K_ALERT_DEFAULT NULL
3079#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003080
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003081struct k_alert {
3082 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003083 atomic_t send_count;
3084 struct k_work work_item;
3085 struct k_sem sem;
3086
Anas Nashif2f203c22016-12-18 06:57:45 -05003087 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003088};
3089
Anas Nashif954d5502018-02-25 08:37:28 -06003090/**
3091 * @internal
3092 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003093extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003094
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003095#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003096 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003097 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003098 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003099 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
3100 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003101 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003102 }
3103
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003104#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
3105
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003106/**
Allan Stephensc98da842016-11-11 15:45:03 -05003107 * INTERNAL_HIDDEN @endcond
3108 */
3109
3110/**
3111 * @addtogroup alert_apis
3112 * @{
3113 */
3114
3115/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003116 * @def K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts)
3117 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003118 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003119 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003120 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003121 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003122 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003124 * @param name Name of the alert.
3125 * @param alert_handler Action to take when alert is sent. Specify either
3126 * the address of a function to be invoked by the system workqueue
3127 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
3128 * K_ALERT_DEFAULT (which causes the alert to pend).
3129 * @param max_num_pending_alerts Maximum number of pending alerts.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003130 *
3131 * @req K-ALERT-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003132 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003133#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003134 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003135 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003136 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003137 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003138
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003139/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003140 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003141 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003142 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003144 * @param alert Address of the alert.
3145 * @param handler Action to take when alert is sent. Specify either the address
3146 * of a function to be invoked by the system workqueue thread,
3147 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3148 * K_ALERT_DEFAULT (which causes the alert to pend).
3149 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003150 *
3151 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003152 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003153 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003154extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3155 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003156
3157/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003158 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003159 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003160 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003161 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003162 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3163 *
3164 * @param alert Address of the alert.
3165 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003166 * or one of the special values K_NO_WAIT and K_FOREVER.
3167 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003168 * @retval 0 Alert received.
3169 * @retval -EBUSY Returned without waiting.
3170 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003171 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003172 */
Andrew Boie310e9872017-09-29 04:41:15 -07003173__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003174
3175/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003176 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003177 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003178 * This routine signals @a alert. The action specified for @a alert will
3179 * be taken, which may trigger the execution of an alert handler function
3180 * and/or cause the alert to pend (assuming the alert has not reached its
3181 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003182 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003183 * @note Can be called by ISRs.
3184 *
3185 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003186 *
3187 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003188 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003189 */
Andrew Boie310e9872017-09-29 04:41:15 -07003190__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003191
3192/**
Anas Nashif166f5192018-02-25 08:02:36 -06003193 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003194 */
3195
Allan Stephensc98da842016-11-11 15:45:03 -05003196/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003197 * @defgroup msgq_apis Message Queue APIs
3198 * @ingroup kernel_apis
3199 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003200 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003201
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003202/**
3203 * @brief Message Queue Structure
3204 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003205struct k_msgq {
3206 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003207 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003208 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003209 char *buffer_start;
3210 char *buffer_end;
3211 char *read_ptr;
3212 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003213 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003214
Anas Nashif2f203c22016-12-18 06:57:45 -05003215 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003216 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003217};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003218/**
3219 * @cond INTERNAL_HIDDEN
3220 */
3221
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003222
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003223#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003224 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003225 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003226 .max_msgs = q_max_msgs, \
3227 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003228 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003229 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003230 .read_ptr = q_buffer, \
3231 .write_ptr = q_buffer, \
3232 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003233 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003234 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003235#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003236/**
3237 * INTERNAL_HIDDEN @endcond
3238 */
3239
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003240
Andrew Boie0fe789f2018-04-12 18:35:56 -07003241#define K_MSGQ_FLAG_ALLOC BIT(0)
3242
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003243/**
3244 * @brief Message Queue Attributes
3245 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303246struct k_msgq_attrs {
3247 size_t msg_size;
3248 u32_t max_msgs;
3249 u32_t used_msgs;
3250};
3251
Allan Stephensc98da842016-11-11 15:45:03 -05003252
3253/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003254 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003255 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003256 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3257 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003258 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3259 * message is similarly aligned to this boundary, @a q_msg_size must also be
3260 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003261 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003262 * The message queue can be accessed outside the module where it is defined
3263 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003264 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003265 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003266 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003267 * @param q_name Name of the message queue.
3268 * @param q_msg_size Message size (in bytes).
3269 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003270 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003271 *
3272 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003273 */
3274#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003275 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003276 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003277 struct k_msgq q_name \
3278 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003279 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003280 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003281
Peter Mitsisd7a37502016-10-13 11:37:40 -04003282/**
3283 * @brief Initialize a message queue.
3284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003285 * This routine initializes a message queue object, prior to its first use.
3286 *
Allan Stephensda827222016-11-09 14:23:58 -06003287 * The message queue's ring buffer must contain space for @a max_msgs messages,
3288 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3289 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3290 * that each message is similarly aligned to this boundary, @a q_msg_size
3291 * must also be a multiple of N.
3292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003293 * @param q Address of the message queue.
3294 * @param buffer Pointer to ring buffer that holds queued messages.
3295 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003296 * @param max_msgs Maximum number of messages that can be queued.
3297 *
3298 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003299 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003300 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003301void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3302 u32_t max_msgs);
3303
3304/**
3305 * @brief Initialize a message queue.
3306 *
3307 * This routine initializes a message queue object, prior to its first use,
3308 * allocating its internal ring buffer from the calling thread's resource
3309 * pool.
3310 *
3311 * Memory allocated for the ring buffer can be released by calling
3312 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3313 * all of its references.
3314 *
3315 * @param q Address of the message queue.
3316 * @param msg_size Message size (in bytes).
3317 * @param max_msgs Maximum number of messages that can be queued.
3318 *
3319 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3320 * thread's resource pool, or -EINVAL if the size parameters cause
3321 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003322 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003323 */
3324__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3325 u32_t max_msgs);
3326
3327
3328void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003329
3330/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003331 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003332 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003333 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003334 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003335 * @note Can be called by ISRs.
3336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003337 * @param q Address of the message queue.
3338 * @param data Pointer to the message.
3339 * @param timeout Waiting period to add the message (in milliseconds),
3340 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003341 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003342 * @retval 0 Message sent.
3343 * @retval -ENOMSG Returned without waiting or queue purged.
3344 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003345 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003346 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003347__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003348
3349/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003350 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003351 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003352 * This routine receives a message from message queue @a q in a "first in,
3353 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003354 *
Allan Stephensc98da842016-11-11 15:45:03 -05003355 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003356 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003357 * @param q Address of the message queue.
3358 * @param data Address of area to hold the received message.
3359 * @param timeout Waiting period to receive the message (in milliseconds),
3360 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003361 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003362 * @retval 0 Message received.
3363 * @retval -ENOMSG Returned without waiting.
3364 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003365 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003366 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003367__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003368
3369/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003370 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003371 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003372 * This routine discards all unreceived messages in a message queue's ring
3373 * buffer. Any threads that are blocked waiting to send a message to the
3374 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003375 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003376 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003377 *
3378 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003379 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003380 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003381__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003382
Peter Mitsis67be2492016-10-07 11:44:34 -04003383/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003384 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003386 * This routine returns the number of unused entries in a message queue's
3387 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003388 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003389 * @param q Address of the message queue.
3390 *
3391 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003392 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003393 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003394__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3395
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303396/**
3397 * @brief Get basic attributes of a message queue.
3398 *
3399 * This routine fetches basic attributes of message queue into attr argument.
3400 *
3401 * @param q Address of the message queue.
3402 * @param attrs pointer to message queue attribute structure.
3403 *
3404 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003405 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303406 */
3407__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3408
3409
Andrew Boie82edb6e2017-10-02 10:53:06 -07003410static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003411{
3412 return q->max_msgs - q->used_msgs;
3413}
3414
Peter Mitsisd7a37502016-10-13 11:37:40 -04003415/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003416 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003418 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003419 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003420 * @param q Address of the message queue.
3421 *
3422 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003423 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003424 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003425__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3426
3427static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003428{
3429 return q->used_msgs;
3430}
3431
Anas Nashif166f5192018-02-25 08:02:36 -06003432/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003433
3434/**
3435 * @defgroup mem_pool_apis Memory Pool APIs
3436 * @ingroup kernel_apis
3437 * @{
3438 */
3439
Andy Ross73cb9582017-05-09 10:42:39 -07003440/* Note on sizing: the use of a 20 bit field for block means that,
3441 * assuming a reasonable minimum block size of 16 bytes, we're limited
3442 * to 16M of memory managed by a single pool. Long term it would be
3443 * good to move to a variable bit size based on configuration.
3444 */
3445struct k_mem_block_id {
3446 u32_t pool : 8;
3447 u32_t level : 4;
3448 u32_t block : 20;
3449};
3450
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003451struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003452 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003453 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003454};
3455
Anas Nashif166f5192018-02-25 08:02:36 -06003456/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003457
3458/**
3459 * @defgroup mailbox_apis Mailbox APIs
3460 * @ingroup kernel_apis
3461 * @{
3462 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003463
3464struct k_mbox_msg {
3465 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003466 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003467 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003468 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003469 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003470 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003471 /** sender's message data buffer */
3472 void *tx_data;
3473 /** internal use only - needed for legacy API support */
3474 void *_rx_data;
3475 /** message data block descriptor */
3476 struct k_mem_block tx_block;
3477 /** source thread id */
3478 k_tid_t rx_source_thread;
3479 /** target thread id */
3480 k_tid_t tx_target_thread;
3481 /** internal use only - thread waiting on send (may be a dummy) */
3482 k_tid_t _syncing_thread;
3483#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3484 /** internal use only - semaphore used during asynchronous send */
3485 struct k_sem *_async_sem;
3486#endif
3487};
3488
3489struct k_mbox {
3490 _wait_q_t tx_msg_queue;
3491 _wait_q_t rx_msg_queue;
3492
Anas Nashif2f203c22016-12-18 06:57:45 -05003493 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003494};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003495/**
3496 * @cond INTERNAL_HIDDEN
3497 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003498
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003499#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003500 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003501 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3502 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003503 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003504 }
3505
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003506#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3507
Peter Mitsis12092702016-10-14 12:57:23 -04003508/**
Allan Stephensc98da842016-11-11 15:45:03 -05003509 * INTERNAL_HIDDEN @endcond
3510 */
3511
3512/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003513 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003514 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003515 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003516 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003517 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003519 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003520 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003521 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003522#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003523 struct k_mbox name \
3524 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003525 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003526
Peter Mitsis12092702016-10-14 12:57:23 -04003527/**
3528 * @brief Initialize a mailbox.
3529 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003530 * This routine initializes a mailbox object, prior to its first use.
3531 *
3532 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003533 *
3534 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003535 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003536 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003537extern void k_mbox_init(struct k_mbox *mbox);
3538
Peter Mitsis12092702016-10-14 12:57:23 -04003539/**
3540 * @brief Send a mailbox message in a synchronous manner.
3541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003542 * This routine sends a message to @a mbox and waits for a receiver to both
3543 * receive and process it. The message data may be in a buffer, in a memory
3544 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003546 * @param mbox Address of the mailbox.
3547 * @param tx_msg Address of the transmit message descriptor.
3548 * @param timeout Waiting period for the message to be received (in
3549 * milliseconds), or one of the special values K_NO_WAIT
3550 * and K_FOREVER. Once the message has been received,
3551 * this routine waits as long as necessary for the message
3552 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003553 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003554 * @retval 0 Message sent.
3555 * @retval -ENOMSG Returned without waiting.
3556 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003557 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003558 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003559extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003560 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003561
Peter Mitsis12092702016-10-14 12:57:23 -04003562/**
3563 * @brief Send a mailbox message in an asynchronous manner.
3564 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003565 * This routine sends a message to @a mbox without waiting for a receiver
3566 * to process it. The message data may be in a buffer, in a memory pool block,
3567 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3568 * will be given when the message has been both received and completely
3569 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003570 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003571 * @param mbox Address of the mailbox.
3572 * @param tx_msg Address of the transmit message descriptor.
3573 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003574 *
3575 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003576 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003577 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003578extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003579 struct k_sem *sem);
3580
Peter Mitsis12092702016-10-14 12:57:23 -04003581/**
3582 * @brief Receive a mailbox message.
3583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003584 * This routine receives a message from @a mbox, then optionally retrieves
3585 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003586 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003587 * @param mbox Address of the mailbox.
3588 * @param rx_msg Address of the receive message descriptor.
3589 * @param buffer Address of the buffer to receive data, or NULL to defer data
3590 * retrieval and message disposal until later.
3591 * @param timeout Waiting period for a message to be received (in
3592 * milliseconds), or one of the special values K_NO_WAIT
3593 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003594 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003595 * @retval 0 Message received.
3596 * @retval -ENOMSG Returned without waiting.
3597 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003598 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003599 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003600extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003601 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003602
3603/**
3604 * @brief Retrieve mailbox message data into a buffer.
3605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003606 * This routine completes the processing of a received message by retrieving
3607 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003608 *
3609 * Alternatively, this routine can be used to dispose of a received message
3610 * without retrieving its data.
3611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003612 * @param rx_msg Address of the receive message descriptor.
3613 * @param buffer Address of the buffer to receive data, or NULL to discard
3614 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003615 *
3616 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003617 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003618 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003619extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003620
3621/**
3622 * @brief Retrieve mailbox message data into a memory pool block.
3623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003624 * This routine completes the processing of a received message by retrieving
3625 * its data into a memory pool block, then disposing of the message.
3626 * The memory pool block that results from successful retrieval must be
3627 * returned to the pool once the data has been processed, even in cases
3628 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003629 *
3630 * Alternatively, this routine can be used to dispose of a received message
3631 * without retrieving its data. In this case there is no need to return a
3632 * memory pool block to the pool.
3633 *
3634 * This routine allocates a new memory pool block for the data only if the
3635 * data is not already in one. If a new block cannot be allocated, the routine
3636 * returns a failure code and the received message is left unchanged. This
3637 * permits the caller to reattempt data retrieval at a later time or to dispose
3638 * of the received message without retrieving its data.
3639 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003640 * @param rx_msg Address of a receive message descriptor.
3641 * @param pool Address of memory pool, or NULL to discard data.
3642 * @param block Address of the area to hold memory pool block info.
3643 * @param timeout Waiting period to wait for a memory pool block (in
3644 * milliseconds), or one of the special values K_NO_WAIT
3645 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003646 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003647 * @retval 0 Data retrieved.
3648 * @retval -ENOMEM Returned without waiting.
3649 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003650 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003651 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003652extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003653 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003654 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003655
Anas Nashif166f5192018-02-25 08:02:36 -06003656/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003657
3658/**
Anas Nashifce78d162018-05-24 12:43:11 -05003659 * @defgroup pipe_apis Pipe APIs
3660 * @ingroup kernel_apis
3661 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003662 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003663
Anas Nashifce78d162018-05-24 12:43:11 -05003664/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003665struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003666 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3667 size_t size; /**< Buffer size */
3668 size_t bytes_used; /**< # bytes used in buffer */
3669 size_t read_index; /**< Where in buffer to read from */
3670 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003671
3672 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003673 _wait_q_t readers; /**< Reader wait queue */
3674 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003675 } wait_q;
3676
Anas Nashif2f203c22016-12-18 06:57:45 -05003677 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Anas Nashifce78d162018-05-24 12:43:11 -05003678 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003679};
3680
Anas Nashifce78d162018-05-24 12:43:11 -05003681/**
3682 * @cond INTERNAL_HIDDEN
3683 */
3684#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3685
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003686#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003687 { \
3688 .buffer = pipe_buffer, \
3689 .size = pipe_buffer_size, \
3690 .bytes_used = 0, \
3691 .read_index = 0, \
3692 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003693 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3694 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003695 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003696 }
3697
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003698#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3699
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003700/**
Allan Stephensc98da842016-11-11 15:45:03 -05003701 * INTERNAL_HIDDEN @endcond
3702 */
3703
3704/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003705 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003706 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003707 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003708 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003709 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003710 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003711 * @param name Name of the pipe.
3712 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3713 * or zero if no ring buffer is used.
3714 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003715 *
3716 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003717 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003718#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3719 static unsigned char __kernel_noinit __aligned(pipe_align) \
3720 _k_pipe_buf_##name[pipe_buffer_size]; \
3721 struct k_pipe name \
3722 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003723 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003724
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003725/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003726 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003727 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003728 * This routine initializes a pipe object, prior to its first use.
3729 *
3730 * @param pipe Address of the pipe.
3731 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3732 * is used.
3733 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3734 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003735 *
3736 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003737 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003738 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003739void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3740
3741/**
3742 * @brief Release a pipe's allocated buffer
3743 *
3744 * If a pipe object was given a dynamically allocated buffer via
3745 * k_pipe_alloc_init(), this will free it. This function does nothing
3746 * if the buffer wasn't dynamically allocated.
3747 *
3748 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003749 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003750 */
3751void k_pipe_cleanup(struct k_pipe *pipe);
3752
3753/**
3754 * @brief Initialize a pipe and allocate a buffer for it
3755 *
3756 * Storage for the buffer region will be allocated from the calling thread's
3757 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3758 * or userspace is enabled and the pipe object loses all references to it.
3759 *
3760 * This function should only be called on uninitialized pipe objects.
3761 *
3762 * @param pipe Address of the pipe.
3763 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3764 * buffer is used.
3765 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003766 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003767 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003768 */
3769__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003770
3771/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003772 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003773 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003774 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003775 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003776 * @param pipe Address of the pipe.
3777 * @param data Address of data to write.
3778 * @param bytes_to_write Size of data (in bytes).
3779 * @param bytes_written Address of area to hold the number of bytes written.
3780 * @param min_xfer Minimum number of bytes to write.
3781 * @param timeout Waiting period to wait for the data to be written (in
3782 * milliseconds), or one of the special values K_NO_WAIT
3783 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003784 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003785 * @retval 0 At least @a min_xfer bytes of data were written.
3786 * @retval -EIO Returned without waiting; zero data bytes were written.
3787 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003788 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003789 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003790 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003791__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3792 size_t bytes_to_write, size_t *bytes_written,
3793 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003794
3795/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003796 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003798 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003800 * @param pipe Address of the pipe.
3801 * @param data Address to place the data read from pipe.
3802 * @param bytes_to_read Maximum number of data bytes to read.
3803 * @param bytes_read Address of area to hold the number of bytes read.
3804 * @param min_xfer Minimum number of data bytes to read.
3805 * @param timeout Waiting period to wait for the data to be read (in
3806 * milliseconds), or one of the special values K_NO_WAIT
3807 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003808 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003809 * @retval 0 At least @a min_xfer bytes of data were read.
3810 * @retval -EIO Returned without waiting; zero data bytes were read.
3811 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003812 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003813 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003814 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003815__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3816 size_t bytes_to_read, size_t *bytes_read,
3817 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003818
3819/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003820 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003821 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003822 * This routine writes the data contained in a memory block to @a pipe.
3823 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003824 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003825 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003826 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003827 * @param block Memory block containing data to send
3828 * @param size Number of data bytes in memory block to send
3829 * @param sem Semaphore to signal upon completion (else NULL)
3830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003831 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003832 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003833 */
3834extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3835 size_t size, struct k_sem *sem);
3836
Anas Nashif166f5192018-02-25 08:02:36 -06003837/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003838
Allan Stephensc98da842016-11-11 15:45:03 -05003839/**
3840 * @cond INTERNAL_HIDDEN
3841 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003842
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003843struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003844 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003845 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003846 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003847 char *buffer;
3848 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003849 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003850
Anas Nashif2f203c22016-12-18 06:57:45 -05003851 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003852};
3853
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003854#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003855 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003856 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003857 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003858 .num_blocks = slab_num_blocks, \
3859 .block_size = slab_block_size, \
3860 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003861 .free_list = NULL, \
3862 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003863 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003864 }
3865
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003866#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3867
3868
Peter Mitsis578f9112016-10-07 13:50:31 -04003869/**
Allan Stephensc98da842016-11-11 15:45:03 -05003870 * INTERNAL_HIDDEN @endcond
3871 */
3872
3873/**
3874 * @defgroup mem_slab_apis Memory Slab APIs
3875 * @ingroup kernel_apis
3876 * @{
3877 */
3878
3879/**
Allan Stephensda827222016-11-09 14:23:58 -06003880 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003881 *
Allan Stephensda827222016-11-09 14:23:58 -06003882 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003883 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003884 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3885 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003886 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003887 *
Allan Stephensda827222016-11-09 14:23:58 -06003888 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003889 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003890 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003891 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003892 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003893 * @param name Name of the memory slab.
3894 * @param slab_block_size Size of each memory block (in bytes).
3895 * @param slab_num_blocks Number memory blocks.
3896 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003897 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003898 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003899#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3900 char __noinit __aligned(slab_align) \
3901 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3902 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003903 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003904 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003905 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003906
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003907/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003908 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003909 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003910 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003911 *
Allan Stephensda827222016-11-09 14:23:58 -06003912 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3913 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3914 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3915 * To ensure that each memory block is similarly aligned to this boundary,
3916 * @a slab_block_size must also be a multiple of N.
3917 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003918 * @param slab Address of the memory slab.
3919 * @param buffer Pointer to buffer used for the memory blocks.
3920 * @param block_size Size of each memory block (in bytes).
3921 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003922 *
3923 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003924 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003925 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003926extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003927 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003928
3929/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003930 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003932 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003934 * @param slab Address of the memory slab.
3935 * @param mem Pointer to block address area.
3936 * @param timeout Maximum time to wait for operation to complete
3937 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3938 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003939 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003940 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003941 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003942 * @retval -ENOMEM Returned without waiting.
3943 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003944 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003945 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003946extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003947 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003948
3949/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003950 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003952 * This routine releases a previously allocated memory block back to its
3953 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003954 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003955 * @param slab Address of the memory slab.
3956 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003957 *
3958 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003959 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003960 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003961extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003962
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003963/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003964 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003965 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003966 * This routine gets the number of memory blocks that are currently
3967 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003969 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003971 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003972 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003973 */
Kumar Galacc334c72017-04-21 10:55:34 -05003974static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003975{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003976 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003977}
3978
Peter Mitsisc001aa82016-10-13 13:53:37 -04003979/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003980 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003981 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003982 * This routine gets the number of memory blocks that are currently
3983 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003984 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003985 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003987 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003988 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04003989 */
Kumar Galacc334c72017-04-21 10:55:34 -05003990static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003991{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003992 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003993}
3994
Anas Nashif166f5192018-02-25 08:02:36 -06003995/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003996
3997/**
3998 * @cond INTERNAL_HIDDEN
3999 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004000
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004001struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004002 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004003 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004004};
4005
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004006/**
Allan Stephensc98da842016-11-11 15:45:03 -05004007 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004008 */
4009
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004010/**
Allan Stephensc98da842016-11-11 15:45:03 -05004011 * @addtogroup mem_pool_apis
4012 * @{
4013 */
4014
4015/**
4016 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004018 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4019 * long. The memory pool allows blocks to be repeatedly partitioned into
4020 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004021 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004022 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004023 * If the pool is to be accessed outside the module where it is defined, it
4024 * can be declared via
4025 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004026 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004028 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004029 * @param minsz Size of the smallest blocks in the pool (in bytes).
4030 * @param maxsz Size of the largest blocks in the pool (in bytes).
4031 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004032 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004033 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004034 */
Andy Ross73cb9582017-05-09 10:42:39 -07004035#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4036 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4037 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08004038 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07004039 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004040 .base = { \
4041 .buf = _mpool_buf_##name, \
4042 .max_sz = maxsz, \
4043 .n_max = nmax, \
4044 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
4045 .levels = _mpool_lvls_##name, \
4046 .flags = SYS_MEM_POOL_KERNEL \
4047 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004048 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004049
Peter Mitsis937042c2016-10-13 13:18:26 -04004050/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004051 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004052 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004053 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004055 * @param pool Address of the memory pool.
4056 * @param block Pointer to block descriptor for the allocated memory.
4057 * @param size Amount of memory to allocate (in bytes).
4058 * @param timeout Maximum time to wait for operation to complete
4059 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4060 * or K_FOREVER to wait as long as necessary.
4061 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004062 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004063 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004064 * @retval -ENOMEM Returned without waiting.
4065 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004066 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004067 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004068extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004069 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004070
4071/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004072 * @brief Allocate memory from a memory pool with malloc() semantics
4073 *
4074 * Such memory must be released using k_free().
4075 *
4076 * @param pool Address of the memory pool.
4077 * @param size Amount of memory to allocate (in bytes).
4078 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004079 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004080 */
4081extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4082
4083/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004084 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004085 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004086 * This routine releases a previously allocated memory block back to its
4087 * memory pool.
4088 *
4089 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004090 *
4091 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004092 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004093 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004094extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004095
4096/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004097 * @brief Free memory allocated from a memory pool.
4098 *
4099 * This routine releases a previously allocated memory block back to its
4100 * memory pool
4101 *
4102 * @param id Memory block identifier.
4103 *
4104 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004105 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004106 */
4107extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4108
4109/**
Anas Nashif166f5192018-02-25 08:02:36 -06004110 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004111 */
4112
4113/**
4114 * @defgroup heap_apis Heap Memory Pool APIs
4115 * @ingroup kernel_apis
4116 * @{
4117 */
4118
4119/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004120 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004121 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004122 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004123 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004125 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004127 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004128 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004129 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004130extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004131
4132/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004133 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004134 *
4135 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004136 * returned must have been allocated from the heap memory pool or
4137 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004138 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004139 * If @a ptr is NULL, no operation is performed.
4140 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004141 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004142 *
4143 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004144 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004145 */
4146extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004147
Allan Stephensc98da842016-11-11 15:45:03 -05004148/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004149 * @brief Allocate memory from heap, array style
4150 *
4151 * This routine provides traditional calloc() semantics. Memory is
4152 * allocated from the heap memory pool and zeroed.
4153 *
4154 * @param nmemb Number of elements in the requested array
4155 * @param size Size of each array element (in bytes).
4156 *
4157 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004158 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004159 */
4160extern void *k_calloc(size_t nmemb, size_t size);
4161
Anas Nashif166f5192018-02-25 08:02:36 -06004162/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004163
Benjamin Walshacc68c12017-01-29 18:57:45 -05004164/* polling API - PRIVATE */
4165
Benjamin Walshb0179862017-02-02 16:39:57 -05004166#ifdef CONFIG_POLL
4167#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
4168#else
4169#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
4170#endif
4171
Benjamin Walshacc68c12017-01-29 18:57:45 -05004172/* private - implementation data created as needed, per-type */
4173struct _poller {
4174 struct k_thread *thread;
Andy Ross55a7e462018-05-31 11:58:09 -07004175 volatile int is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004176};
4177
4178/* private - types bit positions */
4179enum _poll_types_bits {
4180 /* can be used to ignore an event */
4181 _POLL_TYPE_IGNORE,
4182
4183 /* to be signaled by k_poll_signal() */
4184 _POLL_TYPE_SIGNAL,
4185
4186 /* semaphore availability */
4187 _POLL_TYPE_SEM_AVAILABLE,
4188
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004189 /* queue/fifo/lifo data availability */
4190 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004191
4192 _POLL_NUM_TYPES
4193};
4194
4195#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4196
4197/* private - states bit positions */
4198enum _poll_states_bits {
4199 /* default state when creating event */
4200 _POLL_STATE_NOT_READY,
4201
Benjamin Walshacc68c12017-01-29 18:57:45 -05004202 /* signaled by k_poll_signal() */
4203 _POLL_STATE_SIGNALED,
4204
4205 /* semaphore is available */
4206 _POLL_STATE_SEM_AVAILABLE,
4207
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004208 /* data is available to read on queue/fifo/lifo */
4209 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004210
4211 _POLL_NUM_STATES
4212};
4213
4214#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4215
4216#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004217 (32 - (0 \
4218 + 8 /* tag */ \
4219 + _POLL_NUM_TYPES \
4220 + _POLL_NUM_STATES \
4221 + 1 /* modes */ \
4222 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004223
Benjamin Walshacc68c12017-01-29 18:57:45 -05004224/* end of polling API - PRIVATE */
4225
4226
4227/**
4228 * @defgroup poll_apis Async polling APIs
4229 * @ingroup kernel_apis
4230 * @{
4231 */
4232
4233/* Public polling API */
4234
4235/* public - values for k_poll_event.type bitfield */
4236#define K_POLL_TYPE_IGNORE 0
4237#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4238#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004239#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4240#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004241
4242/* public - polling modes */
4243enum k_poll_modes {
4244 /* polling thread does not take ownership of objects when available */
4245 K_POLL_MODE_NOTIFY_ONLY = 0,
4246
4247 K_POLL_NUM_MODES
4248};
4249
4250/* public - values for k_poll_event.state bitfield */
4251#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004252#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4253#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004254#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4255#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004256
4257/* public - poll signal object */
4258struct k_poll_signal {
4259 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004260 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004261
4262 /*
4263 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4264 * user resets it to 0.
4265 */
4266 unsigned int signaled;
4267
4268 /* custom result value passed to k_poll_signal() if needed */
4269 int result;
4270};
4271
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004272#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004273 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004274 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004275 .signaled = 0, \
4276 .result = 0, \
4277 }
4278
4279struct k_poll_event {
4280 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004281 sys_dnode_t _node;
4282
4283 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004284 struct _poller *poller;
4285
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004286 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004287 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004288
Benjamin Walshacc68c12017-01-29 18:57:45 -05004289 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004290 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004291
4292 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004293 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004294
4295 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004296 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004297
4298 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004299 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004300
4301 /* per-type data */
4302 union {
4303 void *obj;
4304 struct k_poll_signal *signal;
4305 struct k_sem *sem;
4306 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004307 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004308 };
4309};
4310
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004311#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004312 { \
4313 .poller = NULL, \
4314 .type = event_type, \
4315 .state = K_POLL_STATE_NOT_READY, \
4316 .mode = event_mode, \
4317 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004318 { .obj = event_obj }, \
4319 }
4320
4321#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4322 event_tag) \
4323 { \
4324 .type = event_type, \
4325 .tag = event_tag, \
4326 .state = K_POLL_STATE_NOT_READY, \
4327 .mode = event_mode, \
4328 .unused = 0, \
4329 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004330 }
4331
4332/**
4333 * @brief Initialize one struct k_poll_event instance
4334 *
4335 * After this routine is called on a poll event, the event it ready to be
4336 * placed in an event array to be passed to k_poll().
4337 *
4338 * @param event The event to initialize.
4339 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4340 * values. Only values that apply to the same object being polled
4341 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4342 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004343 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004344 * @param obj Kernel object or poll signal.
4345 *
4346 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004347 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004348 */
4349
Kumar Galacc334c72017-04-21 10:55:34 -05004350extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004351 int mode, void *obj);
4352
4353/**
4354 * @brief Wait for one or many of multiple poll events to occur
4355 *
4356 * This routine allows a thread to wait concurrently for one or many of
4357 * multiple poll events to have occurred. Such events can be a kernel object
4358 * being available, like a semaphore, or a poll signal event.
4359 *
4360 * When an event notifies that a kernel object is available, the kernel object
4361 * is not "given" to the thread calling k_poll(): it merely signals the fact
4362 * that the object was available when the k_poll() call was in effect. Also,
4363 * all threads trying to acquire an object the regular way, i.e. by pending on
4364 * the object, have precedence over the thread polling on the object. This
4365 * means that the polling thread will never get the poll event on an object
4366 * until the object becomes available and its pend queue is empty. For this
4367 * reason, the k_poll() call is more effective when the objects being polled
4368 * only have one thread, the polling thread, trying to acquire them.
4369 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004370 * When k_poll() returns 0, the caller should loop on all the events that were
4371 * passed to k_poll() and check the state field for the values that were
4372 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004373 *
4374 * Before being reused for another call to k_poll(), the user has to reset the
4375 * state field to K_POLL_STATE_NOT_READY.
4376 *
Andrew Boie3772f772018-05-07 16:52:57 -07004377 * When called from user mode, a temporary memory allocation is required from
4378 * the caller's resource pool.
4379 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004380 * @param events An array of pointers to events to be polled for.
4381 * @param num_events The number of events in the array.
4382 * @param timeout Waiting period for an event to be ready (in milliseconds),
4383 * or one of the special values K_NO_WAIT and K_FOREVER.
4384 *
4385 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004386 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02004387 * @retval -EINTR Poller thread has been interrupted.
Andrew Boie3772f772018-05-07 16:52:57 -07004388 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4389 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004390 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004391 */
4392
Andrew Boie3772f772018-05-07 16:52:57 -07004393__syscall int k_poll(struct k_poll_event *events, int num_events,
4394 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004395
4396/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004397 * @brief Initialize a poll signal object.
4398 *
4399 * Ready a poll signal object to be signaled via k_poll_signal().
4400 *
4401 * @param signal A poll signal.
4402 *
4403 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004404 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004405 */
4406
Andrew Boie3772f772018-05-07 16:52:57 -07004407__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4408
4409/*
4410 * @brief Reset a poll signal object's state to unsignaled.
4411 *
4412 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004413 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004414 */
4415__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4416
4417static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4418{
4419 signal->signaled = 0;
4420}
4421
4422/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004423 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004424 *
4425 * @param signal A poll signal object
4426 * @param signaled An integer buffer which will be written nonzero if the
4427 * object was signaled
4428 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004429 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004430 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004431 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004432 */
4433__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4434 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004435
4436/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004437 * @brief Signal a poll signal object.
4438 *
4439 * This routine makes ready a poll signal, which is basically a poll event of
4440 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4441 * made ready to run. A @a result value can be specified.
4442 *
4443 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004444 * k_poll_signal(), stays set until the user sets it back to 0 with
4445 * k_poll_signal_reset(). It thus has to be reset by the user before being
4446 * passed again to k_poll() or k_poll() will consider it being signaled, and
4447 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004448 *
4449 * @param signal A poll signal.
4450 * @param result The value to store in the result field of the signal.
4451 *
4452 * @retval 0 The signal was delivered successfully.
4453 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004454 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004455 */
4456
Andrew Boie3772f772018-05-07 16:52:57 -07004457__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004458
Anas Nashif954d5502018-02-25 08:37:28 -06004459/**
4460 * @internal
4461 */
Andy Ross8606fab2018-03-26 10:54:40 -07004462extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004463
Anas Nashif166f5192018-02-25 08:02:36 -06004464/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004465
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004466/**
4467 * @brief Make the CPU idle.
4468 *
4469 * This function makes the CPU idle until an event wakes it up.
4470 *
4471 * In a regular system, the idle thread should be the only thread responsible
4472 * for making the CPU idle and triggering any type of power management.
4473 * However, in some more constrained systems, such as a single-threaded system,
4474 * the only thread would be responsible for this if needed.
4475 *
4476 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004477 * @req K-MISC-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004478 */
4479extern void k_cpu_idle(void);
4480
4481/**
4482 * @brief Make the CPU idle in an atomic fashion.
4483 *
4484 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4485 * must be done atomically before making the CPU idle.
4486 *
4487 * @param key Interrupt locking key obtained from irq_lock().
4488 *
4489 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004490 * @req K-MISC-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004491 */
4492extern void k_cpu_atomic_idle(unsigned int key);
4493
Anas Nashif954d5502018-02-25 08:37:28 -06004494
4495/**
4496 * @internal
4497 */
Kumar Galacc334c72017-04-21 10:55:34 -05004498extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004499
Andrew Boiecdb94d62017-04-18 15:22:05 -07004500#ifdef _ARCH_EXCEPT
4501/* This archtecture has direct support for triggering a CPU exception */
4502#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4503#else
4504
Andrew Boiecdb94d62017-04-18 15:22:05 -07004505/* NOTE: This is the implementation for arches that do not implement
4506 * _ARCH_EXCEPT() to generate a real CPU exception.
4507 *
4508 * We won't have a real exception frame to determine the PC value when
4509 * the oops occurred, so print file and line number before we jump into
4510 * the fatal error handler.
4511 */
4512#define _k_except_reason(reason) do { \
4513 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4514 _NanoFatalErrorHandler(reason, &_default_esf); \
4515 CODE_UNREACHABLE; \
4516 } while (0)
4517
4518#endif /* _ARCH__EXCEPT */
4519
4520/**
4521 * @brief Fatally terminate a thread
4522 *
4523 * This should be called when a thread has encountered an unrecoverable
4524 * runtime condition and needs to terminate. What this ultimately
4525 * means is determined by the _fatal_error_handler() implementation, which
4526 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4527 *
4528 * If this is called from ISR context, the default system fatal error handler
4529 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004530 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004531 */
4532#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4533
4534/**
4535 * @brief Fatally terminate the system
4536 *
4537 * This should be called when the Zephyr kernel has encountered an
4538 * unrecoverable runtime condition and needs to terminate. What this ultimately
4539 * means is determined by the _fatal_error_handler() implementation, which
4540 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004541 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004542 */
4543#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4544
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004545/*
4546 * private APIs that are utilized by one or more public APIs
4547 */
4548
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004549#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004550/**
4551 * @internal
4552 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004553extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004554#else
Anas Nashif954d5502018-02-25 08:37:28 -06004555/**
4556 * @internal
4557 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004558#define _init_static_threads() do { } while ((0))
4559#endif
4560
Anas Nashif954d5502018-02-25 08:37:28 -06004561/**
4562 * @internal
4563 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004564extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004565/**
4566 * @internal
4567 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004568extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004569
Andrew Boiedc5d9352017-06-02 12:56:47 -07004570/* arch/cpu.h may declare an architecture or platform-specific macro
4571 * for properly declaring stacks, compatible with MMU/MPU constraints if
4572 * enabled
4573 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004574
4575/**
4576 * @brief Obtain an extern reference to a stack
4577 *
4578 * This macro properly brings the symbol of a thread stack declared
4579 * elsewhere into scope.
4580 *
4581 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004582 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004583 */
4584#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4585
Andrew Boiedc5d9352017-06-02 12:56:47 -07004586#ifdef _ARCH_THREAD_STACK_DEFINE
4587#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4588#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4589 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304590#define K_THREAD_STACK_LEN(size) _ARCH_THREAD_STACK_LEN(size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004591#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4592#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004593static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004594{
4595 return _ARCH_THREAD_STACK_BUFFER(sym);
4596}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004597#else
4598/**
4599 * @brief Declare a toplevel thread stack memory region
4600 *
4601 * This declares a region of memory suitable for use as a thread's stack.
4602 *
4603 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4604 * 'noinit' section so that it isn't zeroed at boot
4605 *
Andrew Boie507852a2017-07-25 18:47:07 -07004606 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004607 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004608 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004609 *
4610 * It is legal to precede this definition with the 'static' keyword.
4611 *
4612 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4613 * parameter of k_thread_create(), it may not be the same as the
4614 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4615 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004616 * Some arches may round the size of the usable stack region up to satisfy
4617 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4618 * size.
4619 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004620 * @param sym Thread stack symbol name
4621 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004622 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004623 */
4624#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004625 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004626
4627/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304628 * @brief Calculate size of stacks to be allocated in a stack array
4629 *
4630 * This macro calculates the size to be allocated for the stacks
4631 * inside a stack array. It accepts the indicated "size" as a parameter
4632 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4633 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4634 *
4635 * @param size Size of the stack memory region
4636 * @req K-TSTACK-001
4637 */
4638#define K_THREAD_STACK_LEN(size) (size)
4639
4640/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004641 * @brief Declare a toplevel array of thread stack memory regions
4642 *
4643 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4644 * definition for additional details and constraints.
4645 *
4646 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4647 * 'noinit' section so that it isn't zeroed at boot
4648 *
4649 * @param sym Thread stack symbol name
4650 * @param nmemb Number of stacks to declare
4651 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004652 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004653 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004654#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004655 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304656 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004657
4658/**
4659 * @brief Declare an embedded stack memory region
4660 *
4661 * Used for stacks embedded within other data structures. Use is highly
4662 * discouraged but in some cases necessary. For memory protection scenarios,
4663 * it is very important that any RAM preceding this member not be writable
4664 * by threads else a stack overflow will lead to silent corruption. In other
4665 * words, the containing data structure should live in RAM owned by the kernel.
4666 *
4667 * @param sym Thread stack symbol name
4668 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004669 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004670 */
4671#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004672 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004673
4674/**
4675 * @brief Return the size in bytes of a stack memory region
4676 *
4677 * Convenience macro for passing the desired stack size to k_thread_create()
4678 * since the underlying implementation may actually create something larger
4679 * (for instance a guard area).
4680 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004681 * The value returned here is not guaranteed to match the 'size' parameter
4682 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004683 *
4684 * @param sym Stack memory symbol
4685 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004686 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004687 */
4688#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4689
4690/**
4691 * @brief Get a pointer to the physical stack buffer
4692 *
4693 * Convenience macro to get at the real underlying stack buffer used by
4694 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4695 * This is really only intended for diagnostic tools which want to examine
4696 * stack memory contents.
4697 *
4698 * @param sym Declared stack symbol name
4699 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004700 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004701 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004702static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004703{
4704 return (char *)sym;
4705}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004706
4707#endif /* _ARCH_DECLARE_STACK */
4708
Chunlin Hane9c97022017-07-07 20:29:30 +08004709/**
4710 * @defgroup mem_domain_apis Memory domain APIs
4711 * @ingroup kernel_apis
4712 * @{
4713 */
4714
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004715/**
4716 * @def MEM_PARTITION_ENTRY
4717 * @brief Used to declare a memory partition entry
4718 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004719 */
4720#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4721 {\
4722 .start = _start, \
4723 .size = _size, \
4724 .attr = _attr, \
4725 }
4726
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004727/**
4728 * @def K_MEM_PARTITION_DEFINE
4729 * @brief Used to declare a memory partition
4730 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004731 */
4732#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4733#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4734 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304735 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004736 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4737#else
4738#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304739 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004740 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4741#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4742
Chunlin Hane9c97022017-07-07 20:29:30 +08004743/* memory partition */
4744struct k_mem_partition {
4745 /* start address of memory partition */
4746 u32_t start;
4747 /* size of memory partition */
4748 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304749#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004750 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304751 k_mem_partition_attr_t attr;
4752#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004753};
4754
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304755/* memory domian
4756 * Note: Always declare this structure with __kernel prefix
4757 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004758struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304759#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004760 /* partitions in the domain */
4761 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304762#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004763 /* domain q */
4764 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004765 /* number of partitions in the domain */
4766 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004767};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304768
Chunlin Hane9c97022017-07-07 20:29:30 +08004769
4770/**
4771 * @brief Initialize a memory domain.
4772 *
4773 * Initialize a memory domain with given name and memory partitions.
4774 *
4775 * @param domain The memory domain to be initialized.
4776 * @param num_parts The number of array items of "parts" parameter.
4777 * @param parts An array of pointers to the memory partitions. Can be NULL
4778 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004779 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004780 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004781extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004782 struct k_mem_partition *parts[]);
4783/**
4784 * @brief Destroy a memory domain.
4785 *
4786 * Destroy a memory domain.
4787 *
4788 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004789 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004790 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004791extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4792
4793/**
4794 * @brief Add a memory partition into a memory domain.
4795 *
4796 * Add a memory partition into a memory domain.
4797 *
4798 * @param domain The memory domain to be added a memory partition.
4799 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004800 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004801 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004802extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4803 struct k_mem_partition *part);
4804
4805/**
4806 * @brief Remove a memory partition from a memory domain.
4807 *
4808 * Remove a memory partition from a memory domain.
4809 *
4810 * @param domain The memory domain to be removed a memory partition.
4811 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004812 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004813 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004814extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4815 struct k_mem_partition *part);
4816
4817/**
4818 * @brief Add a thread into a memory domain.
4819 *
4820 * Add a thread into a memory domain.
4821 *
4822 * @param domain The memory domain that the thread is going to be added into.
4823 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004824 *
4825 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004826 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004827extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4828 k_tid_t thread);
4829
4830/**
4831 * @brief Remove a thread from its memory domain.
4832 *
4833 * Remove a thread from its memory domain.
4834 *
4835 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004836 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004837 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004838extern void k_mem_domain_remove_thread(k_tid_t thread);
4839
Anas Nashif166f5192018-02-25 08:02:36 -06004840/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004841
Andrew Boie756f9072017-10-10 16:01:49 -07004842/**
4843 * @brief Emit a character buffer to the console device
4844 *
4845 * @param c String of characters to print
4846 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004847 *
4848 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004849 */
4850__syscall void k_str_out(char *c, size_t n);
4851
Andy Rosse7172672018-01-24 15:48:32 -08004852/**
4853 * @brief Start a numbered CPU on a MP-capable system
4854
4855 * This starts and initializes a specific CPU. The main thread on
4856 * startup is running on CPU zero, other processors are numbered
4857 * sequentially. On return from this function, the CPU is known to
4858 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004859 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004860 * with the provided key will work to enable them.
4861 *
4862 * Normally, in SMP mode this function will be called by the kernel
4863 * initialization and should not be used as a user API. But it is
4864 * defined here for special-purpose apps which want Zephyr running on
4865 * one core and to use others for design-specific processing.
4866 *
4867 * @param cpu_num Integer number of the CPU
4868 * @param stack Stack memory for the CPU
4869 * @param sz Stack buffer size, in bytes
4870 * @param fn Function to begin running on the CPU. First argument is
4871 * an irq_unlock() key.
4872 * @param arg Untyped argument to be passed to "fn"
4873 */
4874extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4875 void (*fn)(int, void *), void *arg);
4876
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004877#ifdef __cplusplus
4878}
4879#endif
4880
Andrew Boiee004dec2016-11-07 09:01:19 -08004881#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4882/*
4883 * Define new and delete operators.
4884 * At this moment, the operators do nothing since objects are supposed
4885 * to be statically allocated.
4886 */
4887inline void operator delete(void *ptr)
4888{
4889 (void)ptr;
4890}
4891
4892inline void operator delete[](void *ptr)
4893{
4894 (void)ptr;
4895}
4896
4897inline void *operator new(size_t size)
4898{
4899 (void)size;
4900 return NULL;
4901}
4902
4903inline void *operator new[](size_t size)
4904{
4905 (void)size;
4906 return NULL;
4907}
4908
4909/* Placement versions of operator new and delete */
4910inline void operator delete(void *ptr1, void *ptr2)
4911{
4912 (void)ptr1;
4913 (void)ptr2;
4914}
4915
4916inline void operator delete[](void *ptr1, void *ptr2)
4917{
4918 (void)ptr1;
4919 (void)ptr2;
4920}
4921
4922inline void *operator new(size_t size, void *ptr)
4923{
4924 (void)size;
4925 return ptr;
4926}
4927
4928inline void *operator new[](size_t size, void *ptr)
4929{
4930 (void)size;
4931 return ptr;
4932}
4933
4934#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4935
Andrew Boiefa94ee72017-09-28 16:54:35 -07004936#include <syscalls/kernel.h>
4937
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004938#endif /* !_ASMLANGUAGE */
4939
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004940#endif /* _kernel__h_ */