blob: c6e6a0b238b8b6e2a17860a2dd632f8183cc2ac6 [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
Andrew Boie7f4d0062018-07-19 11:09:33 -0700541#ifdef CONFIG_USERSPACE
542 /* Set to the lowest area in the thread stack since this needs to
543 * be directly read/writable by user mode. Not ideal, but best we
544 * can do until we have thread-local storage
545 */
546 int *errno_location;
547#else
Anas Nashifce78d162018-05-24 12:43:11 -0500548 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700549 int errno_var;
550#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700551#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700552
553#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500554 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700555 struct _thread_stack_info stack_info;
556#endif /* CONFIG_THREAD_STACK_INFO */
557
Chunlin Hane9c97022017-07-07 20:29:30 +0800558#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500559 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800560 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500561 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700562 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800563#endif /* CONFIG_USERSPACE */
564
Andy Ross042d8ec2017-12-09 08:37:20 -0800565#if defined(CONFIG_USE_SWITCH)
566 /* When using __switch() a few previously arch-specific items
567 * become part of the core OS
568 */
569
Anas Nashifce78d162018-05-24 12:43:11 -0500570 /** _Swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800571 int swap_retval;
572
Anas Nashifce78d162018-05-24 12:43:11 -0500573 /** Context handle returned via _arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800574 void *switch_handle;
575#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500576 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700577 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800578
Anas Nashifce78d162018-05-24 12:43:11 -0500579 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700580 struct _thread_arch arch;
581};
582
583typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400584typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700585#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400586
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400587enum execution_context_types {
588 K_ISR = 0,
589 K_COOP_THREAD,
590 K_PREEMPT_THREAD,
591};
592
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400593/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100594 * @defgroup profiling_apis Profiling APIs
595 * @ingroup kernel_apis
596 * @{
597 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530598typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
599 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100600
601/**
602 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
603 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700604 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100605 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
606 *
607 * CONFIG_MAIN_STACK_SIZE
608 * CONFIG_IDLE_STACK_SIZE
609 * CONFIG_ISR_STACK_SIZE
610 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
611 *
612 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
613 * produce output.
614 *
615 * @return N/A
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530616 *
617 * @deprecated This API is deprecated. Use k_thread_foreach().
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100618 */
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530619__deprecated extern void k_call_stacks_analyze(void);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100620
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530621/**
622 * @brief Iterate over all the threads in the system.
623 *
624 * This routine iterates over all the threads in the system and
625 * calls the user_cb function for each thread.
626 *
627 * @param user_cb Pointer to the user callback function.
628 * @param user_data Pointer to user data.
629 *
630 * @note CONFIG_THREAD_MONITOR must be set for this function
631 * to be effective. Also this API uses irq_lock to protect the
632 * _kernel.threads list which means creation of new threads and
633 * terminations of existing threads are blocked until this
634 * API returns.
635 *
636 * @return N/A
637 */
638extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
639
Anas Nashif166f5192018-02-25 08:02:36 -0600640/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100641
642/**
Allan Stephensc98da842016-11-11 15:45:03 -0500643 * @defgroup thread_apis Thread APIs
644 * @ingroup kernel_apis
645 * @{
646 */
647
Benjamin Walshed240f22017-01-22 13:05:08 -0500648#endif /* !_ASMLANGUAGE */
649
650
651/*
652 * Thread user options. May be needed by assembly code. Common part uses low
653 * bits, arch-specific use high bits.
654 */
655
Anas Nashifa541e932018-05-24 11:19:16 -0500656/**
657 * @brief system thread that must not abort
658 * @req K-THREAD-000
659 * */
Benjamin Walshed240f22017-01-22 13:05:08 -0500660#define K_ESSENTIAL (1 << 0)
661
662#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500663/**
664 * @brief thread uses floating point registers
665 */
Benjamin Walshed240f22017-01-22 13:05:08 -0500666#define K_FP_REGS (1 << 1)
667#endif
668
Anas Nashifa541e932018-05-24 11:19:16 -0500669/**
670 * @brief user mode thread
671 *
672 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700673 * has additional restrictions
674 */
675#define K_USER (1 << 2)
676
Anas Nashifa541e932018-05-24 11:19:16 -0500677/**
678 * @brief Inherit Permissions
679 *
680 * @details
681 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700682 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
683 * is not enabled.
684 */
685#define K_INHERIT_PERMS (1 << 3)
686
Benjamin Walshed240f22017-01-22 13:05:08 -0500687#ifdef CONFIG_X86
688/* x86 Bitmask definitions for threads user options */
689
690#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
691/* thread uses SSEx (and also FP) registers */
692#define K_SSE_REGS (1 << 7)
693#endif
694#endif
695
696/* end - thread options */
697
698#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400699/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700700 * @brief Create a thread.
701 *
702 * This routine initializes a thread, then schedules it for execution.
703 *
704 * The new thread may be scheduled for immediate execution or a delayed start.
705 * If the newly spawned thread does not have a delayed start the kernel
706 * scheduler may preempt the current thread to allow the new thread to
707 * execute.
708 *
709 * Thread options are architecture-specific, and can include K_ESSENTIAL,
710 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
711 * them using "|" (the logical OR operator).
712 *
713 * Historically, users often would use the beginning of the stack memory region
714 * to store the struct k_thread data, although corruption will occur if the
715 * stack overflows this region and stack protection features may not detect this
716 * situation.
717 *
718 * @param new_thread Pointer to uninitialized struct k_thread
719 * @param stack Pointer to the stack space.
720 * @param stack_size Stack size in bytes.
721 * @param entry Thread entry function.
722 * @param p1 1st entry point parameter.
723 * @param p2 2nd entry point parameter.
724 * @param p3 3rd entry point parameter.
725 * @param prio Thread priority.
726 * @param options Thread options.
727 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
728 *
729 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400730 *
731 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700732 */
Andrew Boie662c3452017-10-02 10:51:18 -0700733__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700734 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700735 size_t stack_size,
736 k_thread_entry_t entry,
737 void *p1, void *p2, void *p3,
738 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700739
Andrew Boie3f091b52017-08-30 14:34:14 -0700740/**
741 * @brief Drop a thread's privileges permanently to user mode
742 *
743 * @param entry Function to start executing from
744 * @param p1 1st entry point parameter
745 * @param p2 2nd entry point parameter
746 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400747 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700748 */
749extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
750 void *p1, void *p2,
751 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700752
Andrew Boied26cf2d2017-03-30 13:07:02 -0700753/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700754 * @brief Grant a thread access to a NULL-terminated set of kernel objects
755 *
756 * This is a convenience function. For the provided thread, grant access to
757 * the remaining arguments, which must be pointers to kernel objects.
758 * The final argument must be a NULL.
759 *
760 * The thread object must be initialized (i.e. running). The objects don't
761 * need to be.
762 *
763 * @param thread Thread to grant access to objects
764 * @param ... NULL-terminated list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400765 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700766 */
767extern void __attribute__((sentinel))
768 k_thread_access_grant(struct k_thread *thread, ...);
769
770/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700771 * @brief Assign a resource memory pool to a thread
772 *
773 * By default, threads have no resource pool assigned unless their parent
774 * thread has a resource pool, in which case it is inherited. Multiple
775 * threads may be assigned to the same memory pool.
776 *
777 * Changing a thread's resource pool will not migrate allocations from the
778 * previous pool.
779 *
780 * @param thread Target thread to assign a memory pool for resource requests,
781 * or NULL if the thread should no longer have a memory pool.
782 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400783 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700784 */
785static inline void k_thread_resource_pool_assign(struct k_thread *thread,
786 struct k_mem_pool *pool)
787{
788 thread->resource_pool = pool;
789}
790
791#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
792/**
793 * @brief Assign the system heap as a thread's resource pool
794 *
795 * Similar to k_thread_resource_pool_assign(), but the thread will use
796 * the kernel heap to draw memory.
797 *
798 * Use with caution, as a malicious thread could perform DoS attacks on the
799 * kernel heap.
800 *
801 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400802 *
803 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700804 */
805void k_thread_system_pool_assign(struct k_thread *thread);
806#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
807
808/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500809 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810 *
Allan Stephensc98da842016-11-11 15:45:03 -0500811 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500812 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400813 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500814 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400815 *
816 * @return N/A
817 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700818__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819
820/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500821 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400822 *
823 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500824 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400825 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400826 * @return N/A
827 */
Kumar Galacc334c72017-04-21 10:55:34 -0500828extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829
830/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500831 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500833 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400834 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500835 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836 *
837 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400838 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400839 */
Andrew Boie468190a2017-09-29 14:00:48 -0700840__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400841
842/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500843 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500845 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500847 * If @a thread is not currently sleeping, the routine has no effect.
848 *
849 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400850 *
851 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400852 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400853 */
Andrew Boie468190a2017-09-29 14:00:48 -0700854__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400855
856/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500857 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400858 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500859 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400860 *
861 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400862 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700863__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400864
865/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500866 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500868 * This routine prevents @a thread from executing if it has not yet started
869 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500871 * @param thread ID of thread to cancel.
872 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700873 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500874 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700875 *
876 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400877 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700878__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400879
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400880/**
Allan Stephensc98da842016-11-11 15:45:03 -0500881 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500883 * This routine permanently stops execution of @a thread. The thread is taken
884 * off all kernel queues it is part of (i.e. the ready queue, the timeout
885 * queue, or a kernel object wait queue). However, any kernel resources the
886 * thread might currently own (such as mutexes or memory blocks) are not
887 * released. It is the responsibility of the caller of this routine to ensure
888 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400889 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500890 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400891 *
892 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400893 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400894 */
Andrew Boie468190a2017-09-29 14:00:48 -0700895__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400896
Andrew Boie7d627c52017-08-30 11:01:56 -0700897
898/**
899 * @brief Start an inactive thread
900 *
901 * If a thread was created with K_FOREVER in the delay parameter, it will
902 * not be added to the scheduling queue until this function is called
903 * on it.
904 *
905 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400906 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700907 */
Andrew Boie468190a2017-09-29 14:00:48 -0700908__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700909
Allan Stephensc98da842016-11-11 15:45:03 -0500910/**
911 * @cond INTERNAL_HIDDEN
912 */
913
Benjamin Walshd211a522016-12-06 11:44:01 -0500914/* timeout has timed out and is not on _timeout_q anymore */
915#define _EXPIRED (-2)
916
917/* timeout is not in use */
918#define _INACTIVE (-1)
919
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400920struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700921 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700922 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400923 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700924 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500925 void *init_p1;
926 void *init_p2;
927 void *init_p3;
928 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500929 u32_t init_options;
930 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500931 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400932};
933
Andrew Boied26cf2d2017-03-30 13:07:02 -0700934#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400935 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500936 prio, options, delay, abort, groups) \
937 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700938 .init_thread = (thread), \
939 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500940 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700941 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400942 .init_p1 = (void *)p1, \
943 .init_p2 = (void *)p2, \
944 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500945 .init_prio = (prio), \
946 .init_options = (options), \
947 .init_delay = (delay), \
948 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400949 }
950
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400951/**
Allan Stephensc98da842016-11-11 15:45:03 -0500952 * INTERNAL_HIDDEN @endcond
953 */
954
955/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500956 * @brief Statically define and initialize a thread.
957 *
958 * The thread may be scheduled for immediate execution or a delayed start.
959 *
960 * Thread options are architecture-specific, and can include K_ESSENTIAL,
961 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
962 * them using "|" (the logical OR operator).
963 *
964 * The ID of the thread can be accessed using:
965 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500966 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500967 *
968 * @param name Name of the thread.
969 * @param stack_size Stack size in bytes.
970 * @param entry Thread entry function.
971 * @param p1 1st entry point parameter.
972 * @param p2 2nd entry point parameter.
973 * @param p3 3rd entry point parameter.
974 * @param prio Thread priority.
975 * @param options Thread options.
976 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400977 *
Anas Nashif47420d02018-05-24 14:20:56 -0400978 * @req K-THREAD-010
979 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400980 * @internal It has been observed that the x86 compiler by default aligns
981 * these _static_thread_data structures to 32-byte boundaries, thereby
982 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400983 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400984 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500985#define K_THREAD_DEFINE(name, stack_size, \
986 entry, p1, p2, p3, \
987 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700988 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700989 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500990 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500991 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700992 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
993 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500994 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700995 NULL, 0); \
996 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400997
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400998/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500999 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001001 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001002 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001003 * @param thread ID of thread whose priority is needed.
1004 *
1005 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -04001006 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001007 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001008__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001009
1010/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001011 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001013 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001014 *
1015 * Rescheduling can occur immediately depending on the priority @a thread is
1016 * set to:
1017 *
1018 * - If its priority is raised above the priority of the caller of this
1019 * function, and the caller is preemptible, @a thread will be scheduled in.
1020 *
1021 * - If the caller operates on itself, it lowers its priority below that of
1022 * other threads in the system, and the caller is preemptible, the thread of
1023 * highest priority will be scheduled in.
1024 *
1025 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1026 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1027 * highest priority.
1028 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001029 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001030 * @param prio New priority.
1031 *
1032 * @warning Changing the priority of a thread currently involved in mutex
1033 * priority inheritance may result in undefined behavior.
1034 *
1035 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001036 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001037 */
Andrew Boie468190a2017-09-29 14:00:48 -07001038__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001039
Andy Ross4a2e50f2018-05-15 11:06:25 -07001040
1041#ifdef CONFIG_SCHED_DEADLINE
1042/**
1043 * @brief Set deadline expiration time for scheduler
1044 *
1045 * This sets the "deadline" expiration as a time delta from the
1046 * current time, in the same units used by k_cycle_get_32(). The
1047 * scheduler (when deadline scheduling is enabled) will choose the
1048 * next expiring thread when selecting between threads at the same
1049 * static priority. Threads at different priorities will be scheduled
1050 * according to their static priority.
1051 *
1052 * @note Deadlines that are negative (i.e. in the past) are still seen
1053 * as higher priority than others, even if the thread has "finished"
1054 * its work. If you don't want it scheduled anymore, you have to
1055 * reset the deadline into the future, block/pend the thread, or
1056 * modify its priority with k_thread_priority_set().
1057 *
1058 * @note Despite the API naming, the scheduler makes no guarantees the
1059 * the thread WILL be scheduled within that deadline, nor does it take
1060 * extra metadata (like e.g. the "runtime" and "period" parameters in
1061 * Linux sched_setattr()) that allows the kernel to validate the
1062 * scheduling for achievability. Such features could be implemented
1063 * above this call, which is simply input to the priority selection
1064 * logic.
1065 *
1066 * @param thread A thread on which to set the deadline
1067 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001068 *
1069 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001070 */
1071__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1072#endif
1073
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001074/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001075 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001076 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001077 * This routine prevents the kernel scheduler from making @a thread the
1078 * current thread. All other internal operations on @a thread are still
1079 * performed; for example, any timeout it is waiting on keeps ticking,
1080 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001081 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001082 * If @a thread is already suspended, the routine has no effect.
1083 *
1084 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001085 *
1086 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001087 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001088 */
Andrew Boie468190a2017-09-29 14:00:48 -07001089__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001090
1091/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001092 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001094 * This routine allows the kernel scheduler to make @a thread the current
1095 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001096 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001097 * If @a thread is not currently suspended, the routine has no effect.
1098 *
1099 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001100 *
1101 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001102 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001103 */
Andrew Boie468190a2017-09-29 14:00:48 -07001104__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001105
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001106/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001107 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001108 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001109 * This routine specifies how the scheduler will perform time slicing of
1110 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001112 * To enable time slicing, @a slice must be non-zero. The scheduler
1113 * ensures that no thread runs for more than the specified time limit
1114 * before other threads of that priority are given a chance to execute.
1115 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001116 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001117 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001118 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001119 * execute. Once the scheduler selects a thread for execution, there is no
1120 * minimum guaranteed time the thread will execute before threads of greater or
1121 * equal priority are scheduled.
1122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001123 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001124 * for execution, this routine has no effect; the thread is immediately
1125 * rescheduled after the slice period expires.
1126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001127 * To disable timeslicing, set both @a slice and @a prio to zero.
1128 *
1129 * @param slice Maximum time slice length (in milliseconds).
1130 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001131 *
1132 * @return N/A
1133 */
Kumar Galacc334c72017-04-21 10:55:34 -05001134extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001135
Anas Nashif166f5192018-02-25 08:02:36 -06001136/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001137
1138/**
1139 * @addtogroup isr_apis
1140 * @{
1141 */
1142
1143/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001144 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001145 *
Allan Stephensc98da842016-11-11 15:45:03 -05001146 * This routine allows the caller to customize its actions, depending on
1147 * whether it is a thread or an ISR.
1148 *
1149 * @note Can be called by ISRs.
1150 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001151 * @return 0 if invoked by a thread.
1152 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001153 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001154extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001155
Benjamin Walsh445830d2016-11-10 15:54:27 -05001156/**
1157 * @brief Determine if code is running in a preemptible thread.
1158 *
Allan Stephensc98da842016-11-11 15:45:03 -05001159 * This routine allows the caller to customize its actions, depending on
1160 * whether it can be preempted by another thread. The routine returns a 'true'
1161 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001162 *
Allan Stephensc98da842016-11-11 15:45:03 -05001163 * - The code is running in a thread, not at ISR.
1164 * - The thread's priority is in the preemptible range.
1165 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001166 *
Allan Stephensc98da842016-11-11 15:45:03 -05001167 * @note Can be called by ISRs.
1168 *
1169 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001170 * @return Non-zero if invoked by a preemptible thread.
1171 */
Andrew Boie468190a2017-09-29 14:00:48 -07001172__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001173
Allan Stephensc98da842016-11-11 15:45:03 -05001174/**
Anas Nashif166f5192018-02-25 08:02:36 -06001175 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001176 */
1177
1178/**
1179 * @addtogroup thread_apis
1180 * @{
1181 */
1182
1183/**
1184 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001185 *
Allan Stephensc98da842016-11-11 15:45:03 -05001186 * This routine prevents the current thread from being preempted by another
1187 * thread by instructing the scheduler to treat it as a cooperative thread.
1188 * If the thread subsequently performs an operation that makes it unready,
1189 * it will be context switched out in the normal manner. When the thread
1190 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001191 *
Allan Stephensc98da842016-11-11 15:45:03 -05001192 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001193 *
Allan Stephensc98da842016-11-11 15:45:03 -05001194 * @note k_sched_lock() and k_sched_unlock() should normally be used
1195 * when the operation being performed can be safely interrupted by ISRs.
1196 * However, if the amount of processing involved is very small, better
1197 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001198 *
1199 * @return N/A
1200 */
1201extern void k_sched_lock(void);
1202
Allan Stephensc98da842016-11-11 15:45:03 -05001203/**
1204 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001205 *
Allan Stephensc98da842016-11-11 15:45:03 -05001206 * This routine reverses the effect of a previous call to k_sched_lock().
1207 * A thread must call the routine once for each time it called k_sched_lock()
1208 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001209 *
1210 * @return N/A
1211 */
1212extern void k_sched_unlock(void);
1213
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001214/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001215 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001216 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001217 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001218 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001219 * Custom data is not used by the kernel itself, and is freely available
1220 * for a thread to use as it sees fit. It can be used as a framework
1221 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001222 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001223 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001224 *
1225 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001226 *
1227 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001228 */
Andrew Boie468190a2017-09-29 14:00:48 -07001229__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001230
1231/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001232 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001233 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001234 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001236 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001237 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001238 */
Andrew Boie468190a2017-09-29 14:00:48 -07001239__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001240
1241/**
Anas Nashif166f5192018-02-25 08:02:36 -06001242 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001243 */
1244
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001245#include <sys_clock.h>
1246
Allan Stephensc2f15a42016-11-17 12:24:22 -05001247/**
1248 * @addtogroup clock_apis
1249 * @{
1250 */
1251
1252/**
1253 * @brief Generate null timeout delay.
1254 *
1255 * This macro generates a timeout delay that that instructs a kernel API
1256 * not to wait if the requested operation cannot be performed immediately.
1257 *
1258 * @return Timeout delay value.
1259 */
1260#define K_NO_WAIT 0
1261
1262/**
1263 * @brief Generate timeout delay from milliseconds.
1264 *
1265 * This macro generates a timeout delay that that instructs a kernel API
1266 * to wait up to @a ms milliseconds to perform the requested operation.
1267 *
1268 * @param ms Duration in milliseconds.
1269 *
1270 * @return Timeout delay value.
1271 */
Johan Hedberg14471692016-11-13 10:52:15 +02001272#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001273
1274/**
1275 * @brief Generate timeout delay from seconds.
1276 *
1277 * This macro generates a timeout delay that that instructs a kernel API
1278 * to wait up to @a s seconds to perform the requested operation.
1279 *
1280 * @param s Duration in seconds.
1281 *
1282 * @return Timeout delay value.
1283 */
Johan Hedberg14471692016-11-13 10:52:15 +02001284#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001285
1286/**
1287 * @brief Generate timeout delay from minutes.
1288 *
1289 * This macro generates a timeout delay that that instructs a kernel API
1290 * to wait up to @a m minutes to perform the requested operation.
1291 *
1292 * @param m Duration in minutes.
1293 *
1294 * @return Timeout delay value.
1295 */
Johan Hedberg14471692016-11-13 10:52:15 +02001296#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001297
1298/**
1299 * @brief Generate timeout delay from hours.
1300 *
1301 * This macro generates a timeout delay that that instructs a kernel API
1302 * to wait up to @a h hours to perform the requested operation.
1303 *
1304 * @param h Duration in hours.
1305 *
1306 * @return Timeout delay value.
1307 */
Johan Hedberg14471692016-11-13 10:52:15 +02001308#define K_HOURS(h) K_MINUTES((h) * 60)
1309
Allan Stephensc98da842016-11-11 15:45:03 -05001310/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001311 * @brief Generate infinite timeout delay.
1312 *
1313 * This macro generates a timeout delay that that instructs a kernel API
1314 * to wait as long as necessary to perform the requested operation.
1315 *
1316 * @return Timeout delay value.
1317 */
1318#define K_FOREVER (-1)
1319
1320/**
Anas Nashif166f5192018-02-25 08:02:36 -06001321 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001322 */
1323
1324/**
Allan Stephensc98da842016-11-11 15:45:03 -05001325 * @cond INTERNAL_HIDDEN
1326 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001327
Benjamin Walsh62092182016-12-20 14:39:08 -05001328/* kernel clocks */
1329
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001330#ifdef CONFIG_SYS_CLOCK_EXISTS
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001331
1332/*
1333 * If timer frequency is known at compile time, a simple (32-bit)
1334 * tick <-> ms conversion could be used for some combinations of
1335 * hardware timer frequency and tick rate. Otherwise precise
1336 * (64-bit) calculations are used.
1337 */
1338
1339#if !defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME)
1340#if (sys_clock_hw_cycles_per_sec % sys_clock_ticks_per_sec) != 0
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001341 #define _NEED_PRECISE_TICK_MS_CONVERSION
1342#elif (MSEC_PER_SEC % sys_clock_ticks_per_sec) != 0
Benjamin Walsh62092182016-12-20 14:39:08 -05001343 #define _NON_OPTIMIZED_TICKS_PER_SEC
1344#endif
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001345#endif
Benjamin Walsh62092182016-12-20 14:39:08 -05001346
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001347#if defined(CONFIG_TIMER_READS_ITS_FREQUENCY_AT_RUNTIME) || \
1348 defined(_NON_OPTIMIZED_TICKS_PER_SEC)
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001349 #define _NEED_PRECISE_TICK_MS_CONVERSION
1350#endif
1351#endif
1352
Kumar Galacc334c72017-04-21 10:55:34 -05001353static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001354{
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001355#ifdef CONFIG_SYS_CLOCK_EXISTS
1356
1357#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
1358 /* use 64-bit math to keep precision */
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001359 s64_t ms_ticks_per_sec = (s64_t)ms * sys_clock_ticks_per_sec;
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001360
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001361 return (s32_t)ceiling_fraction(ms_ticks_per_sec, MSEC_PER_SEC);
1362#else
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001363 /* simple division keeps precision */
1364 s32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
1365
1366 return (s32_t)ceiling_fraction(ms, ms_per_tick);
1367#endif
1368
1369#else
1370 __ASSERT(ms == 0, "ms not zero");
1371 return 0;
Benjamin Walsh62092182016-12-20 14:39:08 -05001372#endif
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001373}
Benjamin Walsh62092182016-12-20 14:39:08 -05001374
Kumar Galacc334c72017-04-21 10:55:34 -05001375static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001376{
Benjamin Walsh62092182016-12-20 14:39:08 -05001377#ifdef CONFIG_SYS_CLOCK_EXISTS
1378
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001379#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
1380 /* use 64-bit math to keep precision */
1381 return (u64_t)ticks * sys_clock_hw_cycles_per_tick * MSEC_PER_SEC /
Piotr Zięcik96aa0d22018-07-13 16:37:50 +02001382 sys_clock_hw_cycles_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001383#else
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001384 /* simple multiplication keeps precision */
1385 u32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
1386
1387 return (u64_t)ticks * ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001388#endif
1389
1390#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001391 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001392 return 0;
1393#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001394}
1395
Piotr Zięcik77f42f82018-06-11 14:26:29 +02001396/* added tick needed to account for tick in progress */
1397#ifdef CONFIG_TICKLESS_KERNEL
1398#define _TICK_ALIGN 0
1399#else
1400#define _TICK_ALIGN 1
1401#endif
1402
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001403struct k_timer {
1404 /*
1405 * _timeout structure must be first here if we want to use
1406 * dynamic timer allocation. timeout.node is used in the double-linked
1407 * list of free timers
1408 */
1409 struct _timeout timeout;
1410
Allan Stephens45bfa372016-10-12 12:39:42 -05001411 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001412 _wait_q_t wait_q;
1413
1414 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001415 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001416
1417 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001418 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001419
1420 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001421 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001422
Allan Stephens45bfa372016-10-12 12:39:42 -05001423 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001424 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001425
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001426 /* user-specific data, also used to support legacy features */
1427 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001428
Anas Nashif2f203c22016-12-18 06:57:45 -05001429 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001430};
1431
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001432#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001433 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001434 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001435 .timeout.wait_q = NULL, \
1436 .timeout.thread = NULL, \
1437 .timeout.func = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001438 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001439 .expiry_fn = expiry, \
1440 .stop_fn = stop, \
1441 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001442 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001443 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001444 }
1445
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001446#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1447
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001448/**
Allan Stephensc98da842016-11-11 15:45:03 -05001449 * INTERNAL_HIDDEN @endcond
1450 */
1451
1452/**
1453 * @defgroup timer_apis Timer APIs
1454 * @ingroup kernel_apis
1455 * @{
1456 */
1457
1458/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001459 * @typedef k_timer_expiry_t
1460 * @brief Timer expiry function type.
1461 *
1462 * A timer's expiry function is executed by the system clock interrupt handler
1463 * each time the timer expires. The expiry function is optional, and is only
1464 * invoked if the timer has been initialized with one.
1465 *
1466 * @param timer Address of timer.
1467 *
1468 * @return N/A
1469 */
1470typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1471
1472/**
1473 * @typedef k_timer_stop_t
1474 * @brief Timer stop function type.
1475 *
1476 * A timer's stop function is executed if the timer is stopped prematurely.
1477 * The function runs in the context of the thread that stops the timer.
1478 * The stop function is optional, and is only invoked if the timer has been
1479 * initialized with one.
1480 *
1481 * @param timer Address of timer.
1482 *
1483 * @return N/A
1484 */
1485typedef void (*k_timer_stop_t)(struct k_timer *timer);
1486
1487/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001488 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001489 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001490 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001491 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001492 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001493 *
1494 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001495 * @param expiry_fn Function to invoke each time the timer expires.
1496 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001497 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001498#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001499 struct k_timer name \
1500 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001501 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001502
Allan Stephens45bfa372016-10-12 12:39:42 -05001503/**
1504 * @brief Initialize a timer.
1505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001506 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001507 *
1508 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001509 * @param expiry_fn Function to invoke each time the timer expires.
1510 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001511 *
1512 * @return N/A
1513 */
1514extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001515 k_timer_expiry_t expiry_fn,
1516 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001517
Allan Stephens45bfa372016-10-12 12:39:42 -05001518/**
1519 * @brief Start a timer.
1520 *
1521 * This routine starts a timer, and resets its status to zero. The timer
1522 * begins counting down using the specified duration and period values.
1523 *
1524 * Attempting to start a timer that is already running is permitted.
1525 * The timer's status is reset to zero and the timer begins counting down
1526 * using the new duration and period values.
1527 *
1528 * @param timer Address of timer.
1529 * @param duration Initial timer duration (in milliseconds).
1530 * @param period Timer period (in milliseconds).
1531 *
1532 * @return N/A
1533 */
Andrew Boiea354d492017-09-29 16:22:28 -07001534__syscall void k_timer_start(struct k_timer *timer,
1535 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001536
1537/**
1538 * @brief Stop a timer.
1539 *
1540 * This routine stops a running timer prematurely. The timer's stop function,
1541 * if one exists, is invoked by the caller.
1542 *
1543 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001544 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001545 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001546 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1547 * if @a k_timer_stop is to be called from ISRs.
1548 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001549 * @param timer Address of timer.
1550 *
1551 * @return N/A
1552 */
Andrew Boiea354d492017-09-29 16:22:28 -07001553__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001554
1555/**
1556 * @brief Read timer status.
1557 *
1558 * This routine reads the timer's status, which indicates the number of times
1559 * it has expired since its status was last read.
1560 *
1561 * Calling this routine resets the timer's status to zero.
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_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001568
1569/**
1570 * @brief Synchronize thread to timer expiration.
1571 *
1572 * This routine blocks the calling thread until the timer's status is non-zero
1573 * (indicating that it has expired at least once since it was last examined)
1574 * or the timer is stopped. If the timer status is already non-zero,
1575 * or the timer is already stopped, the caller continues without waiting.
1576 *
1577 * Calling this routine resets the timer's status to zero.
1578 *
1579 * This routine must not be used by interrupt handlers, since they are not
1580 * allowed to block.
1581 *
1582 * @param timer Address of timer.
1583 *
1584 * @return Timer status.
1585 */
Andrew Boiea354d492017-09-29 16:22:28 -07001586__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001587
1588/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001589 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001590 *
1591 * This routine computes the (approximate) time remaining before a running
1592 * timer next expires. If the timer is not running, it returns zero.
1593 *
1594 * @param timer Address of timer.
1595 *
1596 * @return Remaining time (in milliseconds).
1597 */
Andrew Boiea354d492017-09-29 16:22:28 -07001598__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1599
1600static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001601{
1602 return _timeout_remaining_get(&timer->timeout);
1603}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001604
Allan Stephensc98da842016-11-11 15:45:03 -05001605/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001606 * @brief Associate user-specific data with a timer.
1607 *
1608 * This routine records the @a user_data with the @a timer, to be retrieved
1609 * later.
1610 *
1611 * It can be used e.g. in a timer handler shared across multiple subsystems to
1612 * retrieve data specific to the subsystem this timer is associated with.
1613 *
1614 * @param timer Address of timer.
1615 * @param user_data User data to associate with the timer.
1616 *
1617 * @return N/A
1618 */
Andrew Boiea354d492017-09-29 16:22:28 -07001619__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1620
Anas Nashif954d5502018-02-25 08:37:28 -06001621/**
1622 * @internal
1623 */
Andrew Boiea354d492017-09-29 16:22:28 -07001624static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1625 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001626{
1627 timer->user_data = user_data;
1628}
1629
1630/**
1631 * @brief Retrieve the user-specific data from a timer.
1632 *
1633 * @param timer Address of timer.
1634 *
1635 * @return The user data.
1636 */
Andrew Boiea354d492017-09-29 16:22:28 -07001637__syscall void *k_timer_user_data_get(struct k_timer *timer);
1638
1639static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001640{
1641 return timer->user_data;
1642}
1643
Anas Nashif166f5192018-02-25 08:02:36 -06001644/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001645
Allan Stephensc98da842016-11-11 15:45:03 -05001646/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001647 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001648 * @{
1649 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001650
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001651/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001652 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001653 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001654 * This routine returns the elapsed time since the system booted,
1655 * in milliseconds.
1656 *
1657 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001658 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001659__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001660
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001661/**
1662 * @brief Enable clock always on in tickless kernel
1663 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001664 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001665 * there are no timer events programmed in tickless kernel
1666 * scheduling. This is necessary if the clock is used to track
1667 * passage of time.
1668 *
1669 * @retval prev_status Previous status of always on flag
1670 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301671#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001672static inline int k_enable_sys_clock_always_on(void)
1673{
1674 int prev_status = _sys_clock_always_on;
1675
1676 _sys_clock_always_on = 1;
1677 _enable_sys_clock();
1678
1679 return prev_status;
1680}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301681#else
1682#define k_enable_sys_clock_always_on() do { } while ((0))
1683#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001684
1685/**
1686 * @brief Disable clock always on in tickless kernel
1687 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001688 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001689 * there are no timer events programmed in tickless kernel
1690 * scheduling. To save power, this routine should be called
1691 * immediately when clock is not used to track time.
1692 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301693#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001694static inline void k_disable_sys_clock_always_on(void)
1695{
1696 _sys_clock_always_on = 0;
1697}
1698#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001699#define k_disable_sys_clock_always_on() do { } while ((0))
1700#endif
1701
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001702/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001703 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001705 * This routine returns the lower 32-bits of the elapsed time since the system
1706 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001708 * This routine can be more efficient than k_uptime_get(), as it reduces the
1709 * need for interrupt locking and 64-bit math. However, the 32-bit result
1710 * cannot hold a system uptime time larger than approximately 50 days, so the
1711 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001712 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001713 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001714 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001715__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001716
1717/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001718 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001719 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001720 * This routine computes the elapsed time between the current system uptime
1721 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001722 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001723 * @param reftime Pointer to a reference time, which is updated to the current
1724 * uptime upon return.
1725 *
1726 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001727 */
Kumar Galacc334c72017-04-21 10:55:34 -05001728extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001729
1730/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001731 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001732 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001733 * This routine computes the elapsed time between the current system uptime
1734 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001735 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001736 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1737 * need for interrupt locking and 64-bit math. However, the 32-bit result
1738 * cannot hold an elapsed time larger than approximately 50 days, so the
1739 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001741 * @param reftime Pointer to a reference time, which is updated to the current
1742 * uptime upon return.
1743 *
1744 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001745 */
Kumar Galacc334c72017-04-21 10:55:34 -05001746extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001747
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001748/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001749 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001751 * This routine returns the current time, as measured by the system's hardware
1752 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001753 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001754 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001755 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001756#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001757
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001758/**
Anas Nashif166f5192018-02-25 08:02:36 -06001759 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001760 */
1761
Allan Stephensc98da842016-11-11 15:45:03 -05001762/**
1763 * @cond INTERNAL_HIDDEN
1764 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001765
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001766struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001767 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001768 union {
1769 _wait_q_t wait_q;
1770
1771 _POLL_EVENT;
1772 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001773
1774 _OBJECT_TRACING_NEXT_PTR(k_queue);
1775};
1776
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001777#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001778 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001779 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001780 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001781 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001782 _OBJECT_TRACING_INIT \
1783 }
1784
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001785#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1786
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001787extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1788
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001789/**
1790 * INTERNAL_HIDDEN @endcond
1791 */
1792
1793/**
1794 * @defgroup queue_apis Queue APIs
1795 * @ingroup kernel_apis
1796 * @{
1797 */
1798
1799/**
1800 * @brief Initialize a queue.
1801 *
1802 * This routine initializes a queue object, prior to its first use.
1803 *
1804 * @param queue Address of the queue.
1805 *
1806 * @return N/A
1807 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001808__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001809
1810/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001811 * @brief Cancel waiting on a queue.
1812 *
1813 * This routine causes first thread pending on @a queue, if any, to
1814 * return from k_queue_get() call with NULL value (as if timeout expired).
1815 *
1816 * @note Can be called by ISRs.
1817 *
1818 * @param queue Address of the queue.
1819 *
1820 * @return N/A
1821 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001822__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001823
1824/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001825 * @brief Append an element to the end of a queue.
1826 *
1827 * This routine appends a data item to @a queue. A queue data item must be
1828 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1829 * reserved for the kernel's use.
1830 *
1831 * @note Can be called by ISRs.
1832 *
1833 * @param queue Address of the queue.
1834 * @param data Address of the data item.
1835 *
1836 * @return N/A
1837 */
1838extern void k_queue_append(struct k_queue *queue, void *data);
1839
1840/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001841 * @brief Append an element to a queue.
1842 *
1843 * This routine appends a data item to @a queue. There is an implicit
1844 * memory allocation from the calling thread's resource pool, which is
1845 * automatically freed when the item is removed from the queue.
1846 *
1847 * @note Can be called by ISRs.
1848 *
1849 * @param queue Address of the queue.
1850 * @param data Address of the data item.
1851 *
1852 * @retval 0 on success
1853 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1854 */
1855__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1856
1857/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001858 * @brief Prepend an element to a queue.
1859 *
1860 * This routine prepends a data item to @a queue. A queue data item must be
1861 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1862 * reserved for the kernel's use.
1863 *
1864 * @note Can be called by ISRs.
1865 *
1866 * @param queue Address of the queue.
1867 * @param data Address of the data item.
1868 *
1869 * @return N/A
1870 */
1871extern void k_queue_prepend(struct k_queue *queue, void *data);
1872
1873/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001874 * @brief Prepend an element to a queue.
1875 *
1876 * This routine prepends a data item to @a queue. There is an implicit
1877 * memory allocation from the calling thread's resource pool, which is
1878 * automatically freed when the item is removed from the queue.
1879 *
1880 * @note Can be called by ISRs.
1881 *
1882 * @param queue Address of the queue.
1883 * @param data Address of the data item.
1884 *
1885 * @retval 0 on success
1886 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1887 */
1888__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1889
1890/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001891 * @brief Inserts an element to a queue.
1892 *
1893 * This routine inserts a data item to @a queue after previous item. A queue
1894 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1895 * item are reserved for the kernel's use.
1896 *
1897 * @note Can be called by ISRs.
1898 *
1899 * @param queue Address of the queue.
1900 * @param prev Address of the previous data item.
1901 * @param data Address of the data item.
1902 *
1903 * @return N/A
1904 */
1905extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1906
1907/**
1908 * @brief Atomically append a list of elements to a queue.
1909 *
1910 * This routine adds a list of data items to @a queue in one operation.
1911 * The data items must be in a singly-linked list, with the first 32 bits
1912 * in each data item pointing to the next data item; the list must be
1913 * NULL-terminated.
1914 *
1915 * @note Can be called by ISRs.
1916 *
1917 * @param queue Address of the queue.
1918 * @param head Pointer to first node in singly-linked list.
1919 * @param tail Pointer to last node in singly-linked list.
1920 *
1921 * @return N/A
1922 */
1923extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1924
1925/**
1926 * @brief Atomically add a list of elements to a queue.
1927 *
1928 * This routine adds a list of data items to @a queue in one operation.
1929 * The data items must be in a singly-linked list implemented using a
1930 * sys_slist_t object. Upon completion, the original list is empty.
1931 *
1932 * @note Can be called by ISRs.
1933 *
1934 * @param queue Address of the queue.
1935 * @param list Pointer to sys_slist_t object.
1936 *
1937 * @return N/A
1938 */
1939extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1940
1941/**
1942 * @brief Get an element from a queue.
1943 *
1944 * This routine removes first data item from @a queue. The first 32 bits of the
1945 * data item are reserved for the kernel's use.
1946 *
1947 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1948 *
1949 * @param queue Address of the queue.
1950 * @param timeout Waiting period to obtain a data item (in milliseconds),
1951 * or one of the special values K_NO_WAIT and K_FOREVER.
1952 *
1953 * @return Address of the data item if successful; NULL if returned
1954 * without waiting, or waiting period timed out.
1955 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001956__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001957
1958/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001959 * @brief Remove an element from a queue.
1960 *
1961 * This routine removes data item from @a queue. The first 32 bits of the
1962 * data item are reserved for the kernel's use. Removing elements from k_queue
1963 * rely on sys_slist_find_and_remove which is not a constant time operation.
1964 *
1965 * @note Can be called by ISRs
1966 *
1967 * @param queue Address of the queue.
1968 * @param data Address of the data item.
1969 *
1970 * @return true if data item was removed
1971 */
1972static inline bool k_queue_remove(struct k_queue *queue, void *data)
1973{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001974 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001975}
1976
1977/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001978 * @brief Query a queue to see if it has data available.
1979 *
1980 * Note that the data might be already gone by the time this function returns
1981 * if other threads are also trying to read from the queue.
1982 *
1983 * @note Can be called by ISRs.
1984 *
1985 * @param queue Address of the queue.
1986 *
1987 * @return Non-zero if the queue is empty.
1988 * @return 0 if data is available.
1989 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001990__syscall int k_queue_is_empty(struct k_queue *queue);
1991
1992static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001993{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001994 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001995}
1996
1997/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001998 * @brief Peek element at the head of queue.
1999 *
2000 * Return element from the head of queue without removing it.
2001 *
2002 * @param queue Address of the queue.
2003 *
2004 * @return Head element, or NULL if queue is empty.
2005 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002006__syscall void *k_queue_peek_head(struct k_queue *queue);
2007
2008static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002009{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002010 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002011}
2012
2013/**
2014 * @brief Peek element at the tail of queue.
2015 *
2016 * Return element from the tail of queue without removing it.
2017 *
2018 * @param queue Address of the queue.
2019 *
2020 * @return Tail element, or NULL if queue is empty.
2021 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002022__syscall void *k_queue_peek_tail(struct k_queue *queue);
2023
2024static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002025{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002026 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002027}
2028
2029/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002030 * @brief Statically define and initialize a queue.
2031 *
2032 * The queue can be accessed outside the module where it is defined using:
2033 *
2034 * @code extern struct k_queue <name>; @endcode
2035 *
2036 * @param name Name of the queue.
2037 */
2038#define K_QUEUE_DEFINE(name) \
2039 struct k_queue name \
2040 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002041 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002042
Anas Nashif166f5192018-02-25 08:02:36 -06002043/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002044
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002045struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002046 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002047};
2048
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002049/**
2050 * @cond INTERNAL_HIDDEN
2051 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002052#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002053 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002054 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002055 }
2056
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002057#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
2058
Allan Stephensc98da842016-11-11 15:45:03 -05002059/**
2060 * INTERNAL_HIDDEN @endcond
2061 */
2062
2063/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002064 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002065 * @ingroup kernel_apis
2066 * @{
2067 */
2068
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002069/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002070 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002071 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002072 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002073 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002074 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002075 *
2076 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002077 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002078 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002079#define k_fifo_init(fifo) \
2080 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002081
2082/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002083 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002084 *
2085 * This routine causes first thread pending on @a fifo, if any, to
2086 * return from k_fifo_get() call with NULL value (as if timeout
2087 * expired).
2088 *
2089 * @note Can be called by ISRs.
2090 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002091 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002092 *
2093 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002094 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002095 */
2096#define k_fifo_cancel_wait(fifo) \
2097 k_queue_cancel_wait((struct k_queue *) fifo)
2098
2099/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002100 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002101 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002102 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002103 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2104 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002105 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002106 * @note Can be called by ISRs.
2107 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002108 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002109 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002110 *
2111 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002112 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002113 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002114#define k_fifo_put(fifo, data) \
2115 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002116
2117/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002118 * @brief Add an element to a FIFO queue.
2119 *
2120 * This routine adds a data item to @a fifo. There is an implicit
2121 * memory allocation from the calling thread's resource pool, which is
2122 * automatically freed when the item is removed.
2123 *
2124 * @note Can be called by ISRs.
2125 *
2126 * @param fifo Address of the FIFO.
2127 * @param data Address of the data item.
2128 *
2129 * @retval 0 on success
2130 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002131 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002132 */
2133#define k_fifo_alloc_put(fifo, data) \
2134 k_queue_alloc_append((struct k_queue *) fifo, data)
2135
2136/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002137 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002139 * This routine adds a list of data items to @a fifo in one operation.
2140 * The data items must be in a singly-linked list, with the first 32 bits
2141 * each data item pointing to the next data item; the list must be
2142 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002144 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002145 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002146 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002147 * @param head Pointer to first node in singly-linked list.
2148 * @param tail Pointer to last node in singly-linked list.
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_list(fifo, head, tail) \
2154 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002155
2156/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002157 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002158 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002159 * This routine adds a list of data items to @a fifo in one operation.
2160 * The data items must be in a singly-linked list implemented using a
2161 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002162 * and must be re-initialized via sys_slist_init().
2163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002164 * @note Can be called by ISRs.
2165 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002166 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002167 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002168 *
2169 * @return N/A
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_put_slist(fifo, list) \
2173 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002174
2175/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002176 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002177 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002178 * This routine removes a data item from @a fifo in a "first in, first out"
2179 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002181 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2182 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002183 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002184 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002185 * or one of the special values K_NO_WAIT and K_FOREVER.
2186 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002187 * @return Address of the data item if successful; NULL if returned
2188 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002189 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002190 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002191#define k_fifo_get(fifo, timeout) \
2192 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002193
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002194/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002195 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002196 *
2197 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002198 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002199 *
2200 * @note Can be called by ISRs.
2201 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002202 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002203 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002204 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002205 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002206 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002207 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002208#define k_fifo_is_empty(fifo) \
2209 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002210
2211/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002212 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002213 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002214 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302215 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002216 * on each iteration of processing, a head container will be peeked,
2217 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002218 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002219 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002220 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002221 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002222 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002223 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002224 */
2225#define k_fifo_peek_head(fifo) \
2226 k_queue_peek_head((struct k_queue *) fifo)
2227
2228/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002229 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002230 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002231 * Return element from the tail of FIFO queue (without removing it). A usecase
2232 * for this is if elements of the FIFO queue are themselves containers. Then
2233 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002234 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002235 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002236 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002237 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002238 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002239 */
2240#define k_fifo_peek_tail(fifo) \
2241 k_queue_peek_tail((struct k_queue *) fifo)
2242
2243/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002244 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002245 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002246 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002247 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002248 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002249 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002250 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002251 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002252 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002253#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002254 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002255 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002256 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002257
Anas Nashif166f5192018-02-25 08:02:36 -06002258/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002259
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002260struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002261 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002262};
2263
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002264/**
2265 * @cond INTERNAL_HIDDEN
2266 */
2267
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002268#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002269 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002270 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002271 }
2272
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002273#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2274
Allan Stephensc98da842016-11-11 15:45:03 -05002275/**
2276 * INTERNAL_HIDDEN @endcond
2277 */
2278
2279/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002280 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002281 * @ingroup kernel_apis
2282 * @{
2283 */
2284
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002285/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002286 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002287 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002288 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002289 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002290 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002291 *
2292 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002293 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002294 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002295#define k_lifo_init(lifo) \
2296 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002297
2298/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002299 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002300 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002301 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002302 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2303 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002304 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002305 * @note Can be called by ISRs.
2306 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002307 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002308 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002309 *
2310 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002311 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002312 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002313#define k_lifo_put(lifo, data) \
2314 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002315
2316/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002317 * @brief Add an element to a LIFO queue.
2318 *
2319 * This routine adds a data item to @a lifo. There is an implicit
2320 * memory allocation from the calling thread's resource pool, which is
2321 * automatically freed when the item is removed.
2322 *
2323 * @note Can be called by ISRs.
2324 *
2325 * @param lifo Address of the LIFO.
2326 * @param data Address of the data item.
2327 *
2328 * @retval 0 on success
2329 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002330 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002331 */
2332#define k_lifo_alloc_put(lifo, data) \
2333 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2334
2335/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002336 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002337 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002338 * This routine removes a data item from @a lifo in a "last in, first out"
2339 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002340 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002341 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2342 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002343 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002344 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002345 * or one of the special values K_NO_WAIT and K_FOREVER.
2346 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002347 * @return Address of the data item if successful; NULL if returned
2348 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002349 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002350 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002351#define k_lifo_get(lifo, timeout) \
2352 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002353
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002354/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002355 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002356 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002357 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002358 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002359 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002360 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002361 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002362 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002363 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002364#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002365 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002366 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002367 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002368
Anas Nashif166f5192018-02-25 08:02:36 -06002369/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002370
2371/**
2372 * @cond INTERNAL_HIDDEN
2373 */
Andrew Boief3bee952018-05-02 17:44:39 -07002374#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002375
2376struct k_stack {
2377 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002378 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002379
Anas Nashif2f203c22016-12-18 06:57:45 -05002380 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002381 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002382};
2383
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002384#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002385 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002386 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002387 .base = stack_buffer, \
2388 .next = stack_buffer, \
2389 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002390 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002391 }
2392
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002393#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2394
Allan Stephensc98da842016-11-11 15:45:03 -05002395/**
2396 * INTERNAL_HIDDEN @endcond
2397 */
2398
2399/**
2400 * @defgroup stack_apis Stack APIs
2401 * @ingroup kernel_apis
2402 * @{
2403 */
2404
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002405/**
2406 * @brief Initialize a stack.
2407 *
2408 * This routine initializes a stack object, prior to its first use.
2409 *
2410 * @param stack Address of the stack.
2411 * @param buffer Address of array used to hold stacked values.
2412 * @param num_entries Maximum number of values that can be stacked.
2413 *
2414 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002415 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002416 */
Andrew Boief3bee952018-05-02 17:44:39 -07002417void k_stack_init(struct k_stack *stack,
2418 u32_t *buffer, unsigned int num_entries);
2419
2420
2421/**
2422 * @brief Initialize a stack.
2423 *
2424 * This routine initializes a stack object, prior to its first use. Internal
2425 * buffers will be allocated from the calling thread's resource pool.
2426 * This memory will be released if k_stack_cleanup() is called, or
2427 * userspace is enabled and the stack object loses all references to it.
2428 *
2429 * @param stack Address of the stack.
2430 * @param num_entries Maximum number of values that can be stacked.
2431 *
2432 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002433 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002434 */
2435
2436__syscall int k_stack_alloc_init(struct k_stack *stack,
2437 unsigned int num_entries);
2438
2439/**
2440 * @brief Release a stack's allocated buffer
2441 *
2442 * If a stack object was given a dynamically allocated buffer via
2443 * k_stack_alloc_init(), this will free it. This function does nothing
2444 * if the buffer wasn't dynamically allocated.
2445 *
2446 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002447 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002448 */
2449void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002450
2451/**
2452 * @brief Push an element onto a stack.
2453 *
2454 * This routine adds a 32-bit value @a data to @a stack.
2455 *
2456 * @note Can be called by ISRs.
2457 *
2458 * @param stack Address of the stack.
2459 * @param data Value to push onto the stack.
2460 *
2461 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002462 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002463 */
Andrew Boiee8734462017-09-29 16:42:07 -07002464__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002465
2466/**
2467 * @brief Pop an element from a stack.
2468 *
2469 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2470 * manner and stores the value in @a data.
2471 *
2472 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2473 *
2474 * @param stack Address of the stack.
2475 * @param data Address of area to hold the value popped from the stack.
2476 * @param timeout Waiting period to obtain a value (in milliseconds),
2477 * or one of the special values K_NO_WAIT and K_FOREVER.
2478 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002479 * @retval 0 Element popped from stack.
2480 * @retval -EBUSY Returned without waiting.
2481 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002482 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002483 */
Andrew Boiee8734462017-09-29 16:42:07 -07002484__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002485
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002486/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002487 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002489 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002490 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002491 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002492 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002493 * @param name Name of the stack.
2494 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002495 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002496 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002497#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002498 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002499 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002500 struct k_stack name \
2501 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002502 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002503 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002504
Anas Nashif166f5192018-02-25 08:02:36 -06002505/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002506
Allan Stephens6bba9b02016-11-16 14:56:54 -05002507struct k_work;
2508
Allan Stephensc98da842016-11-11 15:45:03 -05002509/**
2510 * @defgroup workqueue_apis Workqueue Thread APIs
2511 * @ingroup kernel_apis
2512 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002513 */
2514
Allan Stephens6bba9b02016-11-16 14:56:54 -05002515/**
2516 * @typedef k_work_handler_t
2517 * @brief Work item handler function type.
2518 *
2519 * A work item's handler function is executed by a workqueue's thread
2520 * when the work item is processed by the workqueue.
2521 *
2522 * @param work Address of the work item.
2523 *
2524 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002525 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002526 */
2527typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002528
2529/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002530 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002531 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002532
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002533struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002534 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002535 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002536};
2537
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002538enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002539 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002540};
2541
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002542struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002543 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002544 k_work_handler_t handler;
2545 atomic_t flags[1];
2546};
2547
Allan Stephens6bba9b02016-11-16 14:56:54 -05002548struct k_delayed_work {
2549 struct k_work work;
2550 struct _timeout timeout;
2551 struct k_work_q *work_q;
2552};
2553
2554extern struct k_work_q k_sys_work_q;
2555
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002557 * INTERNAL_HIDDEN @endcond
2558 */
2559
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002560#define _K_WORK_INITIALIZER(work_handler) \
2561 { \
2562 ._reserved = NULL, \
2563 .handler = work_handler, \
2564 .flags = { 0 } \
2565 }
2566
2567#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2568
Allan Stephens6bba9b02016-11-16 14:56:54 -05002569/**
2570 * @brief Initialize a statically-defined work item.
2571 *
2572 * This macro can be used to initialize a statically-defined workqueue work
2573 * item, prior to its first use. For example,
2574 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002575 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002576 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002577 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002578 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002579 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002580 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002581#define K_WORK_DEFINE(work, work_handler) \
2582 struct k_work work \
2583 __in_section(_k_work, static, work) = \
2584 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002585
2586/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002587 * @brief Initialize a work item.
2588 *
2589 * This routine initializes a workqueue work item, prior to its first use.
2590 *
2591 * @param work Address of work item.
2592 * @param handler Function to invoke each time work item is processed.
2593 *
2594 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002595 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002596 */
2597static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2598{
Leandro Pereira0e23ad82018-05-29 10:42:17 -07002599 *work = (struct k_work)_K_WORK_INITIALIZER(handler);
Andrew Boie945af952017-08-22 13:15:23 -07002600 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002601}
2602
2603/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002604 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002605 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002606 * This routine submits work item @a work to be processed by workqueue
2607 * @a work_q. If the work item is already pending in the workqueue's queue
2608 * as a result of an earlier submission, this routine has no effect on the
2609 * work item. If the work item has already been processed, or is currently
2610 * being processed, its work is considered complete and the work item can be
2611 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002612 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002613 * @warning
2614 * A submitted work item must not be modified until it has been processed
2615 * by the workqueue.
2616 *
2617 * @note Can be called by ISRs.
2618 *
2619 * @param work_q Address of workqueue.
2620 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002621 *
2622 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002623 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002624 */
2625static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2626 struct k_work *work)
2627{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002628 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002629 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002630 }
2631}
2632
2633/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002634 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002635 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002636 * This routine indicates if work item @a work is pending in a workqueue's
2637 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002638 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002639 * @note Can be called by ISRs.
2640 *
2641 * @param work Address of work item.
2642 *
2643 * @return 1 if work item is pending, or 0 if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002644 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002645 */
2646static inline int k_work_pending(struct k_work *work)
2647{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002648 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002649}
2650
2651/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002652 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002653 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002654 * This routine starts workqueue @a work_q. The workqueue spawns its work
2655 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002656 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002657 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002658 * @param stack Pointer to work queue thread's stack space, as defined by
2659 * K_THREAD_STACK_DEFINE()
2660 * @param stack_size Size of the work queue thread's stack (in bytes), which
2661 * should either be the same constant passed to
2662 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002663 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002664 *
2665 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002666 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002667 */
Andrew Boie507852a2017-07-25 18:47:07 -07002668extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002669 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002670 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002671
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002672/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002673 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002674 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002675 * This routine initializes a workqueue delayed work item, prior to
2676 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002677 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002678 * @param work Address of delayed work item.
2679 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002680 *
2681 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002682 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002683 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002684extern void k_delayed_work_init(struct k_delayed_work *work,
2685 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002686
2687/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002688 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002689 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002690 * This routine schedules work item @a work to be processed by workqueue
2691 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002692 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002693 * Only when the countdown completes is the work item actually submitted to
2694 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002695 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002696 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002697 * counting down cancels the existing submission and restarts the
2698 * countdown using the new delay. Note that this behavior is
2699 * inherently subject to race conditions with the pre-existing
2700 * timeouts and work queue, so care must be taken to synchronize such
2701 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002702 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002703 * @warning
2704 * A delayed work item must not be modified until it has been processed
2705 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002706 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002707 * @note Can be called by ISRs.
2708 *
2709 * @param work_q Address of workqueue.
2710 * @param work Address of delayed work item.
2711 * @param delay Delay before submitting the work item (in milliseconds).
2712 *
2713 * @retval 0 Work item countdown started.
2714 * @retval -EINPROGRESS Work item is already pending.
2715 * @retval -EINVAL Work item is being processed or has completed its work.
2716 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002717 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002718 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002719extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2720 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002721 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002722
2723/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002724 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002725 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002726 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002727 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002728 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002729 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002730 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002731 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002732 * @param work Address of delayed work item.
2733 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002734 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002735 * @retval -EINPROGRESS Work item is already pending.
2736 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002737 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002738 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002739extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002740
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002741/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002742 * @brief Submit a work item to the system workqueue.
2743 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002744 * This routine submits work item @a work to be processed by the system
2745 * workqueue. If the work item is already pending in the workqueue's queue
2746 * as a result of an earlier submission, this routine has no effect on the
2747 * work item. If the work item has already been processed, or is currently
2748 * being processed, its work is considered complete and the work item can be
2749 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002750 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002751 * @warning
2752 * Work items submitted to the system workqueue should avoid using handlers
2753 * that block or yield since this may prevent the system workqueue from
2754 * processing other work items in a timely manner.
2755 *
2756 * @note Can be called by ISRs.
2757 *
2758 * @param work Address of work item.
2759 *
2760 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002761 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002762 */
2763static inline void k_work_submit(struct k_work *work)
2764{
2765 k_work_submit_to_queue(&k_sys_work_q, work);
2766}
2767
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002768/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002769 * @brief Submit a delayed work item to the system workqueue.
2770 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002771 * This routine schedules work item @a work to be processed by the system
2772 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002773 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002774 * Only when the countdown completes is the work item actually submitted to
2775 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002776 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002777 * Submitting a previously submitted delayed work item that is still
2778 * counting down cancels the existing submission and restarts the countdown
2779 * using the new delay. If the work item is currently pending on the
2780 * workqueue's queue because the countdown has completed it is too late to
2781 * resubmit the item, and resubmission fails without impacting the work item.
2782 * If the work item has already been processed, or is currently being processed,
2783 * its work is considered complete and the work item can be resubmitted.
2784 *
2785 * @warning
2786 * Work items submitted to the system workqueue should avoid using handlers
2787 * that block or yield since this may prevent the system workqueue from
2788 * processing other work items in a timely manner.
2789 *
2790 * @note Can be called by ISRs.
2791 *
2792 * @param work Address of delayed work item.
2793 * @param delay Delay before submitting the work item (in milliseconds).
2794 *
2795 * @retval 0 Work item countdown started.
2796 * @retval -EINPROGRESS Work item is already pending.
2797 * @retval -EINVAL Work item is being processed or has completed its work.
2798 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002799 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002800 */
2801static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002802 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002803{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002804 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002805}
2806
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002807/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002808 * @brief Get time remaining before a delayed work gets scheduled.
2809 *
2810 * This routine computes the (approximate) time remaining before a
2811 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002812 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002813 *
2814 * @param work Delayed work item.
2815 *
2816 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002817 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002818 */
Kumar Galacc334c72017-04-21 10:55:34 -05002819static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002820{
2821 return _timeout_remaining_get(&work->timeout);
2822}
2823
Anas Nashif166f5192018-02-25 08:02:36 -06002824/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002825/**
Anas Nashifce78d162018-05-24 12:43:11 -05002826 * @defgroup mutex_apis Mutex APIs
2827 * @ingroup kernel_apis
2828 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002829 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002830
Anas Nashifce78d162018-05-24 12:43:11 -05002831/**
2832 * Mutex Structure
2833 * @ingroup mutex_apis
2834 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002835struct k_mutex {
2836 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002837 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002838 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002839 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002840 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002841
Anas Nashif2f203c22016-12-18 06:57:45 -05002842 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002843};
2844
Anas Nashifce78d162018-05-24 12:43:11 -05002845/**
2846 * @cond INTERNAL_HIDDEN
2847 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002848#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002849 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002850 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002851 .owner = NULL, \
2852 .lock_count = 0, \
2853 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002854 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002855 }
2856
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002857#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2858
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002859/**
Allan Stephensc98da842016-11-11 15:45:03 -05002860 * INTERNAL_HIDDEN @endcond
2861 */
2862
2863/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002864 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002866 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002867 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002868 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002870 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002871 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002872 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002873#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002874 struct k_mutex name \
2875 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002876 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002877
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002878/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002879 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002881 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002883 * Upon completion, the mutex is available and does not have an owner.
2884 *
2885 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002886 *
2887 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002888 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002889 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002890__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002891
2892/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002893 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002894 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002895 * This routine locks @a mutex. If the mutex is locked by another thread,
2896 * the calling thread waits until the mutex becomes available or until
2897 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002898 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002899 * A thread is permitted to lock a mutex it has already locked. The operation
2900 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002902 * @param mutex Address of the mutex.
2903 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002904 * or one of the special values K_NO_WAIT and K_FOREVER.
2905 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002906 * @retval 0 Mutex locked.
2907 * @retval -EBUSY Returned without waiting.
2908 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002909 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002910 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002911__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002912
2913/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002914 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002915 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002916 * This routine unlocks @a mutex. The mutex must already be locked by the
2917 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002918 *
2919 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002920 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002921 * thread.
2922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002923 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002924 *
2925 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002926 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002927 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002928__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002929
Allan Stephensc98da842016-11-11 15:45:03 -05002930/**
Anas Nashif166f5192018-02-25 08:02:36 -06002931 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002932 */
2933
2934/**
2935 * @cond INTERNAL_HIDDEN
2936 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002937
2938struct k_sem {
2939 _wait_q_t wait_q;
2940 unsigned int count;
2941 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002942 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002943
Anas Nashif2f203c22016-12-18 06:57:45 -05002944 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002945};
2946
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002947#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002948 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002949 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002950 .count = initial_count, \
2951 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002952 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002953 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002954 }
2955
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002956#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2957
Allan Stephensc98da842016-11-11 15:45:03 -05002958/**
2959 * INTERNAL_HIDDEN @endcond
2960 */
2961
2962/**
2963 * @defgroup semaphore_apis Semaphore APIs
2964 * @ingroup kernel_apis
2965 * @{
2966 */
2967
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002968/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002969 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002971 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002973 * @param sem Address of the semaphore.
2974 * @param initial_count Initial semaphore count.
2975 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002976 *
2977 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002978 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002979 */
Andrew Boie99280232017-09-29 14:17:47 -07002980__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2981 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002982
2983/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002984 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002986 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002987 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002988 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2989 *
2990 * @param sem Address of the semaphore.
2991 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002992 * or one of the special values K_NO_WAIT and K_FOREVER.
2993 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002994 * @note When porting code from the nanokernel legacy API to the new API, be
2995 * careful with the return value of this function. The return value is the
2996 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2997 * non-zero means failure, while the nano_sem_take family returns 1 for success
2998 * and 0 for failure.
2999 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003000 * @retval 0 Semaphore taken.
3001 * @retval -EBUSY Returned without waiting.
3002 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003003 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003004 */
Andrew Boie99280232017-09-29 14:17:47 -07003005__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003006
3007/**
3008 * @brief Give a semaphore.
3009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003010 * This routine gives @a sem, unless the semaphore is already at its maximum
3011 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003013 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003015 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003016 *
3017 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003018 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003019 */
Andrew Boie99280232017-09-29 14:17:47 -07003020__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003021
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003022/**
3023 * @brief Reset a semaphore's count to zero.
3024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003025 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003026 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003027 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003028 *
3029 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003030 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003031 */
Andrew Boie990bf162017-10-03 12:36:49 -07003032__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003033
Anas Nashif954d5502018-02-25 08:37:28 -06003034/**
3035 * @internal
3036 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003037static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003038{
3039 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003040}
3041
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003042/**
3043 * @brief Get a semaphore's count.
3044 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003045 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003046 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003047 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003048 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003049 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003050 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003051 */
Andrew Boie990bf162017-10-03 12:36:49 -07003052__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003053
Anas Nashif954d5502018-02-25 08:37:28 -06003054/**
3055 * @internal
3056 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003057static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003058{
3059 return sem->count;
3060}
3061
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003062/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003063 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003064 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003065 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003066 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003067 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003069 * @param name Name of the semaphore.
3070 * @param initial_count Initial semaphore count.
3071 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003072 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003073 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003074#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003075 struct k_sem name \
3076 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003077 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303078 BUILD_ASSERT(((count_limit) != 0) && \
3079 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003080
Anas Nashif166f5192018-02-25 08:02:36 -06003081/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003082
3083/**
3084 * @defgroup alert_apis Alert APIs
3085 * @ingroup kernel_apis
3086 * @{
3087 */
3088
Allan Stephens5eceb852016-11-16 10:16:30 -05003089/**
3090 * @typedef k_alert_handler_t
3091 * @brief Alert handler function type.
3092 *
3093 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07003094 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05003095 * and is only invoked if the alert has been initialized with one.
3096 *
3097 * @param alert Address of the alert.
3098 *
3099 * @return 0 if alert has been consumed; non-zero if alert should pend.
3100 */
3101typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05003102
Anas Nashif166f5192018-02-25 08:02:36 -06003103/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003104
3105/**
3106 * @cond INTERNAL_HIDDEN
3107 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003108
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003109#define K_ALERT_DEFAULT NULL
3110#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003111
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003112struct k_alert {
3113 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003114 atomic_t send_count;
3115 struct k_work work_item;
3116 struct k_sem sem;
3117
Anas Nashif2f203c22016-12-18 06:57:45 -05003118 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003119};
3120
Anas Nashif954d5502018-02-25 08:37:28 -06003121/**
3122 * @internal
3123 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003124extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003125
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003126#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003127 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003128 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003129 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003130 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
3131 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003132 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003133 }
3134
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003135#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
3136
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003137/**
Allan Stephensc98da842016-11-11 15:45:03 -05003138 * INTERNAL_HIDDEN @endcond
3139 */
3140
3141/**
3142 * @addtogroup alert_apis
3143 * @{
3144 */
3145
3146/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003147 * @def K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts)
3148 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003149 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003150 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003151 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003152 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003153 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003155 * @param name Name of the alert.
3156 * @param alert_handler Action to take when alert is sent. Specify either
3157 * the address of a function to be invoked by the system workqueue
3158 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
3159 * K_ALERT_DEFAULT (which causes the alert to pend).
3160 * @param max_num_pending_alerts Maximum number of pending alerts.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003161 *
3162 * @req K-ALERT-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003163 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003164#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003165 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003166 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003167 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003168 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003169
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003170/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003171 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003172 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003173 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003174 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003175 * @param alert Address of the alert.
3176 * @param handler Action to take when alert is sent. Specify either the address
3177 * of a function to be invoked by the system workqueue thread,
3178 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3179 * K_ALERT_DEFAULT (which causes the alert to pend).
3180 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003181 *
3182 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003183 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003184 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003185extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3186 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003187
3188/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003189 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003190 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003191 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003192 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003193 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3194 *
3195 * @param alert Address of the alert.
3196 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003197 * or one of the special values K_NO_WAIT and K_FOREVER.
3198 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003199 * @retval 0 Alert received.
3200 * @retval -EBUSY Returned without waiting.
3201 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003202 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003203 */
Andrew Boie310e9872017-09-29 04:41:15 -07003204__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003205
3206/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003207 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003208 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003209 * This routine signals @a alert. The action specified for @a alert will
3210 * be taken, which may trigger the execution of an alert handler function
3211 * and/or cause the alert to pend (assuming the alert has not reached its
3212 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003213 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003214 * @note Can be called by ISRs.
3215 *
3216 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003217 *
3218 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003219 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003220 */
Andrew Boie310e9872017-09-29 04:41:15 -07003221__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003222
3223/**
Anas Nashif166f5192018-02-25 08:02:36 -06003224 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003225 */
3226
Allan Stephensc98da842016-11-11 15:45:03 -05003227/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003228 * @defgroup msgq_apis Message Queue APIs
3229 * @ingroup kernel_apis
3230 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003231 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003232
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003233/**
3234 * @brief Message Queue Structure
3235 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003236struct k_msgq {
3237 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003238 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003239 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003240 char *buffer_start;
3241 char *buffer_end;
3242 char *read_ptr;
3243 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003244 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003245
Anas Nashif2f203c22016-12-18 06:57:45 -05003246 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003247 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003248};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003249/**
3250 * @cond INTERNAL_HIDDEN
3251 */
3252
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003253
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003254#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003255 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003256 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003257 .max_msgs = q_max_msgs, \
3258 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003259 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003260 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003261 .read_ptr = q_buffer, \
3262 .write_ptr = q_buffer, \
3263 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003264 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003265 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003266#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003267/**
3268 * INTERNAL_HIDDEN @endcond
3269 */
3270
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003271
Andrew Boie0fe789f2018-04-12 18:35:56 -07003272#define K_MSGQ_FLAG_ALLOC BIT(0)
3273
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003274/**
3275 * @brief Message Queue Attributes
3276 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303277struct k_msgq_attrs {
3278 size_t msg_size;
3279 u32_t max_msgs;
3280 u32_t used_msgs;
3281};
3282
Allan Stephensc98da842016-11-11 15:45:03 -05003283
3284/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003285 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003286 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003287 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3288 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003289 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3290 * message is similarly aligned to this boundary, @a q_msg_size must also be
3291 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003293 * The message queue can be accessed outside the module where it is defined
3294 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003295 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003296 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003297 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003298 * @param q_name Name of the message queue.
3299 * @param q_msg_size Message size (in bytes).
3300 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003301 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003302 *
3303 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003304 */
3305#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003306 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003307 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003308 struct k_msgq q_name \
3309 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003310 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003311 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003312
Peter Mitsisd7a37502016-10-13 11:37:40 -04003313/**
3314 * @brief Initialize a message queue.
3315 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003316 * This routine initializes a message queue object, prior to its first use.
3317 *
Allan Stephensda827222016-11-09 14:23:58 -06003318 * The message queue's ring buffer must contain space for @a max_msgs messages,
3319 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3320 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3321 * that each message is similarly aligned to this boundary, @a q_msg_size
3322 * must also be a multiple of N.
3323 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003324 * @param q Address of the message queue.
3325 * @param buffer Pointer to ring buffer that holds queued messages.
3326 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003327 * @param max_msgs Maximum number of messages that can be queued.
3328 *
3329 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003330 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003331 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003332void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3333 u32_t max_msgs);
3334
3335/**
3336 * @brief Initialize a message queue.
3337 *
3338 * This routine initializes a message queue object, prior to its first use,
3339 * allocating its internal ring buffer from the calling thread's resource
3340 * pool.
3341 *
3342 * Memory allocated for the ring buffer can be released by calling
3343 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3344 * all of its references.
3345 *
3346 * @param q Address of the message queue.
3347 * @param msg_size Message size (in bytes).
3348 * @param max_msgs Maximum number of messages that can be queued.
3349 *
3350 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3351 * thread's resource pool, or -EINVAL if the size parameters cause
3352 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003353 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003354 */
3355__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3356 u32_t max_msgs);
3357
3358
3359void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003360
3361/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003362 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003363 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003364 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003365 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003366 * @note Can be called by ISRs.
3367 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003368 * @param q Address of the message queue.
3369 * @param data Pointer to the message.
3370 * @param timeout Waiting period to add the message (in milliseconds),
3371 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003372 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003373 * @retval 0 Message sent.
3374 * @retval -ENOMSG Returned without waiting or queue purged.
3375 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003376 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003377 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003378__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003379
3380/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003381 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003383 * This routine receives a message from message queue @a q in a "first in,
3384 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003385 *
Allan Stephensc98da842016-11-11 15:45:03 -05003386 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003388 * @param q Address of the message queue.
3389 * @param data Address of area to hold the received message.
3390 * @param timeout Waiting period to receive the message (in milliseconds),
3391 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003392 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003393 * @retval 0 Message received.
3394 * @retval -ENOMSG Returned without waiting.
3395 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003396 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003397 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003398__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003399
3400/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003401 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003403 * This routine discards all unreceived messages in a message queue's ring
3404 * buffer. Any threads that are blocked waiting to send a message to the
3405 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003407 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003408 *
3409 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003410 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003411 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003412__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003413
Peter Mitsis67be2492016-10-07 11:44:34 -04003414/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003415 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003416 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003417 * This routine returns the number of unused entries in a message queue's
3418 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003419 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003420 * @param q Address of the message queue.
3421 *
3422 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003423 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003424 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003425__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3426
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303427/**
3428 * @brief Get basic attributes of a message queue.
3429 *
3430 * This routine fetches basic attributes of message queue into attr argument.
3431 *
3432 * @param q Address of the message queue.
3433 * @param attrs pointer to message queue attribute structure.
3434 *
3435 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003436 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303437 */
3438__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3439
3440
Andrew Boie82edb6e2017-10-02 10:53:06 -07003441static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003442{
3443 return q->max_msgs - q->used_msgs;
3444}
3445
Peter Mitsisd7a37502016-10-13 11:37:40 -04003446/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003447 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003448 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003449 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003450 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003451 * @param q Address of the message queue.
3452 *
3453 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003454 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003455 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003456__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3457
3458static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003459{
3460 return q->used_msgs;
3461}
3462
Anas Nashif166f5192018-02-25 08:02:36 -06003463/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003464
3465/**
3466 * @defgroup mem_pool_apis Memory Pool APIs
3467 * @ingroup kernel_apis
3468 * @{
3469 */
3470
Andy Ross73cb9582017-05-09 10:42:39 -07003471/* Note on sizing: the use of a 20 bit field for block means that,
3472 * assuming a reasonable minimum block size of 16 bytes, we're limited
3473 * to 16M of memory managed by a single pool. Long term it would be
3474 * good to move to a variable bit size based on configuration.
3475 */
3476struct k_mem_block_id {
3477 u32_t pool : 8;
3478 u32_t level : 4;
3479 u32_t block : 20;
3480};
3481
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003482struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003483 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003484 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003485};
3486
Anas Nashif166f5192018-02-25 08:02:36 -06003487/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003488
3489/**
3490 * @defgroup mailbox_apis Mailbox APIs
3491 * @ingroup kernel_apis
3492 * @{
3493 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003494
3495struct k_mbox_msg {
3496 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003497 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003498 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003499 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003500 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003501 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003502 /** sender's message data buffer */
3503 void *tx_data;
3504 /** internal use only - needed for legacy API support */
3505 void *_rx_data;
3506 /** message data block descriptor */
3507 struct k_mem_block tx_block;
3508 /** source thread id */
3509 k_tid_t rx_source_thread;
3510 /** target thread id */
3511 k_tid_t tx_target_thread;
3512 /** internal use only - thread waiting on send (may be a dummy) */
3513 k_tid_t _syncing_thread;
3514#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3515 /** internal use only - semaphore used during asynchronous send */
3516 struct k_sem *_async_sem;
3517#endif
3518};
3519
3520struct k_mbox {
3521 _wait_q_t tx_msg_queue;
3522 _wait_q_t rx_msg_queue;
3523
Anas Nashif2f203c22016-12-18 06:57:45 -05003524 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003525};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003526/**
3527 * @cond INTERNAL_HIDDEN
3528 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003529
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003530#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003531 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003532 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3533 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003534 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003535 }
3536
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003537#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3538
Peter Mitsis12092702016-10-14 12:57:23 -04003539/**
Allan Stephensc98da842016-11-11 15:45:03 -05003540 * INTERNAL_HIDDEN @endcond
3541 */
3542
3543/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003544 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003545 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003546 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003547 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003548 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003549 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003550 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003551 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003552 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003553#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003554 struct k_mbox name \
3555 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003556 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003557
Peter Mitsis12092702016-10-14 12:57:23 -04003558/**
3559 * @brief Initialize a mailbox.
3560 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003561 * This routine initializes a mailbox object, prior to its first use.
3562 *
3563 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003564 *
3565 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003566 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003567 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003568extern void k_mbox_init(struct k_mbox *mbox);
3569
Peter Mitsis12092702016-10-14 12:57:23 -04003570/**
3571 * @brief Send a mailbox message in a synchronous manner.
3572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003573 * This routine sends a message to @a mbox and waits for a receiver to both
3574 * receive and process it. The message data may be in a buffer, in a memory
3575 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003577 * @param mbox Address of the mailbox.
3578 * @param tx_msg Address of the transmit message descriptor.
3579 * @param timeout Waiting period for the message to be received (in
3580 * milliseconds), or one of the special values K_NO_WAIT
3581 * and K_FOREVER. Once the message has been received,
3582 * this routine waits as long as necessary for the message
3583 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003584 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003585 * @retval 0 Message sent.
3586 * @retval -ENOMSG Returned without waiting.
3587 * @retval -EAGAIN Waiting period timed out.
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 int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003591 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003592
Peter Mitsis12092702016-10-14 12:57:23 -04003593/**
3594 * @brief Send a mailbox message in an asynchronous manner.
3595 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003596 * This routine sends a message to @a mbox without waiting for a receiver
3597 * to process it. The message data may be in a buffer, in a memory pool block,
3598 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3599 * will be given when the message has been both received and completely
3600 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003601 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003602 * @param mbox Address of the mailbox.
3603 * @param tx_msg Address of the transmit message descriptor.
3604 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003605 *
3606 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003607 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003608 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003609extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003610 struct k_sem *sem);
3611
Peter Mitsis12092702016-10-14 12:57:23 -04003612/**
3613 * @brief Receive a mailbox message.
3614 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003615 * This routine receives a message from @a mbox, then optionally retrieves
3616 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003617 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003618 * @param mbox Address of the mailbox.
3619 * @param rx_msg Address of the receive message descriptor.
3620 * @param buffer Address of the buffer to receive data, or NULL to defer data
3621 * retrieval and message disposal until later.
3622 * @param timeout Waiting period for a message to be received (in
3623 * milliseconds), or one of the special values K_NO_WAIT
3624 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003625 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003626 * @retval 0 Message received.
3627 * @retval -ENOMSG Returned without waiting.
3628 * @retval -EAGAIN Waiting period timed out.
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 int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003632 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003633
3634/**
3635 * @brief Retrieve mailbox message data into a buffer.
3636 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003637 * This routine completes the processing of a received message by retrieving
3638 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003639 *
3640 * Alternatively, this routine can be used to dispose of a received message
3641 * without retrieving its data.
3642 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003643 * @param rx_msg Address of the receive message descriptor.
3644 * @param buffer Address of the buffer to receive data, or NULL to discard
3645 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003646 *
3647 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003648 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003649 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003650extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003651
3652/**
3653 * @brief Retrieve mailbox message data into a memory pool block.
3654 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003655 * This routine completes the processing of a received message by retrieving
3656 * its data into a memory pool block, then disposing of the message.
3657 * The memory pool block that results from successful retrieval must be
3658 * returned to the pool once the data has been processed, even in cases
3659 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003660 *
3661 * Alternatively, this routine can be used to dispose of a received message
3662 * without retrieving its data. In this case there is no need to return a
3663 * memory pool block to the pool.
3664 *
3665 * This routine allocates a new memory pool block for the data only if the
3666 * data is not already in one. If a new block cannot be allocated, the routine
3667 * returns a failure code and the received message is left unchanged. This
3668 * permits the caller to reattempt data retrieval at a later time or to dispose
3669 * of the received message without retrieving its data.
3670 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003671 * @param rx_msg Address of a receive message descriptor.
3672 * @param pool Address of memory pool, or NULL to discard data.
3673 * @param block Address of the area to hold memory pool block info.
3674 * @param timeout Waiting period to wait for a memory pool block (in
3675 * milliseconds), or one of the special values K_NO_WAIT
3676 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003677 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003678 * @retval 0 Data retrieved.
3679 * @retval -ENOMEM Returned without waiting.
3680 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003681 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003682 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003683extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003684 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003685 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003686
Anas Nashif166f5192018-02-25 08:02:36 -06003687/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003688
3689/**
Anas Nashifce78d162018-05-24 12:43:11 -05003690 * @defgroup pipe_apis Pipe APIs
3691 * @ingroup kernel_apis
3692 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003693 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003694
Anas Nashifce78d162018-05-24 12:43:11 -05003695/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003696struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003697 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3698 size_t size; /**< Buffer size */
3699 size_t bytes_used; /**< # bytes used in buffer */
3700 size_t read_index; /**< Where in buffer to read from */
3701 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003702
3703 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003704 _wait_q_t readers; /**< Reader wait queue */
3705 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003706 } wait_q;
3707
Anas Nashif2f203c22016-12-18 06:57:45 -05003708 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Anas Nashifce78d162018-05-24 12:43:11 -05003709 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003710};
3711
Anas Nashifce78d162018-05-24 12:43:11 -05003712/**
3713 * @cond INTERNAL_HIDDEN
3714 */
3715#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3716
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003717#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003718 { \
3719 .buffer = pipe_buffer, \
3720 .size = pipe_buffer_size, \
3721 .bytes_used = 0, \
3722 .read_index = 0, \
3723 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003724 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3725 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003726 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003727 }
3728
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003729#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3730
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003731/**
Allan Stephensc98da842016-11-11 15:45:03 -05003732 * INTERNAL_HIDDEN @endcond
3733 */
3734
3735/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003736 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003737 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003738 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003739 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003740 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003742 * @param name Name of the pipe.
3743 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3744 * or zero if no ring buffer is used.
3745 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003746 *
3747 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003748 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003749#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3750 static unsigned char __kernel_noinit __aligned(pipe_align) \
3751 _k_pipe_buf_##name[pipe_buffer_size]; \
3752 struct k_pipe name \
3753 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003754 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003755
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003756/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003757 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003758 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003759 * This routine initializes a pipe object, prior to its first use.
3760 *
3761 * @param pipe Address of the pipe.
3762 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3763 * is used.
3764 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3765 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003766 *
3767 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003768 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003769 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003770void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3771
3772/**
3773 * @brief Release a pipe's allocated buffer
3774 *
3775 * If a pipe object was given a dynamically allocated buffer via
3776 * k_pipe_alloc_init(), this will free it. This function does nothing
3777 * if the buffer wasn't dynamically allocated.
3778 *
3779 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003780 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003781 */
3782void k_pipe_cleanup(struct k_pipe *pipe);
3783
3784/**
3785 * @brief Initialize a pipe and allocate a buffer for it
3786 *
3787 * Storage for the buffer region will be allocated from the calling thread's
3788 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3789 * or userspace is enabled and the pipe object loses all references to it.
3790 *
3791 * This function should only be called on uninitialized pipe objects.
3792 *
3793 * @param pipe Address of the pipe.
3794 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3795 * buffer is used.
3796 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003797 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003798 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003799 */
3800__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003801
3802/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003803 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003804 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003805 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003806 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003807 * @param pipe Address of the pipe.
3808 * @param data Address of data to write.
3809 * @param bytes_to_write Size of data (in bytes).
3810 * @param bytes_written Address of area to hold the number of bytes written.
3811 * @param min_xfer Minimum number of bytes to write.
3812 * @param timeout Waiting period to wait for the data to be written (in
3813 * milliseconds), or one of the special values K_NO_WAIT
3814 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003815 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003816 * @retval 0 At least @a min_xfer bytes of data were written.
3817 * @retval -EIO Returned without waiting; zero data bytes were written.
3818 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003819 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003820 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003821 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003822__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3823 size_t bytes_to_write, size_t *bytes_written,
3824 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003825
3826/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003827 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003828 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003829 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003831 * @param pipe Address of the pipe.
3832 * @param data Address to place the data read from pipe.
3833 * @param bytes_to_read Maximum number of data bytes to read.
3834 * @param bytes_read Address of area to hold the number of bytes read.
3835 * @param min_xfer Minimum number of data bytes to read.
3836 * @param timeout Waiting period to wait for the data to be read (in
3837 * milliseconds), or one of the special values K_NO_WAIT
3838 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003839 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003840 * @retval 0 At least @a min_xfer bytes of data were read.
3841 * @retval -EIO Returned without waiting; zero data bytes were read.
3842 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003843 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003844 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003845 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003846__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3847 size_t bytes_to_read, size_t *bytes_read,
3848 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003849
3850/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003851 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003853 * This routine writes the data contained in a memory block to @a pipe.
3854 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003855 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003857 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003858 * @param block Memory block containing data to send
3859 * @param size Number of data bytes in memory block to send
3860 * @param sem Semaphore to signal upon completion (else NULL)
3861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003862 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003863 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003864 */
3865extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3866 size_t size, struct k_sem *sem);
3867
Anas Nashif166f5192018-02-25 08:02:36 -06003868/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003869
Allan Stephensc98da842016-11-11 15:45:03 -05003870/**
3871 * @cond INTERNAL_HIDDEN
3872 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003873
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003874struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003875 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003876 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003877 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003878 char *buffer;
3879 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003880 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003881
Anas Nashif2f203c22016-12-18 06:57:45 -05003882 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003883};
3884
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003885#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003886 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003887 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003888 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003889 .num_blocks = slab_num_blocks, \
3890 .block_size = slab_block_size, \
3891 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003892 .free_list = NULL, \
3893 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003894 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003895 }
3896
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003897#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3898
3899
Peter Mitsis578f9112016-10-07 13:50:31 -04003900/**
Allan Stephensc98da842016-11-11 15:45:03 -05003901 * INTERNAL_HIDDEN @endcond
3902 */
3903
3904/**
3905 * @defgroup mem_slab_apis Memory Slab APIs
3906 * @ingroup kernel_apis
3907 * @{
3908 */
3909
3910/**
Allan Stephensda827222016-11-09 14:23:58 -06003911 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003912 *
Allan Stephensda827222016-11-09 14:23:58 -06003913 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003914 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003915 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3916 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003917 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003918 *
Allan Stephensda827222016-11-09 14:23:58 -06003919 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003920 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003921 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003922 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003923 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003924 * @param name Name of the memory slab.
3925 * @param slab_block_size Size of each memory block (in bytes).
3926 * @param slab_num_blocks Number memory blocks.
3927 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003928 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003929 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003930#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3931 char __noinit __aligned(slab_align) \
3932 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3933 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003934 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003935 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003936 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003937
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003938/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003939 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003940 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003941 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003942 *
Allan Stephensda827222016-11-09 14:23:58 -06003943 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3944 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3945 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3946 * To ensure that each memory block is similarly aligned to this boundary,
3947 * @a slab_block_size must also be a multiple of N.
3948 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003949 * @param slab Address of the memory slab.
3950 * @param buffer Pointer to buffer used for the memory blocks.
3951 * @param block_size Size of each memory block (in bytes).
3952 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003953 *
3954 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003955 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003956 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003957extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003958 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003959
3960/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003961 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003963 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003964 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003965 * @param slab Address of the memory slab.
3966 * @param mem Pointer to block address area.
3967 * @param timeout Maximum time to wait for operation to complete
3968 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3969 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003970 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003971 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003972 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003973 * @retval -ENOMEM Returned without waiting.
3974 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003975 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003976 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003977extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003978 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003979
3980/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003981 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003983 * This routine releases a previously allocated memory block back to its
3984 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003986 * @param slab Address of the memory slab.
3987 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003988 *
3989 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003990 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003991 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003992extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003993
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003994/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003995 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003996 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003997 * This routine gets the number of memory blocks that are currently
3998 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003999 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004000 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004001 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004002 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004003 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04004004 */
Kumar Galacc334c72017-04-21 10:55:34 -05004005static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004006{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004007 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004008}
4009
Peter Mitsisc001aa82016-10-13 13:53:37 -04004010/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004011 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004013 * This routine gets the number of memory blocks that are currently
4014 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004016 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004018 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004019 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004020 */
Kumar Galacc334c72017-04-21 10:55:34 -05004021static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004022{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004023 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004024}
4025
Anas Nashif166f5192018-02-25 08:02:36 -06004026/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004027
4028/**
4029 * @cond INTERNAL_HIDDEN
4030 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004031
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004032struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004033 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004034 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004035};
4036
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004037/**
Allan Stephensc98da842016-11-11 15:45:03 -05004038 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004039 */
4040
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004041/**
Allan Stephensc98da842016-11-11 15:45:03 -05004042 * @addtogroup mem_pool_apis
4043 * @{
4044 */
4045
4046/**
4047 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004048 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004049 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4050 * long. The memory pool allows blocks to be repeatedly partitioned into
4051 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004052 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004053 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004054 * If the pool is to be accessed outside the module where it is defined, it
4055 * can be declared via
4056 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004057 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004058 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004059 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004060 * @param minsz Size of the smallest blocks in the pool (in bytes).
4061 * @param maxsz Size of the largest blocks in the pool (in bytes).
4062 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004063 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004064 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004065 */
Andy Ross73cb9582017-05-09 10:42:39 -07004066#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4067 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4068 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08004069 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07004070 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004071 .base = { \
4072 .buf = _mpool_buf_##name, \
4073 .max_sz = maxsz, \
4074 .n_max = nmax, \
4075 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
4076 .levels = _mpool_lvls_##name, \
4077 .flags = SYS_MEM_POOL_KERNEL \
4078 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004079 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004080
Peter Mitsis937042c2016-10-13 13:18:26 -04004081/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004082 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004083 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004084 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004085 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004086 * @param pool Address of the memory pool.
4087 * @param block Pointer to block descriptor for the allocated memory.
4088 * @param size Amount of memory to allocate (in bytes).
4089 * @param timeout Maximum time to wait for operation to complete
4090 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4091 * or K_FOREVER to wait as long as necessary.
4092 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004093 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004094 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004095 * @retval -ENOMEM Returned without waiting.
4096 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004097 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004098 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004099extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004100 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004101
4102/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004103 * @brief Allocate memory from a memory pool with malloc() semantics
4104 *
4105 * Such memory must be released using k_free().
4106 *
4107 * @param pool Address of the memory pool.
4108 * @param size Amount of memory to allocate (in bytes).
4109 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004110 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004111 */
4112extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4113
4114/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004115 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004117 * This routine releases a previously allocated memory block back to its
4118 * memory pool.
4119 *
4120 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004121 *
4122 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004123 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004124 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004125extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004126
4127/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004128 * @brief Free memory allocated from a memory pool.
4129 *
4130 * This routine releases a previously allocated memory block back to its
4131 * memory pool
4132 *
4133 * @param id Memory block identifier.
4134 *
4135 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004136 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004137 */
4138extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4139
4140/**
Anas Nashif166f5192018-02-25 08:02:36 -06004141 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004142 */
4143
4144/**
4145 * @defgroup heap_apis Heap Memory Pool APIs
4146 * @ingroup kernel_apis
4147 * @{
4148 */
4149
4150/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004151 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004153 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004154 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004155 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004156 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004157 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004158 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004159 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004160 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004161extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004162
4163/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004164 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004165 *
4166 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004167 * returned must have been allocated from the heap memory pool or
4168 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004169 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004170 * If @a ptr is NULL, no operation is performed.
4171 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004172 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004173 *
4174 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004175 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004176 */
4177extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004178
Allan Stephensc98da842016-11-11 15:45:03 -05004179/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004180 * @brief Allocate memory from heap, array style
4181 *
4182 * This routine provides traditional calloc() semantics. Memory is
4183 * allocated from the heap memory pool and zeroed.
4184 *
4185 * @param nmemb Number of elements in the requested array
4186 * @param size Size of each array element (in bytes).
4187 *
4188 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004189 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004190 */
4191extern void *k_calloc(size_t nmemb, size_t size);
4192
Anas Nashif166f5192018-02-25 08:02:36 -06004193/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004194
Benjamin Walshacc68c12017-01-29 18:57:45 -05004195/* polling API - PRIVATE */
4196
Benjamin Walshb0179862017-02-02 16:39:57 -05004197#ifdef CONFIG_POLL
4198#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
4199#else
4200#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
4201#endif
4202
Benjamin Walshacc68c12017-01-29 18:57:45 -05004203/* private - implementation data created as needed, per-type */
4204struct _poller {
4205 struct k_thread *thread;
Andy Ross55a7e462018-05-31 11:58:09 -07004206 volatile int is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004207};
4208
4209/* private - types bit positions */
4210enum _poll_types_bits {
4211 /* can be used to ignore an event */
4212 _POLL_TYPE_IGNORE,
4213
4214 /* to be signaled by k_poll_signal() */
4215 _POLL_TYPE_SIGNAL,
4216
4217 /* semaphore availability */
4218 _POLL_TYPE_SEM_AVAILABLE,
4219
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004220 /* queue/fifo/lifo data availability */
4221 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004222
4223 _POLL_NUM_TYPES
4224};
4225
4226#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4227
4228/* private - states bit positions */
4229enum _poll_states_bits {
4230 /* default state when creating event */
4231 _POLL_STATE_NOT_READY,
4232
Benjamin Walshacc68c12017-01-29 18:57:45 -05004233 /* signaled by k_poll_signal() */
4234 _POLL_STATE_SIGNALED,
4235
4236 /* semaphore is available */
4237 _POLL_STATE_SEM_AVAILABLE,
4238
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004239 /* data is available to read on queue/fifo/lifo */
4240 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004241
4242 _POLL_NUM_STATES
4243};
4244
4245#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4246
4247#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004248 (32 - (0 \
4249 + 8 /* tag */ \
4250 + _POLL_NUM_TYPES \
4251 + _POLL_NUM_STATES \
4252 + 1 /* modes */ \
4253 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004254
Benjamin Walshacc68c12017-01-29 18:57:45 -05004255/* end of polling API - PRIVATE */
4256
4257
4258/**
4259 * @defgroup poll_apis Async polling APIs
4260 * @ingroup kernel_apis
4261 * @{
4262 */
4263
4264/* Public polling API */
4265
4266/* public - values for k_poll_event.type bitfield */
4267#define K_POLL_TYPE_IGNORE 0
4268#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4269#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004270#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4271#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004272
4273/* public - polling modes */
4274enum k_poll_modes {
4275 /* polling thread does not take ownership of objects when available */
4276 K_POLL_MODE_NOTIFY_ONLY = 0,
4277
4278 K_POLL_NUM_MODES
4279};
4280
4281/* public - values for k_poll_event.state bitfield */
4282#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004283#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4284#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004285#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4286#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004287
4288/* public - poll signal object */
4289struct k_poll_signal {
4290 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004291 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004292
4293 /*
4294 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4295 * user resets it to 0.
4296 */
4297 unsigned int signaled;
4298
4299 /* custom result value passed to k_poll_signal() if needed */
4300 int result;
4301};
4302
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004303#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004304 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004305 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004306 .signaled = 0, \
4307 .result = 0, \
4308 }
4309
4310struct k_poll_event {
4311 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004312 sys_dnode_t _node;
4313
4314 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004315 struct _poller *poller;
4316
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004317 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004318 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004319
Benjamin Walshacc68c12017-01-29 18:57:45 -05004320 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004321 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004322
4323 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004324 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004325
4326 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004327 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004328
4329 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004330 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004331
4332 /* per-type data */
4333 union {
4334 void *obj;
4335 struct k_poll_signal *signal;
4336 struct k_sem *sem;
4337 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004338 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004339 };
4340};
4341
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004342#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004343 { \
4344 .poller = NULL, \
4345 .type = event_type, \
4346 .state = K_POLL_STATE_NOT_READY, \
4347 .mode = event_mode, \
4348 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004349 { .obj = event_obj }, \
4350 }
4351
4352#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4353 event_tag) \
4354 { \
4355 .type = event_type, \
4356 .tag = event_tag, \
4357 .state = K_POLL_STATE_NOT_READY, \
4358 .mode = event_mode, \
4359 .unused = 0, \
4360 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004361 }
4362
4363/**
4364 * @brief Initialize one struct k_poll_event instance
4365 *
4366 * After this routine is called on a poll event, the event it ready to be
4367 * placed in an event array to be passed to k_poll().
4368 *
4369 * @param event The event to initialize.
4370 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4371 * values. Only values that apply to the same object being polled
4372 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4373 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004374 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004375 * @param obj Kernel object or poll signal.
4376 *
4377 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004378 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004379 */
4380
Kumar Galacc334c72017-04-21 10:55:34 -05004381extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004382 int mode, void *obj);
4383
4384/**
4385 * @brief Wait for one or many of multiple poll events to occur
4386 *
4387 * This routine allows a thread to wait concurrently for one or many of
4388 * multiple poll events to have occurred. Such events can be a kernel object
4389 * being available, like a semaphore, or a poll signal event.
4390 *
4391 * When an event notifies that a kernel object is available, the kernel object
4392 * is not "given" to the thread calling k_poll(): it merely signals the fact
4393 * that the object was available when the k_poll() call was in effect. Also,
4394 * all threads trying to acquire an object the regular way, i.e. by pending on
4395 * the object, have precedence over the thread polling on the object. This
4396 * means that the polling thread will never get the poll event on an object
4397 * until the object becomes available and its pend queue is empty. For this
4398 * reason, the k_poll() call is more effective when the objects being polled
4399 * only have one thread, the polling thread, trying to acquire them.
4400 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004401 * When k_poll() returns 0, the caller should loop on all the events that were
4402 * passed to k_poll() and check the state field for the values that were
4403 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004404 *
4405 * Before being reused for another call to k_poll(), the user has to reset the
4406 * state field to K_POLL_STATE_NOT_READY.
4407 *
Andrew Boie3772f772018-05-07 16:52:57 -07004408 * When called from user mode, a temporary memory allocation is required from
4409 * the caller's resource pool.
4410 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004411 * @param events An array of pointers to events to be polled for.
4412 * @param num_events The number of events in the array.
4413 * @param timeout Waiting period for an event to be ready (in milliseconds),
4414 * or one of the special values K_NO_WAIT and K_FOREVER.
4415 *
4416 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004417 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02004418 * @retval -EINTR Poller thread has been interrupted.
Andrew Boie3772f772018-05-07 16:52:57 -07004419 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4420 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004421 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004422 */
4423
Andrew Boie3772f772018-05-07 16:52:57 -07004424__syscall int k_poll(struct k_poll_event *events, int num_events,
4425 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004426
4427/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004428 * @brief Initialize a poll signal object.
4429 *
4430 * Ready a poll signal object to be signaled via k_poll_signal().
4431 *
4432 * @param signal A poll signal.
4433 *
4434 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004435 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004436 */
4437
Andrew Boie3772f772018-05-07 16:52:57 -07004438__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4439
4440/*
4441 * @brief Reset a poll signal object's state to unsignaled.
4442 *
4443 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004444 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004445 */
4446__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4447
4448static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4449{
4450 signal->signaled = 0;
4451}
4452
4453/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004454 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004455 *
4456 * @param signal A poll signal object
4457 * @param signaled An integer buffer which will be written nonzero if the
4458 * object was signaled
4459 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004460 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004461 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004462 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004463 */
4464__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4465 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004466
4467/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004468 * @brief Signal a poll signal object.
4469 *
4470 * This routine makes ready a poll signal, which is basically a poll event of
4471 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4472 * made ready to run. A @a result value can be specified.
4473 *
4474 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004475 * k_poll_signal(), stays set until the user sets it back to 0 with
4476 * k_poll_signal_reset(). It thus has to be reset by the user before being
4477 * passed again to k_poll() or k_poll() will consider it being signaled, and
4478 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004479 *
4480 * @param signal A poll signal.
4481 * @param result The value to store in the result field of the signal.
4482 *
4483 * @retval 0 The signal was delivered successfully.
4484 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004485 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004486 */
4487
Andrew Boie3772f772018-05-07 16:52:57 -07004488__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004489
Anas Nashif954d5502018-02-25 08:37:28 -06004490/**
4491 * @internal
4492 */
Andy Ross8606fab2018-03-26 10:54:40 -07004493extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004494
Anas Nashif166f5192018-02-25 08:02:36 -06004495/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004496
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004497/**
4498 * @brief Make the CPU idle.
4499 *
4500 * This function makes the CPU idle until an event wakes it up.
4501 *
4502 * In a regular system, the idle thread should be the only thread responsible
4503 * for making the CPU idle and triggering any type of power management.
4504 * However, in some more constrained systems, such as a single-threaded system,
4505 * the only thread would be responsible for this if needed.
4506 *
4507 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004508 * @req K-MISC-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004509 */
4510extern void k_cpu_idle(void);
4511
4512/**
4513 * @brief Make the CPU idle in an atomic fashion.
4514 *
4515 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4516 * must be done atomically before making the CPU idle.
4517 *
4518 * @param key Interrupt locking key obtained from irq_lock().
4519 *
4520 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004521 * @req K-MISC-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004522 */
4523extern void k_cpu_atomic_idle(unsigned int key);
4524
Anas Nashif954d5502018-02-25 08:37:28 -06004525
4526/**
4527 * @internal
4528 */
Kumar Galacc334c72017-04-21 10:55:34 -05004529extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004530
Andrew Boiecdb94d62017-04-18 15:22:05 -07004531#ifdef _ARCH_EXCEPT
4532/* This archtecture has direct support for triggering a CPU exception */
4533#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4534#else
4535
Andrew Boiecdb94d62017-04-18 15:22:05 -07004536/* NOTE: This is the implementation for arches that do not implement
4537 * _ARCH_EXCEPT() to generate a real CPU exception.
4538 *
4539 * We won't have a real exception frame to determine the PC value when
4540 * the oops occurred, so print file and line number before we jump into
4541 * the fatal error handler.
4542 */
4543#define _k_except_reason(reason) do { \
4544 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4545 _NanoFatalErrorHandler(reason, &_default_esf); \
4546 CODE_UNREACHABLE; \
4547 } while (0)
4548
4549#endif /* _ARCH__EXCEPT */
4550
4551/**
4552 * @brief Fatally terminate a thread
4553 *
4554 * This should be called when a thread has encountered an unrecoverable
4555 * runtime condition and needs to terminate. What this ultimately
4556 * means is determined by the _fatal_error_handler() implementation, which
4557 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4558 *
4559 * If this is called from ISR context, the default system fatal error handler
4560 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004561 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004562 */
4563#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4564
4565/**
4566 * @brief Fatally terminate the system
4567 *
4568 * This should be called when the Zephyr kernel has encountered an
4569 * unrecoverable runtime condition and needs to terminate. What this ultimately
4570 * means is determined by the _fatal_error_handler() implementation, which
4571 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004572 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004573 */
4574#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4575
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004576/*
4577 * private APIs that are utilized by one or more public APIs
4578 */
4579
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004580#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004581/**
4582 * @internal
4583 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004584extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004585#else
Anas Nashif954d5502018-02-25 08:37:28 -06004586/**
4587 * @internal
4588 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004589#define _init_static_threads() do { } while ((0))
4590#endif
4591
Anas Nashif954d5502018-02-25 08:37:28 -06004592/**
4593 * @internal
4594 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004595extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004596/**
4597 * @internal
4598 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004599extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004600
Andrew Boiedc5d9352017-06-02 12:56:47 -07004601/* arch/cpu.h may declare an architecture or platform-specific macro
4602 * for properly declaring stacks, compatible with MMU/MPU constraints if
4603 * enabled
4604 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004605
4606/**
4607 * @brief Obtain an extern reference to a stack
4608 *
4609 * This macro properly brings the symbol of a thread stack declared
4610 * elsewhere into scope.
4611 *
4612 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004613 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004614 */
4615#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4616
Andrew Boiedc5d9352017-06-02 12:56:47 -07004617#ifdef _ARCH_THREAD_STACK_DEFINE
4618#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4619#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4620 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304621#define K_THREAD_STACK_LEN(size) _ARCH_THREAD_STACK_LEN(size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004622#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4623#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004624static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004625{
4626 return _ARCH_THREAD_STACK_BUFFER(sym);
4627}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004628#else
4629/**
4630 * @brief Declare a toplevel thread stack memory region
4631 *
4632 * This declares a region of memory suitable for use as a thread's stack.
4633 *
4634 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4635 * 'noinit' section so that it isn't zeroed at boot
4636 *
Andrew Boie507852a2017-07-25 18:47:07 -07004637 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004638 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004639 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004640 *
4641 * It is legal to precede this definition with the 'static' keyword.
4642 *
4643 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4644 * parameter of k_thread_create(), it may not be the same as the
4645 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4646 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004647 * Some arches may round the size of the usable stack region up to satisfy
4648 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4649 * size.
4650 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004651 * @param sym Thread stack symbol name
4652 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004653 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004654 */
4655#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004656 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004657
4658/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304659 * @brief Calculate size of stacks to be allocated in a stack array
4660 *
4661 * This macro calculates the size to be allocated for the stacks
4662 * inside a stack array. It accepts the indicated "size" as a parameter
4663 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4664 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4665 *
4666 * @param size Size of the stack memory region
4667 * @req K-TSTACK-001
4668 */
4669#define K_THREAD_STACK_LEN(size) (size)
4670
4671/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004672 * @brief Declare a toplevel array of thread stack memory regions
4673 *
4674 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4675 * definition for additional details and constraints.
4676 *
4677 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4678 * 'noinit' section so that it isn't zeroed at boot
4679 *
4680 * @param sym Thread stack symbol name
4681 * @param nmemb Number of stacks to declare
4682 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004683 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004684 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004685#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004686 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304687 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004688
4689/**
4690 * @brief Declare an embedded stack memory region
4691 *
4692 * Used for stacks embedded within other data structures. Use is highly
4693 * discouraged but in some cases necessary. For memory protection scenarios,
4694 * it is very important that any RAM preceding this member not be writable
4695 * by threads else a stack overflow will lead to silent corruption. In other
4696 * words, the containing data structure should live in RAM owned by the kernel.
4697 *
4698 * @param sym Thread stack symbol name
4699 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004700 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004701 */
4702#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004703 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004704
4705/**
4706 * @brief Return the size in bytes of a stack memory region
4707 *
4708 * Convenience macro for passing the desired stack size to k_thread_create()
4709 * since the underlying implementation may actually create something larger
4710 * (for instance a guard area).
4711 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004712 * The value returned here is not guaranteed to match the 'size' parameter
4713 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004714 *
4715 * @param sym Stack memory symbol
4716 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004717 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004718 */
4719#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4720
4721/**
4722 * @brief Get a pointer to the physical stack buffer
4723 *
4724 * Convenience macro to get at the real underlying stack buffer used by
4725 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4726 * This is really only intended for diagnostic tools which want to examine
4727 * stack memory contents.
4728 *
4729 * @param sym Declared stack symbol name
4730 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004731 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004732 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004733static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004734{
4735 return (char *)sym;
4736}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004737
4738#endif /* _ARCH_DECLARE_STACK */
4739
Chunlin Hane9c97022017-07-07 20:29:30 +08004740/**
4741 * @defgroup mem_domain_apis Memory domain APIs
4742 * @ingroup kernel_apis
4743 * @{
4744 */
4745
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004746/**
4747 * @def MEM_PARTITION_ENTRY
4748 * @brief Used to declare a memory partition entry
4749 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004750 */
4751#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4752 {\
4753 .start = _start, \
4754 .size = _size, \
4755 .attr = _attr, \
4756 }
4757
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004758/**
4759 * @def K_MEM_PARTITION_DEFINE
4760 * @brief Used to declare a memory partition
4761 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004762 */
4763#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4764#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4765 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304766 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004767 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4768#else
4769#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304770 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004771 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4772#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4773
Chunlin Hane9c97022017-07-07 20:29:30 +08004774/* memory partition */
4775struct k_mem_partition {
4776 /* start address of memory partition */
4777 u32_t start;
4778 /* size of memory partition */
4779 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304780#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004781 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304782 k_mem_partition_attr_t attr;
4783#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004784};
4785
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304786/* memory domian
4787 * Note: Always declare this structure with __kernel prefix
4788 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004789struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304790#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004791 /* partitions in the domain */
4792 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304793#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004794 /* domain q */
4795 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004796 /* number of partitions in the domain */
4797 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004798};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304799
Chunlin Hane9c97022017-07-07 20:29:30 +08004800
4801/**
4802 * @brief Initialize a memory domain.
4803 *
4804 * Initialize a memory domain with given name and memory partitions.
4805 *
4806 * @param domain The memory domain to be initialized.
4807 * @param num_parts The number of array items of "parts" parameter.
4808 * @param parts An array of pointers to the memory partitions. Can be NULL
4809 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004810 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004811 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004812extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004813 struct k_mem_partition *parts[]);
4814/**
4815 * @brief Destroy a memory domain.
4816 *
4817 * Destroy a memory domain.
4818 *
4819 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004820 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004821 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004822extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4823
4824/**
4825 * @brief Add a memory partition into a memory domain.
4826 *
4827 * Add a memory partition into a memory domain.
4828 *
4829 * @param domain The memory domain to be added a memory partition.
4830 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004831 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004832 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004833extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4834 struct k_mem_partition *part);
4835
4836/**
4837 * @brief Remove a memory partition from a memory domain.
4838 *
4839 * Remove a memory partition from a memory domain.
4840 *
4841 * @param domain The memory domain to be removed a memory partition.
4842 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004843 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004844 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004845extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4846 struct k_mem_partition *part);
4847
4848/**
4849 * @brief Add a thread into a memory domain.
4850 *
4851 * Add a thread into a memory domain.
4852 *
4853 * @param domain The memory domain that the thread is going to be added into.
4854 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004855 *
4856 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004857 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004858extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4859 k_tid_t thread);
4860
4861/**
4862 * @brief Remove a thread from its memory domain.
4863 *
4864 * Remove a thread from its memory domain.
4865 *
4866 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004867 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004868 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004869extern void k_mem_domain_remove_thread(k_tid_t thread);
4870
Anas Nashif166f5192018-02-25 08:02:36 -06004871/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004872
Andrew Boie756f9072017-10-10 16:01:49 -07004873/**
4874 * @brief Emit a character buffer to the console device
4875 *
4876 * @param c String of characters to print
4877 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004878 *
4879 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004880 */
4881__syscall void k_str_out(char *c, size_t n);
4882
Andy Rosse7172672018-01-24 15:48:32 -08004883/**
4884 * @brief Start a numbered CPU on a MP-capable system
4885
4886 * This starts and initializes a specific CPU. The main thread on
4887 * startup is running on CPU zero, other processors are numbered
4888 * sequentially. On return from this function, the CPU is known to
4889 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004890 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004891 * with the provided key will work to enable them.
4892 *
4893 * Normally, in SMP mode this function will be called by the kernel
4894 * initialization and should not be used as a user API. But it is
4895 * defined here for special-purpose apps which want Zephyr running on
4896 * one core and to use others for design-specific processing.
4897 *
4898 * @param cpu_num Integer number of the CPU
4899 * @param stack Stack memory for the CPU
4900 * @param sz Stack buffer size, in bytes
4901 * @param fn Function to begin running on the CPU. First argument is
4902 * an irq_unlock() key.
4903 * @param arg Untyped argument to be passed to "fn"
4904 */
4905extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4906 void (*fn)(int, void *), void *arg);
4907
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004908#ifdef __cplusplus
4909}
4910#endif
4911
Andrew Boiee004dec2016-11-07 09:01:19 -08004912#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4913/*
4914 * Define new and delete operators.
4915 * At this moment, the operators do nothing since objects are supposed
4916 * to be statically allocated.
4917 */
4918inline void operator delete(void *ptr)
4919{
4920 (void)ptr;
4921}
4922
4923inline void operator delete[](void *ptr)
4924{
4925 (void)ptr;
4926}
4927
4928inline void *operator new(size_t size)
4929{
4930 (void)size;
4931 return NULL;
4932}
4933
4934inline void *operator new[](size_t size)
4935{
4936 (void)size;
4937 return NULL;
4938}
4939
4940/* Placement versions of operator new and delete */
4941inline void operator delete(void *ptr1, void *ptr2)
4942{
4943 (void)ptr1;
4944 (void)ptr2;
4945}
4946
4947inline void operator delete[](void *ptr1, void *ptr2)
4948{
4949 (void)ptr1;
4950 (void)ptr2;
4951}
4952
4953inline void *operator new(size_t size, void *ptr)
4954{
4955 (void)size;
4956 return ptr;
4957}
4958
4959inline void *operator new[](size_t size, void *ptr)
4960{
4961 (void)size;
4962 return ptr;
4963}
4964
4965#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4966
Andrew Boiefa94ee72017-09-28 16:54:35 -07004967#include <syscalls/kernel.h>
4968
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004969#endif /* !_ASMLANGUAGE */
4970
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004971#endif /* _kernel__h_ */