blob: e936aa5338b7de50b573626d17e80433a441a555 [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
Piotr Zięcike995c272018-06-11 14:01:11 +02001322#if (MSEC_PER_SEC % sys_clock_ticks_per_sec) == 0
Benjamin Walsh62092182016-12-20 14:39:08 -05001323
1324 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1325#else
1326 /* yields horrible 64-bit math on many architectures: try to avoid */
1327 #define _NON_OPTIMIZED_TICKS_PER_SEC
1328#endif
1329
Kumar Galacc334c72017-04-21 10:55:34 -05001330static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001331{
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001332#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
1333 s64_t ms_ticks_per_sec = (s64_t)ms * sys_clock_ticks_per_sec;
1334 return (s32_t)ceiling_fraction(ms_ticks_per_sec, MSEC_PER_SEC);
1335#else
Kumar Galacc334c72017-04-21 10:55:34 -05001336 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001337#endif
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001338}
Benjamin Walsh62092182016-12-20 14:39:08 -05001339
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001340/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001341#ifdef CONFIG_TICKLESS_KERNEL
1342#define _TICK_ALIGN 0
1343#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001344#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001345#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001346
Kumar Galacc334c72017-04-21 10:55:34 -05001347static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001348{
Benjamin Walsh62092182016-12-20 14:39:08 -05001349#ifdef CONFIG_SYS_CLOCK_EXISTS
1350
1351#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001352 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001353#else
Kumar Galacc334c72017-04-21 10:55:34 -05001354 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001355#endif
1356
1357#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001358 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001359 return 0;
1360#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001361}
1362
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001363struct k_timer {
1364 /*
1365 * _timeout structure must be first here if we want to use
1366 * dynamic timer allocation. timeout.node is used in the double-linked
1367 * list of free timers
1368 */
1369 struct _timeout timeout;
1370
Allan Stephens45bfa372016-10-12 12:39:42 -05001371 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001372 _wait_q_t wait_q;
1373
1374 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001375 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001376
1377 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001378 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001379
1380 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001381 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001382
Allan Stephens45bfa372016-10-12 12:39:42 -05001383 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001384 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001385
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001386 /* user-specific data, also used to support legacy features */
1387 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001388
Anas Nashif2f203c22016-12-18 06:57:45 -05001389 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001390};
1391
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001392#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001393 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001394 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001395 .timeout.wait_q = NULL, \
1396 .timeout.thread = NULL, \
1397 .timeout.func = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001398 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001399 .expiry_fn = expiry, \
1400 .stop_fn = stop, \
1401 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001402 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001403 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001404 }
1405
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001406#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1407
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001408/**
Allan Stephensc98da842016-11-11 15:45:03 -05001409 * INTERNAL_HIDDEN @endcond
1410 */
1411
1412/**
1413 * @defgroup timer_apis Timer APIs
1414 * @ingroup kernel_apis
1415 * @{
1416 */
1417
1418/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001419 * @typedef k_timer_expiry_t
1420 * @brief Timer expiry function type.
1421 *
1422 * A timer's expiry function is executed by the system clock interrupt handler
1423 * each time the timer expires. The expiry function is optional, and is only
1424 * invoked if the timer has been initialized with one.
1425 *
1426 * @param timer Address of timer.
1427 *
1428 * @return N/A
1429 */
1430typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1431
1432/**
1433 * @typedef k_timer_stop_t
1434 * @brief Timer stop function type.
1435 *
1436 * A timer's stop function is executed if the timer is stopped prematurely.
1437 * The function runs in the context of the thread that stops the timer.
1438 * The stop function is optional, and is only invoked if the timer has been
1439 * initialized with one.
1440 *
1441 * @param timer Address of timer.
1442 *
1443 * @return N/A
1444 */
1445typedef void (*k_timer_stop_t)(struct k_timer *timer);
1446
1447/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001448 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001449 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001450 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001451 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001452 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001453 *
1454 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001455 * @param expiry_fn Function to invoke each time the timer expires.
1456 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001457 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001458#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001459 struct k_timer name \
1460 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001461 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001462
Allan Stephens45bfa372016-10-12 12:39:42 -05001463/**
1464 * @brief Initialize a timer.
1465 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001466 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001467 *
1468 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001469 * @param expiry_fn Function to invoke each time the timer expires.
1470 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001471 *
1472 * @return N/A
1473 */
1474extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001475 k_timer_expiry_t expiry_fn,
1476 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001477
Allan Stephens45bfa372016-10-12 12:39:42 -05001478/**
1479 * @brief Start a timer.
1480 *
1481 * This routine starts a timer, and resets its status to zero. The timer
1482 * begins counting down using the specified duration and period values.
1483 *
1484 * Attempting to start a timer that is already running is permitted.
1485 * The timer's status is reset to zero and the timer begins counting down
1486 * using the new duration and period values.
1487 *
1488 * @param timer Address of timer.
1489 * @param duration Initial timer duration (in milliseconds).
1490 * @param period Timer period (in milliseconds).
1491 *
1492 * @return N/A
1493 */
Andrew Boiea354d492017-09-29 16:22:28 -07001494__syscall void k_timer_start(struct k_timer *timer,
1495 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001496
1497/**
1498 * @brief Stop a timer.
1499 *
1500 * This routine stops a running timer prematurely. The timer's stop function,
1501 * if one exists, is invoked by the caller.
1502 *
1503 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001504 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001505 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001506 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1507 * if @a k_timer_stop is to be called from ISRs.
1508 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001509 * @param timer Address of timer.
1510 *
1511 * @return N/A
1512 */
Andrew Boiea354d492017-09-29 16:22:28 -07001513__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001514
1515/**
1516 * @brief Read timer status.
1517 *
1518 * This routine reads the timer's status, which indicates the number of times
1519 * it has expired since its status was last read.
1520 *
1521 * Calling this routine resets the timer's status to zero.
1522 *
1523 * @param timer Address of timer.
1524 *
1525 * @return Timer status.
1526 */
Andrew Boiea354d492017-09-29 16:22:28 -07001527__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001528
1529/**
1530 * @brief Synchronize thread to timer expiration.
1531 *
1532 * This routine blocks the calling thread until the timer's status is non-zero
1533 * (indicating that it has expired at least once since it was last examined)
1534 * or the timer is stopped. If the timer status is already non-zero,
1535 * or the timer is already stopped, the caller continues without waiting.
1536 *
1537 * Calling this routine resets the timer's status to zero.
1538 *
1539 * This routine must not be used by interrupt handlers, since they are not
1540 * allowed to block.
1541 *
1542 * @param timer Address of timer.
1543 *
1544 * @return Timer status.
1545 */
Andrew Boiea354d492017-09-29 16:22:28 -07001546__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001547
1548/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001549 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001550 *
1551 * This routine computes the (approximate) time remaining before a running
1552 * timer next expires. If the timer is not running, it returns zero.
1553 *
1554 * @param timer Address of timer.
1555 *
1556 * @return Remaining time (in milliseconds).
1557 */
Andrew Boiea354d492017-09-29 16:22:28 -07001558__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1559
1560static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001561{
1562 return _timeout_remaining_get(&timer->timeout);
1563}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001564
Allan Stephensc98da842016-11-11 15:45:03 -05001565/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001566 * @brief Associate user-specific data with a timer.
1567 *
1568 * This routine records the @a user_data with the @a timer, to be retrieved
1569 * later.
1570 *
1571 * It can be used e.g. in a timer handler shared across multiple subsystems to
1572 * retrieve data specific to the subsystem this timer is associated with.
1573 *
1574 * @param timer Address of timer.
1575 * @param user_data User data to associate with the timer.
1576 *
1577 * @return N/A
1578 */
Andrew Boiea354d492017-09-29 16:22:28 -07001579__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1580
Anas Nashif954d5502018-02-25 08:37:28 -06001581/**
1582 * @internal
1583 */
Andrew Boiea354d492017-09-29 16:22:28 -07001584static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1585 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001586{
1587 timer->user_data = user_data;
1588}
1589
1590/**
1591 * @brief Retrieve the user-specific data from a timer.
1592 *
1593 * @param timer Address of timer.
1594 *
1595 * @return The user data.
1596 */
Andrew Boiea354d492017-09-29 16:22:28 -07001597__syscall void *k_timer_user_data_get(struct k_timer *timer);
1598
1599static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001600{
1601 return timer->user_data;
1602}
1603
Anas Nashif166f5192018-02-25 08:02:36 -06001604/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001605
Allan Stephensc98da842016-11-11 15:45:03 -05001606/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001607 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001608 * @{
1609 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001610
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001611/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001612 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001613 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001614 * This routine returns the elapsed time since the system booted,
1615 * in milliseconds.
1616 *
1617 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001618 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001619__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001620
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001621/**
1622 * @brief Enable clock always on in tickless kernel
1623 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001624 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001625 * there are no timer events programmed in tickless kernel
1626 * scheduling. This is necessary if the clock is used to track
1627 * passage of time.
1628 *
1629 * @retval prev_status Previous status of always on flag
1630 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301631#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001632static inline int k_enable_sys_clock_always_on(void)
1633{
1634 int prev_status = _sys_clock_always_on;
1635
1636 _sys_clock_always_on = 1;
1637 _enable_sys_clock();
1638
1639 return prev_status;
1640}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301641#else
1642#define k_enable_sys_clock_always_on() do { } while ((0))
1643#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001644
1645/**
1646 * @brief Disable clock always on in tickless kernel
1647 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001648 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001649 * there are no timer events programmed in tickless kernel
1650 * scheduling. To save power, this routine should be called
1651 * immediately when clock is not used to track time.
1652 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301653#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001654static inline void k_disable_sys_clock_always_on(void)
1655{
1656 _sys_clock_always_on = 0;
1657}
1658#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001659#define k_disable_sys_clock_always_on() do { } while ((0))
1660#endif
1661
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001662/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001663 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001664 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001665 * This routine returns the lower 32-bits of the elapsed time since the system
1666 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001667 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001668 * This routine can be more efficient than k_uptime_get(), as it reduces the
1669 * need for interrupt locking and 64-bit math. However, the 32-bit result
1670 * cannot hold a system uptime time larger than approximately 50 days, so the
1671 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001672 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001673 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001674 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001675__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001676
1677/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001678 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001679 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001680 * This routine computes the elapsed time between the current system uptime
1681 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001682 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001683 * @param reftime Pointer to a reference time, which is updated to the current
1684 * uptime upon return.
1685 *
1686 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001687 */
Kumar Galacc334c72017-04-21 10:55:34 -05001688extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001689
1690/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001691 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001692 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001693 * This routine computes the elapsed time between the current system uptime
1694 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001695 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001696 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1697 * need for interrupt locking and 64-bit math. However, the 32-bit result
1698 * cannot hold an elapsed time larger than approximately 50 days, so the
1699 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001701 * @param reftime Pointer to a reference time, which is updated to the current
1702 * uptime upon return.
1703 *
1704 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001705 */
Kumar Galacc334c72017-04-21 10:55:34 -05001706extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001707
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001708/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001709 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001710 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001711 * This routine returns the current time, as measured by the system's hardware
1712 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001713 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001714 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001715 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001716#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001717
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001718/**
Anas Nashif166f5192018-02-25 08:02:36 -06001719 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001720 */
1721
Allan Stephensc98da842016-11-11 15:45:03 -05001722/**
1723 * @cond INTERNAL_HIDDEN
1724 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001725
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001726struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001727 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001728 union {
1729 _wait_q_t wait_q;
1730
1731 _POLL_EVENT;
1732 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001733
1734 _OBJECT_TRACING_NEXT_PTR(k_queue);
1735};
1736
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001737#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001738 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001739 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001740 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001741 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001742 _OBJECT_TRACING_INIT \
1743 }
1744
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001745#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1746
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001747extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1748
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001749/**
1750 * INTERNAL_HIDDEN @endcond
1751 */
1752
1753/**
1754 * @defgroup queue_apis Queue APIs
1755 * @ingroup kernel_apis
1756 * @{
1757 */
1758
1759/**
1760 * @brief Initialize a queue.
1761 *
1762 * This routine initializes a queue object, prior to its first use.
1763 *
1764 * @param queue Address of the queue.
1765 *
1766 * @return N/A
1767 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001768__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001769
1770/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001771 * @brief Cancel waiting on a queue.
1772 *
1773 * This routine causes first thread pending on @a queue, if any, to
1774 * return from k_queue_get() call with NULL value (as if timeout expired).
1775 *
1776 * @note Can be called by ISRs.
1777 *
1778 * @param queue Address of the queue.
1779 *
1780 * @return N/A
1781 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001782__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001783
1784/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001785 * @brief Append an element to the end of a queue.
1786 *
1787 * This routine appends a data item to @a queue. A queue data item must be
1788 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1789 * reserved for the kernel's use.
1790 *
1791 * @note Can be called by ISRs.
1792 *
1793 * @param queue Address of the queue.
1794 * @param data Address of the data item.
1795 *
1796 * @return N/A
1797 */
1798extern void k_queue_append(struct k_queue *queue, void *data);
1799
1800/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001801 * @brief Append an element to a queue.
1802 *
1803 * This routine appends a data item to @a queue. There is an implicit
1804 * memory allocation from the calling thread's resource pool, which is
1805 * automatically freed when the item is removed from the queue.
1806 *
1807 * @note Can be called by ISRs.
1808 *
1809 * @param queue Address of the queue.
1810 * @param data Address of the data item.
1811 *
1812 * @retval 0 on success
1813 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1814 */
1815__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1816
1817/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001818 * @brief Prepend an element to a queue.
1819 *
1820 * This routine prepends a data item to @a queue. A queue data item must be
1821 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1822 * reserved for the kernel's use.
1823 *
1824 * @note Can be called by ISRs.
1825 *
1826 * @param queue Address of the queue.
1827 * @param data Address of the data item.
1828 *
1829 * @return N/A
1830 */
1831extern void k_queue_prepend(struct k_queue *queue, void *data);
1832
1833/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001834 * @brief Prepend an element to a queue.
1835 *
1836 * This routine prepends a data item to @a queue. There is an implicit
1837 * memory allocation from the calling thread's resource pool, which is
1838 * automatically freed when the item is removed from the queue.
1839 *
1840 * @note Can be called by ISRs.
1841 *
1842 * @param queue Address of the queue.
1843 * @param data Address of the data item.
1844 *
1845 * @retval 0 on success
1846 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1847 */
1848__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1849
1850/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001851 * @brief Inserts an element to a queue.
1852 *
1853 * This routine inserts a data item to @a queue after previous item. A queue
1854 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1855 * item are reserved for the kernel's use.
1856 *
1857 * @note Can be called by ISRs.
1858 *
1859 * @param queue Address of the queue.
1860 * @param prev Address of the previous data item.
1861 * @param data Address of the data item.
1862 *
1863 * @return N/A
1864 */
1865extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1866
1867/**
1868 * @brief Atomically append a list of elements to a queue.
1869 *
1870 * This routine adds a list of data items to @a queue in one operation.
1871 * The data items must be in a singly-linked list, with the first 32 bits
1872 * in each data item pointing to the next data item; the list must be
1873 * NULL-terminated.
1874 *
1875 * @note Can be called by ISRs.
1876 *
1877 * @param queue Address of the queue.
1878 * @param head Pointer to first node in singly-linked list.
1879 * @param tail Pointer to last node in singly-linked list.
1880 *
1881 * @return N/A
1882 */
1883extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1884
1885/**
1886 * @brief Atomically add a list of elements to a queue.
1887 *
1888 * This routine adds a list of data items to @a queue in one operation.
1889 * The data items must be in a singly-linked list implemented using a
1890 * sys_slist_t object. Upon completion, the original list is empty.
1891 *
1892 * @note Can be called by ISRs.
1893 *
1894 * @param queue Address of the queue.
1895 * @param list Pointer to sys_slist_t object.
1896 *
1897 * @return N/A
1898 */
1899extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1900
1901/**
1902 * @brief Get an element from a queue.
1903 *
1904 * This routine removes first data item from @a queue. The first 32 bits of the
1905 * data item are reserved for the kernel's use.
1906 *
1907 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1908 *
1909 * @param queue Address of the queue.
1910 * @param timeout Waiting period to obtain a data item (in milliseconds),
1911 * or one of the special values K_NO_WAIT and K_FOREVER.
1912 *
1913 * @return Address of the data item if successful; NULL if returned
1914 * without waiting, or waiting period timed out.
1915 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001916__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001917
1918/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001919 * @brief Remove an element from a queue.
1920 *
1921 * This routine removes data item from @a queue. The first 32 bits of the
1922 * data item are reserved for the kernel's use. Removing elements from k_queue
1923 * rely on sys_slist_find_and_remove which is not a constant time operation.
1924 *
1925 * @note Can be called by ISRs
1926 *
1927 * @param queue Address of the queue.
1928 * @param data Address of the data item.
1929 *
1930 * @return true if data item was removed
1931 */
1932static inline bool k_queue_remove(struct k_queue *queue, void *data)
1933{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001934 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001935}
1936
1937/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001938 * @brief Query a queue to see if it has data available.
1939 *
1940 * Note that the data might be already gone by the time this function returns
1941 * if other threads are also trying to read from the queue.
1942 *
1943 * @note Can be called by ISRs.
1944 *
1945 * @param queue Address of the queue.
1946 *
1947 * @return Non-zero if the queue is empty.
1948 * @return 0 if data is available.
1949 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001950__syscall int k_queue_is_empty(struct k_queue *queue);
1951
1952static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001953{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001954 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001955}
1956
1957/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001958 * @brief Peek element at the head of queue.
1959 *
1960 * Return element from the head of queue without removing it.
1961 *
1962 * @param queue Address of the queue.
1963 *
1964 * @return Head element, or NULL if queue is empty.
1965 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001966__syscall void *k_queue_peek_head(struct k_queue *queue);
1967
1968static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001969{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001970 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001971}
1972
1973/**
1974 * @brief Peek element at the tail of queue.
1975 *
1976 * Return element from the tail of queue without removing it.
1977 *
1978 * @param queue Address of the queue.
1979 *
1980 * @return Tail element, or NULL if queue is empty.
1981 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001982__syscall void *k_queue_peek_tail(struct k_queue *queue);
1983
1984static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001985{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001986 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001987}
1988
1989/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001990 * @brief Statically define and initialize a queue.
1991 *
1992 * The queue can be accessed outside the module where it is defined using:
1993 *
1994 * @code extern struct k_queue <name>; @endcode
1995 *
1996 * @param name Name of the queue.
1997 */
1998#define K_QUEUE_DEFINE(name) \
1999 struct k_queue name \
2000 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002001 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002002
Anas Nashif166f5192018-02-25 08:02:36 -06002003/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002004
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002005struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002006 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002007};
2008
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002009/**
2010 * @cond INTERNAL_HIDDEN
2011 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002012#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002013 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002014 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002015 }
2016
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002017#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
2018
Allan Stephensc98da842016-11-11 15:45:03 -05002019/**
2020 * INTERNAL_HIDDEN @endcond
2021 */
2022
2023/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002024 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002025 * @ingroup kernel_apis
2026 * @{
2027 */
2028
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002029/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002030 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002031 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002032 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002033 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002034 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002035 *
2036 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002037 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002038 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002039#define k_fifo_init(fifo) \
2040 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002041
2042/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002043 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002044 *
2045 * This routine causes first thread pending on @a fifo, if any, to
2046 * return from k_fifo_get() call with NULL value (as if timeout
2047 * expired).
2048 *
2049 * @note Can be called by ISRs.
2050 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002051 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002052 *
2053 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002054 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002055 */
2056#define k_fifo_cancel_wait(fifo) \
2057 k_queue_cancel_wait((struct k_queue *) fifo)
2058
2059/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002060 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002061 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002062 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002063 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2064 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002065 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002066 * @note Can be called by ISRs.
2067 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002068 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002069 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002070 *
2071 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002072 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002073 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002074#define k_fifo_put(fifo, data) \
2075 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002076
2077/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002078 * @brief Add an element to a FIFO queue.
2079 *
2080 * This routine adds a data item to @a fifo. There is an implicit
2081 * memory allocation from the calling thread's resource pool, which is
2082 * automatically freed when the item is removed.
2083 *
2084 * @note Can be called by ISRs.
2085 *
2086 * @param fifo Address of the FIFO.
2087 * @param data Address of the data item.
2088 *
2089 * @retval 0 on success
2090 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002091 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002092 */
2093#define k_fifo_alloc_put(fifo, data) \
2094 k_queue_alloc_append((struct k_queue *) fifo, data)
2095
2096/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002097 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002098 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002099 * This routine adds a list of data items to @a fifo in one operation.
2100 * The data items must be in a singly-linked list, with the first 32 bits
2101 * each data item pointing to the next data item; the list must be
2102 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002104 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002105 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002106 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002107 * @param head Pointer to first node in singly-linked list.
2108 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002109 *
2110 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002111 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002112 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002113#define k_fifo_put_list(fifo, head, tail) \
2114 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002115
2116/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002117 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002119 * This routine adds a list of data items to @a fifo in one operation.
2120 * The data items must be in a singly-linked list implemented using a
2121 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002122 * and must be re-initialized via sys_slist_init().
2123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002124 * @note Can be called by ISRs.
2125 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002126 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002127 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002128 *
2129 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002130 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002131 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002132#define k_fifo_put_slist(fifo, list) \
2133 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002134
2135/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002136 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002137 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002138 * This routine removes a data item from @a fifo in a "first in, first out"
2139 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002140 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002141 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2142 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002143 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002144 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002145 * or one of the special values K_NO_WAIT and K_FOREVER.
2146 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002147 * @return Address of the data item if successful; NULL if returned
2148 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002149 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002150 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002151#define k_fifo_get(fifo, timeout) \
2152 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002153
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002154/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002155 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002156 *
2157 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002158 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002159 *
2160 * @note Can be called by ISRs.
2161 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002162 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002163 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002164 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002165 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002166 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002167 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002168#define k_fifo_is_empty(fifo) \
2169 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002170
2171/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002172 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002173 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002174 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302175 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002176 * on each iteration of processing, a head container will be peeked,
2177 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002178 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002179 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002180 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002181 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002182 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002183 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002184 */
2185#define k_fifo_peek_head(fifo) \
2186 k_queue_peek_head((struct k_queue *) fifo)
2187
2188/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002189 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002190 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002191 * Return element from the tail of FIFO queue (without removing it). A usecase
2192 * for this is if elements of the FIFO queue are themselves containers. Then
2193 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002194 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002195 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002196 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002197 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002198 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002199 */
2200#define k_fifo_peek_tail(fifo) \
2201 k_queue_peek_tail((struct k_queue *) fifo)
2202
2203/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002204 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002205 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002206 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002207 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002208 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002209 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002210 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002211 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002212 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002213#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002214 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002215 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002216 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002217
Anas Nashif166f5192018-02-25 08:02:36 -06002218/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002219
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002220struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002221 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002222};
2223
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002224/**
2225 * @cond INTERNAL_HIDDEN
2226 */
2227
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002228#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002229 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002230 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002231 }
2232
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002233#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2234
Allan Stephensc98da842016-11-11 15:45:03 -05002235/**
2236 * INTERNAL_HIDDEN @endcond
2237 */
2238
2239/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002240 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002241 * @ingroup kernel_apis
2242 * @{
2243 */
2244
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002245/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002246 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002247 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002248 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002249 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002250 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002251 *
2252 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002253 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002254 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002255#define k_lifo_init(lifo) \
2256 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002257
2258/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002259 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002260 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002261 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002262 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2263 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002264 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002265 * @note Can be called by ISRs.
2266 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002267 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002268 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002269 *
2270 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002271 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002272 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002273#define k_lifo_put(lifo, data) \
2274 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002275
2276/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002277 * @brief Add an element to a LIFO queue.
2278 *
2279 * This routine adds a data item to @a lifo. There is an implicit
2280 * memory allocation from the calling thread's resource pool, which is
2281 * automatically freed when the item is removed.
2282 *
2283 * @note Can be called by ISRs.
2284 *
2285 * @param lifo Address of the LIFO.
2286 * @param data Address of the data item.
2287 *
2288 * @retval 0 on success
2289 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002290 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002291 */
2292#define k_lifo_alloc_put(lifo, data) \
2293 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2294
2295/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002296 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002297 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002298 * This routine removes a data item from @a lifo in a "last in, first out"
2299 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002300 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002301 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2302 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002303 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002304 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002305 * or one of the special values K_NO_WAIT and K_FOREVER.
2306 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002307 * @return Address of the data item if successful; NULL if returned
2308 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002309 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002310 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002311#define k_lifo_get(lifo, timeout) \
2312 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002313
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002314/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002315 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002316 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002317 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002318 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002319 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002320 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002321 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002322 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002323 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002324#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002325 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002326 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002327 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002328
Anas Nashif166f5192018-02-25 08:02:36 -06002329/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002330
2331/**
2332 * @cond INTERNAL_HIDDEN
2333 */
Andrew Boief3bee952018-05-02 17:44:39 -07002334#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002335
2336struct k_stack {
2337 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002338 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002339
Anas Nashif2f203c22016-12-18 06:57:45 -05002340 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002341 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002342};
2343
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002344#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002345 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002346 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002347 .base = stack_buffer, \
2348 .next = stack_buffer, \
2349 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002350 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002351 }
2352
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002353#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2354
Allan Stephensc98da842016-11-11 15:45:03 -05002355/**
2356 * INTERNAL_HIDDEN @endcond
2357 */
2358
2359/**
2360 * @defgroup stack_apis Stack APIs
2361 * @ingroup kernel_apis
2362 * @{
2363 */
2364
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002365/**
2366 * @brief Initialize a stack.
2367 *
2368 * This routine initializes a stack object, prior to its first use.
2369 *
2370 * @param stack Address of the stack.
2371 * @param buffer Address of array used to hold stacked values.
2372 * @param num_entries Maximum number of values that can be stacked.
2373 *
2374 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002375 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002376 */
Andrew Boief3bee952018-05-02 17:44:39 -07002377void k_stack_init(struct k_stack *stack,
2378 u32_t *buffer, unsigned int num_entries);
2379
2380
2381/**
2382 * @brief Initialize a stack.
2383 *
2384 * This routine initializes a stack object, prior to its first use. Internal
2385 * buffers will be allocated from the calling thread's resource pool.
2386 * This memory will be released if k_stack_cleanup() is called, or
2387 * userspace is enabled and the stack object loses all references to it.
2388 *
2389 * @param stack Address of the stack.
2390 * @param num_entries Maximum number of values that can be stacked.
2391 *
2392 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002393 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002394 */
2395
2396__syscall int k_stack_alloc_init(struct k_stack *stack,
2397 unsigned int num_entries);
2398
2399/**
2400 * @brief Release a stack's allocated buffer
2401 *
2402 * If a stack object was given a dynamically allocated buffer via
2403 * k_stack_alloc_init(), this will free it. This function does nothing
2404 * if the buffer wasn't dynamically allocated.
2405 *
2406 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002407 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002408 */
2409void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002410
2411/**
2412 * @brief Push an element onto a stack.
2413 *
2414 * This routine adds a 32-bit value @a data to @a stack.
2415 *
2416 * @note Can be called by ISRs.
2417 *
2418 * @param stack Address of the stack.
2419 * @param data Value to push onto the stack.
2420 *
2421 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002422 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002423 */
Andrew Boiee8734462017-09-29 16:42:07 -07002424__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002425
2426/**
2427 * @brief Pop an element from a stack.
2428 *
2429 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2430 * manner and stores the value in @a data.
2431 *
2432 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2433 *
2434 * @param stack Address of the stack.
2435 * @param data Address of area to hold the value popped from the stack.
2436 * @param timeout Waiting period to obtain a value (in milliseconds),
2437 * or one of the special values K_NO_WAIT and K_FOREVER.
2438 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002439 * @retval 0 Element popped from stack.
2440 * @retval -EBUSY Returned without waiting.
2441 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002442 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002443 */
Andrew Boiee8734462017-09-29 16:42:07 -07002444__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002445
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002446/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002447 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002448 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002449 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002450 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002451 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002452 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002453 * @param name Name of the stack.
2454 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002455 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002456 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002457#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002458 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002459 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002460 struct k_stack name \
2461 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002462 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002463 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002464
Anas Nashif166f5192018-02-25 08:02:36 -06002465/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002466
Allan Stephens6bba9b02016-11-16 14:56:54 -05002467struct k_work;
2468
Allan Stephensc98da842016-11-11 15:45:03 -05002469/**
2470 * @defgroup workqueue_apis Workqueue Thread APIs
2471 * @ingroup kernel_apis
2472 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002473 */
2474
Allan Stephens6bba9b02016-11-16 14:56:54 -05002475/**
2476 * @typedef k_work_handler_t
2477 * @brief Work item handler function type.
2478 *
2479 * A work item's handler function is executed by a workqueue's thread
2480 * when the work item is processed by the workqueue.
2481 *
2482 * @param work Address of the work item.
2483 *
2484 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002485 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002486 */
2487typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002488
2489/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002490 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002491 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002492
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002493struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002494 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002495 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002496};
2497
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002498enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002499 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002500};
2501
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002502struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002503 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002504 k_work_handler_t handler;
2505 atomic_t flags[1];
2506};
2507
Allan Stephens6bba9b02016-11-16 14:56:54 -05002508struct k_delayed_work {
2509 struct k_work work;
2510 struct _timeout timeout;
2511 struct k_work_q *work_q;
2512};
2513
2514extern struct k_work_q k_sys_work_q;
2515
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002516/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002517 * INTERNAL_HIDDEN @endcond
2518 */
2519
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002520#define _K_WORK_INITIALIZER(work_handler) \
2521 { \
2522 ._reserved = NULL, \
2523 .handler = work_handler, \
2524 .flags = { 0 } \
2525 }
2526
2527#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2528
Allan Stephens6bba9b02016-11-16 14:56:54 -05002529/**
2530 * @brief Initialize a statically-defined work item.
2531 *
2532 * This macro can be used to initialize a statically-defined workqueue work
2533 * item, prior to its first use. For example,
2534 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002535 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002536 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002537 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002538 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002539 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002540 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002541#define K_WORK_DEFINE(work, work_handler) \
2542 struct k_work work \
2543 __in_section(_k_work, static, work) = \
2544 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002545
2546/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002547 * @brief Initialize a work item.
2548 *
2549 * This routine initializes a workqueue work item, prior to its first use.
2550 *
2551 * @param work Address of work item.
2552 * @param handler Function to invoke each time work item is processed.
2553 *
2554 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002555 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556 */
2557static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2558{
Leandro Pereira0e23ad82018-05-29 10:42:17 -07002559 *work = (struct k_work)_K_WORK_INITIALIZER(handler);
Andrew Boie945af952017-08-22 13:15:23 -07002560 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002561}
2562
2563/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002564 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002565 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002566 * This routine submits work item @a work to be processed by workqueue
2567 * @a work_q. If the work item is already pending in the workqueue's queue
2568 * as a result of an earlier submission, this routine has no effect on the
2569 * work item. If the work item has already been processed, or is currently
2570 * being processed, its work is considered complete and the work item can be
2571 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002572 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002573 * @warning
2574 * A submitted work item must not be modified until it has been processed
2575 * by the workqueue.
2576 *
2577 * @note Can be called by ISRs.
2578 *
2579 * @param work_q Address of workqueue.
2580 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002581 *
2582 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002583 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002584 */
2585static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2586 struct k_work *work)
2587{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002588 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002589 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002590 }
2591}
2592
2593/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002594 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002595 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002596 * This routine indicates if work item @a work is pending in a workqueue's
2597 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002598 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002599 * @note Can be called by ISRs.
2600 *
2601 * @param work Address of work item.
2602 *
2603 * @return 1 if work item is pending, or 0 if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002604 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002605 */
2606static inline int k_work_pending(struct k_work *work)
2607{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002608 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002609}
2610
2611/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002612 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002613 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002614 * This routine starts workqueue @a work_q. The workqueue spawns its work
2615 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002616 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002617 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002618 * @param stack Pointer to work queue thread's stack space, as defined by
2619 * K_THREAD_STACK_DEFINE()
2620 * @param stack_size Size of the work queue thread's stack (in bytes), which
2621 * should either be the same constant passed to
2622 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002623 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002624 *
2625 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002626 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002627 */
Andrew Boie507852a2017-07-25 18:47:07 -07002628extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002629 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002630 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002631
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002632/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002633 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002634 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002635 * This routine initializes a workqueue delayed work item, prior to
2636 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002637 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002638 * @param work Address of delayed work item.
2639 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002640 *
2641 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002642 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002643 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002644extern void k_delayed_work_init(struct k_delayed_work *work,
2645 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002646
2647/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002648 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002649 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002650 * This routine schedules work item @a work to be processed by workqueue
2651 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002652 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002653 * Only when the countdown completes is the work item actually submitted to
2654 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002655 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002656 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002657 * counting down cancels the existing submission and restarts the
2658 * countdown using the new delay. Note that this behavior is
2659 * inherently subject to race conditions with the pre-existing
2660 * timeouts and work queue, so care must be taken to synchronize such
2661 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002662 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002663 * @warning
2664 * A delayed work item must not be modified until it has been processed
2665 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002666 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002667 * @note Can be called by ISRs.
2668 *
2669 * @param work_q Address of workqueue.
2670 * @param work Address of delayed work item.
2671 * @param delay Delay before submitting the work item (in milliseconds).
2672 *
2673 * @retval 0 Work item countdown started.
2674 * @retval -EINPROGRESS Work item is already pending.
2675 * @retval -EINVAL Work item is being processed or has completed its work.
2676 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002677 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002678 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002679extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2680 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002681 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002682
2683/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002684 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002685 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002686 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002687 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002688 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002689 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002690 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002691 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002692 * @param work Address of delayed work item.
2693 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002694 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002695 * @retval -EINPROGRESS Work item is already pending.
2696 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002697 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002698 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002699extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002700
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002701/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002702 * @brief Submit a work item to the system workqueue.
2703 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002704 * This routine submits work item @a work to be processed by the system
2705 * workqueue. If the work item is already pending in the workqueue's queue
2706 * as a result of an earlier submission, this routine has no effect on the
2707 * work item. If the work item has already been processed, or is currently
2708 * being processed, its work is considered complete and the work item can be
2709 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002710 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002711 * @warning
2712 * Work items submitted to the system workqueue should avoid using handlers
2713 * that block or yield since this may prevent the system workqueue from
2714 * processing other work items in a timely manner.
2715 *
2716 * @note Can be called by ISRs.
2717 *
2718 * @param work Address of work item.
2719 *
2720 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002721 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002722 */
2723static inline void k_work_submit(struct k_work *work)
2724{
2725 k_work_submit_to_queue(&k_sys_work_q, work);
2726}
2727
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002728/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002729 * @brief Submit a delayed work item to the system workqueue.
2730 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002731 * This routine schedules work item @a work to be processed by the system
2732 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002733 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002734 * Only when the countdown completes is the work item actually submitted to
2735 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002736 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002737 * Submitting a previously submitted delayed work item that is still
2738 * counting down cancels the existing submission and restarts the countdown
2739 * using the new delay. If the work item is currently pending on the
2740 * workqueue's queue because the countdown has completed it is too late to
2741 * resubmit the item, and resubmission fails without impacting the work item.
2742 * If the work item has already been processed, or is currently being processed,
2743 * its work is considered complete and the work item can be resubmitted.
2744 *
2745 * @warning
2746 * Work items submitted to the system workqueue should avoid using handlers
2747 * that block or yield since this may prevent the system workqueue from
2748 * processing other work items in a timely manner.
2749 *
2750 * @note Can be called by ISRs.
2751 *
2752 * @param work Address of delayed work item.
2753 * @param delay Delay before submitting the work item (in milliseconds).
2754 *
2755 * @retval 0 Work item countdown started.
2756 * @retval -EINPROGRESS Work item is already pending.
2757 * @retval -EINVAL Work item is being processed or has completed its work.
2758 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002759 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002760 */
2761static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002762 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002763{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002764 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002765}
2766
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002767/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002768 * @brief Get time remaining before a delayed work gets scheduled.
2769 *
2770 * This routine computes the (approximate) time remaining before a
2771 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002772 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002773 *
2774 * @param work Delayed work item.
2775 *
2776 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002777 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002778 */
Kumar Galacc334c72017-04-21 10:55:34 -05002779static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002780{
2781 return _timeout_remaining_get(&work->timeout);
2782}
2783
Anas Nashif166f5192018-02-25 08:02:36 -06002784/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002785/**
Anas Nashifce78d162018-05-24 12:43:11 -05002786 * @defgroup mutex_apis Mutex APIs
2787 * @ingroup kernel_apis
2788 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002789 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002790
Anas Nashifce78d162018-05-24 12:43:11 -05002791/**
2792 * Mutex Structure
2793 * @ingroup mutex_apis
2794 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002795struct k_mutex {
2796 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002797 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002798 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002799 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002800 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002801
Anas Nashif2f203c22016-12-18 06:57:45 -05002802 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002803};
2804
Anas Nashifce78d162018-05-24 12:43:11 -05002805/**
2806 * @cond INTERNAL_HIDDEN
2807 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002808#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002809 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002810 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002811 .owner = NULL, \
2812 .lock_count = 0, \
2813 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002814 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002815 }
2816
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002817#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2818
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002819/**
Allan Stephensc98da842016-11-11 15:45:03 -05002820 * INTERNAL_HIDDEN @endcond
2821 */
2822
2823/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002824 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002825 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002826 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002827 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002828 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002829 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002830 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002831 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002832 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002833#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002834 struct k_mutex name \
2835 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002836 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002837
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002838/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002839 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002841 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002842 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002843 * Upon completion, the mutex is available and does not have an owner.
2844 *
2845 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002846 *
2847 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002848 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002849 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002850__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002851
2852/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002853 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002855 * This routine locks @a mutex. If the mutex is locked by another thread,
2856 * the calling thread waits until the mutex becomes available or until
2857 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002858 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002859 * A thread is permitted to lock a mutex it has already locked. The operation
2860 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002862 * @param mutex Address of the mutex.
2863 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002864 * or one of the special values K_NO_WAIT and K_FOREVER.
2865 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002866 * @retval 0 Mutex locked.
2867 * @retval -EBUSY Returned without waiting.
2868 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002869 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002870 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002871__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002872
2873/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002874 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002876 * This routine unlocks @a mutex. The mutex must already be locked by the
2877 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002878 *
2879 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002880 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002881 * thread.
2882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002883 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002884 *
2885 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002886 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002887 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002888__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002889
Allan Stephensc98da842016-11-11 15:45:03 -05002890/**
Anas Nashif166f5192018-02-25 08:02:36 -06002891 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002892 */
2893
2894/**
2895 * @cond INTERNAL_HIDDEN
2896 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002897
2898struct k_sem {
2899 _wait_q_t wait_q;
2900 unsigned int count;
2901 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002902 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002903
Anas Nashif2f203c22016-12-18 06:57:45 -05002904 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002905};
2906
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002907#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002908 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002909 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002910 .count = initial_count, \
2911 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002912 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002913 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002914 }
2915
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002916#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2917
Allan Stephensc98da842016-11-11 15:45:03 -05002918/**
2919 * INTERNAL_HIDDEN @endcond
2920 */
2921
2922/**
2923 * @defgroup semaphore_apis Semaphore APIs
2924 * @ingroup kernel_apis
2925 * @{
2926 */
2927
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002928/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002929 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002930 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002931 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002933 * @param sem Address of the semaphore.
2934 * @param initial_count Initial semaphore count.
2935 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002936 *
2937 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002938 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002939 */
Andrew Boie99280232017-09-29 14:17:47 -07002940__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2941 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002942
2943/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002944 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002946 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002947 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002948 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2949 *
2950 * @param sem Address of the semaphore.
2951 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002952 * or one of the special values K_NO_WAIT and K_FOREVER.
2953 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002954 * @note When porting code from the nanokernel legacy API to the new API, be
2955 * careful with the return value of this function. The return value is the
2956 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2957 * non-zero means failure, while the nano_sem_take family returns 1 for success
2958 * and 0 for failure.
2959 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002960 * @retval 0 Semaphore taken.
2961 * @retval -EBUSY Returned without waiting.
2962 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002963 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002964 */
Andrew Boie99280232017-09-29 14:17:47 -07002965__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002966
2967/**
2968 * @brief Give a semaphore.
2969 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002970 * This routine gives @a sem, unless the semaphore is already at its maximum
2971 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002973 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002975 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002976 *
2977 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002978 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002979 */
Andrew Boie99280232017-09-29 14:17:47 -07002980__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002981
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002982/**
2983 * @brief Reset a semaphore's count to zero.
2984 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002985 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002987 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002988 *
2989 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002990 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002991 */
Andrew Boie990bf162017-10-03 12:36:49 -07002992__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002993
Anas Nashif954d5502018-02-25 08:37:28 -06002994/**
2995 * @internal
2996 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002997static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002998{
2999 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003000}
3001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003002/**
3003 * @brief Get a semaphore's count.
3004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003005 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003007 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003008 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003009 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003010 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003011 */
Andrew Boie990bf162017-10-03 12:36:49 -07003012__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003013
Anas Nashif954d5502018-02-25 08:37:28 -06003014/**
3015 * @internal
3016 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003017static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003018{
3019 return sem->count;
3020}
3021
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003022/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003023 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003025 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003026 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003027 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003028 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003029 * @param name Name of the semaphore.
3030 * @param initial_count Initial semaphore count.
3031 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003032 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003033 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003034#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003035 struct k_sem name \
3036 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003037 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303038 BUILD_ASSERT(((count_limit) != 0) && \
3039 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003040
Anas Nashif166f5192018-02-25 08:02:36 -06003041/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003042
3043/**
3044 * @defgroup alert_apis Alert APIs
3045 * @ingroup kernel_apis
3046 * @{
3047 */
3048
Allan Stephens5eceb852016-11-16 10:16:30 -05003049/**
3050 * @typedef k_alert_handler_t
3051 * @brief Alert handler function type.
3052 *
3053 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07003054 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05003055 * and is only invoked if the alert has been initialized with one.
3056 *
3057 * @param alert Address of the alert.
3058 *
3059 * @return 0 if alert has been consumed; non-zero if alert should pend.
3060 */
3061typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05003062
Anas Nashif166f5192018-02-25 08:02:36 -06003063/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003064
3065/**
3066 * @cond INTERNAL_HIDDEN
3067 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003068
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003069#define K_ALERT_DEFAULT NULL
3070#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003071
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003072struct k_alert {
3073 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003074 atomic_t send_count;
3075 struct k_work work_item;
3076 struct k_sem sem;
3077
Anas Nashif2f203c22016-12-18 06:57:45 -05003078 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003079};
3080
Anas Nashif954d5502018-02-25 08:37:28 -06003081/**
3082 * @internal
3083 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003084extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003085
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003086#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003087 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003088 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003089 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003090 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
3091 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003092 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003093 }
3094
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003095#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
3096
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003097/**
Allan Stephensc98da842016-11-11 15:45:03 -05003098 * INTERNAL_HIDDEN @endcond
3099 */
3100
3101/**
3102 * @addtogroup alert_apis
3103 * @{
3104 */
3105
3106/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003107 * @def K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts)
3108 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003109 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003110 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003111 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003112 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003113 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003114 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003115 * @param name Name of the alert.
3116 * @param alert_handler Action to take when alert is sent. Specify either
3117 * the address of a function to be invoked by the system workqueue
3118 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
3119 * K_ALERT_DEFAULT (which causes the alert to pend).
3120 * @param max_num_pending_alerts Maximum number of pending alerts.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003121 *
3122 * @req K-ALERT-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003123 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003124#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003125 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003126 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003127 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003128 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003129
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003130/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003131 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003133 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003135 * @param alert Address of the alert.
3136 * @param handler Action to take when alert is sent. Specify either the address
3137 * of a function to be invoked by the system workqueue thread,
3138 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3139 * K_ALERT_DEFAULT (which causes the alert to pend).
3140 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003141 *
3142 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003143 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003144 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003145extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3146 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003147
3148/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003149 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003150 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003151 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003153 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3154 *
3155 * @param alert Address of the alert.
3156 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003157 * or one of the special values K_NO_WAIT and K_FOREVER.
3158 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003159 * @retval 0 Alert received.
3160 * @retval -EBUSY Returned without waiting.
3161 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003162 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003163 */
Andrew Boie310e9872017-09-29 04:41:15 -07003164__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003165
3166/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003167 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003168 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003169 * This routine signals @a alert. The action specified for @a alert will
3170 * be taken, which may trigger the execution of an alert handler function
3171 * and/or cause the alert to pend (assuming the alert has not reached its
3172 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003174 * @note Can be called by ISRs.
3175 *
3176 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003177 *
3178 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003179 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003180 */
Andrew Boie310e9872017-09-29 04:41:15 -07003181__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003182
3183/**
Anas Nashif166f5192018-02-25 08:02:36 -06003184 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003185 */
3186
Allan Stephensc98da842016-11-11 15:45:03 -05003187/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003188 * @defgroup msgq_apis Message Queue APIs
3189 * @ingroup kernel_apis
3190 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003191 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003192
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003193/**
3194 * @brief Message Queue Structure
3195 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003196struct k_msgq {
3197 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003198 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003199 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003200 char *buffer_start;
3201 char *buffer_end;
3202 char *read_ptr;
3203 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003204 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003205
Anas Nashif2f203c22016-12-18 06:57:45 -05003206 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003207 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003208};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003209/**
3210 * @cond INTERNAL_HIDDEN
3211 */
3212
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003213
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003214#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003215 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003216 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003217 .max_msgs = q_max_msgs, \
3218 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003219 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003220 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003221 .read_ptr = q_buffer, \
3222 .write_ptr = q_buffer, \
3223 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003224 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003225 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003226#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003227/**
3228 * INTERNAL_HIDDEN @endcond
3229 */
3230
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003231
Andrew Boie0fe789f2018-04-12 18:35:56 -07003232#define K_MSGQ_FLAG_ALLOC BIT(0)
3233
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003234/**
3235 * @brief Message Queue Attributes
3236 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303237struct k_msgq_attrs {
3238 size_t msg_size;
3239 u32_t max_msgs;
3240 u32_t used_msgs;
3241};
3242
Allan Stephensc98da842016-11-11 15:45:03 -05003243
3244/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003245 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003246 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003247 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3248 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003249 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3250 * message is similarly aligned to this boundary, @a q_msg_size must also be
3251 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003252 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003253 * The message queue can be accessed outside the module where it is defined
3254 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003255 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003256 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003257 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003258 * @param q_name Name of the message queue.
3259 * @param q_msg_size Message size (in bytes).
3260 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003261 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003262 *
3263 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003264 */
3265#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003266 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003267 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003268 struct k_msgq q_name \
3269 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003270 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003271 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003272
Peter Mitsisd7a37502016-10-13 11:37:40 -04003273/**
3274 * @brief Initialize a message queue.
3275 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003276 * This routine initializes a message queue object, prior to its first use.
3277 *
Allan Stephensda827222016-11-09 14:23:58 -06003278 * The message queue's ring buffer must contain space for @a max_msgs messages,
3279 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3280 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3281 * that each message is similarly aligned to this boundary, @a q_msg_size
3282 * must also be a multiple of N.
3283 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003284 * @param q Address of the message queue.
3285 * @param buffer Pointer to ring buffer that holds queued messages.
3286 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003287 * @param max_msgs Maximum number of messages that can be queued.
3288 *
3289 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003290 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003291 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003292void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3293 u32_t max_msgs);
3294
3295/**
3296 * @brief Initialize a message queue.
3297 *
3298 * This routine initializes a message queue object, prior to its first use,
3299 * allocating its internal ring buffer from the calling thread's resource
3300 * pool.
3301 *
3302 * Memory allocated for the ring buffer can be released by calling
3303 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3304 * all of its references.
3305 *
3306 * @param q Address of the message queue.
3307 * @param msg_size Message size (in bytes).
3308 * @param max_msgs Maximum number of messages that can be queued.
3309 *
3310 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3311 * thread's resource pool, or -EINVAL if the size parameters cause
3312 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003313 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003314 */
3315__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3316 u32_t max_msgs);
3317
3318
3319void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003320
3321/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003322 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003323 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003324 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003325 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003326 * @note Can be called by ISRs.
3327 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003328 * @param q Address of the message queue.
3329 * @param data Pointer to the message.
3330 * @param timeout Waiting period to add the message (in milliseconds),
3331 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003332 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003333 * @retval 0 Message sent.
3334 * @retval -ENOMSG Returned without waiting or queue purged.
3335 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003336 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003337 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003338__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003339
3340/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003341 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003343 * This routine receives a message from message queue @a q in a "first in,
3344 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003345 *
Allan Stephensc98da842016-11-11 15:45:03 -05003346 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003347 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003348 * @param q Address of the message queue.
3349 * @param data Address of area to hold the received message.
3350 * @param timeout Waiting period to receive the message (in milliseconds),
3351 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003352 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003353 * @retval 0 Message received.
3354 * @retval -ENOMSG Returned without waiting.
3355 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003356 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003357 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003358__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003359
3360/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003361 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003362 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003363 * This routine discards all unreceived messages in a message queue's ring
3364 * buffer. Any threads that are blocked waiting to send a message to the
3365 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003366 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003367 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003368 *
3369 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003370 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003371 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003372__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003373
Peter Mitsis67be2492016-10-07 11:44:34 -04003374/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003375 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003376 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003377 * This routine returns the number of unused entries in a message queue's
3378 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003379 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003380 * @param q Address of the message queue.
3381 *
3382 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003383 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003384 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003385__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3386
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303387/**
3388 * @brief Get basic attributes of a message queue.
3389 *
3390 * This routine fetches basic attributes of message queue into attr argument.
3391 *
3392 * @param q Address of the message queue.
3393 * @param attrs pointer to message queue attribute structure.
3394 *
3395 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003396 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303397 */
3398__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3399
3400
Andrew Boie82edb6e2017-10-02 10:53:06 -07003401static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003402{
3403 return q->max_msgs - q->used_msgs;
3404}
3405
Peter Mitsisd7a37502016-10-13 11:37:40 -04003406/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003407 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003409 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003410 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003411 * @param q Address of the message queue.
3412 *
3413 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003414 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003415 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003416__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3417
3418static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003419{
3420 return q->used_msgs;
3421}
3422
Anas Nashif166f5192018-02-25 08:02:36 -06003423/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003424
3425/**
3426 * @defgroup mem_pool_apis Memory Pool APIs
3427 * @ingroup kernel_apis
3428 * @{
3429 */
3430
Andy Ross73cb9582017-05-09 10:42:39 -07003431/* Note on sizing: the use of a 20 bit field for block means that,
3432 * assuming a reasonable minimum block size of 16 bytes, we're limited
3433 * to 16M of memory managed by a single pool. Long term it would be
3434 * good to move to a variable bit size based on configuration.
3435 */
3436struct k_mem_block_id {
3437 u32_t pool : 8;
3438 u32_t level : 4;
3439 u32_t block : 20;
3440};
3441
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003442struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003443 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003444 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003445};
3446
Anas Nashif166f5192018-02-25 08:02:36 -06003447/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003448
3449/**
3450 * @defgroup mailbox_apis Mailbox APIs
3451 * @ingroup kernel_apis
3452 * @{
3453 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003454
3455struct k_mbox_msg {
3456 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003457 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003458 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003459 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003460 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003461 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003462 /** sender's message data buffer */
3463 void *tx_data;
3464 /** internal use only - needed for legacy API support */
3465 void *_rx_data;
3466 /** message data block descriptor */
3467 struct k_mem_block tx_block;
3468 /** source thread id */
3469 k_tid_t rx_source_thread;
3470 /** target thread id */
3471 k_tid_t tx_target_thread;
3472 /** internal use only - thread waiting on send (may be a dummy) */
3473 k_tid_t _syncing_thread;
3474#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3475 /** internal use only - semaphore used during asynchronous send */
3476 struct k_sem *_async_sem;
3477#endif
3478};
3479
3480struct k_mbox {
3481 _wait_q_t tx_msg_queue;
3482 _wait_q_t rx_msg_queue;
3483
Anas Nashif2f203c22016-12-18 06:57:45 -05003484 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003485};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003486/**
3487 * @cond INTERNAL_HIDDEN
3488 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003489
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003490#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003491 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003492 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3493 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003494 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003495 }
3496
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003497#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3498
Peter Mitsis12092702016-10-14 12:57:23 -04003499/**
Allan Stephensc98da842016-11-11 15:45:03 -05003500 * INTERNAL_HIDDEN @endcond
3501 */
3502
3503/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003504 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003506 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003507 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003508 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003509 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003510 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003511 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003512 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003513#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003514 struct k_mbox name \
3515 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003516 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003517
Peter Mitsis12092702016-10-14 12:57:23 -04003518/**
3519 * @brief Initialize a mailbox.
3520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003521 * This routine initializes a mailbox object, prior to its first use.
3522 *
3523 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003524 *
3525 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003526 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003527 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003528extern void k_mbox_init(struct k_mbox *mbox);
3529
Peter Mitsis12092702016-10-14 12:57:23 -04003530/**
3531 * @brief Send a mailbox message in a synchronous manner.
3532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003533 * This routine sends a message to @a mbox and waits for a receiver to both
3534 * receive and process it. The message data may be in a buffer, in a memory
3535 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003536 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003537 * @param mbox Address of the mailbox.
3538 * @param tx_msg Address of the transmit message descriptor.
3539 * @param timeout Waiting period for the message to be received (in
3540 * milliseconds), or one of the special values K_NO_WAIT
3541 * and K_FOREVER. Once the message has been received,
3542 * this routine waits as long as necessary for the message
3543 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003544 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003545 * @retval 0 Message sent.
3546 * @retval -ENOMSG Returned without waiting.
3547 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003548 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003549 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003550extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003551 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003552
Peter Mitsis12092702016-10-14 12:57:23 -04003553/**
3554 * @brief Send a mailbox message in an asynchronous manner.
3555 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003556 * This routine sends a message to @a mbox without waiting for a receiver
3557 * to process it. The message data may be in a buffer, in a memory pool block,
3558 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3559 * will be given when the message has been both received and completely
3560 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003562 * @param mbox Address of the mailbox.
3563 * @param tx_msg Address of the transmit message descriptor.
3564 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003565 *
3566 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003567 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003568 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003569extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003570 struct k_sem *sem);
3571
Peter Mitsis12092702016-10-14 12:57:23 -04003572/**
3573 * @brief Receive a mailbox message.
3574 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003575 * This routine receives a message from @a mbox, then optionally retrieves
3576 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003577 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003578 * @param mbox Address of the mailbox.
3579 * @param rx_msg Address of the receive message descriptor.
3580 * @param buffer Address of the buffer to receive data, or NULL to defer data
3581 * retrieval and message disposal until later.
3582 * @param timeout Waiting period for a message to be received (in
3583 * milliseconds), or one of the special values K_NO_WAIT
3584 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003585 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003586 * @retval 0 Message received.
3587 * @retval -ENOMSG Returned without waiting.
3588 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003589 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003590 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003591extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003592 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003593
3594/**
3595 * @brief Retrieve mailbox message data into a buffer.
3596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003597 * This routine completes the processing of a received message by retrieving
3598 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003599 *
3600 * Alternatively, this routine can be used to dispose of a received message
3601 * without retrieving its data.
3602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003603 * @param rx_msg Address of the receive message descriptor.
3604 * @param buffer Address of the buffer to receive data, or NULL to discard
3605 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003606 *
3607 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003608 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003609 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003610extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003611
3612/**
3613 * @brief Retrieve mailbox message data into a memory pool block.
3614 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003615 * This routine completes the processing of a received message by retrieving
3616 * its data into a memory pool block, then disposing of the message.
3617 * The memory pool block that results from successful retrieval must be
3618 * returned to the pool once the data has been processed, even in cases
3619 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003620 *
3621 * Alternatively, this routine can be used to dispose of a received message
3622 * without retrieving its data. In this case there is no need to return a
3623 * memory pool block to the pool.
3624 *
3625 * This routine allocates a new memory pool block for the data only if the
3626 * data is not already in one. If a new block cannot be allocated, the routine
3627 * returns a failure code and the received message is left unchanged. This
3628 * permits the caller to reattempt data retrieval at a later time or to dispose
3629 * of the received message without retrieving its data.
3630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003631 * @param rx_msg Address of a receive message descriptor.
3632 * @param pool Address of memory pool, or NULL to discard data.
3633 * @param block Address of the area to hold memory pool block info.
3634 * @param timeout Waiting period to wait for a memory pool block (in
3635 * milliseconds), or one of the special values K_NO_WAIT
3636 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003637 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003638 * @retval 0 Data retrieved.
3639 * @retval -ENOMEM Returned without waiting.
3640 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003641 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003642 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003643extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003644 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003645 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003646
Anas Nashif166f5192018-02-25 08:02:36 -06003647/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003648
3649/**
Anas Nashifce78d162018-05-24 12:43:11 -05003650 * @defgroup pipe_apis Pipe APIs
3651 * @ingroup kernel_apis
3652 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003653 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003654
Anas Nashifce78d162018-05-24 12:43:11 -05003655/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003656struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003657 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3658 size_t size; /**< Buffer size */
3659 size_t bytes_used; /**< # bytes used in buffer */
3660 size_t read_index; /**< Where in buffer to read from */
3661 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003662
3663 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003664 _wait_q_t readers; /**< Reader wait queue */
3665 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003666 } wait_q;
3667
Anas Nashif2f203c22016-12-18 06:57:45 -05003668 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Anas Nashifce78d162018-05-24 12:43:11 -05003669 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003670};
3671
Anas Nashifce78d162018-05-24 12:43:11 -05003672/**
3673 * @cond INTERNAL_HIDDEN
3674 */
3675#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3676
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003677#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003678 { \
3679 .buffer = pipe_buffer, \
3680 .size = pipe_buffer_size, \
3681 .bytes_used = 0, \
3682 .read_index = 0, \
3683 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003684 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3685 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003686 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003687 }
3688
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003689#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3690
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003691/**
Allan Stephensc98da842016-11-11 15:45:03 -05003692 * INTERNAL_HIDDEN @endcond
3693 */
3694
3695/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003696 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003697 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003698 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003699 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003700 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003702 * @param name Name of the pipe.
3703 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3704 * or zero if no ring buffer is used.
3705 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003706 *
3707 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003708 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003709#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3710 static unsigned char __kernel_noinit __aligned(pipe_align) \
3711 _k_pipe_buf_##name[pipe_buffer_size]; \
3712 struct k_pipe name \
3713 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003714 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003715
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003716/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003717 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003718 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003719 * This routine initializes a pipe object, prior to its first use.
3720 *
3721 * @param pipe Address of the pipe.
3722 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3723 * is used.
3724 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3725 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003726 *
3727 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003728 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003729 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003730void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3731
3732/**
3733 * @brief Release a pipe's allocated buffer
3734 *
3735 * If a pipe object was given a dynamically allocated buffer via
3736 * k_pipe_alloc_init(), this will free it. This function does nothing
3737 * if the buffer wasn't dynamically allocated.
3738 *
3739 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003740 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003741 */
3742void k_pipe_cleanup(struct k_pipe *pipe);
3743
3744/**
3745 * @brief Initialize a pipe and allocate a buffer for it
3746 *
3747 * Storage for the buffer region will be allocated from the calling thread's
3748 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3749 * or userspace is enabled and the pipe object loses all references to it.
3750 *
3751 * This function should only be called on uninitialized pipe objects.
3752 *
3753 * @param pipe Address of the pipe.
3754 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3755 * buffer is used.
3756 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003757 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003758 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003759 */
3760__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003761
3762/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003763 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003764 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003765 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003766 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003767 * @param pipe Address of the pipe.
3768 * @param data Address of data to write.
3769 * @param bytes_to_write Size of data (in bytes).
3770 * @param bytes_written Address of area to hold the number of bytes written.
3771 * @param min_xfer Minimum number of bytes to write.
3772 * @param timeout Waiting period to wait for the data to be written (in
3773 * milliseconds), or one of the special values K_NO_WAIT
3774 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003775 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003776 * @retval 0 At least @a min_xfer bytes of data were written.
3777 * @retval -EIO Returned without waiting; zero data bytes were written.
3778 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003779 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003780 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003781 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003782__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3783 size_t bytes_to_write, size_t *bytes_written,
3784 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003785
3786/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003787 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003788 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003789 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003791 * @param pipe Address of the pipe.
3792 * @param data Address to place the data read from pipe.
3793 * @param bytes_to_read Maximum number of data bytes to read.
3794 * @param bytes_read Address of area to hold the number of bytes read.
3795 * @param min_xfer Minimum number of data bytes to read.
3796 * @param timeout Waiting period to wait for the data to be read (in
3797 * milliseconds), or one of the special values K_NO_WAIT
3798 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003799 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003800 * @retval 0 At least @a min_xfer bytes of data were read.
3801 * @retval -EIO Returned without waiting; zero data bytes were read.
3802 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003803 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003804 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003805 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003806__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3807 size_t bytes_to_read, size_t *bytes_read,
3808 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003809
3810/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003811 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003813 * This routine writes the data contained in a memory block to @a pipe.
3814 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003815 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003817 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003818 * @param block Memory block containing data to send
3819 * @param size Number of data bytes in memory block to send
3820 * @param sem Semaphore to signal upon completion (else NULL)
3821 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003822 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003823 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003824 */
3825extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3826 size_t size, struct k_sem *sem);
3827
Anas Nashif166f5192018-02-25 08:02:36 -06003828/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003829
Allan Stephensc98da842016-11-11 15:45:03 -05003830/**
3831 * @cond INTERNAL_HIDDEN
3832 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003833
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003834struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003835 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003836 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003837 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003838 char *buffer;
3839 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003840 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003841
Anas Nashif2f203c22016-12-18 06:57:45 -05003842 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003843};
3844
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003845#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003846 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003847 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003848 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003849 .num_blocks = slab_num_blocks, \
3850 .block_size = slab_block_size, \
3851 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003852 .free_list = NULL, \
3853 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003854 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003855 }
3856
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003857#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3858
3859
Peter Mitsis578f9112016-10-07 13:50:31 -04003860/**
Allan Stephensc98da842016-11-11 15:45:03 -05003861 * INTERNAL_HIDDEN @endcond
3862 */
3863
3864/**
3865 * @defgroup mem_slab_apis Memory Slab APIs
3866 * @ingroup kernel_apis
3867 * @{
3868 */
3869
3870/**
Allan Stephensda827222016-11-09 14:23:58 -06003871 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003872 *
Allan Stephensda827222016-11-09 14:23:58 -06003873 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003874 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003875 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3876 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003877 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003878 *
Allan Stephensda827222016-11-09 14:23:58 -06003879 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003880 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003881 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003882 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003884 * @param name Name of the memory slab.
3885 * @param slab_block_size Size of each memory block (in bytes).
3886 * @param slab_num_blocks Number memory blocks.
3887 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003888 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003889 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003890#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3891 char __noinit __aligned(slab_align) \
3892 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3893 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003894 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003895 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003896 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003897
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003898/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003899 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003900 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003901 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003902 *
Allan Stephensda827222016-11-09 14:23:58 -06003903 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3904 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3905 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3906 * To ensure that each memory block is similarly aligned to this boundary,
3907 * @a slab_block_size must also be a multiple of N.
3908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003909 * @param slab Address of the memory slab.
3910 * @param buffer Pointer to buffer used for the memory blocks.
3911 * @param block_size Size of each memory block (in bytes).
3912 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003913 *
3914 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003915 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003916 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003917extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003918 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003919
3920/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003921 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003923 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003924 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003925 * @param slab Address of the memory slab.
3926 * @param mem Pointer to block address area.
3927 * @param timeout Maximum time to wait for operation to complete
3928 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3929 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003930 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003931 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003932 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003933 * @retval -ENOMEM Returned without waiting.
3934 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003935 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003936 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003937extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003938 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003939
3940/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003941 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003943 * This routine releases a previously allocated memory block back to its
3944 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003946 * @param slab Address of the memory slab.
3947 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003948 *
3949 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003950 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003951 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003952extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003953
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003954/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003955 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003957 * This routine gets the number of memory blocks that are currently
3958 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003960 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003962 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003963 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003964 */
Kumar Galacc334c72017-04-21 10:55:34 -05003965static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003966{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003967 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003968}
3969
Peter Mitsisc001aa82016-10-13 13:53:37 -04003970/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003971 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003973 * This routine gets the number of memory blocks that are currently
3974 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003975 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003976 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003977 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003978 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003979 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04003980 */
Kumar Galacc334c72017-04-21 10:55:34 -05003981static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003982{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003983 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003984}
3985
Anas Nashif166f5192018-02-25 08:02:36 -06003986/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003987
3988/**
3989 * @cond INTERNAL_HIDDEN
3990 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003991
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003992struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003993 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003994 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003995};
3996
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003997/**
Allan Stephensc98da842016-11-11 15:45:03 -05003998 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003999 */
4000
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004001/**
Allan Stephensc98da842016-11-11 15:45:03 -05004002 * @addtogroup mem_pool_apis
4003 * @{
4004 */
4005
4006/**
4007 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004008 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004009 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4010 * long. The memory pool allows blocks to be repeatedly partitioned into
4011 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004012 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004013 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004014 * If the pool is to be accessed outside the module where it is defined, it
4015 * can be declared via
4016 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004017 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004019 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004020 * @param minsz Size of the smallest blocks in the pool (in bytes).
4021 * @param maxsz Size of the largest blocks in the pool (in bytes).
4022 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004023 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004024 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004025 */
Andy Ross73cb9582017-05-09 10:42:39 -07004026#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4027 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4028 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08004029 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07004030 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004031 .base = { \
4032 .buf = _mpool_buf_##name, \
4033 .max_sz = maxsz, \
4034 .n_max = nmax, \
4035 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
4036 .levels = _mpool_lvls_##name, \
4037 .flags = SYS_MEM_POOL_KERNEL \
4038 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004039 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004040
Peter Mitsis937042c2016-10-13 13:18:26 -04004041/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004042 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004043 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004044 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004046 * @param pool Address of the memory pool.
4047 * @param block Pointer to block descriptor for the allocated memory.
4048 * @param size Amount of memory to allocate (in bytes).
4049 * @param timeout Maximum time to wait for operation to complete
4050 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4051 * or K_FOREVER to wait as long as necessary.
4052 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004053 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004054 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004055 * @retval -ENOMEM Returned without waiting.
4056 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004057 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004058 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004059extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004060 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004061
4062/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004063 * @brief Allocate memory from a memory pool with malloc() semantics
4064 *
4065 * Such memory must be released using k_free().
4066 *
4067 * @param pool Address of the memory pool.
4068 * @param size Amount of memory to allocate (in bytes).
4069 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004070 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004071 */
4072extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4073
4074/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004075 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004076 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004077 * This routine releases a previously allocated memory block back to its
4078 * memory pool.
4079 *
4080 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004081 *
4082 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004083 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004084 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004085extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004086
4087/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004088 * @brief Free memory allocated from a memory pool.
4089 *
4090 * This routine releases a previously allocated memory block back to its
4091 * memory pool
4092 *
4093 * @param id Memory block identifier.
4094 *
4095 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004096 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004097 */
4098extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4099
4100/**
Anas Nashif166f5192018-02-25 08:02:36 -06004101 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004102 */
4103
4104/**
4105 * @defgroup heap_apis Heap Memory Pool APIs
4106 * @ingroup kernel_apis
4107 * @{
4108 */
4109
4110/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004111 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004112 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004113 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004114 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004115 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004116 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004117 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004118 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004119 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004120 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004121extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004122
4123/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004124 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004125 *
4126 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004127 * returned must have been allocated from the heap memory pool or
4128 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004129 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004130 * If @a ptr is NULL, no operation is performed.
4131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004132 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004133 *
4134 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004135 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004136 */
4137extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004138
Allan Stephensc98da842016-11-11 15:45:03 -05004139/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004140 * @brief Allocate memory from heap, array style
4141 *
4142 * This routine provides traditional calloc() semantics. Memory is
4143 * allocated from the heap memory pool and zeroed.
4144 *
4145 * @param nmemb Number of elements in the requested array
4146 * @param size Size of each array element (in bytes).
4147 *
4148 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004149 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004150 */
4151extern void *k_calloc(size_t nmemb, size_t size);
4152
Anas Nashif166f5192018-02-25 08:02:36 -06004153/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004154
Benjamin Walshacc68c12017-01-29 18:57:45 -05004155/* polling API - PRIVATE */
4156
Benjamin Walshb0179862017-02-02 16:39:57 -05004157#ifdef CONFIG_POLL
4158#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
4159#else
4160#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
4161#endif
4162
Benjamin Walshacc68c12017-01-29 18:57:45 -05004163/* private - implementation data created as needed, per-type */
4164struct _poller {
4165 struct k_thread *thread;
Andy Ross55a7e462018-05-31 11:58:09 -07004166 volatile int is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004167};
4168
4169/* private - types bit positions */
4170enum _poll_types_bits {
4171 /* can be used to ignore an event */
4172 _POLL_TYPE_IGNORE,
4173
4174 /* to be signaled by k_poll_signal() */
4175 _POLL_TYPE_SIGNAL,
4176
4177 /* semaphore availability */
4178 _POLL_TYPE_SEM_AVAILABLE,
4179
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004180 /* queue/fifo/lifo data availability */
4181 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004182
4183 _POLL_NUM_TYPES
4184};
4185
4186#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4187
4188/* private - states bit positions */
4189enum _poll_states_bits {
4190 /* default state when creating event */
4191 _POLL_STATE_NOT_READY,
4192
Benjamin Walshacc68c12017-01-29 18:57:45 -05004193 /* signaled by k_poll_signal() */
4194 _POLL_STATE_SIGNALED,
4195
4196 /* semaphore is available */
4197 _POLL_STATE_SEM_AVAILABLE,
4198
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004199 /* data is available to read on queue/fifo/lifo */
4200 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004201
4202 _POLL_NUM_STATES
4203};
4204
4205#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4206
4207#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004208 (32 - (0 \
4209 + 8 /* tag */ \
4210 + _POLL_NUM_TYPES \
4211 + _POLL_NUM_STATES \
4212 + 1 /* modes */ \
4213 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004214
Benjamin Walshacc68c12017-01-29 18:57:45 -05004215/* end of polling API - PRIVATE */
4216
4217
4218/**
4219 * @defgroup poll_apis Async polling APIs
4220 * @ingroup kernel_apis
4221 * @{
4222 */
4223
4224/* Public polling API */
4225
4226/* public - values for k_poll_event.type bitfield */
4227#define K_POLL_TYPE_IGNORE 0
4228#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4229#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004230#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4231#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004232
4233/* public - polling modes */
4234enum k_poll_modes {
4235 /* polling thread does not take ownership of objects when available */
4236 K_POLL_MODE_NOTIFY_ONLY = 0,
4237
4238 K_POLL_NUM_MODES
4239};
4240
4241/* public - values for k_poll_event.state bitfield */
4242#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004243#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4244#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004245#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4246#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004247
4248/* public - poll signal object */
4249struct k_poll_signal {
4250 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004251 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004252
4253 /*
4254 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4255 * user resets it to 0.
4256 */
4257 unsigned int signaled;
4258
4259 /* custom result value passed to k_poll_signal() if needed */
4260 int result;
4261};
4262
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004263#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004264 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004265 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004266 .signaled = 0, \
4267 .result = 0, \
4268 }
4269
4270struct k_poll_event {
4271 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004272 sys_dnode_t _node;
4273
4274 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004275 struct _poller *poller;
4276
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004277 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004278 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004279
Benjamin Walshacc68c12017-01-29 18:57:45 -05004280 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004281 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004282
4283 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004284 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004285
4286 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004287 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004288
4289 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004290 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004291
4292 /* per-type data */
4293 union {
4294 void *obj;
4295 struct k_poll_signal *signal;
4296 struct k_sem *sem;
4297 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004298 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004299 };
4300};
4301
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004302#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004303 { \
4304 .poller = NULL, \
4305 .type = event_type, \
4306 .state = K_POLL_STATE_NOT_READY, \
4307 .mode = event_mode, \
4308 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004309 { .obj = event_obj }, \
4310 }
4311
4312#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4313 event_tag) \
4314 { \
4315 .type = event_type, \
4316 .tag = event_tag, \
4317 .state = K_POLL_STATE_NOT_READY, \
4318 .mode = event_mode, \
4319 .unused = 0, \
4320 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004321 }
4322
4323/**
4324 * @brief Initialize one struct k_poll_event instance
4325 *
4326 * After this routine is called on a poll event, the event it ready to be
4327 * placed in an event array to be passed to k_poll().
4328 *
4329 * @param event The event to initialize.
4330 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4331 * values. Only values that apply to the same object being polled
4332 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4333 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004334 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004335 * @param obj Kernel object or poll signal.
4336 *
4337 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004338 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004339 */
4340
Kumar Galacc334c72017-04-21 10:55:34 -05004341extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004342 int mode, void *obj);
4343
4344/**
4345 * @brief Wait for one or many of multiple poll events to occur
4346 *
4347 * This routine allows a thread to wait concurrently for one or many of
4348 * multiple poll events to have occurred. Such events can be a kernel object
4349 * being available, like a semaphore, or a poll signal event.
4350 *
4351 * When an event notifies that a kernel object is available, the kernel object
4352 * is not "given" to the thread calling k_poll(): it merely signals the fact
4353 * that the object was available when the k_poll() call was in effect. Also,
4354 * all threads trying to acquire an object the regular way, i.e. by pending on
4355 * the object, have precedence over the thread polling on the object. This
4356 * means that the polling thread will never get the poll event on an object
4357 * until the object becomes available and its pend queue is empty. For this
4358 * reason, the k_poll() call is more effective when the objects being polled
4359 * only have one thread, the polling thread, trying to acquire them.
4360 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004361 * When k_poll() returns 0, the caller should loop on all the events that were
4362 * passed to k_poll() and check the state field for the values that were
4363 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004364 *
4365 * Before being reused for another call to k_poll(), the user has to reset the
4366 * state field to K_POLL_STATE_NOT_READY.
4367 *
Andrew Boie3772f772018-05-07 16:52:57 -07004368 * When called from user mode, a temporary memory allocation is required from
4369 * the caller's resource pool.
4370 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004371 * @param events An array of pointers to events to be polled for.
4372 * @param num_events The number of events in the array.
4373 * @param timeout Waiting period for an event to be ready (in milliseconds),
4374 * or one of the special values K_NO_WAIT and K_FOREVER.
4375 *
4376 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004377 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02004378 * @retval -EINTR Poller thread has been interrupted.
Andrew Boie3772f772018-05-07 16:52:57 -07004379 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4380 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004381 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004382 */
4383
Andrew Boie3772f772018-05-07 16:52:57 -07004384__syscall int k_poll(struct k_poll_event *events, int num_events,
4385 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004386
4387/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004388 * @brief Initialize a poll signal object.
4389 *
4390 * Ready a poll signal object to be signaled via k_poll_signal().
4391 *
4392 * @param signal A poll signal.
4393 *
4394 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004395 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004396 */
4397
Andrew Boie3772f772018-05-07 16:52:57 -07004398__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4399
4400/*
4401 * @brief Reset a poll signal object's state to unsignaled.
4402 *
4403 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004404 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004405 */
4406__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4407
4408static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4409{
4410 signal->signaled = 0;
4411}
4412
4413/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004414 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004415 *
4416 * @param signal A poll signal object
4417 * @param signaled An integer buffer which will be written nonzero if the
4418 * object was signaled
4419 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004420 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004421 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004422 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004423 */
4424__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4425 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004426
4427/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004428 * @brief Signal a poll signal object.
4429 *
4430 * This routine makes ready a poll signal, which is basically a poll event of
4431 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4432 * made ready to run. A @a result value can be specified.
4433 *
4434 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004435 * k_poll_signal(), stays set until the user sets it back to 0 with
4436 * k_poll_signal_reset(). It thus has to be reset by the user before being
4437 * passed again to k_poll() or k_poll() will consider it being signaled, and
4438 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004439 *
4440 * @param signal A poll signal.
4441 * @param result The value to store in the result field of the signal.
4442 *
4443 * @retval 0 The signal was delivered successfully.
4444 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004445 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004446 */
4447
Andrew Boie3772f772018-05-07 16:52:57 -07004448__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004449
Anas Nashif954d5502018-02-25 08:37:28 -06004450/**
4451 * @internal
4452 */
Andy Ross8606fab2018-03-26 10:54:40 -07004453extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004454
Anas Nashif166f5192018-02-25 08:02:36 -06004455/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004456
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004457/**
4458 * @brief Make the CPU idle.
4459 *
4460 * This function makes the CPU idle until an event wakes it up.
4461 *
4462 * In a regular system, the idle thread should be the only thread responsible
4463 * for making the CPU idle and triggering any type of power management.
4464 * However, in some more constrained systems, such as a single-threaded system,
4465 * the only thread would be responsible for this if needed.
4466 *
4467 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004468 * @req K-MISC-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004469 */
4470extern void k_cpu_idle(void);
4471
4472/**
4473 * @brief Make the CPU idle in an atomic fashion.
4474 *
4475 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4476 * must be done atomically before making the CPU idle.
4477 *
4478 * @param key Interrupt locking key obtained from irq_lock().
4479 *
4480 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004481 * @req K-MISC-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004482 */
4483extern void k_cpu_atomic_idle(unsigned int key);
4484
Anas Nashif954d5502018-02-25 08:37:28 -06004485
4486/**
4487 * @internal
4488 */
Kumar Galacc334c72017-04-21 10:55:34 -05004489extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004490
Andrew Boiecdb94d62017-04-18 15:22:05 -07004491#ifdef _ARCH_EXCEPT
4492/* This archtecture has direct support for triggering a CPU exception */
4493#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4494#else
4495
Andrew Boiecdb94d62017-04-18 15:22:05 -07004496/* NOTE: This is the implementation for arches that do not implement
4497 * _ARCH_EXCEPT() to generate a real CPU exception.
4498 *
4499 * We won't have a real exception frame to determine the PC value when
4500 * the oops occurred, so print file and line number before we jump into
4501 * the fatal error handler.
4502 */
4503#define _k_except_reason(reason) do { \
4504 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4505 _NanoFatalErrorHandler(reason, &_default_esf); \
4506 CODE_UNREACHABLE; \
4507 } while (0)
4508
4509#endif /* _ARCH__EXCEPT */
4510
4511/**
4512 * @brief Fatally terminate a thread
4513 *
4514 * This should be called when a thread has encountered an unrecoverable
4515 * runtime condition and needs to terminate. What this ultimately
4516 * means is determined by the _fatal_error_handler() implementation, which
4517 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4518 *
4519 * If this is called from ISR context, the default system fatal error handler
4520 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004521 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004522 */
4523#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4524
4525/**
4526 * @brief Fatally terminate the system
4527 *
4528 * This should be called when the Zephyr kernel has encountered an
4529 * unrecoverable runtime condition and needs to terminate. What this ultimately
4530 * means is determined by the _fatal_error_handler() implementation, which
4531 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004532 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004533 */
4534#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4535
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004536/*
4537 * private APIs that are utilized by one or more public APIs
4538 */
4539
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004540#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004541/**
4542 * @internal
4543 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004544extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004545#else
Anas Nashif954d5502018-02-25 08:37:28 -06004546/**
4547 * @internal
4548 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004549#define _init_static_threads() do { } while ((0))
4550#endif
4551
Anas Nashif954d5502018-02-25 08:37:28 -06004552/**
4553 * @internal
4554 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004555extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004556/**
4557 * @internal
4558 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004559extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004560
Andrew Boiedc5d9352017-06-02 12:56:47 -07004561/* arch/cpu.h may declare an architecture or platform-specific macro
4562 * for properly declaring stacks, compatible with MMU/MPU constraints if
4563 * enabled
4564 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004565
4566/**
4567 * @brief Obtain an extern reference to a stack
4568 *
4569 * This macro properly brings the symbol of a thread stack declared
4570 * elsewhere into scope.
4571 *
4572 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004573 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004574 */
4575#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4576
Andrew Boiedc5d9352017-06-02 12:56:47 -07004577#ifdef _ARCH_THREAD_STACK_DEFINE
4578#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4579#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4580 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304581#define K_THREAD_STACK_LEN(size) _ARCH_THREAD_STACK_LEN(size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004582#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4583#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004584static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004585{
4586 return _ARCH_THREAD_STACK_BUFFER(sym);
4587}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004588#else
4589/**
4590 * @brief Declare a toplevel thread stack memory region
4591 *
4592 * This declares a region of memory suitable for use as a thread's stack.
4593 *
4594 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4595 * 'noinit' section so that it isn't zeroed at boot
4596 *
Andrew Boie507852a2017-07-25 18:47:07 -07004597 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004598 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004599 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004600 *
4601 * It is legal to precede this definition with the 'static' keyword.
4602 *
4603 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4604 * parameter of k_thread_create(), it may not be the same as the
4605 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4606 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004607 * Some arches may round the size of the usable stack region up to satisfy
4608 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4609 * size.
4610 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004611 * @param sym Thread stack symbol name
4612 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004613 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004614 */
4615#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004616 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004617
4618/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304619 * @brief Calculate size of stacks to be allocated in a stack array
4620 *
4621 * This macro calculates the size to be allocated for the stacks
4622 * inside a stack array. It accepts the indicated "size" as a parameter
4623 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4624 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4625 *
4626 * @param size Size of the stack memory region
4627 * @req K-TSTACK-001
4628 */
4629#define K_THREAD_STACK_LEN(size) (size)
4630
4631/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004632 * @brief Declare a toplevel array of thread stack memory regions
4633 *
4634 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4635 * definition for additional details and constraints.
4636 *
4637 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4638 * 'noinit' section so that it isn't zeroed at boot
4639 *
4640 * @param sym Thread stack symbol name
4641 * @param nmemb Number of stacks to declare
4642 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004643 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004644 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004645#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004646 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304647 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004648
4649/**
4650 * @brief Declare an embedded stack memory region
4651 *
4652 * Used for stacks embedded within other data structures. Use is highly
4653 * discouraged but in some cases necessary. For memory protection scenarios,
4654 * it is very important that any RAM preceding this member not be writable
4655 * by threads else a stack overflow will lead to silent corruption. In other
4656 * words, the containing data structure should live in RAM owned by the kernel.
4657 *
4658 * @param sym Thread stack symbol name
4659 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004660 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004661 */
4662#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004663 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004664
4665/**
4666 * @brief Return the size in bytes of a stack memory region
4667 *
4668 * Convenience macro for passing the desired stack size to k_thread_create()
4669 * since the underlying implementation may actually create something larger
4670 * (for instance a guard area).
4671 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004672 * The value returned here is not guaranteed to match the 'size' parameter
4673 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004674 *
4675 * @param sym Stack memory symbol
4676 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004677 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004678 */
4679#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4680
4681/**
4682 * @brief Get a pointer to the physical stack buffer
4683 *
4684 * Convenience macro to get at the real underlying stack buffer used by
4685 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4686 * This is really only intended for diagnostic tools which want to examine
4687 * stack memory contents.
4688 *
4689 * @param sym Declared stack symbol name
4690 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004691 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004692 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004693static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004694{
4695 return (char *)sym;
4696}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004697
4698#endif /* _ARCH_DECLARE_STACK */
4699
Chunlin Hane9c97022017-07-07 20:29:30 +08004700/**
4701 * @defgroup mem_domain_apis Memory domain APIs
4702 * @ingroup kernel_apis
4703 * @{
4704 */
4705
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004706/**
4707 * @def MEM_PARTITION_ENTRY
4708 * @brief Used to declare a memory partition entry
4709 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004710 */
4711#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4712 {\
4713 .start = _start, \
4714 .size = _size, \
4715 .attr = _attr, \
4716 }
4717
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004718/**
4719 * @def K_MEM_PARTITION_DEFINE
4720 * @brief Used to declare a memory partition
4721 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004722 */
4723#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4724#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4725 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304726 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004727 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4728#else
4729#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304730 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004731 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4732#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4733
Chunlin Hane9c97022017-07-07 20:29:30 +08004734/* memory partition */
4735struct k_mem_partition {
4736 /* start address of memory partition */
4737 u32_t start;
4738 /* size of memory partition */
4739 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304740#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004741 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304742 k_mem_partition_attr_t attr;
4743#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004744};
4745
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304746/* memory domian
4747 * Note: Always declare this structure with __kernel prefix
4748 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004749struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304750#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004751 /* partitions in the domain */
4752 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304753#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004754 /* domain q */
4755 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004756 /* number of partitions in the domain */
4757 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004758};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304759
Chunlin Hane9c97022017-07-07 20:29:30 +08004760
4761/**
4762 * @brief Initialize a memory domain.
4763 *
4764 * Initialize a memory domain with given name and memory partitions.
4765 *
4766 * @param domain The memory domain to be initialized.
4767 * @param num_parts The number of array items of "parts" parameter.
4768 * @param parts An array of pointers to the memory partitions. Can be NULL
4769 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004770 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004771 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004772extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004773 struct k_mem_partition *parts[]);
4774/**
4775 * @brief Destroy a memory domain.
4776 *
4777 * Destroy a memory domain.
4778 *
4779 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004780 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004781 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004782extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4783
4784/**
4785 * @brief Add a memory partition into a memory domain.
4786 *
4787 * Add a memory partition into a memory domain.
4788 *
4789 * @param domain The memory domain to be added a memory partition.
4790 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004791 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004792 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004793extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4794 struct k_mem_partition *part);
4795
4796/**
4797 * @brief Remove a memory partition from a memory domain.
4798 *
4799 * Remove a memory partition from a memory domain.
4800 *
4801 * @param domain The memory domain to be removed a memory partition.
4802 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004803 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004804 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004805extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4806 struct k_mem_partition *part);
4807
4808/**
4809 * @brief Add a thread into a memory domain.
4810 *
4811 * Add a thread into a memory domain.
4812 *
4813 * @param domain The memory domain that the thread is going to be added into.
4814 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004815 *
4816 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004817 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004818extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4819 k_tid_t thread);
4820
4821/**
4822 * @brief Remove a thread from its memory domain.
4823 *
4824 * Remove a thread from its memory domain.
4825 *
4826 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004827 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004828 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004829extern void k_mem_domain_remove_thread(k_tid_t thread);
4830
Anas Nashif166f5192018-02-25 08:02:36 -06004831/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004832
Andrew Boie756f9072017-10-10 16:01:49 -07004833/**
4834 * @brief Emit a character buffer to the console device
4835 *
4836 * @param c String of characters to print
4837 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004838 *
4839 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004840 */
4841__syscall void k_str_out(char *c, size_t n);
4842
Andy Rosse7172672018-01-24 15:48:32 -08004843/**
4844 * @brief Start a numbered CPU on a MP-capable system
4845
4846 * This starts and initializes a specific CPU. The main thread on
4847 * startup is running on CPU zero, other processors are numbered
4848 * sequentially. On return from this function, the CPU is known to
4849 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004850 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004851 * with the provided key will work to enable them.
4852 *
4853 * Normally, in SMP mode this function will be called by the kernel
4854 * initialization and should not be used as a user API. But it is
4855 * defined here for special-purpose apps which want Zephyr running on
4856 * one core and to use others for design-specific processing.
4857 *
4858 * @param cpu_num Integer number of the CPU
4859 * @param stack Stack memory for the CPU
4860 * @param sz Stack buffer size, in bytes
4861 * @param fn Function to begin running on the CPU. First argument is
4862 * an irq_unlock() key.
4863 * @param arg Untyped argument to be passed to "fn"
4864 */
4865extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4866 void (*fn)(int, void *), void *arg);
4867
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004868#ifdef __cplusplus
4869}
4870#endif
4871
Andrew Boiee004dec2016-11-07 09:01:19 -08004872#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4873/*
4874 * Define new and delete operators.
4875 * At this moment, the operators do nothing since objects are supposed
4876 * to be statically allocated.
4877 */
4878inline void operator delete(void *ptr)
4879{
4880 (void)ptr;
4881}
4882
4883inline void operator delete[](void *ptr)
4884{
4885 (void)ptr;
4886}
4887
4888inline void *operator new(size_t size)
4889{
4890 (void)size;
4891 return NULL;
4892}
4893
4894inline void *operator new[](size_t size)
4895{
4896 (void)size;
4897 return NULL;
4898}
4899
4900/* Placement versions of operator new and delete */
4901inline void operator delete(void *ptr1, void *ptr2)
4902{
4903 (void)ptr1;
4904 (void)ptr2;
4905}
4906
4907inline void operator delete[](void *ptr1, void *ptr2)
4908{
4909 (void)ptr1;
4910 (void)ptr2;
4911}
4912
4913inline void *operator new(size_t size, void *ptr)
4914{
4915 (void)size;
4916 return ptr;
4917}
4918
4919inline void *operator new[](size_t size, void *ptr)
4920{
4921 (void)size;
4922 return ptr;
4923}
4924
4925#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4926
Andrew Boiefa94ee72017-09-28 16:54:35 -07004927#include <syscalls/kernel.h>
4928
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004929#endif /* !_ASMLANGUAGE */
4930
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004931#endif /* _kernel__h_ */