blob: 1dfe96b719fef024f0a24a9387e1dcd038dbc1a5 [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ęcik91fe22e2018-06-11 14:24:41 +02001322#ifdef CONFIG_SYS_CLOCK_EXISTS
1323#if (CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC % sys_clock_ticks_per_sec) != 0
1324 #define _NEED_PRECISE_TICK_MS_CONVERSION
1325#elif (MSEC_PER_SEC % sys_clock_ticks_per_sec) != 0
Benjamin Walsh62092182016-12-20 14:39:08 -05001326 #define _NON_OPTIMIZED_TICKS_PER_SEC
1327#endif
1328
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001329#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
1330 #define _NEED_PRECISE_TICK_MS_CONVERSION
1331#endif
1332#endif
1333
Kumar Galacc334c72017-04-21 10:55:34 -05001334static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001335{
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001336#ifdef CONFIG_SYS_CLOCK_EXISTS
1337
1338#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
1339 /* use 64-bit math to keep precision */
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001340 s64_t ms_ticks_per_sec = (s64_t)ms * sys_clock_ticks_per_sec;
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001341
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001342 return (s32_t)ceiling_fraction(ms_ticks_per_sec, MSEC_PER_SEC);
1343#else
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001344 /* simple division keeps precision */
1345 s32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
1346
1347 return (s32_t)ceiling_fraction(ms, ms_per_tick);
1348#endif
1349
1350#else
1351 __ASSERT(ms == 0, "ms not zero");
1352 return 0;
Benjamin Walsh62092182016-12-20 14:39:08 -05001353#endif
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001354}
Benjamin Walsh62092182016-12-20 14:39:08 -05001355
Kumar Galacc334c72017-04-21 10:55:34 -05001356static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001357{
Benjamin Walsh62092182016-12-20 14:39:08 -05001358#ifdef CONFIG_SYS_CLOCK_EXISTS
1359
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001360#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
1361 /* use 64-bit math to keep precision */
1362 return (u64_t)ticks * sys_clock_hw_cycles_per_tick * MSEC_PER_SEC /
1363 CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001364#else
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001365 /* simple multiplication keeps precision */
1366 u32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
1367
1368 return (u64_t)ticks * ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001369#endif
1370
1371#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001372 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001373 return 0;
1374#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001375}
1376
Piotr Zięcik77f42f82018-06-11 14:26:29 +02001377/* added tick needed to account for tick in progress */
1378#ifdef CONFIG_TICKLESS_KERNEL
1379#define _TICK_ALIGN 0
1380#else
1381#define _TICK_ALIGN 1
1382#endif
1383
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001384struct k_timer {
1385 /*
1386 * _timeout structure must be first here if we want to use
1387 * dynamic timer allocation. timeout.node is used in the double-linked
1388 * list of free timers
1389 */
1390 struct _timeout timeout;
1391
Allan Stephens45bfa372016-10-12 12:39:42 -05001392 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001393 _wait_q_t wait_q;
1394
1395 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001396 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001397
1398 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001399 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001400
1401 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001402 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001403
Allan Stephens45bfa372016-10-12 12:39:42 -05001404 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001405 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001406
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001407 /* user-specific data, also used to support legacy features */
1408 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001409
Anas Nashif2f203c22016-12-18 06:57:45 -05001410 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001411};
1412
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001413#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001414 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001415 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001416 .timeout.wait_q = NULL, \
1417 .timeout.thread = NULL, \
1418 .timeout.func = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001419 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001420 .expiry_fn = expiry, \
1421 .stop_fn = stop, \
1422 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001423 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001424 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001425 }
1426
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001427#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1428
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001429/**
Allan Stephensc98da842016-11-11 15:45:03 -05001430 * INTERNAL_HIDDEN @endcond
1431 */
1432
1433/**
1434 * @defgroup timer_apis Timer APIs
1435 * @ingroup kernel_apis
1436 * @{
1437 */
1438
1439/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001440 * @typedef k_timer_expiry_t
1441 * @brief Timer expiry function type.
1442 *
1443 * A timer's expiry function is executed by the system clock interrupt handler
1444 * each time the timer expires. The expiry function is optional, and is only
1445 * invoked if the timer has been initialized with one.
1446 *
1447 * @param timer Address of timer.
1448 *
1449 * @return N/A
1450 */
1451typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1452
1453/**
1454 * @typedef k_timer_stop_t
1455 * @brief Timer stop function type.
1456 *
1457 * A timer's stop function is executed if the timer is stopped prematurely.
1458 * The function runs in the context of the thread that stops the timer.
1459 * The stop function is optional, and is only invoked if the timer has been
1460 * initialized with one.
1461 *
1462 * @param timer Address of timer.
1463 *
1464 * @return N/A
1465 */
1466typedef void (*k_timer_stop_t)(struct k_timer *timer);
1467
1468/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001469 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001470 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001471 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001472 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001473 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001474 *
1475 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001476 * @param expiry_fn Function to invoke each time the timer expires.
1477 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001478 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001479#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001480 struct k_timer name \
1481 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001482 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001483
Allan Stephens45bfa372016-10-12 12:39:42 -05001484/**
1485 * @brief Initialize a timer.
1486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001487 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001488 *
1489 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001490 * @param expiry_fn Function to invoke each time the timer expires.
1491 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001492 *
1493 * @return N/A
1494 */
1495extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001496 k_timer_expiry_t expiry_fn,
1497 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001498
Allan Stephens45bfa372016-10-12 12:39:42 -05001499/**
1500 * @brief Start a timer.
1501 *
1502 * This routine starts a timer, and resets its status to zero. The timer
1503 * begins counting down using the specified duration and period values.
1504 *
1505 * Attempting to start a timer that is already running is permitted.
1506 * The timer's status is reset to zero and the timer begins counting down
1507 * using the new duration and period values.
1508 *
1509 * @param timer Address of timer.
1510 * @param duration Initial timer duration (in milliseconds).
1511 * @param period Timer period (in milliseconds).
1512 *
1513 * @return N/A
1514 */
Andrew Boiea354d492017-09-29 16:22:28 -07001515__syscall void k_timer_start(struct k_timer *timer,
1516 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001517
1518/**
1519 * @brief Stop a timer.
1520 *
1521 * This routine stops a running timer prematurely. The timer's stop function,
1522 * if one exists, is invoked by the caller.
1523 *
1524 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001525 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001526 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001527 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1528 * if @a k_timer_stop is to be called from ISRs.
1529 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001530 * @param timer Address of timer.
1531 *
1532 * @return N/A
1533 */
Andrew Boiea354d492017-09-29 16:22:28 -07001534__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001535
1536/**
1537 * @brief Read timer status.
1538 *
1539 * This routine reads the timer's status, which indicates the number of times
1540 * it has expired since its status was last read.
1541 *
1542 * Calling this routine resets the timer's status to zero.
1543 *
1544 * @param timer Address of timer.
1545 *
1546 * @return Timer status.
1547 */
Andrew Boiea354d492017-09-29 16:22:28 -07001548__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001549
1550/**
1551 * @brief Synchronize thread to timer expiration.
1552 *
1553 * This routine blocks the calling thread until the timer's status is non-zero
1554 * (indicating that it has expired at least once since it was last examined)
1555 * or the timer is stopped. If the timer status is already non-zero,
1556 * or the timer is already stopped, the caller continues without waiting.
1557 *
1558 * Calling this routine resets the timer's status to zero.
1559 *
1560 * This routine must not be used by interrupt handlers, since they are not
1561 * allowed to block.
1562 *
1563 * @param timer Address of timer.
1564 *
1565 * @return Timer status.
1566 */
Andrew Boiea354d492017-09-29 16:22:28 -07001567__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001568
1569/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001570 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001571 *
1572 * This routine computes the (approximate) time remaining before a running
1573 * timer next expires. If the timer is not running, it returns zero.
1574 *
1575 * @param timer Address of timer.
1576 *
1577 * @return Remaining time (in milliseconds).
1578 */
Andrew Boiea354d492017-09-29 16:22:28 -07001579__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1580
1581static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001582{
1583 return _timeout_remaining_get(&timer->timeout);
1584}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001585
Allan Stephensc98da842016-11-11 15:45:03 -05001586/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001587 * @brief Associate user-specific data with a timer.
1588 *
1589 * This routine records the @a user_data with the @a timer, to be retrieved
1590 * later.
1591 *
1592 * It can be used e.g. in a timer handler shared across multiple subsystems to
1593 * retrieve data specific to the subsystem this timer is associated with.
1594 *
1595 * @param timer Address of timer.
1596 * @param user_data User data to associate with the timer.
1597 *
1598 * @return N/A
1599 */
Andrew Boiea354d492017-09-29 16:22:28 -07001600__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1601
Anas Nashif954d5502018-02-25 08:37:28 -06001602/**
1603 * @internal
1604 */
Andrew Boiea354d492017-09-29 16:22:28 -07001605static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1606 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001607{
1608 timer->user_data = user_data;
1609}
1610
1611/**
1612 * @brief Retrieve the user-specific data from a timer.
1613 *
1614 * @param timer Address of timer.
1615 *
1616 * @return The user data.
1617 */
Andrew Boiea354d492017-09-29 16:22:28 -07001618__syscall void *k_timer_user_data_get(struct k_timer *timer);
1619
1620static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001621{
1622 return timer->user_data;
1623}
1624
Anas Nashif166f5192018-02-25 08:02:36 -06001625/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001626
Allan Stephensc98da842016-11-11 15:45:03 -05001627/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001628 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001629 * @{
1630 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001631
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001632/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001633 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001635 * This routine returns the elapsed time since the system booted,
1636 * in milliseconds.
1637 *
1638 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001639 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001640__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001641
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001642/**
1643 * @brief Enable clock always on in tickless kernel
1644 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001645 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001646 * there are no timer events programmed in tickless kernel
1647 * scheduling. This is necessary if the clock is used to track
1648 * passage of time.
1649 *
1650 * @retval prev_status Previous status of always on flag
1651 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301652#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001653static inline int k_enable_sys_clock_always_on(void)
1654{
1655 int prev_status = _sys_clock_always_on;
1656
1657 _sys_clock_always_on = 1;
1658 _enable_sys_clock();
1659
1660 return prev_status;
1661}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301662#else
1663#define k_enable_sys_clock_always_on() do { } while ((0))
1664#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001665
1666/**
1667 * @brief Disable clock always on in tickless kernel
1668 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001669 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001670 * there are no timer events programmed in tickless kernel
1671 * scheduling. To save power, this routine should be called
1672 * immediately when clock is not used to track time.
1673 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301674#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001675static inline void k_disable_sys_clock_always_on(void)
1676{
1677 _sys_clock_always_on = 0;
1678}
1679#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001680#define k_disable_sys_clock_always_on() do { } while ((0))
1681#endif
1682
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001683/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001684 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001686 * This routine returns the lower 32-bits of the elapsed time since the system
1687 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001688 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001689 * This routine can be more efficient than k_uptime_get(), as it reduces the
1690 * need for interrupt locking and 64-bit math. However, the 32-bit result
1691 * cannot hold a system uptime time larger than approximately 50 days, so the
1692 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001694 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001695 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001696__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001697
1698/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001699 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001701 * This routine computes the elapsed time between the current system uptime
1702 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001703 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001704 * @param reftime Pointer to a reference time, which is updated to the current
1705 * uptime upon return.
1706 *
1707 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001708 */
Kumar Galacc334c72017-04-21 10:55:34 -05001709extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001710
1711/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001712 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001713 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001714 * This routine computes the elapsed time between the current system uptime
1715 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001717 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1718 * need for interrupt locking and 64-bit math. However, the 32-bit result
1719 * cannot hold an elapsed time larger than approximately 50 days, so the
1720 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001722 * @param reftime Pointer to a reference time, which is updated to the current
1723 * uptime upon return.
1724 *
1725 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001726 */
Kumar Galacc334c72017-04-21 10:55:34 -05001727extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001728
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001729/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001730 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001731 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001732 * This routine returns the current time, as measured by the system's hardware
1733 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001734 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001735 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001736 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001737#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001738
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001739/**
Anas Nashif166f5192018-02-25 08:02:36 -06001740 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001741 */
1742
Allan Stephensc98da842016-11-11 15:45:03 -05001743/**
1744 * @cond INTERNAL_HIDDEN
1745 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001746
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001747struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001748 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001749 union {
1750 _wait_q_t wait_q;
1751
1752 _POLL_EVENT;
1753 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001754
1755 _OBJECT_TRACING_NEXT_PTR(k_queue);
1756};
1757
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001758#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001759 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001760 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001761 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001762 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001763 _OBJECT_TRACING_INIT \
1764 }
1765
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001766#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1767
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001768extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1769
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001770/**
1771 * INTERNAL_HIDDEN @endcond
1772 */
1773
1774/**
1775 * @defgroup queue_apis Queue APIs
1776 * @ingroup kernel_apis
1777 * @{
1778 */
1779
1780/**
1781 * @brief Initialize a queue.
1782 *
1783 * This routine initializes a queue object, prior to its first use.
1784 *
1785 * @param queue Address of the queue.
1786 *
1787 * @return N/A
1788 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001789__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001790
1791/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001792 * @brief Cancel waiting on a queue.
1793 *
1794 * This routine causes first thread pending on @a queue, if any, to
1795 * return from k_queue_get() call with NULL value (as if timeout expired).
1796 *
1797 * @note Can be called by ISRs.
1798 *
1799 * @param queue Address of the queue.
1800 *
1801 * @return N/A
1802 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001803__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001804
1805/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001806 * @brief Append an element to the end of a queue.
1807 *
1808 * This routine appends a data item to @a queue. A queue data item must be
1809 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1810 * reserved for the kernel's use.
1811 *
1812 * @note Can be called by ISRs.
1813 *
1814 * @param queue Address of the queue.
1815 * @param data Address of the data item.
1816 *
1817 * @return N/A
1818 */
1819extern void k_queue_append(struct k_queue *queue, void *data);
1820
1821/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001822 * @brief Append an element to a queue.
1823 *
1824 * This routine appends a data item to @a queue. There is an implicit
1825 * memory allocation from the calling thread's resource pool, which is
1826 * automatically freed when the item is removed from the queue.
1827 *
1828 * @note Can be called by ISRs.
1829 *
1830 * @param queue Address of the queue.
1831 * @param data Address of the data item.
1832 *
1833 * @retval 0 on success
1834 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1835 */
1836__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1837
1838/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001839 * @brief Prepend an element to a queue.
1840 *
1841 * This routine prepends a data item to @a queue. A queue data item must be
1842 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1843 * reserved for the kernel's use.
1844 *
1845 * @note Can be called by ISRs.
1846 *
1847 * @param queue Address of the queue.
1848 * @param data Address of the data item.
1849 *
1850 * @return N/A
1851 */
1852extern void k_queue_prepend(struct k_queue *queue, void *data);
1853
1854/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001855 * @brief Prepend an element to a queue.
1856 *
1857 * This routine prepends a data item to @a queue. There is an implicit
1858 * memory allocation from the calling thread's resource pool, which is
1859 * automatically freed when the item is removed from the queue.
1860 *
1861 * @note Can be called by ISRs.
1862 *
1863 * @param queue Address of the queue.
1864 * @param data Address of the data item.
1865 *
1866 * @retval 0 on success
1867 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1868 */
1869__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1870
1871/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001872 * @brief Inserts an element to a queue.
1873 *
1874 * This routine inserts a data item to @a queue after previous item. A queue
1875 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1876 * item are reserved for the kernel's use.
1877 *
1878 * @note Can be called by ISRs.
1879 *
1880 * @param queue Address of the queue.
1881 * @param prev Address of the previous data item.
1882 * @param data Address of the data item.
1883 *
1884 * @return N/A
1885 */
1886extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1887
1888/**
1889 * @brief Atomically append a list of elements to a queue.
1890 *
1891 * This routine adds a list of data items to @a queue in one operation.
1892 * The data items must be in a singly-linked list, with the first 32 bits
1893 * in each data item pointing to the next data item; the list must be
1894 * NULL-terminated.
1895 *
1896 * @note Can be called by ISRs.
1897 *
1898 * @param queue Address of the queue.
1899 * @param head Pointer to first node in singly-linked list.
1900 * @param tail Pointer to last node in singly-linked list.
1901 *
1902 * @return N/A
1903 */
1904extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1905
1906/**
1907 * @brief Atomically add a list of elements to a queue.
1908 *
1909 * This routine adds a list of data items to @a queue in one operation.
1910 * The data items must be in a singly-linked list implemented using a
1911 * sys_slist_t object. Upon completion, the original list is empty.
1912 *
1913 * @note Can be called by ISRs.
1914 *
1915 * @param queue Address of the queue.
1916 * @param list Pointer to sys_slist_t object.
1917 *
1918 * @return N/A
1919 */
1920extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1921
1922/**
1923 * @brief Get an element from a queue.
1924 *
1925 * This routine removes first data item from @a queue. The first 32 bits of the
1926 * data item are reserved for the kernel's use.
1927 *
1928 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1929 *
1930 * @param queue Address of the queue.
1931 * @param timeout Waiting period to obtain a data item (in milliseconds),
1932 * or one of the special values K_NO_WAIT and K_FOREVER.
1933 *
1934 * @return Address of the data item if successful; NULL if returned
1935 * without waiting, or waiting period timed out.
1936 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001937__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001938
1939/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001940 * @brief Remove an element from a queue.
1941 *
1942 * This routine removes data item from @a queue. The first 32 bits of the
1943 * data item are reserved for the kernel's use. Removing elements from k_queue
1944 * rely on sys_slist_find_and_remove which is not a constant time operation.
1945 *
1946 * @note Can be called by ISRs
1947 *
1948 * @param queue Address of the queue.
1949 * @param data Address of the data item.
1950 *
1951 * @return true if data item was removed
1952 */
1953static inline bool k_queue_remove(struct k_queue *queue, void *data)
1954{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001955 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001956}
1957
1958/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001959 * @brief Query a queue to see if it has data available.
1960 *
1961 * Note that the data might be already gone by the time this function returns
1962 * if other threads are also trying to read from the queue.
1963 *
1964 * @note Can be called by ISRs.
1965 *
1966 * @param queue Address of the queue.
1967 *
1968 * @return Non-zero if the queue is empty.
1969 * @return 0 if data is available.
1970 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001971__syscall int k_queue_is_empty(struct k_queue *queue);
1972
1973static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001974{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001975 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001976}
1977
1978/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001979 * @brief Peek element at the head of queue.
1980 *
1981 * Return element from the head of queue without removing it.
1982 *
1983 * @param queue Address of the queue.
1984 *
1985 * @return Head element, or NULL if queue is empty.
1986 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001987__syscall void *k_queue_peek_head(struct k_queue *queue);
1988
1989static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001990{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001991 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001992}
1993
1994/**
1995 * @brief Peek element at the tail of queue.
1996 *
1997 * Return element from the tail of queue without removing it.
1998 *
1999 * @param queue Address of the queue.
2000 *
2001 * @return Tail element, or NULL if queue is empty.
2002 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002003__syscall void *k_queue_peek_tail(struct k_queue *queue);
2004
2005static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002006{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002007 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002008}
2009
2010/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002011 * @brief Statically define and initialize a queue.
2012 *
2013 * The queue can be accessed outside the module where it is defined using:
2014 *
2015 * @code extern struct k_queue <name>; @endcode
2016 *
2017 * @param name Name of the queue.
2018 */
2019#define K_QUEUE_DEFINE(name) \
2020 struct k_queue name \
2021 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002022 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002023
Anas Nashif166f5192018-02-25 08:02:36 -06002024/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002025
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002026struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002027 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002028};
2029
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002030/**
2031 * @cond INTERNAL_HIDDEN
2032 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002033#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002034 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002035 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002036 }
2037
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002038#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
2039
Allan Stephensc98da842016-11-11 15:45:03 -05002040/**
2041 * INTERNAL_HIDDEN @endcond
2042 */
2043
2044/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002045 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002046 * @ingroup kernel_apis
2047 * @{
2048 */
2049
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002050/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002051 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002052 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002053 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002054 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002055 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002056 *
2057 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002058 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002059 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002060#define k_fifo_init(fifo) \
2061 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002062
2063/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002064 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002065 *
2066 * This routine causes first thread pending on @a fifo, if any, to
2067 * return from k_fifo_get() call with NULL value (as if timeout
2068 * expired).
2069 *
2070 * @note Can be called by ISRs.
2071 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002072 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002073 *
2074 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002075 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002076 */
2077#define k_fifo_cancel_wait(fifo) \
2078 k_queue_cancel_wait((struct k_queue *) fifo)
2079
2080/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002081 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002082 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002083 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002084 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2085 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002086 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002087 * @note Can be called by ISRs.
2088 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002089 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002090 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002091 *
2092 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002093 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002094 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002095#define k_fifo_put(fifo, data) \
2096 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002097
2098/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002099 * @brief Add an element to a FIFO queue.
2100 *
2101 * This routine adds a data item to @a fifo. There is an implicit
2102 * memory allocation from the calling thread's resource pool, which is
2103 * automatically freed when the item is removed.
2104 *
2105 * @note Can be called by ISRs.
2106 *
2107 * @param fifo Address of the FIFO.
2108 * @param data Address of the data item.
2109 *
2110 * @retval 0 on success
2111 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002112 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002113 */
2114#define k_fifo_alloc_put(fifo, data) \
2115 k_queue_alloc_append((struct k_queue *) fifo, data)
2116
2117/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002118 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002119 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002120 * This routine adds a list of data items to @a fifo in one operation.
2121 * The data items must be in a singly-linked list, with the first 32 bits
2122 * each data item pointing to the next data item; the list must be
2123 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002125 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002126 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002127 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002128 * @param head Pointer to first node in singly-linked list.
2129 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002130 *
2131 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002132 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002133 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002134#define k_fifo_put_list(fifo, head, tail) \
2135 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002136
2137/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002138 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002139 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002140 * This routine adds a list of data items to @a fifo in one operation.
2141 * The data items must be in a singly-linked list implemented using a
2142 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002143 * and must be re-initialized via sys_slist_init().
2144 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002145 * @note Can be called by ISRs.
2146 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002147 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002148 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002149 *
2150 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002151 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002152 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002153#define k_fifo_put_slist(fifo, list) \
2154 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002155
2156/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002157 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002158 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002159 * This routine removes a data item from @a fifo in a "first in, first out"
2160 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002161 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002162 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2163 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002164 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002165 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002166 * or one of the special values K_NO_WAIT and K_FOREVER.
2167 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002168 * @return Address of the data item if successful; NULL if returned
2169 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002170 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002171 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002172#define k_fifo_get(fifo, timeout) \
2173 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002174
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002175/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002176 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002177 *
2178 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002179 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002180 *
2181 * @note Can be called by ISRs.
2182 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002183 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002184 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002185 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002186 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002187 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002188 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002189#define k_fifo_is_empty(fifo) \
2190 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002191
2192/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002193 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002194 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002195 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302196 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002197 * on each iteration of processing, a head container will be peeked,
2198 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002199 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002200 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002201 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002202 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002203 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002204 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002205 */
2206#define k_fifo_peek_head(fifo) \
2207 k_queue_peek_head((struct k_queue *) fifo)
2208
2209/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002210 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002211 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002212 * Return element from the tail of FIFO queue (without removing it). A usecase
2213 * for this is if elements of the FIFO queue are themselves containers. Then
2214 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002215 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002216 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002217 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002218 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002219 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002220 */
2221#define k_fifo_peek_tail(fifo) \
2222 k_queue_peek_tail((struct k_queue *) fifo)
2223
2224/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002225 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002226 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002227 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002228 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002229 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002230 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002231 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002232 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002233 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002234#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002235 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002236 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002237 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002238
Anas Nashif166f5192018-02-25 08:02:36 -06002239/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002240
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002241struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002242 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002243};
2244
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002245/**
2246 * @cond INTERNAL_HIDDEN
2247 */
2248
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002249#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002250 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002251 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002252 }
2253
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002254#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2255
Allan Stephensc98da842016-11-11 15:45:03 -05002256/**
2257 * INTERNAL_HIDDEN @endcond
2258 */
2259
2260/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002261 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002262 * @ingroup kernel_apis
2263 * @{
2264 */
2265
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002266/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002267 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002268 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002269 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002270 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002271 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002272 *
2273 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002274 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002275 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002276#define k_lifo_init(lifo) \
2277 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002278
2279/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002280 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002281 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002282 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002283 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2284 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002285 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002286 * @note Can be called by ISRs.
2287 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002288 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002289 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002290 *
2291 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002292 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002293 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002294#define k_lifo_put(lifo, data) \
2295 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002296
2297/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002298 * @brief Add an element to a LIFO queue.
2299 *
2300 * This routine adds a data item to @a lifo. There is an implicit
2301 * memory allocation from the calling thread's resource pool, which is
2302 * automatically freed when the item is removed.
2303 *
2304 * @note Can be called by ISRs.
2305 *
2306 * @param lifo Address of the LIFO.
2307 * @param data Address of the data item.
2308 *
2309 * @retval 0 on success
2310 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002311 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002312 */
2313#define k_lifo_alloc_put(lifo, data) \
2314 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2315
2316/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002317 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002318 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002319 * This routine removes a data item from @a lifo in a "last in, first out"
2320 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002321 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002322 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2323 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002324 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002325 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002326 * or one of the special values K_NO_WAIT and K_FOREVER.
2327 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002328 * @return Address of the data item if successful; NULL if returned
2329 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002330 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002331 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002332#define k_lifo_get(lifo, timeout) \
2333 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002334
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002335/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002336 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002337 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002338 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002339 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002340 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002341 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002342 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002343 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002344 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002345#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002346 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002347 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002348 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002349
Anas Nashif166f5192018-02-25 08:02:36 -06002350/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002351
2352/**
2353 * @cond INTERNAL_HIDDEN
2354 */
Andrew Boief3bee952018-05-02 17:44:39 -07002355#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002356
2357struct k_stack {
2358 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002359 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002360
Anas Nashif2f203c22016-12-18 06:57:45 -05002361 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002362 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002363};
2364
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002365#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002366 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002367 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002368 .base = stack_buffer, \
2369 .next = stack_buffer, \
2370 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002371 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002372 }
2373
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002374#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2375
Allan Stephensc98da842016-11-11 15:45:03 -05002376/**
2377 * INTERNAL_HIDDEN @endcond
2378 */
2379
2380/**
2381 * @defgroup stack_apis Stack APIs
2382 * @ingroup kernel_apis
2383 * @{
2384 */
2385
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002386/**
2387 * @brief Initialize a stack.
2388 *
2389 * This routine initializes a stack object, prior to its first use.
2390 *
2391 * @param stack Address of the stack.
2392 * @param buffer Address of array used to hold stacked values.
2393 * @param num_entries Maximum number of values that can be stacked.
2394 *
2395 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002396 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002397 */
Andrew Boief3bee952018-05-02 17:44:39 -07002398void k_stack_init(struct k_stack *stack,
2399 u32_t *buffer, unsigned int num_entries);
2400
2401
2402/**
2403 * @brief Initialize a stack.
2404 *
2405 * This routine initializes a stack object, prior to its first use. Internal
2406 * buffers will be allocated from the calling thread's resource pool.
2407 * This memory will be released if k_stack_cleanup() is called, or
2408 * userspace is enabled and the stack object loses all references to it.
2409 *
2410 * @param stack Address of the stack.
2411 * @param num_entries Maximum number of values that can be stacked.
2412 *
2413 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002414 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002415 */
2416
2417__syscall int k_stack_alloc_init(struct k_stack *stack,
2418 unsigned int num_entries);
2419
2420/**
2421 * @brief Release a stack's allocated buffer
2422 *
2423 * If a stack object was given a dynamically allocated buffer via
2424 * k_stack_alloc_init(), this will free it. This function does nothing
2425 * if the buffer wasn't dynamically allocated.
2426 *
2427 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002428 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002429 */
2430void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002431
2432/**
2433 * @brief Push an element onto a stack.
2434 *
2435 * This routine adds a 32-bit value @a data to @a stack.
2436 *
2437 * @note Can be called by ISRs.
2438 *
2439 * @param stack Address of the stack.
2440 * @param data Value to push onto the stack.
2441 *
2442 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002443 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002444 */
Andrew Boiee8734462017-09-29 16:42:07 -07002445__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002446
2447/**
2448 * @brief Pop an element from a stack.
2449 *
2450 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2451 * manner and stores the value in @a data.
2452 *
2453 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2454 *
2455 * @param stack Address of the stack.
2456 * @param data Address of area to hold the value popped from the stack.
2457 * @param timeout Waiting period to obtain a value (in milliseconds),
2458 * or one of the special values K_NO_WAIT and K_FOREVER.
2459 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002460 * @retval 0 Element popped from stack.
2461 * @retval -EBUSY Returned without waiting.
2462 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002463 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002464 */
Andrew Boiee8734462017-09-29 16:42:07 -07002465__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002466
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002467/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002468 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002469 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002470 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002471 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002472 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002474 * @param name Name of the stack.
2475 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002476 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002477 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002478#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002479 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002480 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002481 struct k_stack name \
2482 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002483 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002484 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002485
Anas Nashif166f5192018-02-25 08:02:36 -06002486/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002487
Allan Stephens6bba9b02016-11-16 14:56:54 -05002488struct k_work;
2489
Allan Stephensc98da842016-11-11 15:45:03 -05002490/**
2491 * @defgroup workqueue_apis Workqueue Thread APIs
2492 * @ingroup kernel_apis
2493 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002494 */
2495
Allan Stephens6bba9b02016-11-16 14:56:54 -05002496/**
2497 * @typedef k_work_handler_t
2498 * @brief Work item handler function type.
2499 *
2500 * A work item's handler function is executed by a workqueue's thread
2501 * when the work item is processed by the workqueue.
2502 *
2503 * @param work Address of the work item.
2504 *
2505 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002506 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002507 */
2508typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002509
2510/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002511 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002512 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002513
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002514struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002515 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002516 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002517};
2518
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002519enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002520 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002521};
2522
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002523struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002524 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002525 k_work_handler_t handler;
2526 atomic_t flags[1];
2527};
2528
Allan Stephens6bba9b02016-11-16 14:56:54 -05002529struct k_delayed_work {
2530 struct k_work work;
2531 struct _timeout timeout;
2532 struct k_work_q *work_q;
2533};
2534
2535extern struct k_work_q k_sys_work_q;
2536
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002537/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002538 * INTERNAL_HIDDEN @endcond
2539 */
2540
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002541#define _K_WORK_INITIALIZER(work_handler) \
2542 { \
2543 ._reserved = NULL, \
2544 .handler = work_handler, \
2545 .flags = { 0 } \
2546 }
2547
2548#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2549
Allan Stephens6bba9b02016-11-16 14:56:54 -05002550/**
2551 * @brief Initialize a statically-defined work item.
2552 *
2553 * This macro can be used to initialize a statically-defined workqueue work
2554 * item, prior to its first use. For example,
2555 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002556 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002557 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002558 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002559 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002560 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002561 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002562#define K_WORK_DEFINE(work, work_handler) \
2563 struct k_work work \
2564 __in_section(_k_work, static, work) = \
2565 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002566
2567/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002568 * @brief Initialize a work item.
2569 *
2570 * This routine initializes a workqueue work item, prior to its first use.
2571 *
2572 * @param work Address of work item.
2573 * @param handler Function to invoke each time work item is processed.
2574 *
2575 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002576 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002577 */
2578static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2579{
Leandro Pereira0e23ad82018-05-29 10:42:17 -07002580 *work = (struct k_work)_K_WORK_INITIALIZER(handler);
Andrew Boie945af952017-08-22 13:15:23 -07002581 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002582}
2583
2584/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002585 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002586 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002587 * This routine submits work item @a work to be processed by workqueue
2588 * @a work_q. If the work item is already pending in the workqueue's queue
2589 * as a result of an earlier submission, this routine has no effect on the
2590 * work item. If the work item has already been processed, or is currently
2591 * being processed, its work is considered complete and the work item can be
2592 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002593 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002594 * @warning
2595 * A submitted work item must not be modified until it has been processed
2596 * by the workqueue.
2597 *
2598 * @note Can be called by ISRs.
2599 *
2600 * @param work_q Address of workqueue.
2601 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002602 *
2603 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002604 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002605 */
2606static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2607 struct k_work *work)
2608{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002609 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002610 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002611 }
2612}
2613
2614/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002615 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002616 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002617 * This routine indicates if work item @a work is pending in a workqueue's
2618 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002619 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002620 * @note Can be called by ISRs.
2621 *
2622 * @param work Address of work item.
2623 *
2624 * @return 1 if work item is pending, or 0 if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002625 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002626 */
2627static inline int k_work_pending(struct k_work *work)
2628{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002629 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002630}
2631
2632/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002633 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002634 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002635 * This routine starts workqueue @a work_q. The workqueue spawns its work
2636 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002637 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002638 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002639 * @param stack Pointer to work queue thread's stack space, as defined by
2640 * K_THREAD_STACK_DEFINE()
2641 * @param stack_size Size of the work queue thread's stack (in bytes), which
2642 * should either be the same constant passed to
2643 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002644 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002645 *
2646 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002647 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002648 */
Andrew Boie507852a2017-07-25 18:47:07 -07002649extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002650 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002651 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002652
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002653/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002654 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002655 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002656 * This routine initializes a workqueue delayed work item, prior to
2657 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002658 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002659 * @param work Address of delayed work item.
2660 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002661 *
2662 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002663 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002664 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002665extern void k_delayed_work_init(struct k_delayed_work *work,
2666 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002667
2668/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002669 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002670 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002671 * This routine schedules work item @a work to be processed by workqueue
2672 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002673 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002674 * Only when the countdown completes is the work item actually submitted to
2675 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002676 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002677 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002678 * counting down cancels the existing submission and restarts the
2679 * countdown using the new delay. Note that this behavior is
2680 * inherently subject to race conditions with the pre-existing
2681 * timeouts and work queue, so care must be taken to synchronize such
2682 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002683 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002684 * @warning
2685 * A delayed work item must not be modified until it has been processed
2686 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002687 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002688 * @note Can be called by ISRs.
2689 *
2690 * @param work_q Address of workqueue.
2691 * @param work Address of delayed work item.
2692 * @param delay Delay before submitting the work item (in milliseconds).
2693 *
2694 * @retval 0 Work item countdown started.
2695 * @retval -EINPROGRESS Work item is already pending.
2696 * @retval -EINVAL Work item is being processed or has completed its work.
2697 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002698 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002699 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002700extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2701 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002702 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002703
2704/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002705 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002706 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002707 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002708 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002709 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002710 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002711 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002712 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002713 * @param work Address of delayed work item.
2714 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002715 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002716 * @retval -EINPROGRESS Work item is already pending.
2717 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002718 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002719 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002720extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002721
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002722/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002723 * @brief Submit a work item to the system workqueue.
2724 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002725 * This routine submits work item @a work to be processed by the system
2726 * workqueue. If the work item is already pending in the workqueue's queue
2727 * as a result of an earlier submission, this routine has no effect on the
2728 * work item. If the work item has already been processed, or is currently
2729 * being processed, its work is considered complete and the work item can be
2730 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002731 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002732 * @warning
2733 * Work items submitted to the system workqueue should avoid using handlers
2734 * that block or yield since this may prevent the system workqueue from
2735 * processing other work items in a timely manner.
2736 *
2737 * @note Can be called by ISRs.
2738 *
2739 * @param work Address of work item.
2740 *
2741 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002742 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002743 */
2744static inline void k_work_submit(struct k_work *work)
2745{
2746 k_work_submit_to_queue(&k_sys_work_q, work);
2747}
2748
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002749/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002750 * @brief Submit a delayed work item to the system workqueue.
2751 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002752 * This routine schedules work item @a work to be processed by the system
2753 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002754 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002755 * Only when the countdown completes is the work item actually submitted to
2756 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002757 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002758 * Submitting a previously submitted delayed work item that is still
2759 * counting down cancels the existing submission and restarts the countdown
2760 * using the new delay. If the work item is currently pending on the
2761 * workqueue's queue because the countdown has completed it is too late to
2762 * resubmit the item, and resubmission fails without impacting the work item.
2763 * If the work item has already been processed, or is currently being processed,
2764 * its work is considered complete and the work item can be resubmitted.
2765 *
2766 * @warning
2767 * Work items submitted to the system workqueue should avoid using handlers
2768 * that block or yield since this may prevent the system workqueue from
2769 * processing other work items in a timely manner.
2770 *
2771 * @note Can be called by ISRs.
2772 *
2773 * @param work Address of delayed work item.
2774 * @param delay Delay before submitting the work item (in milliseconds).
2775 *
2776 * @retval 0 Work item countdown started.
2777 * @retval -EINPROGRESS Work item is already pending.
2778 * @retval -EINVAL Work item is being processed or has completed its work.
2779 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002780 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002781 */
2782static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002783 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002784{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002785 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002786}
2787
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002788/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002789 * @brief Get time remaining before a delayed work gets scheduled.
2790 *
2791 * This routine computes the (approximate) time remaining before a
2792 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002793 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002794 *
2795 * @param work Delayed work item.
2796 *
2797 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002798 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002799 */
Kumar Galacc334c72017-04-21 10:55:34 -05002800static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002801{
2802 return _timeout_remaining_get(&work->timeout);
2803}
2804
Anas Nashif166f5192018-02-25 08:02:36 -06002805/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002806/**
Anas Nashifce78d162018-05-24 12:43:11 -05002807 * @defgroup mutex_apis Mutex APIs
2808 * @ingroup kernel_apis
2809 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002810 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002811
Anas Nashifce78d162018-05-24 12:43:11 -05002812/**
2813 * Mutex Structure
2814 * @ingroup mutex_apis
2815 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002816struct k_mutex {
2817 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002818 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002819 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002820 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002821 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002822
Anas Nashif2f203c22016-12-18 06:57:45 -05002823 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002824};
2825
Anas Nashifce78d162018-05-24 12:43:11 -05002826/**
2827 * @cond INTERNAL_HIDDEN
2828 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002829#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002830 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002831 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002832 .owner = NULL, \
2833 .lock_count = 0, \
2834 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002835 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002836 }
2837
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002838#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2839
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002840/**
Allan Stephensc98da842016-11-11 15:45:03 -05002841 * INTERNAL_HIDDEN @endcond
2842 */
2843
2844/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002845 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002847 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002848 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002849 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002850 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002851 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002852 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002853 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002854#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002855 struct k_mutex name \
2856 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002857 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002858
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002859/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002860 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002862 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002864 * Upon completion, the mutex is available and does not have an owner.
2865 *
2866 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002867 *
2868 * @return N/A
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 void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002872
2873/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002874 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002876 * This routine locks @a mutex. If the mutex is locked by another thread,
2877 * the calling thread waits until the mutex becomes available or until
2878 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002880 * A thread is permitted to lock a mutex it has already locked. The operation
2881 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002883 * @param mutex Address of the mutex.
2884 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002885 * or one of the special values K_NO_WAIT and K_FOREVER.
2886 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002887 * @retval 0 Mutex locked.
2888 * @retval -EBUSY Returned without waiting.
2889 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002890 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002891 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002892__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002893
2894/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002895 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002897 * This routine unlocks @a mutex. The mutex must already be locked by the
2898 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002899 *
2900 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002901 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002902 * thread.
2903 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002904 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002905 *
2906 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002907 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002908 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002909__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002910
Allan Stephensc98da842016-11-11 15:45:03 -05002911/**
Anas Nashif166f5192018-02-25 08:02:36 -06002912 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002913 */
2914
2915/**
2916 * @cond INTERNAL_HIDDEN
2917 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002918
2919struct k_sem {
2920 _wait_q_t wait_q;
2921 unsigned int count;
2922 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002923 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002924
Anas Nashif2f203c22016-12-18 06:57:45 -05002925 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002926};
2927
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002928#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002929 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002930 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002931 .count = initial_count, \
2932 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002933 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002934 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002935 }
2936
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002937#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2938
Allan Stephensc98da842016-11-11 15:45:03 -05002939/**
2940 * INTERNAL_HIDDEN @endcond
2941 */
2942
2943/**
2944 * @defgroup semaphore_apis Semaphore APIs
2945 * @ingroup kernel_apis
2946 * @{
2947 */
2948
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002949/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002950 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002952 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002954 * @param sem Address of the semaphore.
2955 * @param initial_count Initial semaphore count.
2956 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002957 *
2958 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002959 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002960 */
Andrew Boie99280232017-09-29 14:17:47 -07002961__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2962 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002963
2964/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002965 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002966 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002967 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002969 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2970 *
2971 * @param sem Address of the semaphore.
2972 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002973 * or one of the special values K_NO_WAIT and K_FOREVER.
2974 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002975 * @note When porting code from the nanokernel legacy API to the new API, be
2976 * careful with the return value of this function. The return value is the
2977 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2978 * non-zero means failure, while the nano_sem_take family returns 1 for success
2979 * and 0 for failure.
2980 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002981 * @retval 0 Semaphore taken.
2982 * @retval -EBUSY Returned without waiting.
2983 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002984 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002985 */
Andrew Boie99280232017-09-29 14:17:47 -07002986__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002987
2988/**
2989 * @brief Give a semaphore.
2990 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002991 * This routine gives @a sem, unless the semaphore is already at its maximum
2992 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002994 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002996 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002997 *
2998 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002999 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003000 */
Andrew Boie99280232017-09-29 14:17:47 -07003001__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003003/**
3004 * @brief Reset a semaphore's count to zero.
3005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003006 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003008 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003009 *
3010 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003011 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003012 */
Andrew Boie990bf162017-10-03 12:36:49 -07003013__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003014
Anas Nashif954d5502018-02-25 08:37:28 -06003015/**
3016 * @internal
3017 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003018static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003019{
3020 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003021}
3022
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003023/**
3024 * @brief Get a semaphore's count.
3025 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003026 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003028 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003029 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003030 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003031 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003032 */
Andrew Boie990bf162017-10-03 12:36:49 -07003033__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003034
Anas Nashif954d5502018-02-25 08:37:28 -06003035/**
3036 * @internal
3037 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003038static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003039{
3040 return sem->count;
3041}
3042
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003043/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003044 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003045 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003046 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003047 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003048 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003049 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003050 * @param name Name of the semaphore.
3051 * @param initial_count Initial semaphore count.
3052 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003053 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003054 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003055#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003056 struct k_sem name \
3057 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003058 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303059 BUILD_ASSERT(((count_limit) != 0) && \
3060 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003061
Anas Nashif166f5192018-02-25 08:02:36 -06003062/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003063
3064/**
3065 * @defgroup alert_apis Alert APIs
3066 * @ingroup kernel_apis
3067 * @{
3068 */
3069
Allan Stephens5eceb852016-11-16 10:16:30 -05003070/**
3071 * @typedef k_alert_handler_t
3072 * @brief Alert handler function type.
3073 *
3074 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07003075 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05003076 * and is only invoked if the alert has been initialized with one.
3077 *
3078 * @param alert Address of the alert.
3079 *
3080 * @return 0 if alert has been consumed; non-zero if alert should pend.
3081 */
3082typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05003083
Anas Nashif166f5192018-02-25 08:02:36 -06003084/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003085
3086/**
3087 * @cond INTERNAL_HIDDEN
3088 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003089
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003090#define K_ALERT_DEFAULT NULL
3091#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003092
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003093struct k_alert {
3094 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003095 atomic_t send_count;
3096 struct k_work work_item;
3097 struct k_sem sem;
3098
Anas Nashif2f203c22016-12-18 06:57:45 -05003099 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003100};
3101
Anas Nashif954d5502018-02-25 08:37:28 -06003102/**
3103 * @internal
3104 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003105extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003106
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003107#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003108 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003109 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003110 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003111 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
3112 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003113 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003114 }
3115
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003116#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
3117
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003118/**
Allan Stephensc98da842016-11-11 15:45:03 -05003119 * INTERNAL_HIDDEN @endcond
3120 */
3121
3122/**
3123 * @addtogroup alert_apis
3124 * @{
3125 */
3126
3127/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003128 * @def K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts)
3129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003130 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003131 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003132 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003133 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003134 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003135 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003136 * @param name Name of the alert.
3137 * @param alert_handler Action to take when alert is sent. Specify either
3138 * the address of a function to be invoked by the system workqueue
3139 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
3140 * K_ALERT_DEFAULT (which causes the alert to pend).
3141 * @param max_num_pending_alerts Maximum number of pending alerts.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003142 *
3143 * @req K-ALERT-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003144 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003145#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003146 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003147 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003148 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003149 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003150
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003151/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003152 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003153 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003154 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003155 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003156 * @param alert Address of the alert.
3157 * @param handler Action to take when alert is sent. Specify either the address
3158 * of a function to be invoked by the system workqueue thread,
3159 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3160 * K_ALERT_DEFAULT (which causes the alert to pend).
3161 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003162 *
3163 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003164 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003165 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003166extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3167 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003168
3169/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003170 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003171 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003172 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003174 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3175 *
3176 * @param alert Address of the alert.
3177 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003178 * or one of the special values K_NO_WAIT and K_FOREVER.
3179 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003180 * @retval 0 Alert received.
3181 * @retval -EBUSY Returned without waiting.
3182 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003183 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003184 */
Andrew Boie310e9872017-09-29 04:41:15 -07003185__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003186
3187/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003188 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003189 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003190 * This routine signals @a alert. The action specified for @a alert will
3191 * be taken, which may trigger the execution of an alert handler function
3192 * and/or cause the alert to pend (assuming the alert has not reached its
3193 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003195 * @note Can be called by ISRs.
3196 *
3197 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003198 *
3199 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003200 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003201 */
Andrew Boie310e9872017-09-29 04:41:15 -07003202__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003203
3204/**
Anas Nashif166f5192018-02-25 08:02:36 -06003205 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003206 */
3207
Allan Stephensc98da842016-11-11 15:45:03 -05003208/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003209 * @defgroup msgq_apis Message Queue APIs
3210 * @ingroup kernel_apis
3211 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003212 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003213
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003214/**
3215 * @brief Message Queue Structure
3216 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003217struct k_msgq {
3218 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003219 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003220 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003221 char *buffer_start;
3222 char *buffer_end;
3223 char *read_ptr;
3224 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003225 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003226
Anas Nashif2f203c22016-12-18 06:57:45 -05003227 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003228 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003229};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003230/**
3231 * @cond INTERNAL_HIDDEN
3232 */
3233
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003234
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003235#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003236 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003237 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003238 .max_msgs = q_max_msgs, \
3239 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003240 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003241 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003242 .read_ptr = q_buffer, \
3243 .write_ptr = q_buffer, \
3244 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003245 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003246 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003247#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003248/**
3249 * INTERNAL_HIDDEN @endcond
3250 */
3251
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003252
Andrew Boie0fe789f2018-04-12 18:35:56 -07003253#define K_MSGQ_FLAG_ALLOC BIT(0)
3254
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003255/**
3256 * @brief Message Queue Attributes
3257 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303258struct k_msgq_attrs {
3259 size_t msg_size;
3260 u32_t max_msgs;
3261 u32_t used_msgs;
3262};
3263
Allan Stephensc98da842016-11-11 15:45:03 -05003264
3265/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003266 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003267 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003268 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3269 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003270 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3271 * message is similarly aligned to this boundary, @a q_msg_size must also be
3272 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003273 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003274 * The message queue can be accessed outside the module where it is defined
3275 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003276 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003277 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003279 * @param q_name Name of the message queue.
3280 * @param q_msg_size Message size (in bytes).
3281 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003282 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003283 *
3284 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003285 */
3286#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003287 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003288 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003289 struct k_msgq q_name \
3290 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003291 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003292 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003293
Peter Mitsisd7a37502016-10-13 11:37:40 -04003294/**
3295 * @brief Initialize a message queue.
3296 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003297 * This routine initializes a message queue object, prior to its first use.
3298 *
Allan Stephensda827222016-11-09 14:23:58 -06003299 * The message queue's ring buffer must contain space for @a max_msgs messages,
3300 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3301 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3302 * that each message is similarly aligned to this boundary, @a q_msg_size
3303 * must also be a multiple of N.
3304 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003305 * @param q Address of the message queue.
3306 * @param buffer Pointer to ring buffer that holds queued messages.
3307 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003308 * @param max_msgs Maximum number of messages that can be queued.
3309 *
3310 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003311 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003312 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003313void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3314 u32_t max_msgs);
3315
3316/**
3317 * @brief Initialize a message queue.
3318 *
3319 * This routine initializes a message queue object, prior to its first use,
3320 * allocating its internal ring buffer from the calling thread's resource
3321 * pool.
3322 *
3323 * Memory allocated for the ring buffer can be released by calling
3324 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3325 * all of its references.
3326 *
3327 * @param q Address of the message queue.
3328 * @param msg_size Message size (in bytes).
3329 * @param max_msgs Maximum number of messages that can be queued.
3330 *
3331 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3332 * thread's resource pool, or -EINVAL if the size parameters cause
3333 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003334 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003335 */
3336__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3337 u32_t max_msgs);
3338
3339
3340void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003341
3342/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003343 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003345 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003346 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003347 * @note Can be called by ISRs.
3348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003349 * @param q Address of the message queue.
3350 * @param data Pointer to the message.
3351 * @param timeout Waiting period to add the message (in milliseconds),
3352 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003353 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003354 * @retval 0 Message sent.
3355 * @retval -ENOMSG Returned without waiting or queue purged.
3356 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003357 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003358 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003359__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003360
3361/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003362 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003363 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003364 * This routine receives a message from message queue @a q in a "first in,
3365 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003366 *
Allan Stephensc98da842016-11-11 15:45:03 -05003367 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003368 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003369 * @param q Address of the message queue.
3370 * @param data Address of area to hold the received message.
3371 * @param timeout Waiting period to receive the message (in milliseconds),
3372 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003373 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003374 * @retval 0 Message received.
3375 * @retval -ENOMSG Returned without waiting.
3376 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003377 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003378 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003379__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003380
3381/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003382 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003383 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003384 * This routine discards all unreceived messages in a message queue's ring
3385 * buffer. Any threads that are blocked waiting to send a message to the
3386 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003388 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003389 *
3390 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003391 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003392 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003393__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003394
Peter Mitsis67be2492016-10-07 11:44:34 -04003395/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003396 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003397 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003398 * This routine returns the number of unused entries in a message queue's
3399 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003400 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003401 * @param q Address of the message queue.
3402 *
3403 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003404 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003405 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003406__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3407
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303408/**
3409 * @brief Get basic attributes of a message queue.
3410 *
3411 * This routine fetches basic attributes of message queue into attr argument.
3412 *
3413 * @param q Address of the message queue.
3414 * @param attrs pointer to message queue attribute structure.
3415 *
3416 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003417 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303418 */
3419__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3420
3421
Andrew Boie82edb6e2017-10-02 10:53:06 -07003422static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003423{
3424 return q->max_msgs - q->used_msgs;
3425}
3426
Peter Mitsisd7a37502016-10-13 11:37:40 -04003427/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003428 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003429 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003430 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003431 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003432 * @param q Address of the message queue.
3433 *
3434 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003435 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003436 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003437__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3438
3439static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003440{
3441 return q->used_msgs;
3442}
3443
Anas Nashif166f5192018-02-25 08:02:36 -06003444/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003445
3446/**
3447 * @defgroup mem_pool_apis Memory Pool APIs
3448 * @ingroup kernel_apis
3449 * @{
3450 */
3451
Andy Ross73cb9582017-05-09 10:42:39 -07003452/* Note on sizing: the use of a 20 bit field for block means that,
3453 * assuming a reasonable minimum block size of 16 bytes, we're limited
3454 * to 16M of memory managed by a single pool. Long term it would be
3455 * good to move to a variable bit size based on configuration.
3456 */
3457struct k_mem_block_id {
3458 u32_t pool : 8;
3459 u32_t level : 4;
3460 u32_t block : 20;
3461};
3462
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003463struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003464 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003465 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003466};
3467
Anas Nashif166f5192018-02-25 08:02:36 -06003468/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003469
3470/**
3471 * @defgroup mailbox_apis Mailbox APIs
3472 * @ingroup kernel_apis
3473 * @{
3474 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003475
3476struct k_mbox_msg {
3477 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003478 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003479 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003480 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003481 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003482 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003483 /** sender's message data buffer */
3484 void *tx_data;
3485 /** internal use only - needed for legacy API support */
3486 void *_rx_data;
3487 /** message data block descriptor */
3488 struct k_mem_block tx_block;
3489 /** source thread id */
3490 k_tid_t rx_source_thread;
3491 /** target thread id */
3492 k_tid_t tx_target_thread;
3493 /** internal use only - thread waiting on send (may be a dummy) */
3494 k_tid_t _syncing_thread;
3495#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3496 /** internal use only - semaphore used during asynchronous send */
3497 struct k_sem *_async_sem;
3498#endif
3499};
3500
3501struct k_mbox {
3502 _wait_q_t tx_msg_queue;
3503 _wait_q_t rx_msg_queue;
3504
Anas Nashif2f203c22016-12-18 06:57:45 -05003505 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003506};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003507/**
3508 * @cond INTERNAL_HIDDEN
3509 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003510
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003511#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003512 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003513 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3514 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003515 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003516 }
3517
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003518#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3519
Peter Mitsis12092702016-10-14 12:57:23 -04003520/**
Allan Stephensc98da842016-11-11 15:45:03 -05003521 * INTERNAL_HIDDEN @endcond
3522 */
3523
3524/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003525 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003526 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003527 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003528 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003529 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003530 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003531 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003532 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003533 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003534#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003535 struct k_mbox name \
3536 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003537 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003538
Peter Mitsis12092702016-10-14 12:57:23 -04003539/**
3540 * @brief Initialize a mailbox.
3541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003542 * This routine initializes a mailbox object, prior to its first use.
3543 *
3544 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003545 *
3546 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003547 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003548 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003549extern void k_mbox_init(struct k_mbox *mbox);
3550
Peter Mitsis12092702016-10-14 12:57:23 -04003551/**
3552 * @brief Send a mailbox message in a synchronous manner.
3553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003554 * This routine sends a message to @a mbox and waits for a receiver to both
3555 * receive and process it. The message data may be in a buffer, in a memory
3556 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003558 * @param mbox Address of the mailbox.
3559 * @param tx_msg Address of the transmit message descriptor.
3560 * @param timeout Waiting period for the message to be received (in
3561 * milliseconds), or one of the special values K_NO_WAIT
3562 * and K_FOREVER. Once the message has been received,
3563 * this routine waits as long as necessary for the message
3564 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003565 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003566 * @retval 0 Message sent.
3567 * @retval -ENOMSG Returned without waiting.
3568 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003569 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003570 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003571extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003572 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003573
Peter Mitsis12092702016-10-14 12:57:23 -04003574/**
3575 * @brief Send a mailbox message in an asynchronous manner.
3576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003577 * This routine sends a message to @a mbox without waiting for a receiver
3578 * to process it. The message data may be in a buffer, in a memory pool block,
3579 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3580 * will be given when the message has been both received and completely
3581 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003583 * @param mbox Address of the mailbox.
3584 * @param tx_msg Address of the transmit message descriptor.
3585 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003586 *
3587 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003588 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003589 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003590extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003591 struct k_sem *sem);
3592
Peter Mitsis12092702016-10-14 12:57:23 -04003593/**
3594 * @brief Receive a mailbox message.
3595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003596 * This routine receives a message from @a mbox, then optionally retrieves
3597 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003598 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003599 * @param mbox Address of the mailbox.
3600 * @param rx_msg Address of the receive message descriptor.
3601 * @param buffer Address of the buffer to receive data, or NULL to defer data
3602 * retrieval and message disposal until later.
3603 * @param timeout Waiting period for a message to be received (in
3604 * milliseconds), or one of the special values K_NO_WAIT
3605 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003606 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003607 * @retval 0 Message received.
3608 * @retval -ENOMSG Returned without waiting.
3609 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003610 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003611 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003612extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003613 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003614
3615/**
3616 * @brief Retrieve mailbox message data into a buffer.
3617 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003618 * This routine completes the processing of a received message by retrieving
3619 * its data into a buffer, then disposing of the message.
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.
3623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003624 * @param rx_msg Address of the receive message descriptor.
3625 * @param buffer Address of the buffer to receive data, or NULL to discard
3626 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003627 *
3628 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003629 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003630 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003631extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003632
3633/**
3634 * @brief Retrieve mailbox message data into a memory pool block.
3635 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003636 * This routine completes the processing of a received message by retrieving
3637 * its data into a memory pool block, then disposing of the message.
3638 * The memory pool block that results from successful retrieval must be
3639 * returned to the pool once the data has been processed, even in cases
3640 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003641 *
3642 * Alternatively, this routine can be used to dispose of a received message
3643 * without retrieving its data. In this case there is no need to return a
3644 * memory pool block to the pool.
3645 *
3646 * This routine allocates a new memory pool block for the data only if the
3647 * data is not already in one. If a new block cannot be allocated, the routine
3648 * returns a failure code and the received message is left unchanged. This
3649 * permits the caller to reattempt data retrieval at a later time or to dispose
3650 * of the received message without retrieving its data.
3651 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003652 * @param rx_msg Address of a receive message descriptor.
3653 * @param pool Address of memory pool, or NULL to discard data.
3654 * @param block Address of the area to hold memory pool block info.
3655 * @param timeout Waiting period to wait for a memory pool block (in
3656 * milliseconds), or one of the special values K_NO_WAIT
3657 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003658 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003659 * @retval 0 Data retrieved.
3660 * @retval -ENOMEM Returned without waiting.
3661 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003662 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003663 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003664extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003665 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003666 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003667
Anas Nashif166f5192018-02-25 08:02:36 -06003668/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003669
3670/**
Anas Nashifce78d162018-05-24 12:43:11 -05003671 * @defgroup pipe_apis Pipe APIs
3672 * @ingroup kernel_apis
3673 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003674 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003675
Anas Nashifce78d162018-05-24 12:43:11 -05003676/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003677struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003678 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3679 size_t size; /**< Buffer size */
3680 size_t bytes_used; /**< # bytes used in buffer */
3681 size_t read_index; /**< Where in buffer to read from */
3682 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003683
3684 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003685 _wait_q_t readers; /**< Reader wait queue */
3686 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003687 } wait_q;
3688
Anas Nashif2f203c22016-12-18 06:57:45 -05003689 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Anas Nashifce78d162018-05-24 12:43:11 -05003690 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003691};
3692
Anas Nashifce78d162018-05-24 12:43:11 -05003693/**
3694 * @cond INTERNAL_HIDDEN
3695 */
3696#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3697
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003698#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003699 { \
3700 .buffer = pipe_buffer, \
3701 .size = pipe_buffer_size, \
3702 .bytes_used = 0, \
3703 .read_index = 0, \
3704 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003705 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3706 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003707 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003708 }
3709
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003710#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3711
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003712/**
Allan Stephensc98da842016-11-11 15:45:03 -05003713 * INTERNAL_HIDDEN @endcond
3714 */
3715
3716/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003717 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003718 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003719 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003720 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003721 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003722 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003723 * @param name Name of the pipe.
3724 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3725 * or zero if no ring buffer is used.
3726 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003727 *
3728 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003729 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003730#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3731 static unsigned char __kernel_noinit __aligned(pipe_align) \
3732 _k_pipe_buf_##name[pipe_buffer_size]; \
3733 struct k_pipe name \
3734 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003735 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003736
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003737/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003738 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003739 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003740 * This routine initializes a pipe object, prior to its first use.
3741 *
3742 * @param pipe Address of the pipe.
3743 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3744 * is used.
3745 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3746 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003747 *
3748 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003749 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003750 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003751void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3752
3753/**
3754 * @brief Release a pipe's allocated buffer
3755 *
3756 * If a pipe object was given a dynamically allocated buffer via
3757 * k_pipe_alloc_init(), this will free it. This function does nothing
3758 * if the buffer wasn't dynamically allocated.
3759 *
3760 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003761 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003762 */
3763void k_pipe_cleanup(struct k_pipe *pipe);
3764
3765/**
3766 * @brief Initialize a pipe and allocate a buffer for it
3767 *
3768 * Storage for the buffer region will be allocated from the calling thread's
3769 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3770 * or userspace is enabled and the pipe object loses all references to it.
3771 *
3772 * This function should only be called on uninitialized pipe objects.
3773 *
3774 * @param pipe Address of the pipe.
3775 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3776 * buffer is used.
3777 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003778 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003779 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003780 */
3781__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003782
3783/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003784 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003785 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003786 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003788 * @param pipe Address of the pipe.
3789 * @param data Address of data to write.
3790 * @param bytes_to_write Size of data (in bytes).
3791 * @param bytes_written Address of area to hold the number of bytes written.
3792 * @param min_xfer Minimum number of bytes to write.
3793 * @param timeout Waiting period to wait for the data to be written (in
3794 * milliseconds), or one of the special values K_NO_WAIT
3795 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003796 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003797 * @retval 0 At least @a min_xfer bytes of data were written.
3798 * @retval -EIO Returned without waiting; zero data bytes were written.
3799 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003800 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003801 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003802 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003803__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3804 size_t bytes_to_write, size_t *bytes_written,
3805 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003806
3807/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003808 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003809 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003810 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003812 * @param pipe Address of the pipe.
3813 * @param data Address to place the data read from pipe.
3814 * @param bytes_to_read Maximum number of data bytes to read.
3815 * @param bytes_read Address of area to hold the number of bytes read.
3816 * @param min_xfer Minimum number of data bytes to read.
3817 * @param timeout Waiting period to wait for the data to be read (in
3818 * milliseconds), or one of the special values K_NO_WAIT
3819 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003820 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003821 * @retval 0 At least @a min_xfer bytes of data were read.
3822 * @retval -EIO Returned without waiting; zero data bytes were read.
3823 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003824 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003825 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003826 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003827__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3828 size_t bytes_to_read, size_t *bytes_read,
3829 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003830
3831/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003832 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003833 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003834 * This routine writes the data contained in a memory block to @a pipe.
3835 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003836 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003838 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003839 * @param block Memory block containing data to send
3840 * @param size Number of data bytes in memory block to send
3841 * @param sem Semaphore to signal upon completion (else NULL)
3842 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003843 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003844 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003845 */
3846extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3847 size_t size, struct k_sem *sem);
3848
Anas Nashif166f5192018-02-25 08:02:36 -06003849/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003850
Allan Stephensc98da842016-11-11 15:45:03 -05003851/**
3852 * @cond INTERNAL_HIDDEN
3853 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003854
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003855struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003856 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003857 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003858 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003859 char *buffer;
3860 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003861 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003862
Anas Nashif2f203c22016-12-18 06:57:45 -05003863 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003864};
3865
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003866#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003867 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003868 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003869 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003870 .num_blocks = slab_num_blocks, \
3871 .block_size = slab_block_size, \
3872 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003873 .free_list = NULL, \
3874 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003875 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003876 }
3877
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003878#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3879
3880
Peter Mitsis578f9112016-10-07 13:50:31 -04003881/**
Allan Stephensc98da842016-11-11 15:45:03 -05003882 * INTERNAL_HIDDEN @endcond
3883 */
3884
3885/**
3886 * @defgroup mem_slab_apis Memory Slab APIs
3887 * @ingroup kernel_apis
3888 * @{
3889 */
3890
3891/**
Allan Stephensda827222016-11-09 14:23:58 -06003892 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003893 *
Allan Stephensda827222016-11-09 14:23:58 -06003894 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003895 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003896 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3897 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003898 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003899 *
Allan Stephensda827222016-11-09 14:23:58 -06003900 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003901 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003902 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003903 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003905 * @param name Name of the memory slab.
3906 * @param slab_block_size Size of each memory block (in bytes).
3907 * @param slab_num_blocks Number memory blocks.
3908 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003909 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003910 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003911#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3912 char __noinit __aligned(slab_align) \
3913 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3914 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003915 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003916 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003917 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003918
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003919/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003920 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003921 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003922 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003923 *
Allan Stephensda827222016-11-09 14:23:58 -06003924 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3925 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3926 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3927 * To ensure that each memory block is similarly aligned to this boundary,
3928 * @a slab_block_size must also be a multiple of N.
3929 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003930 * @param slab Address of the memory slab.
3931 * @param buffer Pointer to buffer used for the memory blocks.
3932 * @param block_size Size of each memory block (in bytes).
3933 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003934 *
3935 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003936 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003937 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003938extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003939 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003940
3941/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003942 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003943 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003944 * This routine allocates a memory block from a 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.
3948 * @param timeout Maximum time to wait for operation to complete
3949 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3950 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003951 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003952 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003953 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003954 * @retval -ENOMEM Returned without waiting.
3955 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003956 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003957 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003958extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003959 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003960
3961/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003962 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003963 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003964 * This routine releases a previously allocated memory block back to its
3965 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003966 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003967 * @param slab Address of the memory slab.
3968 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003969 *
3970 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003971 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003972 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003973extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003974
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003975/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003976 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003977 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003978 * This routine gets the number of memory blocks that are currently
3979 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003980 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003981 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003983 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003984 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003985 */
Kumar Galacc334c72017-04-21 10:55:34 -05003986static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003987{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003988 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003989}
3990
Peter Mitsisc001aa82016-10-13 13:53:37 -04003991/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003992 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003994 * This routine gets the number of memory blocks that are currently
3995 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003996 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003997 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003999 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004000 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004001 */
Kumar Galacc334c72017-04-21 10:55:34 -05004002static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004003{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004004 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004005}
4006
Anas Nashif166f5192018-02-25 08:02:36 -06004007/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004008
4009/**
4010 * @cond INTERNAL_HIDDEN
4011 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004012
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004013struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004014 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004015 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004016};
4017
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004018/**
Allan Stephensc98da842016-11-11 15:45:03 -05004019 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004020 */
4021
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004022/**
Allan Stephensc98da842016-11-11 15:45:03 -05004023 * @addtogroup mem_pool_apis
4024 * @{
4025 */
4026
4027/**
4028 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004029 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004030 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4031 * long. The memory pool allows blocks to be repeatedly partitioned into
4032 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004033 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004034 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004035 * If the pool is to be accessed outside the module where it is defined, it
4036 * can be declared via
4037 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004038 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004039 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004040 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004041 * @param minsz Size of the smallest blocks in the pool (in bytes).
4042 * @param maxsz Size of the largest blocks in the pool (in bytes).
4043 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004044 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004045 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004046 */
Andy Ross73cb9582017-05-09 10:42:39 -07004047#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4048 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4049 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08004050 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07004051 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004052 .base = { \
4053 .buf = _mpool_buf_##name, \
4054 .max_sz = maxsz, \
4055 .n_max = nmax, \
4056 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
4057 .levels = _mpool_lvls_##name, \
4058 .flags = SYS_MEM_POOL_KERNEL \
4059 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004060 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004061
Peter Mitsis937042c2016-10-13 13:18:26 -04004062/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004063 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004064 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004065 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004066 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004067 * @param pool Address of the memory pool.
4068 * @param block Pointer to block descriptor for the allocated memory.
4069 * @param size Amount of memory to allocate (in bytes).
4070 * @param timeout Maximum time to wait for operation to complete
4071 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4072 * or K_FOREVER to wait as long as necessary.
4073 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004074 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004075 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004076 * @retval -ENOMEM Returned without waiting.
4077 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004078 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004079 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004080extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004081 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004082
4083/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004084 * @brief Allocate memory from a memory pool with malloc() semantics
4085 *
4086 * Such memory must be released using k_free().
4087 *
4088 * @param pool Address of the memory pool.
4089 * @param size Amount of memory to allocate (in bytes).
4090 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004091 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004092 */
4093extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4094
4095/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004096 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004097 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004098 * This routine releases a previously allocated memory block back to its
4099 * memory pool.
4100 *
4101 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004102 *
4103 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004104 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004105 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004106extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004107
4108/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004109 * @brief Free memory allocated from a memory pool.
4110 *
4111 * This routine releases a previously allocated memory block back to its
4112 * memory pool
4113 *
4114 * @param id Memory block identifier.
4115 *
4116 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004117 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004118 */
4119extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4120
4121/**
Anas Nashif166f5192018-02-25 08:02:36 -06004122 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004123 */
4124
4125/**
4126 * @defgroup heap_apis Heap Memory Pool APIs
4127 * @ingroup kernel_apis
4128 * @{
4129 */
4130
4131/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004132 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004133 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004134 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004135 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004137 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004139 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004140 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004141 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004142extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004143
4144/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004145 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004146 *
4147 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004148 * returned must have been allocated from the heap memory pool or
4149 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004150 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004151 * If @a ptr is NULL, no operation is performed.
4152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004153 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004154 *
4155 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004156 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004157 */
4158extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004159
Allan Stephensc98da842016-11-11 15:45:03 -05004160/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004161 * @brief Allocate memory from heap, array style
4162 *
4163 * This routine provides traditional calloc() semantics. Memory is
4164 * allocated from the heap memory pool and zeroed.
4165 *
4166 * @param nmemb Number of elements in the requested array
4167 * @param size Size of each array element (in bytes).
4168 *
4169 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004170 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004171 */
4172extern void *k_calloc(size_t nmemb, size_t size);
4173
Anas Nashif166f5192018-02-25 08:02:36 -06004174/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004175
Benjamin Walshacc68c12017-01-29 18:57:45 -05004176/* polling API - PRIVATE */
4177
Benjamin Walshb0179862017-02-02 16:39:57 -05004178#ifdef CONFIG_POLL
4179#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
4180#else
4181#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
4182#endif
4183
Benjamin Walshacc68c12017-01-29 18:57:45 -05004184/* private - implementation data created as needed, per-type */
4185struct _poller {
4186 struct k_thread *thread;
Andy Ross55a7e462018-05-31 11:58:09 -07004187 volatile int is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004188};
4189
4190/* private - types bit positions */
4191enum _poll_types_bits {
4192 /* can be used to ignore an event */
4193 _POLL_TYPE_IGNORE,
4194
4195 /* to be signaled by k_poll_signal() */
4196 _POLL_TYPE_SIGNAL,
4197
4198 /* semaphore availability */
4199 _POLL_TYPE_SEM_AVAILABLE,
4200
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004201 /* queue/fifo/lifo data availability */
4202 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004203
4204 _POLL_NUM_TYPES
4205};
4206
4207#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4208
4209/* private - states bit positions */
4210enum _poll_states_bits {
4211 /* default state when creating event */
4212 _POLL_STATE_NOT_READY,
4213
Benjamin Walshacc68c12017-01-29 18:57:45 -05004214 /* signaled by k_poll_signal() */
4215 _POLL_STATE_SIGNALED,
4216
4217 /* semaphore is available */
4218 _POLL_STATE_SEM_AVAILABLE,
4219
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004220 /* data is available to read on queue/fifo/lifo */
4221 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004222
4223 _POLL_NUM_STATES
4224};
4225
4226#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4227
4228#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004229 (32 - (0 \
4230 + 8 /* tag */ \
4231 + _POLL_NUM_TYPES \
4232 + _POLL_NUM_STATES \
4233 + 1 /* modes */ \
4234 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004235
Benjamin Walshacc68c12017-01-29 18:57:45 -05004236/* end of polling API - PRIVATE */
4237
4238
4239/**
4240 * @defgroup poll_apis Async polling APIs
4241 * @ingroup kernel_apis
4242 * @{
4243 */
4244
4245/* Public polling API */
4246
4247/* public - values for k_poll_event.type bitfield */
4248#define K_POLL_TYPE_IGNORE 0
4249#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4250#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004251#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4252#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004253
4254/* public - polling modes */
4255enum k_poll_modes {
4256 /* polling thread does not take ownership of objects when available */
4257 K_POLL_MODE_NOTIFY_ONLY = 0,
4258
4259 K_POLL_NUM_MODES
4260};
4261
4262/* public - values for k_poll_event.state bitfield */
4263#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004264#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4265#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004266#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4267#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004268
4269/* public - poll signal object */
4270struct k_poll_signal {
4271 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004272 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004273
4274 /*
4275 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4276 * user resets it to 0.
4277 */
4278 unsigned int signaled;
4279
4280 /* custom result value passed to k_poll_signal() if needed */
4281 int result;
4282};
4283
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004284#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004285 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004286 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004287 .signaled = 0, \
4288 .result = 0, \
4289 }
4290
4291struct k_poll_event {
4292 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004293 sys_dnode_t _node;
4294
4295 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004296 struct _poller *poller;
4297
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004298 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004299 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004300
Benjamin Walshacc68c12017-01-29 18:57:45 -05004301 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004302 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004303
4304 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004305 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004306
4307 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004308 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004309
4310 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004311 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004312
4313 /* per-type data */
4314 union {
4315 void *obj;
4316 struct k_poll_signal *signal;
4317 struct k_sem *sem;
4318 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004319 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004320 };
4321};
4322
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004323#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004324 { \
4325 .poller = NULL, \
4326 .type = event_type, \
4327 .state = K_POLL_STATE_NOT_READY, \
4328 .mode = event_mode, \
4329 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004330 { .obj = event_obj }, \
4331 }
4332
4333#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4334 event_tag) \
4335 { \
4336 .type = event_type, \
4337 .tag = event_tag, \
4338 .state = K_POLL_STATE_NOT_READY, \
4339 .mode = event_mode, \
4340 .unused = 0, \
4341 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004342 }
4343
4344/**
4345 * @brief Initialize one struct k_poll_event instance
4346 *
4347 * After this routine is called on a poll event, the event it ready to be
4348 * placed in an event array to be passed to k_poll().
4349 *
4350 * @param event The event to initialize.
4351 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4352 * values. Only values that apply to the same object being polled
4353 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4354 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004355 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004356 * @param obj Kernel object or poll signal.
4357 *
4358 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004359 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004360 */
4361
Kumar Galacc334c72017-04-21 10:55:34 -05004362extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004363 int mode, void *obj);
4364
4365/**
4366 * @brief Wait for one or many of multiple poll events to occur
4367 *
4368 * This routine allows a thread to wait concurrently for one or many of
4369 * multiple poll events to have occurred. Such events can be a kernel object
4370 * being available, like a semaphore, or a poll signal event.
4371 *
4372 * When an event notifies that a kernel object is available, the kernel object
4373 * is not "given" to the thread calling k_poll(): it merely signals the fact
4374 * that the object was available when the k_poll() call was in effect. Also,
4375 * all threads trying to acquire an object the regular way, i.e. by pending on
4376 * the object, have precedence over the thread polling on the object. This
4377 * means that the polling thread will never get the poll event on an object
4378 * until the object becomes available and its pend queue is empty. For this
4379 * reason, the k_poll() call is more effective when the objects being polled
4380 * only have one thread, the polling thread, trying to acquire them.
4381 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004382 * When k_poll() returns 0, the caller should loop on all the events that were
4383 * passed to k_poll() and check the state field for the values that were
4384 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004385 *
4386 * Before being reused for another call to k_poll(), the user has to reset the
4387 * state field to K_POLL_STATE_NOT_READY.
4388 *
Andrew Boie3772f772018-05-07 16:52:57 -07004389 * When called from user mode, a temporary memory allocation is required from
4390 * the caller's resource pool.
4391 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004392 * @param events An array of pointers to events to be polled for.
4393 * @param num_events The number of events in the array.
4394 * @param timeout Waiting period for an event to be ready (in milliseconds),
4395 * or one of the special values K_NO_WAIT and K_FOREVER.
4396 *
4397 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004398 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02004399 * @retval -EINTR Poller thread has been interrupted.
Andrew Boie3772f772018-05-07 16:52:57 -07004400 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4401 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004402 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004403 */
4404
Andrew Boie3772f772018-05-07 16:52:57 -07004405__syscall int k_poll(struct k_poll_event *events, int num_events,
4406 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004407
4408/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004409 * @brief Initialize a poll signal object.
4410 *
4411 * Ready a poll signal object to be signaled via k_poll_signal().
4412 *
4413 * @param signal A poll signal.
4414 *
4415 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004416 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004417 */
4418
Andrew Boie3772f772018-05-07 16:52:57 -07004419__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4420
4421/*
4422 * @brief Reset a poll signal object's state to unsignaled.
4423 *
4424 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004425 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004426 */
4427__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4428
4429static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4430{
4431 signal->signaled = 0;
4432}
4433
4434/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004435 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004436 *
4437 * @param signal A poll signal object
4438 * @param signaled An integer buffer which will be written nonzero if the
4439 * object was signaled
4440 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004441 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004442 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004443 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004444 */
4445__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4446 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004447
4448/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004449 * @brief Signal a poll signal object.
4450 *
4451 * This routine makes ready a poll signal, which is basically a poll event of
4452 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4453 * made ready to run. A @a result value can be specified.
4454 *
4455 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004456 * k_poll_signal(), stays set until the user sets it back to 0 with
4457 * k_poll_signal_reset(). It thus has to be reset by the user before being
4458 * passed again to k_poll() or k_poll() will consider it being signaled, and
4459 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004460 *
4461 * @param signal A poll signal.
4462 * @param result The value to store in the result field of the signal.
4463 *
4464 * @retval 0 The signal was delivered successfully.
4465 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004466 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004467 */
4468
Andrew Boie3772f772018-05-07 16:52:57 -07004469__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004470
Anas Nashif954d5502018-02-25 08:37:28 -06004471/**
4472 * @internal
4473 */
Andy Ross8606fab2018-03-26 10:54:40 -07004474extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004475
Anas Nashif166f5192018-02-25 08:02:36 -06004476/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004477
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004478/**
4479 * @brief Make the CPU idle.
4480 *
4481 * This function makes the CPU idle until an event wakes it up.
4482 *
4483 * In a regular system, the idle thread should be the only thread responsible
4484 * for making the CPU idle and triggering any type of power management.
4485 * However, in some more constrained systems, such as a single-threaded system,
4486 * the only thread would be responsible for this if needed.
4487 *
4488 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004489 * @req K-MISC-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004490 */
4491extern void k_cpu_idle(void);
4492
4493/**
4494 * @brief Make the CPU idle in an atomic fashion.
4495 *
4496 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4497 * must be done atomically before making the CPU idle.
4498 *
4499 * @param key Interrupt locking key obtained from irq_lock().
4500 *
4501 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004502 * @req K-MISC-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004503 */
4504extern void k_cpu_atomic_idle(unsigned int key);
4505
Anas Nashif954d5502018-02-25 08:37:28 -06004506
4507/**
4508 * @internal
4509 */
Kumar Galacc334c72017-04-21 10:55:34 -05004510extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004511
Andrew Boiecdb94d62017-04-18 15:22:05 -07004512#ifdef _ARCH_EXCEPT
4513/* This archtecture has direct support for triggering a CPU exception */
4514#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4515#else
4516
Andrew Boiecdb94d62017-04-18 15:22:05 -07004517/* NOTE: This is the implementation for arches that do not implement
4518 * _ARCH_EXCEPT() to generate a real CPU exception.
4519 *
4520 * We won't have a real exception frame to determine the PC value when
4521 * the oops occurred, so print file and line number before we jump into
4522 * the fatal error handler.
4523 */
4524#define _k_except_reason(reason) do { \
4525 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4526 _NanoFatalErrorHandler(reason, &_default_esf); \
4527 CODE_UNREACHABLE; \
4528 } while (0)
4529
4530#endif /* _ARCH__EXCEPT */
4531
4532/**
4533 * @brief Fatally terminate a thread
4534 *
4535 * This should be called when a thread has encountered an unrecoverable
4536 * runtime condition and needs to terminate. What this ultimately
4537 * means is determined by the _fatal_error_handler() implementation, which
4538 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4539 *
4540 * If this is called from ISR context, the default system fatal error handler
4541 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004542 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004543 */
4544#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4545
4546/**
4547 * @brief Fatally terminate the system
4548 *
4549 * This should be called when the Zephyr kernel has encountered an
4550 * unrecoverable runtime condition and needs to terminate. What this ultimately
4551 * means is determined by the _fatal_error_handler() implementation, which
4552 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004553 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004554 */
4555#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4556
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004557/*
4558 * private APIs that are utilized by one or more public APIs
4559 */
4560
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004561#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004562/**
4563 * @internal
4564 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004565extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004566#else
Anas Nashif954d5502018-02-25 08:37:28 -06004567/**
4568 * @internal
4569 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004570#define _init_static_threads() do { } while ((0))
4571#endif
4572
Anas Nashif954d5502018-02-25 08:37:28 -06004573/**
4574 * @internal
4575 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004576extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004577/**
4578 * @internal
4579 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004580extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004581
Andrew Boiedc5d9352017-06-02 12:56:47 -07004582/* arch/cpu.h may declare an architecture or platform-specific macro
4583 * for properly declaring stacks, compatible with MMU/MPU constraints if
4584 * enabled
4585 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004586
4587/**
4588 * @brief Obtain an extern reference to a stack
4589 *
4590 * This macro properly brings the symbol of a thread stack declared
4591 * elsewhere into scope.
4592 *
4593 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004594 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004595 */
4596#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4597
Andrew Boiedc5d9352017-06-02 12:56:47 -07004598#ifdef _ARCH_THREAD_STACK_DEFINE
4599#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4600#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4601 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304602#define K_THREAD_STACK_LEN(size) _ARCH_THREAD_STACK_LEN(size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004603#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4604#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004605static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004606{
4607 return _ARCH_THREAD_STACK_BUFFER(sym);
4608}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004609#else
4610/**
4611 * @brief Declare a toplevel thread stack memory region
4612 *
4613 * This declares a region of memory suitable for use as a thread's stack.
4614 *
4615 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4616 * 'noinit' section so that it isn't zeroed at boot
4617 *
Andrew Boie507852a2017-07-25 18:47:07 -07004618 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004619 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004620 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004621 *
4622 * It is legal to precede this definition with the 'static' keyword.
4623 *
4624 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4625 * parameter of k_thread_create(), it may not be the same as the
4626 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4627 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004628 * Some arches may round the size of the usable stack region up to satisfy
4629 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4630 * size.
4631 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004632 * @param sym Thread stack symbol name
4633 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004634 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004635 */
4636#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004637 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004638
4639/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304640 * @brief Calculate size of stacks to be allocated in a stack array
4641 *
4642 * This macro calculates the size to be allocated for the stacks
4643 * inside a stack array. It accepts the indicated "size" as a parameter
4644 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4645 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4646 *
4647 * @param size Size of the stack memory region
4648 * @req K-TSTACK-001
4649 */
4650#define K_THREAD_STACK_LEN(size) (size)
4651
4652/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004653 * @brief Declare a toplevel array of thread stack memory regions
4654 *
4655 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4656 * definition for additional details and constraints.
4657 *
4658 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4659 * 'noinit' section so that it isn't zeroed at boot
4660 *
4661 * @param sym Thread stack symbol name
4662 * @param nmemb Number of stacks to declare
4663 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004664 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004665 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004666#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004667 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304668 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004669
4670/**
4671 * @brief Declare an embedded stack memory region
4672 *
4673 * Used for stacks embedded within other data structures. Use is highly
4674 * discouraged but in some cases necessary. For memory protection scenarios,
4675 * it is very important that any RAM preceding this member not be writable
4676 * by threads else a stack overflow will lead to silent corruption. In other
4677 * words, the containing data structure should live in RAM owned by the kernel.
4678 *
4679 * @param sym Thread stack symbol name
4680 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004681 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004682 */
4683#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004684 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004685
4686/**
4687 * @brief Return the size in bytes of a stack memory region
4688 *
4689 * Convenience macro for passing the desired stack size to k_thread_create()
4690 * since the underlying implementation may actually create something larger
4691 * (for instance a guard area).
4692 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004693 * The value returned here is not guaranteed to match the 'size' parameter
4694 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004695 *
4696 * @param sym Stack memory symbol
4697 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004698 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004699 */
4700#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4701
4702/**
4703 * @brief Get a pointer to the physical stack buffer
4704 *
4705 * Convenience macro to get at the real underlying stack buffer used by
4706 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4707 * This is really only intended for diagnostic tools which want to examine
4708 * stack memory contents.
4709 *
4710 * @param sym Declared stack symbol name
4711 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004712 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004713 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004714static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004715{
4716 return (char *)sym;
4717}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004718
4719#endif /* _ARCH_DECLARE_STACK */
4720
Chunlin Hane9c97022017-07-07 20:29:30 +08004721/**
4722 * @defgroup mem_domain_apis Memory domain APIs
4723 * @ingroup kernel_apis
4724 * @{
4725 */
4726
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004727/**
4728 * @def MEM_PARTITION_ENTRY
4729 * @brief Used to declare a memory partition entry
4730 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004731 */
4732#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4733 {\
4734 .start = _start, \
4735 .size = _size, \
4736 .attr = _attr, \
4737 }
4738
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004739/**
4740 * @def K_MEM_PARTITION_DEFINE
4741 * @brief Used to declare a memory partition
4742 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004743 */
4744#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4745#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4746 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304747 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004748 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4749#else
4750#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304751 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004752 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4753#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4754
Chunlin Hane9c97022017-07-07 20:29:30 +08004755/* memory partition */
4756struct k_mem_partition {
4757 /* start address of memory partition */
4758 u32_t start;
4759 /* size of memory partition */
4760 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304761#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004762 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304763 k_mem_partition_attr_t attr;
4764#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004765};
4766
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304767/* memory domian
4768 * Note: Always declare this structure with __kernel prefix
4769 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004770struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304771#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004772 /* partitions in the domain */
4773 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304774#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004775 /* domain q */
4776 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004777 /* number of partitions in the domain */
4778 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004779};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304780
Chunlin Hane9c97022017-07-07 20:29:30 +08004781
4782/**
4783 * @brief Initialize a memory domain.
4784 *
4785 * Initialize a memory domain with given name and memory partitions.
4786 *
4787 * @param domain The memory domain to be initialized.
4788 * @param num_parts The number of array items of "parts" parameter.
4789 * @param parts An array of pointers to the memory partitions. Can be NULL
4790 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004791 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004792 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004793extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004794 struct k_mem_partition *parts[]);
4795/**
4796 * @brief Destroy a memory domain.
4797 *
4798 * Destroy a memory domain.
4799 *
4800 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004801 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004802 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004803extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4804
4805/**
4806 * @brief Add a memory partition into a memory domain.
4807 *
4808 * Add a memory partition into a memory domain.
4809 *
4810 * @param domain The memory domain to be added a memory partition.
4811 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004812 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004813 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004814extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4815 struct k_mem_partition *part);
4816
4817/**
4818 * @brief Remove a memory partition from a memory domain.
4819 *
4820 * Remove a memory partition from a memory domain.
4821 *
4822 * @param domain The memory domain to be removed a memory partition.
4823 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004824 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004825 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004826extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4827 struct k_mem_partition *part);
4828
4829/**
4830 * @brief Add a thread into a memory domain.
4831 *
4832 * Add a thread into a memory domain.
4833 *
4834 * @param domain The memory domain that the thread is going to be added into.
4835 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004836 *
4837 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004838 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004839extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4840 k_tid_t thread);
4841
4842/**
4843 * @brief Remove a thread from its memory domain.
4844 *
4845 * Remove a thread from its memory domain.
4846 *
4847 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004848 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004849 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004850extern void k_mem_domain_remove_thread(k_tid_t thread);
4851
Anas Nashif166f5192018-02-25 08:02:36 -06004852/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004853
Andrew Boie756f9072017-10-10 16:01:49 -07004854/**
4855 * @brief Emit a character buffer to the console device
4856 *
4857 * @param c String of characters to print
4858 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004859 *
4860 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004861 */
4862__syscall void k_str_out(char *c, size_t n);
4863
Andy Rosse7172672018-01-24 15:48:32 -08004864/**
4865 * @brief Start a numbered CPU on a MP-capable system
4866
4867 * This starts and initializes a specific CPU. The main thread on
4868 * startup is running on CPU zero, other processors are numbered
4869 * sequentially. On return from this function, the CPU is known to
4870 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004871 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004872 * with the provided key will work to enable them.
4873 *
4874 * Normally, in SMP mode this function will be called by the kernel
4875 * initialization and should not be used as a user API. But it is
4876 * defined here for special-purpose apps which want Zephyr running on
4877 * one core and to use others for design-specific processing.
4878 *
4879 * @param cpu_num Integer number of the CPU
4880 * @param stack Stack memory for the CPU
4881 * @param sz Stack buffer size, in bytes
4882 * @param fn Function to begin running on the CPU. First argument is
4883 * an irq_unlock() key.
4884 * @param arg Untyped argument to be passed to "fn"
4885 */
4886extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4887 void (*fn)(int, void *), void *arg);
4888
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004889#ifdef __cplusplus
4890}
4891#endif
4892
Andrew Boiee004dec2016-11-07 09:01:19 -08004893#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4894/*
4895 * Define new and delete operators.
4896 * At this moment, the operators do nothing since objects are supposed
4897 * to be statically allocated.
4898 */
4899inline void operator delete(void *ptr)
4900{
4901 (void)ptr;
4902}
4903
4904inline void operator delete[](void *ptr)
4905{
4906 (void)ptr;
4907}
4908
4909inline void *operator new(size_t size)
4910{
4911 (void)size;
4912 return NULL;
4913}
4914
4915inline void *operator new[](size_t size)
4916{
4917 (void)size;
4918 return NULL;
4919}
4920
4921/* Placement versions of operator new and delete */
4922inline void operator delete(void *ptr1, void *ptr2)
4923{
4924 (void)ptr1;
4925 (void)ptr2;
4926}
4927
4928inline void operator delete[](void *ptr1, void *ptr2)
4929{
4930 (void)ptr1;
4931 (void)ptr2;
4932}
4933
4934inline void *operator new(size_t size, void *ptr)
4935{
4936 (void)size;
4937 return ptr;
4938}
4939
4940inline void *operator new[](size_t size, void *ptr)
4941{
4942 (void)size;
4943 return ptr;
4944}
4945
4946#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4947
Andrew Boiefa94ee72017-09-28 16:54:35 -07004948#include <syscalls/kernel.h>
4949
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004950#endif /* !_ASMLANGUAGE */
4951
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004952#endif /* _kernel__h_ */