blob: d58f1a8e8a70f6127ccec6ee1c0eb68a86e885bd [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 {
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700500 /* Stack Start - Identical to K_THREAD_STACK_BUFFER() on the stack
501 * object. Represents thread-writable stack area without any extras.
502 */
Andrew Boie73abd322017-04-04 13:19:13 -0700503 u32_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700504
505 /* Stack Size - Thread writable stack buffer size. Represents
506 * the size of the actual area, starting from the start member,
507 * that should be writable by the thread
508 */
Andrew Boie73abd322017-04-04 13:19:13 -0700509 u32_t size;
510};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700511
512typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700513#endif /* CONFIG_THREAD_STACK_INFO */
514
Chunlin Hane9c97022017-07-07 20:29:30 +0800515#if defined(CONFIG_USERSPACE)
516struct _mem_domain_info {
517 /* memory domain queue node */
518 sys_dnode_t mem_domain_q_node;
519 /* memory domain of the thread */
520 struct k_mem_domain *mem_domain;
521};
522
523#endif /* CONFIG_USERSPACE */
524
Anas Nashifce78d162018-05-24 12:43:11 -0500525/**
526 * @ingroup thread_apis
527 * Thread Structure
528 */
Andrew Boie73abd322017-04-04 13:19:13 -0700529struct k_thread {
530
531 struct _thread_base base;
532
Anas Nashifce78d162018-05-24 12:43:11 -0500533 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700534 struct _caller_saved caller_saved;
Anas Nashifce78d162018-05-24 12:43:11 -0500535 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700536 struct _callee_saved callee_saved;
537
Anas Nashifce78d162018-05-24 12:43:11 -0500538 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700539 void *init_data;
540
Anas Nashifce78d162018-05-24 12:43:11 -0500541 /**
542 * abort function
543 * @req K-THREAD-002
544 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700545 void (*fn_abort)(void);
546
547#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500548 /** thread entry and parameters description */
Andrew Boie73abd322017-04-04 13:19:13 -0700549 struct __thread_entry *entry;
550
Anas Nashifce78d162018-05-24 12:43:11 -0500551 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700552 struct k_thread *next_thread;
553#endif
554
555#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500556 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700557 void *custom_data;
558#endif
559
560#ifdef CONFIG_ERRNO
Anas Nashifce78d162018-05-24 12:43:11 -0500561 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700562 int errno_var;
563#endif
564
565#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500566 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700567 struct _thread_stack_info stack_info;
568#endif /* CONFIG_THREAD_STACK_INFO */
569
Chunlin Hane9c97022017-07-07 20:29:30 +0800570#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500571 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800572 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500573 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700574 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800575#endif /* CONFIG_USERSPACE */
576
Andy Ross042d8ec2017-12-09 08:37:20 -0800577#if defined(CONFIG_USE_SWITCH)
578 /* When using __switch() a few previously arch-specific items
579 * become part of the core OS
580 */
581
Anas Nashifce78d162018-05-24 12:43:11 -0500582 /** _Swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800583 int swap_retval;
584
Anas Nashifce78d162018-05-24 12:43:11 -0500585 /** Context handle returned via _arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800586 void *switch_handle;
587#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500588 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700589 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800590
Anas Nashifce78d162018-05-24 12:43:11 -0500591 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700592 struct _thread_arch arch;
593};
594
595typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400596typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700597#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400598
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400599enum execution_context_types {
600 K_ISR = 0,
601 K_COOP_THREAD,
602 K_PREEMPT_THREAD,
603};
604
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400605/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100606 * @defgroup profiling_apis Profiling APIs
607 * @ingroup kernel_apis
608 * @{
609 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530610typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
611 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100612
613/**
614 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
615 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700616 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100617 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
618 *
619 * CONFIG_MAIN_STACK_SIZE
620 * CONFIG_IDLE_STACK_SIZE
621 * CONFIG_ISR_STACK_SIZE
622 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
623 *
624 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
625 * produce output.
626 *
627 * @return N/A
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530628 *
629 * @deprecated This API is deprecated. Use k_thread_foreach().
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100630 */
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530631__deprecated extern void k_call_stacks_analyze(void);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100632
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530633/**
634 * @brief Iterate over all the threads in the system.
635 *
636 * This routine iterates over all the threads in the system and
637 * calls the user_cb function for each thread.
638 *
639 * @param user_cb Pointer to the user callback function.
640 * @param user_data Pointer to user data.
641 *
642 * @note CONFIG_THREAD_MONITOR must be set for this function
643 * to be effective. Also this API uses irq_lock to protect the
644 * _kernel.threads list which means creation of new threads and
645 * terminations of existing threads are blocked until this
646 * API returns.
647 *
648 * @return N/A
649 */
650extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
651
Anas Nashif166f5192018-02-25 08:02:36 -0600652/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100653
654/**
Allan Stephensc98da842016-11-11 15:45:03 -0500655 * @defgroup thread_apis Thread APIs
656 * @ingroup kernel_apis
657 * @{
658 */
659
Benjamin Walshed240f22017-01-22 13:05:08 -0500660#endif /* !_ASMLANGUAGE */
661
662
663/*
664 * Thread user options. May be needed by assembly code. Common part uses low
665 * bits, arch-specific use high bits.
666 */
667
Anas Nashifa541e932018-05-24 11:19:16 -0500668/**
669 * @brief system thread that must not abort
670 * @req K-THREAD-000
671 * */
Benjamin Walshed240f22017-01-22 13:05:08 -0500672#define K_ESSENTIAL (1 << 0)
673
674#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500675/**
676 * @brief thread uses floating point registers
677 */
Benjamin Walshed240f22017-01-22 13:05:08 -0500678#define K_FP_REGS (1 << 1)
679#endif
680
Anas Nashifa541e932018-05-24 11:19:16 -0500681/**
682 * @brief user mode thread
683 *
684 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700685 * has additional restrictions
686 */
687#define K_USER (1 << 2)
688
Anas Nashifa541e932018-05-24 11:19:16 -0500689/**
690 * @brief Inherit Permissions
691 *
692 * @details
693 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700694 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
695 * is not enabled.
696 */
697#define K_INHERIT_PERMS (1 << 3)
698
Benjamin Walshed240f22017-01-22 13:05:08 -0500699#ifdef CONFIG_X86
700/* x86 Bitmask definitions for threads user options */
701
702#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
703/* thread uses SSEx (and also FP) registers */
704#define K_SSE_REGS (1 << 7)
705#endif
706#endif
707
708/* end - thread options */
709
710#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400711/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700712 * @brief Create a thread.
713 *
714 * This routine initializes a thread, then schedules it for execution.
715 *
716 * The new thread may be scheduled for immediate execution or a delayed start.
717 * If the newly spawned thread does not have a delayed start the kernel
718 * scheduler may preempt the current thread to allow the new thread to
719 * execute.
720 *
721 * Thread options are architecture-specific, and can include K_ESSENTIAL,
722 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
723 * them using "|" (the logical OR operator).
724 *
725 * Historically, users often would use the beginning of the stack memory region
726 * to store the struct k_thread data, although corruption will occur if the
727 * stack overflows this region and stack protection features may not detect this
728 * situation.
729 *
730 * @param new_thread Pointer to uninitialized struct k_thread
731 * @param stack Pointer to the stack space.
732 * @param stack_size Stack size in bytes.
733 * @param entry Thread entry function.
734 * @param p1 1st entry point parameter.
735 * @param p2 2nd entry point parameter.
736 * @param p3 3rd entry point parameter.
737 * @param prio Thread priority.
738 * @param options Thread options.
739 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
740 *
741 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400742 *
743 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700744 */
Andrew Boie662c3452017-10-02 10:51:18 -0700745__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700746 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700747 size_t stack_size,
748 k_thread_entry_t entry,
749 void *p1, void *p2, void *p3,
750 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700751
Andrew Boie3f091b52017-08-30 14:34:14 -0700752/**
753 * @brief Drop a thread's privileges permanently to user mode
754 *
755 * @param entry Function to start executing from
756 * @param p1 1st entry point parameter
757 * @param p2 2nd entry point parameter
758 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400759 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700760 */
761extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
762 void *p1, void *p2,
763 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700764
Andrew Boied26cf2d2017-03-30 13:07:02 -0700765/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700766 * @brief Grant a thread access to a NULL-terminated set of kernel objects
767 *
768 * This is a convenience function. For the provided thread, grant access to
769 * the remaining arguments, which must be pointers to kernel objects.
770 * The final argument must be a NULL.
771 *
772 * The thread object must be initialized (i.e. running). The objects don't
773 * need to be.
774 *
775 * @param thread Thread to grant access to objects
776 * @param ... NULL-terminated list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400777 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700778 */
779extern void __attribute__((sentinel))
780 k_thread_access_grant(struct k_thread *thread, ...);
781
782/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700783 * @brief Assign a resource memory pool to a thread
784 *
785 * By default, threads have no resource pool assigned unless their parent
786 * thread has a resource pool, in which case it is inherited. Multiple
787 * threads may be assigned to the same memory pool.
788 *
789 * Changing a thread's resource pool will not migrate allocations from the
790 * previous pool.
791 *
792 * @param thread Target thread to assign a memory pool for resource requests,
793 * or NULL if the thread should no longer have a memory pool.
794 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400795 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700796 */
797static inline void k_thread_resource_pool_assign(struct k_thread *thread,
798 struct k_mem_pool *pool)
799{
800 thread->resource_pool = pool;
801}
802
803#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
804/**
805 * @brief Assign the system heap as a thread's resource pool
806 *
807 * Similar to k_thread_resource_pool_assign(), but the thread will use
808 * the kernel heap to draw memory.
809 *
810 * Use with caution, as a malicious thread could perform DoS attacks on the
811 * kernel heap.
812 *
813 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400814 *
815 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700816 */
817void k_thread_system_pool_assign(struct k_thread *thread);
818#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
819
820/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500821 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400822 *
Allan Stephensc98da842016-11-11 15:45:03 -0500823 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500824 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400825 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500826 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827 *
828 * @return N/A
829 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700830__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400831
832/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500833 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400834 *
835 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500836 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400837 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838 * @return N/A
839 */
Kumar Galacc334c72017-04-21 10:55:34 -0500840extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400841
842/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500843 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500845 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400846 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500847 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848 *
849 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400850 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 */
Andrew Boie468190a2017-09-29 14:00:48 -0700852__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400853
854/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500855 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500857 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400858 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500859 * If @a thread is not currently sleeping, the routine has no effect.
860 *
861 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400862 *
863 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400864 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865 */
Andrew Boie468190a2017-09-29 14:00:48 -0700866__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400867
868/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500869 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500871 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400872 *
873 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400874 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700875__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400876
877/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500878 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500880 * This routine prevents @a thread from executing if it has not yet started
881 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500883 * @param thread ID of thread to cancel.
884 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700885 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500886 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700887 *
888 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400889 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700890__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400891
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400892/**
Allan Stephensc98da842016-11-11 15:45:03 -0500893 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400894 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500895 * This routine permanently stops execution of @a thread. The thread is taken
896 * off all kernel queues it is part of (i.e. the ready queue, the timeout
897 * queue, or a kernel object wait queue). However, any kernel resources the
898 * thread might currently own (such as mutexes or memory blocks) are not
899 * released. It is the responsibility of the caller of this routine to ensure
900 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500902 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400903 *
904 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400905 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400906 */
Andrew Boie468190a2017-09-29 14:00:48 -0700907__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400908
Andrew Boie7d627c52017-08-30 11:01:56 -0700909
910/**
911 * @brief Start an inactive thread
912 *
913 * If a thread was created with K_FOREVER in the delay parameter, it will
914 * not be added to the scheduling queue until this function is called
915 * on it.
916 *
917 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400918 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700919 */
Andrew Boie468190a2017-09-29 14:00:48 -0700920__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700921
Allan Stephensc98da842016-11-11 15:45:03 -0500922/**
923 * @cond INTERNAL_HIDDEN
924 */
925
Benjamin Walshd211a522016-12-06 11:44:01 -0500926/* timeout has timed out and is not on _timeout_q anymore */
927#define _EXPIRED (-2)
928
929/* timeout is not in use */
930#define _INACTIVE (-1)
931
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400932struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700933 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700934 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400935 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700936 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500937 void *init_p1;
938 void *init_p2;
939 void *init_p3;
940 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500941 u32_t init_options;
942 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500943 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400944};
945
Andrew Boied26cf2d2017-03-30 13:07:02 -0700946#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400947 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500948 prio, options, delay, abort, groups) \
949 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700950 .init_thread = (thread), \
951 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500952 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700953 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400954 .init_p1 = (void *)p1, \
955 .init_p2 = (void *)p2, \
956 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500957 .init_prio = (prio), \
958 .init_options = (options), \
959 .init_delay = (delay), \
960 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400961 }
962
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400963/**
Allan Stephensc98da842016-11-11 15:45:03 -0500964 * INTERNAL_HIDDEN @endcond
965 */
966
967/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500968 * @brief Statically define and initialize a thread.
969 *
970 * The thread may be scheduled for immediate execution or a delayed start.
971 *
972 * Thread options are architecture-specific, and can include K_ESSENTIAL,
973 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
974 * them using "|" (the logical OR operator).
975 *
976 * The ID of the thread can be accessed using:
977 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500978 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500979 *
980 * @param name Name of the thread.
981 * @param stack_size Stack size in bytes.
982 * @param entry Thread entry function.
983 * @param p1 1st entry point parameter.
984 * @param p2 2nd entry point parameter.
985 * @param p3 3rd entry point parameter.
986 * @param prio Thread priority.
987 * @param options Thread options.
988 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400989 *
Anas Nashif47420d02018-05-24 14:20:56 -0400990 * @req K-THREAD-010
991 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400992 * @internal It has been observed that the x86 compiler by default aligns
993 * these _static_thread_data structures to 32-byte boundaries, thereby
994 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400995 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400996 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500997#define K_THREAD_DEFINE(name, stack_size, \
998 entry, p1, p2, p3, \
999 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -07001000 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -07001001 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -05001002 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001003 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -07001004 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
1005 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -05001006 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -07001007 NULL, 0); \
1008 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001009
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001010/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001011 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001013 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001015 * @param thread ID of thread whose priority is needed.
1016 *
1017 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -04001018 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001019 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001020__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001021
1022/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001023 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001025 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001026 *
1027 * Rescheduling can occur immediately depending on the priority @a thread is
1028 * set to:
1029 *
1030 * - If its priority is raised above the priority of the caller of this
1031 * function, and the caller is preemptible, @a thread will be scheduled in.
1032 *
1033 * - If the caller operates on itself, it lowers its priority below that of
1034 * other threads in the system, and the caller is preemptible, the thread of
1035 * highest priority will be scheduled in.
1036 *
1037 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1038 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1039 * highest priority.
1040 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001041 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001042 * @param prio New priority.
1043 *
1044 * @warning Changing the priority of a thread currently involved in mutex
1045 * priority inheritance may result in undefined behavior.
1046 *
1047 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001048 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001049 */
Andrew Boie468190a2017-09-29 14:00:48 -07001050__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001051
Andy Ross4a2e50f2018-05-15 11:06:25 -07001052
1053#ifdef CONFIG_SCHED_DEADLINE
1054/**
1055 * @brief Set deadline expiration time for scheduler
1056 *
1057 * This sets the "deadline" expiration as a time delta from the
1058 * current time, in the same units used by k_cycle_get_32(). The
1059 * scheduler (when deadline scheduling is enabled) will choose the
1060 * next expiring thread when selecting between threads at the same
1061 * static priority. Threads at different priorities will be scheduled
1062 * according to their static priority.
1063 *
1064 * @note Deadlines that are negative (i.e. in the past) are still seen
1065 * as higher priority than others, even if the thread has "finished"
1066 * its work. If you don't want it scheduled anymore, you have to
1067 * reset the deadline into the future, block/pend the thread, or
1068 * modify its priority with k_thread_priority_set().
1069 *
1070 * @note Despite the API naming, the scheduler makes no guarantees the
1071 * the thread WILL be scheduled within that deadline, nor does it take
1072 * extra metadata (like e.g. the "runtime" and "period" parameters in
1073 * Linux sched_setattr()) that allows the kernel to validate the
1074 * scheduling for achievability. Such features could be implemented
1075 * above this call, which is simply input to the priority selection
1076 * logic.
1077 *
1078 * @param thread A thread on which to set the deadline
1079 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001080 *
1081 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001082 */
1083__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1084#endif
1085
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001086/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001087 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001089 * This routine prevents the kernel scheduler from making @a thread the
1090 * current thread. All other internal operations on @a thread are still
1091 * performed; for example, any timeout it is waiting on keeps ticking,
1092 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001094 * If @a thread is already suspended, the routine has no effect.
1095 *
1096 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001097 *
1098 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001099 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001100 */
Andrew Boie468190a2017-09-29 14:00:48 -07001101__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001102
1103/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001104 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001105 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001106 * This routine allows the kernel scheduler to make @a thread the current
1107 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001108 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001109 * If @a thread is not currently suspended, the routine has no effect.
1110 *
1111 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001112 *
1113 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001114 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001115 */
Andrew Boie468190a2017-09-29 14:00:48 -07001116__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001117
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001118/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001119 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001120 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001121 * This routine specifies how the scheduler will perform time slicing of
1122 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001124 * To enable time slicing, @a slice must be non-zero. The scheduler
1125 * ensures that no thread runs for more than the specified time limit
1126 * before other threads of that priority are given a chance to execute.
1127 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001128 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001130 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001131 * execute. Once the scheduler selects a thread for execution, there is no
1132 * minimum guaranteed time the thread will execute before threads of greater or
1133 * equal priority are scheduled.
1134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001135 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001136 * for execution, this routine has no effect; the thread is immediately
1137 * rescheduled after the slice period expires.
1138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001139 * To disable timeslicing, set both @a slice and @a prio to zero.
1140 *
1141 * @param slice Maximum time slice length (in milliseconds).
1142 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001143 *
1144 * @return N/A
1145 */
Kumar Galacc334c72017-04-21 10:55:34 -05001146extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001147
Anas Nashif166f5192018-02-25 08:02:36 -06001148/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001149
1150/**
1151 * @addtogroup isr_apis
1152 * @{
1153 */
1154
1155/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001156 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001157 *
Allan Stephensc98da842016-11-11 15:45:03 -05001158 * This routine allows the caller to customize its actions, depending on
1159 * whether it is a thread or an ISR.
1160 *
1161 * @note Can be called by ISRs.
1162 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001163 * @return 0 if invoked by a thread.
1164 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001165 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001166extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001167
Benjamin Walsh445830d2016-11-10 15:54:27 -05001168/**
1169 * @brief Determine if code is running in a preemptible thread.
1170 *
Allan Stephensc98da842016-11-11 15:45:03 -05001171 * This routine allows the caller to customize its actions, depending on
1172 * whether it can be preempted by another thread. The routine returns a 'true'
1173 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001174 *
Allan Stephensc98da842016-11-11 15:45:03 -05001175 * - The code is running in a thread, not at ISR.
1176 * - The thread's priority is in the preemptible range.
1177 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001178 *
Allan Stephensc98da842016-11-11 15:45:03 -05001179 * @note Can be called by ISRs.
1180 *
1181 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001182 * @return Non-zero if invoked by a preemptible thread.
1183 */
Andrew Boie468190a2017-09-29 14:00:48 -07001184__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001185
Allan Stephensc98da842016-11-11 15:45:03 -05001186/**
Anas Nashif166f5192018-02-25 08:02:36 -06001187 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001188 */
1189
1190/**
1191 * @addtogroup thread_apis
1192 * @{
1193 */
1194
1195/**
1196 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001197 *
Allan Stephensc98da842016-11-11 15:45:03 -05001198 * This routine prevents the current thread from being preempted by another
1199 * thread by instructing the scheduler to treat it as a cooperative thread.
1200 * If the thread subsequently performs an operation that makes it unready,
1201 * it will be context switched out in the normal manner. When the thread
1202 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001203 *
Allan Stephensc98da842016-11-11 15:45:03 -05001204 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001205 *
Allan Stephensc98da842016-11-11 15:45:03 -05001206 * @note k_sched_lock() and k_sched_unlock() should normally be used
1207 * when the operation being performed can be safely interrupted by ISRs.
1208 * However, if the amount of processing involved is very small, better
1209 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001210 *
1211 * @return N/A
1212 */
1213extern void k_sched_lock(void);
1214
Allan Stephensc98da842016-11-11 15:45:03 -05001215/**
1216 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001217 *
Allan Stephensc98da842016-11-11 15:45:03 -05001218 * This routine reverses the effect of a previous call to k_sched_lock().
1219 * A thread must call the routine once for each time it called k_sched_lock()
1220 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001221 *
1222 * @return N/A
1223 */
1224extern void k_sched_unlock(void);
1225
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001226/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001227 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001228 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001229 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001230 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001231 * Custom data is not used by the kernel itself, and is freely available
1232 * for a thread to use as it sees fit. It can be used as a framework
1233 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001235 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001236 *
1237 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001238 *
1239 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001240 */
Andrew Boie468190a2017-09-29 14:00:48 -07001241__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001242
1243/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001244 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001245 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001246 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001247 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001248 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001249 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001250 */
Andrew Boie468190a2017-09-29 14:00:48 -07001251__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001252
1253/**
Anas Nashif166f5192018-02-25 08:02:36 -06001254 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001255 */
1256
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001257#include <sys_clock.h>
1258
Allan Stephensc2f15a42016-11-17 12:24:22 -05001259/**
1260 * @addtogroup clock_apis
1261 * @{
1262 */
1263
1264/**
1265 * @brief Generate null timeout delay.
1266 *
1267 * This macro generates a timeout delay that that instructs a kernel API
1268 * not to wait if the requested operation cannot be performed immediately.
1269 *
1270 * @return Timeout delay value.
1271 */
1272#define K_NO_WAIT 0
1273
1274/**
1275 * @brief Generate timeout delay from milliseconds.
1276 *
1277 * This macro generates a timeout delay that that instructs a kernel API
1278 * to wait up to @a ms milliseconds to perform the requested operation.
1279 *
1280 * @param ms Duration in milliseconds.
1281 *
1282 * @return Timeout delay value.
1283 */
Johan Hedberg14471692016-11-13 10:52:15 +02001284#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001285
1286/**
1287 * @brief Generate timeout delay from seconds.
1288 *
1289 * This macro generates a timeout delay that that instructs a kernel API
1290 * to wait up to @a s seconds to perform the requested operation.
1291 *
1292 * @param s Duration in seconds.
1293 *
1294 * @return Timeout delay value.
1295 */
Johan Hedberg14471692016-11-13 10:52:15 +02001296#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001297
1298/**
1299 * @brief Generate timeout delay from minutes.
1300 *
1301 * This macro generates a timeout delay that that instructs a kernel API
1302 * to wait up to @a m minutes to perform the requested operation.
1303 *
1304 * @param m Duration in minutes.
1305 *
1306 * @return Timeout delay value.
1307 */
Johan Hedberg14471692016-11-13 10:52:15 +02001308#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001309
1310/**
1311 * @brief Generate timeout delay from hours.
1312 *
1313 * This macro generates a timeout delay that that instructs a kernel API
1314 * to wait up to @a h hours to perform the requested operation.
1315 *
1316 * @param h Duration in hours.
1317 *
1318 * @return Timeout delay value.
1319 */
Johan Hedberg14471692016-11-13 10:52:15 +02001320#define K_HOURS(h) K_MINUTES((h) * 60)
1321
Allan Stephensc98da842016-11-11 15:45:03 -05001322/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001323 * @brief Generate infinite timeout delay.
1324 *
1325 * This macro generates a timeout delay that that instructs a kernel API
1326 * to wait as long as necessary to perform the requested operation.
1327 *
1328 * @return Timeout delay value.
1329 */
1330#define K_FOREVER (-1)
1331
1332/**
Anas Nashif166f5192018-02-25 08:02:36 -06001333 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001334 */
1335
1336/**
Allan Stephensc98da842016-11-11 15:45:03 -05001337 * @cond INTERNAL_HIDDEN
1338 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001339
Benjamin Walsh62092182016-12-20 14:39:08 -05001340/* kernel clocks */
1341
1342#if (sys_clock_ticks_per_sec == 1000) || \
1343 (sys_clock_ticks_per_sec == 500) || \
1344 (sys_clock_ticks_per_sec == 250) || \
1345 (sys_clock_ticks_per_sec == 125) || \
1346 (sys_clock_ticks_per_sec == 100) || \
1347 (sys_clock_ticks_per_sec == 50) || \
1348 (sys_clock_ticks_per_sec == 25) || \
1349 (sys_clock_ticks_per_sec == 20) || \
1350 (sys_clock_ticks_per_sec == 10) || \
1351 (sys_clock_ticks_per_sec == 1)
1352
1353 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1354#else
1355 /* yields horrible 64-bit math on many architectures: try to avoid */
1356 #define _NON_OPTIMIZED_TICKS_PER_SEC
1357#endif
1358
1359#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001360extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001361#else
Kumar Galacc334c72017-04-21 10:55:34 -05001362static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001363{
Kumar Galacc334c72017-04-21 10:55:34 -05001364 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001365}
1366#endif
1367
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001368/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001369#ifdef CONFIG_TICKLESS_KERNEL
1370#define _TICK_ALIGN 0
1371#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001372#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001373#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001374
Kumar Galacc334c72017-04-21 10:55:34 -05001375static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001376{
Benjamin Walsh62092182016-12-20 14:39:08 -05001377#ifdef CONFIG_SYS_CLOCK_EXISTS
1378
1379#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001380 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001381#else
Kumar Galacc334c72017-04-21 10:55:34 -05001382 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001383#endif
1384
1385#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001386 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001387 return 0;
1388#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001389}
1390
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001391struct k_timer {
1392 /*
1393 * _timeout structure must be first here if we want to use
1394 * dynamic timer allocation. timeout.node is used in the double-linked
1395 * list of free timers
1396 */
1397 struct _timeout timeout;
1398
Allan Stephens45bfa372016-10-12 12:39:42 -05001399 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001400 _wait_q_t wait_q;
1401
1402 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001403 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001404
1405 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001406 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001407
1408 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001409 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001410
Allan Stephens45bfa372016-10-12 12:39:42 -05001411 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001412 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001413
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001414 /* user-specific data, also used to support legacy features */
1415 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001416
Anas Nashif2f203c22016-12-18 06:57:45 -05001417 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001418};
1419
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001420#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001421 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001422 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001423 .timeout.wait_q = NULL, \
1424 .timeout.thread = NULL, \
1425 .timeout.func = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001426 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001427 .expiry_fn = expiry, \
1428 .stop_fn = stop, \
1429 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001430 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001431 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001432 }
1433
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001434#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1435
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001436/**
Allan Stephensc98da842016-11-11 15:45:03 -05001437 * INTERNAL_HIDDEN @endcond
1438 */
1439
1440/**
1441 * @defgroup timer_apis Timer APIs
1442 * @ingroup kernel_apis
1443 * @{
1444 */
1445
1446/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001447 * @typedef k_timer_expiry_t
1448 * @brief Timer expiry function type.
1449 *
1450 * A timer's expiry function is executed by the system clock interrupt handler
1451 * each time the timer expires. The expiry function is optional, and is only
1452 * invoked if the timer has been initialized with one.
1453 *
1454 * @param timer Address of timer.
1455 *
1456 * @return N/A
1457 */
1458typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1459
1460/**
1461 * @typedef k_timer_stop_t
1462 * @brief Timer stop function type.
1463 *
1464 * A timer's stop function is executed if the timer is stopped prematurely.
1465 * The function runs in the context of the thread that stops the timer.
1466 * The stop function is optional, and is only invoked if the timer has been
1467 * initialized with one.
1468 *
1469 * @param timer Address of timer.
1470 *
1471 * @return N/A
1472 */
1473typedef void (*k_timer_stop_t)(struct k_timer *timer);
1474
1475/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001476 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001478 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001479 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001480 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001481 *
1482 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001483 * @param expiry_fn Function to invoke each time the timer expires.
1484 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001485 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001486#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001487 struct k_timer name \
1488 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001489 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001490
Allan Stephens45bfa372016-10-12 12:39:42 -05001491/**
1492 * @brief Initialize a timer.
1493 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001494 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001495 *
1496 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001497 * @param expiry_fn Function to invoke each time the timer expires.
1498 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001499 *
1500 * @return N/A
1501 */
1502extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001503 k_timer_expiry_t expiry_fn,
1504 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001505
Allan Stephens45bfa372016-10-12 12:39:42 -05001506/**
1507 * @brief Start a timer.
1508 *
1509 * This routine starts a timer, and resets its status to zero. The timer
1510 * begins counting down using the specified duration and period values.
1511 *
1512 * Attempting to start a timer that is already running is permitted.
1513 * The timer's status is reset to zero and the timer begins counting down
1514 * using the new duration and period values.
1515 *
1516 * @param timer Address of timer.
1517 * @param duration Initial timer duration (in milliseconds).
1518 * @param period Timer period (in milliseconds).
1519 *
1520 * @return N/A
1521 */
Andrew Boiea354d492017-09-29 16:22:28 -07001522__syscall void k_timer_start(struct k_timer *timer,
1523 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001524
1525/**
1526 * @brief Stop a timer.
1527 *
1528 * This routine stops a running timer prematurely. The timer's stop function,
1529 * if one exists, is invoked by the caller.
1530 *
1531 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001532 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001533 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001534 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1535 * if @a k_timer_stop is to be called from ISRs.
1536 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001537 * @param timer Address of timer.
1538 *
1539 * @return N/A
1540 */
Andrew Boiea354d492017-09-29 16:22:28 -07001541__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001542
1543/**
1544 * @brief Read timer status.
1545 *
1546 * This routine reads the timer's status, which indicates the number of times
1547 * it has expired since its status was last read.
1548 *
1549 * Calling this routine resets the timer's status to zero.
1550 *
1551 * @param timer Address of timer.
1552 *
1553 * @return Timer status.
1554 */
Andrew Boiea354d492017-09-29 16:22:28 -07001555__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001556
1557/**
1558 * @brief Synchronize thread to timer expiration.
1559 *
1560 * This routine blocks the calling thread until the timer's status is non-zero
1561 * (indicating that it has expired at least once since it was last examined)
1562 * or the timer is stopped. If the timer status is already non-zero,
1563 * or the timer is already stopped, the caller continues without waiting.
1564 *
1565 * Calling this routine resets the timer's status to zero.
1566 *
1567 * This routine must not be used by interrupt handlers, since they are not
1568 * allowed to block.
1569 *
1570 * @param timer Address of timer.
1571 *
1572 * @return Timer status.
1573 */
Andrew Boiea354d492017-09-29 16:22:28 -07001574__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001575
1576/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001577 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001578 *
1579 * This routine computes the (approximate) time remaining before a running
1580 * timer next expires. If the timer is not running, it returns zero.
1581 *
1582 * @param timer Address of timer.
1583 *
1584 * @return Remaining time (in milliseconds).
1585 */
Andrew Boiea354d492017-09-29 16:22:28 -07001586__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1587
1588static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001589{
1590 return _timeout_remaining_get(&timer->timeout);
1591}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001592
Allan Stephensc98da842016-11-11 15:45:03 -05001593/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001594 * @brief Associate user-specific data with a timer.
1595 *
1596 * This routine records the @a user_data with the @a timer, to be retrieved
1597 * later.
1598 *
1599 * It can be used e.g. in a timer handler shared across multiple subsystems to
1600 * retrieve data specific to the subsystem this timer is associated with.
1601 *
1602 * @param timer Address of timer.
1603 * @param user_data User data to associate with the timer.
1604 *
1605 * @return N/A
1606 */
Andrew Boiea354d492017-09-29 16:22:28 -07001607__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1608
Anas Nashif954d5502018-02-25 08:37:28 -06001609/**
1610 * @internal
1611 */
Andrew Boiea354d492017-09-29 16:22:28 -07001612static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1613 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001614{
1615 timer->user_data = user_data;
1616}
1617
1618/**
1619 * @brief Retrieve the user-specific data from a timer.
1620 *
1621 * @param timer Address of timer.
1622 *
1623 * @return The user data.
1624 */
Andrew Boiea354d492017-09-29 16:22:28 -07001625__syscall void *k_timer_user_data_get(struct k_timer *timer);
1626
1627static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001628{
1629 return timer->user_data;
1630}
1631
Anas Nashif166f5192018-02-25 08:02:36 -06001632/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001633
Allan Stephensc98da842016-11-11 15:45:03 -05001634/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001635 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001636 * @{
1637 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001638
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001639/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001640 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001641 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001642 * This routine returns the elapsed time since the system booted,
1643 * in milliseconds.
1644 *
1645 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001646 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001647__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001648
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001649/**
1650 * @brief Enable clock always on in tickless kernel
1651 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001652 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001653 * there are no timer events programmed in tickless kernel
1654 * scheduling. This is necessary if the clock is used to track
1655 * passage of time.
1656 *
1657 * @retval prev_status Previous status of always on flag
1658 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301659#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001660static inline int k_enable_sys_clock_always_on(void)
1661{
1662 int prev_status = _sys_clock_always_on;
1663
1664 _sys_clock_always_on = 1;
1665 _enable_sys_clock();
1666
1667 return prev_status;
1668}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301669#else
1670#define k_enable_sys_clock_always_on() do { } while ((0))
1671#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001672
1673/**
1674 * @brief Disable clock always on in tickless kernel
1675 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001676 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001677 * there are no timer events programmed in tickless kernel
1678 * scheduling. To save power, this routine should be called
1679 * immediately when clock is not used to track time.
1680 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301681#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001682static inline void k_disable_sys_clock_always_on(void)
1683{
1684 _sys_clock_always_on = 0;
1685}
1686#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001687#define k_disable_sys_clock_always_on() do { } while ((0))
1688#endif
1689
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001690/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001691 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001692 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001693 * This routine returns the lower 32-bits of the elapsed time since the system
1694 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001695 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001696 * This routine can be more efficient than k_uptime_get(), as it reduces the
1697 * need for interrupt locking and 64-bit math. However, the 32-bit result
1698 * cannot hold a system uptime time larger than approximately 50 days, so the
1699 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001701 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001702 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001703__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001704
1705/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001706 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001708 * This routine computes the elapsed time between the current system uptime
1709 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001710 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001711 * @param reftime Pointer to a reference time, which is updated to the current
1712 * uptime upon return.
1713 *
1714 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001715 */
Kumar Galacc334c72017-04-21 10:55:34 -05001716extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001717
1718/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001719 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001720 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001721 * This routine computes the elapsed time between the current system uptime
1722 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001723 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001724 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1725 * need for interrupt locking and 64-bit math. However, the 32-bit result
1726 * cannot hold an elapsed time larger than approximately 50 days, so the
1727 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001728 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001729 * @param reftime Pointer to a reference time, which is updated to the current
1730 * uptime upon return.
1731 *
1732 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001733 */
Kumar Galacc334c72017-04-21 10:55:34 -05001734extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001735
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001736/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001737 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001738 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001739 * This routine returns the current time, as measured by the system's hardware
1740 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001742 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001743 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001744#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001745
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001746/**
Anas Nashif166f5192018-02-25 08:02:36 -06001747 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001748 */
1749
Allan Stephensc98da842016-11-11 15:45:03 -05001750/**
1751 * @cond INTERNAL_HIDDEN
1752 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001753
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001754struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001755 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001756 union {
1757 _wait_q_t wait_q;
1758
1759 _POLL_EVENT;
1760 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001761
1762 _OBJECT_TRACING_NEXT_PTR(k_queue);
1763};
1764
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001765#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001766 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001767 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001768 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001769 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001770 _OBJECT_TRACING_INIT \
1771 }
1772
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001773#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1774
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001775extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1776
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001777/**
1778 * INTERNAL_HIDDEN @endcond
1779 */
1780
1781/**
1782 * @defgroup queue_apis Queue APIs
1783 * @ingroup kernel_apis
1784 * @{
1785 */
1786
1787/**
1788 * @brief Initialize a queue.
1789 *
1790 * This routine initializes a queue object, prior to its first use.
1791 *
1792 * @param queue Address of the queue.
1793 *
1794 * @return N/A
1795 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001796__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001797
1798/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001799 * @brief Cancel waiting on a queue.
1800 *
1801 * This routine causes first thread pending on @a queue, if any, to
1802 * return from k_queue_get() call with NULL value (as if timeout expired).
1803 *
1804 * @note Can be called by ISRs.
1805 *
1806 * @param queue Address of the queue.
1807 *
1808 * @return N/A
1809 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001810__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001811
1812/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001813 * @brief Append an element to the end of a queue.
1814 *
1815 * This routine appends a data item to @a queue. A queue data item must be
1816 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1817 * reserved for the kernel's use.
1818 *
1819 * @note Can be called by ISRs.
1820 *
1821 * @param queue Address of the queue.
1822 * @param data Address of the data item.
1823 *
1824 * @return N/A
1825 */
1826extern void k_queue_append(struct k_queue *queue, void *data);
1827
1828/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001829 * @brief Append an element to a queue.
1830 *
1831 * This routine appends a data item to @a queue. There is an implicit
1832 * memory allocation from the calling thread's resource pool, which is
1833 * automatically freed when the item is removed from the queue.
1834 *
1835 * @note Can be called by ISRs.
1836 *
1837 * @param queue Address of the queue.
1838 * @param data Address of the data item.
1839 *
1840 * @retval 0 on success
1841 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1842 */
1843__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1844
1845/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001846 * @brief Prepend an element to a queue.
1847 *
1848 * This routine prepends a data item to @a queue. A queue data item must be
1849 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1850 * reserved for the kernel's use.
1851 *
1852 * @note Can be called by ISRs.
1853 *
1854 * @param queue Address of the queue.
1855 * @param data Address of the data item.
1856 *
1857 * @return N/A
1858 */
1859extern void k_queue_prepend(struct k_queue *queue, void *data);
1860
1861/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001862 * @brief Prepend an element to a queue.
1863 *
1864 * This routine prepends a data item to @a queue. There is an implicit
1865 * memory allocation from the calling thread's resource pool, which is
1866 * automatically freed when the item is removed from the queue.
1867 *
1868 * @note Can be called by ISRs.
1869 *
1870 * @param queue Address of the queue.
1871 * @param data Address of the data item.
1872 *
1873 * @retval 0 on success
1874 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1875 */
1876__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1877
1878/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001879 * @brief Inserts an element to a queue.
1880 *
1881 * This routine inserts a data item to @a queue after previous item. A queue
1882 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1883 * item are reserved for the kernel's use.
1884 *
1885 * @note Can be called by ISRs.
1886 *
1887 * @param queue Address of the queue.
1888 * @param prev Address of the previous data item.
1889 * @param data Address of the data item.
1890 *
1891 * @return N/A
1892 */
1893extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1894
1895/**
1896 * @brief Atomically append a list of elements to a queue.
1897 *
1898 * This routine adds a list of data items to @a queue in one operation.
1899 * The data items must be in a singly-linked list, with the first 32 bits
1900 * in each data item pointing to the next data item; the list must be
1901 * NULL-terminated.
1902 *
1903 * @note Can be called by ISRs.
1904 *
1905 * @param queue Address of the queue.
1906 * @param head Pointer to first node in singly-linked list.
1907 * @param tail Pointer to last node in singly-linked list.
1908 *
1909 * @return N/A
1910 */
1911extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1912
1913/**
1914 * @brief Atomically add a list of elements to a queue.
1915 *
1916 * This routine adds a list of data items to @a queue in one operation.
1917 * The data items must be in a singly-linked list implemented using a
1918 * sys_slist_t object. Upon completion, the original list is empty.
1919 *
1920 * @note Can be called by ISRs.
1921 *
1922 * @param queue Address of the queue.
1923 * @param list Pointer to sys_slist_t object.
1924 *
1925 * @return N/A
1926 */
1927extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1928
1929/**
1930 * @brief Get an element from a queue.
1931 *
1932 * This routine removes first data item from @a queue. The first 32 bits of the
1933 * data item are reserved for the kernel's use.
1934 *
1935 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1936 *
1937 * @param queue Address of the queue.
1938 * @param timeout Waiting period to obtain a data item (in milliseconds),
1939 * or one of the special values K_NO_WAIT and K_FOREVER.
1940 *
1941 * @return Address of the data item if successful; NULL if returned
1942 * without waiting, or waiting period timed out.
1943 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001944__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001945
1946/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001947 * @brief Remove an element from a queue.
1948 *
1949 * This routine removes data item from @a queue. The first 32 bits of the
1950 * data item are reserved for the kernel's use. Removing elements from k_queue
1951 * rely on sys_slist_find_and_remove which is not a constant time operation.
1952 *
1953 * @note Can be called by ISRs
1954 *
1955 * @param queue Address of the queue.
1956 * @param data Address of the data item.
1957 *
1958 * @return true if data item was removed
1959 */
1960static inline bool k_queue_remove(struct k_queue *queue, void *data)
1961{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001962 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001963}
1964
1965/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001966 * @brief Query a queue to see if it has data available.
1967 *
1968 * Note that the data might be already gone by the time this function returns
1969 * if other threads are also trying to read from the queue.
1970 *
1971 * @note Can be called by ISRs.
1972 *
1973 * @param queue Address of the queue.
1974 *
1975 * @return Non-zero if the queue is empty.
1976 * @return 0 if data is available.
1977 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001978__syscall int k_queue_is_empty(struct k_queue *queue);
1979
1980static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001981{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001982 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001983}
1984
1985/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001986 * @brief Peek element at the head of queue.
1987 *
1988 * Return element from the head of queue without removing it.
1989 *
1990 * @param queue Address of the queue.
1991 *
1992 * @return Head element, or NULL if queue is empty.
1993 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001994__syscall void *k_queue_peek_head(struct k_queue *queue);
1995
1996static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001997{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001998 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001999}
2000
2001/**
2002 * @brief Peek element at the tail of queue.
2003 *
2004 * Return element from the tail of queue without removing it.
2005 *
2006 * @param queue Address of the queue.
2007 *
2008 * @return Tail element, or NULL if queue is empty.
2009 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002010__syscall void *k_queue_peek_tail(struct k_queue *queue);
2011
2012static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002013{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002014 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002015}
2016
2017/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002018 * @brief Statically define and initialize a queue.
2019 *
2020 * The queue can be accessed outside the module where it is defined using:
2021 *
2022 * @code extern struct k_queue <name>; @endcode
2023 *
2024 * @param name Name of the queue.
2025 */
2026#define K_QUEUE_DEFINE(name) \
2027 struct k_queue name \
2028 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002029 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002030
Anas Nashif166f5192018-02-25 08:02:36 -06002031/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002032
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002033struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002034 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002035};
2036
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002037/**
2038 * @cond INTERNAL_HIDDEN
2039 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002040#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002041 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002042 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002043 }
2044
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002045#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
2046
Allan Stephensc98da842016-11-11 15:45:03 -05002047/**
2048 * INTERNAL_HIDDEN @endcond
2049 */
2050
2051/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002052 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002053 * @ingroup kernel_apis
2054 * @{
2055 */
2056
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002057/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002058 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002059 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002060 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002061 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002062 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002063 *
2064 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002065 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002066 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002067#define k_fifo_init(fifo) \
2068 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002069
2070/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002071 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002072 *
2073 * This routine causes first thread pending on @a fifo, if any, to
2074 * return from k_fifo_get() call with NULL value (as if timeout
2075 * expired).
2076 *
2077 * @note Can be called by ISRs.
2078 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002079 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002080 *
2081 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002082 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002083 */
2084#define k_fifo_cancel_wait(fifo) \
2085 k_queue_cancel_wait((struct k_queue *) fifo)
2086
2087/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002088 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002089 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002090 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002091 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2092 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002094 * @note Can be called by ISRs.
2095 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002096 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002097 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002098 *
2099 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002100 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002101 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002102#define k_fifo_put(fifo, data) \
2103 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002104
2105/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002106 * @brief Add an element to a FIFO queue.
2107 *
2108 * This routine adds a data item to @a fifo. There is an implicit
2109 * memory allocation from the calling thread's resource pool, which is
2110 * automatically freed when the item is removed.
2111 *
2112 * @note Can be called by ISRs.
2113 *
2114 * @param fifo Address of the FIFO.
2115 * @param data Address of the data item.
2116 *
2117 * @retval 0 on success
2118 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002119 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002120 */
2121#define k_fifo_alloc_put(fifo, data) \
2122 k_queue_alloc_append((struct k_queue *) fifo, data)
2123
2124/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002125 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002127 * This routine adds a list of data items to @a fifo in one operation.
2128 * The data items must be in a singly-linked list, with the first 32 bits
2129 * each data item pointing to the next data item; the list must be
2130 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002132 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002133 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002134 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002135 * @param head Pointer to first node in singly-linked list.
2136 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002137 *
2138 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002139 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002140 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002141#define k_fifo_put_list(fifo, head, tail) \
2142 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002143
2144/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002145 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002146 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002147 * This routine adds a list of data items to @a fifo in one operation.
2148 * The data items must be in a singly-linked list implemented using a
2149 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002150 * and must be re-initialized via sys_slist_init().
2151 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002152 * @note Can be called by ISRs.
2153 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002154 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002155 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002156 *
2157 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002158 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002159 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002160#define k_fifo_put_slist(fifo, list) \
2161 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002162
2163/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002164 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002165 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002166 * This routine removes a data item from @a fifo in a "first in, first out"
2167 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002168 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002169 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2170 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002171 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002172 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002173 * or one of the special values K_NO_WAIT and K_FOREVER.
2174 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002175 * @return Address of the data item if successful; NULL if returned
2176 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002177 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002178 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002179#define k_fifo_get(fifo, timeout) \
2180 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002181
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002182/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002183 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002184 *
2185 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002186 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002187 *
2188 * @note Can be called by ISRs.
2189 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002190 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002191 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002192 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002193 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002194 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002195 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002196#define k_fifo_is_empty(fifo) \
2197 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002198
2199/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002200 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002201 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002202 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302203 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002204 * on each iteration of processing, a head container will be peeked,
2205 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002206 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002207 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002208 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002209 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002210 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002211 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002212 */
2213#define k_fifo_peek_head(fifo) \
2214 k_queue_peek_head((struct k_queue *) fifo)
2215
2216/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002217 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002218 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002219 * Return element from the tail of FIFO queue (without removing it). A usecase
2220 * for this is if elements of the FIFO queue are themselves containers. Then
2221 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002222 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002223 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002224 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002225 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002226 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002227 */
2228#define k_fifo_peek_tail(fifo) \
2229 k_queue_peek_tail((struct k_queue *) fifo)
2230
2231/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002232 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002233 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002234 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002235 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002236 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002237 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002238 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002239 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002240 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002241#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002242 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002243 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002244 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002245
Anas Nashif166f5192018-02-25 08:02:36 -06002246/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002247
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002248struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002249 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002250};
2251
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002252/**
2253 * @cond INTERNAL_HIDDEN
2254 */
2255
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002256#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002257 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002258 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002259 }
2260
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002261#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2262
Allan Stephensc98da842016-11-11 15:45:03 -05002263/**
2264 * INTERNAL_HIDDEN @endcond
2265 */
2266
2267/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002268 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002269 * @ingroup kernel_apis
2270 * @{
2271 */
2272
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002273/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002274 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002275 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002276 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002277 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002278 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002279 *
2280 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002281 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002282 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002283#define k_lifo_init(lifo) \
2284 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002285
2286/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002287 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002288 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002289 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002290 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2291 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002293 * @note Can be called by ISRs.
2294 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002295 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002296 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002297 *
2298 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002299 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002300 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002301#define k_lifo_put(lifo, data) \
2302 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002303
2304/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002305 * @brief Add an element to a LIFO queue.
2306 *
2307 * This routine adds a data item to @a lifo. There is an implicit
2308 * memory allocation from the calling thread's resource pool, which is
2309 * automatically freed when the item is removed.
2310 *
2311 * @note Can be called by ISRs.
2312 *
2313 * @param lifo Address of the LIFO.
2314 * @param data Address of the data item.
2315 *
2316 * @retval 0 on success
2317 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002318 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002319 */
2320#define k_lifo_alloc_put(lifo, data) \
2321 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2322
2323/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002324 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002325 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002326 * This routine removes a data item from @a lifo in a "last in, first out"
2327 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002328 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002329 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2330 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002331 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002332 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002333 * or one of the special values K_NO_WAIT and K_FOREVER.
2334 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002335 * @return Address of the data item if successful; NULL if returned
2336 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002337 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002338 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002339#define k_lifo_get(lifo, timeout) \
2340 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002341
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002342/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002343 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002344 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002345 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002346 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002347 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002349 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002350 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002351 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002352#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002353 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002354 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002355 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002356
Anas Nashif166f5192018-02-25 08:02:36 -06002357/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002358
2359/**
2360 * @cond INTERNAL_HIDDEN
2361 */
Andrew Boief3bee952018-05-02 17:44:39 -07002362#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002363
2364struct k_stack {
2365 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002366 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002367
Anas Nashif2f203c22016-12-18 06:57:45 -05002368 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002369 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002370};
2371
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002372#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002373 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002374 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002375 .base = stack_buffer, \
2376 .next = stack_buffer, \
2377 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002378 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002379 }
2380
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002381#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2382
Allan Stephensc98da842016-11-11 15:45:03 -05002383/**
2384 * INTERNAL_HIDDEN @endcond
2385 */
2386
2387/**
2388 * @defgroup stack_apis Stack APIs
2389 * @ingroup kernel_apis
2390 * @{
2391 */
2392
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002393/**
2394 * @brief Initialize a stack.
2395 *
2396 * This routine initializes a stack object, prior to its first use.
2397 *
2398 * @param stack Address of the stack.
2399 * @param buffer Address of array used to hold stacked values.
2400 * @param num_entries Maximum number of values that can be stacked.
2401 *
2402 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002403 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002404 */
Andrew Boief3bee952018-05-02 17:44:39 -07002405void k_stack_init(struct k_stack *stack,
2406 u32_t *buffer, unsigned int num_entries);
2407
2408
2409/**
2410 * @brief Initialize a stack.
2411 *
2412 * This routine initializes a stack object, prior to its first use. Internal
2413 * buffers will be allocated from the calling thread's resource pool.
2414 * This memory will be released if k_stack_cleanup() is called, or
2415 * userspace is enabled and the stack object loses all references to it.
2416 *
2417 * @param stack Address of the stack.
2418 * @param num_entries Maximum number of values that can be stacked.
2419 *
2420 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002421 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002422 */
2423
2424__syscall int k_stack_alloc_init(struct k_stack *stack,
2425 unsigned int num_entries);
2426
2427/**
2428 * @brief Release a stack's allocated buffer
2429 *
2430 * If a stack object was given a dynamically allocated buffer via
2431 * k_stack_alloc_init(), this will free it. This function does nothing
2432 * if the buffer wasn't dynamically allocated.
2433 *
2434 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002435 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002436 */
2437void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002438
2439/**
2440 * @brief Push an element onto a stack.
2441 *
2442 * This routine adds a 32-bit value @a data to @a stack.
2443 *
2444 * @note Can be called by ISRs.
2445 *
2446 * @param stack Address of the stack.
2447 * @param data Value to push onto the stack.
2448 *
2449 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002450 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002451 */
Andrew Boiee8734462017-09-29 16:42:07 -07002452__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002453
2454/**
2455 * @brief Pop an element from a stack.
2456 *
2457 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2458 * manner and stores the value in @a data.
2459 *
2460 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2461 *
2462 * @param stack Address of the stack.
2463 * @param data Address of area to hold the value popped from the stack.
2464 * @param timeout Waiting period to obtain a value (in milliseconds),
2465 * or one of the special values K_NO_WAIT and K_FOREVER.
2466 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002467 * @retval 0 Element popped from stack.
2468 * @retval -EBUSY Returned without waiting.
2469 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002470 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002471 */
Andrew Boiee8734462017-09-29 16:42:07 -07002472__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002473
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002474/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002475 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002476 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002477 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002478 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002479 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002480 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002481 * @param name Name of the stack.
2482 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002483 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002484 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002485#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002486 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002487 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002488 struct k_stack name \
2489 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002490 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002491 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002492
Anas Nashif166f5192018-02-25 08:02:36 -06002493/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002494
Allan Stephens6bba9b02016-11-16 14:56:54 -05002495struct k_work;
2496
Allan Stephensc98da842016-11-11 15:45:03 -05002497/**
2498 * @defgroup workqueue_apis Workqueue Thread APIs
2499 * @ingroup kernel_apis
2500 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002501 */
2502
Allan Stephens6bba9b02016-11-16 14:56:54 -05002503/**
2504 * @typedef k_work_handler_t
2505 * @brief Work item handler function type.
2506 *
2507 * A work item's handler function is executed by a workqueue's thread
2508 * when the work item is processed by the workqueue.
2509 *
2510 * @param work Address of the work item.
2511 *
2512 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002513 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002514 */
2515typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002516
2517/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002518 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002519 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002520
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002521struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002522 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002523 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002524};
2525
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002526enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002527 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002528};
2529
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002530struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002531 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002532 k_work_handler_t handler;
2533 atomic_t flags[1];
2534};
2535
Allan Stephens6bba9b02016-11-16 14:56:54 -05002536struct k_delayed_work {
2537 struct k_work work;
2538 struct _timeout timeout;
2539 struct k_work_q *work_q;
2540};
2541
2542extern struct k_work_q k_sys_work_q;
2543
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002544/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002545 * INTERNAL_HIDDEN @endcond
2546 */
2547
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002548#define _K_WORK_INITIALIZER(work_handler) \
2549 { \
2550 ._reserved = NULL, \
2551 .handler = work_handler, \
2552 .flags = { 0 } \
2553 }
2554
2555#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2556
Allan Stephens6bba9b02016-11-16 14:56:54 -05002557/**
2558 * @brief Initialize a statically-defined work item.
2559 *
2560 * This macro can be used to initialize a statically-defined workqueue work
2561 * item, prior to its first use. For example,
2562 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002563 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002564 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002565 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002566 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002567 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002568 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002569#define K_WORK_DEFINE(work, work_handler) \
2570 struct k_work work \
2571 __in_section(_k_work, static, work) = \
2572 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002573
2574/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002575 * @brief Initialize a work item.
2576 *
2577 * This routine initializes a workqueue work item, prior to its first use.
2578 *
2579 * @param work Address of work item.
2580 * @param handler Function to invoke each time work item is processed.
2581 *
2582 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002583 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002584 */
2585static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2586{
Leandro Pereira0e23ad82018-05-29 10:42:17 -07002587 *work = (struct k_work)_K_WORK_INITIALIZER(handler);
Andrew Boie945af952017-08-22 13:15:23 -07002588 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002589}
2590
2591/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002592 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002593 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002594 * This routine submits work item @a work to be processed by workqueue
2595 * @a work_q. If the work item is already pending in the workqueue's queue
2596 * as a result of an earlier submission, this routine has no effect on the
2597 * work item. If the work item has already been processed, or is currently
2598 * being processed, its work is considered complete and the work item can be
2599 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002600 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002601 * @warning
2602 * A submitted work item must not be modified until it has been processed
2603 * by the workqueue.
2604 *
2605 * @note Can be called by ISRs.
2606 *
2607 * @param work_q Address of workqueue.
2608 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002609 *
2610 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002611 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002612 */
2613static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2614 struct k_work *work)
2615{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002616 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002617 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002618 }
2619}
2620
2621/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002622 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002623 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002624 * This routine indicates if work item @a work is pending in a workqueue's
2625 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002626 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002627 * @note Can be called by ISRs.
2628 *
2629 * @param work Address of work item.
2630 *
2631 * @return 1 if work item is pending, or 0 if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002632 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002633 */
2634static inline int k_work_pending(struct k_work *work)
2635{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002636 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002637}
2638
2639/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002640 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002641 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002642 * This routine starts workqueue @a work_q. The workqueue spawns its work
2643 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002644 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002645 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002646 * @param stack Pointer to work queue thread's stack space, as defined by
2647 * K_THREAD_STACK_DEFINE()
2648 * @param stack_size Size of the work queue thread's stack (in bytes), which
2649 * should either be the same constant passed to
2650 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002651 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002652 *
2653 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002654 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002655 */
Andrew Boie507852a2017-07-25 18:47:07 -07002656extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002657 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002658 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002659
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002660/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002661 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002662 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002663 * This routine initializes a workqueue delayed work item, prior to
2664 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002665 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002666 * @param work Address of delayed work item.
2667 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002668 *
2669 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002670 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002671 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002672extern void k_delayed_work_init(struct k_delayed_work *work,
2673 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002674
2675/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002676 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002677 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002678 * This routine schedules work item @a work to be processed by workqueue
2679 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002680 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002681 * Only when the countdown completes is the work item actually submitted to
2682 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002683 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002684 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002685 * counting down cancels the existing submission and restarts the
2686 * countdown using the new delay. Note that this behavior is
2687 * inherently subject to race conditions with the pre-existing
2688 * timeouts and work queue, so care must be taken to synchronize such
2689 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002690 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002691 * @warning
2692 * A delayed work item must not be modified until it has been processed
2693 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002694 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002695 * @note Can be called by ISRs.
2696 *
2697 * @param work_q Address of workqueue.
2698 * @param work Address of delayed work item.
2699 * @param delay Delay before submitting the work item (in milliseconds).
2700 *
2701 * @retval 0 Work item countdown started.
2702 * @retval -EINPROGRESS Work item is already pending.
2703 * @retval -EINVAL Work item is being processed or has completed its work.
2704 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002705 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002706 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002707extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2708 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002709 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002710
2711/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002712 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002713 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002714 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002715 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002716 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002717 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002718 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002719 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002720 * @param work Address of delayed work item.
2721 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002722 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002723 * @retval -EINPROGRESS Work item is already pending.
2724 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002725 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002726 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002727extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002728
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002729/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002730 * @brief Submit a work item to the system workqueue.
2731 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002732 * This routine submits work item @a work to be processed by the system
2733 * workqueue. If the work item is already pending in the workqueue's queue
2734 * as a result of an earlier submission, this routine has no effect on the
2735 * work item. If the work item has already been processed, or is currently
2736 * being processed, its work is considered complete and the work item can be
2737 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002738 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002739 * @warning
2740 * Work items submitted to the system workqueue should avoid using handlers
2741 * that block or yield since this may prevent the system workqueue from
2742 * processing other work items in a timely manner.
2743 *
2744 * @note Can be called by ISRs.
2745 *
2746 * @param work Address of work item.
2747 *
2748 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002749 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002750 */
2751static inline void k_work_submit(struct k_work *work)
2752{
2753 k_work_submit_to_queue(&k_sys_work_q, work);
2754}
2755
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002756/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002757 * @brief Submit a delayed work item to the system workqueue.
2758 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002759 * This routine schedules work item @a work to be processed by the system
2760 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002761 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002762 * Only when the countdown completes is the work item actually submitted to
2763 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002764 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002765 * Submitting a previously submitted delayed work item that is still
2766 * counting down cancels the existing submission and restarts the countdown
2767 * using the new delay. If the work item is currently pending on the
2768 * workqueue's queue because the countdown has completed it is too late to
2769 * resubmit the item, and resubmission fails without impacting the work item.
2770 * If the work item has already been processed, or is currently being processed,
2771 * its work is considered complete and the work item can be resubmitted.
2772 *
2773 * @warning
2774 * Work items submitted to the system workqueue should avoid using handlers
2775 * that block or yield since this may prevent the system workqueue from
2776 * processing other work items in a timely manner.
2777 *
2778 * @note Can be called by ISRs.
2779 *
2780 * @param work Address of delayed work item.
2781 * @param delay Delay before submitting the work item (in milliseconds).
2782 *
2783 * @retval 0 Work item countdown started.
2784 * @retval -EINPROGRESS Work item is already pending.
2785 * @retval -EINVAL Work item is being processed or has completed its work.
2786 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002787 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002788 */
2789static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002790 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002791{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002792 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002793}
2794
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002795/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002796 * @brief Get time remaining before a delayed work gets scheduled.
2797 *
2798 * This routine computes the (approximate) time remaining before a
2799 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002800 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002801 *
2802 * @param work Delayed work item.
2803 *
2804 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002805 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002806 */
Kumar Galacc334c72017-04-21 10:55:34 -05002807static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002808{
2809 return _timeout_remaining_get(&work->timeout);
2810}
2811
Anas Nashif166f5192018-02-25 08:02:36 -06002812/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002813/**
Anas Nashifce78d162018-05-24 12:43:11 -05002814 * @defgroup mutex_apis Mutex APIs
2815 * @ingroup kernel_apis
2816 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002817 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002818
Anas Nashifce78d162018-05-24 12:43:11 -05002819/**
2820 * Mutex Structure
2821 * @ingroup mutex_apis
2822 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002823struct k_mutex {
2824 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002825 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002826 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002827 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002828 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002829
Anas Nashif2f203c22016-12-18 06:57:45 -05002830 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002831};
2832
Anas Nashifce78d162018-05-24 12:43:11 -05002833/**
2834 * @cond INTERNAL_HIDDEN
2835 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002836#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002837 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002838 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002839 .owner = NULL, \
2840 .lock_count = 0, \
2841 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002842 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002843 }
2844
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002845#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2846
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002847/**
Allan Stephensc98da842016-11-11 15:45:03 -05002848 * INTERNAL_HIDDEN @endcond
2849 */
2850
2851/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002852 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002854 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002855 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002856 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002858 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002859 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002860 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002861#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002862 struct k_mutex name \
2863 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002864 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002865
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002866/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002867 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002869 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002871 * Upon completion, the mutex is available and does not have an owner.
2872 *
2873 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002874 *
2875 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002876 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002877 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002878__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002879
2880/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002881 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002883 * This routine locks @a mutex. If the mutex is locked by another thread,
2884 * the calling thread waits until the mutex becomes available or until
2885 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002887 * A thread is permitted to lock a mutex it has already locked. The operation
2888 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002889 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002890 * @param mutex Address of the mutex.
2891 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002892 * or one of the special values K_NO_WAIT and K_FOREVER.
2893 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002894 * @retval 0 Mutex locked.
2895 * @retval -EBUSY Returned without waiting.
2896 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002897 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002898 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002899__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002900
2901/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002902 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002903 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002904 * This routine unlocks @a mutex. The mutex must already be locked by the
2905 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002906 *
2907 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002908 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002909 * thread.
2910 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002911 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002912 *
2913 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002914 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002915 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002916__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002917
Allan Stephensc98da842016-11-11 15:45:03 -05002918/**
Anas Nashif166f5192018-02-25 08:02:36 -06002919 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002920 */
2921
2922/**
2923 * @cond INTERNAL_HIDDEN
2924 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002925
2926struct k_sem {
2927 _wait_q_t wait_q;
2928 unsigned int count;
2929 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002930 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002931
Anas Nashif2f203c22016-12-18 06:57:45 -05002932 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002933};
2934
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002935#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002936 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002937 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002938 .count = initial_count, \
2939 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002940 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002941 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002942 }
2943
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002944#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2945
Allan Stephensc98da842016-11-11 15:45:03 -05002946/**
2947 * INTERNAL_HIDDEN @endcond
2948 */
2949
2950/**
2951 * @defgroup semaphore_apis Semaphore APIs
2952 * @ingroup kernel_apis
2953 * @{
2954 */
2955
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002956/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002957 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002958 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002959 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002960 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002961 * @param sem Address of the semaphore.
2962 * @param initial_count Initial semaphore count.
2963 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002964 *
2965 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002966 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002967 */
Andrew Boie99280232017-09-29 14:17:47 -07002968__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2969 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002970
2971/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002972 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002974 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002975 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002976 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2977 *
2978 * @param sem Address of the semaphore.
2979 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002980 * or one of the special values K_NO_WAIT and K_FOREVER.
2981 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002982 * @note When porting code from the nanokernel legacy API to the new API, be
2983 * careful with the return value of this function. The return value is the
2984 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2985 * non-zero means failure, while the nano_sem_take family returns 1 for success
2986 * and 0 for failure.
2987 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002988 * @retval 0 Semaphore taken.
2989 * @retval -EBUSY Returned without waiting.
2990 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002991 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002992 */
Andrew Boie99280232017-09-29 14:17:47 -07002993__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002994
2995/**
2996 * @brief Give a semaphore.
2997 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002998 * This routine gives @a sem, unless the semaphore is already at its maximum
2999 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003001 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003002 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003003 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003004 *
3005 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003006 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003007 */
Andrew Boie99280232017-09-29 14:17:47 -07003008__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003009
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003010/**
3011 * @brief Reset a semaphore's count to zero.
3012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003013 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003015 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003016 *
3017 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003018 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003019 */
Andrew Boie990bf162017-10-03 12:36:49 -07003020__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003021
Anas Nashif954d5502018-02-25 08:37:28 -06003022/**
3023 * @internal
3024 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003025static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003026{
3027 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003028}
3029
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003030/**
3031 * @brief Get a semaphore's count.
3032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003033 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003035 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003037 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003038 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003039 */
Andrew Boie990bf162017-10-03 12:36:49 -07003040__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003041
Anas Nashif954d5502018-02-25 08:37:28 -06003042/**
3043 * @internal
3044 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003045static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003046{
3047 return sem->count;
3048}
3049
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003050/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003051 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003052 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003053 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003054 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003055 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003056 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003057 * @param name Name of the semaphore.
3058 * @param initial_count Initial semaphore count.
3059 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003060 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003061 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003062#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003063 struct k_sem name \
3064 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003065 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303066 BUILD_ASSERT(((count_limit) != 0) && \
3067 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003068
Anas Nashif166f5192018-02-25 08:02:36 -06003069/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003070
3071/**
3072 * @defgroup alert_apis Alert APIs
3073 * @ingroup kernel_apis
3074 * @{
3075 */
3076
Allan Stephens5eceb852016-11-16 10:16:30 -05003077/**
3078 * @typedef k_alert_handler_t
3079 * @brief Alert handler function type.
3080 *
3081 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07003082 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05003083 * and is only invoked if the alert has been initialized with one.
3084 *
3085 * @param alert Address of the alert.
3086 *
3087 * @return 0 if alert has been consumed; non-zero if alert should pend.
3088 */
3089typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05003090
Anas Nashif166f5192018-02-25 08:02:36 -06003091/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003092
3093/**
3094 * @cond INTERNAL_HIDDEN
3095 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003096
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003097#define K_ALERT_DEFAULT NULL
3098#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003099
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003100struct k_alert {
3101 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003102 atomic_t send_count;
3103 struct k_work work_item;
3104 struct k_sem sem;
3105
Anas Nashif2f203c22016-12-18 06:57:45 -05003106 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003107};
3108
Anas Nashif954d5502018-02-25 08:37:28 -06003109/**
3110 * @internal
3111 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003112extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003113
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003114#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003115 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003116 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003117 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003118 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
3119 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003120 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003121 }
3122
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003123#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
3124
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003125/**
Allan Stephensc98da842016-11-11 15:45:03 -05003126 * INTERNAL_HIDDEN @endcond
3127 */
3128
3129/**
3130 * @addtogroup alert_apis
3131 * @{
3132 */
3133
3134/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003135 * @def K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts)
3136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003137 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003138 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003139 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003140 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003141 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003143 * @param name Name of the alert.
3144 * @param alert_handler Action to take when alert is sent. Specify either
3145 * the address of a function to be invoked by the system workqueue
3146 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
3147 * K_ALERT_DEFAULT (which causes the alert to pend).
3148 * @param max_num_pending_alerts Maximum number of pending alerts.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003149 *
3150 * @req K-ALERT-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003151 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003152#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003153 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003154 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003155 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003156 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003157
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003158/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003159 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003160 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003161 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003162 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003163 * @param alert Address of the alert.
3164 * @param handler Action to take when alert is sent. Specify either the address
3165 * of a function to be invoked by the system workqueue thread,
3166 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3167 * K_ALERT_DEFAULT (which causes the alert to pend).
3168 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003169 *
3170 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003171 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003172 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003173extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3174 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003175
3176/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003177 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003178 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003179 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003181 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3182 *
3183 * @param alert Address of the alert.
3184 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003185 * or one of the special values K_NO_WAIT and K_FOREVER.
3186 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003187 * @retval 0 Alert received.
3188 * @retval -EBUSY Returned without waiting.
3189 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003190 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003191 */
Andrew Boie310e9872017-09-29 04:41:15 -07003192__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003193
3194/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003195 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003197 * This routine signals @a alert. The action specified for @a alert will
3198 * be taken, which may trigger the execution of an alert handler function
3199 * and/or cause the alert to pend (assuming the alert has not reached its
3200 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003201 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003202 * @note Can be called by ISRs.
3203 *
3204 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003205 *
3206 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003207 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003208 */
Andrew Boie310e9872017-09-29 04:41:15 -07003209__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003210
3211/**
Anas Nashif166f5192018-02-25 08:02:36 -06003212 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003213 */
3214
Allan Stephensc98da842016-11-11 15:45:03 -05003215/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003216 * @defgroup msgq_apis Message Queue APIs
3217 * @ingroup kernel_apis
3218 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003219 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003220
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003221/**
3222 * @brief Message Queue Structure
3223 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003224struct k_msgq {
3225 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003226 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003227 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003228 char *buffer_start;
3229 char *buffer_end;
3230 char *read_ptr;
3231 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003232 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003233
Anas Nashif2f203c22016-12-18 06:57:45 -05003234 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003235 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003236};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003237/**
3238 * @cond INTERNAL_HIDDEN
3239 */
3240
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003241
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003242#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003243 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003244 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003245 .max_msgs = q_max_msgs, \
3246 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003247 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003248 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003249 .read_ptr = q_buffer, \
3250 .write_ptr = q_buffer, \
3251 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003252 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003253 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003254#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003255/**
3256 * INTERNAL_HIDDEN @endcond
3257 */
3258
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003259
Andrew Boie0fe789f2018-04-12 18:35:56 -07003260#define K_MSGQ_FLAG_ALLOC BIT(0)
3261
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003262/**
3263 * @brief Message Queue Attributes
3264 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303265struct k_msgq_attrs {
3266 size_t msg_size;
3267 u32_t max_msgs;
3268 u32_t used_msgs;
3269};
3270
Allan Stephensc98da842016-11-11 15:45:03 -05003271
3272/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003273 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003274 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003275 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3276 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003277 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3278 * message is similarly aligned to this boundary, @a q_msg_size must also be
3279 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003280 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003281 * The message queue can be accessed outside the module where it is defined
3282 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003283 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003284 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003285 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003286 * @param q_name Name of the message queue.
3287 * @param q_msg_size Message size (in bytes).
3288 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003289 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003290 *
3291 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003292 */
3293#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003294 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003295 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003296 struct k_msgq q_name \
3297 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003298 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003299 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003300
Peter Mitsisd7a37502016-10-13 11:37:40 -04003301/**
3302 * @brief Initialize a message queue.
3303 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003304 * This routine initializes a message queue object, prior to its first use.
3305 *
Allan Stephensda827222016-11-09 14:23:58 -06003306 * The message queue's ring buffer must contain space for @a max_msgs messages,
3307 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3308 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3309 * that each message is similarly aligned to this boundary, @a q_msg_size
3310 * must also be a multiple of N.
3311 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003312 * @param q Address of the message queue.
3313 * @param buffer Pointer to ring buffer that holds queued messages.
3314 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003315 * @param max_msgs Maximum number of messages that can be queued.
3316 *
3317 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003318 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003319 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003320void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3321 u32_t max_msgs);
3322
3323/**
3324 * @brief Initialize a message queue.
3325 *
3326 * This routine initializes a message queue object, prior to its first use,
3327 * allocating its internal ring buffer from the calling thread's resource
3328 * pool.
3329 *
3330 * Memory allocated for the ring buffer can be released by calling
3331 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3332 * all of its references.
3333 *
3334 * @param q Address of the message queue.
3335 * @param msg_size Message size (in bytes).
3336 * @param max_msgs Maximum number of messages that can be queued.
3337 *
3338 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3339 * thread's resource pool, or -EINVAL if the size parameters cause
3340 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003341 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003342 */
3343__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3344 u32_t max_msgs);
3345
3346
3347void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003348
3349/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003350 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003351 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003352 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003353 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003354 * @note Can be called by ISRs.
3355 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003356 * @param q Address of the message queue.
3357 * @param data Pointer to the message.
3358 * @param timeout Waiting period to add the message (in milliseconds),
3359 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003360 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003361 * @retval 0 Message sent.
3362 * @retval -ENOMSG Returned without waiting or queue purged.
3363 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003364 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003365 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003366__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003367
3368/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003369 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003370 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003371 * This routine receives a message from message queue @a q in a "first in,
3372 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003373 *
Allan Stephensc98da842016-11-11 15:45:03 -05003374 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003375 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003376 * @param q Address of the message queue.
3377 * @param data Address of area to hold the received message.
3378 * @param timeout Waiting period to receive the message (in milliseconds),
3379 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003380 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003381 * @retval 0 Message received.
3382 * @retval -ENOMSG Returned without waiting.
3383 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003384 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003385 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003386__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003387
3388/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003389 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003391 * This routine discards all unreceived messages in a message queue's ring
3392 * buffer. Any threads that are blocked waiting to send a message to the
3393 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003394 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003395 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003396 *
3397 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003398 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003399 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003400__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003401
Peter Mitsis67be2492016-10-07 11:44:34 -04003402/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003403 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003404 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003405 * This routine returns the number of unused entries in a message queue's
3406 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003407 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003408 * @param q Address of the message queue.
3409 *
3410 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003411 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003412 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003413__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3414
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303415/**
3416 * @brief Get basic attributes of a message queue.
3417 *
3418 * This routine fetches basic attributes of message queue into attr argument.
3419 *
3420 * @param q Address of the message queue.
3421 * @param attrs pointer to message queue attribute structure.
3422 *
3423 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003424 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303425 */
3426__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3427
3428
Andrew Boie82edb6e2017-10-02 10:53:06 -07003429static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003430{
3431 return q->max_msgs - q->used_msgs;
3432}
3433
Peter Mitsisd7a37502016-10-13 11:37:40 -04003434/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003435 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003436 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003437 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003439 * @param q Address of the message queue.
3440 *
3441 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003442 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003443 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003444__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3445
3446static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003447{
3448 return q->used_msgs;
3449}
3450
Anas Nashif166f5192018-02-25 08:02:36 -06003451/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003452
3453/**
3454 * @defgroup mem_pool_apis Memory Pool APIs
3455 * @ingroup kernel_apis
3456 * @{
3457 */
3458
Andy Ross73cb9582017-05-09 10:42:39 -07003459/* Note on sizing: the use of a 20 bit field for block means that,
3460 * assuming a reasonable minimum block size of 16 bytes, we're limited
3461 * to 16M of memory managed by a single pool. Long term it would be
3462 * good to move to a variable bit size based on configuration.
3463 */
3464struct k_mem_block_id {
3465 u32_t pool : 8;
3466 u32_t level : 4;
3467 u32_t block : 20;
3468};
3469
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003470struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003471 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003472 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003473};
3474
Anas Nashif166f5192018-02-25 08:02:36 -06003475/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003476
3477/**
3478 * @defgroup mailbox_apis Mailbox APIs
3479 * @ingroup kernel_apis
3480 * @{
3481 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003482
3483struct k_mbox_msg {
3484 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003485 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003486 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003487 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003488 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003489 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003490 /** sender's message data buffer */
3491 void *tx_data;
3492 /** internal use only - needed for legacy API support */
3493 void *_rx_data;
3494 /** message data block descriptor */
3495 struct k_mem_block tx_block;
3496 /** source thread id */
3497 k_tid_t rx_source_thread;
3498 /** target thread id */
3499 k_tid_t tx_target_thread;
3500 /** internal use only - thread waiting on send (may be a dummy) */
3501 k_tid_t _syncing_thread;
3502#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3503 /** internal use only - semaphore used during asynchronous send */
3504 struct k_sem *_async_sem;
3505#endif
3506};
3507
3508struct k_mbox {
3509 _wait_q_t tx_msg_queue;
3510 _wait_q_t rx_msg_queue;
3511
Anas Nashif2f203c22016-12-18 06:57:45 -05003512 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003513};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003514/**
3515 * @cond INTERNAL_HIDDEN
3516 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003517
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003518#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003519 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003520 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3521 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003522 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003523 }
3524
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003525#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3526
Peter Mitsis12092702016-10-14 12:57:23 -04003527/**
Allan Stephensc98da842016-11-11 15:45:03 -05003528 * INTERNAL_HIDDEN @endcond
3529 */
3530
3531/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003532 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003533 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003534 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003535 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003536 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003537 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003538 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003539 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003540 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003541#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003542 struct k_mbox name \
3543 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003544 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003545
Peter Mitsis12092702016-10-14 12:57:23 -04003546/**
3547 * @brief Initialize a mailbox.
3548 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003549 * This routine initializes a mailbox object, prior to its first use.
3550 *
3551 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003552 *
3553 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003554 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003555 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003556extern void k_mbox_init(struct k_mbox *mbox);
3557
Peter Mitsis12092702016-10-14 12:57:23 -04003558/**
3559 * @brief Send a mailbox message in a synchronous manner.
3560 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003561 * This routine sends a message to @a mbox and waits for a receiver to both
3562 * receive and process it. The message data may be in a buffer, in a memory
3563 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003564 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003565 * @param mbox Address of the mailbox.
3566 * @param tx_msg Address of the transmit message descriptor.
3567 * @param timeout Waiting period for the message to be received (in
3568 * milliseconds), or one of the special values K_NO_WAIT
3569 * and K_FOREVER. Once the message has been received,
3570 * this routine waits as long as necessary for the message
3571 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003572 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003573 * @retval 0 Message sent.
3574 * @retval -ENOMSG Returned without waiting.
3575 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003576 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003577 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003578extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003579 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003580
Peter Mitsis12092702016-10-14 12:57:23 -04003581/**
3582 * @brief Send a mailbox message in an asynchronous manner.
3583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003584 * This routine sends a message to @a mbox without waiting for a receiver
3585 * to process it. The message data may be in a buffer, in a memory pool block,
3586 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3587 * will be given when the message has been both received and completely
3588 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003590 * @param mbox Address of the mailbox.
3591 * @param tx_msg Address of the transmit message descriptor.
3592 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003593 *
3594 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003595 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003596 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003597extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003598 struct k_sem *sem);
3599
Peter Mitsis12092702016-10-14 12:57:23 -04003600/**
3601 * @brief Receive a mailbox message.
3602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003603 * This routine receives a message from @a mbox, then optionally retrieves
3604 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003606 * @param mbox Address of the mailbox.
3607 * @param rx_msg Address of the receive message descriptor.
3608 * @param buffer Address of the buffer to receive data, or NULL to defer data
3609 * retrieval and message disposal until later.
3610 * @param timeout Waiting period for a message to be received (in
3611 * milliseconds), or one of the special values K_NO_WAIT
3612 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003613 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003614 * @retval 0 Message received.
3615 * @retval -ENOMSG Returned without waiting.
3616 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003617 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003618 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003619extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003620 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003621
3622/**
3623 * @brief Retrieve mailbox message data into a buffer.
3624 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003625 * This routine completes the processing of a received message by retrieving
3626 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003627 *
3628 * Alternatively, this routine can be used to dispose of a received message
3629 * without retrieving its data.
3630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003631 * @param rx_msg Address of the receive message descriptor.
3632 * @param buffer Address of the buffer to receive data, or NULL to discard
3633 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003634 *
3635 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003636 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003637 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003638extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003639
3640/**
3641 * @brief Retrieve mailbox message data into a memory pool block.
3642 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003643 * This routine completes the processing of a received message by retrieving
3644 * its data into a memory pool block, then disposing of the message.
3645 * The memory pool block that results from successful retrieval must be
3646 * returned to the pool once the data has been processed, even in cases
3647 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003648 *
3649 * Alternatively, this routine can be used to dispose of a received message
3650 * without retrieving its data. In this case there is no need to return a
3651 * memory pool block to the pool.
3652 *
3653 * This routine allocates a new memory pool block for the data only if the
3654 * data is not already in one. If a new block cannot be allocated, the routine
3655 * returns a failure code and the received message is left unchanged. This
3656 * permits the caller to reattempt data retrieval at a later time or to dispose
3657 * of the received message without retrieving its data.
3658 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003659 * @param rx_msg Address of a receive message descriptor.
3660 * @param pool Address of memory pool, or NULL to discard data.
3661 * @param block Address of the area to hold memory pool block info.
3662 * @param timeout Waiting period to wait for a memory pool block (in
3663 * milliseconds), or one of the special values K_NO_WAIT
3664 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003665 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003666 * @retval 0 Data retrieved.
3667 * @retval -ENOMEM Returned without waiting.
3668 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003669 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003670 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003671extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003672 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003673 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003674
Anas Nashif166f5192018-02-25 08:02:36 -06003675/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003676
3677/**
Anas Nashifce78d162018-05-24 12:43:11 -05003678 * @defgroup pipe_apis Pipe APIs
3679 * @ingroup kernel_apis
3680 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003681 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003682
Anas Nashifce78d162018-05-24 12:43:11 -05003683/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003684struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003685 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3686 size_t size; /**< Buffer size */
3687 size_t bytes_used; /**< # bytes used in buffer */
3688 size_t read_index; /**< Where in buffer to read from */
3689 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003690
3691 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003692 _wait_q_t readers; /**< Reader wait queue */
3693 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003694 } wait_q;
3695
Anas Nashif2f203c22016-12-18 06:57:45 -05003696 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Anas Nashifce78d162018-05-24 12:43:11 -05003697 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003698};
3699
Anas Nashifce78d162018-05-24 12:43:11 -05003700/**
3701 * @cond INTERNAL_HIDDEN
3702 */
3703#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3704
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003705#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003706 { \
3707 .buffer = pipe_buffer, \
3708 .size = pipe_buffer_size, \
3709 .bytes_used = 0, \
3710 .read_index = 0, \
3711 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003712 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3713 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003714 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003715 }
3716
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003717#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3718
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003719/**
Allan Stephensc98da842016-11-11 15:45:03 -05003720 * INTERNAL_HIDDEN @endcond
3721 */
3722
3723/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003724 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003725 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003726 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003727 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003728 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003729 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003730 * @param name Name of the pipe.
3731 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3732 * or zero if no ring buffer is used.
3733 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003734 *
3735 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003736 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003737#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3738 static unsigned char __kernel_noinit __aligned(pipe_align) \
3739 _k_pipe_buf_##name[pipe_buffer_size]; \
3740 struct k_pipe name \
3741 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003742 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003743
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003744/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003745 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003746 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003747 * This routine initializes a pipe object, prior to its first use.
3748 *
3749 * @param pipe Address of the pipe.
3750 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3751 * is used.
3752 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3753 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003754 *
3755 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003756 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003757 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003758void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3759
3760/**
3761 * @brief Release a pipe's allocated buffer
3762 *
3763 * If a pipe object was given a dynamically allocated buffer via
3764 * k_pipe_alloc_init(), this will free it. This function does nothing
3765 * if the buffer wasn't dynamically allocated.
3766 *
3767 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003768 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003769 */
3770void k_pipe_cleanup(struct k_pipe *pipe);
3771
3772/**
3773 * @brief Initialize a pipe and allocate a buffer for it
3774 *
3775 * Storage for the buffer region will be allocated from the calling thread's
3776 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3777 * or userspace is enabled and the pipe object loses all references to it.
3778 *
3779 * This function should only be called on uninitialized pipe objects.
3780 *
3781 * @param pipe Address of the pipe.
3782 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3783 * buffer is used.
3784 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003785 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003786 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003787 */
3788__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003789
3790/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003791 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003792 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003793 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003794 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003795 * @param pipe Address of the pipe.
3796 * @param data Address of data to write.
3797 * @param bytes_to_write Size of data (in bytes).
3798 * @param bytes_written Address of area to hold the number of bytes written.
3799 * @param min_xfer Minimum number of bytes to write.
3800 * @param timeout Waiting period to wait for the data to be written (in
3801 * milliseconds), or one of the special values K_NO_WAIT
3802 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003803 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003804 * @retval 0 At least @a min_xfer bytes of data were written.
3805 * @retval -EIO Returned without waiting; zero data bytes were written.
3806 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003807 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003808 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003809 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003810__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3811 size_t bytes_to_write, size_t *bytes_written,
3812 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003813
3814/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003815 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003817 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003819 * @param pipe Address of the pipe.
3820 * @param data Address to place the data read from pipe.
3821 * @param bytes_to_read Maximum number of data bytes to read.
3822 * @param bytes_read Address of area to hold the number of bytes read.
3823 * @param min_xfer Minimum number of data bytes to read.
3824 * @param timeout Waiting period to wait for the data to be read (in
3825 * milliseconds), or one of the special values K_NO_WAIT
3826 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003827 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003828 * @retval 0 At least @a min_xfer bytes of data were read.
3829 * @retval -EIO Returned without waiting; zero data bytes were read.
3830 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003831 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003832 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003833 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003834__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3835 size_t bytes_to_read, size_t *bytes_read,
3836 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003837
3838/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003839 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003841 * This routine writes the data contained in a memory block to @a pipe.
3842 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003843 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003845 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003846 * @param block Memory block containing data to send
3847 * @param size Number of data bytes in memory block to send
3848 * @param sem Semaphore to signal upon completion (else NULL)
3849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003850 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003851 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003852 */
3853extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3854 size_t size, struct k_sem *sem);
3855
Anas Nashif166f5192018-02-25 08:02:36 -06003856/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003857
Allan Stephensc98da842016-11-11 15:45:03 -05003858/**
3859 * @cond INTERNAL_HIDDEN
3860 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003861
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003862struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003863 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003864 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003865 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003866 char *buffer;
3867 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003868 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003869
Anas Nashif2f203c22016-12-18 06:57:45 -05003870 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003871};
3872
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003873#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003874 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003875 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003876 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003877 .num_blocks = slab_num_blocks, \
3878 .block_size = slab_block_size, \
3879 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003880 .free_list = NULL, \
3881 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003882 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003883 }
3884
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003885#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3886
3887
Peter Mitsis578f9112016-10-07 13:50:31 -04003888/**
Allan Stephensc98da842016-11-11 15:45:03 -05003889 * INTERNAL_HIDDEN @endcond
3890 */
3891
3892/**
3893 * @defgroup mem_slab_apis Memory Slab APIs
3894 * @ingroup kernel_apis
3895 * @{
3896 */
3897
3898/**
Allan Stephensda827222016-11-09 14:23:58 -06003899 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003900 *
Allan Stephensda827222016-11-09 14:23:58 -06003901 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003902 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003903 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3904 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003905 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003906 *
Allan Stephensda827222016-11-09 14:23:58 -06003907 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003908 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003909 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003910 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003911 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003912 * @param name Name of the memory slab.
3913 * @param slab_block_size Size of each memory block (in bytes).
3914 * @param slab_num_blocks Number memory blocks.
3915 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003916 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003917 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003918#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3919 char __noinit __aligned(slab_align) \
3920 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3921 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003922 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003923 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003924 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003925
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003926/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003927 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003929 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003930 *
Allan Stephensda827222016-11-09 14:23:58 -06003931 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3932 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3933 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3934 * To ensure that each memory block is similarly aligned to this boundary,
3935 * @a slab_block_size must also be a multiple of N.
3936 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003937 * @param slab Address of the memory slab.
3938 * @param buffer Pointer to buffer used for the memory blocks.
3939 * @param block_size Size of each memory block (in bytes).
3940 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003941 *
3942 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003943 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003944 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003945extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003946 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003947
3948/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003949 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003950 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003951 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003953 * @param slab Address of the memory slab.
3954 * @param mem Pointer to block address area.
3955 * @param timeout Maximum time to wait for operation to complete
3956 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3957 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003958 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003959 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003960 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003961 * @retval -ENOMEM Returned without waiting.
3962 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003963 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003964 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003965extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003966 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003967
3968/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003969 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003971 * This routine releases a previously allocated memory block back to its
3972 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003974 * @param slab Address of the memory slab.
3975 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003976 *
3977 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003978 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003979 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003980extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003981
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003982/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003983 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003984 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003985 * This routine gets the number of memory blocks that are currently
3986 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003987 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003988 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003989 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003990 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003991 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003992 */
Kumar Galacc334c72017-04-21 10:55:34 -05003993static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003994{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003995 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003996}
3997
Peter Mitsisc001aa82016-10-13 13:53:37 -04003998/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003999 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004001 * This routine gets the number of memory blocks that are currently
4002 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004004 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004006 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004007 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004008 */
Kumar Galacc334c72017-04-21 10:55:34 -05004009static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004010{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004011 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004012}
4013
Anas Nashif166f5192018-02-25 08:02:36 -06004014/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004015
4016/**
4017 * @cond INTERNAL_HIDDEN
4018 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004019
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004020struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004021 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004022 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004023};
4024
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004025/**
Allan Stephensc98da842016-11-11 15:45:03 -05004026 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004027 */
4028
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004029/**
Allan Stephensc98da842016-11-11 15:45:03 -05004030 * @addtogroup mem_pool_apis
4031 * @{
4032 */
4033
4034/**
4035 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004037 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4038 * long. The memory pool allows blocks to be repeatedly partitioned into
4039 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004040 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004041 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004042 * If the pool is to be accessed outside the module where it is defined, it
4043 * can be declared via
4044 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004045 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004046 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004047 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004048 * @param minsz Size of the smallest blocks in the pool (in bytes).
4049 * @param maxsz Size of the largest blocks in the pool (in bytes).
4050 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004051 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004052 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004053 */
Andy Ross73cb9582017-05-09 10:42:39 -07004054#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4055 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4056 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08004057 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07004058 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004059 .base = { \
4060 .buf = _mpool_buf_##name, \
4061 .max_sz = maxsz, \
4062 .n_max = nmax, \
4063 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
4064 .levels = _mpool_lvls_##name, \
4065 .flags = SYS_MEM_POOL_KERNEL \
4066 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004067 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004068
Peter Mitsis937042c2016-10-13 13:18:26 -04004069/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004070 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004071 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004072 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004074 * @param pool Address of the memory pool.
4075 * @param block Pointer to block descriptor for the allocated memory.
4076 * @param size Amount of memory to allocate (in bytes).
4077 * @param timeout Maximum time to wait for operation to complete
4078 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4079 * or K_FOREVER to wait as long as necessary.
4080 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004081 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004082 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004083 * @retval -ENOMEM Returned without waiting.
4084 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004085 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004086 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004087extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004088 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004089
4090/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004091 * @brief Allocate memory from a memory pool with malloc() semantics
4092 *
4093 * Such memory must be released using k_free().
4094 *
4095 * @param pool Address of the memory pool.
4096 * @param size Amount of memory to allocate (in bytes).
4097 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004098 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004099 */
4100extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4101
4102/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004103 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004104 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004105 * This routine releases a previously allocated memory block back to its
4106 * memory pool.
4107 *
4108 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004109 *
4110 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004111 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004112 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004113extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004114
4115/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004116 * @brief Free memory allocated from a memory pool.
4117 *
4118 * This routine releases a previously allocated memory block back to its
4119 * memory pool
4120 *
4121 * @param id Memory block identifier.
4122 *
4123 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004124 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004125 */
4126extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4127
4128/**
Anas Nashif166f5192018-02-25 08:02:36 -06004129 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004130 */
4131
4132/**
4133 * @defgroup heap_apis Heap Memory Pool APIs
4134 * @ingroup kernel_apis
4135 * @{
4136 */
4137
4138/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004139 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004140 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004141 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004142 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004144 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004145 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004146 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004147 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004148 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004149extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004150
4151/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004152 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004153 *
4154 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004155 * returned must have been allocated from the heap memory pool or
4156 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004157 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004158 * If @a ptr is NULL, no operation is performed.
4159 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004160 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004161 *
4162 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004163 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004164 */
4165extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004166
Allan Stephensc98da842016-11-11 15:45:03 -05004167/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004168 * @brief Allocate memory from heap, array style
4169 *
4170 * This routine provides traditional calloc() semantics. Memory is
4171 * allocated from the heap memory pool and zeroed.
4172 *
4173 * @param nmemb Number of elements in the requested array
4174 * @param size Size of each array element (in bytes).
4175 *
4176 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004177 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004178 */
4179extern void *k_calloc(size_t nmemb, size_t size);
4180
Anas Nashif166f5192018-02-25 08:02:36 -06004181/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004182
Benjamin Walshacc68c12017-01-29 18:57:45 -05004183/* polling API - PRIVATE */
4184
Benjamin Walshb0179862017-02-02 16:39:57 -05004185#ifdef CONFIG_POLL
4186#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
4187#else
4188#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
4189#endif
4190
Benjamin Walshacc68c12017-01-29 18:57:45 -05004191/* private - implementation data created as needed, per-type */
4192struct _poller {
4193 struct k_thread *thread;
4194};
4195
4196/* private - types bit positions */
4197enum _poll_types_bits {
4198 /* can be used to ignore an event */
4199 _POLL_TYPE_IGNORE,
4200
4201 /* to be signaled by k_poll_signal() */
4202 _POLL_TYPE_SIGNAL,
4203
4204 /* semaphore availability */
4205 _POLL_TYPE_SEM_AVAILABLE,
4206
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004207 /* queue/fifo/lifo data availability */
4208 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004209
4210 _POLL_NUM_TYPES
4211};
4212
4213#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4214
4215/* private - states bit positions */
4216enum _poll_states_bits {
4217 /* default state when creating event */
4218 _POLL_STATE_NOT_READY,
4219
Benjamin Walshacc68c12017-01-29 18:57:45 -05004220 /* signaled by k_poll_signal() */
4221 _POLL_STATE_SIGNALED,
4222
4223 /* semaphore is available */
4224 _POLL_STATE_SEM_AVAILABLE,
4225
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004226 /* data is available to read on queue/fifo/lifo */
4227 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004228
4229 _POLL_NUM_STATES
4230};
4231
4232#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4233
4234#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004235 (32 - (0 \
4236 + 8 /* tag */ \
4237 + _POLL_NUM_TYPES \
4238 + _POLL_NUM_STATES \
4239 + 1 /* modes */ \
4240 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004241
Benjamin Walshacc68c12017-01-29 18:57:45 -05004242/* end of polling API - PRIVATE */
4243
4244
4245/**
4246 * @defgroup poll_apis Async polling APIs
4247 * @ingroup kernel_apis
4248 * @{
4249 */
4250
4251/* Public polling API */
4252
4253/* public - values for k_poll_event.type bitfield */
4254#define K_POLL_TYPE_IGNORE 0
4255#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4256#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004257#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4258#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004259
4260/* public - polling modes */
4261enum k_poll_modes {
4262 /* polling thread does not take ownership of objects when available */
4263 K_POLL_MODE_NOTIFY_ONLY = 0,
4264
4265 K_POLL_NUM_MODES
4266};
4267
4268/* public - values for k_poll_event.state bitfield */
4269#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004270#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4271#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004272#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4273#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004274
4275/* public - poll signal object */
4276struct k_poll_signal {
4277 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004278 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004279
4280 /*
4281 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4282 * user resets it to 0.
4283 */
4284 unsigned int signaled;
4285
4286 /* custom result value passed to k_poll_signal() if needed */
4287 int result;
4288};
4289
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004290#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004291 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004292 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004293 .signaled = 0, \
4294 .result = 0, \
4295 }
4296
4297struct k_poll_event {
4298 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004299 sys_dnode_t _node;
4300
4301 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004302 struct _poller *poller;
4303
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004304 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004305 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004306
Benjamin Walshacc68c12017-01-29 18:57:45 -05004307 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004308 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004309
4310 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004311 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004312
4313 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004314 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004315
4316 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004317 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004318
4319 /* per-type data */
4320 union {
4321 void *obj;
4322 struct k_poll_signal *signal;
4323 struct k_sem *sem;
4324 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004325 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004326 };
4327};
4328
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004329#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004330 { \
4331 .poller = NULL, \
4332 .type = event_type, \
4333 .state = K_POLL_STATE_NOT_READY, \
4334 .mode = event_mode, \
4335 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004336 { .obj = event_obj }, \
4337 }
4338
4339#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4340 event_tag) \
4341 { \
4342 .type = event_type, \
4343 .tag = event_tag, \
4344 .state = K_POLL_STATE_NOT_READY, \
4345 .mode = event_mode, \
4346 .unused = 0, \
4347 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004348 }
4349
4350/**
4351 * @brief Initialize one struct k_poll_event instance
4352 *
4353 * After this routine is called on a poll event, the event it ready to be
4354 * placed in an event array to be passed to k_poll().
4355 *
4356 * @param event The event to initialize.
4357 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4358 * values. Only values that apply to the same object being polled
4359 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4360 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004361 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004362 * @param obj Kernel object or poll signal.
4363 *
4364 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004365 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004366 */
4367
Kumar Galacc334c72017-04-21 10:55:34 -05004368extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004369 int mode, void *obj);
4370
4371/**
4372 * @brief Wait for one or many of multiple poll events to occur
4373 *
4374 * This routine allows a thread to wait concurrently for one or many of
4375 * multiple poll events to have occurred. Such events can be a kernel object
4376 * being available, like a semaphore, or a poll signal event.
4377 *
4378 * When an event notifies that a kernel object is available, the kernel object
4379 * is not "given" to the thread calling k_poll(): it merely signals the fact
4380 * that the object was available when the k_poll() call was in effect. Also,
4381 * all threads trying to acquire an object the regular way, i.e. by pending on
4382 * the object, have precedence over the thread polling on the object. This
4383 * means that the polling thread will never get the poll event on an object
4384 * until the object becomes available and its pend queue is empty. For this
4385 * reason, the k_poll() call is more effective when the objects being polled
4386 * only have one thread, the polling thread, trying to acquire them.
4387 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004388 * When k_poll() returns 0, the caller should loop on all the events that were
4389 * passed to k_poll() and check the state field for the values that were
4390 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004391 *
4392 * Before being reused for another call to k_poll(), the user has to reset the
4393 * state field to K_POLL_STATE_NOT_READY.
4394 *
Andrew Boie3772f772018-05-07 16:52:57 -07004395 * When called from user mode, a temporary memory allocation is required from
4396 * the caller's resource pool.
4397 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004398 * @param events An array of pointers to events to be polled for.
4399 * @param num_events The number of events in the array.
4400 * @param timeout Waiting period for an event to be ready (in milliseconds),
4401 * or one of the special values K_NO_WAIT and K_FOREVER.
4402 *
4403 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004404 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02004405 * @retval -EINTR Poller thread has been interrupted.
Andrew Boie3772f772018-05-07 16:52:57 -07004406 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4407 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004408 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004409 */
4410
Andrew Boie3772f772018-05-07 16:52:57 -07004411__syscall int k_poll(struct k_poll_event *events, int num_events,
4412 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004413
4414/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004415 * @brief Initialize a poll signal object.
4416 *
4417 * Ready a poll signal object to be signaled via k_poll_signal().
4418 *
4419 * @param signal A poll signal.
4420 *
4421 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004422 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004423 */
4424
Andrew Boie3772f772018-05-07 16:52:57 -07004425__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4426
4427/*
4428 * @brief Reset a poll signal object's state to unsignaled.
4429 *
4430 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004431 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004432 */
4433__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4434
4435static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4436{
4437 signal->signaled = 0;
4438}
4439
4440/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004441 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004442 *
4443 * @param signal A poll signal object
4444 * @param signaled An integer buffer which will be written nonzero if the
4445 * object was signaled
4446 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004447 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004448 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004449 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004450 */
4451__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4452 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004453
4454/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004455 * @brief Signal a poll signal object.
4456 *
4457 * This routine makes ready a poll signal, which is basically a poll event of
4458 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4459 * made ready to run. A @a result value can be specified.
4460 *
4461 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004462 * k_poll_signal(), stays set until the user sets it back to 0 with
4463 * k_poll_signal_reset(). It thus has to be reset by the user before being
4464 * passed again to k_poll() or k_poll() will consider it being signaled, and
4465 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004466 *
4467 * @param signal A poll signal.
4468 * @param result The value to store in the result field of the signal.
4469 *
4470 * @retval 0 The signal was delivered successfully.
4471 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004472 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004473 */
4474
Andrew Boie3772f772018-05-07 16:52:57 -07004475__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004476
Anas Nashif954d5502018-02-25 08:37:28 -06004477/**
4478 * @internal
4479 */
Andy Ross8606fab2018-03-26 10:54:40 -07004480extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004481
Anas Nashif166f5192018-02-25 08:02:36 -06004482/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004483
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004484/**
4485 * @brief Make the CPU idle.
4486 *
4487 * This function makes the CPU idle until an event wakes it up.
4488 *
4489 * In a regular system, the idle thread should be the only thread responsible
4490 * for making the CPU idle and triggering any type of power management.
4491 * However, in some more constrained systems, such as a single-threaded system,
4492 * the only thread would be responsible for this if needed.
4493 *
4494 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004495 * @req K-MISC-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004496 */
4497extern void k_cpu_idle(void);
4498
4499/**
4500 * @brief Make the CPU idle in an atomic fashion.
4501 *
4502 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4503 * must be done atomically before making the CPU idle.
4504 *
4505 * @param key Interrupt locking key obtained from irq_lock().
4506 *
4507 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004508 * @req K-MISC-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004509 */
4510extern void k_cpu_atomic_idle(unsigned int key);
4511
Anas Nashif954d5502018-02-25 08:37:28 -06004512
4513/**
4514 * @internal
4515 */
Kumar Galacc334c72017-04-21 10:55:34 -05004516extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004517
Andrew Boiecdb94d62017-04-18 15:22:05 -07004518#ifdef _ARCH_EXCEPT
4519/* This archtecture has direct support for triggering a CPU exception */
4520#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4521#else
4522
Andrew Boiecdb94d62017-04-18 15:22:05 -07004523/* NOTE: This is the implementation for arches that do not implement
4524 * _ARCH_EXCEPT() to generate a real CPU exception.
4525 *
4526 * We won't have a real exception frame to determine the PC value when
4527 * the oops occurred, so print file and line number before we jump into
4528 * the fatal error handler.
4529 */
4530#define _k_except_reason(reason) do { \
4531 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4532 _NanoFatalErrorHandler(reason, &_default_esf); \
4533 CODE_UNREACHABLE; \
4534 } while (0)
4535
4536#endif /* _ARCH__EXCEPT */
4537
4538/**
4539 * @brief Fatally terminate a thread
4540 *
4541 * This should be called when a thread has encountered an unrecoverable
4542 * runtime condition and needs to terminate. What this ultimately
4543 * means is determined by the _fatal_error_handler() implementation, which
4544 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4545 *
4546 * If this is called from ISR context, the default system fatal error handler
4547 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004548 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004549 */
4550#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4551
4552/**
4553 * @brief Fatally terminate the system
4554 *
4555 * This should be called when the Zephyr kernel has encountered an
4556 * unrecoverable runtime condition and needs to terminate. What this ultimately
4557 * means is determined by the _fatal_error_handler() implementation, which
4558 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004559 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004560 */
4561#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4562
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004563/*
4564 * private APIs that are utilized by one or more public APIs
4565 */
4566
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004567#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004568/**
4569 * @internal
4570 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004571extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004572#else
Anas Nashif954d5502018-02-25 08:37:28 -06004573/**
4574 * @internal
4575 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004576#define _init_static_threads() do { } while ((0))
4577#endif
4578
Anas Nashif954d5502018-02-25 08:37:28 -06004579/**
4580 * @internal
4581 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004582extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004583/**
4584 * @internal
4585 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004586extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004587
Andrew Boiedc5d9352017-06-02 12:56:47 -07004588/* arch/cpu.h may declare an architecture or platform-specific macro
4589 * for properly declaring stacks, compatible with MMU/MPU constraints if
4590 * enabled
4591 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004592
4593/**
4594 * @brief Obtain an extern reference to a stack
4595 *
4596 * This macro properly brings the symbol of a thread stack declared
4597 * elsewhere into scope.
4598 *
4599 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004600 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004601 */
4602#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4603
Andrew Boiedc5d9352017-06-02 12:56:47 -07004604#ifdef _ARCH_THREAD_STACK_DEFINE
4605#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4606#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4607 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4608#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4609#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004610static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004611{
4612 return _ARCH_THREAD_STACK_BUFFER(sym);
4613}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004614#else
4615/**
4616 * @brief Declare a toplevel thread stack memory region
4617 *
4618 * This declares a region of memory suitable for use as a thread's stack.
4619 *
4620 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4621 * 'noinit' section so that it isn't zeroed at boot
4622 *
Andrew Boie507852a2017-07-25 18:47:07 -07004623 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004624 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004625 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004626 *
4627 * It is legal to precede this definition with the 'static' keyword.
4628 *
4629 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4630 * parameter of k_thread_create(), it may not be the same as the
4631 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4632 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004633 * Some arches may round the size of the usable stack region up to satisfy
4634 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4635 * size.
4636 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004637 * @param sym Thread stack symbol name
4638 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004639 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004640 */
4641#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004642 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004643
4644/**
4645 * @brief Declare a toplevel array of thread stack memory regions
4646 *
4647 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4648 * definition for additional details and constraints.
4649 *
4650 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4651 * 'noinit' section so that it isn't zeroed at boot
4652 *
4653 * @param sym Thread stack symbol name
4654 * @param nmemb Number of stacks to declare
4655 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004656 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004657 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004658#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004659 struct _k_thread_stack_element __noinit \
4660 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004661
4662/**
4663 * @brief Declare an embedded stack memory region
4664 *
4665 * Used for stacks embedded within other data structures. Use is highly
4666 * discouraged but in some cases necessary. For memory protection scenarios,
4667 * it is very important that any RAM preceding this member not be writable
4668 * by threads else a stack overflow will lead to silent corruption. In other
4669 * words, the containing data structure should live in RAM owned by the kernel.
4670 *
4671 * @param sym Thread stack symbol name
4672 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004673 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004674 */
4675#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004676 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004677
4678/**
4679 * @brief Return the size in bytes of a stack memory region
4680 *
4681 * Convenience macro for passing the desired stack size to k_thread_create()
4682 * since the underlying implementation may actually create something larger
4683 * (for instance a guard area).
4684 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004685 * The value returned here is not guaranteed to match the 'size' parameter
4686 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004687 *
4688 * @param sym Stack memory symbol
4689 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004690 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004691 */
4692#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4693
4694/**
4695 * @brief Get a pointer to the physical stack buffer
4696 *
4697 * Convenience macro to get at the real underlying stack buffer used by
4698 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4699 * This is really only intended for diagnostic tools which want to examine
4700 * stack memory contents.
4701 *
4702 * @param sym Declared stack symbol name
4703 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004704 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004705 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004706static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004707{
4708 return (char *)sym;
4709}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004710
4711#endif /* _ARCH_DECLARE_STACK */
4712
Chunlin Hane9c97022017-07-07 20:29:30 +08004713/**
4714 * @defgroup mem_domain_apis Memory domain APIs
4715 * @ingroup kernel_apis
4716 * @{
4717 */
4718
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004719/**
4720 * @def MEM_PARTITION_ENTRY
4721 * @brief Used to declare a memory partition entry
4722 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004723 */
4724#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4725 {\
4726 .start = _start, \
4727 .size = _size, \
4728 .attr = _attr, \
4729 }
4730
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004731/**
4732 * @def K_MEM_PARTITION_DEFINE
4733 * @brief Used to declare a memory partition
4734 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004735 */
4736#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4737#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4738 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304739 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004740 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4741#else
4742#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304743 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004744 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4745#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4746
Chunlin Hane9c97022017-07-07 20:29:30 +08004747/* memory partition */
4748struct k_mem_partition {
4749 /* start address of memory partition */
4750 u32_t start;
4751 /* size of memory partition */
4752 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304753#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004754 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304755 k_mem_partition_attr_t attr;
4756#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004757};
4758
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304759/* memory domian
4760 * Note: Always declare this structure with __kernel prefix
4761 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004762struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304763#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004764 /* partitions in the domain */
4765 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304766#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004767 /* domain q */
4768 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004769 /* number of partitions in the domain */
4770 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004771};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304772
Chunlin Hane9c97022017-07-07 20:29:30 +08004773
4774/**
4775 * @brief Initialize a memory domain.
4776 *
4777 * Initialize a memory domain with given name and memory partitions.
4778 *
4779 * @param domain The memory domain to be initialized.
4780 * @param num_parts The number of array items of "parts" parameter.
4781 * @param parts An array of pointers to the memory partitions. Can be NULL
4782 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004783 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004784 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004785extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004786 struct k_mem_partition *parts[]);
4787/**
4788 * @brief Destroy a memory domain.
4789 *
4790 * Destroy a memory domain.
4791 *
4792 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004793 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004794 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004795extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4796
4797/**
4798 * @brief Add a memory partition into a memory domain.
4799 *
4800 * Add a memory partition into a memory domain.
4801 *
4802 * @param domain The memory domain to be added a memory partition.
4803 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004804 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004805 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004806extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4807 struct k_mem_partition *part);
4808
4809/**
4810 * @brief Remove a memory partition from a memory domain.
4811 *
4812 * Remove a memory partition from a memory domain.
4813 *
4814 * @param domain The memory domain to be removed a memory partition.
4815 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004816 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004817 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004818extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4819 struct k_mem_partition *part);
4820
4821/**
4822 * @brief Add a thread into a memory domain.
4823 *
4824 * Add a thread into a memory domain.
4825 *
4826 * @param domain The memory domain that the thread is going to be added into.
4827 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004828 *
4829 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004830 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004831extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4832 k_tid_t thread);
4833
4834/**
4835 * @brief Remove a thread from its memory domain.
4836 *
4837 * Remove a thread from its memory domain.
4838 *
4839 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004840 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004841 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004842extern void k_mem_domain_remove_thread(k_tid_t thread);
4843
Anas Nashif166f5192018-02-25 08:02:36 -06004844/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004845
Andrew Boie756f9072017-10-10 16:01:49 -07004846/**
4847 * @brief Emit a character buffer to the console device
4848 *
4849 * @param c String of characters to print
4850 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004851 *
4852 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004853 */
4854__syscall void k_str_out(char *c, size_t n);
4855
Andy Rosse7172672018-01-24 15:48:32 -08004856/**
4857 * @brief Start a numbered CPU on a MP-capable system
4858
4859 * This starts and initializes a specific CPU. The main thread on
4860 * startup is running on CPU zero, other processors are numbered
4861 * sequentially. On return from this function, the CPU is known to
4862 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004863 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004864 * with the provided key will work to enable them.
4865 *
4866 * Normally, in SMP mode this function will be called by the kernel
4867 * initialization and should not be used as a user API. But it is
4868 * defined here for special-purpose apps which want Zephyr running on
4869 * one core and to use others for design-specific processing.
4870 *
4871 * @param cpu_num Integer number of the CPU
4872 * @param stack Stack memory for the CPU
4873 * @param sz Stack buffer size, in bytes
4874 * @param fn Function to begin running on the CPU. First argument is
4875 * an irq_unlock() key.
4876 * @param arg Untyped argument to be passed to "fn"
4877 */
4878extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4879 void (*fn)(int, void *), void *arg);
4880
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004881#ifdef __cplusplus
4882}
4883#endif
4884
Andrew Boiee004dec2016-11-07 09:01:19 -08004885#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4886/*
4887 * Define new and delete operators.
4888 * At this moment, the operators do nothing since objects are supposed
4889 * to be statically allocated.
4890 */
4891inline void operator delete(void *ptr)
4892{
4893 (void)ptr;
4894}
4895
4896inline void operator delete[](void *ptr)
4897{
4898 (void)ptr;
4899}
4900
4901inline void *operator new(size_t size)
4902{
4903 (void)size;
4904 return NULL;
4905}
4906
4907inline void *operator new[](size_t size)
4908{
4909 (void)size;
4910 return NULL;
4911}
4912
4913/* Placement versions of operator new and delete */
4914inline void operator delete(void *ptr1, void *ptr2)
4915{
4916 (void)ptr1;
4917 (void)ptr2;
4918}
4919
4920inline void operator delete[](void *ptr1, void *ptr2)
4921{
4922 (void)ptr1;
4923 (void)ptr2;
4924}
4925
4926inline void *operator new(size_t size, void *ptr)
4927{
4928 (void)size;
4929 return ptr;
4930}
4931
4932inline void *operator new[](size_t size, void *ptr)
4933{
4934 (void)size;
4935 return ptr;
4936}
4937
4938#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4939
Andrew Boiefa94ee72017-09-28 16:54:35 -07004940#include <syscalls/kernel.h>
4941
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004942#endif /* !_ASMLANGUAGE */
4943
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004944#endif /* _kernel__h_ */