blob: 419a1b84106583f6c1dac84e995643cd923fead5 [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)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040017#include <stddef.h>
Kumar Gala78908162017-04-19 10:32:08 -050018#include <zephyr/types.h>
Anas Nashif173902f2017-01-17 07:08:56 -050019#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020#include <toolchain.h>
Anas Nashif397d29d2017-06-17 11:30:47 -040021#include <linker/sections.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040022#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
Andy Ross1acd8c22018-05-03 14:51:49 -070025#include <sched_priq.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040026#include <misc/dlist.h>
27#include <misc/slist.h>
Andrew Boie2b9b4b22018-04-27 13:21:22 -070028#include <misc/sflist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050029#include <misc/util.h>
Andrew Boieaa6de292018-03-06 17:12:37 -080030#include <misc/mempool_base.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050031#include <kernel_version.h>
Leandro Pereiraadce1d12017-10-13 15:45:02 -070032#include <random/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070033#include <kernel_arch_thread.h>
Andrew Boie13ca6fe2017-09-23 12:05:49 -070034#include <syscall.h>
Andrew Boie43263fc2017-11-02 11:07:31 -070035#include <misc/printk.h>
36#include <arch/cpu.h>
Andy Ross1acd8c22018-05-03 14:51:49 -070037#include <misc/rb.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040038
39#ifdef __cplusplus
40extern "C" {
41#endif
42
Anas Nashifbbb157d2017-01-15 08:46:31 -050043/**
44 * @brief Kernel APIs
45 * @defgroup kernel_apis Kernel APIs
46 * @{
47 * @}
48 */
49
Anas Nashif61f4b242016-11-18 10:53:59 -050050#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040051#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
52#else
53#define K_DEBUG(fmt, ...)
54#endif
55
Benjamin Walsh2f280412017-01-14 19:23:46 -050056#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
57#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
58#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
59#elif defined(CONFIG_COOP_ENABLED)
60#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
61#define _NUM_PREEMPT_PRIO (0)
62#elif defined(CONFIG_PREEMPT_ENABLED)
63#define _NUM_COOP_PRIO (0)
64#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
65#else
66#error "invalid configuration"
67#endif
68
69#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040070#define K_PRIO_PREEMPT(x) (x)
71
Benjamin Walsh456c6da2016-09-02 18:55:39 -040072#define K_ANY NULL
73#define K_END NULL
74
Benjamin Walshedb35702017-01-14 18:47:22 -050075#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040076#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050077#elif defined(CONFIG_COOP_ENABLED)
78#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
79#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050081#else
82#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040083#endif
84
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050085#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040086#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
87#else
88#define K_LOWEST_THREAD_PRIO -1
89#endif
90
Benjamin Walshfab8d922016-11-08 15:36:36 -050091#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
92
Benjamin Walsh456c6da2016-09-02 18:55:39 -040093#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
94#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
95
Andy Ross1acd8c22018-05-03 14:51:49 -070096#ifdef CONFIG_WAITQ_FAST
97
98typedef struct {
99 struct _priq_rb waitq;
100} _wait_q_t;
101
102extern int _priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
103
104#define _WAIT_Q_INIT(wait_q) { { { .lessthan_fn = _priq_rb_lessthan } } }
105
106#else
107
Andy Rossccf3bf72018-05-10 11:10:34 -0700108typedef struct {
109 sys_dlist_t waitq;
110} _wait_q_t;
111
112#define _WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400113
Andy Ross1acd8c22018-05-03 14:51:49 -0700114#endif
115
Anas Nashif2f203c22016-12-18 06:57:45 -0500116#ifdef CONFIG_OBJECT_TRACING
117#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
118#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400119#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500120#define _OBJECT_TRACING_INIT
121#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122#endif
123
Benjamin Walshacc68c12017-01-29 18:57:45 -0500124#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300125#define _POLL_EVENT_OBJ_INIT(obj) \
126 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
127#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500128#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300129#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500130#define _POLL_EVENT
131#endif
132
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500133struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400134struct k_mutex;
135struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400136struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400137struct k_msgq;
138struct k_mbox;
139struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200140struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400141struct k_fifo;
142struct k_lifo;
143struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400144struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400145struct k_mem_pool;
146struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500147struct k_poll_event;
148struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800149struct k_mem_domain;
150struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400151
Andrew Boie5bd891d2017-09-27 12:59:28 -0700152/* This enumeration needs to be kept in sync with the lists of kernel objects
153 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
154 * function in kernel/userspace.c
155 */
Andrew Boie945af952017-08-22 13:15:23 -0700156enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700157 K_OBJ_ANY,
158
Leandro Pereirac2003672018-04-04 13:50:32 -0700159 /** @cond
160 * Doxygen should ignore this build-time generated include file
161 * when genrating API documentation. Enumeration values are
162 * generated during build by gen_kobject_list.py. It includes
163 * basic kernel objects (e.g. pipes and mutexes) and driver types.
164 */
165#include <kobj-types-enum.h>
166 /** @endcond
167 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700168
Andrew Boie945af952017-08-22 13:15:23 -0700169 K_OBJ_LAST
170};
171
172#ifdef CONFIG_USERSPACE
173/* Table generated by gperf, these objects are retrieved via
174 * _k_object_find() */
175struct _k_object {
176 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700177 u8_t perms[CONFIG_MAX_THREAD_BYTES];
178 u8_t type;
179 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700180 u32_t data;
Andrew Boie945af952017-08-22 13:15:23 -0700181} __packed;
182
Andrew Boie877f82e2017-10-17 11:20:22 -0700183struct _k_object_assignment {
184 struct k_thread *thread;
185 void * const *objects;
186};
187
188/**
189 * @brief Grant a static thread access to a list of kernel objects
190 *
191 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
192 * a set of kernel objects. These objects do not need to be in an initialized
193 * state. The permissions will be granted when the threads are initialized
194 * in the early boot sequence.
195 *
196 * All arguments beyond the first must be pointers to kernel objects.
197 *
198 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
199 */
200#define K_THREAD_ACCESS_GRANT(name_, ...) \
201 static void * const _CONCAT(_object_list_, name_)[] = \
202 { __VA_ARGS__, NULL }; \
203 static __used __in_section_unique(object_access) \
204 const struct _k_object_assignment \
205 _CONCAT(_object_access_, name_) = \
206 { (&_k_thread_obj_ ## name_), \
207 (_CONCAT(_object_list_, name_)) }
208
Andrew Boie945af952017-08-22 13:15:23 -0700209#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700210#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700211#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie945af952017-08-22 13:15:23 -0700212
213/**
214 * Lookup a kernel object and init its metadata if it exists
215 *
216 * Calling this on an object will make it usable from userspace.
217 * Intended to be called as the last statement in kernel object init
218 * functions.
219 *
220 * @param object Address of the kernel object
221 */
222void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700223#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700224
225#define K_THREAD_ACCESS_GRANT(thread, ...)
226
Anas Nashif954d5502018-02-25 08:37:28 -0600227/**
228 * @internal
229 */
Andrew Boie743e4682017-10-04 12:25:50 -0700230static inline void _k_object_init(void *obj)
231{
232 ARG_UNUSED(obj);
233}
234
Anas Nashif954d5502018-02-25 08:37:28 -0600235/**
236 * @internal
237 */
Andrew Boie743e4682017-10-04 12:25:50 -0700238static inline void _impl_k_object_access_grant(void *object,
239 struct k_thread *thread)
240{
241 ARG_UNUSED(object);
242 ARG_UNUSED(thread);
243}
244
Anas Nashif954d5502018-02-25 08:37:28 -0600245/**
246 * @internal
247 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700248static inline void k_object_access_revoke(void *object,
249 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700250{
251 ARG_UNUSED(object);
252 ARG_UNUSED(thread);
253}
254
Andrew Boiee9cfc542018-04-13 13:15:28 -0700255/**
256 * @internal
257 */
258static inline void _impl_k_object_release(void *object)
259{
260 ARG_UNUSED(object);
261}
262
Andrew Boie41bab6e2017-10-14 14:42:23 -0700263static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700264{
265 ARG_UNUSED(object);
266}
267#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700268
269/**
270 * grant a thread access to a kernel object
271 *
272 * The thread will be granted access to the object if the caller is from
273 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700274 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700275 *
276 * @param object Address of kernel object
277 * @param thread Thread to grant access to the object
278 */
Andrew Boie743e4682017-10-04 12:25:50 -0700279__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700280
Andrew Boiea89bf012017-10-09 14:47:55 -0700281/**
282 * grant a thread access to a kernel object
283 *
284 * The thread will lose access to the object if the caller is from
285 * supervisor mode, or the caller is from user mode AND has permissions
286 * on both the object and the thread whose access is being revoked.
287 *
288 * @param object Address of kernel object
289 * @param thread Thread to remove access to the object
290 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700291void k_object_access_revoke(void *object, struct k_thread *thread);
292
293
294__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700295
296/**
297 * grant all present and future threads access to an object
298 *
299 * If the caller is from supervisor mode, or the caller is from user mode and
300 * have sufficient permissions on the object, then that object will have
301 * permissions granted to it for *all* current and future threads running in
302 * the system, effectively becoming a public kernel object.
303 *
304 * Use of this API should be avoided on systems that are running untrusted code
305 * as it is possible for such code to derive the addresses of kernel objects
306 * and perform unwanted operations on them.
307 *
Andrew Boie04caa672017-10-13 13:57:07 -0700308 * It is not possible to revoke permissions on public objects; once public,
309 * any thread may use it.
310 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700311 * @param object Address of kernel object
312 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700313void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700314
Andrew Boie31bdfc02017-11-08 16:38:03 -0800315/**
316 * Allocate a kernel object of a designated type
317 *
318 * This will instantiate at runtime a kernel object of the specified type,
319 * returning a pointer to it. The object will be returned in an uninitialized
320 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700321 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800322 *
323 * Currently, allocation of thread stacks is not supported.
324 *
325 * @param otype Requested kernel object type
326 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
327 * available
328 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700329__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800330
Andrew Boie97bf0012018-04-24 17:01:37 -0700331#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800332/**
333 * Free a kernel object previously allocated with k_object_alloc()
334 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700335 * This will return memory for a kernel object back to resource pool it was
336 * allocated from. Care must be exercised that the object will not be used
337 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800338 *
339 * @param obj Pointer to the kernel object memory address.
340 */
341void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700342#else
343static inline void *_impl_k_object_alloc(enum k_objects otype)
344{
Kumar Gala85699f72018-05-17 09:26:03 -0500345 ARG_UNUSED(otype);
346
Andrew Boie97bf0012018-04-24 17:01:37 -0700347 return NULL;
348}
349
350static inline void k_obj_free(void *obj)
351{
352 ARG_UNUSED(obj);
353}
Andrew Boie31bdfc02017-11-08 16:38:03 -0800354#endif /* CONFIG_DYNAMIC_OBJECTS */
355
Andrew Boiebca15da2017-10-15 14:17:48 -0700356/* Using typedef deliberately here, this is quite intended to be an opaque
357 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
358 *
359 * The purpose of this data type is to clearly distinguish between the
360 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
361 * buffer which composes the stack data actually used by the underlying
362 * thread; they cannot be used interchangably as some arches precede the
363 * stack buffer region with guard areas that trigger a MPU or MMU fault
364 * if written to.
365 *
366 * APIs that want to work with the buffer inside should continue to use
367 * char *.
368 *
369 * Stacks should always be created with K_THREAD_STACK_DEFINE().
370 */
371struct __packed _k_thread_stack_element {
372 char data;
373};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700374typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700375
Andrew Boie73abd322017-04-04 13:19:13 -0700376/* timeouts */
377
378struct _timeout;
379typedef void (*_timeout_func_t)(struct _timeout *t);
380
381struct _timeout {
382 sys_dnode_t node;
383 struct k_thread *thread;
384 sys_dlist_t *wait_q;
385 s32_t delta_ticks_from_prev;
386 _timeout_func_t func;
387};
388
389extern s32_t _timeout_remaining_get(struct _timeout *timeout);
390
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700391/**
392 * @typedef k_thread_entry_t
393 * @brief Thread entry point function type.
394 *
395 * A thread's entry point function is invoked when the thread starts executing.
396 * Up to 3 argument values can be passed to the function.
397 *
398 * The thread terminates execution permanently if the entry point function
399 * returns. The thread is responsible for releasing any shared resources
400 * it may own (such as mutexes and dynamically allocated memory), prior to
401 * returning.
402 *
403 * @param p1 First argument.
404 * @param p2 Second argument.
405 * @param p3 Third argument.
406 *
407 * @return N/A
408 */
409typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700410
411#ifdef CONFIG_THREAD_MONITOR
412struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700413 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700414 void *parameter1;
415 void *parameter2;
416 void *parameter3;
417};
418#endif
419
420/* can be used for creating 'dummy' threads, e.g. for pending on objects */
421struct _thread_base {
422
423 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700424 union {
425 sys_dlist_t qnode_dlist;
426 struct rbnode qnode_rb;
427 };
428
429#ifdef CONFIG_WAITQ_FAST
430 /* wait queue on which the thread is pended (needed only for
431 * trees, not dumb lists)
432 */
433 _wait_q_t *pended_on;
434#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700435
436 /* user facing 'thread options'; values defined in include/kernel.h */
437 u8_t user_options;
438
439 /* thread state */
440 u8_t thread_state;
441
442 /*
443 * scheduler lock count and thread priority
444 *
445 * These two fields control the preemptibility of a thread.
446 *
447 * When the scheduler is locked, sched_locked is decremented, which
448 * means that the scheduler is locked for values from 0xff to 0x01. A
449 * thread is coop if its prio is negative, thus 0x80 to 0xff when
450 * looked at the value as unsigned.
451 *
452 * By putting them end-to-end, this means that a thread is
453 * non-preemptible if the bundled value is greater than or equal to
454 * 0x0080.
455 */
456 union {
457 struct {
458#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
459 u8_t sched_locked;
460 s8_t prio;
461#else /* LITTLE and PDP */
462 s8_t prio;
463 u8_t sched_locked;
464#endif
465 };
466 u16_t preempt;
467 };
468
Andy Ross1acd8c22018-05-03 14:51:49 -0700469 u32_t order_key;
470
Andy Ross2724fd12018-01-29 14:55:20 -0800471#ifdef CONFIG_SMP
472 /* True for the per-CPU idle threads */
473 u8_t is_idle;
474
Andy Ross2724fd12018-01-29 14:55:20 -0800475 /* CPU index on which thread was last run */
476 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700477
478 /* Recursive count of irq_lock() calls */
479 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800480#endif
481
Andrew Boie73abd322017-04-04 13:19:13 -0700482 /* data returned by APIs */
483 void *swap_data;
484
485#ifdef CONFIG_SYS_CLOCK_EXISTS
486 /* this thread's entry in a timeout queue */
487 struct _timeout timeout;
488#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700489};
490
491typedef struct _thread_base _thread_base_t;
492
493#if defined(CONFIG_THREAD_STACK_INFO)
494/* Contains the stack information of a thread */
495struct _thread_stack_info {
496 /* Stack Start */
497 u32_t start;
498 /* Stack Size */
499 u32_t size;
500};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700501
502typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700503#endif /* CONFIG_THREAD_STACK_INFO */
504
Chunlin Hane9c97022017-07-07 20:29:30 +0800505#if defined(CONFIG_USERSPACE)
506struct _mem_domain_info {
507 /* memory domain queue node */
508 sys_dnode_t mem_domain_q_node;
509 /* memory domain of the thread */
510 struct k_mem_domain *mem_domain;
511};
512
513#endif /* CONFIG_USERSPACE */
514
Andrew Boie73abd322017-04-04 13:19:13 -0700515struct k_thread {
516
517 struct _thread_base base;
518
519 /* defined by the architecture, but all archs need these */
520 struct _caller_saved caller_saved;
521 struct _callee_saved callee_saved;
522
523 /* static thread init data */
524 void *init_data;
525
526 /* abort function */
527 void (*fn_abort)(void);
528
529#if defined(CONFIG_THREAD_MONITOR)
530 /* thread entry and parameters description */
531 struct __thread_entry *entry;
532
533 /* next item in list of all threads */
534 struct k_thread *next_thread;
535#endif
536
537#ifdef CONFIG_THREAD_CUSTOM_DATA
538 /* crude thread-local storage */
539 void *custom_data;
540#endif
541
542#ifdef CONFIG_ERRNO
543 /* per-thread errno variable */
544 int errno_var;
545#endif
546
547#if defined(CONFIG_THREAD_STACK_INFO)
548 /* Stack Info */
549 struct _thread_stack_info stack_info;
550#endif /* CONFIG_THREAD_STACK_INFO */
551
Chunlin Hane9c97022017-07-07 20:29:30 +0800552#if defined(CONFIG_USERSPACE)
553 /* memory domain info of the thread */
554 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700555 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700556 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800557#endif /* CONFIG_USERSPACE */
558
Andy Ross042d8ec2017-12-09 08:37:20 -0800559#if defined(CONFIG_USE_SWITCH)
560 /* When using __switch() a few previously arch-specific items
561 * become part of the core OS
562 */
563
564 /* _Swap() return value */
565 int swap_retval;
566
567 /* Context handle returned via _arch_switch() */
568 void *switch_handle;
569#endif
Andrew Boie92e5bd72018-04-12 17:12:15 -0700570 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800571
Andrew Boie73abd322017-04-04 13:19:13 -0700572 /* arch-specifics: must always be at the end */
573 struct _thread_arch arch;
574};
575
576typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400577typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700578#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400579
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400580enum execution_context_types {
581 K_ISR = 0,
582 K_COOP_THREAD,
583 K_PREEMPT_THREAD,
584};
585
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400586/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100587 * @defgroup profiling_apis Profiling APIs
588 * @ingroup kernel_apis
589 * @{
590 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530591typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
592 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100593
594/**
595 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
596 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700597 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100598 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
599 *
600 * CONFIG_MAIN_STACK_SIZE
601 * CONFIG_IDLE_STACK_SIZE
602 * CONFIG_ISR_STACK_SIZE
603 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
604 *
605 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
606 * produce output.
607 *
608 * @return N/A
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530609 *
610 * @deprecated This API is deprecated. Use k_thread_foreach().
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100611 */
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530612__deprecated extern void k_call_stacks_analyze(void);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100613
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530614/**
615 * @brief Iterate over all the threads in the system.
616 *
617 * This routine iterates over all the threads in the system and
618 * calls the user_cb function for each thread.
619 *
620 * @param user_cb Pointer to the user callback function.
621 * @param user_data Pointer to user data.
622 *
623 * @note CONFIG_THREAD_MONITOR must be set for this function
624 * to be effective. Also this API uses irq_lock to protect the
625 * _kernel.threads list which means creation of new threads and
626 * terminations of existing threads are blocked until this
627 * API returns.
628 *
629 * @return N/A
630 */
631extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
632
Anas Nashif166f5192018-02-25 08:02:36 -0600633/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100634
635/**
Allan Stephensc98da842016-11-11 15:45:03 -0500636 * @defgroup thread_apis Thread APIs
637 * @ingroup kernel_apis
638 * @{
639 */
640
Benjamin Walshed240f22017-01-22 13:05:08 -0500641#endif /* !_ASMLANGUAGE */
642
643
644/*
645 * Thread user options. May be needed by assembly code. Common part uses low
646 * bits, arch-specific use high bits.
647 */
648
649/* system thread that must not abort */
650#define K_ESSENTIAL (1 << 0)
651
652#if defined(CONFIG_FP_SHARING)
653/* thread uses floating point registers */
654#define K_FP_REGS (1 << 1)
655#endif
656
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700657/* This thread has dropped from supervisor mode to user mode and consequently
658 * has additional restrictions
659 */
660#define K_USER (1 << 2)
661
Andrew Boie47f8fd12017-10-05 11:11:02 -0700662/* Indicates that the thread being created should inherit all kernel object
663 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
664 * is not enabled.
665 */
666#define K_INHERIT_PERMS (1 << 3)
667
Benjamin Walshed240f22017-01-22 13:05:08 -0500668#ifdef CONFIG_X86
669/* x86 Bitmask definitions for threads user options */
670
671#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
672/* thread uses SSEx (and also FP) registers */
673#define K_SSE_REGS (1 << 7)
674#endif
675#endif
676
677/* end - thread options */
678
679#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400680/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700681 * @brief Create a thread.
682 *
683 * This routine initializes a thread, then schedules it for execution.
684 *
685 * The new thread may be scheduled for immediate execution or a delayed start.
686 * If the newly spawned thread does not have a delayed start the kernel
687 * scheduler may preempt the current thread to allow the new thread to
688 * execute.
689 *
690 * Thread options are architecture-specific, and can include K_ESSENTIAL,
691 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
692 * them using "|" (the logical OR operator).
693 *
694 * Historically, users often would use the beginning of the stack memory region
695 * to store the struct k_thread data, although corruption will occur if the
696 * stack overflows this region and stack protection features may not detect this
697 * situation.
698 *
699 * @param new_thread Pointer to uninitialized struct k_thread
700 * @param stack Pointer to the stack space.
701 * @param stack_size Stack size in bytes.
702 * @param entry Thread entry function.
703 * @param p1 1st entry point parameter.
704 * @param p2 2nd entry point parameter.
705 * @param p3 3rd entry point parameter.
706 * @param prio Thread priority.
707 * @param options Thread options.
708 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
709 *
710 * @return ID of new thread.
711 */
Andrew Boie662c3452017-10-02 10:51:18 -0700712__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700713 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700714 size_t stack_size,
715 k_thread_entry_t entry,
716 void *p1, void *p2, void *p3,
717 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700718
Andrew Boie3f091b52017-08-30 14:34:14 -0700719/**
720 * @brief Drop a thread's privileges permanently to user mode
721 *
722 * @param entry Function to start executing from
723 * @param p1 1st entry point parameter
724 * @param p2 2nd entry point parameter
725 * @param p3 3rd entry point parameter
726 */
727extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
728 void *p1, void *p2,
729 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700730
Andrew Boied26cf2d2017-03-30 13:07:02 -0700731/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700732 * @brief Grant a thread access to a NULL-terminated set of kernel objects
733 *
734 * This is a convenience function. For the provided thread, grant access to
735 * the remaining arguments, which must be pointers to kernel objects.
736 * The final argument must be a NULL.
737 *
738 * The thread object must be initialized (i.e. running). The objects don't
739 * need to be.
740 *
741 * @param thread Thread to grant access to objects
742 * @param ... NULL-terminated list of kernel object pointers
743 */
744extern void __attribute__((sentinel))
745 k_thread_access_grant(struct k_thread *thread, ...);
746
747/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700748 * @brief Assign a resource memory pool to a thread
749 *
750 * By default, threads have no resource pool assigned unless their parent
751 * thread has a resource pool, in which case it is inherited. Multiple
752 * threads may be assigned to the same memory pool.
753 *
754 * Changing a thread's resource pool will not migrate allocations from the
755 * previous pool.
756 *
757 * @param thread Target thread to assign a memory pool for resource requests,
758 * or NULL if the thread should no longer have a memory pool.
759 * @param pool Memory pool to use for resources.
760 */
761static inline void k_thread_resource_pool_assign(struct k_thread *thread,
762 struct k_mem_pool *pool)
763{
764 thread->resource_pool = pool;
765}
766
767#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
768/**
769 * @brief Assign the system heap as a thread's resource pool
770 *
771 * Similar to k_thread_resource_pool_assign(), but the thread will use
772 * the kernel heap to draw memory.
773 *
774 * Use with caution, as a malicious thread could perform DoS attacks on the
775 * kernel heap.
776 *
777 * @param thread Target thread to assign the system heap for resource requests
778 */
779void k_thread_system_pool_assign(struct k_thread *thread);
780#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
781
782/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500783 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400784 *
Allan Stephensc98da842016-11-11 15:45:03 -0500785 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500786 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500788 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400789 *
790 * @return N/A
791 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700792__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400793
794/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500795 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400796 *
797 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500798 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400799 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400800 * @return N/A
801 */
Kumar Galacc334c72017-04-21 10:55:34 -0500802extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400803
804/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500805 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400806 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500807 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400808 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500809 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810 *
811 * @return N/A
812 */
Andrew Boie468190a2017-09-29 14:00:48 -0700813__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400814
815/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500816 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400817 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500818 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500820 * If @a thread is not currently sleeping, the routine has no effect.
821 *
822 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400823 *
824 * @return N/A
825 */
Andrew Boie468190a2017-09-29 14:00:48 -0700826__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827
828/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500829 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500831 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700833__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400834
835/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500836 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500838 * This routine prevents @a thread from executing if it has not yet started
839 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500841 * @param thread ID of thread to cancel.
842 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700843 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500844 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700845 *
846 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400847 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700848__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400849
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400850/**
Allan Stephensc98da842016-11-11 15:45:03 -0500851 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500853 * This routine permanently stops execution of @a thread. The thread is taken
854 * off all kernel queues it is part of (i.e. the ready queue, the timeout
855 * queue, or a kernel object wait queue). However, any kernel resources the
856 * thread might currently own (such as mutexes or memory blocks) are not
857 * released. It is the responsibility of the caller of this routine to ensure
858 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500860 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400861 *
862 * @return N/A
863 */
Andrew Boie468190a2017-09-29 14:00:48 -0700864__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400865
Andrew Boie7d627c52017-08-30 11:01:56 -0700866
867/**
868 * @brief Start an inactive thread
869 *
870 * If a thread was created with K_FOREVER in the delay parameter, it will
871 * not be added to the scheduling queue until this function is called
872 * on it.
873 *
874 * @param thread thread to start
875 */
Andrew Boie468190a2017-09-29 14:00:48 -0700876__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700877
Allan Stephensc98da842016-11-11 15:45:03 -0500878/**
879 * @cond INTERNAL_HIDDEN
880 */
881
Benjamin Walshd211a522016-12-06 11:44:01 -0500882/* timeout has timed out and is not on _timeout_q anymore */
883#define _EXPIRED (-2)
884
885/* timeout is not in use */
886#define _INACTIVE (-1)
887
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400888struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700889 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700890 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400891 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700892 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500893 void *init_p1;
894 void *init_p2;
895 void *init_p3;
896 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500897 u32_t init_options;
898 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500899 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400900};
901
Andrew Boied26cf2d2017-03-30 13:07:02 -0700902#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400903 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500904 prio, options, delay, abort, groups) \
905 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700906 .init_thread = (thread), \
907 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500908 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700909 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400910 .init_p1 = (void *)p1, \
911 .init_p2 = (void *)p2, \
912 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500913 .init_prio = (prio), \
914 .init_options = (options), \
915 .init_delay = (delay), \
916 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400917 }
918
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400919/**
Allan Stephensc98da842016-11-11 15:45:03 -0500920 * INTERNAL_HIDDEN @endcond
921 */
922
923/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500924 * @brief Statically define and initialize a thread.
925 *
926 * The thread may be scheduled for immediate execution or a delayed start.
927 *
928 * Thread options are architecture-specific, and can include K_ESSENTIAL,
929 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
930 * them using "|" (the logical OR operator).
931 *
932 * The ID of the thread can be accessed using:
933 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500934 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500935 *
936 * @param name Name of the thread.
937 * @param stack_size Stack size in bytes.
938 * @param entry Thread entry function.
939 * @param p1 1st entry point parameter.
940 * @param p2 2nd entry point parameter.
941 * @param p3 3rd entry point parameter.
942 * @param prio Thread priority.
943 * @param options Thread options.
944 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400945 *
946 * @internal It has been observed that the x86 compiler by default aligns
947 * these _static_thread_data structures to 32-byte boundaries, thereby
948 * wasting space. To work around this, force a 4-byte alignment.
949 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500950#define K_THREAD_DEFINE(name, stack_size, \
951 entry, p1, p2, p3, \
952 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700953 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700954 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500955 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500956 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700957 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
958 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500959 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700960 NULL, 0); \
961 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400962
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400963/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500964 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400965 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500966 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400967 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500968 * @param thread ID of thread whose priority is needed.
969 *
970 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400971 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700972__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400973
974/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500975 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500977 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400978 *
979 * Rescheduling can occur immediately depending on the priority @a thread is
980 * set to:
981 *
982 * - If its priority is raised above the priority of the caller of this
983 * function, and the caller is preemptible, @a thread will be scheduled in.
984 *
985 * - If the caller operates on itself, it lowers its priority below that of
986 * other threads in the system, and the caller is preemptible, the thread of
987 * highest priority will be scheduled in.
988 *
989 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
990 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
991 * highest priority.
992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500993 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400994 * @param prio New priority.
995 *
996 * @warning Changing the priority of a thread currently involved in mutex
997 * priority inheritance may result in undefined behavior.
998 *
999 * @return N/A
1000 */
Andrew Boie468190a2017-09-29 14:00:48 -07001001__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001003/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001004 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001006 * This routine prevents the kernel scheduler from making @a thread the
1007 * current thread. All other internal operations on @a thread are still
1008 * performed; for example, any timeout it is waiting on keeps ticking,
1009 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001011 * If @a thread is already suspended, the routine has no effect.
1012 *
1013 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001014 *
1015 * @return N/A
1016 */
Andrew Boie468190a2017-09-29 14:00:48 -07001017__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001018
1019/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001020 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001021 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001022 * This routine allows the kernel scheduler to make @a thread the current
1023 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001025 * If @a thread is not currently suspended, the routine has no effect.
1026 *
1027 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001028 *
1029 * @return N/A
1030 */
Andrew Boie468190a2017-09-29 14:00:48 -07001031__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001032
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001033/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001034 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001035 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001036 * This routine specifies how the scheduler will perform time slicing of
1037 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001038 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001039 * To enable time slicing, @a slice must be non-zero. The scheduler
1040 * ensures that no thread runs for more than the specified time limit
1041 * before other threads of that priority are given a chance to execute.
1042 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001043 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001044 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001045 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001046 * execute. Once the scheduler selects a thread for execution, there is no
1047 * minimum guaranteed time the thread will execute before threads of greater or
1048 * equal priority are scheduled.
1049 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001050 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001051 * for execution, this routine has no effect; the thread is immediately
1052 * rescheduled after the slice period expires.
1053 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001054 * To disable timeslicing, set both @a slice and @a prio to zero.
1055 *
1056 * @param slice Maximum time slice length (in milliseconds).
1057 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001058 *
1059 * @return N/A
1060 */
Kumar Galacc334c72017-04-21 10:55:34 -05001061extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001062
Anas Nashif166f5192018-02-25 08:02:36 -06001063/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001064
1065/**
1066 * @addtogroup isr_apis
1067 * @{
1068 */
1069
1070/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001071 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001072 *
Allan Stephensc98da842016-11-11 15:45:03 -05001073 * This routine allows the caller to customize its actions, depending on
1074 * whether it is a thread or an ISR.
1075 *
1076 * @note Can be called by ISRs.
1077 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001078 * @return 0 if invoked by a thread.
1079 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001080 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001081extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001082
Benjamin Walsh445830d2016-11-10 15:54:27 -05001083/**
1084 * @brief Determine if code is running in a preemptible thread.
1085 *
Allan Stephensc98da842016-11-11 15:45:03 -05001086 * This routine allows the caller to customize its actions, depending on
1087 * whether it can be preempted by another thread. The routine returns a 'true'
1088 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001089 *
Allan Stephensc98da842016-11-11 15:45:03 -05001090 * - The code is running in a thread, not at ISR.
1091 * - The thread's priority is in the preemptible range.
1092 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001093 *
Allan Stephensc98da842016-11-11 15:45:03 -05001094 * @note Can be called by ISRs.
1095 *
1096 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001097 * @return Non-zero if invoked by a preemptible thread.
1098 */
Andrew Boie468190a2017-09-29 14:00:48 -07001099__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001100
Allan Stephensc98da842016-11-11 15:45:03 -05001101/**
Anas Nashif166f5192018-02-25 08:02:36 -06001102 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001103 */
1104
1105/**
1106 * @addtogroup thread_apis
1107 * @{
1108 */
1109
1110/**
1111 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001112 *
Allan Stephensc98da842016-11-11 15:45:03 -05001113 * This routine prevents the current thread from being preempted by another
1114 * thread by instructing the scheduler to treat it as a cooperative thread.
1115 * If the thread subsequently performs an operation that makes it unready,
1116 * it will be context switched out in the normal manner. When the thread
1117 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001118 *
Allan Stephensc98da842016-11-11 15:45:03 -05001119 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001120 *
Allan Stephensc98da842016-11-11 15:45:03 -05001121 * @note k_sched_lock() and k_sched_unlock() should normally be used
1122 * when the operation being performed can be safely interrupted by ISRs.
1123 * However, if the amount of processing involved is very small, better
1124 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001125 *
1126 * @return N/A
1127 */
1128extern void k_sched_lock(void);
1129
Allan Stephensc98da842016-11-11 15:45:03 -05001130/**
1131 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001132 *
Allan Stephensc98da842016-11-11 15:45:03 -05001133 * This routine reverses the effect of a previous call to k_sched_lock().
1134 * A thread must call the routine once for each time it called k_sched_lock()
1135 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001136 *
1137 * @return N/A
1138 */
1139extern void k_sched_unlock(void);
1140
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001141/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001142 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001144 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001145 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001146 * Custom data is not used by the kernel itself, and is freely available
1147 * for a thread to use as it sees fit. It can be used as a framework
1148 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001149 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001150 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001151 *
1152 * @return N/A
1153 */
Andrew Boie468190a2017-09-29 14:00:48 -07001154__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001155
1156/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001157 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001158 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001159 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001160 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001161 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001162 */
Andrew Boie468190a2017-09-29 14:00:48 -07001163__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001164
1165/**
Anas Nashif166f5192018-02-25 08:02:36 -06001166 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001167 */
1168
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001169#include <sys_clock.h>
1170
Allan Stephensc2f15a42016-11-17 12:24:22 -05001171/**
1172 * @addtogroup clock_apis
1173 * @{
1174 */
1175
1176/**
1177 * @brief Generate null timeout delay.
1178 *
1179 * This macro generates a timeout delay that that instructs a kernel API
1180 * not to wait if the requested operation cannot be performed immediately.
1181 *
1182 * @return Timeout delay value.
1183 */
1184#define K_NO_WAIT 0
1185
1186/**
1187 * @brief Generate timeout delay from milliseconds.
1188 *
1189 * This macro generates a timeout delay that that instructs a kernel API
1190 * to wait up to @a ms milliseconds to perform the requested operation.
1191 *
1192 * @param ms Duration in milliseconds.
1193 *
1194 * @return Timeout delay value.
1195 */
Johan Hedberg14471692016-11-13 10:52:15 +02001196#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001197
1198/**
1199 * @brief Generate timeout delay from seconds.
1200 *
1201 * This macro generates a timeout delay that that instructs a kernel API
1202 * to wait up to @a s seconds to perform the requested operation.
1203 *
1204 * @param s Duration in seconds.
1205 *
1206 * @return Timeout delay value.
1207 */
Johan Hedberg14471692016-11-13 10:52:15 +02001208#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001209
1210/**
1211 * @brief Generate timeout delay from minutes.
1212 *
1213 * This macro generates a timeout delay that that instructs a kernel API
1214 * to wait up to @a m minutes to perform the requested operation.
1215 *
1216 * @param m Duration in minutes.
1217 *
1218 * @return Timeout delay value.
1219 */
Johan Hedberg14471692016-11-13 10:52:15 +02001220#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001221
1222/**
1223 * @brief Generate timeout delay from hours.
1224 *
1225 * This macro generates a timeout delay that that instructs a kernel API
1226 * to wait up to @a h hours to perform the requested operation.
1227 *
1228 * @param h Duration in hours.
1229 *
1230 * @return Timeout delay value.
1231 */
Johan Hedberg14471692016-11-13 10:52:15 +02001232#define K_HOURS(h) K_MINUTES((h) * 60)
1233
Allan Stephensc98da842016-11-11 15:45:03 -05001234/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001235 * @brief Generate infinite timeout delay.
1236 *
1237 * This macro generates a timeout delay that that instructs a kernel API
1238 * to wait as long as necessary to perform the requested operation.
1239 *
1240 * @return Timeout delay value.
1241 */
1242#define K_FOREVER (-1)
1243
1244/**
Anas Nashif166f5192018-02-25 08:02:36 -06001245 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001246 */
1247
1248/**
Allan Stephensc98da842016-11-11 15:45:03 -05001249 * @cond INTERNAL_HIDDEN
1250 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001251
Benjamin Walsh62092182016-12-20 14:39:08 -05001252/* kernel clocks */
1253
1254#if (sys_clock_ticks_per_sec == 1000) || \
1255 (sys_clock_ticks_per_sec == 500) || \
1256 (sys_clock_ticks_per_sec == 250) || \
1257 (sys_clock_ticks_per_sec == 125) || \
1258 (sys_clock_ticks_per_sec == 100) || \
1259 (sys_clock_ticks_per_sec == 50) || \
1260 (sys_clock_ticks_per_sec == 25) || \
1261 (sys_clock_ticks_per_sec == 20) || \
1262 (sys_clock_ticks_per_sec == 10) || \
1263 (sys_clock_ticks_per_sec == 1)
1264
1265 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1266#else
1267 /* yields horrible 64-bit math on many architectures: try to avoid */
1268 #define _NON_OPTIMIZED_TICKS_PER_SEC
1269#endif
1270
1271#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001272extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001273#else
Kumar Galacc334c72017-04-21 10:55:34 -05001274static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001275{
Kumar Galacc334c72017-04-21 10:55:34 -05001276 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001277}
1278#endif
1279
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001280/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001281#ifdef CONFIG_TICKLESS_KERNEL
1282#define _TICK_ALIGN 0
1283#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001284#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001285#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001286
Kumar Galacc334c72017-04-21 10:55:34 -05001287static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001288{
Benjamin Walsh62092182016-12-20 14:39:08 -05001289#ifdef CONFIG_SYS_CLOCK_EXISTS
1290
1291#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001292 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001293#else
Kumar Galacc334c72017-04-21 10:55:34 -05001294 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001295#endif
1296
1297#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001298 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001299 return 0;
1300#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001301}
1302
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001303struct k_timer {
1304 /*
1305 * _timeout structure must be first here if we want to use
1306 * dynamic timer allocation. timeout.node is used in the double-linked
1307 * list of free timers
1308 */
1309 struct _timeout timeout;
1310
Allan Stephens45bfa372016-10-12 12:39:42 -05001311 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001312 _wait_q_t wait_q;
1313
1314 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001315 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001316
1317 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001318 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001319
1320 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001321 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001322
Allan Stephens45bfa372016-10-12 12:39:42 -05001323 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001324 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001325
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001326 /* user-specific data, also used to support legacy features */
1327 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001328
Anas Nashif2f203c22016-12-18 06:57:45 -05001329 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001330};
1331
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001332#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001333 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001334 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001335 .timeout.wait_q = NULL, \
1336 .timeout.thread = NULL, \
1337 .timeout.func = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001338 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001339 .expiry_fn = expiry, \
1340 .stop_fn = stop, \
1341 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001342 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001343 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001344 }
1345
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001346#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1347
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001348/**
Allan Stephensc98da842016-11-11 15:45:03 -05001349 * INTERNAL_HIDDEN @endcond
1350 */
1351
1352/**
1353 * @defgroup timer_apis Timer APIs
1354 * @ingroup kernel_apis
1355 * @{
1356 */
1357
1358/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001359 * @typedef k_timer_expiry_t
1360 * @brief Timer expiry function type.
1361 *
1362 * A timer's expiry function is executed by the system clock interrupt handler
1363 * each time the timer expires. The expiry function is optional, and is only
1364 * invoked if the timer has been initialized with one.
1365 *
1366 * @param timer Address of timer.
1367 *
1368 * @return N/A
1369 */
1370typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1371
1372/**
1373 * @typedef k_timer_stop_t
1374 * @brief Timer stop function type.
1375 *
1376 * A timer's stop function is executed if the timer is stopped prematurely.
1377 * The function runs in the context of the thread that stops the timer.
1378 * The stop function is optional, and is only invoked if the timer has been
1379 * initialized with one.
1380 *
1381 * @param timer Address of timer.
1382 *
1383 * @return N/A
1384 */
1385typedef void (*k_timer_stop_t)(struct k_timer *timer);
1386
1387/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001388 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001390 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001391 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001392 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001393 *
1394 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001395 * @param expiry_fn Function to invoke each time the timer expires.
1396 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001397 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001398#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001399 struct k_timer name \
1400 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001401 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001402
Allan Stephens45bfa372016-10-12 12:39:42 -05001403/**
1404 * @brief Initialize a timer.
1405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001406 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001407 *
1408 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001409 * @param expiry_fn Function to invoke each time the timer expires.
1410 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001411 *
1412 * @return N/A
1413 */
1414extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001415 k_timer_expiry_t expiry_fn,
1416 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001417
Allan Stephens45bfa372016-10-12 12:39:42 -05001418/**
1419 * @brief Start a timer.
1420 *
1421 * This routine starts a timer, and resets its status to zero. The timer
1422 * begins counting down using the specified duration and period values.
1423 *
1424 * Attempting to start a timer that is already running is permitted.
1425 * The timer's status is reset to zero and the timer begins counting down
1426 * using the new duration and period values.
1427 *
1428 * @param timer Address of timer.
1429 * @param duration Initial timer duration (in milliseconds).
1430 * @param period Timer period (in milliseconds).
1431 *
1432 * @return N/A
1433 */
Andrew Boiea354d492017-09-29 16:22:28 -07001434__syscall void k_timer_start(struct k_timer *timer,
1435 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001436
1437/**
1438 * @brief Stop a timer.
1439 *
1440 * This routine stops a running timer prematurely. The timer's stop function,
1441 * if one exists, is invoked by the caller.
1442 *
1443 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001444 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001445 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001446 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1447 * if @a k_timer_stop is to be called from ISRs.
1448 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001449 * @param timer Address of timer.
1450 *
1451 * @return N/A
1452 */
Andrew Boiea354d492017-09-29 16:22:28 -07001453__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001454
1455/**
1456 * @brief Read timer status.
1457 *
1458 * This routine reads the timer's status, which indicates the number of times
1459 * it has expired since its status was last read.
1460 *
1461 * Calling this routine resets the timer's status to zero.
1462 *
1463 * @param timer Address of timer.
1464 *
1465 * @return Timer status.
1466 */
Andrew Boiea354d492017-09-29 16:22:28 -07001467__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001468
1469/**
1470 * @brief Synchronize thread to timer expiration.
1471 *
1472 * This routine blocks the calling thread until the timer's status is non-zero
1473 * (indicating that it has expired at least once since it was last examined)
1474 * or the timer is stopped. If the timer status is already non-zero,
1475 * or the timer is already stopped, the caller continues without waiting.
1476 *
1477 * Calling this routine resets the timer's status to zero.
1478 *
1479 * This routine must not be used by interrupt handlers, since they are not
1480 * allowed to block.
1481 *
1482 * @param timer Address of timer.
1483 *
1484 * @return Timer status.
1485 */
Andrew Boiea354d492017-09-29 16:22:28 -07001486__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001487
1488/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001489 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001490 *
1491 * This routine computes the (approximate) time remaining before a running
1492 * timer next expires. If the timer is not running, it returns zero.
1493 *
1494 * @param timer Address of timer.
1495 *
1496 * @return Remaining time (in milliseconds).
1497 */
Andrew Boiea354d492017-09-29 16:22:28 -07001498__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1499
1500static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001501{
1502 return _timeout_remaining_get(&timer->timeout);
1503}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001504
Allan Stephensc98da842016-11-11 15:45:03 -05001505/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001506 * @brief Associate user-specific data with a timer.
1507 *
1508 * This routine records the @a user_data with the @a timer, to be retrieved
1509 * later.
1510 *
1511 * It can be used e.g. in a timer handler shared across multiple subsystems to
1512 * retrieve data specific to the subsystem this timer is associated with.
1513 *
1514 * @param timer Address of timer.
1515 * @param user_data User data to associate with the timer.
1516 *
1517 * @return N/A
1518 */
Andrew Boiea354d492017-09-29 16:22:28 -07001519__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1520
Anas Nashif954d5502018-02-25 08:37:28 -06001521/**
1522 * @internal
1523 */
Andrew Boiea354d492017-09-29 16:22:28 -07001524static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1525 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001526{
1527 timer->user_data = user_data;
1528}
1529
1530/**
1531 * @brief Retrieve the user-specific data from a timer.
1532 *
1533 * @param timer Address of timer.
1534 *
1535 * @return The user data.
1536 */
Andrew Boiea354d492017-09-29 16:22:28 -07001537__syscall void *k_timer_user_data_get(struct k_timer *timer);
1538
1539static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001540{
1541 return timer->user_data;
1542}
1543
Anas Nashif166f5192018-02-25 08:02:36 -06001544/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001545
Allan Stephensc98da842016-11-11 15:45:03 -05001546/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001547 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001548 * @{
1549 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001550
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001551/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001552 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001554 * This routine returns the elapsed time since the system booted,
1555 * in milliseconds.
1556 *
1557 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001558 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001559__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001560
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001561/**
1562 * @brief Enable clock always on in tickless kernel
1563 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001564 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001565 * there are no timer events programmed in tickless kernel
1566 * scheduling. This is necessary if the clock is used to track
1567 * passage of time.
1568 *
1569 * @retval prev_status Previous status of always on flag
1570 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301571#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001572static inline int k_enable_sys_clock_always_on(void)
1573{
1574 int prev_status = _sys_clock_always_on;
1575
1576 _sys_clock_always_on = 1;
1577 _enable_sys_clock();
1578
1579 return prev_status;
1580}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301581#else
1582#define k_enable_sys_clock_always_on() do { } while ((0))
1583#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001584
1585/**
1586 * @brief Disable clock always on in tickless kernel
1587 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001588 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001589 * there are no timer events programmed in tickless kernel
1590 * scheduling. To save power, this routine should be called
1591 * immediately when clock is not used to track time.
1592 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301593#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001594static inline void k_disable_sys_clock_always_on(void)
1595{
1596 _sys_clock_always_on = 0;
1597}
1598#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001599#define k_disable_sys_clock_always_on() do { } while ((0))
1600#endif
1601
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001602/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001603 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001605 * This routine returns the lower 32-bits of the elapsed time since the system
1606 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001608 * This routine can be more efficient than k_uptime_get(), as it reduces the
1609 * need for interrupt locking and 64-bit math. However, the 32-bit result
1610 * cannot hold a system uptime time larger than approximately 50 days, so the
1611 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001612 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001613 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001614 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001615__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001616
1617/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001618 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001619 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001620 * This routine computes the elapsed time between the current system uptime
1621 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001622 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001623 * @param reftime Pointer to a reference time, which is updated to the current
1624 * uptime upon return.
1625 *
1626 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001627 */
Kumar Galacc334c72017-04-21 10:55:34 -05001628extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001629
1630/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001631 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001632 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001633 * This routine computes the elapsed time between the current system uptime
1634 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001635 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001636 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1637 * need for interrupt locking and 64-bit math. However, the 32-bit result
1638 * cannot hold an elapsed time larger than approximately 50 days, so the
1639 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001640 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001641 * @param reftime Pointer to a reference time, which is updated to the current
1642 * uptime upon return.
1643 *
1644 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001645 */
Kumar Galacc334c72017-04-21 10:55:34 -05001646extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001647
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001648/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001649 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001651 * This routine returns the current time, as measured by the system's hardware
1652 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001653 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001654 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001655 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001656#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001657
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001658/**
Anas Nashif166f5192018-02-25 08:02:36 -06001659 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001660 */
1661
Allan Stephensc98da842016-11-11 15:45:03 -05001662/**
1663 * @cond INTERNAL_HIDDEN
1664 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001665
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001666struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001667 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001668 union {
1669 _wait_q_t wait_q;
1670
1671 _POLL_EVENT;
1672 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001673
1674 _OBJECT_TRACING_NEXT_PTR(k_queue);
1675};
1676
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001677#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001678 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001679 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001680 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001681 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001682 _OBJECT_TRACING_INIT \
1683 }
1684
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001685#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1686
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001687extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1688
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001689/**
1690 * INTERNAL_HIDDEN @endcond
1691 */
1692
1693/**
1694 * @defgroup queue_apis Queue APIs
1695 * @ingroup kernel_apis
1696 * @{
1697 */
1698
1699/**
1700 * @brief Initialize a queue.
1701 *
1702 * This routine initializes a queue object, prior to its first use.
1703 *
1704 * @param queue Address of the queue.
1705 *
1706 * @return N/A
1707 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001708__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001709
1710/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001711 * @brief Cancel waiting on a queue.
1712 *
1713 * This routine causes first thread pending on @a queue, if any, to
1714 * return from k_queue_get() call with NULL value (as if timeout expired).
1715 *
1716 * @note Can be called by ISRs.
1717 *
1718 * @param queue Address of the queue.
1719 *
1720 * @return N/A
1721 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001722__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001723
1724/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001725 * @brief Append an element to the end of a queue.
1726 *
1727 * This routine appends a data item to @a queue. A queue data item must be
1728 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1729 * reserved for the kernel's use.
1730 *
1731 * @note Can be called by ISRs.
1732 *
1733 * @param queue Address of the queue.
1734 * @param data Address of the data item.
1735 *
1736 * @return N/A
1737 */
1738extern void k_queue_append(struct k_queue *queue, void *data);
1739
1740/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001741 * @brief Append an element to a queue.
1742 *
1743 * This routine appends a data item to @a queue. There is an implicit
1744 * memory allocation from the calling thread's resource pool, which is
1745 * automatically freed when the item is removed from the queue.
1746 *
1747 * @note Can be called by ISRs.
1748 *
1749 * @param queue Address of the queue.
1750 * @param data Address of the data item.
1751 *
1752 * @retval 0 on success
1753 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1754 */
1755__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1756
1757/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001758 * @brief Prepend an element to a queue.
1759 *
1760 * This routine prepends a data item to @a queue. A queue data item must be
1761 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1762 * reserved for the kernel's use.
1763 *
1764 * @note Can be called by ISRs.
1765 *
1766 * @param queue Address of the queue.
1767 * @param data Address of the data item.
1768 *
1769 * @return N/A
1770 */
1771extern void k_queue_prepend(struct k_queue *queue, void *data);
1772
1773/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001774 * @brief Prepend an element to a queue.
1775 *
1776 * This routine prepends a data item to @a queue. There is an implicit
1777 * memory allocation from the calling thread's resource pool, which is
1778 * automatically freed when the item is removed from the queue.
1779 *
1780 * @note Can be called by ISRs.
1781 *
1782 * @param queue Address of the queue.
1783 * @param data Address of the data item.
1784 *
1785 * @retval 0 on success
1786 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1787 */
1788__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1789
1790/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001791 * @brief Inserts an element to a queue.
1792 *
1793 * This routine inserts a data item to @a queue after previous item. A queue
1794 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1795 * item are reserved for the kernel's use.
1796 *
1797 * @note Can be called by ISRs.
1798 *
1799 * @param queue Address of the queue.
1800 * @param prev Address of the previous data item.
1801 * @param data Address of the data item.
1802 *
1803 * @return N/A
1804 */
1805extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1806
1807/**
1808 * @brief Atomically append a list of elements to a queue.
1809 *
1810 * This routine adds a list of data items to @a queue in one operation.
1811 * The data items must be in a singly-linked list, with the first 32 bits
1812 * in each data item pointing to the next data item; the list must be
1813 * NULL-terminated.
1814 *
1815 * @note Can be called by ISRs.
1816 *
1817 * @param queue Address of the queue.
1818 * @param head Pointer to first node in singly-linked list.
1819 * @param tail Pointer to last node in singly-linked list.
1820 *
1821 * @return N/A
1822 */
1823extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1824
1825/**
1826 * @brief Atomically add a list of elements to a queue.
1827 *
1828 * This routine adds a list of data items to @a queue in one operation.
1829 * The data items must be in a singly-linked list implemented using a
1830 * sys_slist_t object. Upon completion, the original list is empty.
1831 *
1832 * @note Can be called by ISRs.
1833 *
1834 * @param queue Address of the queue.
1835 * @param list Pointer to sys_slist_t object.
1836 *
1837 * @return N/A
1838 */
1839extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1840
1841/**
1842 * @brief Get an element from a queue.
1843 *
1844 * This routine removes first data item from @a queue. The first 32 bits of the
1845 * data item are reserved for the kernel's use.
1846 *
1847 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1848 *
1849 * @param queue Address of the queue.
1850 * @param timeout Waiting period to obtain a data item (in milliseconds),
1851 * or one of the special values K_NO_WAIT and K_FOREVER.
1852 *
1853 * @return Address of the data item if successful; NULL if returned
1854 * without waiting, or waiting period timed out.
1855 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001856__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001857
1858/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001859 * @brief Remove an element from a queue.
1860 *
1861 * This routine removes data item from @a queue. The first 32 bits of the
1862 * data item are reserved for the kernel's use. Removing elements from k_queue
1863 * rely on sys_slist_find_and_remove which is not a constant time operation.
1864 *
1865 * @note Can be called by ISRs
1866 *
1867 * @param queue Address of the queue.
1868 * @param data Address of the data item.
1869 *
1870 * @return true if data item was removed
1871 */
1872static inline bool k_queue_remove(struct k_queue *queue, void *data)
1873{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001874 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001875}
1876
1877/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001878 * @brief Query a queue to see if it has data available.
1879 *
1880 * Note that the data might be already gone by the time this function returns
1881 * if other threads are also trying to read from the queue.
1882 *
1883 * @note Can be called by ISRs.
1884 *
1885 * @param queue Address of the queue.
1886 *
1887 * @return Non-zero if the queue is empty.
1888 * @return 0 if data is available.
1889 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001890__syscall int k_queue_is_empty(struct k_queue *queue);
1891
1892static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001893{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001894 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001895}
1896
1897/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001898 * @brief Peek element at the head of queue.
1899 *
1900 * Return element from the head of queue without removing it.
1901 *
1902 * @param queue Address of the queue.
1903 *
1904 * @return Head element, or NULL if queue is empty.
1905 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001906__syscall void *k_queue_peek_head(struct k_queue *queue);
1907
1908static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001909{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001910 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001911}
1912
1913/**
1914 * @brief Peek element at the tail of queue.
1915 *
1916 * Return element from the tail of queue without removing it.
1917 *
1918 * @param queue Address of the queue.
1919 *
1920 * @return Tail element, or NULL if queue is empty.
1921 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001922__syscall void *k_queue_peek_tail(struct k_queue *queue);
1923
1924static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001925{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001926 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001927}
1928
1929/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001930 * @brief Statically define and initialize a queue.
1931 *
1932 * The queue can be accessed outside the module where it is defined using:
1933 *
1934 * @code extern struct k_queue <name>; @endcode
1935 *
1936 * @param name Name of the queue.
1937 */
1938#define K_QUEUE_DEFINE(name) \
1939 struct k_queue name \
1940 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001941 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001942
Anas Nashif166f5192018-02-25 08:02:36 -06001943/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001944
1945/**
1946 * @cond INTERNAL_HIDDEN
1947 */
1948
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001949struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001950 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001951};
1952
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001953#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001954 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001955 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001956 }
1957
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001958#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1959
Allan Stephensc98da842016-11-11 15:45:03 -05001960/**
1961 * INTERNAL_HIDDEN @endcond
1962 */
1963
1964/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001965 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001966 * @ingroup kernel_apis
1967 * @{
1968 */
1969
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001970/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001971 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001972 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001973 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001974 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001975 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001976 *
1977 * @return N/A
1978 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001979#define k_fifo_init(fifo) \
1980 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001981
1982/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001983 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001984 *
1985 * This routine causes first thread pending on @a fifo, if any, to
1986 * return from k_fifo_get() call with NULL value (as if timeout
1987 * expired).
1988 *
1989 * @note Can be called by ISRs.
1990 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001991 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001992 *
1993 * @return N/A
1994 */
1995#define k_fifo_cancel_wait(fifo) \
1996 k_queue_cancel_wait((struct k_queue *) fifo)
1997
1998/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001999 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002000 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002001 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002002 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2003 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002005 * @note Can be called by ISRs.
2006 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002007 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002008 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002009 *
2010 * @return N/A
2011 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002012#define k_fifo_put(fifo, data) \
2013 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002014
2015/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002016 * @brief Add an element to a FIFO queue.
2017 *
2018 * This routine adds a data item to @a fifo. There is an implicit
2019 * memory allocation from the calling thread's resource pool, which is
2020 * automatically freed when the item is removed.
2021 *
2022 * @note Can be called by ISRs.
2023 *
2024 * @param fifo Address of the FIFO.
2025 * @param data Address of the data item.
2026 *
2027 * @retval 0 on success
2028 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2029 */
2030#define k_fifo_alloc_put(fifo, data) \
2031 k_queue_alloc_append((struct k_queue *) fifo, data)
2032
2033/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002034 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002035 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002036 * This routine adds a list of data items to @a fifo in one operation.
2037 * The data items must be in a singly-linked list, with the first 32 bits
2038 * each data item pointing to the next data item; the list must be
2039 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002040 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002041 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002042 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002043 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002044 * @param head Pointer to first node in singly-linked list.
2045 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002046 *
2047 * @return N/A
2048 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002049#define k_fifo_put_list(fifo, head, tail) \
2050 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002051
2052/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002053 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002054 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002055 * This routine adds a list of data items to @a fifo in one operation.
2056 * The data items must be in a singly-linked list implemented using a
2057 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002058 * and must be re-initialized via sys_slist_init().
2059 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002060 * @note Can be called by ISRs.
2061 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002062 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002063 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002064 *
2065 * @return N/A
2066 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002067#define k_fifo_put_slist(fifo, list) \
2068 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002069
2070/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002071 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002073 * This routine removes a data item from @a fifo in a "first in, first out"
2074 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002076 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2077 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002078 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002079 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002080 * or one of the special values K_NO_WAIT and K_FOREVER.
2081 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002082 * @return Address of the data item if successful; NULL if returned
2083 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002084 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002085#define k_fifo_get(fifo, timeout) \
2086 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002087
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002088/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002089 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002090 *
2091 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002092 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002093 *
2094 * @note Can be called by ISRs.
2095 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002096 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002097 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002098 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002099 * @return 0 if data is available.
2100 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002101#define k_fifo_is_empty(fifo) \
2102 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002103
2104/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002105 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002106 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002107 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302108 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002109 * on each iteration of processing, a head container will be peeked,
2110 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002111 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002112 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002113 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002114 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002115 * @return Head element, or NULL if the FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002116 */
2117#define k_fifo_peek_head(fifo) \
2118 k_queue_peek_head((struct k_queue *) fifo)
2119
2120/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002121 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002122 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002123 * Return element from the tail of FIFO queue (without removing it). A usecase
2124 * for this is if elements of the FIFO queue are themselves containers. Then
2125 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002126 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002127 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002128 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002129 * @return Tail element, or NULL if a FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002130 */
2131#define k_fifo_peek_tail(fifo) \
2132 k_queue_peek_tail((struct k_queue *) fifo)
2133
2134/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002135 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002136 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002137 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002138 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002139 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002140 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002141 * @param name Name of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002142 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002143#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002144 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002145 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002146 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002147
Anas Nashif166f5192018-02-25 08:02:36 -06002148/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002149
2150/**
2151 * @cond INTERNAL_HIDDEN
2152 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002153
2154struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002155 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002156};
2157
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002158#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002159 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002160 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002161 }
2162
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002163#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2164
Allan Stephensc98da842016-11-11 15:45:03 -05002165/**
2166 * INTERNAL_HIDDEN @endcond
2167 */
2168
2169/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002170 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002171 * @ingroup kernel_apis
2172 * @{
2173 */
2174
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002175/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002176 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002177 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002178 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002179 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002180 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002181 *
2182 * @return N/A
2183 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002184#define k_lifo_init(lifo) \
2185 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002186
2187/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002188 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002189 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002190 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002191 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2192 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002193 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002194 * @note Can be called by ISRs.
2195 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002196 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002197 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002198 *
2199 * @return N/A
2200 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002201#define k_lifo_put(lifo, data) \
2202 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002203
2204/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002205 * @brief Add an element to a LIFO queue.
2206 *
2207 * This routine adds a data item to @a lifo. There is an implicit
2208 * memory allocation from the calling thread's resource pool, which is
2209 * automatically freed when the item is removed.
2210 *
2211 * @note Can be called by ISRs.
2212 *
2213 * @param lifo Address of the LIFO.
2214 * @param data Address of the data item.
2215 *
2216 * @retval 0 on success
2217 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2218 */
2219#define k_lifo_alloc_put(lifo, data) \
2220 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2221
2222/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002223 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002224 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002225 * This routine removes a data item from @a lifo in a "last in, first out"
2226 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002227 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002228 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2229 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002230 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002231 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002232 * or one of the special values K_NO_WAIT and K_FOREVER.
2233 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002234 * @return Address of the data item if successful; NULL if returned
2235 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002236 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002237#define k_lifo_get(lifo, timeout) \
2238 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002239
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002240/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002241 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002242 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002243 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002244 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002245 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002246 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002247 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002248 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002249#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002250 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002251 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002252 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002253
Anas Nashif166f5192018-02-25 08:02:36 -06002254/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002255
2256/**
2257 * @cond INTERNAL_HIDDEN
2258 */
Andrew Boief3bee952018-05-02 17:44:39 -07002259#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002260
2261struct k_stack {
2262 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002263 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002264
Anas Nashif2f203c22016-12-18 06:57:45 -05002265 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002266 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002267};
2268
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002269#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002270 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002271 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002272 .base = stack_buffer, \
2273 .next = stack_buffer, \
2274 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002275 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002276 }
2277
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002278#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2279
Allan Stephensc98da842016-11-11 15:45:03 -05002280/**
2281 * INTERNAL_HIDDEN @endcond
2282 */
2283
2284/**
2285 * @defgroup stack_apis Stack APIs
2286 * @ingroup kernel_apis
2287 * @{
2288 */
2289
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002290/**
2291 * @brief Initialize a stack.
2292 *
2293 * This routine initializes a stack object, prior to its first use.
2294 *
2295 * @param stack Address of the stack.
2296 * @param buffer Address of array used to hold stacked values.
2297 * @param num_entries Maximum number of values that can be stacked.
2298 *
2299 * @return N/A
2300 */
Andrew Boief3bee952018-05-02 17:44:39 -07002301void k_stack_init(struct k_stack *stack,
2302 u32_t *buffer, unsigned int num_entries);
2303
2304
2305/**
2306 * @brief Initialize a stack.
2307 *
2308 * This routine initializes a stack object, prior to its first use. Internal
2309 * buffers will be allocated from the calling thread's resource pool.
2310 * This memory will be released if k_stack_cleanup() is called, or
2311 * userspace is enabled and the stack object loses all references to it.
2312 *
2313 * @param stack Address of the stack.
2314 * @param num_entries Maximum number of values that can be stacked.
2315 *
2316 * @return -ENOMEM if memory couldn't be allocated
2317 */
2318
2319__syscall int k_stack_alloc_init(struct k_stack *stack,
2320 unsigned int num_entries);
2321
2322/**
2323 * @brief Release a stack's allocated buffer
2324 *
2325 * If a stack object was given a dynamically allocated buffer via
2326 * k_stack_alloc_init(), this will free it. This function does nothing
2327 * if the buffer wasn't dynamically allocated.
2328 *
2329 * @param stack Address of the stack.
2330 */
2331void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002332
2333/**
2334 * @brief Push an element onto a stack.
2335 *
2336 * This routine adds a 32-bit value @a data to @a stack.
2337 *
2338 * @note Can be called by ISRs.
2339 *
2340 * @param stack Address of the stack.
2341 * @param data Value to push onto the stack.
2342 *
2343 * @return N/A
2344 */
Andrew Boiee8734462017-09-29 16:42:07 -07002345__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002346
2347/**
2348 * @brief Pop an element from a stack.
2349 *
2350 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2351 * manner and stores the value in @a data.
2352 *
2353 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2354 *
2355 * @param stack Address of the stack.
2356 * @param data Address of area to hold the value popped from the stack.
2357 * @param timeout Waiting period to obtain a value (in milliseconds),
2358 * or one of the special values K_NO_WAIT and K_FOREVER.
2359 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002360 * @retval 0 Element popped from stack.
2361 * @retval -EBUSY Returned without waiting.
2362 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002363 */
Andrew Boiee8734462017-09-29 16:42:07 -07002364__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002365
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002366/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002367 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002368 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002369 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002370 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002371 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002372 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002373 * @param name Name of the stack.
2374 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002375 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002376#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002377 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002378 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002379 struct k_stack name \
2380 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002381 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002382 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002383
Anas Nashif166f5192018-02-25 08:02:36 -06002384/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002385
Allan Stephens6bba9b02016-11-16 14:56:54 -05002386struct k_work;
2387
Allan Stephensc98da842016-11-11 15:45:03 -05002388/**
2389 * @defgroup workqueue_apis Workqueue Thread APIs
2390 * @ingroup kernel_apis
2391 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002392 */
2393
Allan Stephens6bba9b02016-11-16 14:56:54 -05002394/**
2395 * @typedef k_work_handler_t
2396 * @brief Work item handler function type.
2397 *
2398 * A work item's handler function is executed by a workqueue's thread
2399 * when the work item is processed by the workqueue.
2400 *
2401 * @param work Address of the work item.
2402 *
2403 * @return N/A
2404 */
2405typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002406
2407/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002408 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002409 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002410
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002411struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002412 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002413 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002414};
2415
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002416enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002417 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002418};
2419
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002420struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002421 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002422 k_work_handler_t handler;
2423 atomic_t flags[1];
2424};
2425
Allan Stephens6bba9b02016-11-16 14:56:54 -05002426struct k_delayed_work {
2427 struct k_work work;
2428 struct _timeout timeout;
2429 struct k_work_q *work_q;
2430};
2431
2432extern struct k_work_q k_sys_work_q;
2433
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002434/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002435 * INTERNAL_HIDDEN @endcond
2436 */
2437
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002438#define _K_WORK_INITIALIZER(work_handler) \
2439 { \
2440 ._reserved = NULL, \
2441 .handler = work_handler, \
2442 .flags = { 0 } \
2443 }
2444
2445#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2446
Allan Stephens6bba9b02016-11-16 14:56:54 -05002447/**
2448 * @brief Initialize a statically-defined work item.
2449 *
2450 * This macro can be used to initialize a statically-defined workqueue work
2451 * item, prior to its first use. For example,
2452 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002453 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002454 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002455 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002456 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002457 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002458#define K_WORK_DEFINE(work, work_handler) \
2459 struct k_work work \
2460 __in_section(_k_work, static, work) = \
2461 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002462
2463/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002464 * @brief Initialize a work item.
2465 *
2466 * This routine initializes a workqueue work item, prior to its first use.
2467 *
2468 * @param work Address of work item.
2469 * @param handler Function to invoke each time work item is processed.
2470 *
2471 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002472 */
2473static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2474{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002475 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002476 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002477 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002478}
2479
2480/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002481 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002482 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002483 * This routine submits work item @a work to be processed by workqueue
2484 * @a work_q. If the work item is already pending in the workqueue's queue
2485 * as a result of an earlier submission, this routine has no effect on the
2486 * work item. If the work item has already been processed, or is currently
2487 * being processed, its work is considered complete and the work item can be
2488 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002489 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002490 * @warning
2491 * A submitted work item must not be modified until it has been processed
2492 * by the workqueue.
2493 *
2494 * @note Can be called by ISRs.
2495 *
2496 * @param work_q Address of workqueue.
2497 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002498 *
2499 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002500 */
2501static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2502 struct k_work *work)
2503{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002504 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002505 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002506 }
2507}
2508
2509/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002510 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002511 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002512 * This routine indicates if work item @a work is pending in a workqueue's
2513 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002514 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002515 * @note Can be called by ISRs.
2516 *
2517 * @param work Address of work item.
2518 *
2519 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002520 */
2521static inline int k_work_pending(struct k_work *work)
2522{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002523 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002524}
2525
2526/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002527 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002528 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002529 * This routine starts workqueue @a work_q. The workqueue spawns its work
2530 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002531 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002532 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002533 * @param stack Pointer to work queue thread's stack space, as defined by
2534 * K_THREAD_STACK_DEFINE()
2535 * @param stack_size Size of the work queue thread's stack (in bytes), which
2536 * should either be the same constant passed to
2537 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002538 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002539 *
2540 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002541 */
Andrew Boie507852a2017-07-25 18:47:07 -07002542extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002543 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002544 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002545
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002546/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002547 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002548 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002549 * This routine initializes a workqueue delayed work item, prior to
2550 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002551 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002552 * @param work Address of delayed work item.
2553 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002554 *
2555 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002556 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002557extern void k_delayed_work_init(struct k_delayed_work *work,
2558 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002559
2560/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002561 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002562 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002563 * This routine schedules work item @a work to be processed by workqueue
2564 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002565 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002566 * Only when the countdown completes is the work item actually submitted to
2567 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002568 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002569 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002570 * counting down cancels the existing submission and restarts the
2571 * countdown using the new delay. Note that this behavior is
2572 * inherently subject to race conditions with the pre-existing
2573 * timeouts and work queue, so care must be taken to synchronize such
2574 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002575 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002576 * @warning
2577 * A delayed work item must not be modified until it has been processed
2578 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002579 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002580 * @note Can be called by ISRs.
2581 *
2582 * @param work_q Address of workqueue.
2583 * @param work Address of delayed work item.
2584 * @param delay Delay before submitting the work item (in milliseconds).
2585 *
2586 * @retval 0 Work item countdown started.
2587 * @retval -EINPROGRESS Work item is already pending.
2588 * @retval -EINVAL Work item is being processed or has completed its work.
2589 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002590 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002591extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2592 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002593 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002594
2595/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002596 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002597 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002598 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002599 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002600 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002601 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002602 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002603 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002604 * @param work Address of delayed work item.
2605 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002606 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002607 * @retval -EINPROGRESS Work item is already pending.
2608 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002609 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002610extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002611
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002612/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002613 * @brief Submit a work item to the system workqueue.
2614 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002615 * This routine submits work item @a work to be processed by the system
2616 * workqueue. If the work item is already pending in the workqueue's queue
2617 * as a result of an earlier submission, this routine has no effect on the
2618 * work item. If the work item has already been processed, or is currently
2619 * being processed, its work is considered complete and the work item can be
2620 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002621 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002622 * @warning
2623 * Work items submitted to the system workqueue should avoid using handlers
2624 * that block or yield since this may prevent the system workqueue from
2625 * processing other work items in a timely manner.
2626 *
2627 * @note Can be called by ISRs.
2628 *
2629 * @param work Address of work item.
2630 *
2631 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002632 */
2633static inline void k_work_submit(struct k_work *work)
2634{
2635 k_work_submit_to_queue(&k_sys_work_q, work);
2636}
2637
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002638/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002639 * @brief Submit a delayed work item to the system workqueue.
2640 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002641 * This routine schedules work item @a work to be processed by the system
2642 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002643 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002644 * Only when the countdown completes is the work item actually submitted to
2645 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002646 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002647 * Submitting a previously submitted delayed work item that is still
2648 * counting down cancels the existing submission and restarts the countdown
2649 * using the new delay. If the work item is currently pending on the
2650 * workqueue's queue because the countdown has completed it is too late to
2651 * resubmit the item, and resubmission fails without impacting the work item.
2652 * If the work item has already been processed, or is currently being processed,
2653 * its work is considered complete and the work item can be resubmitted.
2654 *
2655 * @warning
2656 * Work items submitted to the system workqueue should avoid using handlers
2657 * that block or yield since this may prevent the system workqueue from
2658 * processing other work items in a timely manner.
2659 *
2660 * @note Can be called by ISRs.
2661 *
2662 * @param work Address of delayed work item.
2663 * @param delay Delay before submitting the work item (in milliseconds).
2664 *
2665 * @retval 0 Work item countdown started.
2666 * @retval -EINPROGRESS Work item is already pending.
2667 * @retval -EINVAL Work item is being processed or has completed its work.
2668 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002669 */
2670static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002671 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002672{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002673 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002674}
2675
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002676/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002677 * @brief Get time remaining before a delayed work gets scheduled.
2678 *
2679 * This routine computes the (approximate) time remaining before a
2680 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002681 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002682 *
2683 * @param work Delayed work item.
2684 *
2685 * @return Remaining time (in milliseconds).
2686 */
Kumar Galacc334c72017-04-21 10:55:34 -05002687static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002688{
2689 return _timeout_remaining_get(&work->timeout);
2690}
2691
Anas Nashif166f5192018-02-25 08:02:36 -06002692/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002693
Allan Stephensc98da842016-11-11 15:45:03 -05002694/**
2695 * @cond INTERNAL_HIDDEN
2696 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002697
2698struct k_mutex {
2699 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002700 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002701 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002702 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002703
Anas Nashif2f203c22016-12-18 06:57:45 -05002704 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002705};
2706
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002707#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002708 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002709 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002710 .owner = NULL, \
2711 .lock_count = 0, \
2712 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002713 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002714 }
2715
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002716#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2717
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002718/**
Allan Stephensc98da842016-11-11 15:45:03 -05002719 * INTERNAL_HIDDEN @endcond
2720 */
2721
2722/**
2723 * @defgroup mutex_apis Mutex APIs
2724 * @ingroup kernel_apis
2725 * @{
2726 */
2727
2728/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002729 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002730 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002731 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002732 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002733 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002734 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002735 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002736 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002737#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002738 struct k_mutex name \
2739 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002740 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002741
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002742/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002743 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002744 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002745 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002746 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002747 * Upon completion, the mutex is available and does not have an owner.
2748 *
2749 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002750 *
2751 * @return N/A
2752 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002753__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002754
2755/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002756 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002757 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002758 * This routine locks @a mutex. If the mutex is locked by another thread,
2759 * the calling thread waits until the mutex becomes available or until
2760 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002761 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002762 * A thread is permitted to lock a mutex it has already locked. The operation
2763 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002764 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002765 * @param mutex Address of the mutex.
2766 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002767 * or one of the special values K_NO_WAIT and K_FOREVER.
2768 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002769 * @retval 0 Mutex locked.
2770 * @retval -EBUSY Returned without waiting.
2771 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002772 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002773__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002774
2775/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002776 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002777 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002778 * This routine unlocks @a mutex. The mutex must already be locked by the
2779 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002780 *
2781 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002782 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002783 * thread.
2784 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002785 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002786 *
2787 * @return N/A
2788 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002789__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002790
Allan Stephensc98da842016-11-11 15:45:03 -05002791/**
Anas Nashif166f5192018-02-25 08:02:36 -06002792 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002793 */
2794
2795/**
2796 * @cond INTERNAL_HIDDEN
2797 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002798
2799struct k_sem {
2800 _wait_q_t wait_q;
2801 unsigned int count;
2802 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002803 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002804
Anas Nashif2f203c22016-12-18 06:57:45 -05002805 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002806};
2807
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002808#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002809 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002810 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002811 .count = initial_count, \
2812 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002813 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002814 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002815 }
2816
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002817#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2818
Allan Stephensc98da842016-11-11 15:45:03 -05002819/**
2820 * INTERNAL_HIDDEN @endcond
2821 */
2822
2823/**
2824 * @defgroup semaphore_apis Semaphore APIs
2825 * @ingroup kernel_apis
2826 * @{
2827 */
2828
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002829/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002830 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002831 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002832 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002833 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002834 * @param sem Address of the semaphore.
2835 * @param initial_count Initial semaphore count.
2836 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002837 *
2838 * @return N/A
2839 */
Andrew Boie99280232017-09-29 14:17:47 -07002840__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2841 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002842
2843/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002844 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002846 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002848 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2849 *
2850 * @param sem Address of the semaphore.
2851 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002852 * or one of the special values K_NO_WAIT and K_FOREVER.
2853 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002854 * @note When porting code from the nanokernel legacy API to the new API, be
2855 * careful with the return value of this function. The return value is the
2856 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2857 * non-zero means failure, while the nano_sem_take family returns 1 for success
2858 * and 0 for failure.
2859 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002860 * @retval 0 Semaphore taken.
2861 * @retval -EBUSY Returned without waiting.
2862 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002863 */
Andrew Boie99280232017-09-29 14:17:47 -07002864__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002865
2866/**
2867 * @brief Give a semaphore.
2868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002869 * This routine gives @a sem, unless the semaphore is already at its maximum
2870 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002872 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002873 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002874 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002875 *
2876 * @return N/A
2877 */
Andrew Boie99280232017-09-29 14:17:47 -07002878__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002879
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002880/**
2881 * @brief Reset a semaphore's count to zero.
2882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002883 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002885 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002886 *
2887 * @return N/A
2888 */
Andrew Boie990bf162017-10-03 12:36:49 -07002889__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002890
Anas Nashif954d5502018-02-25 08:37:28 -06002891/**
2892 * @internal
2893 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002894static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002895{
2896 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002897}
2898
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002899/**
2900 * @brief Get a semaphore's count.
2901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002902 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002903 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002904 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002905 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002906 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002907 */
Andrew Boie990bf162017-10-03 12:36:49 -07002908__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002909
Anas Nashif954d5502018-02-25 08:37:28 -06002910/**
2911 * @internal
2912 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002913static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002914{
2915 return sem->count;
2916}
2917
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002918/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002919 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002921 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002922 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002923 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002924 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002925 * @param name Name of the semaphore.
2926 * @param initial_count Initial semaphore count.
2927 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002928 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002929#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002930 struct k_sem name \
2931 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07002932 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05302933 BUILD_ASSERT(((count_limit) != 0) && \
2934 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002935
Anas Nashif166f5192018-02-25 08:02:36 -06002936/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002937
2938/**
2939 * @defgroup alert_apis Alert APIs
2940 * @ingroup kernel_apis
2941 * @{
2942 */
2943
Allan Stephens5eceb852016-11-16 10:16:30 -05002944/**
2945 * @typedef k_alert_handler_t
2946 * @brief Alert handler function type.
2947 *
2948 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002949 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002950 * and is only invoked if the alert has been initialized with one.
2951 *
2952 * @param alert Address of the alert.
2953 *
2954 * @return 0 if alert has been consumed; non-zero if alert should pend.
2955 */
2956typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002957
Anas Nashif166f5192018-02-25 08:02:36 -06002958/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002959
2960/**
2961 * @cond INTERNAL_HIDDEN
2962 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002963
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002964#define K_ALERT_DEFAULT NULL
2965#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002966
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002967struct k_alert {
2968 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002969 atomic_t send_count;
2970 struct k_work work_item;
2971 struct k_sem sem;
2972
Anas Nashif2f203c22016-12-18 06:57:45 -05002973 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002974};
2975
Anas Nashif954d5502018-02-25 08:37:28 -06002976/**
2977 * @internal
2978 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002979extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002980
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002981#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002982 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002983 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002984 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002985 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2986 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002987 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002988 }
2989
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002990#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2991
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002992/**
Allan Stephensc98da842016-11-11 15:45:03 -05002993 * INTERNAL_HIDDEN @endcond
2994 */
2995
2996/**
2997 * @addtogroup alert_apis
2998 * @{
2999 */
3000
3001/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003002 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003003 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003004 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003005 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003006 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003008 * @param name Name of the alert.
3009 * @param alert_handler Action to take when alert is sent. Specify either
3010 * the address of a function to be invoked by the system workqueue
3011 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
3012 * K_ALERT_DEFAULT (which causes the alert to pend).
3013 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003014 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003015#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003016 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003017 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003018 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003019 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003020
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003021/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003022 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003023 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003024 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003025 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003026 * @param alert Address of the alert.
3027 * @param handler Action to take when alert is sent. Specify either the address
3028 * of a function to be invoked by the system workqueue thread,
3029 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3030 * K_ALERT_DEFAULT (which causes the alert to pend).
3031 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003032 *
3033 * @return N/A
3034 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003035extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3036 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003037
3038/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003039 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003040 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003041 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003042 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003043 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3044 *
3045 * @param alert Address of the alert.
3046 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003047 * or one of the special values K_NO_WAIT and K_FOREVER.
3048 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003049 * @retval 0 Alert received.
3050 * @retval -EBUSY Returned without waiting.
3051 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003052 */
Andrew Boie310e9872017-09-29 04:41:15 -07003053__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003054
3055/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003056 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003058 * This routine signals @a alert. The action specified for @a alert will
3059 * be taken, which may trigger the execution of an alert handler function
3060 * and/or cause the alert to pend (assuming the alert has not reached its
3061 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003062 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003063 * @note Can be called by ISRs.
3064 *
3065 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003066 *
3067 * @return N/A
3068 */
Andrew Boie310e9872017-09-29 04:41:15 -07003069__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003070
3071/**
Anas Nashif166f5192018-02-25 08:02:36 -06003072 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003073 */
3074
Allan Stephensc98da842016-11-11 15:45:03 -05003075/**
3076 * @cond INTERNAL_HIDDEN
3077 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003078
3079struct k_msgq {
3080 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003081 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003082 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003083 char *buffer_start;
3084 char *buffer_end;
3085 char *read_ptr;
3086 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003087 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003088
Anas Nashif2f203c22016-12-18 06:57:45 -05003089 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003090 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003091};
3092
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003093#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003094 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003095 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003096 .max_msgs = q_max_msgs, \
3097 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003098 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003099 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003100 .read_ptr = q_buffer, \
3101 .write_ptr = q_buffer, \
3102 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003103 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003104 }
3105
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003106#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
3107
Andrew Boie0fe789f2018-04-12 18:35:56 -07003108#define K_MSGQ_FLAG_ALLOC BIT(0)
3109
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303110struct k_msgq_attrs {
3111 size_t msg_size;
3112 u32_t max_msgs;
3113 u32_t used_msgs;
3114};
3115
Peter Mitsis1da807e2016-10-06 11:36:59 -04003116/**
Allan Stephensc98da842016-11-11 15:45:03 -05003117 * INTERNAL_HIDDEN @endcond
3118 */
3119
3120/**
3121 * @defgroup msgq_apis Message Queue APIs
3122 * @ingroup kernel_apis
3123 * @{
3124 */
3125
3126/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003127 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003128 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003129 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3130 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003131 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3132 * message is similarly aligned to this boundary, @a q_msg_size must also be
3133 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003135 * The message queue can be accessed outside the module where it is defined
3136 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003137 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003138 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003139 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003140 * @param q_name Name of the message queue.
3141 * @param q_msg_size Message size (in bytes).
3142 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003143 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003144 */
3145#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003146 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003147 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003148 struct k_msgq q_name \
3149 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003150 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003151 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003152
Peter Mitsisd7a37502016-10-13 11:37:40 -04003153/**
3154 * @brief Initialize a message queue.
3155 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003156 * This routine initializes a message queue object, prior to its first use.
3157 *
Allan Stephensda827222016-11-09 14:23:58 -06003158 * The message queue's ring buffer must contain space for @a max_msgs messages,
3159 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3160 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3161 * that each message is similarly aligned to this boundary, @a q_msg_size
3162 * must also be a multiple of N.
3163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003164 * @param q Address of the message queue.
3165 * @param buffer Pointer to ring buffer that holds queued messages.
3166 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003167 * @param max_msgs Maximum number of messages that can be queued.
3168 *
3169 * @return N/A
3170 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003171void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3172 u32_t max_msgs);
3173
3174/**
3175 * @brief Initialize a message queue.
3176 *
3177 * This routine initializes a message queue object, prior to its first use,
3178 * allocating its internal ring buffer from the calling thread's resource
3179 * pool.
3180 *
3181 * Memory allocated for the ring buffer can be released by calling
3182 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3183 * all of its references.
3184 *
3185 * @param q Address of the message queue.
3186 * @param msg_size Message size (in bytes).
3187 * @param max_msgs Maximum number of messages that can be queued.
3188 *
3189 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3190 * thread's resource pool, or -EINVAL if the size parameters cause
3191 * an integer overflow.
3192 */
3193__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3194 u32_t max_msgs);
3195
3196
3197void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003198
3199/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003200 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003201 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003202 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003203 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003204 * @note Can be called by ISRs.
3205 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003206 * @param q Address of the message queue.
3207 * @param data Pointer to the message.
3208 * @param timeout Waiting period to add the message (in milliseconds),
3209 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003210 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003211 * @retval 0 Message sent.
3212 * @retval -ENOMSG Returned without waiting or queue purged.
3213 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003214 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003215__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003216
3217/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003218 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003219 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003220 * This routine receives a message from message queue @a q in a "first in,
3221 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003222 *
Allan Stephensc98da842016-11-11 15:45:03 -05003223 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003224 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003225 * @param q Address of the message queue.
3226 * @param data Address of area to hold the received message.
3227 * @param timeout Waiting period to receive the message (in milliseconds),
3228 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003229 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003230 * @retval 0 Message received.
3231 * @retval -ENOMSG Returned without waiting.
3232 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003233 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003234__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003235
3236/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003237 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003239 * This routine discards all unreceived messages in a message queue's ring
3240 * buffer. Any threads that are blocked waiting to send a message to the
3241 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003242 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003243 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003244 *
3245 * @return N/A
3246 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003247__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003248
Peter Mitsis67be2492016-10-07 11:44:34 -04003249/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003250 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003251 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003252 * This routine returns the number of unused entries in a message queue's
3253 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003255 * @param q Address of the message queue.
3256 *
3257 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04003258 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003259__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3260
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303261/**
3262 * @brief Get basic attributes of a message queue.
3263 *
3264 * This routine fetches basic attributes of message queue into attr argument.
3265 *
3266 * @param q Address of the message queue.
3267 * @param attrs pointer to message queue attribute structure.
3268 *
3269 * @return N/A
3270 */
3271__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3272
3273
Andrew Boie82edb6e2017-10-02 10:53:06 -07003274static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003275{
3276 return q->max_msgs - q->used_msgs;
3277}
3278
Peter Mitsisd7a37502016-10-13 11:37:40 -04003279/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003280 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003281 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003282 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003283 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003284 * @param q Address of the message queue.
3285 *
3286 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003287 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003288__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3289
3290static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003291{
3292 return q->used_msgs;
3293}
3294
Anas Nashif166f5192018-02-25 08:02:36 -06003295/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003296
3297/**
3298 * @defgroup mem_pool_apis Memory Pool APIs
3299 * @ingroup kernel_apis
3300 * @{
3301 */
3302
Andy Ross73cb9582017-05-09 10:42:39 -07003303/* Note on sizing: the use of a 20 bit field for block means that,
3304 * assuming a reasonable minimum block size of 16 bytes, we're limited
3305 * to 16M of memory managed by a single pool. Long term it would be
3306 * good to move to a variable bit size based on configuration.
3307 */
3308struct k_mem_block_id {
3309 u32_t pool : 8;
3310 u32_t level : 4;
3311 u32_t block : 20;
3312};
3313
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003314struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003315 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003316 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003317};
3318
Anas Nashif166f5192018-02-25 08:02:36 -06003319/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003320
3321/**
3322 * @defgroup mailbox_apis Mailbox APIs
3323 * @ingroup kernel_apis
3324 * @{
3325 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003326
3327struct k_mbox_msg {
3328 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003329 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003330 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003331 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003332 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003333 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003334 /** sender's message data buffer */
3335 void *tx_data;
3336 /** internal use only - needed for legacy API support */
3337 void *_rx_data;
3338 /** message data block descriptor */
3339 struct k_mem_block tx_block;
3340 /** source thread id */
3341 k_tid_t rx_source_thread;
3342 /** target thread id */
3343 k_tid_t tx_target_thread;
3344 /** internal use only - thread waiting on send (may be a dummy) */
3345 k_tid_t _syncing_thread;
3346#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3347 /** internal use only - semaphore used during asynchronous send */
3348 struct k_sem *_async_sem;
3349#endif
3350};
3351
Allan Stephensc98da842016-11-11 15:45:03 -05003352/**
3353 * @cond INTERNAL_HIDDEN
3354 */
3355
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003356struct k_mbox {
3357 _wait_q_t tx_msg_queue;
3358 _wait_q_t rx_msg_queue;
3359
Anas Nashif2f203c22016-12-18 06:57:45 -05003360 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003361};
3362
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003363#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003364 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003365 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3366 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003367 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003368 }
3369
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003370#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3371
Peter Mitsis12092702016-10-14 12:57:23 -04003372/**
Allan Stephensc98da842016-11-11 15:45:03 -05003373 * INTERNAL_HIDDEN @endcond
3374 */
3375
3376/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003377 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003379 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003380 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003381 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003383 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003384 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003385#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003386 struct k_mbox name \
3387 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003388 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003389
Peter Mitsis12092702016-10-14 12:57:23 -04003390/**
3391 * @brief Initialize a mailbox.
3392 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003393 * This routine initializes a mailbox object, prior to its first use.
3394 *
3395 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003396 *
3397 * @return N/A
3398 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003399extern void k_mbox_init(struct k_mbox *mbox);
3400
Peter Mitsis12092702016-10-14 12:57:23 -04003401/**
3402 * @brief Send a mailbox message in a synchronous manner.
3403 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003404 * This routine sends a message to @a mbox and waits for a receiver to both
3405 * receive and process it. The message data may be in a buffer, in a memory
3406 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003407 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003408 * @param mbox Address of the mailbox.
3409 * @param tx_msg Address of the transmit message descriptor.
3410 * @param timeout Waiting period for the message to be received (in
3411 * milliseconds), or one of the special values K_NO_WAIT
3412 * and K_FOREVER. Once the message has been received,
3413 * this routine waits as long as necessary for the message
3414 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003415 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003416 * @retval 0 Message sent.
3417 * @retval -ENOMSG Returned without waiting.
3418 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003419 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003420extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003421 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003422
Peter Mitsis12092702016-10-14 12:57:23 -04003423/**
3424 * @brief Send a mailbox message in an asynchronous manner.
3425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003426 * This routine sends a message to @a mbox without waiting for a receiver
3427 * to process it. The message data may be in a buffer, in a memory pool block,
3428 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3429 * will be given when the message has been both received and completely
3430 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003431 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003432 * @param mbox Address of the mailbox.
3433 * @param tx_msg Address of the transmit message descriptor.
3434 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003435 *
3436 * @return N/A
3437 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003438extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003439 struct k_sem *sem);
3440
Peter Mitsis12092702016-10-14 12:57:23 -04003441/**
3442 * @brief Receive a mailbox message.
3443 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003444 * This routine receives a message from @a mbox, then optionally retrieves
3445 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003446 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003447 * @param mbox Address of the mailbox.
3448 * @param rx_msg Address of the receive message descriptor.
3449 * @param buffer Address of the buffer to receive data, or NULL to defer data
3450 * retrieval and message disposal until later.
3451 * @param timeout Waiting period for a message to be received (in
3452 * milliseconds), or one of the special values K_NO_WAIT
3453 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003454 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003455 * @retval 0 Message received.
3456 * @retval -ENOMSG Returned without waiting.
3457 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003458 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003459extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003460 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003461
3462/**
3463 * @brief Retrieve mailbox message data into a buffer.
3464 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003465 * This routine completes the processing of a received message by retrieving
3466 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003467 *
3468 * Alternatively, this routine can be used to dispose of a received message
3469 * without retrieving its data.
3470 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003471 * @param rx_msg Address of the receive message descriptor.
3472 * @param buffer Address of the buffer to receive data, or NULL to discard
3473 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003474 *
3475 * @return N/A
3476 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003477extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003478
3479/**
3480 * @brief Retrieve mailbox message data into a memory pool block.
3481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003482 * This routine completes the processing of a received message by retrieving
3483 * its data into a memory pool block, then disposing of the message.
3484 * The memory pool block that results from successful retrieval must be
3485 * returned to the pool once the data has been processed, even in cases
3486 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003487 *
3488 * Alternatively, this routine can be used to dispose of a received message
3489 * without retrieving its data. In this case there is no need to return a
3490 * memory pool block to the pool.
3491 *
3492 * This routine allocates a new memory pool block for the data only if the
3493 * data is not already in one. If a new block cannot be allocated, the routine
3494 * returns a failure code and the received message is left unchanged. This
3495 * permits the caller to reattempt data retrieval at a later time or to dispose
3496 * of the received message without retrieving its data.
3497 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003498 * @param rx_msg Address of a receive message descriptor.
3499 * @param pool Address of memory pool, or NULL to discard data.
3500 * @param block Address of the area to hold memory pool block info.
3501 * @param timeout Waiting period to wait for a memory pool block (in
3502 * milliseconds), or one of the special values K_NO_WAIT
3503 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003504 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003505 * @retval 0 Data retrieved.
3506 * @retval -ENOMEM Returned without waiting.
3507 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003508 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003509extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003510 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003511 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003512
Anas Nashif166f5192018-02-25 08:02:36 -06003513/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003514
3515/**
3516 * @cond INTERNAL_HIDDEN
3517 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003518
Andrew Boie44fe8122018-04-12 17:38:12 -07003519#define K_PIPE_FLAG_ALLOC BIT(0) /* Buffer was allocated */
3520
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003521struct k_pipe {
3522 unsigned char *buffer; /* Pipe buffer: may be NULL */
3523 size_t size; /* Buffer size */
3524 size_t bytes_used; /* # bytes used in buffer */
3525 size_t read_index; /* Where in buffer to read from */
3526 size_t write_index; /* Where in buffer to write */
3527
3528 struct {
3529 _wait_q_t readers; /* Reader wait queue */
3530 _wait_q_t writers; /* Writer wait queue */
3531 } wait_q;
3532
Anas Nashif2f203c22016-12-18 06:57:45 -05003533 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Andrew Boie44fe8122018-04-12 17:38:12 -07003534 u8_t flags; /* Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003535};
3536
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003537#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003538 { \
3539 .buffer = pipe_buffer, \
3540 .size = pipe_buffer_size, \
3541 .bytes_used = 0, \
3542 .read_index = 0, \
3543 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003544 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3545 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003546 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003547 }
3548
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003549#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3550
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003551/**
Allan Stephensc98da842016-11-11 15:45:03 -05003552 * INTERNAL_HIDDEN @endcond
3553 */
3554
3555/**
3556 * @defgroup pipe_apis Pipe APIs
3557 * @ingroup kernel_apis
3558 * @{
3559 */
3560
3561/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003562 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003563 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003564 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003565 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003566 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003568 * @param name Name of the pipe.
3569 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3570 * or zero if no ring buffer is used.
3571 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003572 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003573#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3574 static unsigned char __kernel_noinit __aligned(pipe_align) \
3575 _k_pipe_buf_##name[pipe_buffer_size]; \
3576 struct k_pipe name \
3577 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003578 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003579
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003580/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003581 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003583 * This routine initializes a pipe object, prior to its first use.
3584 *
3585 * @param pipe Address of the pipe.
3586 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3587 * is used.
3588 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3589 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003590 *
3591 * @return N/A
3592 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003593void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3594
3595/**
3596 * @brief Release a pipe's allocated buffer
3597 *
3598 * If a pipe object was given a dynamically allocated buffer via
3599 * k_pipe_alloc_init(), this will free it. This function does nothing
3600 * if the buffer wasn't dynamically allocated.
3601 *
3602 * @param pipe Address of the pipe.
3603 */
3604void k_pipe_cleanup(struct k_pipe *pipe);
3605
3606/**
3607 * @brief Initialize a pipe and allocate a buffer for it
3608 *
3609 * Storage for the buffer region will be allocated from the calling thread's
3610 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3611 * or userspace is enabled and the pipe object loses all references to it.
3612 *
3613 * This function should only be called on uninitialized pipe objects.
3614 *
3615 * @param pipe Address of the pipe.
3616 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3617 * buffer is used.
3618 * @retval 0 on success
3619 * @retval -ENOMEM if memory couln't be allocated
3620 */
3621__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003622
3623/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003624 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003626 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003627 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003628 * @param pipe Address of the pipe.
3629 * @param data Address of data to write.
3630 * @param bytes_to_write Size of data (in bytes).
3631 * @param bytes_written Address of area to hold the number of bytes written.
3632 * @param min_xfer Minimum number of bytes to write.
3633 * @param timeout Waiting period to wait for the data to be written (in
3634 * milliseconds), or one of the special values K_NO_WAIT
3635 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003636 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003637 * @retval 0 At least @a min_xfer bytes of data were written.
3638 * @retval -EIO Returned without waiting; zero data bytes were written.
3639 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003640 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003641 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003642__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3643 size_t bytes_to_write, size_t *bytes_written,
3644 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003645
3646/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003647 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003648 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003649 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003651 * @param pipe Address of the pipe.
3652 * @param data Address to place the data read from pipe.
3653 * @param bytes_to_read Maximum number of data bytes to read.
3654 * @param bytes_read Address of area to hold the number of bytes read.
3655 * @param min_xfer Minimum number of data bytes to read.
3656 * @param timeout Waiting period to wait for the data to be read (in
3657 * milliseconds), or one of the special values K_NO_WAIT
3658 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003659 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003660 * @retval 0 At least @a min_xfer bytes of data were read.
3661 * @retval -EIO Returned without waiting; zero data bytes were read.
3662 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003663 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003664 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003665__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3666 size_t bytes_to_read, size_t *bytes_read,
3667 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003668
3669/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003670 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003672 * This routine writes the data contained in a memory block to @a pipe.
3673 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003674 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003675 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003676 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003677 * @param block Memory block containing data to send
3678 * @param size Number of data bytes in memory block to send
3679 * @param sem Semaphore to signal upon completion (else NULL)
3680 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003681 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003682 */
3683extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3684 size_t size, struct k_sem *sem);
3685
Anas Nashif166f5192018-02-25 08:02:36 -06003686/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003687
Allan Stephensc98da842016-11-11 15:45:03 -05003688/**
3689 * @cond INTERNAL_HIDDEN
3690 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003691
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003692struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003693 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003694 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003695 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003696 char *buffer;
3697 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003698 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003699
Anas Nashif2f203c22016-12-18 06:57:45 -05003700 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003701};
3702
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003703#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003704 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003705 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003706 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003707 .num_blocks = slab_num_blocks, \
3708 .block_size = slab_block_size, \
3709 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003710 .free_list = NULL, \
3711 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003712 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003713 }
3714
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003715#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3716
3717
Peter Mitsis578f9112016-10-07 13:50:31 -04003718/**
Allan Stephensc98da842016-11-11 15:45:03 -05003719 * INTERNAL_HIDDEN @endcond
3720 */
3721
3722/**
3723 * @defgroup mem_slab_apis Memory Slab APIs
3724 * @ingroup kernel_apis
3725 * @{
3726 */
3727
3728/**
Allan Stephensda827222016-11-09 14:23:58 -06003729 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003730 *
Allan Stephensda827222016-11-09 14:23:58 -06003731 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003732 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003733 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3734 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003735 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003736 *
Allan Stephensda827222016-11-09 14:23:58 -06003737 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003738 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003739 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003740 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003742 * @param name Name of the memory slab.
3743 * @param slab_block_size Size of each memory block (in bytes).
3744 * @param slab_num_blocks Number memory blocks.
3745 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003746 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003747#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3748 char __noinit __aligned(slab_align) \
3749 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3750 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003751 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003752 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003753 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003754
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003755/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003756 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003757 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003758 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003759 *
Allan Stephensda827222016-11-09 14:23:58 -06003760 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3761 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3762 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3763 * To ensure that each memory block is similarly aligned to this boundary,
3764 * @a slab_block_size must also be a multiple of N.
3765 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003766 * @param slab Address of the memory slab.
3767 * @param buffer Pointer to buffer used for the memory blocks.
3768 * @param block_size Size of each memory block (in bytes).
3769 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003770 *
3771 * @return N/A
3772 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003773extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003774 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003775
3776/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003777 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003778 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003779 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003781 * @param slab Address of the memory slab.
3782 * @param mem Pointer to block address area.
3783 * @param timeout Maximum time to wait for operation to complete
3784 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3785 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003786 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003787 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003788 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003789 * @retval -ENOMEM Returned without waiting.
3790 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003791 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003792extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003793 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003794
3795/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003796 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003798 * This routine releases a previously allocated memory block back to its
3799 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003801 * @param slab Address of the memory slab.
3802 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003803 *
3804 * @return N/A
3805 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003806extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003807
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003808/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003809 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003810 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003811 * This routine gets the number of memory blocks that are currently
3812 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003813 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003814 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003816 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003817 */
Kumar Galacc334c72017-04-21 10:55:34 -05003818static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003819{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003820 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003821}
3822
Peter Mitsisc001aa82016-10-13 13:53:37 -04003823/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003824 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003825 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003826 * This routine gets the number of memory blocks that are currently
3827 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003828 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003829 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003831 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003832 */
Kumar Galacc334c72017-04-21 10:55:34 -05003833static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003834{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003835 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003836}
3837
Anas Nashif166f5192018-02-25 08:02:36 -06003838/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003839
3840/**
3841 * @cond INTERNAL_HIDDEN
3842 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003843
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003844struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003845 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003846 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003847};
3848
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003849/**
Allan Stephensc98da842016-11-11 15:45:03 -05003850 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003851 */
3852
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003853/**
Allan Stephensc98da842016-11-11 15:45:03 -05003854 * @addtogroup mem_pool_apis
3855 * @{
3856 */
3857
3858/**
3859 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003861 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3862 * long. The memory pool allows blocks to be repeatedly partitioned into
3863 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003864 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003865 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003866 * If the pool is to be accessed outside the module where it is defined, it
3867 * can be declared via
3868 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003869 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003871 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003872 * @param minsz Size of the smallest blocks in the pool (in bytes).
3873 * @param maxsz Size of the largest blocks in the pool (in bytes).
3874 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003875 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003876 */
Andy Ross73cb9582017-05-09 10:42:39 -07003877#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3878 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3879 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003880 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003881 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003882 .base = { \
3883 .buf = _mpool_buf_##name, \
3884 .max_sz = maxsz, \
3885 .n_max = nmax, \
3886 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3887 .levels = _mpool_lvls_##name, \
3888 .flags = SYS_MEM_POOL_KERNEL \
3889 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003890 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003891
Peter Mitsis937042c2016-10-13 13:18:26 -04003892/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003893 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003894 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003895 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003897 * @param pool Address of the memory pool.
3898 * @param block Pointer to block descriptor for the allocated memory.
3899 * @param size Amount of memory to allocate (in bytes).
3900 * @param timeout Maximum time to wait for operation to complete
3901 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3902 * or K_FOREVER to wait as long as necessary.
3903 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003904 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003905 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003906 * @retval -ENOMEM Returned without waiting.
3907 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003908 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003909extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003910 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003911
3912/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07003913 * @brief Allocate memory from a memory pool with malloc() semantics
3914 *
3915 * Such memory must be released using k_free().
3916 *
3917 * @param pool Address of the memory pool.
3918 * @param size Amount of memory to allocate (in bytes).
3919 * @return Address of the allocated memory if successful, otherwise NULL
3920 */
3921extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
3922
3923/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003924 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003925 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003926 * This routine releases a previously allocated memory block back to its
3927 * memory pool.
3928 *
3929 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003930 *
3931 * @return N/A
3932 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003933extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003934
3935/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003936 * @brief Free memory allocated from a memory pool.
3937 *
3938 * This routine releases a previously allocated memory block back to its
3939 * memory pool
3940 *
3941 * @param id Memory block identifier.
3942 *
3943 * @return N/A
3944 */
3945extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3946
3947/**
Anas Nashif166f5192018-02-25 08:02:36 -06003948 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003949 */
3950
3951/**
3952 * @defgroup heap_apis Heap Memory Pool APIs
3953 * @ingroup kernel_apis
3954 * @{
3955 */
3956
3957/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003958 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003960 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003961 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003963 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003964 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003965 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003966 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003967extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003968
3969/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003970 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003971 *
3972 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07003973 * returned must have been allocated from the heap memory pool or
3974 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04003975 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003976 * If @a ptr is NULL, no operation is performed.
3977 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003978 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003979 *
3980 * @return N/A
3981 */
3982extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003983
Allan Stephensc98da842016-11-11 15:45:03 -05003984/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003985 * @brief Allocate memory from heap, array style
3986 *
3987 * This routine provides traditional calloc() semantics. Memory is
3988 * allocated from the heap memory pool and zeroed.
3989 *
3990 * @param nmemb Number of elements in the requested array
3991 * @param size Size of each array element (in bytes).
3992 *
3993 * @return Address of the allocated memory if successful; otherwise NULL.
3994 */
3995extern void *k_calloc(size_t nmemb, size_t size);
3996
Anas Nashif166f5192018-02-25 08:02:36 -06003997/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003998
Benjamin Walshacc68c12017-01-29 18:57:45 -05003999/* polling API - PRIVATE */
4000
Benjamin Walshb0179862017-02-02 16:39:57 -05004001#ifdef CONFIG_POLL
4002#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
4003#else
4004#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
4005#endif
4006
Benjamin Walshacc68c12017-01-29 18:57:45 -05004007/* private - implementation data created as needed, per-type */
4008struct _poller {
4009 struct k_thread *thread;
4010};
4011
4012/* private - types bit positions */
4013enum _poll_types_bits {
4014 /* can be used to ignore an event */
4015 _POLL_TYPE_IGNORE,
4016
4017 /* to be signaled by k_poll_signal() */
4018 _POLL_TYPE_SIGNAL,
4019
4020 /* semaphore availability */
4021 _POLL_TYPE_SEM_AVAILABLE,
4022
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004023 /* queue/fifo/lifo data availability */
4024 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004025
4026 _POLL_NUM_TYPES
4027};
4028
4029#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4030
4031/* private - states bit positions */
4032enum _poll_states_bits {
4033 /* default state when creating event */
4034 _POLL_STATE_NOT_READY,
4035
Benjamin Walshacc68c12017-01-29 18:57:45 -05004036 /* signaled by k_poll_signal() */
4037 _POLL_STATE_SIGNALED,
4038
4039 /* semaphore is available */
4040 _POLL_STATE_SEM_AVAILABLE,
4041
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004042 /* data is available to read on queue/fifo/lifo */
4043 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004044
4045 _POLL_NUM_STATES
4046};
4047
4048#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4049
4050#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004051 (32 - (0 \
4052 + 8 /* tag */ \
4053 + _POLL_NUM_TYPES \
4054 + _POLL_NUM_STATES \
4055 + 1 /* modes */ \
4056 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004057
Benjamin Walshacc68c12017-01-29 18:57:45 -05004058/* end of polling API - PRIVATE */
4059
4060
4061/**
4062 * @defgroup poll_apis Async polling APIs
4063 * @ingroup kernel_apis
4064 * @{
4065 */
4066
4067/* Public polling API */
4068
4069/* public - values for k_poll_event.type bitfield */
4070#define K_POLL_TYPE_IGNORE 0
4071#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4072#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004073#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4074#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004075
4076/* public - polling modes */
4077enum k_poll_modes {
4078 /* polling thread does not take ownership of objects when available */
4079 K_POLL_MODE_NOTIFY_ONLY = 0,
4080
4081 K_POLL_NUM_MODES
4082};
4083
4084/* public - values for k_poll_event.state bitfield */
4085#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004086#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4087#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004088#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4089#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004090
4091/* public - poll signal object */
4092struct k_poll_signal {
4093 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004094 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004095
4096 /*
4097 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4098 * user resets it to 0.
4099 */
4100 unsigned int signaled;
4101
4102 /* custom result value passed to k_poll_signal() if needed */
4103 int result;
4104};
4105
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004106#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004107 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004108 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004109 .signaled = 0, \
4110 .result = 0, \
4111 }
4112
4113struct k_poll_event {
4114 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004115 sys_dnode_t _node;
4116
4117 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004118 struct _poller *poller;
4119
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004120 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004121 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004122
Benjamin Walshacc68c12017-01-29 18:57:45 -05004123 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004124 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004125
4126 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004127 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004128
4129 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004130 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004131
4132 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004133 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004134
4135 /* per-type data */
4136 union {
4137 void *obj;
4138 struct k_poll_signal *signal;
4139 struct k_sem *sem;
4140 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004141 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004142 };
4143};
4144
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004145#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004146 { \
4147 .poller = NULL, \
4148 .type = event_type, \
4149 .state = K_POLL_STATE_NOT_READY, \
4150 .mode = event_mode, \
4151 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004152 { .obj = event_obj }, \
4153 }
4154
4155#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4156 event_tag) \
4157 { \
4158 .type = event_type, \
4159 .tag = event_tag, \
4160 .state = K_POLL_STATE_NOT_READY, \
4161 .mode = event_mode, \
4162 .unused = 0, \
4163 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004164 }
4165
4166/**
4167 * @brief Initialize one struct k_poll_event instance
4168 *
4169 * After this routine is called on a poll event, the event it ready to be
4170 * placed in an event array to be passed to k_poll().
4171 *
4172 * @param event The event to initialize.
4173 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4174 * values. Only values that apply to the same object being polled
4175 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4176 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004177 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004178 * @param obj Kernel object or poll signal.
4179 *
4180 * @return N/A
4181 */
4182
Kumar Galacc334c72017-04-21 10:55:34 -05004183extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004184 int mode, void *obj);
4185
4186/**
4187 * @brief Wait for one or many of multiple poll events to occur
4188 *
4189 * This routine allows a thread to wait concurrently for one or many of
4190 * multiple poll events to have occurred. Such events can be a kernel object
4191 * being available, like a semaphore, or a poll signal event.
4192 *
4193 * When an event notifies that a kernel object is available, the kernel object
4194 * is not "given" to the thread calling k_poll(): it merely signals the fact
4195 * that the object was available when the k_poll() call was in effect. Also,
4196 * all threads trying to acquire an object the regular way, i.e. by pending on
4197 * the object, have precedence over the thread polling on the object. This
4198 * means that the polling thread will never get the poll event on an object
4199 * until the object becomes available and its pend queue is empty. For this
4200 * reason, the k_poll() call is more effective when the objects being polled
4201 * only have one thread, the polling thread, trying to acquire them.
4202 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004203 * When k_poll() returns 0, the caller should loop on all the events that were
4204 * passed to k_poll() and check the state field for the values that were
4205 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004206 *
4207 * Before being reused for another call to k_poll(), the user has to reset the
4208 * state field to K_POLL_STATE_NOT_READY.
4209 *
Andrew Boie3772f772018-05-07 16:52:57 -07004210 * When called from user mode, a temporary memory allocation is required from
4211 * the caller's resource pool.
4212 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004213 * @param events An array of pointers to events to be polled for.
4214 * @param num_events The number of events in the array.
4215 * @param timeout Waiting period for an event to be ready (in milliseconds),
4216 * or one of the special values K_NO_WAIT and K_FOREVER.
4217 *
4218 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004219 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02004220 * @retval -EINTR Poller thread has been interrupted.
Andrew Boie3772f772018-05-07 16:52:57 -07004221 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4222 * @retval -EINVAL Bad parameters (user mode only)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004223 */
4224
Andrew Boie3772f772018-05-07 16:52:57 -07004225__syscall int k_poll(struct k_poll_event *events, int num_events,
4226 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004227
4228/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004229 * @brief Initialize a poll signal object.
4230 *
4231 * Ready a poll signal object to be signaled via k_poll_signal().
4232 *
4233 * @param signal A poll signal.
4234 *
4235 * @return N/A
4236 */
4237
Andrew Boie3772f772018-05-07 16:52:57 -07004238__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4239
4240/*
4241 * @brief Reset a poll signal object's state to unsignaled.
4242 *
4243 * @param signal A poll signal object
4244 */
4245__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4246
4247static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4248{
4249 signal->signaled = 0;
4250}
4251
4252/**
4253 * @brief Fetch the signaled state and resylt value of a poll signal
4254 *
4255 * @param signal A poll signal object
4256 * @param signaled An integer buffer which will be written nonzero if the
4257 * object was signaled
4258 * @param result An integer destination buffer which will be written with the
4259 * result value if the object was signaed, or an undefined
4260 * value if it was not.
4261 */
4262__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4263 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004264
4265/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004266 * @brief Signal a poll signal object.
4267 *
4268 * This routine makes ready a poll signal, which is basically a poll event of
4269 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4270 * made ready to run. A @a result value can be specified.
4271 *
4272 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004273 * k_poll_signal(), stays set until the user sets it back to 0 with
4274 * k_poll_signal_reset(). It thus has to be reset by the user before being
4275 * passed again to k_poll() or k_poll() will consider it being signaled, and
4276 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004277 *
4278 * @param signal A poll signal.
4279 * @param result The value to store in the result field of the signal.
4280 *
4281 * @retval 0 The signal was delivered successfully.
4282 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
4283 */
4284
Andrew Boie3772f772018-05-07 16:52:57 -07004285__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004286
Anas Nashif954d5502018-02-25 08:37:28 -06004287/**
4288 * @internal
4289 */
Andy Ross8606fab2018-03-26 10:54:40 -07004290extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004291
Anas Nashif166f5192018-02-25 08:02:36 -06004292/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004293
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004294/**
4295 * @brief Make the CPU idle.
4296 *
4297 * This function makes the CPU idle until an event wakes it up.
4298 *
4299 * In a regular system, the idle thread should be the only thread responsible
4300 * for making the CPU idle and triggering any type of power management.
4301 * However, in some more constrained systems, such as a single-threaded system,
4302 * the only thread would be responsible for this if needed.
4303 *
4304 * @return N/A
4305 */
4306extern void k_cpu_idle(void);
4307
4308/**
4309 * @brief Make the CPU idle in an atomic fashion.
4310 *
4311 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4312 * must be done atomically before making the CPU idle.
4313 *
4314 * @param key Interrupt locking key obtained from irq_lock().
4315 *
4316 * @return N/A
4317 */
4318extern void k_cpu_atomic_idle(unsigned int key);
4319
Anas Nashif954d5502018-02-25 08:37:28 -06004320
4321/**
4322 * @internal
4323 */
Kumar Galacc334c72017-04-21 10:55:34 -05004324extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004325
Andrew Boiecdb94d62017-04-18 15:22:05 -07004326#ifdef _ARCH_EXCEPT
4327/* This archtecture has direct support for triggering a CPU exception */
4328#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4329#else
4330
Andrew Boiecdb94d62017-04-18 15:22:05 -07004331/* NOTE: This is the implementation for arches that do not implement
4332 * _ARCH_EXCEPT() to generate a real CPU exception.
4333 *
4334 * We won't have a real exception frame to determine the PC value when
4335 * the oops occurred, so print file and line number before we jump into
4336 * the fatal error handler.
4337 */
4338#define _k_except_reason(reason) do { \
4339 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4340 _NanoFatalErrorHandler(reason, &_default_esf); \
4341 CODE_UNREACHABLE; \
4342 } while (0)
4343
4344#endif /* _ARCH__EXCEPT */
4345
4346/**
4347 * @brief Fatally terminate a thread
4348 *
4349 * This should be called when a thread has encountered an unrecoverable
4350 * runtime condition and needs to terminate. What this ultimately
4351 * means is determined by the _fatal_error_handler() implementation, which
4352 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4353 *
4354 * If this is called from ISR context, the default system fatal error handler
4355 * will treat it as an unrecoverable system error, just like k_panic().
4356 */
4357#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4358
4359/**
4360 * @brief Fatally terminate the system
4361 *
4362 * This should be called when the Zephyr kernel has encountered an
4363 * unrecoverable runtime condition and needs to terminate. What this ultimately
4364 * means is determined by the _fatal_error_handler() implementation, which
4365 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4366 */
4367#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4368
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004369/*
4370 * private APIs that are utilized by one or more public APIs
4371 */
4372
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004373#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004374/**
4375 * @internal
4376 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004377extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004378#else
Anas Nashif954d5502018-02-25 08:37:28 -06004379/**
4380 * @internal
4381 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004382#define _init_static_threads() do { } while ((0))
4383#endif
4384
Anas Nashif954d5502018-02-25 08:37:28 -06004385/**
4386 * @internal
4387 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004388extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004389/**
4390 * @internal
4391 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004392extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004393
Andrew Boiedc5d9352017-06-02 12:56:47 -07004394/* arch/cpu.h may declare an architecture or platform-specific macro
4395 * for properly declaring stacks, compatible with MMU/MPU constraints if
4396 * enabled
4397 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004398
4399/**
4400 * @brief Obtain an extern reference to a stack
4401 *
4402 * This macro properly brings the symbol of a thread stack declared
4403 * elsewhere into scope.
4404 *
4405 * @param sym Thread stack symbol name
4406 */
4407#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4408
Andrew Boiedc5d9352017-06-02 12:56:47 -07004409#ifdef _ARCH_THREAD_STACK_DEFINE
4410#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4411#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4412 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4413#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4414#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004415static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004416{
4417 return _ARCH_THREAD_STACK_BUFFER(sym);
4418}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004419#else
4420/**
4421 * @brief Declare a toplevel thread stack memory region
4422 *
4423 * This declares a region of memory suitable for use as a thread's stack.
4424 *
4425 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4426 * 'noinit' section so that it isn't zeroed at boot
4427 *
Andrew Boie507852a2017-07-25 18:47:07 -07004428 * The declared symbol will always be a k_thread_stack_t which can be passed to
4429 * k_thread_create, but should otherwise not be manipulated. If the buffer
4430 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004431 *
4432 * It is legal to precede this definition with the 'static' keyword.
4433 *
4434 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4435 * parameter of k_thread_create(), it may not be the same as the
4436 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4437 *
4438 * @param sym Thread stack symbol name
4439 * @param size Size of the stack memory region
4440 */
4441#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004442 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004443
4444/**
4445 * @brief Declare a toplevel array of thread stack memory regions
4446 *
4447 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4448 * definition for additional details and constraints.
4449 *
4450 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4451 * 'noinit' section so that it isn't zeroed at boot
4452 *
4453 * @param sym Thread stack symbol name
4454 * @param nmemb Number of stacks to declare
4455 * @param size Size of the stack memory region
4456 */
4457
4458#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004459 struct _k_thread_stack_element __noinit \
4460 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004461
4462/**
4463 * @brief Declare an embedded stack memory region
4464 *
4465 * Used for stacks embedded within other data structures. Use is highly
4466 * discouraged but in some cases necessary. For memory protection scenarios,
4467 * it is very important that any RAM preceding this member not be writable
4468 * by threads else a stack overflow will lead to silent corruption. In other
4469 * words, the containing data structure should live in RAM owned by the kernel.
4470 *
4471 * @param sym Thread stack symbol name
4472 * @param size Size of the stack memory region
4473 */
4474#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004475 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004476
4477/**
4478 * @brief Return the size in bytes of a stack memory region
4479 *
4480 * Convenience macro for passing the desired stack size to k_thread_create()
4481 * since the underlying implementation may actually create something larger
4482 * (for instance a guard area).
4483 *
4484 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004485 * passed to K_THREAD_STACK_DEFINE.
4486 *
4487 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4488 * it is not guaranteed to return the original value since each array
4489 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004490 *
4491 * @param sym Stack memory symbol
4492 * @return Size of the stack
4493 */
4494#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4495
4496/**
4497 * @brief Get a pointer to the physical stack buffer
4498 *
4499 * Convenience macro to get at the real underlying stack buffer used by
4500 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4501 * This is really only intended for diagnostic tools which want to examine
4502 * stack memory contents.
4503 *
4504 * @param sym Declared stack symbol name
4505 * @return The buffer itself, a char *
4506 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004507static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004508{
4509 return (char *)sym;
4510}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004511
4512#endif /* _ARCH_DECLARE_STACK */
4513
Chunlin Hane9c97022017-07-07 20:29:30 +08004514/**
4515 * @defgroup mem_domain_apis Memory domain APIs
4516 * @ingroup kernel_apis
4517 * @{
4518 */
4519
4520/** @def MEM_PARTITION_ENTRY
4521 * @brief Used to declare a memory partition entry
4522 */
4523#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4524 {\
4525 .start = _start, \
4526 .size = _size, \
4527 .attr = _attr, \
4528 }
4529
4530/** @def K_MEM_PARTITION_DEFINE
4531 * @brief Used to declare a memory partition
4532 */
4533#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4534#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4535 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304536 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004537 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4538#else
4539#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304540 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004541 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4542#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4543
Chunlin Hane9c97022017-07-07 20:29:30 +08004544/* memory partition */
4545struct k_mem_partition {
4546 /* start address of memory partition */
4547 u32_t start;
4548 /* size of memory partition */
4549 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304550#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004551 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304552 k_mem_partition_attr_t attr;
4553#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004554};
4555
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304556/* memory domian
4557 * Note: Always declare this structure with __kernel prefix
4558 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004559struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304560#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004561 /* partitions in the domain */
4562 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304563#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004564 /* domain q */
4565 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004566 /* number of partitions in the domain */
4567 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004568};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304569
Chunlin Hane9c97022017-07-07 20:29:30 +08004570
4571/**
4572 * @brief Initialize a memory domain.
4573 *
4574 * Initialize a memory domain with given name and memory partitions.
4575 *
4576 * @param domain The memory domain to be initialized.
4577 * @param num_parts The number of array items of "parts" parameter.
4578 * @param parts An array of pointers to the memory partitions. Can be NULL
4579 * if num_parts is zero.
4580 */
4581
Leandro Pereira08de6582018-02-28 14:22:57 -08004582extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004583 struct k_mem_partition *parts[]);
4584/**
4585 * @brief Destroy a memory domain.
4586 *
4587 * Destroy a memory domain.
4588 *
4589 * @param domain The memory domain to be destroyed.
4590 */
4591
4592extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4593
4594/**
4595 * @brief Add a memory partition into a memory domain.
4596 *
4597 * Add a memory partition into a memory domain.
4598 *
4599 * @param domain The memory domain to be added a memory partition.
4600 * @param part The memory partition to be added
4601 */
4602
4603extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4604 struct k_mem_partition *part);
4605
4606/**
4607 * @brief Remove a memory partition from a memory domain.
4608 *
4609 * Remove a memory partition from a memory domain.
4610 *
4611 * @param domain The memory domain to be removed a memory partition.
4612 * @param part The memory partition to be removed
4613 */
4614
4615extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4616 struct k_mem_partition *part);
4617
4618/**
4619 * @brief Add a thread into a memory domain.
4620 *
4621 * Add a thread into a memory domain.
4622 *
4623 * @param domain The memory domain that the thread is going to be added into.
4624 * @param thread ID of thread going to be added into the memory domain.
4625 */
4626
4627extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4628 k_tid_t thread);
4629
4630/**
4631 * @brief Remove a thread from its memory domain.
4632 *
4633 * Remove a thread from its memory domain.
4634 *
4635 * @param thread ID of thread going to be removed from its memory domain.
4636 */
4637
4638extern void k_mem_domain_remove_thread(k_tid_t thread);
4639
Anas Nashif166f5192018-02-25 08:02:36 -06004640/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004641
Andrew Boie756f9072017-10-10 16:01:49 -07004642/**
4643 * @brief Emit a character buffer to the console device
4644 *
4645 * @param c String of characters to print
4646 * @param n The length of the string
4647 */
4648__syscall void k_str_out(char *c, size_t n);
4649
Andy Rosse7172672018-01-24 15:48:32 -08004650/**
4651 * @brief Start a numbered CPU on a MP-capable system
4652
4653 * This starts and initializes a specific CPU. The main thread on
4654 * startup is running on CPU zero, other processors are numbered
4655 * sequentially. On return from this function, the CPU is known to
4656 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004657 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004658 * with the provided key will work to enable them.
4659 *
4660 * Normally, in SMP mode this function will be called by the kernel
4661 * initialization and should not be used as a user API. But it is
4662 * defined here for special-purpose apps which want Zephyr running on
4663 * one core and to use others for design-specific processing.
4664 *
4665 * @param cpu_num Integer number of the CPU
4666 * @param stack Stack memory for the CPU
4667 * @param sz Stack buffer size, in bytes
4668 * @param fn Function to begin running on the CPU. First argument is
4669 * an irq_unlock() key.
4670 * @param arg Untyped argument to be passed to "fn"
4671 */
4672extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4673 void (*fn)(int, void *), void *arg);
4674
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004675#ifdef __cplusplus
4676}
4677#endif
4678
Andrew Boiee004dec2016-11-07 09:01:19 -08004679#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4680/*
4681 * Define new and delete operators.
4682 * At this moment, the operators do nothing since objects are supposed
4683 * to be statically allocated.
4684 */
4685inline void operator delete(void *ptr)
4686{
4687 (void)ptr;
4688}
4689
4690inline void operator delete[](void *ptr)
4691{
4692 (void)ptr;
4693}
4694
4695inline void *operator new(size_t size)
4696{
4697 (void)size;
4698 return NULL;
4699}
4700
4701inline void *operator new[](size_t size)
4702{
4703 (void)size;
4704 return NULL;
4705}
4706
4707/* Placement versions of operator new and delete */
4708inline void operator delete(void *ptr1, void *ptr2)
4709{
4710 (void)ptr1;
4711 (void)ptr2;
4712}
4713
4714inline void operator delete[](void *ptr1, void *ptr2)
4715{
4716 (void)ptr1;
4717 (void)ptr2;
4718}
4719
4720inline void *operator new(size_t size, void *ptr)
4721{
4722 (void)size;
4723 return ptr;
4724}
4725
4726inline void *operator new[](size_t size, void *ptr)
4727{
4728 (void)size;
4729 return ptr;
4730}
4731
4732#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4733
Andrew Boiefa94ee72017-09-28 16:54:35 -07004734#include <syscalls/kernel.h>
4735
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004736#endif /* !_ASMLANGUAGE */
4737
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004738#endif /* _kernel__h_ */