blob: 13cac80e584133f8c870953f414dbbd866ed3878 [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 Boiedf555242018-05-25 07:28:54 -0700181} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700182
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 Ross4a2e50f2018-05-15 11:06:25 -0700469#ifdef CONFIG_SCHED_DEADLINE
470 int prio_deadline;
471#endif
472
Andy Ross1acd8c22018-05-03 14:51:49 -0700473 u32_t order_key;
474
Andy Ross2724fd12018-01-29 14:55:20 -0800475#ifdef CONFIG_SMP
476 /* True for the per-CPU idle threads */
477 u8_t is_idle;
478
Andy Ross2724fd12018-01-29 14:55:20 -0800479 /* CPU index on which thread was last run */
480 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700481
482 /* Recursive count of irq_lock() calls */
483 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800484#endif
485
Andrew Boie73abd322017-04-04 13:19:13 -0700486 /* data returned by APIs */
487 void *swap_data;
488
489#ifdef CONFIG_SYS_CLOCK_EXISTS
490 /* this thread's entry in a timeout queue */
491 struct _timeout timeout;
492#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700493};
494
495typedef struct _thread_base _thread_base_t;
496
497#if defined(CONFIG_THREAD_STACK_INFO)
498/* Contains the stack information of a thread */
499struct _thread_stack_info {
500 /* Stack Start */
501 u32_t start;
502 /* Stack Size */
503 u32_t size;
504};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700505
506typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700507#endif /* CONFIG_THREAD_STACK_INFO */
508
Chunlin Hane9c97022017-07-07 20:29:30 +0800509#if defined(CONFIG_USERSPACE)
510struct _mem_domain_info {
511 /* memory domain queue node */
512 sys_dnode_t mem_domain_q_node;
513 /* memory domain of the thread */
514 struct k_mem_domain *mem_domain;
515};
516
517#endif /* CONFIG_USERSPACE */
518
Anas Nashifce78d162018-05-24 12:43:11 -0500519/**
520 * @ingroup thread_apis
521 * Thread Structure
522 */
Andrew Boie73abd322017-04-04 13:19:13 -0700523struct k_thread {
524
525 struct _thread_base base;
526
Anas Nashifce78d162018-05-24 12:43:11 -0500527 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700528 struct _caller_saved caller_saved;
Anas Nashifce78d162018-05-24 12:43:11 -0500529 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700530 struct _callee_saved callee_saved;
531
Anas Nashifce78d162018-05-24 12:43:11 -0500532 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700533 void *init_data;
534
Anas Nashifce78d162018-05-24 12:43:11 -0500535 /**
536 * abort function
537 * @req K-THREAD-002
538 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700539 void (*fn_abort)(void);
540
541#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500542 /** thread entry and parameters description */
Andrew Boie73abd322017-04-04 13:19:13 -0700543 struct __thread_entry *entry;
544
Anas Nashifce78d162018-05-24 12:43:11 -0500545 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700546 struct k_thread *next_thread;
547#endif
548
549#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500550 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700551 void *custom_data;
552#endif
553
554#ifdef CONFIG_ERRNO
Anas Nashifce78d162018-05-24 12:43:11 -0500555 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700556 int errno_var;
557#endif
558
559#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500560 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700561 struct _thread_stack_info stack_info;
562#endif /* CONFIG_THREAD_STACK_INFO */
563
Chunlin Hane9c97022017-07-07 20:29:30 +0800564#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500565 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800566 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500567 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700568 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800569#endif /* CONFIG_USERSPACE */
570
Andy Ross042d8ec2017-12-09 08:37:20 -0800571#if defined(CONFIG_USE_SWITCH)
572 /* When using __switch() a few previously arch-specific items
573 * become part of the core OS
574 */
575
Anas Nashifce78d162018-05-24 12:43:11 -0500576 /** _Swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800577 int swap_retval;
578
Anas Nashifce78d162018-05-24 12:43:11 -0500579 /** Context handle returned via _arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800580 void *switch_handle;
581#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500582 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700583 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800584
Anas Nashifce78d162018-05-24 12:43:11 -0500585 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700586 struct _thread_arch arch;
587};
588
589typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400590typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700591#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400592
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400593enum execution_context_types {
594 K_ISR = 0,
595 K_COOP_THREAD,
596 K_PREEMPT_THREAD,
597};
598
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400599/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100600 * @defgroup profiling_apis Profiling APIs
601 * @ingroup kernel_apis
602 * @{
603 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530604typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
605 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100606
607/**
608 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
609 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700610 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100611 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
612 *
613 * CONFIG_MAIN_STACK_SIZE
614 * CONFIG_IDLE_STACK_SIZE
615 * CONFIG_ISR_STACK_SIZE
616 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
617 *
618 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
619 * produce output.
620 *
621 * @return N/A
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530622 *
623 * @deprecated This API is deprecated. Use k_thread_foreach().
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100624 */
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530625__deprecated extern void k_call_stacks_analyze(void);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100626
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530627/**
628 * @brief Iterate over all the threads in the system.
629 *
630 * This routine iterates over all the threads in the system and
631 * calls the user_cb function for each thread.
632 *
633 * @param user_cb Pointer to the user callback function.
634 * @param user_data Pointer to user data.
635 *
636 * @note CONFIG_THREAD_MONITOR must be set for this function
637 * to be effective. Also this API uses irq_lock to protect the
638 * _kernel.threads list which means creation of new threads and
639 * terminations of existing threads are blocked until this
640 * API returns.
641 *
642 * @return N/A
643 */
644extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
645
Anas Nashif166f5192018-02-25 08:02:36 -0600646/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100647
648/**
Allan Stephensc98da842016-11-11 15:45:03 -0500649 * @defgroup thread_apis Thread APIs
650 * @ingroup kernel_apis
651 * @{
652 */
653
Benjamin Walshed240f22017-01-22 13:05:08 -0500654#endif /* !_ASMLANGUAGE */
655
656
657/*
658 * Thread user options. May be needed by assembly code. Common part uses low
659 * bits, arch-specific use high bits.
660 */
661
Anas Nashifa541e932018-05-24 11:19:16 -0500662/**
663 * @brief system thread that must not abort
664 * @req K-THREAD-000
665 * */
Benjamin Walshed240f22017-01-22 13:05:08 -0500666#define K_ESSENTIAL (1 << 0)
667
668#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500669/**
670 * @brief thread uses floating point registers
671 */
Benjamin Walshed240f22017-01-22 13:05:08 -0500672#define K_FP_REGS (1 << 1)
673#endif
674
Anas Nashifa541e932018-05-24 11:19:16 -0500675/**
676 * @brief user mode thread
677 *
678 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700679 * has additional restrictions
680 */
681#define K_USER (1 << 2)
682
Anas Nashifa541e932018-05-24 11:19:16 -0500683/**
684 * @brief Inherit Permissions
685 *
686 * @details
687 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700688 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
689 * is not enabled.
690 */
691#define K_INHERIT_PERMS (1 << 3)
692
Benjamin Walshed240f22017-01-22 13:05:08 -0500693#ifdef CONFIG_X86
694/* x86 Bitmask definitions for threads user options */
695
696#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
697/* thread uses SSEx (and also FP) registers */
698#define K_SSE_REGS (1 << 7)
699#endif
700#endif
701
702/* end - thread options */
703
704#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400705/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700706 * @brief Create a thread.
707 *
708 * This routine initializes a thread, then schedules it for execution.
709 *
710 * The new thread may be scheduled for immediate execution or a delayed start.
711 * If the newly spawned thread does not have a delayed start the kernel
712 * scheduler may preempt the current thread to allow the new thread to
713 * execute.
714 *
715 * Thread options are architecture-specific, and can include K_ESSENTIAL,
716 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
717 * them using "|" (the logical OR operator).
718 *
719 * Historically, users often would use the beginning of the stack memory region
720 * to store the struct k_thread data, although corruption will occur if the
721 * stack overflows this region and stack protection features may not detect this
722 * situation.
723 *
724 * @param new_thread Pointer to uninitialized struct k_thread
725 * @param stack Pointer to the stack space.
726 * @param stack_size Stack size in bytes.
727 * @param entry Thread entry function.
728 * @param p1 1st entry point parameter.
729 * @param p2 2nd entry point parameter.
730 * @param p3 3rd entry point parameter.
731 * @param prio Thread priority.
732 * @param options Thread options.
733 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
734 *
735 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400736 *
737 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700738 */
Andrew Boie662c3452017-10-02 10:51:18 -0700739__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700740 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700741 size_t stack_size,
742 k_thread_entry_t entry,
743 void *p1, void *p2, void *p3,
744 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700745
Andrew Boie3f091b52017-08-30 14:34:14 -0700746/**
747 * @brief Drop a thread's privileges permanently to user mode
748 *
749 * @param entry Function to start executing from
750 * @param p1 1st entry point parameter
751 * @param p2 2nd entry point parameter
752 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400753 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700754 */
755extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
756 void *p1, void *p2,
757 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700758
Andrew Boied26cf2d2017-03-30 13:07:02 -0700759/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700760 * @brief Grant a thread access to a NULL-terminated set of kernel objects
761 *
762 * This is a convenience function. For the provided thread, grant access to
763 * the remaining arguments, which must be pointers to kernel objects.
764 * The final argument must be a NULL.
765 *
766 * The thread object must be initialized (i.e. running). The objects don't
767 * need to be.
768 *
769 * @param thread Thread to grant access to objects
770 * @param ... NULL-terminated list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400771 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700772 */
773extern void __attribute__((sentinel))
774 k_thread_access_grant(struct k_thread *thread, ...);
775
776/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700777 * @brief Assign a resource memory pool to a thread
778 *
779 * By default, threads have no resource pool assigned unless their parent
780 * thread has a resource pool, in which case it is inherited. Multiple
781 * threads may be assigned to the same memory pool.
782 *
783 * Changing a thread's resource pool will not migrate allocations from the
784 * previous pool.
785 *
786 * @param thread Target thread to assign a memory pool for resource requests,
787 * or NULL if the thread should no longer have a memory pool.
788 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400789 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700790 */
791static inline void k_thread_resource_pool_assign(struct k_thread *thread,
792 struct k_mem_pool *pool)
793{
794 thread->resource_pool = pool;
795}
796
797#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
798/**
799 * @brief Assign the system heap as a thread's resource pool
800 *
801 * Similar to k_thread_resource_pool_assign(), but the thread will use
802 * the kernel heap to draw memory.
803 *
804 * Use with caution, as a malicious thread could perform DoS attacks on the
805 * kernel heap.
806 *
807 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400808 *
809 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700810 */
811void k_thread_system_pool_assign(struct k_thread *thread);
812#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
813
814/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500815 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400816 *
Allan Stephensc98da842016-11-11 15:45:03 -0500817 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500818 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500820 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400821 *
822 * @return N/A
823 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700824__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400825
826/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500827 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400828 *
829 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500830 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400831 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832 * @return N/A
833 */
Kumar Galacc334c72017-04-21 10:55:34 -0500834extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400835
836/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500837 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500839 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400840 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500841 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400842 *
843 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400844 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400845 */
Andrew Boie468190a2017-09-29 14:00:48 -0700846__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400847
848/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500849 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400850 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500851 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500853 * If @a thread is not currently sleeping, the routine has no effect.
854 *
855 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856 *
857 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400858 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400859 */
Andrew Boie468190a2017-09-29 14:00:48 -0700860__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400861
862/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500863 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500865 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400866 *
867 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400868 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700869__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400870
871/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500872 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400873 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500874 * This routine prevents @a thread from executing if it has not yet started
875 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500877 * @param thread ID of thread to cancel.
878 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700879 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500880 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700881 *
882 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400883 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700884__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400885
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400886/**
Allan Stephensc98da842016-11-11 15:45:03 -0500887 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400888 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500889 * This routine permanently stops execution of @a thread. The thread is taken
890 * off all kernel queues it is part of (i.e. the ready queue, the timeout
891 * queue, or a kernel object wait queue). However, any kernel resources the
892 * thread might currently own (such as mutexes or memory blocks) are not
893 * released. It is the responsibility of the caller of this routine to ensure
894 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400895 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500896 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400897 *
898 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400899 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400900 */
Andrew Boie468190a2017-09-29 14:00:48 -0700901__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400902
Andrew Boie7d627c52017-08-30 11:01:56 -0700903
904/**
905 * @brief Start an inactive thread
906 *
907 * If a thread was created with K_FOREVER in the delay parameter, it will
908 * not be added to the scheduling queue until this function is called
909 * on it.
910 *
911 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400912 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700913 */
Andrew Boie468190a2017-09-29 14:00:48 -0700914__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700915
Allan Stephensc98da842016-11-11 15:45:03 -0500916/**
917 * @cond INTERNAL_HIDDEN
918 */
919
Benjamin Walshd211a522016-12-06 11:44:01 -0500920/* timeout has timed out and is not on _timeout_q anymore */
921#define _EXPIRED (-2)
922
923/* timeout is not in use */
924#define _INACTIVE (-1)
925
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400926struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700927 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700928 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400929 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700930 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500931 void *init_p1;
932 void *init_p2;
933 void *init_p3;
934 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500935 u32_t init_options;
936 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500937 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400938};
939
Andrew Boied26cf2d2017-03-30 13:07:02 -0700940#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400941 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500942 prio, options, delay, abort, groups) \
943 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700944 .init_thread = (thread), \
945 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500946 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700947 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400948 .init_p1 = (void *)p1, \
949 .init_p2 = (void *)p2, \
950 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500951 .init_prio = (prio), \
952 .init_options = (options), \
953 .init_delay = (delay), \
954 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400955 }
956
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400957/**
Allan Stephensc98da842016-11-11 15:45:03 -0500958 * INTERNAL_HIDDEN @endcond
959 */
960
961/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500962 * @brief Statically define and initialize a thread.
963 *
964 * The thread may be scheduled for immediate execution or a delayed start.
965 *
966 * Thread options are architecture-specific, and can include K_ESSENTIAL,
967 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
968 * them using "|" (the logical OR operator).
969 *
970 * The ID of the thread can be accessed using:
971 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500972 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500973 *
974 * @param name Name of the thread.
975 * @param stack_size Stack size in bytes.
976 * @param entry Thread entry function.
977 * @param p1 1st entry point parameter.
978 * @param p2 2nd entry point parameter.
979 * @param p3 3rd entry point parameter.
980 * @param prio Thread priority.
981 * @param options Thread options.
982 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400983 *
Anas Nashif47420d02018-05-24 14:20:56 -0400984 * @req K-THREAD-010
985 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400986 * @internal It has been observed that the x86 compiler by default aligns
987 * these _static_thread_data structures to 32-byte boundaries, thereby
988 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400989 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400990 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500991#define K_THREAD_DEFINE(name, stack_size, \
992 entry, p1, p2, p3, \
993 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700994 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700995 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500996 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500997 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700998 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
999 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -05001000 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -07001001 NULL, 0); \
1002 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001003
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001004/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001005 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001007 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001008 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001009 * @param thread ID of thread whose priority is needed.
1010 *
1011 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -04001012 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001013 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001014__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001015
1016/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001017 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001018 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001019 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001020 *
1021 * Rescheduling can occur immediately depending on the priority @a thread is
1022 * set to:
1023 *
1024 * - If its priority is raised above the priority of the caller of this
1025 * function, and the caller is preemptible, @a thread will be scheduled in.
1026 *
1027 * - If the caller operates on itself, it lowers its priority below that of
1028 * other threads in the system, and the caller is preemptible, the thread of
1029 * highest priority will be scheduled in.
1030 *
1031 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1032 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1033 * highest priority.
1034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001035 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001036 * @param prio New priority.
1037 *
1038 * @warning Changing the priority of a thread currently involved in mutex
1039 * priority inheritance may result in undefined behavior.
1040 *
1041 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001042 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001043 */
Andrew Boie468190a2017-09-29 14:00:48 -07001044__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001045
Andy Ross4a2e50f2018-05-15 11:06:25 -07001046
1047#ifdef CONFIG_SCHED_DEADLINE
1048/**
1049 * @brief Set deadline expiration time for scheduler
1050 *
1051 * This sets the "deadline" expiration as a time delta from the
1052 * current time, in the same units used by k_cycle_get_32(). The
1053 * scheduler (when deadline scheduling is enabled) will choose the
1054 * next expiring thread when selecting between threads at the same
1055 * static priority. Threads at different priorities will be scheduled
1056 * according to their static priority.
1057 *
1058 * @note Deadlines that are negative (i.e. in the past) are still seen
1059 * as higher priority than others, even if the thread has "finished"
1060 * its work. If you don't want it scheduled anymore, you have to
1061 * reset the deadline into the future, block/pend the thread, or
1062 * modify its priority with k_thread_priority_set().
1063 *
1064 * @note Despite the API naming, the scheduler makes no guarantees the
1065 * the thread WILL be scheduled within that deadline, nor does it take
1066 * extra metadata (like e.g. the "runtime" and "period" parameters in
1067 * Linux sched_setattr()) that allows the kernel to validate the
1068 * scheduling for achievability. Such features could be implemented
1069 * above this call, which is simply input to the priority selection
1070 * logic.
1071 *
1072 * @param thread A thread on which to set the deadline
1073 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001074 *
1075 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001076 */
1077__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1078#endif
1079
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001080/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001081 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001082 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001083 * This routine prevents the kernel scheduler from making @a thread the
1084 * current thread. All other internal operations on @a thread are still
1085 * performed; for example, any timeout it is waiting on keeps ticking,
1086 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001087 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001088 * If @a thread is already suspended, the routine has no effect.
1089 *
1090 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001091 *
1092 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001093 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001094 */
Andrew Boie468190a2017-09-29 14:00:48 -07001095__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001096
1097/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001098 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001099 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001100 * This routine allows the kernel scheduler to make @a thread the current
1101 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001102 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001103 * If @a thread is not currently suspended, the routine has no effect.
1104 *
1105 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001106 *
1107 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001108 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001109 */
Andrew Boie468190a2017-09-29 14:00:48 -07001110__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001111
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001112/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001113 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001114 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001115 * This routine specifies how the scheduler will perform time slicing of
1116 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001117 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001118 * To enable time slicing, @a slice must be non-zero. The scheduler
1119 * ensures that no thread runs for more than the specified time limit
1120 * before other threads of that priority are given a chance to execute.
1121 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001122 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001124 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001125 * execute. Once the scheduler selects a thread for execution, there is no
1126 * minimum guaranteed time the thread will execute before threads of greater or
1127 * equal priority are scheduled.
1128 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001129 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001130 * for execution, this routine has no effect; the thread is immediately
1131 * rescheduled after the slice period expires.
1132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001133 * To disable timeslicing, set both @a slice and @a prio to zero.
1134 *
1135 * @param slice Maximum time slice length (in milliseconds).
1136 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001137 *
1138 * @return N/A
1139 */
Kumar Galacc334c72017-04-21 10:55:34 -05001140extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001141
Anas Nashif166f5192018-02-25 08:02:36 -06001142/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001143
1144/**
1145 * @addtogroup isr_apis
1146 * @{
1147 */
1148
1149/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001150 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001151 *
Allan Stephensc98da842016-11-11 15:45:03 -05001152 * This routine allows the caller to customize its actions, depending on
1153 * whether it is a thread or an ISR.
1154 *
1155 * @note Can be called by ISRs.
1156 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001157 * @return 0 if invoked by a thread.
1158 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001159 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001160extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001161
Benjamin Walsh445830d2016-11-10 15:54:27 -05001162/**
1163 * @brief Determine if code is running in a preemptible thread.
1164 *
Allan Stephensc98da842016-11-11 15:45:03 -05001165 * This routine allows the caller to customize its actions, depending on
1166 * whether it can be preempted by another thread. The routine returns a 'true'
1167 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001168 *
Allan Stephensc98da842016-11-11 15:45:03 -05001169 * - The code is running in a thread, not at ISR.
1170 * - The thread's priority is in the preemptible range.
1171 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001172 *
Allan Stephensc98da842016-11-11 15:45:03 -05001173 * @note Can be called by ISRs.
1174 *
1175 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001176 * @return Non-zero if invoked by a preemptible thread.
1177 */
Andrew Boie468190a2017-09-29 14:00:48 -07001178__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001179
Allan Stephensc98da842016-11-11 15:45:03 -05001180/**
Anas Nashif166f5192018-02-25 08:02:36 -06001181 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001182 */
1183
1184/**
1185 * @addtogroup thread_apis
1186 * @{
1187 */
1188
1189/**
1190 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001191 *
Allan Stephensc98da842016-11-11 15:45:03 -05001192 * This routine prevents the current thread from being preempted by another
1193 * thread by instructing the scheduler to treat it as a cooperative thread.
1194 * If the thread subsequently performs an operation that makes it unready,
1195 * it will be context switched out in the normal manner. When the thread
1196 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001197 *
Allan Stephensc98da842016-11-11 15:45:03 -05001198 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001199 *
Allan Stephensc98da842016-11-11 15:45:03 -05001200 * @note k_sched_lock() and k_sched_unlock() should normally be used
1201 * when the operation being performed can be safely interrupted by ISRs.
1202 * However, if the amount of processing involved is very small, better
1203 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001204 *
1205 * @return N/A
1206 */
1207extern void k_sched_lock(void);
1208
Allan Stephensc98da842016-11-11 15:45:03 -05001209/**
1210 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001211 *
Allan Stephensc98da842016-11-11 15:45:03 -05001212 * This routine reverses the effect of a previous call to k_sched_lock().
1213 * A thread must call the routine once for each time it called k_sched_lock()
1214 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001215 *
1216 * @return N/A
1217 */
1218extern void k_sched_unlock(void);
1219
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001220/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001221 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001222 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001223 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001224 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001225 * Custom data is not used by the kernel itself, and is freely available
1226 * for a thread to use as it sees fit. It can be used as a framework
1227 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001229 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001230 *
1231 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001232 *
1233 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001234 */
Andrew Boie468190a2017-09-29 14:00:48 -07001235__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001236
1237/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001238 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001239 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001240 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001242 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001243 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001244 */
Andrew Boie468190a2017-09-29 14:00:48 -07001245__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001246
1247/**
Anas Nashif166f5192018-02-25 08:02:36 -06001248 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001249 */
1250
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001251#include <sys_clock.h>
1252
Allan Stephensc2f15a42016-11-17 12:24:22 -05001253/**
1254 * @addtogroup clock_apis
1255 * @{
1256 */
1257
1258/**
1259 * @brief Generate null timeout delay.
1260 *
1261 * This macro generates a timeout delay that that instructs a kernel API
1262 * not to wait if the requested operation cannot be performed immediately.
1263 *
1264 * @return Timeout delay value.
1265 */
1266#define K_NO_WAIT 0
1267
1268/**
1269 * @brief Generate timeout delay from milliseconds.
1270 *
1271 * This macro generates a timeout delay that that instructs a kernel API
1272 * to wait up to @a ms milliseconds to perform the requested operation.
1273 *
1274 * @param ms Duration in milliseconds.
1275 *
1276 * @return Timeout delay value.
1277 */
Johan Hedberg14471692016-11-13 10:52:15 +02001278#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001279
1280/**
1281 * @brief Generate timeout delay from seconds.
1282 *
1283 * This macro generates a timeout delay that that instructs a kernel API
1284 * to wait up to @a s seconds to perform the requested operation.
1285 *
1286 * @param s Duration in seconds.
1287 *
1288 * @return Timeout delay value.
1289 */
Johan Hedberg14471692016-11-13 10:52:15 +02001290#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001291
1292/**
1293 * @brief Generate timeout delay from minutes.
1294 *
1295 * This macro generates a timeout delay that that instructs a kernel API
1296 * to wait up to @a m minutes to perform the requested operation.
1297 *
1298 * @param m Duration in minutes.
1299 *
1300 * @return Timeout delay value.
1301 */
Johan Hedberg14471692016-11-13 10:52:15 +02001302#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001303
1304/**
1305 * @brief Generate timeout delay from hours.
1306 *
1307 * This macro generates a timeout delay that that instructs a kernel API
1308 * to wait up to @a h hours to perform the requested operation.
1309 *
1310 * @param h Duration in hours.
1311 *
1312 * @return Timeout delay value.
1313 */
Johan Hedberg14471692016-11-13 10:52:15 +02001314#define K_HOURS(h) K_MINUTES((h) * 60)
1315
Allan Stephensc98da842016-11-11 15:45:03 -05001316/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001317 * @brief Generate infinite timeout delay.
1318 *
1319 * This macro generates a timeout delay that that instructs a kernel API
1320 * to wait as long as necessary to perform the requested operation.
1321 *
1322 * @return Timeout delay value.
1323 */
1324#define K_FOREVER (-1)
1325
1326/**
Anas Nashif166f5192018-02-25 08:02:36 -06001327 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001328 */
1329
1330/**
Allan Stephensc98da842016-11-11 15:45:03 -05001331 * @cond INTERNAL_HIDDEN
1332 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001333
Benjamin Walsh62092182016-12-20 14:39:08 -05001334/* kernel clocks */
1335
1336#if (sys_clock_ticks_per_sec == 1000) || \
1337 (sys_clock_ticks_per_sec == 500) || \
1338 (sys_clock_ticks_per_sec == 250) || \
1339 (sys_clock_ticks_per_sec == 125) || \
1340 (sys_clock_ticks_per_sec == 100) || \
1341 (sys_clock_ticks_per_sec == 50) || \
1342 (sys_clock_ticks_per_sec == 25) || \
1343 (sys_clock_ticks_per_sec == 20) || \
1344 (sys_clock_ticks_per_sec == 10) || \
1345 (sys_clock_ticks_per_sec == 1)
1346
1347 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1348#else
1349 /* yields horrible 64-bit math on many architectures: try to avoid */
1350 #define _NON_OPTIMIZED_TICKS_PER_SEC
1351#endif
1352
1353#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001354extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001355#else
Kumar Galacc334c72017-04-21 10:55:34 -05001356static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001357{
Kumar Galacc334c72017-04-21 10:55:34 -05001358 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001359}
1360#endif
1361
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001362/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001363#ifdef CONFIG_TICKLESS_KERNEL
1364#define _TICK_ALIGN 0
1365#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001366#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001367#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001368
Kumar Galacc334c72017-04-21 10:55:34 -05001369static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001370{
Benjamin Walsh62092182016-12-20 14:39:08 -05001371#ifdef CONFIG_SYS_CLOCK_EXISTS
1372
1373#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001374 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001375#else
Kumar Galacc334c72017-04-21 10:55:34 -05001376 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001377#endif
1378
1379#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001380 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001381 return 0;
1382#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001383}
1384
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001385struct k_timer {
1386 /*
1387 * _timeout structure must be first here if we want to use
1388 * dynamic timer allocation. timeout.node is used in the double-linked
1389 * list of free timers
1390 */
1391 struct _timeout timeout;
1392
Allan Stephens45bfa372016-10-12 12:39:42 -05001393 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001394 _wait_q_t wait_q;
1395
1396 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001397 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001398
1399 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001400 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001401
1402 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001403 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001404
Allan Stephens45bfa372016-10-12 12:39:42 -05001405 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001406 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001407
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001408 /* user-specific data, also used to support legacy features */
1409 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001410
Anas Nashif2f203c22016-12-18 06:57:45 -05001411 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001412};
1413
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001414#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001415 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001416 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001417 .timeout.wait_q = NULL, \
1418 .timeout.thread = NULL, \
1419 .timeout.func = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001420 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001421 .expiry_fn = expiry, \
1422 .stop_fn = stop, \
1423 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001424 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001425 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001426 }
1427
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001428#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1429
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001430/**
Allan Stephensc98da842016-11-11 15:45:03 -05001431 * INTERNAL_HIDDEN @endcond
1432 */
1433
1434/**
1435 * @defgroup timer_apis Timer APIs
1436 * @ingroup kernel_apis
1437 * @{
1438 */
1439
1440/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001441 * @typedef k_timer_expiry_t
1442 * @brief Timer expiry function type.
1443 *
1444 * A timer's expiry function is executed by the system clock interrupt handler
1445 * each time the timer expires. The expiry function is optional, and is only
1446 * invoked if the timer has been initialized with one.
1447 *
1448 * @param timer Address of timer.
1449 *
1450 * @return N/A
1451 */
1452typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1453
1454/**
1455 * @typedef k_timer_stop_t
1456 * @brief Timer stop function type.
1457 *
1458 * A timer's stop function is executed if the timer is stopped prematurely.
1459 * The function runs in the context of the thread that stops the timer.
1460 * The stop function is optional, and is only invoked if the timer has been
1461 * initialized with one.
1462 *
1463 * @param timer Address of timer.
1464 *
1465 * @return N/A
1466 */
1467typedef void (*k_timer_stop_t)(struct k_timer *timer);
1468
1469/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001470 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001471 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001472 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001473 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001474 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001475 *
1476 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001477 * @param expiry_fn Function to invoke each time the timer expires.
1478 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001479 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001480#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001481 struct k_timer name \
1482 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001483 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001484
Allan Stephens45bfa372016-10-12 12:39:42 -05001485/**
1486 * @brief Initialize a timer.
1487 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001488 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001489 *
1490 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001491 * @param expiry_fn Function to invoke each time the timer expires.
1492 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001493 *
1494 * @return N/A
1495 */
1496extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001497 k_timer_expiry_t expiry_fn,
1498 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001499
Allan Stephens45bfa372016-10-12 12:39:42 -05001500/**
1501 * @brief Start a timer.
1502 *
1503 * This routine starts a timer, and resets its status to zero. The timer
1504 * begins counting down using the specified duration and period values.
1505 *
1506 * Attempting to start a timer that is already running is permitted.
1507 * The timer's status is reset to zero and the timer begins counting down
1508 * using the new duration and period values.
1509 *
1510 * @param timer Address of timer.
1511 * @param duration Initial timer duration (in milliseconds).
1512 * @param period Timer period (in milliseconds).
1513 *
1514 * @return N/A
1515 */
Andrew Boiea354d492017-09-29 16:22:28 -07001516__syscall void k_timer_start(struct k_timer *timer,
1517 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001518
1519/**
1520 * @brief Stop a timer.
1521 *
1522 * This routine stops a running timer prematurely. The timer's stop function,
1523 * if one exists, is invoked by the caller.
1524 *
1525 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001526 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001527 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001528 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1529 * if @a k_timer_stop is to be called from ISRs.
1530 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001531 * @param timer Address of timer.
1532 *
1533 * @return N/A
1534 */
Andrew Boiea354d492017-09-29 16:22:28 -07001535__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001536
1537/**
1538 * @brief Read timer status.
1539 *
1540 * This routine reads the timer's status, which indicates the number of times
1541 * it has expired since its status was last read.
1542 *
1543 * Calling this routine resets the timer's status to zero.
1544 *
1545 * @param timer Address of timer.
1546 *
1547 * @return Timer status.
1548 */
Andrew Boiea354d492017-09-29 16:22:28 -07001549__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001550
1551/**
1552 * @brief Synchronize thread to timer expiration.
1553 *
1554 * This routine blocks the calling thread until the timer's status is non-zero
1555 * (indicating that it has expired at least once since it was last examined)
1556 * or the timer is stopped. If the timer status is already non-zero,
1557 * or the timer is already stopped, the caller continues without waiting.
1558 *
1559 * Calling this routine resets the timer's status to zero.
1560 *
1561 * This routine must not be used by interrupt handlers, since they are not
1562 * allowed to block.
1563 *
1564 * @param timer Address of timer.
1565 *
1566 * @return Timer status.
1567 */
Andrew Boiea354d492017-09-29 16:22:28 -07001568__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001569
1570/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001571 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001572 *
1573 * This routine computes the (approximate) time remaining before a running
1574 * timer next expires. If the timer is not running, it returns zero.
1575 *
1576 * @param timer Address of timer.
1577 *
1578 * @return Remaining time (in milliseconds).
1579 */
Andrew Boiea354d492017-09-29 16:22:28 -07001580__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1581
1582static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001583{
1584 return _timeout_remaining_get(&timer->timeout);
1585}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001586
Allan Stephensc98da842016-11-11 15:45:03 -05001587/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001588 * @brief Associate user-specific data with a timer.
1589 *
1590 * This routine records the @a user_data with the @a timer, to be retrieved
1591 * later.
1592 *
1593 * It can be used e.g. in a timer handler shared across multiple subsystems to
1594 * retrieve data specific to the subsystem this timer is associated with.
1595 *
1596 * @param timer Address of timer.
1597 * @param user_data User data to associate with the timer.
1598 *
1599 * @return N/A
1600 */
Andrew Boiea354d492017-09-29 16:22:28 -07001601__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1602
Anas Nashif954d5502018-02-25 08:37:28 -06001603/**
1604 * @internal
1605 */
Andrew Boiea354d492017-09-29 16:22:28 -07001606static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1607 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001608{
1609 timer->user_data = user_data;
1610}
1611
1612/**
1613 * @brief Retrieve the user-specific data from a timer.
1614 *
1615 * @param timer Address of timer.
1616 *
1617 * @return The user data.
1618 */
Andrew Boiea354d492017-09-29 16:22:28 -07001619__syscall void *k_timer_user_data_get(struct k_timer *timer);
1620
1621static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001622{
1623 return timer->user_data;
1624}
1625
Anas Nashif166f5192018-02-25 08:02:36 -06001626/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001627
Allan Stephensc98da842016-11-11 15:45:03 -05001628/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001629 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001630 * @{
1631 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001632
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001633/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001634 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001635 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001636 * This routine returns the elapsed time since the system booted,
1637 * in milliseconds.
1638 *
1639 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001640 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001641__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001642
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001643/**
1644 * @brief Enable clock always on in tickless kernel
1645 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001646 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001647 * there are no timer events programmed in tickless kernel
1648 * scheduling. This is necessary if the clock is used to track
1649 * passage of time.
1650 *
1651 * @retval prev_status Previous status of always on flag
1652 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301653#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001654static inline int k_enable_sys_clock_always_on(void)
1655{
1656 int prev_status = _sys_clock_always_on;
1657
1658 _sys_clock_always_on = 1;
1659 _enable_sys_clock();
1660
1661 return prev_status;
1662}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301663#else
1664#define k_enable_sys_clock_always_on() do { } while ((0))
1665#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001666
1667/**
1668 * @brief Disable clock always on in tickless kernel
1669 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001670 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001671 * there are no timer events programmed in tickless kernel
1672 * scheduling. To save power, this routine should be called
1673 * immediately when clock is not used to track time.
1674 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301675#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001676static inline void k_disable_sys_clock_always_on(void)
1677{
1678 _sys_clock_always_on = 0;
1679}
1680#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001681#define k_disable_sys_clock_always_on() do { } while ((0))
1682#endif
1683
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001684/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001685 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001686 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001687 * This routine returns the lower 32-bits of the elapsed time since the system
1688 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001690 * This routine can be more efficient than k_uptime_get(), as it reduces the
1691 * need for interrupt locking and 64-bit math. However, the 32-bit result
1692 * cannot hold a system uptime time larger than approximately 50 days, so the
1693 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001694 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001695 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001696 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001697__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001698
1699/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001700 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001702 * This routine computes the elapsed time between the current system uptime
1703 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001705 * @param reftime Pointer to a reference time, which is updated to the current
1706 * uptime upon return.
1707 *
1708 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001709 */
Kumar Galacc334c72017-04-21 10:55:34 -05001710extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001711
1712/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001713 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001714 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001715 * This routine computes the elapsed time between the current system uptime
1716 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001717 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001718 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1719 * need for interrupt locking and 64-bit math. However, the 32-bit result
1720 * cannot hold an elapsed time larger than approximately 50 days, so the
1721 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001722 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001723 * @param reftime Pointer to a reference time, which is updated to the current
1724 * uptime upon return.
1725 *
1726 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001727 */
Kumar Galacc334c72017-04-21 10:55:34 -05001728extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001729
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001730/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001731 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001732 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001733 * This routine returns the current time, as measured by the system's hardware
1734 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001735 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001736 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001737 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001738#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001739
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001740/**
Anas Nashif166f5192018-02-25 08:02:36 -06001741 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001742 */
1743
Allan Stephensc98da842016-11-11 15:45:03 -05001744/**
1745 * @cond INTERNAL_HIDDEN
1746 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001747
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001748struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001749 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001750 union {
1751 _wait_q_t wait_q;
1752
1753 _POLL_EVENT;
1754 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001755
1756 _OBJECT_TRACING_NEXT_PTR(k_queue);
1757};
1758
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001759#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001760 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001761 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001762 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001763 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001764 _OBJECT_TRACING_INIT \
1765 }
1766
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001767#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1768
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001769extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1770
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001771/**
1772 * INTERNAL_HIDDEN @endcond
1773 */
1774
1775/**
1776 * @defgroup queue_apis Queue APIs
1777 * @ingroup kernel_apis
1778 * @{
1779 */
1780
1781/**
1782 * @brief Initialize a queue.
1783 *
1784 * This routine initializes a queue object, prior to its first use.
1785 *
1786 * @param queue Address of the queue.
1787 *
1788 * @return N/A
1789 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001790__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001791
1792/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001793 * @brief Cancel waiting on a queue.
1794 *
1795 * This routine causes first thread pending on @a queue, if any, to
1796 * return from k_queue_get() call with NULL value (as if timeout expired).
1797 *
1798 * @note Can be called by ISRs.
1799 *
1800 * @param queue Address of the queue.
1801 *
1802 * @return N/A
1803 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001804__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001805
1806/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001807 * @brief Append an element to the end of a queue.
1808 *
1809 * This routine appends a data item to @a queue. A queue data item must be
1810 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1811 * reserved for the kernel's use.
1812 *
1813 * @note Can be called by ISRs.
1814 *
1815 * @param queue Address of the queue.
1816 * @param data Address of the data item.
1817 *
1818 * @return N/A
1819 */
1820extern void k_queue_append(struct k_queue *queue, void *data);
1821
1822/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001823 * @brief Append an element to a queue.
1824 *
1825 * This routine appends a data item to @a queue. There is an implicit
1826 * memory allocation from the calling thread's resource pool, which is
1827 * automatically freed when the item is removed from the queue.
1828 *
1829 * @note Can be called by ISRs.
1830 *
1831 * @param queue Address of the queue.
1832 * @param data Address of the data item.
1833 *
1834 * @retval 0 on success
1835 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1836 */
1837__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1838
1839/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001840 * @brief Prepend an element to a queue.
1841 *
1842 * This routine prepends a data item to @a queue. A queue data item must be
1843 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1844 * reserved for the kernel's use.
1845 *
1846 * @note Can be called by ISRs.
1847 *
1848 * @param queue Address of the queue.
1849 * @param data Address of the data item.
1850 *
1851 * @return N/A
1852 */
1853extern void k_queue_prepend(struct k_queue *queue, void *data);
1854
1855/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001856 * @brief Prepend an element to a queue.
1857 *
1858 * This routine prepends a data item to @a queue. There is an implicit
1859 * memory allocation from the calling thread's resource pool, which is
1860 * automatically freed when the item is removed from the queue.
1861 *
1862 * @note Can be called by ISRs.
1863 *
1864 * @param queue Address of the queue.
1865 * @param data Address of the data item.
1866 *
1867 * @retval 0 on success
1868 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1869 */
1870__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1871
1872/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001873 * @brief Inserts an element to a queue.
1874 *
1875 * This routine inserts a data item to @a queue after previous item. A queue
1876 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1877 * item are reserved for the kernel's use.
1878 *
1879 * @note Can be called by ISRs.
1880 *
1881 * @param queue Address of the queue.
1882 * @param prev Address of the previous data item.
1883 * @param data Address of the data item.
1884 *
1885 * @return N/A
1886 */
1887extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1888
1889/**
1890 * @brief Atomically append a list of elements to a queue.
1891 *
1892 * This routine adds a list of data items to @a queue in one operation.
1893 * The data items must be in a singly-linked list, with the first 32 bits
1894 * in each data item pointing to the next data item; the list must be
1895 * NULL-terminated.
1896 *
1897 * @note Can be called by ISRs.
1898 *
1899 * @param queue Address of the queue.
1900 * @param head Pointer to first node in singly-linked list.
1901 * @param tail Pointer to last node in singly-linked list.
1902 *
1903 * @return N/A
1904 */
1905extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1906
1907/**
1908 * @brief Atomically add a list of elements to a queue.
1909 *
1910 * This routine adds a list of data items to @a queue in one operation.
1911 * The data items must be in a singly-linked list implemented using a
1912 * sys_slist_t object. Upon completion, the original list is empty.
1913 *
1914 * @note Can be called by ISRs.
1915 *
1916 * @param queue Address of the queue.
1917 * @param list Pointer to sys_slist_t object.
1918 *
1919 * @return N/A
1920 */
1921extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1922
1923/**
1924 * @brief Get an element from a queue.
1925 *
1926 * This routine removes first data item from @a queue. The first 32 bits of the
1927 * data item are reserved for the kernel's use.
1928 *
1929 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1930 *
1931 * @param queue Address of the queue.
1932 * @param timeout Waiting period to obtain a data item (in milliseconds),
1933 * or one of the special values K_NO_WAIT and K_FOREVER.
1934 *
1935 * @return Address of the data item if successful; NULL if returned
1936 * without waiting, or waiting period timed out.
1937 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001938__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001939
1940/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001941 * @brief Remove an element from a queue.
1942 *
1943 * This routine removes data item from @a queue. The first 32 bits of the
1944 * data item are reserved for the kernel's use. Removing elements from k_queue
1945 * rely on sys_slist_find_and_remove which is not a constant time operation.
1946 *
1947 * @note Can be called by ISRs
1948 *
1949 * @param queue Address of the queue.
1950 * @param data Address of the data item.
1951 *
1952 * @return true if data item was removed
1953 */
1954static inline bool k_queue_remove(struct k_queue *queue, void *data)
1955{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001956 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001957}
1958
1959/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001960 * @brief Query a queue to see if it has data available.
1961 *
1962 * Note that the data might be already gone by the time this function returns
1963 * if other threads are also trying to read from the queue.
1964 *
1965 * @note Can be called by ISRs.
1966 *
1967 * @param queue Address of the queue.
1968 *
1969 * @return Non-zero if the queue is empty.
1970 * @return 0 if data is available.
1971 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001972__syscall int k_queue_is_empty(struct k_queue *queue);
1973
1974static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001975{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001976 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001977}
1978
1979/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001980 * @brief Peek element at the head of queue.
1981 *
1982 * Return element from the head of queue without removing it.
1983 *
1984 * @param queue Address of the queue.
1985 *
1986 * @return Head element, or NULL if queue is empty.
1987 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001988__syscall void *k_queue_peek_head(struct k_queue *queue);
1989
1990static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001991{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001992 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001993}
1994
1995/**
1996 * @brief Peek element at the tail of queue.
1997 *
1998 * Return element from the tail of queue without removing it.
1999 *
2000 * @param queue Address of the queue.
2001 *
2002 * @return Tail element, or NULL if queue is empty.
2003 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002004__syscall void *k_queue_peek_tail(struct k_queue *queue);
2005
2006static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002007{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002008 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002009}
2010
2011/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002012 * @brief Statically define and initialize a queue.
2013 *
2014 * The queue can be accessed outside the module where it is defined using:
2015 *
2016 * @code extern struct k_queue <name>; @endcode
2017 *
2018 * @param name Name of the queue.
2019 */
2020#define K_QUEUE_DEFINE(name) \
2021 struct k_queue name \
2022 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002023 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002024
Anas Nashif166f5192018-02-25 08:02:36 -06002025/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002026
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002027struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002028 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002029};
2030
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002031/**
2032 * @cond INTERNAL_HIDDEN
2033 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002034#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002035 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002036 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002037 }
2038
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002039#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
2040
Allan Stephensc98da842016-11-11 15:45:03 -05002041/**
2042 * INTERNAL_HIDDEN @endcond
2043 */
2044
2045/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002046 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002047 * @ingroup kernel_apis
2048 * @{
2049 */
2050
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002051/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002052 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002053 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002054 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002055 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002056 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002057 *
2058 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002059 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002060 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002061#define k_fifo_init(fifo) \
2062 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002063
2064/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002065 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002066 *
2067 * This routine causes first thread pending on @a fifo, if any, to
2068 * return from k_fifo_get() call with NULL value (as if timeout
2069 * expired).
2070 *
2071 * @note Can be called by ISRs.
2072 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002073 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002074 *
2075 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002076 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002077 */
2078#define k_fifo_cancel_wait(fifo) \
2079 k_queue_cancel_wait((struct k_queue *) fifo)
2080
2081/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002082 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002083 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002084 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002085 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2086 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002087 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002088 * @note Can be called by ISRs.
2089 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002090 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002091 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002092 *
2093 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002094 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002095 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002096#define k_fifo_put(fifo, data) \
2097 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002098
2099/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002100 * @brief Add an element to a FIFO queue.
2101 *
2102 * This routine adds a data item to @a fifo. There is an implicit
2103 * memory allocation from the calling thread's resource pool, which is
2104 * automatically freed when the item is removed.
2105 *
2106 * @note Can be called by ISRs.
2107 *
2108 * @param fifo Address of the FIFO.
2109 * @param data Address of the data item.
2110 *
2111 * @retval 0 on success
2112 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002113 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002114 */
2115#define k_fifo_alloc_put(fifo, data) \
2116 k_queue_alloc_append((struct k_queue *) fifo, data)
2117
2118/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002119 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002120 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002121 * This routine adds a list of data items to @a fifo in one operation.
2122 * The data items must be in a singly-linked list, with the first 32 bits
2123 * each data item pointing to the next data item; the list must be
2124 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002125 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002126 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002127 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002128 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002129 * @param head Pointer to first node in singly-linked list.
2130 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002131 *
2132 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002133 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002134 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002135#define k_fifo_put_list(fifo, head, tail) \
2136 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002137
2138/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002139 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002140 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002141 * This routine adds a list of data items to @a fifo in one operation.
2142 * The data items must be in a singly-linked list implemented using a
2143 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002144 * and must be re-initialized via sys_slist_init().
2145 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002146 * @note Can be called by ISRs.
2147 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002148 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002149 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002150 *
2151 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002152 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002153 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002154#define k_fifo_put_slist(fifo, list) \
2155 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002156
2157/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002158 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002159 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002160 * This routine removes a data item from @a fifo in a "first in, first out"
2161 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002162 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002163 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2164 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002165 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002166 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002167 * or one of the special values K_NO_WAIT and K_FOREVER.
2168 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002169 * @return Address of the data item if successful; NULL if returned
2170 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002171 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002172 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002173#define k_fifo_get(fifo, timeout) \
2174 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002175
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002176/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002177 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002178 *
2179 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002180 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002181 *
2182 * @note Can be called by ISRs.
2183 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002184 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002185 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002186 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002187 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002188 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002189 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002190#define k_fifo_is_empty(fifo) \
2191 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002192
2193/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002194 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002195 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002196 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302197 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002198 * on each iteration of processing, a head container will be peeked,
2199 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002200 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002201 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002202 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002203 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002204 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002205 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002206 */
2207#define k_fifo_peek_head(fifo) \
2208 k_queue_peek_head((struct k_queue *) fifo)
2209
2210/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002211 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002212 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002213 * Return element from the tail of FIFO queue (without removing it). A usecase
2214 * for this is if elements of the FIFO queue are themselves containers. Then
2215 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002216 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002217 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002218 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002219 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002220 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002221 */
2222#define k_fifo_peek_tail(fifo) \
2223 k_queue_peek_tail((struct k_queue *) fifo)
2224
2225/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002226 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002227 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002228 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002229 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002230 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002231 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002232 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002233 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002234 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002235#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002236 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002237 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002238 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002239
Anas Nashif166f5192018-02-25 08:02:36 -06002240/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002241
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002242struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002243 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002244};
2245
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002246/**
2247 * @cond INTERNAL_HIDDEN
2248 */
2249
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002250#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002251 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002252 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002253 }
2254
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002255#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2256
Allan Stephensc98da842016-11-11 15:45:03 -05002257/**
2258 * INTERNAL_HIDDEN @endcond
2259 */
2260
2261/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002262 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002263 * @ingroup kernel_apis
2264 * @{
2265 */
2266
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002267/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002268 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002269 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002270 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002271 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002272 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002273 *
2274 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002275 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002276 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002277#define k_lifo_init(lifo) \
2278 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002279
2280/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002281 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002282 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002283 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002284 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2285 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002286 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002287 * @note Can be called by ISRs.
2288 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002289 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002290 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002291 *
2292 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002293 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002294 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002295#define k_lifo_put(lifo, data) \
2296 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002297
2298/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002299 * @brief Add an element to a LIFO queue.
2300 *
2301 * This routine adds a data item to @a lifo. There is an implicit
2302 * memory allocation from the calling thread's resource pool, which is
2303 * automatically freed when the item is removed.
2304 *
2305 * @note Can be called by ISRs.
2306 *
2307 * @param lifo Address of the LIFO.
2308 * @param data Address of the data item.
2309 *
2310 * @retval 0 on success
2311 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002312 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002313 */
2314#define k_lifo_alloc_put(lifo, data) \
2315 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2316
2317/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002318 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002319 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002320 * This routine removes a data item from @a lifo in a "last in, first out"
2321 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002322 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002323 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2324 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002325 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002326 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002327 * or one of the special values K_NO_WAIT and K_FOREVER.
2328 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002329 * @return Address of the data item if successful; NULL if returned
2330 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002331 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002332 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002333#define k_lifo_get(lifo, timeout) \
2334 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002335
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002336/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002337 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002338 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002339 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002340 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002341 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002342 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002343 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002344 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002345 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002346#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002347 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002348 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002349 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002350
Anas Nashif166f5192018-02-25 08:02:36 -06002351/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002352
2353/**
2354 * @cond INTERNAL_HIDDEN
2355 */
Andrew Boief3bee952018-05-02 17:44:39 -07002356#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002357
2358struct k_stack {
2359 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002360 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002361
Anas Nashif2f203c22016-12-18 06:57:45 -05002362 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002363 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002364};
2365
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002366#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002367 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002368 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002369 .base = stack_buffer, \
2370 .next = stack_buffer, \
2371 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002372 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002373 }
2374
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002375#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2376
Allan Stephensc98da842016-11-11 15:45:03 -05002377/**
2378 * INTERNAL_HIDDEN @endcond
2379 */
2380
2381/**
2382 * @defgroup stack_apis Stack APIs
2383 * @ingroup kernel_apis
2384 * @{
2385 */
2386
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002387/**
2388 * @brief Initialize a stack.
2389 *
2390 * This routine initializes a stack object, prior to its first use.
2391 *
2392 * @param stack Address of the stack.
2393 * @param buffer Address of array used to hold stacked values.
2394 * @param num_entries Maximum number of values that can be stacked.
2395 *
2396 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002397 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002398 */
Andrew Boief3bee952018-05-02 17:44:39 -07002399void k_stack_init(struct k_stack *stack,
2400 u32_t *buffer, unsigned int num_entries);
2401
2402
2403/**
2404 * @brief Initialize a stack.
2405 *
2406 * This routine initializes a stack object, prior to its first use. Internal
2407 * buffers will be allocated from the calling thread's resource pool.
2408 * This memory will be released if k_stack_cleanup() is called, or
2409 * userspace is enabled and the stack object loses all references to it.
2410 *
2411 * @param stack Address of the stack.
2412 * @param num_entries Maximum number of values that can be stacked.
2413 *
2414 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002415 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002416 */
2417
2418__syscall int k_stack_alloc_init(struct k_stack *stack,
2419 unsigned int num_entries);
2420
2421/**
2422 * @brief Release a stack's allocated buffer
2423 *
2424 * If a stack object was given a dynamically allocated buffer via
2425 * k_stack_alloc_init(), this will free it. This function does nothing
2426 * if the buffer wasn't dynamically allocated.
2427 *
2428 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002429 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002430 */
2431void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002432
2433/**
2434 * @brief Push an element onto a stack.
2435 *
2436 * This routine adds a 32-bit value @a data to @a stack.
2437 *
2438 * @note Can be called by ISRs.
2439 *
2440 * @param stack Address of the stack.
2441 * @param data Value to push onto the stack.
2442 *
2443 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002444 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002445 */
Andrew Boiee8734462017-09-29 16:42:07 -07002446__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002447
2448/**
2449 * @brief Pop an element from a stack.
2450 *
2451 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2452 * manner and stores the value in @a data.
2453 *
2454 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2455 *
2456 * @param stack Address of the stack.
2457 * @param data Address of area to hold the value popped from the stack.
2458 * @param timeout Waiting period to obtain a value (in milliseconds),
2459 * or one of the special values K_NO_WAIT and K_FOREVER.
2460 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002461 * @retval 0 Element popped from stack.
2462 * @retval -EBUSY Returned without waiting.
2463 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002464 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002465 */
Andrew Boiee8734462017-09-29 16:42:07 -07002466__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002467
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002468/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002469 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002470 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002471 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002472 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002473 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002475 * @param name Name of the stack.
2476 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002477 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002478 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002479#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002480 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002481 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002482 struct k_stack name \
2483 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002484 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002485 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002486
Anas Nashif166f5192018-02-25 08:02:36 -06002487/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002488
Allan Stephens6bba9b02016-11-16 14:56:54 -05002489struct k_work;
2490
Allan Stephensc98da842016-11-11 15:45:03 -05002491/**
2492 * @defgroup workqueue_apis Workqueue Thread APIs
2493 * @ingroup kernel_apis
2494 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002495 */
2496
Allan Stephens6bba9b02016-11-16 14:56:54 -05002497/**
2498 * @typedef k_work_handler_t
2499 * @brief Work item handler function type.
2500 *
2501 * A work item's handler function is executed by a workqueue's thread
2502 * when the work item is processed by the workqueue.
2503 *
2504 * @param work Address of the work item.
2505 *
2506 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002507 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002508 */
2509typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002510
2511/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002512 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002513 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002514
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002515struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002516 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002517 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002518};
2519
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002520enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002521 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002522};
2523
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002524struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002525 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002526 k_work_handler_t handler;
2527 atomic_t flags[1];
2528};
2529
Allan Stephens6bba9b02016-11-16 14:56:54 -05002530struct k_delayed_work {
2531 struct k_work work;
2532 struct _timeout timeout;
2533 struct k_work_q *work_q;
2534};
2535
2536extern struct k_work_q k_sys_work_q;
2537
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002538/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002539 * INTERNAL_HIDDEN @endcond
2540 */
2541
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002542#define _K_WORK_INITIALIZER(work_handler) \
2543 { \
2544 ._reserved = NULL, \
2545 .handler = work_handler, \
2546 .flags = { 0 } \
2547 }
2548
2549#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2550
Allan Stephens6bba9b02016-11-16 14:56:54 -05002551/**
2552 * @brief Initialize a statically-defined work item.
2553 *
2554 * This macro can be used to initialize a statically-defined workqueue work
2555 * item, prior to its first use. For example,
2556 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002557 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002558 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002559 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002560 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002561 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002562 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002563#define K_WORK_DEFINE(work, work_handler) \
2564 struct k_work work \
2565 __in_section(_k_work, static, work) = \
2566 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002567
2568/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002569 * @brief Initialize a work item.
2570 *
2571 * This routine initializes a workqueue work item, prior to its first use.
2572 *
2573 * @param work Address of work item.
2574 * @param handler Function to invoke each time work item is processed.
2575 *
2576 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002577 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002578 */
2579static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2580{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002581 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002582 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002583 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002584}
2585
2586/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002587 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002588 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002589 * This routine submits work item @a work to be processed by workqueue
2590 * @a work_q. If the work item is already pending in the workqueue's queue
2591 * as a result of an earlier submission, this routine has no effect on the
2592 * work item. If the work item has already been processed, or is currently
2593 * being processed, its work is considered complete and the work item can be
2594 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002595 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002596 * @warning
2597 * A submitted work item must not be modified until it has been processed
2598 * by the workqueue.
2599 *
2600 * @note Can be called by ISRs.
2601 *
2602 * @param work_q Address of workqueue.
2603 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002604 *
2605 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002606 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002607 */
2608static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2609 struct k_work *work)
2610{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002611 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002612 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002613 }
2614}
2615
2616/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002617 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002618 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002619 * This routine indicates if work item @a work is pending in a workqueue's
2620 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002621 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002622 * @note Can be called by ISRs.
2623 *
2624 * @param work Address of work item.
2625 *
2626 * @return 1 if work item is pending, or 0 if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002627 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002628 */
2629static inline int k_work_pending(struct k_work *work)
2630{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002631 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002632}
2633
2634/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002635 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002636 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002637 * This routine starts workqueue @a work_q. The workqueue spawns its work
2638 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002639 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002640 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002641 * @param stack Pointer to work queue thread's stack space, as defined by
2642 * K_THREAD_STACK_DEFINE()
2643 * @param stack_size Size of the work queue thread's stack (in bytes), which
2644 * should either be the same constant passed to
2645 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002646 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002647 *
2648 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002649 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002650 */
Andrew Boie507852a2017-07-25 18:47:07 -07002651extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002652 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002653 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002654
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002655/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002656 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002657 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002658 * This routine initializes a workqueue delayed work item, prior to
2659 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002660 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002661 * @param work Address of delayed work item.
2662 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002663 *
2664 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002665 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002666 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002667extern void k_delayed_work_init(struct k_delayed_work *work,
2668 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002669
2670/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002671 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002672 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002673 * This routine schedules work item @a work to be processed by workqueue
2674 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002675 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002676 * Only when the countdown completes is the work item actually submitted to
2677 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002678 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002679 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002680 * counting down cancels the existing submission and restarts the
2681 * countdown using the new delay. Note that this behavior is
2682 * inherently subject to race conditions with the pre-existing
2683 * timeouts and work queue, so care must be taken to synchronize such
2684 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002685 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002686 * @warning
2687 * A delayed work item must not be modified until it has been processed
2688 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002689 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002690 * @note Can be called by ISRs.
2691 *
2692 * @param work_q Address of workqueue.
2693 * @param work Address of delayed work item.
2694 * @param delay Delay before submitting the work item (in milliseconds).
2695 *
2696 * @retval 0 Work item countdown started.
2697 * @retval -EINPROGRESS Work item is already pending.
2698 * @retval -EINVAL Work item is being processed or has completed its work.
2699 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002700 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002701 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002702extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2703 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002704 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002705
2706/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002707 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002708 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002709 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002710 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002711 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002712 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002713 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002714 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002715 * @param work Address of delayed work item.
2716 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002717 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002718 * @retval -EINPROGRESS Work item is already pending.
2719 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002720 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002721 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002722extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002723
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002724/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002725 * @brief Submit a work item to the system workqueue.
2726 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002727 * This routine submits work item @a work to be processed by the system
2728 * workqueue. If the work item is already pending in the workqueue's queue
2729 * as a result of an earlier submission, this routine has no effect on the
2730 * work item. If the work item has already been processed, or is currently
2731 * being processed, its work is considered complete and the work item can be
2732 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002733 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002734 * @warning
2735 * Work items submitted to the system workqueue should avoid using handlers
2736 * that block or yield since this may prevent the system workqueue from
2737 * processing other work items in a timely manner.
2738 *
2739 * @note Can be called by ISRs.
2740 *
2741 * @param work Address of work item.
2742 *
2743 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002744 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002745 */
2746static inline void k_work_submit(struct k_work *work)
2747{
2748 k_work_submit_to_queue(&k_sys_work_q, work);
2749}
2750
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002751/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002752 * @brief Submit a delayed work item to the system workqueue.
2753 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002754 * This routine schedules work item @a work to be processed by the system
2755 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002756 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002757 * Only when the countdown completes is the work item actually submitted to
2758 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002759 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002760 * Submitting a previously submitted delayed work item that is still
2761 * counting down cancels the existing submission and restarts the countdown
2762 * using the new delay. If the work item is currently pending on the
2763 * workqueue's queue because the countdown has completed it is too late to
2764 * resubmit the item, and resubmission fails without impacting the work item.
2765 * If the work item has already been processed, or is currently being processed,
2766 * its work is considered complete and the work item can be resubmitted.
2767 *
2768 * @warning
2769 * Work items submitted to the system workqueue should avoid using handlers
2770 * that block or yield since this may prevent the system workqueue from
2771 * processing other work items in a timely manner.
2772 *
2773 * @note Can be called by ISRs.
2774 *
2775 * @param work Address of delayed work item.
2776 * @param delay Delay before submitting the work item (in milliseconds).
2777 *
2778 * @retval 0 Work item countdown started.
2779 * @retval -EINPROGRESS Work item is already pending.
2780 * @retval -EINVAL Work item is being processed or has completed its work.
2781 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002782 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002783 */
2784static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002785 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002786{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002787 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002788}
2789
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002790/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002791 * @brief Get time remaining before a delayed work gets scheduled.
2792 *
2793 * This routine computes the (approximate) time remaining before a
2794 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002795 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002796 *
2797 * @param work Delayed work item.
2798 *
2799 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002800 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002801 */
Kumar Galacc334c72017-04-21 10:55:34 -05002802static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002803{
2804 return _timeout_remaining_get(&work->timeout);
2805}
2806
Anas Nashif166f5192018-02-25 08:02:36 -06002807/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002808/**
Anas Nashifce78d162018-05-24 12:43:11 -05002809 * @defgroup mutex_apis Mutex APIs
2810 * @ingroup kernel_apis
2811 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002812 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002813
Anas Nashifce78d162018-05-24 12:43:11 -05002814/**
2815 * Mutex Structure
2816 * @ingroup mutex_apis
2817 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002818struct k_mutex {
2819 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002820 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002821 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002822 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002823 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002824
Anas Nashif2f203c22016-12-18 06:57:45 -05002825 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002826};
2827
Anas Nashifce78d162018-05-24 12:43:11 -05002828/**
2829 * @cond INTERNAL_HIDDEN
2830 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002831#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002832 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002833 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002834 .owner = NULL, \
2835 .lock_count = 0, \
2836 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002837 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002838 }
2839
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002840#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2841
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002842/**
Allan Stephensc98da842016-11-11 15:45:03 -05002843 * INTERNAL_HIDDEN @endcond
2844 */
2845
2846/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002847 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002849 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002850 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002851 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002853 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002854 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002855 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002856#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002857 struct k_mutex name \
2858 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002859 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002860
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002861/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002862 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002864 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002866 * Upon completion, the mutex is available and does not have an owner.
2867 *
2868 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002869 *
2870 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002871 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002872 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002873__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002874
2875/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002876 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002878 * This routine locks @a mutex. If the mutex is locked by another thread,
2879 * the calling thread waits until the mutex becomes available or until
2880 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002882 * A thread is permitted to lock a mutex it has already locked. The operation
2883 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002884 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002885 * @param mutex Address of the mutex.
2886 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002887 * or one of the special values K_NO_WAIT and K_FOREVER.
2888 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002889 * @retval 0 Mutex locked.
2890 * @retval -EBUSY Returned without waiting.
2891 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002892 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002893 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002894__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002895
2896/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002897 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002898 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002899 * This routine unlocks @a mutex. The mutex must already be locked by the
2900 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002901 *
2902 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002903 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002904 * thread.
2905 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002906 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002907 *
2908 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002909 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002910 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002911__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002912
Allan Stephensc98da842016-11-11 15:45:03 -05002913/**
Anas Nashif166f5192018-02-25 08:02:36 -06002914 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002915 */
2916
2917/**
2918 * @cond INTERNAL_HIDDEN
2919 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002920
2921struct k_sem {
2922 _wait_q_t wait_q;
2923 unsigned int count;
2924 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002925 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002926
Anas Nashif2f203c22016-12-18 06:57:45 -05002927 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002928};
2929
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002930#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002931 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002932 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002933 .count = initial_count, \
2934 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002935 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002936 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002937 }
2938
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002939#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2940
Allan Stephensc98da842016-11-11 15:45:03 -05002941/**
2942 * INTERNAL_HIDDEN @endcond
2943 */
2944
2945/**
2946 * @defgroup semaphore_apis Semaphore APIs
2947 * @ingroup kernel_apis
2948 * @{
2949 */
2950
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002951/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002952 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002954 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002956 * @param sem Address of the semaphore.
2957 * @param initial_count Initial semaphore count.
2958 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002959 *
2960 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002961 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002962 */
Andrew Boie99280232017-09-29 14:17:47 -07002963__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2964 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002965
2966/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002967 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002969 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002971 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2972 *
2973 * @param sem Address of the semaphore.
2974 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002975 * or one of the special values K_NO_WAIT and K_FOREVER.
2976 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002977 * @note When porting code from the nanokernel legacy API to the new API, be
2978 * careful with the return value of this function. The return value is the
2979 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2980 * non-zero means failure, while the nano_sem_take family returns 1 for success
2981 * and 0 for failure.
2982 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002983 * @retval 0 Semaphore taken.
2984 * @retval -EBUSY Returned without waiting.
2985 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002986 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002987 */
Andrew Boie99280232017-09-29 14:17:47 -07002988__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002989
2990/**
2991 * @brief Give a semaphore.
2992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002993 * This routine gives @a sem, unless the semaphore is already at its maximum
2994 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002996 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002997 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002998 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002999 *
3000 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003001 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003002 */
Andrew Boie99280232017-09-29 14:17:47 -07003003__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003004
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003005/**
3006 * @brief Reset a semaphore's count to zero.
3007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003008 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003010 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003011 *
3012 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003013 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003014 */
Andrew Boie990bf162017-10-03 12:36:49 -07003015__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003016
Anas Nashif954d5502018-02-25 08:37:28 -06003017/**
3018 * @internal
3019 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003020static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003021{
3022 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003023}
3024
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003025/**
3026 * @brief Get a semaphore's count.
3027 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003028 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003029 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003030 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003032 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003033 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003034 */
Andrew Boie990bf162017-10-03 12:36:49 -07003035__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003036
Anas Nashif954d5502018-02-25 08:37:28 -06003037/**
3038 * @internal
3039 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003040static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003041{
3042 return sem->count;
3043}
3044
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003045/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003046 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003048 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003049 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003050 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003051 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003052 * @param name Name of the semaphore.
3053 * @param initial_count Initial semaphore count.
3054 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003055 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003056 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003057#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003058 struct k_sem name \
3059 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003060 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303061 BUILD_ASSERT(((count_limit) != 0) && \
3062 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003063
Anas Nashif166f5192018-02-25 08:02:36 -06003064/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003065
3066/**
3067 * @defgroup alert_apis Alert APIs
3068 * @ingroup kernel_apis
3069 * @{
3070 */
3071
Allan Stephens5eceb852016-11-16 10:16:30 -05003072/**
3073 * @typedef k_alert_handler_t
3074 * @brief Alert handler function type.
3075 *
3076 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07003077 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05003078 * and is only invoked if the alert has been initialized with one.
3079 *
3080 * @param alert Address of the alert.
3081 *
3082 * @return 0 if alert has been consumed; non-zero if alert should pend.
3083 */
3084typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05003085
Anas Nashif166f5192018-02-25 08:02:36 -06003086/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003087
3088/**
3089 * @cond INTERNAL_HIDDEN
3090 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003091
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003092#define K_ALERT_DEFAULT NULL
3093#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003094
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003095struct k_alert {
3096 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003097 atomic_t send_count;
3098 struct k_work work_item;
3099 struct k_sem sem;
3100
Anas Nashif2f203c22016-12-18 06:57:45 -05003101 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003102};
3103
Anas Nashif954d5502018-02-25 08:37:28 -06003104/**
3105 * @internal
3106 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003107extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003108
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003109#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003110 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003111 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003112 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003113 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
3114 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003115 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003116 }
3117
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003118#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
3119
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003120/**
Allan Stephensc98da842016-11-11 15:45:03 -05003121 * INTERNAL_HIDDEN @endcond
3122 */
3123
3124/**
3125 * @addtogroup alert_apis
3126 * @{
3127 */
3128
3129/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003130 * @def K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts)
3131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003132 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003133 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003134 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003135 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003136 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003137 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003138 * @param name Name of the alert.
3139 * @param alert_handler Action to take when alert is sent. Specify either
3140 * the address of a function to be invoked by the system workqueue
3141 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
3142 * K_ALERT_DEFAULT (which causes the alert to pend).
3143 * @param max_num_pending_alerts Maximum number of pending alerts.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003144 *
3145 * @req K-ALERT-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003146 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003147#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003148 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003149 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003150 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003151 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003152
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003153/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003154 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003155 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003156 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003157 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003158 * @param alert Address of the alert.
3159 * @param handler Action to take when alert is sent. Specify either the address
3160 * of a function to be invoked by the system workqueue thread,
3161 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3162 * K_ALERT_DEFAULT (which causes the alert to pend).
3163 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003164 *
3165 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003166 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003167 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003168extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3169 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003170
3171/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003172 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003174 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003175 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003176 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3177 *
3178 * @param alert Address of the alert.
3179 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003180 * or one of the special values K_NO_WAIT and K_FOREVER.
3181 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003182 * @retval 0 Alert received.
3183 * @retval -EBUSY Returned without waiting.
3184 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003185 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003186 */
Andrew Boie310e9872017-09-29 04:41:15 -07003187__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003188
3189/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003190 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003191 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003192 * This routine signals @a alert. The action specified for @a alert will
3193 * be taken, which may trigger the execution of an alert handler function
3194 * and/or cause the alert to pend (assuming the alert has not reached its
3195 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003197 * @note Can be called by ISRs.
3198 *
3199 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003200 *
3201 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003202 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003203 */
Andrew Boie310e9872017-09-29 04:41:15 -07003204__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003205
3206/**
Anas Nashif166f5192018-02-25 08:02:36 -06003207 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003208 */
3209
Allan Stephensc98da842016-11-11 15:45:03 -05003210/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003211 * @defgroup msgq_apis Message Queue APIs
3212 * @ingroup kernel_apis
3213 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003214 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003215
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003216/**
3217 * @brief Message Queue Structure
3218 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003219struct k_msgq {
3220 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003221 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003222 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003223 char *buffer_start;
3224 char *buffer_end;
3225 char *read_ptr;
3226 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003227 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003228
Anas Nashif2f203c22016-12-18 06:57:45 -05003229 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003230 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003231};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003232/**
3233 * @cond INTERNAL_HIDDEN
3234 */
3235
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003236
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003237#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003238 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003239 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003240 .max_msgs = q_max_msgs, \
3241 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003242 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003243 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003244 .read_ptr = q_buffer, \
3245 .write_ptr = q_buffer, \
3246 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003247 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003248 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003249#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003250/**
3251 * INTERNAL_HIDDEN @endcond
3252 */
3253
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003254
Andrew Boie0fe789f2018-04-12 18:35:56 -07003255#define K_MSGQ_FLAG_ALLOC BIT(0)
3256
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003257/**
3258 * @brief Message Queue Attributes
3259 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303260struct k_msgq_attrs {
3261 size_t msg_size;
3262 u32_t max_msgs;
3263 u32_t used_msgs;
3264};
3265
Allan Stephensc98da842016-11-11 15:45:03 -05003266
3267/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003268 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003269 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003270 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3271 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003272 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3273 * message is similarly aligned to this boundary, @a q_msg_size must also be
3274 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003275 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003276 * The message queue can be accessed outside the module where it is defined
3277 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003278 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003279 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003280 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003281 * @param q_name Name of the message queue.
3282 * @param q_msg_size Message size (in bytes).
3283 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003284 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003285 *
3286 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003287 */
3288#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003289 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003290 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003291 struct k_msgq q_name \
3292 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003293 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003294 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003295
Peter Mitsisd7a37502016-10-13 11:37:40 -04003296/**
3297 * @brief Initialize a message queue.
3298 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003299 * This routine initializes a message queue object, prior to its first use.
3300 *
Allan Stephensda827222016-11-09 14:23:58 -06003301 * The message queue's ring buffer must contain space for @a max_msgs messages,
3302 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3303 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3304 * that each message is similarly aligned to this boundary, @a q_msg_size
3305 * must also be a multiple of N.
3306 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003307 * @param q Address of the message queue.
3308 * @param buffer Pointer to ring buffer that holds queued messages.
3309 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003310 * @param max_msgs Maximum number of messages that can be queued.
3311 *
3312 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003313 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003314 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003315void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3316 u32_t max_msgs);
3317
3318/**
3319 * @brief Initialize a message queue.
3320 *
3321 * This routine initializes a message queue object, prior to its first use,
3322 * allocating its internal ring buffer from the calling thread's resource
3323 * pool.
3324 *
3325 * Memory allocated for the ring buffer can be released by calling
3326 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3327 * all of its references.
3328 *
3329 * @param q Address of the message queue.
3330 * @param msg_size Message size (in bytes).
3331 * @param max_msgs Maximum number of messages that can be queued.
3332 *
3333 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3334 * thread's resource pool, or -EINVAL if the size parameters cause
3335 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003336 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003337 */
3338__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3339 u32_t max_msgs);
3340
3341
3342void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003343
3344/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003345 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003346 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003347 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003348 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003349 * @note Can be called by ISRs.
3350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003351 * @param q Address of the message queue.
3352 * @param data Pointer to the message.
3353 * @param timeout Waiting period to add the message (in milliseconds),
3354 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003355 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003356 * @retval 0 Message sent.
3357 * @retval -ENOMSG Returned without waiting or queue purged.
3358 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003359 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003360 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003361__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003362
3363/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003364 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003366 * This routine receives a message from message queue @a q in a "first in,
3367 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003368 *
Allan Stephensc98da842016-11-11 15:45:03 -05003369 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003370 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003371 * @param q Address of the message queue.
3372 * @param data Address of area to hold the received message.
3373 * @param timeout Waiting period to receive the message (in milliseconds),
3374 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003375 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003376 * @retval 0 Message received.
3377 * @retval -ENOMSG Returned without waiting.
3378 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003379 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003380 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003381__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003382
3383/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003384 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003385 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003386 * This routine discards all unreceived messages in a message queue's ring
3387 * buffer. Any threads that are blocked waiting to send a message to the
3388 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003390 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003391 *
3392 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003393 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003394 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003395__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003396
Peter Mitsis67be2492016-10-07 11:44:34 -04003397/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003398 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003399 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003400 * This routine returns the number of unused entries in a message queue's
3401 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003403 * @param q Address of the message queue.
3404 *
3405 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003406 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003407 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003408__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3409
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303410/**
3411 * @brief Get basic attributes of a message queue.
3412 *
3413 * This routine fetches basic attributes of message queue into attr argument.
3414 *
3415 * @param q Address of the message queue.
3416 * @param attrs pointer to message queue attribute structure.
3417 *
3418 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003419 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303420 */
3421__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3422
3423
Andrew Boie82edb6e2017-10-02 10:53:06 -07003424static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003425{
3426 return q->max_msgs - q->used_msgs;
3427}
3428
Peter Mitsisd7a37502016-10-13 11:37:40 -04003429/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003430 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003431 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003432 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003433 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003434 * @param q Address of the message queue.
3435 *
3436 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003437 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003438 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003439__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3440
3441static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003442{
3443 return q->used_msgs;
3444}
3445
Anas Nashif166f5192018-02-25 08:02:36 -06003446/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003447
3448/**
3449 * @defgroup mem_pool_apis Memory Pool APIs
3450 * @ingroup kernel_apis
3451 * @{
3452 */
3453
Andy Ross73cb9582017-05-09 10:42:39 -07003454/* Note on sizing: the use of a 20 bit field for block means that,
3455 * assuming a reasonable minimum block size of 16 bytes, we're limited
3456 * to 16M of memory managed by a single pool. Long term it would be
3457 * good to move to a variable bit size based on configuration.
3458 */
3459struct k_mem_block_id {
3460 u32_t pool : 8;
3461 u32_t level : 4;
3462 u32_t block : 20;
3463};
3464
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003465struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003466 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003467 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003468};
3469
Anas Nashif166f5192018-02-25 08:02:36 -06003470/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003471
3472/**
3473 * @defgroup mailbox_apis Mailbox APIs
3474 * @ingroup kernel_apis
3475 * @{
3476 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003477
3478struct k_mbox_msg {
3479 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003480 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003481 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003482 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003483 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003484 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003485 /** sender's message data buffer */
3486 void *tx_data;
3487 /** internal use only - needed for legacy API support */
3488 void *_rx_data;
3489 /** message data block descriptor */
3490 struct k_mem_block tx_block;
3491 /** source thread id */
3492 k_tid_t rx_source_thread;
3493 /** target thread id */
3494 k_tid_t tx_target_thread;
3495 /** internal use only - thread waiting on send (may be a dummy) */
3496 k_tid_t _syncing_thread;
3497#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3498 /** internal use only - semaphore used during asynchronous send */
3499 struct k_sem *_async_sem;
3500#endif
3501};
3502
3503struct k_mbox {
3504 _wait_q_t tx_msg_queue;
3505 _wait_q_t rx_msg_queue;
3506
Anas Nashif2f203c22016-12-18 06:57:45 -05003507 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003508};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003509/**
3510 * @cond INTERNAL_HIDDEN
3511 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003512
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003513#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003514 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003515 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3516 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003517 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003518 }
3519
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003520#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3521
Peter Mitsis12092702016-10-14 12:57:23 -04003522/**
Allan Stephensc98da842016-11-11 15:45:03 -05003523 * INTERNAL_HIDDEN @endcond
3524 */
3525
3526/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003527 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003529 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003530 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003531 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003532 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003533 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003534 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003535 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003536#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003537 struct k_mbox name \
3538 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003539 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003540
Peter Mitsis12092702016-10-14 12:57:23 -04003541/**
3542 * @brief Initialize a mailbox.
3543 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003544 * This routine initializes a mailbox object, prior to its first use.
3545 *
3546 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003547 *
3548 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003549 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003550 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003551extern void k_mbox_init(struct k_mbox *mbox);
3552
Peter Mitsis12092702016-10-14 12:57:23 -04003553/**
3554 * @brief Send a mailbox message in a synchronous manner.
3555 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003556 * This routine sends a message to @a mbox and waits for a receiver to both
3557 * receive and process it. The message data may be in a buffer, in a memory
3558 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003559 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003560 * @param mbox Address of the mailbox.
3561 * @param tx_msg Address of the transmit message descriptor.
3562 * @param timeout Waiting period for the message to be received (in
3563 * milliseconds), or one of the special values K_NO_WAIT
3564 * and K_FOREVER. Once the message has been received,
3565 * this routine waits as long as necessary for the message
3566 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003567 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003568 * @retval 0 Message sent.
3569 * @retval -ENOMSG Returned without waiting.
3570 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003571 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003572 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003573extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003574 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003575
Peter Mitsis12092702016-10-14 12:57:23 -04003576/**
3577 * @brief Send a mailbox message in an asynchronous manner.
3578 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003579 * This routine sends a message to @a mbox without waiting for a receiver
3580 * to process it. The message data may be in a buffer, in a memory pool block,
3581 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3582 * will be given when the message has been both received and completely
3583 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003585 * @param mbox Address of the mailbox.
3586 * @param tx_msg Address of the transmit message descriptor.
3587 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003588 *
3589 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003590 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003591 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003592extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003593 struct k_sem *sem);
3594
Peter Mitsis12092702016-10-14 12:57:23 -04003595/**
3596 * @brief Receive a mailbox message.
3597 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003598 * This routine receives a message from @a mbox, then optionally retrieves
3599 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003600 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003601 * @param mbox Address of the mailbox.
3602 * @param rx_msg Address of the receive message descriptor.
3603 * @param buffer Address of the buffer to receive data, or NULL to defer data
3604 * retrieval and message disposal until later.
3605 * @param timeout Waiting period for a message to be received (in
3606 * milliseconds), or one of the special values K_NO_WAIT
3607 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003608 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003609 * @retval 0 Message received.
3610 * @retval -ENOMSG Returned without waiting.
3611 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003612 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003613 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003614extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003615 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003616
3617/**
3618 * @brief Retrieve mailbox message data into a buffer.
3619 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003620 * This routine completes the processing of a received message by retrieving
3621 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003622 *
3623 * Alternatively, this routine can be used to dispose of a received message
3624 * without retrieving its data.
3625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003626 * @param rx_msg Address of the receive message descriptor.
3627 * @param buffer Address of the buffer to receive data, or NULL to discard
3628 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003629 *
3630 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003631 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003632 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003633extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003634
3635/**
3636 * @brief Retrieve mailbox message data into a memory pool block.
3637 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003638 * This routine completes the processing of a received message by retrieving
3639 * its data into a memory pool block, then disposing of the message.
3640 * The memory pool block that results from successful retrieval must be
3641 * returned to the pool once the data has been processed, even in cases
3642 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003643 *
3644 * Alternatively, this routine can be used to dispose of a received message
3645 * without retrieving its data. In this case there is no need to return a
3646 * memory pool block to the pool.
3647 *
3648 * This routine allocates a new memory pool block for the data only if the
3649 * data is not already in one. If a new block cannot be allocated, the routine
3650 * returns a failure code and the received message is left unchanged. This
3651 * permits the caller to reattempt data retrieval at a later time or to dispose
3652 * of the received message without retrieving its data.
3653 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003654 * @param rx_msg Address of a receive message descriptor.
3655 * @param pool Address of memory pool, or NULL to discard data.
3656 * @param block Address of the area to hold memory pool block info.
3657 * @param timeout Waiting period to wait for a memory pool block (in
3658 * milliseconds), or one of the special values K_NO_WAIT
3659 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003660 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003661 * @retval 0 Data retrieved.
3662 * @retval -ENOMEM Returned without waiting.
3663 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003664 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003665 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003666extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003667 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003668 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003669
Anas Nashif166f5192018-02-25 08:02:36 -06003670/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003671
3672/**
Anas Nashifce78d162018-05-24 12:43:11 -05003673 * @defgroup pipe_apis Pipe APIs
3674 * @ingroup kernel_apis
3675 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003676 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003677
Anas Nashifce78d162018-05-24 12:43:11 -05003678/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003679struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003680 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3681 size_t size; /**< Buffer size */
3682 size_t bytes_used; /**< # bytes used in buffer */
3683 size_t read_index; /**< Where in buffer to read from */
3684 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003685
3686 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003687 _wait_q_t readers; /**< Reader wait queue */
3688 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003689 } wait_q;
3690
Anas Nashif2f203c22016-12-18 06:57:45 -05003691 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Anas Nashifce78d162018-05-24 12:43:11 -05003692 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003693};
3694
Anas Nashifce78d162018-05-24 12:43:11 -05003695/**
3696 * @cond INTERNAL_HIDDEN
3697 */
3698#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3699
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003700#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003701 { \
3702 .buffer = pipe_buffer, \
3703 .size = pipe_buffer_size, \
3704 .bytes_used = 0, \
3705 .read_index = 0, \
3706 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003707 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3708 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003709 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003710 }
3711
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003712#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3713
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003714/**
Allan Stephensc98da842016-11-11 15:45:03 -05003715 * INTERNAL_HIDDEN @endcond
3716 */
3717
3718/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003719 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003720 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003721 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003722 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003723 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003724 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003725 * @param name Name of the pipe.
3726 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3727 * or zero if no ring buffer is used.
3728 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003729 *
3730 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003731 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003732#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3733 static unsigned char __kernel_noinit __aligned(pipe_align) \
3734 _k_pipe_buf_##name[pipe_buffer_size]; \
3735 struct k_pipe name \
3736 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003737 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003738
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003739/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003740 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003742 * This routine initializes a pipe object, prior to its first use.
3743 *
3744 * @param pipe Address of the pipe.
3745 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3746 * is used.
3747 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3748 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003749 *
3750 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003751 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003752 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003753void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3754
3755/**
3756 * @brief Release a pipe's allocated buffer
3757 *
3758 * If a pipe object was given a dynamically allocated buffer via
3759 * k_pipe_alloc_init(), this will free it. This function does nothing
3760 * if the buffer wasn't dynamically allocated.
3761 *
3762 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003763 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003764 */
3765void k_pipe_cleanup(struct k_pipe *pipe);
3766
3767/**
3768 * @brief Initialize a pipe and allocate a buffer for it
3769 *
3770 * Storage for the buffer region will be allocated from the calling thread's
3771 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3772 * or userspace is enabled and the pipe object loses all references to it.
3773 *
3774 * This function should only be called on uninitialized pipe objects.
3775 *
3776 * @param pipe Address of the pipe.
3777 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3778 * buffer is used.
3779 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003780 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003781 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003782 */
3783__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003784
3785/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003786 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003788 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003789 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003790 * @param pipe Address of the pipe.
3791 * @param data Address of data to write.
3792 * @param bytes_to_write Size of data (in bytes).
3793 * @param bytes_written Address of area to hold the number of bytes written.
3794 * @param min_xfer Minimum number of bytes to write.
3795 * @param timeout Waiting period to wait for the data to be written (in
3796 * milliseconds), or one of the special values K_NO_WAIT
3797 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003798 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003799 * @retval 0 At least @a min_xfer bytes of data were written.
3800 * @retval -EIO Returned without waiting; zero data bytes were written.
3801 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003802 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003803 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003804 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003805__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3806 size_t bytes_to_write, size_t *bytes_written,
3807 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003808
3809/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003810 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003812 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003813 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003814 * @param pipe Address of the pipe.
3815 * @param data Address to place the data read from pipe.
3816 * @param bytes_to_read Maximum number of data bytes to read.
3817 * @param bytes_read Address of area to hold the number of bytes read.
3818 * @param min_xfer Minimum number of data bytes to read.
3819 * @param timeout Waiting period to wait for the data to be read (in
3820 * milliseconds), or one of the special values K_NO_WAIT
3821 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003822 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003823 * @retval 0 At least @a min_xfer bytes of data were read.
3824 * @retval -EIO Returned without waiting; zero data bytes were read.
3825 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003826 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003827 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003828 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003829__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3830 size_t bytes_to_read, size_t *bytes_read,
3831 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003832
3833/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003834 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003835 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003836 * This routine writes the data contained in a memory block to @a pipe.
3837 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003838 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003840 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003841 * @param block Memory block containing data to send
3842 * @param size Number of data bytes in memory block to send
3843 * @param sem Semaphore to signal upon completion (else NULL)
3844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003845 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003846 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003847 */
3848extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3849 size_t size, struct k_sem *sem);
3850
Anas Nashif166f5192018-02-25 08:02:36 -06003851/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003852
Allan Stephensc98da842016-11-11 15:45:03 -05003853/**
3854 * @cond INTERNAL_HIDDEN
3855 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003856
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003857struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003858 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003859 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003860 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003861 char *buffer;
3862 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003863 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003864
Anas Nashif2f203c22016-12-18 06:57:45 -05003865 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003866};
3867
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003868#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003869 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003870 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003871 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003872 .num_blocks = slab_num_blocks, \
3873 .block_size = slab_block_size, \
3874 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003875 .free_list = NULL, \
3876 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003877 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003878 }
3879
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003880#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3881
3882
Peter Mitsis578f9112016-10-07 13:50:31 -04003883/**
Allan Stephensc98da842016-11-11 15:45:03 -05003884 * INTERNAL_HIDDEN @endcond
3885 */
3886
3887/**
3888 * @defgroup mem_slab_apis Memory Slab APIs
3889 * @ingroup kernel_apis
3890 * @{
3891 */
3892
3893/**
Allan Stephensda827222016-11-09 14:23:58 -06003894 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003895 *
Allan Stephensda827222016-11-09 14:23:58 -06003896 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003897 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003898 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3899 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003900 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003901 *
Allan Stephensda827222016-11-09 14:23:58 -06003902 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003903 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003904 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003905 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003906 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003907 * @param name Name of the memory slab.
3908 * @param slab_block_size Size of each memory block (in bytes).
3909 * @param slab_num_blocks Number memory blocks.
3910 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003911 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003912 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003913#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3914 char __noinit __aligned(slab_align) \
3915 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3916 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003917 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003918 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003919 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003920
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003921/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003922 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003923 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003924 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003925 *
Allan Stephensda827222016-11-09 14:23:58 -06003926 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3927 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3928 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3929 * To ensure that each memory block is similarly aligned to this boundary,
3930 * @a slab_block_size must also be a multiple of N.
3931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003932 * @param slab Address of the memory slab.
3933 * @param buffer Pointer to buffer used for the memory blocks.
3934 * @param block_size Size of each memory block (in bytes).
3935 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003936 *
3937 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003938 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003939 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003940extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003941 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003942
3943/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003944 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003946 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003947 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003948 * @param slab Address of the memory slab.
3949 * @param mem Pointer to block address area.
3950 * @param timeout Maximum time to wait for operation to complete
3951 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3952 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003953 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003954 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003955 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003956 * @retval -ENOMEM Returned without waiting.
3957 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003958 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003959 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003960extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003961 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003962
3963/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003964 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003965 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003966 * This routine releases a previously allocated memory block back to its
3967 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003969 * @param slab Address of the memory slab.
3970 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003971 *
3972 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003973 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003974 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003975extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003976
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003977/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003978 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003979 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003980 * This routine gets the number of memory blocks that are currently
3981 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003983 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003984 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003985 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003986 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003987 */
Kumar Galacc334c72017-04-21 10:55:34 -05003988static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003989{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003990 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003991}
3992
Peter Mitsisc001aa82016-10-13 13:53:37 -04003993/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003994 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003996 * This routine gets the number of memory blocks that are currently
3997 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003999 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004001 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004002 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004003 */
Kumar Galacc334c72017-04-21 10:55:34 -05004004static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004005{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004006 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004007}
4008
Anas Nashif166f5192018-02-25 08:02:36 -06004009/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004010
4011/**
4012 * @cond INTERNAL_HIDDEN
4013 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004014
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004015struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004016 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004017 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004018};
4019
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004020/**
Allan Stephensc98da842016-11-11 15:45:03 -05004021 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004022 */
4023
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004024/**
Allan Stephensc98da842016-11-11 15:45:03 -05004025 * @addtogroup mem_pool_apis
4026 * @{
4027 */
4028
4029/**
4030 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004032 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4033 * long. The memory pool allows blocks to be repeatedly partitioned into
4034 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004035 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004036 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004037 * If the pool is to be accessed outside the module where it is defined, it
4038 * can be declared via
4039 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004040 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004042 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004043 * @param minsz Size of the smallest blocks in the pool (in bytes).
4044 * @param maxsz Size of the largest blocks in the pool (in bytes).
4045 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004046 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004047 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004048 */
Andy Ross73cb9582017-05-09 10:42:39 -07004049#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4050 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4051 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08004052 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07004053 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004054 .base = { \
4055 .buf = _mpool_buf_##name, \
4056 .max_sz = maxsz, \
4057 .n_max = nmax, \
4058 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
4059 .levels = _mpool_lvls_##name, \
4060 .flags = SYS_MEM_POOL_KERNEL \
4061 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004062 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004063
Peter Mitsis937042c2016-10-13 13:18:26 -04004064/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004065 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004066 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004067 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004069 * @param pool Address of the memory pool.
4070 * @param block Pointer to block descriptor for the allocated memory.
4071 * @param size Amount of memory to allocate (in bytes).
4072 * @param timeout Maximum time to wait for operation to complete
4073 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4074 * or K_FOREVER to wait as long as necessary.
4075 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004076 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004077 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004078 * @retval -ENOMEM Returned without waiting.
4079 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004080 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004081 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004082extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004083 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004084
4085/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004086 * @brief Allocate memory from a memory pool with malloc() semantics
4087 *
4088 * Such memory must be released using k_free().
4089 *
4090 * @param pool Address of the memory pool.
4091 * @param size Amount of memory to allocate (in bytes).
4092 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004093 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004094 */
4095extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4096
4097/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004098 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004099 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004100 * This routine releases a previously allocated memory block back to its
4101 * memory pool.
4102 *
4103 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004104 *
4105 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004106 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004107 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004108extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004109
4110/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004111 * @brief Free memory allocated from a memory pool.
4112 *
4113 * This routine releases a previously allocated memory block back to its
4114 * memory pool
4115 *
4116 * @param id Memory block identifier.
4117 *
4118 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004119 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004120 */
4121extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4122
4123/**
Anas Nashif166f5192018-02-25 08:02:36 -06004124 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004125 */
4126
4127/**
4128 * @defgroup heap_apis Heap Memory Pool APIs
4129 * @ingroup kernel_apis
4130 * @{
4131 */
4132
4133/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004134 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004135 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004136 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004137 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004139 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004140 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004141 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004142 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004143 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004144extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004145
4146/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004147 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004148 *
4149 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004150 * returned must have been allocated from the heap memory pool or
4151 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004152 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004153 * If @a ptr is NULL, no operation is performed.
4154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004155 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004156 *
4157 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004158 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004159 */
4160extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004161
Allan Stephensc98da842016-11-11 15:45:03 -05004162/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004163 * @brief Allocate memory from heap, array style
4164 *
4165 * This routine provides traditional calloc() semantics. Memory is
4166 * allocated from the heap memory pool and zeroed.
4167 *
4168 * @param nmemb Number of elements in the requested array
4169 * @param size Size of each array element (in bytes).
4170 *
4171 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004172 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004173 */
4174extern void *k_calloc(size_t nmemb, size_t size);
4175
Anas Nashif166f5192018-02-25 08:02:36 -06004176/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004177
Benjamin Walshacc68c12017-01-29 18:57:45 -05004178/* polling API - PRIVATE */
4179
Benjamin Walshb0179862017-02-02 16:39:57 -05004180#ifdef CONFIG_POLL
4181#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
4182#else
4183#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
4184#endif
4185
Benjamin Walshacc68c12017-01-29 18:57:45 -05004186/* private - implementation data created as needed, per-type */
4187struct _poller {
4188 struct k_thread *thread;
4189};
4190
4191/* private - types bit positions */
4192enum _poll_types_bits {
4193 /* can be used to ignore an event */
4194 _POLL_TYPE_IGNORE,
4195
4196 /* to be signaled by k_poll_signal() */
4197 _POLL_TYPE_SIGNAL,
4198
4199 /* semaphore availability */
4200 _POLL_TYPE_SEM_AVAILABLE,
4201
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004202 /* queue/fifo/lifo data availability */
4203 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004204
4205 _POLL_NUM_TYPES
4206};
4207
4208#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4209
4210/* private - states bit positions */
4211enum _poll_states_bits {
4212 /* default state when creating event */
4213 _POLL_STATE_NOT_READY,
4214
Benjamin Walshacc68c12017-01-29 18:57:45 -05004215 /* signaled by k_poll_signal() */
4216 _POLL_STATE_SIGNALED,
4217
4218 /* semaphore is available */
4219 _POLL_STATE_SEM_AVAILABLE,
4220
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004221 /* data is available to read on queue/fifo/lifo */
4222 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004223
4224 _POLL_NUM_STATES
4225};
4226
4227#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4228
4229#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004230 (32 - (0 \
4231 + 8 /* tag */ \
4232 + _POLL_NUM_TYPES \
4233 + _POLL_NUM_STATES \
4234 + 1 /* modes */ \
4235 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004236
Benjamin Walshacc68c12017-01-29 18:57:45 -05004237/* end of polling API - PRIVATE */
4238
4239
4240/**
4241 * @defgroup poll_apis Async polling APIs
4242 * @ingroup kernel_apis
4243 * @{
4244 */
4245
4246/* Public polling API */
4247
4248/* public - values for k_poll_event.type bitfield */
4249#define K_POLL_TYPE_IGNORE 0
4250#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4251#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004252#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4253#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004254
4255/* public - polling modes */
4256enum k_poll_modes {
4257 /* polling thread does not take ownership of objects when available */
4258 K_POLL_MODE_NOTIFY_ONLY = 0,
4259
4260 K_POLL_NUM_MODES
4261};
4262
4263/* public - values for k_poll_event.state bitfield */
4264#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004265#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4266#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004267#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4268#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004269
4270/* public - poll signal object */
4271struct k_poll_signal {
4272 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004273 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004274
4275 /*
4276 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4277 * user resets it to 0.
4278 */
4279 unsigned int signaled;
4280
4281 /* custom result value passed to k_poll_signal() if needed */
4282 int result;
4283};
4284
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004285#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004286 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004287 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004288 .signaled = 0, \
4289 .result = 0, \
4290 }
4291
4292struct k_poll_event {
4293 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004294 sys_dnode_t _node;
4295
4296 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004297 struct _poller *poller;
4298
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004299 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004300 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004301
Benjamin Walshacc68c12017-01-29 18:57:45 -05004302 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004303 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004304
4305 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004306 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004307
4308 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004309 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004310
4311 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004312 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004313
4314 /* per-type data */
4315 union {
4316 void *obj;
4317 struct k_poll_signal *signal;
4318 struct k_sem *sem;
4319 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004320 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004321 };
4322};
4323
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004324#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004325 { \
4326 .poller = NULL, \
4327 .type = event_type, \
4328 .state = K_POLL_STATE_NOT_READY, \
4329 .mode = event_mode, \
4330 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004331 { .obj = event_obj }, \
4332 }
4333
4334#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4335 event_tag) \
4336 { \
4337 .type = event_type, \
4338 .tag = event_tag, \
4339 .state = K_POLL_STATE_NOT_READY, \
4340 .mode = event_mode, \
4341 .unused = 0, \
4342 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004343 }
4344
4345/**
4346 * @brief Initialize one struct k_poll_event instance
4347 *
4348 * After this routine is called on a poll event, the event it ready to be
4349 * placed in an event array to be passed to k_poll().
4350 *
4351 * @param event The event to initialize.
4352 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4353 * values. Only values that apply to the same object being polled
4354 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4355 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004356 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004357 * @param obj Kernel object or poll signal.
4358 *
4359 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004360 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004361 */
4362
Kumar Galacc334c72017-04-21 10:55:34 -05004363extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004364 int mode, void *obj);
4365
4366/**
4367 * @brief Wait for one or many of multiple poll events to occur
4368 *
4369 * This routine allows a thread to wait concurrently for one or many of
4370 * multiple poll events to have occurred. Such events can be a kernel object
4371 * being available, like a semaphore, or a poll signal event.
4372 *
4373 * When an event notifies that a kernel object is available, the kernel object
4374 * is not "given" to the thread calling k_poll(): it merely signals the fact
4375 * that the object was available when the k_poll() call was in effect. Also,
4376 * all threads trying to acquire an object the regular way, i.e. by pending on
4377 * the object, have precedence over the thread polling on the object. This
4378 * means that the polling thread will never get the poll event on an object
4379 * until the object becomes available and its pend queue is empty. For this
4380 * reason, the k_poll() call is more effective when the objects being polled
4381 * only have one thread, the polling thread, trying to acquire them.
4382 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004383 * When k_poll() returns 0, the caller should loop on all the events that were
4384 * passed to k_poll() and check the state field for the values that were
4385 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004386 *
4387 * Before being reused for another call to k_poll(), the user has to reset the
4388 * state field to K_POLL_STATE_NOT_READY.
4389 *
Andrew Boie3772f772018-05-07 16:52:57 -07004390 * When called from user mode, a temporary memory allocation is required from
4391 * the caller's resource pool.
4392 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004393 * @param events An array of pointers to events to be polled for.
4394 * @param num_events The number of events in the array.
4395 * @param timeout Waiting period for an event to be ready (in milliseconds),
4396 * or one of the special values K_NO_WAIT and K_FOREVER.
4397 *
4398 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004399 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02004400 * @retval -EINTR Poller thread has been interrupted.
Andrew Boie3772f772018-05-07 16:52:57 -07004401 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4402 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004403 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004404 */
4405
Andrew Boie3772f772018-05-07 16:52:57 -07004406__syscall int k_poll(struct k_poll_event *events, int num_events,
4407 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004408
4409/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004410 * @brief Initialize a poll signal object.
4411 *
4412 * Ready a poll signal object to be signaled via k_poll_signal().
4413 *
4414 * @param signal A poll signal.
4415 *
4416 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004417 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004418 */
4419
Andrew Boie3772f772018-05-07 16:52:57 -07004420__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4421
4422/*
4423 * @brief Reset a poll signal object's state to unsignaled.
4424 *
4425 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004426 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004427 */
4428__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4429
4430static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4431{
4432 signal->signaled = 0;
4433}
4434
4435/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004436 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004437 *
4438 * @param signal A poll signal object
4439 * @param signaled An integer buffer which will be written nonzero if the
4440 * object was signaled
4441 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004442 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004443 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004444 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004445 */
4446__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4447 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004448
4449/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004450 * @brief Signal a poll signal object.
4451 *
4452 * This routine makes ready a poll signal, which is basically a poll event of
4453 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4454 * made ready to run. A @a result value can be specified.
4455 *
4456 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004457 * k_poll_signal(), stays set until the user sets it back to 0 with
4458 * k_poll_signal_reset(). It thus has to be reset by the user before being
4459 * passed again to k_poll() or k_poll() will consider it being signaled, and
4460 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004461 *
4462 * @param signal A poll signal.
4463 * @param result The value to store in the result field of the signal.
4464 *
4465 * @retval 0 The signal was delivered successfully.
4466 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004467 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004468 */
4469
Andrew Boie3772f772018-05-07 16:52:57 -07004470__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004471
Anas Nashif954d5502018-02-25 08:37:28 -06004472/**
4473 * @internal
4474 */
Andy Ross8606fab2018-03-26 10:54:40 -07004475extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004476
Anas Nashif166f5192018-02-25 08:02:36 -06004477/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004478
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004479/**
4480 * @brief Make the CPU idle.
4481 *
4482 * This function makes the CPU idle until an event wakes it up.
4483 *
4484 * In a regular system, the idle thread should be the only thread responsible
4485 * for making the CPU idle and triggering any type of power management.
4486 * However, in some more constrained systems, such as a single-threaded system,
4487 * the only thread would be responsible for this if needed.
4488 *
4489 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004490 * @req K-MISC-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004491 */
4492extern void k_cpu_idle(void);
4493
4494/**
4495 * @brief Make the CPU idle in an atomic fashion.
4496 *
4497 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4498 * must be done atomically before making the CPU idle.
4499 *
4500 * @param key Interrupt locking key obtained from irq_lock().
4501 *
4502 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004503 * @req K-MISC-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004504 */
4505extern void k_cpu_atomic_idle(unsigned int key);
4506
Anas Nashif954d5502018-02-25 08:37:28 -06004507
4508/**
4509 * @internal
4510 */
Kumar Galacc334c72017-04-21 10:55:34 -05004511extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004512
Andrew Boiecdb94d62017-04-18 15:22:05 -07004513#ifdef _ARCH_EXCEPT
4514/* This archtecture has direct support for triggering a CPU exception */
4515#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4516#else
4517
Andrew Boiecdb94d62017-04-18 15:22:05 -07004518/* NOTE: This is the implementation for arches that do not implement
4519 * _ARCH_EXCEPT() to generate a real CPU exception.
4520 *
4521 * We won't have a real exception frame to determine the PC value when
4522 * the oops occurred, so print file and line number before we jump into
4523 * the fatal error handler.
4524 */
4525#define _k_except_reason(reason) do { \
4526 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4527 _NanoFatalErrorHandler(reason, &_default_esf); \
4528 CODE_UNREACHABLE; \
4529 } while (0)
4530
4531#endif /* _ARCH__EXCEPT */
4532
4533/**
4534 * @brief Fatally terminate a thread
4535 *
4536 * This should be called when a thread has encountered an unrecoverable
4537 * runtime condition and needs to terminate. What this ultimately
4538 * means is determined by the _fatal_error_handler() implementation, which
4539 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4540 *
4541 * If this is called from ISR context, the default system fatal error handler
4542 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004543 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004544 */
4545#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4546
4547/**
4548 * @brief Fatally terminate the system
4549 *
4550 * This should be called when the Zephyr kernel has encountered an
4551 * unrecoverable runtime condition and needs to terminate. What this ultimately
4552 * means is determined by the _fatal_error_handler() implementation, which
4553 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004554 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004555 */
4556#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4557
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004558/*
4559 * private APIs that are utilized by one or more public APIs
4560 */
4561
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004562#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004563/**
4564 * @internal
4565 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004566extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004567#else
Anas Nashif954d5502018-02-25 08:37:28 -06004568/**
4569 * @internal
4570 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004571#define _init_static_threads() do { } while ((0))
4572#endif
4573
Anas Nashif954d5502018-02-25 08:37:28 -06004574/**
4575 * @internal
4576 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004577extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004578/**
4579 * @internal
4580 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004581extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004582
Andrew Boiedc5d9352017-06-02 12:56:47 -07004583/* arch/cpu.h may declare an architecture or platform-specific macro
4584 * for properly declaring stacks, compatible with MMU/MPU constraints if
4585 * enabled
4586 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004587
4588/**
4589 * @brief Obtain an extern reference to a stack
4590 *
4591 * This macro properly brings the symbol of a thread stack declared
4592 * elsewhere into scope.
4593 *
4594 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004595 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004596 */
4597#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4598
Andrew Boiedc5d9352017-06-02 12:56:47 -07004599#ifdef _ARCH_THREAD_STACK_DEFINE
4600#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4601#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4602 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4603#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4604#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004605static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004606{
4607 return _ARCH_THREAD_STACK_BUFFER(sym);
4608}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004609#else
4610/**
4611 * @brief Declare a toplevel thread stack memory region
4612 *
4613 * This declares a region of memory suitable for use as a thread's stack.
4614 *
4615 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4616 * 'noinit' section so that it isn't zeroed at boot
4617 *
Andrew Boie507852a2017-07-25 18:47:07 -07004618 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004619 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004620 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004621 *
4622 * It is legal to precede this definition with the 'static' keyword.
4623 *
4624 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4625 * parameter of k_thread_create(), it may not be the same as the
4626 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4627 *
4628 * @param sym Thread stack symbol name
4629 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004630 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004631 */
4632#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004633 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004634
4635/**
4636 * @brief Declare a toplevel array of thread stack memory regions
4637 *
4638 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4639 * definition for additional details and constraints.
4640 *
4641 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4642 * 'noinit' section so that it isn't zeroed at boot
4643 *
4644 * @param sym Thread stack symbol name
4645 * @param nmemb Number of stacks to declare
4646 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004647 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004648 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004649#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004650 struct _k_thread_stack_element __noinit \
4651 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004652
4653/**
4654 * @brief Declare an embedded stack memory region
4655 *
4656 * Used for stacks embedded within other data structures. Use is highly
4657 * discouraged but in some cases necessary. For memory protection scenarios,
4658 * it is very important that any RAM preceding this member not be writable
4659 * by threads else a stack overflow will lead to silent corruption. In other
4660 * words, the containing data structure should live in RAM owned by the kernel.
4661 *
4662 * @param sym Thread stack symbol name
4663 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004664 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004665 */
4666#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004667 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004668
4669/**
4670 * @brief Return the size in bytes of a stack memory region
4671 *
4672 * Convenience macro for passing the desired stack size to k_thread_create()
4673 * since the underlying implementation may actually create something larger
4674 * (for instance a guard area).
4675 *
4676 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004677 * passed to K_THREAD_STACK_DEFINE.
4678 *
4679 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4680 * it is not guaranteed to return the original value since each array
4681 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004682 *
4683 * @param sym Stack memory symbol
4684 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004685 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004686 */
4687#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4688
4689/**
4690 * @brief Get a pointer to the physical stack buffer
4691 *
4692 * Convenience macro to get at the real underlying stack buffer used by
4693 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4694 * This is really only intended for diagnostic tools which want to examine
4695 * stack memory contents.
4696 *
4697 * @param sym Declared stack symbol name
4698 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004699 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004700 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004701static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004702{
4703 return (char *)sym;
4704}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004705
4706#endif /* _ARCH_DECLARE_STACK */
4707
Chunlin Hane9c97022017-07-07 20:29:30 +08004708/**
4709 * @defgroup mem_domain_apis Memory domain APIs
4710 * @ingroup kernel_apis
4711 * @{
4712 */
4713
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004714/**
4715 * @def MEM_PARTITION_ENTRY
4716 * @brief Used to declare a memory partition entry
4717 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004718 */
4719#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4720 {\
4721 .start = _start, \
4722 .size = _size, \
4723 .attr = _attr, \
4724 }
4725
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004726/**
4727 * @def K_MEM_PARTITION_DEFINE
4728 * @brief Used to declare a memory partition
4729 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004730 */
4731#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4732#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4733 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304734 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004735 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4736#else
4737#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304738 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004739 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4740#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4741
Chunlin Hane9c97022017-07-07 20:29:30 +08004742/* memory partition */
4743struct k_mem_partition {
4744 /* start address of memory partition */
4745 u32_t start;
4746 /* size of memory partition */
4747 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304748#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004749 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304750 k_mem_partition_attr_t attr;
4751#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004752};
4753
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304754/* memory domian
4755 * Note: Always declare this structure with __kernel prefix
4756 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004757struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304758#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004759 /* partitions in the domain */
4760 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304761#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004762 /* domain q */
4763 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004764 /* number of partitions in the domain */
4765 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004766};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304767
Chunlin Hane9c97022017-07-07 20:29:30 +08004768
4769/**
4770 * @brief Initialize a memory domain.
4771 *
4772 * Initialize a memory domain with given name and memory partitions.
4773 *
4774 * @param domain The memory domain to be initialized.
4775 * @param num_parts The number of array items of "parts" parameter.
4776 * @param parts An array of pointers to the memory partitions. Can be NULL
4777 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004778 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004779 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004780extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004781 struct k_mem_partition *parts[]);
4782/**
4783 * @brief Destroy a memory domain.
4784 *
4785 * Destroy a memory domain.
4786 *
4787 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004788 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004789 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004790extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4791
4792/**
4793 * @brief Add a memory partition into a memory domain.
4794 *
4795 * Add a memory partition into a memory domain.
4796 *
4797 * @param domain The memory domain to be added a memory partition.
4798 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004799 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004800 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004801extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4802 struct k_mem_partition *part);
4803
4804/**
4805 * @brief Remove a memory partition from a memory domain.
4806 *
4807 * Remove a memory partition from a memory domain.
4808 *
4809 * @param domain The memory domain to be removed a memory partition.
4810 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004811 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004812 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004813extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4814 struct k_mem_partition *part);
4815
4816/**
4817 * @brief Add a thread into a memory domain.
4818 *
4819 * Add a thread into a memory domain.
4820 *
4821 * @param domain The memory domain that the thread is going to be added into.
4822 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004823 *
4824 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004825 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004826extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4827 k_tid_t thread);
4828
4829/**
4830 * @brief Remove a thread from its memory domain.
4831 *
4832 * Remove a thread from its memory domain.
4833 *
4834 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004835 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004836 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004837extern void k_mem_domain_remove_thread(k_tid_t thread);
4838
Anas Nashif166f5192018-02-25 08:02:36 -06004839/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004840
Andrew Boie756f9072017-10-10 16:01:49 -07004841/**
4842 * @brief Emit a character buffer to the console device
4843 *
4844 * @param c String of characters to print
4845 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004846 *
4847 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004848 */
4849__syscall void k_str_out(char *c, size_t n);
4850
Andy Rosse7172672018-01-24 15:48:32 -08004851/**
4852 * @brief Start a numbered CPU on a MP-capable system
4853
4854 * This starts and initializes a specific CPU. The main thread on
4855 * startup is running on CPU zero, other processors are numbered
4856 * sequentially. On return from this function, the CPU is known to
4857 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004858 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004859 * with the provided key will work to enable them.
4860 *
4861 * Normally, in SMP mode this function will be called by the kernel
4862 * initialization and should not be used as a user API. But it is
4863 * defined here for special-purpose apps which want Zephyr running on
4864 * one core and to use others for design-specific processing.
4865 *
4866 * @param cpu_num Integer number of the CPU
4867 * @param stack Stack memory for the CPU
4868 * @param sz Stack buffer size, in bytes
4869 * @param fn Function to begin running on the CPU. First argument is
4870 * an irq_unlock() key.
4871 * @param arg Untyped argument to be passed to "fn"
4872 */
4873extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4874 void (*fn)(int, void *), void *arg);
4875
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004876#ifdef __cplusplus
4877}
4878#endif
4879
Andrew Boiee004dec2016-11-07 09:01:19 -08004880#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4881/*
4882 * Define new and delete operators.
4883 * At this moment, the operators do nothing since objects are supposed
4884 * to be statically allocated.
4885 */
4886inline void operator delete(void *ptr)
4887{
4888 (void)ptr;
4889}
4890
4891inline void operator delete[](void *ptr)
4892{
4893 (void)ptr;
4894}
4895
4896inline void *operator new(size_t size)
4897{
4898 (void)size;
4899 return NULL;
4900}
4901
4902inline void *operator new[](size_t size)
4903{
4904 (void)size;
4905 return NULL;
4906}
4907
4908/* Placement versions of operator new and delete */
4909inline void operator delete(void *ptr1, void *ptr2)
4910{
4911 (void)ptr1;
4912 (void)ptr2;
4913}
4914
4915inline void operator delete[](void *ptr1, void *ptr2)
4916{
4917 (void)ptr1;
4918 (void)ptr2;
4919}
4920
4921inline void *operator new(size_t size, void *ptr)
4922{
4923 (void)size;
4924 return ptr;
4925}
4926
4927inline void *operator new[](size_t size, void *ptr)
4928{
4929 (void)size;
4930 return ptr;
4931}
4932
4933#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4934
Andrew Boiefa94ee72017-09-28 16:54:35 -07004935#include <syscalls/kernel.h>
4936
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004937#endif /* !_ASMLANGUAGE */
4938
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004939#endif /* _kernel__h_ */