blob: a6a2c61983d954bd761690481188ae03f97e90e4 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
13#ifndef _kernel__h_
14#define _kernel__h_
15
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040017#include <stddef.h>
Kumar Gala78908162017-04-19 10:32:08 -050018#include <zephyr/types.h>
Anas Nashif173902f2017-01-17 07:08:56 -050019#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020#include <toolchain.h>
Anas Nashif397d29d2017-06-17 11:30:47 -040021#include <linker/sections.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040022#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
Andy Ross1acd8c22018-05-03 14:51:49 -070025#include <sched_priq.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040026#include <misc/dlist.h>
27#include <misc/slist.h>
Andrew Boie2b9b4b22018-04-27 13:21:22 -070028#include <misc/sflist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050029#include <misc/util.h>
Andrew Boieaa6de292018-03-06 17:12:37 -080030#include <misc/mempool_base.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050031#include <kernel_version.h>
Leandro Pereiraadce1d12017-10-13 15:45:02 -070032#include <random/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070033#include <kernel_arch_thread.h>
Andrew Boie13ca6fe2017-09-23 12:05:49 -070034#include <syscall.h>
Andrew Boie43263fc2017-11-02 11:07:31 -070035#include <misc/printk.h>
36#include <arch/cpu.h>
Andy Ross1acd8c22018-05-03 14:51:49 -070037#include <misc/rb.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040038
39#ifdef __cplusplus
40extern "C" {
41#endif
42
Anas Nashifbbb157d2017-01-15 08:46:31 -050043/**
44 * @brief Kernel APIs
45 * @defgroup kernel_apis Kernel APIs
46 * @{
47 * @}
48 */
49
Anas Nashif61f4b242016-11-18 10:53:59 -050050#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040051#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
52#else
53#define K_DEBUG(fmt, ...)
54#endif
55
Benjamin Walsh2f280412017-01-14 19:23:46 -050056#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
57#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
58#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
59#elif defined(CONFIG_COOP_ENABLED)
60#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
61#define _NUM_PREEMPT_PRIO (0)
62#elif defined(CONFIG_PREEMPT_ENABLED)
63#define _NUM_COOP_PRIO (0)
64#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
65#else
66#error "invalid configuration"
67#endif
68
69#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040070#define K_PRIO_PREEMPT(x) (x)
71
Benjamin Walsh456c6da2016-09-02 18:55:39 -040072#define K_ANY NULL
73#define K_END NULL
74
Benjamin Walshedb35702017-01-14 18:47:22 -050075#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040076#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050077#elif defined(CONFIG_COOP_ENABLED)
78#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
79#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050081#else
82#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040083#endif
84
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050085#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040086#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
87#else
88#define K_LOWEST_THREAD_PRIO -1
89#endif
90
Benjamin Walshfab8d922016-11-08 15:36:36 -050091#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
92
Benjamin Walsh456c6da2016-09-02 18:55:39 -040093#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
94#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
95
Andy Ross1acd8c22018-05-03 14:51:49 -070096#ifdef CONFIG_WAITQ_FAST
97
98typedef struct {
99 struct _priq_rb waitq;
100} _wait_q_t;
101
102extern int _priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
103
104#define _WAIT_Q_INIT(wait_q) { { { .lessthan_fn = _priq_rb_lessthan } } }
105
106#else
107
Andy Rossccf3bf72018-05-10 11:10:34 -0700108typedef struct {
109 sys_dlist_t waitq;
110} _wait_q_t;
111
112#define _WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400113
Andy Ross1acd8c22018-05-03 14:51:49 -0700114#endif
115
Anas Nashif2f203c22016-12-18 06:57:45 -0500116#ifdef CONFIG_OBJECT_TRACING
117#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
118#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400119#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500120#define _OBJECT_TRACING_INIT
121#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122#endif
123
Benjamin Walshacc68c12017-01-29 18:57:45 -0500124#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300125#define _POLL_EVENT_OBJ_INIT(obj) \
126 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
127#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500128#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300129#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500130#define _POLL_EVENT
131#endif
132
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500133struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400134struct k_mutex;
135struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400136struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400137struct k_msgq;
138struct k_mbox;
139struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200140struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400141struct k_fifo;
142struct k_lifo;
143struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400144struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400145struct k_mem_pool;
146struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500147struct k_poll_event;
148struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800149struct k_mem_domain;
150struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400151
Andrew Boie5bd891d2017-09-27 12:59:28 -0700152/* This enumeration needs to be kept in sync with the lists of kernel objects
153 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
154 * function in kernel/userspace.c
155 */
Andrew Boie945af952017-08-22 13:15:23 -0700156enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700157 K_OBJ_ANY,
158
Leandro Pereirac2003672018-04-04 13:50:32 -0700159 /** @cond
160 * Doxygen should ignore this build-time generated include file
161 * when genrating API documentation. Enumeration values are
162 * generated during build by gen_kobject_list.py. It includes
163 * basic kernel objects (e.g. pipes and mutexes) and driver types.
164 */
165#include <kobj-types-enum.h>
166 /** @endcond
167 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700168
Andrew Boie945af952017-08-22 13:15:23 -0700169 K_OBJ_LAST
170};
171
172#ifdef CONFIG_USERSPACE
173/* Table generated by gperf, these objects are retrieved via
174 * _k_object_find() */
175struct _k_object {
176 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700177 u8_t perms[CONFIG_MAX_THREAD_BYTES];
178 u8_t type;
179 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700180 u32_t data;
Andrew Boie945af952017-08-22 13:15:23 -0700181} __packed;
182
Andrew Boie877f82e2017-10-17 11:20:22 -0700183struct _k_object_assignment {
184 struct k_thread *thread;
185 void * const *objects;
186};
187
188/**
189 * @brief Grant a static thread access to a list of kernel objects
190 *
191 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
192 * a set of kernel objects. These objects do not need to be in an initialized
193 * state. The permissions will be granted when the threads are initialized
194 * in the early boot sequence.
195 *
196 * All arguments beyond the first must be pointers to kernel objects.
197 *
198 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
199 */
200#define K_THREAD_ACCESS_GRANT(name_, ...) \
201 static void * const _CONCAT(_object_list_, name_)[] = \
202 { __VA_ARGS__, NULL }; \
203 static __used __in_section_unique(object_access) \
204 const struct _k_object_assignment \
205 _CONCAT(_object_access_, name_) = \
206 { (&_k_thread_obj_ ## name_), \
207 (_CONCAT(_object_list_, name_)) }
208
Andrew Boie945af952017-08-22 13:15:23 -0700209#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700210#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700211#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie945af952017-08-22 13:15:23 -0700212
213/**
214 * Lookup a kernel object and init its metadata if it exists
215 *
216 * Calling this on an object will make it usable from userspace.
217 * Intended to be called as the last statement in kernel object init
218 * functions.
219 *
220 * @param object Address of the kernel object
221 */
222void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700223#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700224
225#define K_THREAD_ACCESS_GRANT(thread, ...)
226
Anas Nashif954d5502018-02-25 08:37:28 -0600227/**
228 * @internal
229 */
Andrew Boie743e4682017-10-04 12:25:50 -0700230static inline void _k_object_init(void *obj)
231{
232 ARG_UNUSED(obj);
233}
234
Anas Nashif954d5502018-02-25 08:37:28 -0600235/**
236 * @internal
237 */
Andrew Boie743e4682017-10-04 12:25:50 -0700238static inline void _impl_k_object_access_grant(void *object,
239 struct k_thread *thread)
240{
241 ARG_UNUSED(object);
242 ARG_UNUSED(thread);
243}
244
Anas Nashif954d5502018-02-25 08:37:28 -0600245/**
246 * @internal
247 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700248static inline void k_object_access_revoke(void *object,
249 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700250{
251 ARG_UNUSED(object);
252 ARG_UNUSED(thread);
253}
254
Andrew Boiee9cfc542018-04-13 13:15:28 -0700255/**
256 * @internal
257 */
258static inline void _impl_k_object_release(void *object)
259{
260 ARG_UNUSED(object);
261}
262
Andrew Boie41bab6e2017-10-14 14:42:23 -0700263static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700264{
265 ARG_UNUSED(object);
266}
267#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700268
269/**
270 * grant a thread access to a kernel object
271 *
272 * The thread will be granted access to the object if the caller is from
273 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700274 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700275 *
276 * @param object Address of kernel object
277 * @param thread Thread to grant access to the object
278 */
Andrew Boie743e4682017-10-04 12:25:50 -0700279__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700280
Andrew Boiea89bf012017-10-09 14:47:55 -0700281/**
282 * grant a thread access to a kernel object
283 *
284 * The thread will lose access to the object if the caller is from
285 * supervisor mode, or the caller is from user mode AND has permissions
286 * on both the object and the thread whose access is being revoked.
287 *
288 * @param object Address of kernel object
289 * @param thread Thread to remove access to the object
290 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700291void k_object_access_revoke(void *object, struct k_thread *thread);
292
293
294__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700295
296/**
297 * grant all present and future threads access to an object
298 *
299 * If the caller is from supervisor mode, or the caller is from user mode and
300 * have sufficient permissions on the object, then that object will have
301 * permissions granted to it for *all* current and future threads running in
302 * the system, effectively becoming a public kernel object.
303 *
304 * Use of this API should be avoided on systems that are running untrusted code
305 * as it is possible for such code to derive the addresses of kernel objects
306 * and perform unwanted operations on them.
307 *
Andrew Boie04caa672017-10-13 13:57:07 -0700308 * It is not possible to revoke permissions on public objects; once public,
309 * any thread may use it.
310 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700311 * @param object Address of kernel object
312 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700313void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700314
Andrew Boie31bdfc02017-11-08 16:38:03 -0800315/**
316 * Allocate a kernel object of a designated type
317 *
318 * This will instantiate at runtime a kernel object of the specified type,
319 * returning a pointer to it. The object will be returned in an uninitialized
320 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700321 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800322 *
323 * Currently, allocation of thread stacks is not supported.
324 *
325 * @param otype Requested kernel object type
326 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
327 * available
328 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700329__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800330
Andrew Boie97bf0012018-04-24 17:01:37 -0700331#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800332/**
333 * Free a kernel object previously allocated with k_object_alloc()
334 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700335 * This will return memory for a kernel object back to resource pool it was
336 * allocated from. Care must be exercised that the object will not be used
337 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800338 *
339 * @param obj Pointer to the kernel object memory address.
340 */
341void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700342#else
343static inline void *_impl_k_object_alloc(enum k_objects otype)
344{
Kumar Gala85699f72018-05-17 09:26:03 -0500345 ARG_UNUSED(otype);
346
Andrew Boie97bf0012018-04-24 17:01:37 -0700347 return NULL;
348}
349
350static inline void k_obj_free(void *obj)
351{
352 ARG_UNUSED(obj);
353}
Andrew Boie31bdfc02017-11-08 16:38:03 -0800354#endif /* CONFIG_DYNAMIC_OBJECTS */
355
Andrew Boiebca15da2017-10-15 14:17:48 -0700356/* Using typedef deliberately here, this is quite intended to be an opaque
357 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
358 *
359 * The purpose of this data type is to clearly distinguish between the
360 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
361 * buffer which composes the stack data actually used by the underlying
362 * thread; they cannot be used interchangably as some arches precede the
363 * stack buffer region with guard areas that trigger a MPU or MMU fault
364 * if written to.
365 *
366 * APIs that want to work with the buffer inside should continue to use
367 * char *.
368 *
369 * Stacks should always be created with K_THREAD_STACK_DEFINE().
370 */
371struct __packed _k_thread_stack_element {
372 char data;
373};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700374typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700375
Andrew Boie73abd322017-04-04 13:19:13 -0700376/* timeouts */
377
378struct _timeout;
379typedef void (*_timeout_func_t)(struct _timeout *t);
380
381struct _timeout {
382 sys_dnode_t node;
383 struct k_thread *thread;
384 sys_dlist_t *wait_q;
385 s32_t delta_ticks_from_prev;
386 _timeout_func_t func;
387};
388
389extern s32_t _timeout_remaining_get(struct _timeout *timeout);
390
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700391/**
392 * @typedef k_thread_entry_t
393 * @brief Thread entry point function type.
394 *
395 * A thread's entry point function is invoked when the thread starts executing.
396 * Up to 3 argument values can be passed to the function.
397 *
398 * The thread terminates execution permanently if the entry point function
399 * returns. The thread is responsible for releasing any shared resources
400 * it may own (such as mutexes and dynamically allocated memory), prior to
401 * returning.
402 *
403 * @param p1 First argument.
404 * @param p2 Second argument.
405 * @param p3 Third argument.
406 *
407 * @return N/A
408 */
409typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700410
411#ifdef CONFIG_THREAD_MONITOR
412struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700413 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700414 void *parameter1;
415 void *parameter2;
416 void *parameter3;
417};
418#endif
419
420/* can be used for creating 'dummy' threads, e.g. for pending on objects */
421struct _thread_base {
422
423 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700424 union {
425 sys_dlist_t qnode_dlist;
426 struct rbnode qnode_rb;
427 };
428
429#ifdef CONFIG_WAITQ_FAST
430 /* wait queue on which the thread is pended (needed only for
431 * trees, not dumb lists)
432 */
433 _wait_q_t *pended_on;
434#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700435
436 /* user facing 'thread options'; values defined in include/kernel.h */
437 u8_t user_options;
438
439 /* thread state */
440 u8_t thread_state;
441
442 /*
443 * scheduler lock count and thread priority
444 *
445 * These two fields control the preemptibility of a thread.
446 *
447 * When the scheduler is locked, sched_locked is decremented, which
448 * means that the scheduler is locked for values from 0xff to 0x01. A
449 * thread is coop if its prio is negative, thus 0x80 to 0xff when
450 * looked at the value as unsigned.
451 *
452 * By putting them end-to-end, this means that a thread is
453 * non-preemptible if the bundled value is greater than or equal to
454 * 0x0080.
455 */
456 union {
457 struct {
458#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
459 u8_t sched_locked;
460 s8_t prio;
461#else /* LITTLE and PDP */
462 s8_t prio;
463 u8_t sched_locked;
464#endif
465 };
466 u16_t preempt;
467 };
468
Andy Ross4a2e50f2018-05-15 11:06:25 -0700469#ifdef CONFIG_SCHED_DEADLINE
470 int prio_deadline;
471#endif
472
Andy Ross1acd8c22018-05-03 14:51:49 -0700473 u32_t order_key;
474
Andy Ross2724fd12018-01-29 14:55:20 -0800475#ifdef CONFIG_SMP
476 /* True for the per-CPU idle threads */
477 u8_t is_idle;
478
Andy Ross2724fd12018-01-29 14:55:20 -0800479 /* CPU index on which thread was last run */
480 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700481
482 /* Recursive count of irq_lock() calls */
483 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800484#endif
485
Andrew Boie73abd322017-04-04 13:19:13 -0700486 /* data returned by APIs */
487 void *swap_data;
488
489#ifdef CONFIG_SYS_CLOCK_EXISTS
490 /* this thread's entry in a timeout queue */
491 struct _timeout timeout;
492#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700493};
494
495typedef struct _thread_base _thread_base_t;
496
497#if defined(CONFIG_THREAD_STACK_INFO)
498/* Contains the stack information of a thread */
499struct _thread_stack_info {
500 /* Stack Start */
501 u32_t start;
502 /* Stack Size */
503 u32_t size;
504};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700505
506typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700507#endif /* CONFIG_THREAD_STACK_INFO */
508
Chunlin Hane9c97022017-07-07 20:29:30 +0800509#if defined(CONFIG_USERSPACE)
510struct _mem_domain_info {
511 /* memory domain queue node */
512 sys_dnode_t mem_domain_q_node;
513 /* memory domain of the thread */
514 struct k_mem_domain *mem_domain;
515};
516
517#endif /* CONFIG_USERSPACE */
518
Andrew Boie73abd322017-04-04 13:19:13 -0700519struct k_thread {
520
521 struct _thread_base base;
522
523 /* defined by the architecture, but all archs need these */
524 struct _caller_saved caller_saved;
525 struct _callee_saved callee_saved;
526
527 /* static thread init data */
528 void *init_data;
529
530 /* abort function */
531 void (*fn_abort)(void);
532
533#if defined(CONFIG_THREAD_MONITOR)
534 /* thread entry and parameters description */
535 struct __thread_entry *entry;
536
537 /* next item in list of all threads */
538 struct k_thread *next_thread;
539#endif
540
541#ifdef CONFIG_THREAD_CUSTOM_DATA
542 /* crude thread-local storage */
543 void *custom_data;
544#endif
545
546#ifdef CONFIG_ERRNO
547 /* per-thread errno variable */
548 int errno_var;
549#endif
550
551#if defined(CONFIG_THREAD_STACK_INFO)
552 /* Stack Info */
553 struct _thread_stack_info stack_info;
554#endif /* CONFIG_THREAD_STACK_INFO */
555
Chunlin Hane9c97022017-07-07 20:29:30 +0800556#if defined(CONFIG_USERSPACE)
557 /* memory domain info of the thread */
558 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700559 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700560 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800561#endif /* CONFIG_USERSPACE */
562
Andy Ross042d8ec2017-12-09 08:37:20 -0800563#if defined(CONFIG_USE_SWITCH)
564 /* When using __switch() a few previously arch-specific items
565 * become part of the core OS
566 */
567
568 /* _Swap() return value */
569 int swap_retval;
570
571 /* Context handle returned via _arch_switch() */
572 void *switch_handle;
573#endif
Andrew Boie92e5bd72018-04-12 17:12:15 -0700574 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800575
Andrew Boie73abd322017-04-04 13:19:13 -0700576 /* arch-specifics: must always be at the end */
577 struct _thread_arch arch;
578};
579
580typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400581typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700582#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400583
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400584enum execution_context_types {
585 K_ISR = 0,
586 K_COOP_THREAD,
587 K_PREEMPT_THREAD,
588};
589
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400590/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100591 * @defgroup profiling_apis Profiling APIs
592 * @ingroup kernel_apis
593 * @{
594 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530595typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
596 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100597
598/**
599 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
600 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700601 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100602 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
603 *
604 * CONFIG_MAIN_STACK_SIZE
605 * CONFIG_IDLE_STACK_SIZE
606 * CONFIG_ISR_STACK_SIZE
607 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
608 *
609 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
610 * produce output.
611 *
612 * @return N/A
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530613 *
614 * @deprecated This API is deprecated. Use k_thread_foreach().
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100615 */
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530616__deprecated extern void k_call_stacks_analyze(void);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100617
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530618/**
619 * @brief Iterate over all the threads in the system.
620 *
621 * This routine iterates over all the threads in the system and
622 * calls the user_cb function for each thread.
623 *
624 * @param user_cb Pointer to the user callback function.
625 * @param user_data Pointer to user data.
626 *
627 * @note CONFIG_THREAD_MONITOR must be set for this function
628 * to be effective. Also this API uses irq_lock to protect the
629 * _kernel.threads list which means creation of new threads and
630 * terminations of existing threads are blocked until this
631 * API returns.
632 *
633 * @return N/A
634 */
635extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
636
Anas Nashif166f5192018-02-25 08:02:36 -0600637/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100638
639/**
Allan Stephensc98da842016-11-11 15:45:03 -0500640 * @defgroup thread_apis Thread APIs
641 * @ingroup kernel_apis
642 * @{
643 */
644
Benjamin Walshed240f22017-01-22 13:05:08 -0500645#endif /* !_ASMLANGUAGE */
646
647
648/*
649 * Thread user options. May be needed by assembly code. Common part uses low
650 * bits, arch-specific use high bits.
651 */
652
653/* system thread that must not abort */
654#define K_ESSENTIAL (1 << 0)
655
656#if defined(CONFIG_FP_SHARING)
657/* thread uses floating point registers */
658#define K_FP_REGS (1 << 1)
659#endif
660
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700661/* This thread has dropped from supervisor mode to user mode and consequently
662 * has additional restrictions
663 */
664#define K_USER (1 << 2)
665
Andrew Boie47f8fd12017-10-05 11:11:02 -0700666/* Indicates that the thread being created should inherit all kernel object
667 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
668 * is not enabled.
669 */
670#define K_INHERIT_PERMS (1 << 3)
671
Benjamin Walshed240f22017-01-22 13:05:08 -0500672#ifdef CONFIG_X86
673/* x86 Bitmask definitions for threads user options */
674
675#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
676/* thread uses SSEx (and also FP) registers */
677#define K_SSE_REGS (1 << 7)
678#endif
679#endif
680
681/* end - thread options */
682
683#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400684/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700685 * @brief Create a thread.
686 *
687 * This routine initializes a thread, then schedules it for execution.
688 *
689 * The new thread may be scheduled for immediate execution or a delayed start.
690 * If the newly spawned thread does not have a delayed start the kernel
691 * scheduler may preempt the current thread to allow the new thread to
692 * execute.
693 *
694 * Thread options are architecture-specific, and can include K_ESSENTIAL,
695 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
696 * them using "|" (the logical OR operator).
697 *
698 * Historically, users often would use the beginning of the stack memory region
699 * to store the struct k_thread data, although corruption will occur if the
700 * stack overflows this region and stack protection features may not detect this
701 * situation.
702 *
703 * @param new_thread Pointer to uninitialized struct k_thread
704 * @param stack Pointer to the stack space.
705 * @param stack_size Stack size in bytes.
706 * @param entry Thread entry function.
707 * @param p1 1st entry point parameter.
708 * @param p2 2nd entry point parameter.
709 * @param p3 3rd entry point parameter.
710 * @param prio Thread priority.
711 * @param options Thread options.
712 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
713 *
714 * @return ID of new thread.
715 */
Andrew Boie662c3452017-10-02 10:51:18 -0700716__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700717 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700718 size_t stack_size,
719 k_thread_entry_t entry,
720 void *p1, void *p2, void *p3,
721 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700722
Andrew Boie3f091b52017-08-30 14:34:14 -0700723/**
724 * @brief Drop a thread's privileges permanently to user mode
725 *
726 * @param entry Function to start executing from
727 * @param p1 1st entry point parameter
728 * @param p2 2nd entry point parameter
729 * @param p3 3rd entry point parameter
730 */
731extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
732 void *p1, void *p2,
733 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700734
Andrew Boied26cf2d2017-03-30 13:07:02 -0700735/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700736 * @brief Grant a thread access to a NULL-terminated set of kernel objects
737 *
738 * This is a convenience function. For the provided thread, grant access to
739 * the remaining arguments, which must be pointers to kernel objects.
740 * The final argument must be a NULL.
741 *
742 * The thread object must be initialized (i.e. running). The objects don't
743 * need to be.
744 *
745 * @param thread Thread to grant access to objects
746 * @param ... NULL-terminated list of kernel object pointers
747 */
748extern void __attribute__((sentinel))
749 k_thread_access_grant(struct k_thread *thread, ...);
750
751/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700752 * @brief Assign a resource memory pool to a thread
753 *
754 * By default, threads have no resource pool assigned unless their parent
755 * thread has a resource pool, in which case it is inherited. Multiple
756 * threads may be assigned to the same memory pool.
757 *
758 * Changing a thread's resource pool will not migrate allocations from the
759 * previous pool.
760 *
761 * @param thread Target thread to assign a memory pool for resource requests,
762 * or NULL if the thread should no longer have a memory pool.
763 * @param pool Memory pool to use for resources.
764 */
765static inline void k_thread_resource_pool_assign(struct k_thread *thread,
766 struct k_mem_pool *pool)
767{
768 thread->resource_pool = pool;
769}
770
771#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
772/**
773 * @brief Assign the system heap as a thread's resource pool
774 *
775 * Similar to k_thread_resource_pool_assign(), but the thread will use
776 * the kernel heap to draw memory.
777 *
778 * Use with caution, as a malicious thread could perform DoS attacks on the
779 * kernel heap.
780 *
781 * @param thread Target thread to assign the system heap for resource requests
782 */
783void k_thread_system_pool_assign(struct k_thread *thread);
784#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
785
786/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500787 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400788 *
Allan Stephensc98da842016-11-11 15:45:03 -0500789 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500790 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400791 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500792 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400793 *
794 * @return N/A
795 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700796__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400797
798/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500799 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400800 *
801 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500802 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400803 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400804 * @return N/A
805 */
Kumar Galacc334c72017-04-21 10:55:34 -0500806extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400807
808/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500809 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500811 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400812 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500813 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400814 *
815 * @return N/A
816 */
Andrew Boie468190a2017-09-29 14:00:48 -0700817__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400818
819/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500820 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400821 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500822 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500824 * If @a thread is not currently sleeping, the routine has no effect.
825 *
826 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827 *
828 * @return N/A
829 */
Andrew Boie468190a2017-09-29 14:00:48 -0700830__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400831
832/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500833 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400834 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500835 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700837__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838
839/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500840 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500842 * This routine prevents @a thread from executing if it has not yet started
843 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500845 * @param thread ID of thread to cancel.
846 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700847 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500848 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700849 *
850 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700852__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400853
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400854/**
Allan Stephensc98da842016-11-11 15:45:03 -0500855 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500857 * This routine permanently stops execution of @a thread. The thread is taken
858 * off all kernel queues it is part of (i.e. the ready queue, the timeout
859 * queue, or a kernel object wait queue). However, any kernel resources the
860 * thread might currently own (such as mutexes or memory blocks) are not
861 * released. It is the responsibility of the caller of this routine to ensure
862 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500864 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865 *
866 * @return N/A
867 */
Andrew Boie468190a2017-09-29 14:00:48 -0700868__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400869
Andrew Boie7d627c52017-08-30 11:01:56 -0700870
871/**
872 * @brief Start an inactive thread
873 *
874 * If a thread was created with K_FOREVER in the delay parameter, it will
875 * not be added to the scheduling queue until this function is called
876 * on it.
877 *
878 * @param thread thread to start
879 */
Andrew Boie468190a2017-09-29 14:00:48 -0700880__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700881
Allan Stephensc98da842016-11-11 15:45:03 -0500882/**
883 * @cond INTERNAL_HIDDEN
884 */
885
Benjamin Walshd211a522016-12-06 11:44:01 -0500886/* timeout has timed out and is not on _timeout_q anymore */
887#define _EXPIRED (-2)
888
889/* timeout is not in use */
890#define _INACTIVE (-1)
891
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400892struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700893 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700894 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400895 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700896 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500897 void *init_p1;
898 void *init_p2;
899 void *init_p3;
900 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500901 u32_t init_options;
902 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500903 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400904};
905
Andrew Boied26cf2d2017-03-30 13:07:02 -0700906#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400907 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500908 prio, options, delay, abort, groups) \
909 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700910 .init_thread = (thread), \
911 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500912 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700913 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400914 .init_p1 = (void *)p1, \
915 .init_p2 = (void *)p2, \
916 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500917 .init_prio = (prio), \
918 .init_options = (options), \
919 .init_delay = (delay), \
920 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400921 }
922
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400923/**
Allan Stephensc98da842016-11-11 15:45:03 -0500924 * INTERNAL_HIDDEN @endcond
925 */
926
927/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500928 * @brief Statically define and initialize a thread.
929 *
930 * The thread may be scheduled for immediate execution or a delayed start.
931 *
932 * Thread options are architecture-specific, and can include K_ESSENTIAL,
933 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
934 * them using "|" (the logical OR operator).
935 *
936 * The ID of the thread can be accessed using:
937 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500938 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500939 *
940 * @param name Name of the thread.
941 * @param stack_size Stack size in bytes.
942 * @param entry Thread entry function.
943 * @param p1 1st entry point parameter.
944 * @param p2 2nd entry point parameter.
945 * @param p3 3rd entry point parameter.
946 * @param prio Thread priority.
947 * @param options Thread options.
948 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400949 *
950 * @internal It has been observed that the x86 compiler by default aligns
951 * these _static_thread_data structures to 32-byte boundaries, thereby
952 * wasting space. To work around this, force a 4-byte alignment.
953 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500954#define K_THREAD_DEFINE(name, stack_size, \
955 entry, p1, p2, p3, \
956 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700957 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700958 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500959 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500960 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700961 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
962 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500963 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700964 NULL, 0); \
965 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400966
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400967/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500968 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400969 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500970 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500972 * @param thread ID of thread whose priority is needed.
973 *
974 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400975 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700976__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400977
978/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500979 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400980 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500981 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400982 *
983 * Rescheduling can occur immediately depending on the priority @a thread is
984 * set to:
985 *
986 * - If its priority is raised above the priority of the caller of this
987 * function, and the caller is preemptible, @a thread will be scheduled in.
988 *
989 * - If the caller operates on itself, it lowers its priority below that of
990 * other threads in the system, and the caller is preemptible, the thread of
991 * highest priority will be scheduled in.
992 *
993 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
994 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
995 * highest priority.
996 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500997 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400998 * @param prio New priority.
999 *
1000 * @warning Changing the priority of a thread currently involved in mutex
1001 * priority inheritance may result in undefined behavior.
1002 *
1003 * @return N/A
1004 */
Andrew Boie468190a2017-09-29 14:00:48 -07001005__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001006
Andy Ross4a2e50f2018-05-15 11:06:25 -07001007
1008#ifdef CONFIG_SCHED_DEADLINE
1009/**
1010 * @brief Set deadline expiration time for scheduler
1011 *
1012 * This sets the "deadline" expiration as a time delta from the
1013 * current time, in the same units used by k_cycle_get_32(). The
1014 * scheduler (when deadline scheduling is enabled) will choose the
1015 * next expiring thread when selecting between threads at the same
1016 * static priority. Threads at different priorities will be scheduled
1017 * according to their static priority.
1018 *
1019 * @note Deadlines that are negative (i.e. in the past) are still seen
1020 * as higher priority than others, even if the thread has "finished"
1021 * its work. If you don't want it scheduled anymore, you have to
1022 * reset the deadline into the future, block/pend the thread, or
1023 * modify its priority with k_thread_priority_set().
1024 *
1025 * @note Despite the API naming, the scheduler makes no guarantees the
1026 * the thread WILL be scheduled within that deadline, nor does it take
1027 * extra metadata (like e.g. the "runtime" and "period" parameters in
1028 * Linux sched_setattr()) that allows the kernel to validate the
1029 * scheduling for achievability. Such features could be implemented
1030 * above this call, which is simply input to the priority selection
1031 * logic.
1032 *
1033 * @param thread A thread on which to set the deadline
1034 * @param deadline A time delta, in cycle units
1035 */
1036__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1037#endif
1038
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001039/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001040 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001041 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001042 * This routine prevents the kernel scheduler from making @a thread the
1043 * current thread. All other internal operations on @a thread are still
1044 * performed; for example, any timeout it is waiting on keeps ticking,
1045 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001046 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001047 * If @a thread is already suspended, the routine has no effect.
1048 *
1049 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001050 *
1051 * @return N/A
1052 */
Andrew Boie468190a2017-09-29 14:00:48 -07001053__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001054
1055/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001056 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001058 * This routine allows the kernel scheduler to make @a thread the current
1059 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001060 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001061 * If @a thread is not currently suspended, the routine has no effect.
1062 *
1063 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001064 *
1065 * @return N/A
1066 */
Andrew Boie468190a2017-09-29 14:00:48 -07001067__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001068
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001069/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001070 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001071 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001072 * This routine specifies how the scheduler will perform time slicing of
1073 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001075 * To enable time slicing, @a slice must be non-zero. The scheduler
1076 * ensures that no thread runs for more than the specified time limit
1077 * before other threads of that priority are given a chance to execute.
1078 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001079 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001080 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001081 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001082 * execute. Once the scheduler selects a thread for execution, there is no
1083 * minimum guaranteed time the thread will execute before threads of greater or
1084 * equal priority are scheduled.
1085 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001086 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001087 * for execution, this routine has no effect; the thread is immediately
1088 * rescheduled after the slice period expires.
1089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001090 * To disable timeslicing, set both @a slice and @a prio to zero.
1091 *
1092 * @param slice Maximum time slice length (in milliseconds).
1093 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001094 *
1095 * @return N/A
1096 */
Kumar Galacc334c72017-04-21 10:55:34 -05001097extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001098
Anas Nashif166f5192018-02-25 08:02:36 -06001099/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001100
1101/**
1102 * @addtogroup isr_apis
1103 * @{
1104 */
1105
1106/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001107 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001108 *
Allan Stephensc98da842016-11-11 15:45:03 -05001109 * This routine allows the caller to customize its actions, depending on
1110 * whether it is a thread or an ISR.
1111 *
1112 * @note Can be called by ISRs.
1113 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001114 * @return 0 if invoked by a thread.
1115 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001116 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001117extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001118
Benjamin Walsh445830d2016-11-10 15:54:27 -05001119/**
1120 * @brief Determine if code is running in a preemptible thread.
1121 *
Allan Stephensc98da842016-11-11 15:45:03 -05001122 * This routine allows the caller to customize its actions, depending on
1123 * whether it can be preempted by another thread. The routine returns a 'true'
1124 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001125 *
Allan Stephensc98da842016-11-11 15:45:03 -05001126 * - The code is running in a thread, not at ISR.
1127 * - The thread's priority is in the preemptible range.
1128 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001129 *
Allan Stephensc98da842016-11-11 15:45:03 -05001130 * @note Can be called by ISRs.
1131 *
1132 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001133 * @return Non-zero if invoked by a preemptible thread.
1134 */
Andrew Boie468190a2017-09-29 14:00:48 -07001135__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001136
Allan Stephensc98da842016-11-11 15:45:03 -05001137/**
Anas Nashif166f5192018-02-25 08:02:36 -06001138 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001139 */
1140
1141/**
1142 * @addtogroup thread_apis
1143 * @{
1144 */
1145
1146/**
1147 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001148 *
Allan Stephensc98da842016-11-11 15:45:03 -05001149 * This routine prevents the current thread from being preempted by another
1150 * thread by instructing the scheduler to treat it as a cooperative thread.
1151 * If the thread subsequently performs an operation that makes it unready,
1152 * it will be context switched out in the normal manner. When the thread
1153 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001154 *
Allan Stephensc98da842016-11-11 15:45:03 -05001155 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001156 *
Allan Stephensc98da842016-11-11 15:45:03 -05001157 * @note k_sched_lock() and k_sched_unlock() should normally be used
1158 * when the operation being performed can be safely interrupted by ISRs.
1159 * However, if the amount of processing involved is very small, better
1160 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001161 *
1162 * @return N/A
1163 */
1164extern void k_sched_lock(void);
1165
Allan Stephensc98da842016-11-11 15:45:03 -05001166/**
1167 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001168 *
Allan Stephensc98da842016-11-11 15:45:03 -05001169 * This routine reverses the effect of a previous call to k_sched_lock().
1170 * A thread must call the routine once for each time it called k_sched_lock()
1171 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001172 *
1173 * @return N/A
1174 */
1175extern void k_sched_unlock(void);
1176
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001177/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001178 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001180 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001182 * Custom data is not used by the kernel itself, and is freely available
1183 * for a thread to use as it sees fit. It can be used as a framework
1184 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001185 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001186 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001187 *
1188 * @return N/A
1189 */
Andrew Boie468190a2017-09-29 14:00:48 -07001190__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001191
1192/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001193 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001195 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001196 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001197 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001198 */
Andrew Boie468190a2017-09-29 14:00:48 -07001199__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001200
1201/**
Anas Nashif166f5192018-02-25 08:02:36 -06001202 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001203 */
1204
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001205#include <sys_clock.h>
1206
Allan Stephensc2f15a42016-11-17 12:24:22 -05001207/**
1208 * @addtogroup clock_apis
1209 * @{
1210 */
1211
1212/**
1213 * @brief Generate null timeout delay.
1214 *
1215 * This macro generates a timeout delay that that instructs a kernel API
1216 * not to wait if the requested operation cannot be performed immediately.
1217 *
1218 * @return Timeout delay value.
1219 */
1220#define K_NO_WAIT 0
1221
1222/**
1223 * @brief Generate timeout delay from milliseconds.
1224 *
1225 * This macro generates a timeout delay that that instructs a kernel API
1226 * to wait up to @a ms milliseconds to perform the requested operation.
1227 *
1228 * @param ms Duration in milliseconds.
1229 *
1230 * @return Timeout delay value.
1231 */
Johan Hedberg14471692016-11-13 10:52:15 +02001232#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001233
1234/**
1235 * @brief Generate timeout delay from seconds.
1236 *
1237 * This macro generates a timeout delay that that instructs a kernel API
1238 * to wait up to @a s seconds to perform the requested operation.
1239 *
1240 * @param s Duration in seconds.
1241 *
1242 * @return Timeout delay value.
1243 */
Johan Hedberg14471692016-11-13 10:52:15 +02001244#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001245
1246/**
1247 * @brief Generate timeout delay from minutes.
1248 *
1249 * This macro generates a timeout delay that that instructs a kernel API
1250 * to wait up to @a m minutes to perform the requested operation.
1251 *
1252 * @param m Duration in minutes.
1253 *
1254 * @return Timeout delay value.
1255 */
Johan Hedberg14471692016-11-13 10:52:15 +02001256#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001257
1258/**
1259 * @brief Generate timeout delay from hours.
1260 *
1261 * This macro generates a timeout delay that that instructs a kernel API
1262 * to wait up to @a h hours to perform the requested operation.
1263 *
1264 * @param h Duration in hours.
1265 *
1266 * @return Timeout delay value.
1267 */
Johan Hedberg14471692016-11-13 10:52:15 +02001268#define K_HOURS(h) K_MINUTES((h) * 60)
1269
Allan Stephensc98da842016-11-11 15:45:03 -05001270/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001271 * @brief Generate infinite timeout delay.
1272 *
1273 * This macro generates a timeout delay that that instructs a kernel API
1274 * to wait as long as necessary to perform the requested operation.
1275 *
1276 * @return Timeout delay value.
1277 */
1278#define K_FOREVER (-1)
1279
1280/**
Anas Nashif166f5192018-02-25 08:02:36 -06001281 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001282 */
1283
1284/**
Allan Stephensc98da842016-11-11 15:45:03 -05001285 * @cond INTERNAL_HIDDEN
1286 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001287
Benjamin Walsh62092182016-12-20 14:39:08 -05001288/* kernel clocks */
1289
1290#if (sys_clock_ticks_per_sec == 1000) || \
1291 (sys_clock_ticks_per_sec == 500) || \
1292 (sys_clock_ticks_per_sec == 250) || \
1293 (sys_clock_ticks_per_sec == 125) || \
1294 (sys_clock_ticks_per_sec == 100) || \
1295 (sys_clock_ticks_per_sec == 50) || \
1296 (sys_clock_ticks_per_sec == 25) || \
1297 (sys_clock_ticks_per_sec == 20) || \
1298 (sys_clock_ticks_per_sec == 10) || \
1299 (sys_clock_ticks_per_sec == 1)
1300
1301 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1302#else
1303 /* yields horrible 64-bit math on many architectures: try to avoid */
1304 #define _NON_OPTIMIZED_TICKS_PER_SEC
1305#endif
1306
1307#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001308extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001309#else
Kumar Galacc334c72017-04-21 10:55:34 -05001310static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001311{
Kumar Galacc334c72017-04-21 10:55:34 -05001312 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001313}
1314#endif
1315
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001316/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001317#ifdef CONFIG_TICKLESS_KERNEL
1318#define _TICK_ALIGN 0
1319#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001320#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001321#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001322
Kumar Galacc334c72017-04-21 10:55:34 -05001323static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001324{
Benjamin Walsh62092182016-12-20 14:39:08 -05001325#ifdef CONFIG_SYS_CLOCK_EXISTS
1326
1327#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001328 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001329#else
Kumar Galacc334c72017-04-21 10:55:34 -05001330 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001331#endif
1332
1333#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001334 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001335 return 0;
1336#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001337}
1338
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001339struct k_timer {
1340 /*
1341 * _timeout structure must be first here if we want to use
1342 * dynamic timer allocation. timeout.node is used in the double-linked
1343 * list of free timers
1344 */
1345 struct _timeout timeout;
1346
Allan Stephens45bfa372016-10-12 12:39:42 -05001347 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001348 _wait_q_t wait_q;
1349
1350 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001351 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001352
1353 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001354 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001355
1356 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001357 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001358
Allan Stephens45bfa372016-10-12 12:39:42 -05001359 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001360 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001361
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001362 /* user-specific data, also used to support legacy features */
1363 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001364
Anas Nashif2f203c22016-12-18 06:57:45 -05001365 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001366};
1367
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001368#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001369 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001370 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001371 .timeout.wait_q = NULL, \
1372 .timeout.thread = NULL, \
1373 .timeout.func = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001374 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001375 .expiry_fn = expiry, \
1376 .stop_fn = stop, \
1377 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001378 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001379 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001380 }
1381
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001382#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1383
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001384/**
Allan Stephensc98da842016-11-11 15:45:03 -05001385 * INTERNAL_HIDDEN @endcond
1386 */
1387
1388/**
1389 * @defgroup timer_apis Timer APIs
1390 * @ingroup kernel_apis
1391 * @{
1392 */
1393
1394/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001395 * @typedef k_timer_expiry_t
1396 * @brief Timer expiry function type.
1397 *
1398 * A timer's expiry function is executed by the system clock interrupt handler
1399 * each time the timer expires. The expiry function is optional, and is only
1400 * invoked if the timer has been initialized with one.
1401 *
1402 * @param timer Address of timer.
1403 *
1404 * @return N/A
1405 */
1406typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1407
1408/**
1409 * @typedef k_timer_stop_t
1410 * @brief Timer stop function type.
1411 *
1412 * A timer's stop function is executed if the timer is stopped prematurely.
1413 * The function runs in the context of the thread that stops the timer.
1414 * The stop function is optional, and is only invoked if the timer has been
1415 * initialized with one.
1416 *
1417 * @param timer Address of timer.
1418 *
1419 * @return N/A
1420 */
1421typedef void (*k_timer_stop_t)(struct k_timer *timer);
1422
1423/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001424 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001426 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001427 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001428 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001429 *
1430 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001431 * @param expiry_fn Function to invoke each time the timer expires.
1432 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001433 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001434#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001435 struct k_timer name \
1436 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001437 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001438
Allan Stephens45bfa372016-10-12 12:39:42 -05001439/**
1440 * @brief Initialize a timer.
1441 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001442 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001443 *
1444 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001445 * @param expiry_fn Function to invoke each time the timer expires.
1446 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001447 *
1448 * @return N/A
1449 */
1450extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001451 k_timer_expiry_t expiry_fn,
1452 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001453
Allan Stephens45bfa372016-10-12 12:39:42 -05001454/**
1455 * @brief Start a timer.
1456 *
1457 * This routine starts a timer, and resets its status to zero. The timer
1458 * begins counting down using the specified duration and period values.
1459 *
1460 * Attempting to start a timer that is already running is permitted.
1461 * The timer's status is reset to zero and the timer begins counting down
1462 * using the new duration and period values.
1463 *
1464 * @param timer Address of timer.
1465 * @param duration Initial timer duration (in milliseconds).
1466 * @param period Timer period (in milliseconds).
1467 *
1468 * @return N/A
1469 */
Andrew Boiea354d492017-09-29 16:22:28 -07001470__syscall void k_timer_start(struct k_timer *timer,
1471 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001472
1473/**
1474 * @brief Stop a timer.
1475 *
1476 * This routine stops a running timer prematurely. The timer's stop function,
1477 * if one exists, is invoked by the caller.
1478 *
1479 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001480 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001481 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001482 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1483 * if @a k_timer_stop is to be called from ISRs.
1484 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001485 * @param timer Address of timer.
1486 *
1487 * @return N/A
1488 */
Andrew Boiea354d492017-09-29 16:22:28 -07001489__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001490
1491/**
1492 * @brief Read timer status.
1493 *
1494 * This routine reads the timer's status, which indicates the number of times
1495 * it has expired since its status was last read.
1496 *
1497 * Calling this routine resets the timer's status to zero.
1498 *
1499 * @param timer Address of timer.
1500 *
1501 * @return Timer status.
1502 */
Andrew Boiea354d492017-09-29 16:22:28 -07001503__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001504
1505/**
1506 * @brief Synchronize thread to timer expiration.
1507 *
1508 * This routine blocks the calling thread until the timer's status is non-zero
1509 * (indicating that it has expired at least once since it was last examined)
1510 * or the timer is stopped. If the timer status is already non-zero,
1511 * or the timer is already stopped, the caller continues without waiting.
1512 *
1513 * Calling this routine resets the timer's status to zero.
1514 *
1515 * This routine must not be used by interrupt handlers, since they are not
1516 * allowed to block.
1517 *
1518 * @param timer Address of timer.
1519 *
1520 * @return Timer status.
1521 */
Andrew Boiea354d492017-09-29 16:22:28 -07001522__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001523
1524/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001525 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001526 *
1527 * This routine computes the (approximate) time remaining before a running
1528 * timer next expires. If the timer is not running, it returns zero.
1529 *
1530 * @param timer Address of timer.
1531 *
1532 * @return Remaining time (in milliseconds).
1533 */
Andrew Boiea354d492017-09-29 16:22:28 -07001534__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1535
1536static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001537{
1538 return _timeout_remaining_get(&timer->timeout);
1539}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001540
Allan Stephensc98da842016-11-11 15:45:03 -05001541/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001542 * @brief Associate user-specific data with a timer.
1543 *
1544 * This routine records the @a user_data with the @a timer, to be retrieved
1545 * later.
1546 *
1547 * It can be used e.g. in a timer handler shared across multiple subsystems to
1548 * retrieve data specific to the subsystem this timer is associated with.
1549 *
1550 * @param timer Address of timer.
1551 * @param user_data User data to associate with the timer.
1552 *
1553 * @return N/A
1554 */
Andrew Boiea354d492017-09-29 16:22:28 -07001555__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1556
Anas Nashif954d5502018-02-25 08:37:28 -06001557/**
1558 * @internal
1559 */
Andrew Boiea354d492017-09-29 16:22:28 -07001560static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1561 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001562{
1563 timer->user_data = user_data;
1564}
1565
1566/**
1567 * @brief Retrieve the user-specific data from a timer.
1568 *
1569 * @param timer Address of timer.
1570 *
1571 * @return The user data.
1572 */
Andrew Boiea354d492017-09-29 16:22:28 -07001573__syscall void *k_timer_user_data_get(struct k_timer *timer);
1574
1575static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001576{
1577 return timer->user_data;
1578}
1579
Anas Nashif166f5192018-02-25 08:02:36 -06001580/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001581
Allan Stephensc98da842016-11-11 15:45:03 -05001582/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001583 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001584 * @{
1585 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001586
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001587/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001588 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001590 * This routine returns the elapsed time since the system booted,
1591 * in milliseconds.
1592 *
1593 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001594 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001595__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001596
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001597/**
1598 * @brief Enable clock always on in tickless kernel
1599 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001600 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001601 * there are no timer events programmed in tickless kernel
1602 * scheduling. This is necessary if the clock is used to track
1603 * passage of time.
1604 *
1605 * @retval prev_status Previous status of always on flag
1606 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301607#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001608static inline int k_enable_sys_clock_always_on(void)
1609{
1610 int prev_status = _sys_clock_always_on;
1611
1612 _sys_clock_always_on = 1;
1613 _enable_sys_clock();
1614
1615 return prev_status;
1616}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301617#else
1618#define k_enable_sys_clock_always_on() do { } while ((0))
1619#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001620
1621/**
1622 * @brief Disable clock always on in tickless kernel
1623 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001624 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001625 * there are no timer events programmed in tickless kernel
1626 * scheduling. To save power, this routine should be called
1627 * immediately when clock is not used to track time.
1628 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301629#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001630static inline void k_disable_sys_clock_always_on(void)
1631{
1632 _sys_clock_always_on = 0;
1633}
1634#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001635#define k_disable_sys_clock_always_on() do { } while ((0))
1636#endif
1637
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001638/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001639 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001640 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001641 * This routine returns the lower 32-bits of the elapsed time since the system
1642 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001644 * This routine can be more efficient than k_uptime_get(), as it reduces the
1645 * need for interrupt locking and 64-bit math. However, the 32-bit result
1646 * cannot hold a system uptime time larger than approximately 50 days, so the
1647 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001648 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001649 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001650 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001651__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001652
1653/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001654 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001656 * This routine computes the elapsed time between the current system uptime
1657 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001658 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001659 * @param reftime Pointer to a reference time, which is updated to the current
1660 * uptime upon return.
1661 *
1662 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001663 */
Kumar Galacc334c72017-04-21 10:55:34 -05001664extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001665
1666/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001667 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001668 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001669 * This routine computes the elapsed time between the current system uptime
1670 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001671 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001672 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1673 * need for interrupt locking and 64-bit math. However, the 32-bit result
1674 * cannot hold an elapsed time larger than approximately 50 days, so the
1675 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001676 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001677 * @param reftime Pointer to a reference time, which is updated to the current
1678 * uptime upon return.
1679 *
1680 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001681 */
Kumar Galacc334c72017-04-21 10:55:34 -05001682extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001683
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001684/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001685 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001686 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001687 * This routine returns the current time, as measured by the system's hardware
1688 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001690 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001691 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001692#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001693
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001694/**
Anas Nashif166f5192018-02-25 08:02:36 -06001695 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001696 */
1697
Allan Stephensc98da842016-11-11 15:45:03 -05001698/**
1699 * @cond INTERNAL_HIDDEN
1700 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001701
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001702struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001703 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001704 union {
1705 _wait_q_t wait_q;
1706
1707 _POLL_EVENT;
1708 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001709
1710 _OBJECT_TRACING_NEXT_PTR(k_queue);
1711};
1712
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001713#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001714 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001715 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001716 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001717 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001718 _OBJECT_TRACING_INIT \
1719 }
1720
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001721#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1722
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001723extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1724
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001725/**
1726 * INTERNAL_HIDDEN @endcond
1727 */
1728
1729/**
1730 * @defgroup queue_apis Queue APIs
1731 * @ingroup kernel_apis
1732 * @{
1733 */
1734
1735/**
1736 * @brief Initialize a queue.
1737 *
1738 * This routine initializes a queue object, prior to its first use.
1739 *
1740 * @param queue Address of the queue.
1741 *
1742 * @return N/A
1743 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001744__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001745
1746/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001747 * @brief Cancel waiting on a queue.
1748 *
1749 * This routine causes first thread pending on @a queue, if any, to
1750 * return from k_queue_get() call with NULL value (as if timeout expired).
1751 *
1752 * @note Can be called by ISRs.
1753 *
1754 * @param queue Address of the queue.
1755 *
1756 * @return N/A
1757 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001758__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001759
1760/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001761 * @brief Append an element to the end of a queue.
1762 *
1763 * This routine appends a data item to @a queue. A queue data item must be
1764 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1765 * reserved for the kernel's use.
1766 *
1767 * @note Can be called by ISRs.
1768 *
1769 * @param queue Address of the queue.
1770 * @param data Address of the data item.
1771 *
1772 * @return N/A
1773 */
1774extern void k_queue_append(struct k_queue *queue, void *data);
1775
1776/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001777 * @brief Append an element to a queue.
1778 *
1779 * This routine appends a data item to @a queue. There is an implicit
1780 * memory allocation from the calling thread's resource pool, which is
1781 * automatically freed when the item is removed from the queue.
1782 *
1783 * @note Can be called by ISRs.
1784 *
1785 * @param queue Address of the queue.
1786 * @param data Address of the data item.
1787 *
1788 * @retval 0 on success
1789 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1790 */
1791__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1792
1793/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001794 * @brief Prepend an element to a queue.
1795 *
1796 * This routine prepends a data item to @a queue. A queue data item must be
1797 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1798 * reserved for the kernel's use.
1799 *
1800 * @note Can be called by ISRs.
1801 *
1802 * @param queue Address of the queue.
1803 * @param data Address of the data item.
1804 *
1805 * @return N/A
1806 */
1807extern void k_queue_prepend(struct k_queue *queue, void *data);
1808
1809/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001810 * @brief Prepend an element to a queue.
1811 *
1812 * This routine prepends a data item to @a queue. There is an implicit
1813 * memory allocation from the calling thread's resource pool, which is
1814 * automatically freed when the item is removed from the queue.
1815 *
1816 * @note Can be called by ISRs.
1817 *
1818 * @param queue Address of the queue.
1819 * @param data Address of the data item.
1820 *
1821 * @retval 0 on success
1822 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1823 */
1824__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1825
1826/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001827 * @brief Inserts an element to a queue.
1828 *
1829 * This routine inserts a data item to @a queue after previous item. A queue
1830 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1831 * item are reserved for the kernel's use.
1832 *
1833 * @note Can be called by ISRs.
1834 *
1835 * @param queue Address of the queue.
1836 * @param prev Address of the previous data item.
1837 * @param data Address of the data item.
1838 *
1839 * @return N/A
1840 */
1841extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1842
1843/**
1844 * @brief Atomically append a list of elements to a queue.
1845 *
1846 * This routine adds a list of data items to @a queue in one operation.
1847 * The data items must be in a singly-linked list, with the first 32 bits
1848 * in each data item pointing to the next data item; the list must be
1849 * NULL-terminated.
1850 *
1851 * @note Can be called by ISRs.
1852 *
1853 * @param queue Address of the queue.
1854 * @param head Pointer to first node in singly-linked list.
1855 * @param tail Pointer to last node in singly-linked list.
1856 *
1857 * @return N/A
1858 */
1859extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1860
1861/**
1862 * @brief Atomically add a list of elements to a queue.
1863 *
1864 * This routine adds a list of data items to @a queue in one operation.
1865 * The data items must be in a singly-linked list implemented using a
1866 * sys_slist_t object. Upon completion, the original list is empty.
1867 *
1868 * @note Can be called by ISRs.
1869 *
1870 * @param queue Address of the queue.
1871 * @param list Pointer to sys_slist_t object.
1872 *
1873 * @return N/A
1874 */
1875extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1876
1877/**
1878 * @brief Get an element from a queue.
1879 *
1880 * This routine removes first data item from @a queue. The first 32 bits of the
1881 * data item are reserved for the kernel's use.
1882 *
1883 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1884 *
1885 * @param queue Address of the queue.
1886 * @param timeout Waiting period to obtain a data item (in milliseconds),
1887 * or one of the special values K_NO_WAIT and K_FOREVER.
1888 *
1889 * @return Address of the data item if successful; NULL if returned
1890 * without waiting, or waiting period timed out.
1891 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001892__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001893
1894/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001895 * @brief Remove an element from a queue.
1896 *
1897 * This routine removes data item from @a queue. The first 32 bits of the
1898 * data item are reserved for the kernel's use. Removing elements from k_queue
1899 * rely on sys_slist_find_and_remove which is not a constant time operation.
1900 *
1901 * @note Can be called by ISRs
1902 *
1903 * @param queue Address of the queue.
1904 * @param data Address of the data item.
1905 *
1906 * @return true if data item was removed
1907 */
1908static inline bool k_queue_remove(struct k_queue *queue, void *data)
1909{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001910 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001911}
1912
1913/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001914 * @brief Query a queue to see if it has data available.
1915 *
1916 * Note that the data might be already gone by the time this function returns
1917 * if other threads are also trying to read from the queue.
1918 *
1919 * @note Can be called by ISRs.
1920 *
1921 * @param queue Address of the queue.
1922 *
1923 * @return Non-zero if the queue is empty.
1924 * @return 0 if data is available.
1925 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001926__syscall int k_queue_is_empty(struct k_queue *queue);
1927
1928static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001929{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001930 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001931}
1932
1933/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001934 * @brief Peek element at the head of queue.
1935 *
1936 * Return element from the head of queue without removing it.
1937 *
1938 * @param queue Address of the queue.
1939 *
1940 * @return Head element, or NULL if queue is empty.
1941 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001942__syscall void *k_queue_peek_head(struct k_queue *queue);
1943
1944static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001945{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001946 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001947}
1948
1949/**
1950 * @brief Peek element at the tail of queue.
1951 *
1952 * Return element from the tail of queue without removing it.
1953 *
1954 * @param queue Address of the queue.
1955 *
1956 * @return Tail element, or NULL if queue is empty.
1957 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001958__syscall void *k_queue_peek_tail(struct k_queue *queue);
1959
1960static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001961{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001962 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001963}
1964
1965/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001966 * @brief Statically define and initialize a queue.
1967 *
1968 * The queue can be accessed outside the module where it is defined using:
1969 *
1970 * @code extern struct k_queue <name>; @endcode
1971 *
1972 * @param name Name of the queue.
1973 */
1974#define K_QUEUE_DEFINE(name) \
1975 struct k_queue name \
1976 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001977 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001978
Anas Nashif166f5192018-02-25 08:02:36 -06001979/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001980
1981/**
1982 * @cond INTERNAL_HIDDEN
1983 */
1984
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001985struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001986 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001987};
1988
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001989#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001990 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001991 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001992 }
1993
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001994#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1995
Allan Stephensc98da842016-11-11 15:45:03 -05001996/**
1997 * INTERNAL_HIDDEN @endcond
1998 */
1999
2000/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002001 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002002 * @ingroup kernel_apis
2003 * @{
2004 */
2005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002006/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002007 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002008 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002009 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002010 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002011 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002012 *
2013 * @return N/A
2014 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002015#define k_fifo_init(fifo) \
2016 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002017
2018/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002019 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002020 *
2021 * This routine causes first thread pending on @a fifo, if any, to
2022 * return from k_fifo_get() call with NULL value (as if timeout
2023 * expired).
2024 *
2025 * @note Can be called by ISRs.
2026 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002027 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002028 *
2029 * @return N/A
2030 */
2031#define k_fifo_cancel_wait(fifo) \
2032 k_queue_cancel_wait((struct k_queue *) fifo)
2033
2034/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002035 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002036 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002037 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002038 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2039 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002040 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002041 * @note Can be called by ISRs.
2042 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002043 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002044 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002045 *
2046 * @return N/A
2047 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002048#define k_fifo_put(fifo, data) \
2049 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002050
2051/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002052 * @brief Add an element to a FIFO queue.
2053 *
2054 * This routine adds a data item to @a fifo. There is an implicit
2055 * memory allocation from the calling thread's resource pool, which is
2056 * automatically freed when the item is removed.
2057 *
2058 * @note Can be called by ISRs.
2059 *
2060 * @param fifo Address of the FIFO.
2061 * @param data Address of the data item.
2062 *
2063 * @retval 0 on success
2064 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2065 */
2066#define k_fifo_alloc_put(fifo, data) \
2067 k_queue_alloc_append((struct k_queue *) fifo, data)
2068
2069/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002070 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002071 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002072 * This routine adds a list of data items to @a fifo in one operation.
2073 * The data items must be in a singly-linked list, with the first 32 bits
2074 * each data item pointing to the next data item; the list must be
2075 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002076 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002077 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002078 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002079 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002080 * @param head Pointer to first node in singly-linked list.
2081 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002082 *
2083 * @return N/A
2084 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002085#define k_fifo_put_list(fifo, head, tail) \
2086 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002087
2088/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002089 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002090 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002091 * This routine adds a list of data items to @a fifo in one operation.
2092 * The data items must be in a singly-linked list implemented using a
2093 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002094 * and must be re-initialized via sys_slist_init().
2095 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002096 * @note Can be called by ISRs.
2097 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002098 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002099 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002100 *
2101 * @return N/A
2102 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002103#define k_fifo_put_slist(fifo, list) \
2104 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002105
2106/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002107 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002108 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002109 * This routine removes a data item from @a fifo in a "first in, first out"
2110 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002112 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2113 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002114 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002115 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002116 * or one of the special values K_NO_WAIT and K_FOREVER.
2117 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002118 * @return Address of the data item if successful; NULL if returned
2119 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002120 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002121#define k_fifo_get(fifo, timeout) \
2122 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002123
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002124/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002125 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002126 *
2127 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002128 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002129 *
2130 * @note Can be called by ISRs.
2131 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002132 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002133 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002134 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002135 * @return 0 if data is available.
2136 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002137#define k_fifo_is_empty(fifo) \
2138 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002139
2140/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002141 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002142 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002143 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302144 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002145 * on each iteration of processing, a head container will be peeked,
2146 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002147 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002148 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002149 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002150 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002151 * @return Head element, or NULL if the FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002152 */
2153#define k_fifo_peek_head(fifo) \
2154 k_queue_peek_head((struct k_queue *) fifo)
2155
2156/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002157 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002158 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002159 * Return element from the tail of FIFO queue (without removing it). A usecase
2160 * for this is if elements of the FIFO queue are themselves containers. Then
2161 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002162 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002163 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002164 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002165 * @return Tail element, or NULL if a FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002166 */
2167#define k_fifo_peek_tail(fifo) \
2168 k_queue_peek_tail((struct k_queue *) fifo)
2169
2170/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002171 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002172 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002173 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002174 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002175 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002176 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002177 * @param name Name of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002178 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002179#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002180 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002181 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002182 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002183
Anas Nashif166f5192018-02-25 08:02:36 -06002184/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002185
2186/**
2187 * @cond INTERNAL_HIDDEN
2188 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002189
2190struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002191 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002192};
2193
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002194#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002195 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002196 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002197 }
2198
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002199#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2200
Allan Stephensc98da842016-11-11 15:45:03 -05002201/**
2202 * INTERNAL_HIDDEN @endcond
2203 */
2204
2205/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002206 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002207 * @ingroup kernel_apis
2208 * @{
2209 */
2210
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002211/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002212 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002213 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002214 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002215 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002216 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002217 *
2218 * @return N/A
2219 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002220#define k_lifo_init(lifo) \
2221 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002222
2223/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002224 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002225 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002226 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002227 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2228 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002229 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002230 * @note Can be called by ISRs.
2231 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002232 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002233 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002234 *
2235 * @return N/A
2236 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002237#define k_lifo_put(lifo, data) \
2238 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002239
2240/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002241 * @brief Add an element to a LIFO queue.
2242 *
2243 * This routine adds a data item to @a lifo. There is an implicit
2244 * memory allocation from the calling thread's resource pool, which is
2245 * automatically freed when the item is removed.
2246 *
2247 * @note Can be called by ISRs.
2248 *
2249 * @param lifo Address of the LIFO.
2250 * @param data Address of the data item.
2251 *
2252 * @retval 0 on success
2253 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2254 */
2255#define k_lifo_alloc_put(lifo, data) \
2256 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2257
2258/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002259 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002260 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002261 * This routine removes a data item from @a lifo in a "last in, first out"
2262 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002263 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002264 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2265 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002266 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002267 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002268 * or one of the special values K_NO_WAIT and K_FOREVER.
2269 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002270 * @return Address of the data item if successful; NULL if returned
2271 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002272 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002273#define k_lifo_get(lifo, timeout) \
2274 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002275
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002276/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002277 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002278 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002279 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002280 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002281 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002282 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002283 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002284 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002285#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002286 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002287 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002288 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002289
Anas Nashif166f5192018-02-25 08:02:36 -06002290/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002291
2292/**
2293 * @cond INTERNAL_HIDDEN
2294 */
Andrew Boief3bee952018-05-02 17:44:39 -07002295#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002296
2297struct k_stack {
2298 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002299 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002300
Anas Nashif2f203c22016-12-18 06:57:45 -05002301 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002302 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002303};
2304
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002305#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002306 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002307 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002308 .base = stack_buffer, \
2309 .next = stack_buffer, \
2310 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002311 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002312 }
2313
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002314#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2315
Allan Stephensc98da842016-11-11 15:45:03 -05002316/**
2317 * INTERNAL_HIDDEN @endcond
2318 */
2319
2320/**
2321 * @defgroup stack_apis Stack APIs
2322 * @ingroup kernel_apis
2323 * @{
2324 */
2325
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002326/**
2327 * @brief Initialize a stack.
2328 *
2329 * This routine initializes a stack object, prior to its first use.
2330 *
2331 * @param stack Address of the stack.
2332 * @param buffer Address of array used to hold stacked values.
2333 * @param num_entries Maximum number of values that can be stacked.
2334 *
2335 * @return N/A
2336 */
Andrew Boief3bee952018-05-02 17:44:39 -07002337void k_stack_init(struct k_stack *stack,
2338 u32_t *buffer, unsigned int num_entries);
2339
2340
2341/**
2342 * @brief Initialize a stack.
2343 *
2344 * This routine initializes a stack object, prior to its first use. Internal
2345 * buffers will be allocated from the calling thread's resource pool.
2346 * This memory will be released if k_stack_cleanup() is called, or
2347 * userspace is enabled and the stack object loses all references to it.
2348 *
2349 * @param stack Address of the stack.
2350 * @param num_entries Maximum number of values that can be stacked.
2351 *
2352 * @return -ENOMEM if memory couldn't be allocated
2353 */
2354
2355__syscall int k_stack_alloc_init(struct k_stack *stack,
2356 unsigned int num_entries);
2357
2358/**
2359 * @brief Release a stack's allocated buffer
2360 *
2361 * If a stack object was given a dynamically allocated buffer via
2362 * k_stack_alloc_init(), this will free it. This function does nothing
2363 * if the buffer wasn't dynamically allocated.
2364 *
2365 * @param stack Address of the stack.
2366 */
2367void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002368
2369/**
2370 * @brief Push an element onto a stack.
2371 *
2372 * This routine adds a 32-bit value @a data to @a stack.
2373 *
2374 * @note Can be called by ISRs.
2375 *
2376 * @param stack Address of the stack.
2377 * @param data Value to push onto the stack.
2378 *
2379 * @return N/A
2380 */
Andrew Boiee8734462017-09-29 16:42:07 -07002381__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002382
2383/**
2384 * @brief Pop an element from a stack.
2385 *
2386 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2387 * manner and stores the value in @a data.
2388 *
2389 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2390 *
2391 * @param stack Address of the stack.
2392 * @param data Address of area to hold the value popped from the stack.
2393 * @param timeout Waiting period to obtain a value (in milliseconds),
2394 * or one of the special values K_NO_WAIT and K_FOREVER.
2395 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002396 * @retval 0 Element popped from stack.
2397 * @retval -EBUSY Returned without waiting.
2398 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002399 */
Andrew Boiee8734462017-09-29 16:42:07 -07002400__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002401
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002402/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002403 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002404 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002405 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002406 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002407 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002409 * @param name Name of the stack.
2410 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002411 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002412#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002413 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002414 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002415 struct k_stack name \
2416 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002417 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002418 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002419
Anas Nashif166f5192018-02-25 08:02:36 -06002420/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002421
Allan Stephens6bba9b02016-11-16 14:56:54 -05002422struct k_work;
2423
Allan Stephensc98da842016-11-11 15:45:03 -05002424/**
2425 * @defgroup workqueue_apis Workqueue Thread APIs
2426 * @ingroup kernel_apis
2427 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002428 */
2429
Allan Stephens6bba9b02016-11-16 14:56:54 -05002430/**
2431 * @typedef k_work_handler_t
2432 * @brief Work item handler function type.
2433 *
2434 * A work item's handler function is executed by a workqueue's thread
2435 * when the work item is processed by the workqueue.
2436 *
2437 * @param work Address of the work item.
2438 *
2439 * @return N/A
2440 */
2441typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002442
2443/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002444 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002445 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002446
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002447struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002448 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002449 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002450};
2451
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002452enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002453 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002454};
2455
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002456struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002457 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002458 k_work_handler_t handler;
2459 atomic_t flags[1];
2460};
2461
Allan Stephens6bba9b02016-11-16 14:56:54 -05002462struct k_delayed_work {
2463 struct k_work work;
2464 struct _timeout timeout;
2465 struct k_work_q *work_q;
2466};
2467
2468extern struct k_work_q k_sys_work_q;
2469
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002470/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002471 * INTERNAL_HIDDEN @endcond
2472 */
2473
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002474#define _K_WORK_INITIALIZER(work_handler) \
2475 { \
2476 ._reserved = NULL, \
2477 .handler = work_handler, \
2478 .flags = { 0 } \
2479 }
2480
2481#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2482
Allan Stephens6bba9b02016-11-16 14:56:54 -05002483/**
2484 * @brief Initialize a statically-defined work item.
2485 *
2486 * This macro can be used to initialize a statically-defined workqueue work
2487 * item, prior to its first use. For example,
2488 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002489 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002490 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002491 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002492 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002493 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002494#define K_WORK_DEFINE(work, work_handler) \
2495 struct k_work work \
2496 __in_section(_k_work, static, work) = \
2497 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002498
2499/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002500 * @brief Initialize a work item.
2501 *
2502 * This routine initializes a workqueue work item, prior to its first use.
2503 *
2504 * @param work Address of work item.
2505 * @param handler Function to invoke each time work item is processed.
2506 *
2507 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002508 */
2509static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2510{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002511 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002512 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002513 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002514}
2515
2516/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002517 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002518 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002519 * This routine submits work item @a work to be processed by workqueue
2520 * @a work_q. If the work item is already pending in the workqueue's queue
2521 * as a result of an earlier submission, this routine has no effect on the
2522 * work item. If the work item has already been processed, or is currently
2523 * being processed, its work is considered complete and the work item can be
2524 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002525 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002526 * @warning
2527 * A submitted work item must not be modified until it has been processed
2528 * by the workqueue.
2529 *
2530 * @note Can be called by ISRs.
2531 *
2532 * @param work_q Address of workqueue.
2533 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002534 *
2535 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002536 */
2537static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2538 struct k_work *work)
2539{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002540 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002541 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002542 }
2543}
2544
2545/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002546 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002547 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002548 * This routine indicates if work item @a work is pending in a workqueue's
2549 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002550 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002551 * @note Can be called by ISRs.
2552 *
2553 * @param work Address of work item.
2554 *
2555 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002556 */
2557static inline int k_work_pending(struct k_work *work)
2558{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002559 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002560}
2561
2562/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002563 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002564 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002565 * This routine starts workqueue @a work_q. The workqueue spawns its work
2566 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002567 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002568 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002569 * @param stack Pointer to work queue thread's stack space, as defined by
2570 * K_THREAD_STACK_DEFINE()
2571 * @param stack_size Size of the work queue thread's stack (in bytes), which
2572 * should either be the same constant passed to
2573 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002574 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002575 *
2576 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002577 */
Andrew Boie507852a2017-07-25 18:47:07 -07002578extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002579 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002580 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002581
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002582/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002583 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002584 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002585 * This routine initializes a workqueue delayed work item, prior to
2586 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002587 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002588 * @param work Address of delayed work item.
2589 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002590 *
2591 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002592 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002593extern void k_delayed_work_init(struct k_delayed_work *work,
2594 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002595
2596/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002597 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002598 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002599 * This routine schedules work item @a work to be processed by workqueue
2600 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002601 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002602 * Only when the countdown completes is the work item actually submitted to
2603 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002604 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002605 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002606 * counting down cancels the existing submission and restarts the
2607 * countdown using the new delay. Note that this behavior is
2608 * inherently subject to race conditions with the pre-existing
2609 * timeouts and work queue, so care must be taken to synchronize such
2610 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002611 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002612 * @warning
2613 * A delayed work item must not be modified until it has been processed
2614 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002615 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002616 * @note Can be called by ISRs.
2617 *
2618 * @param work_q Address of workqueue.
2619 * @param work Address of delayed work item.
2620 * @param delay Delay before submitting the work item (in milliseconds).
2621 *
2622 * @retval 0 Work item countdown started.
2623 * @retval -EINPROGRESS Work item is already pending.
2624 * @retval -EINVAL Work item is being processed or has completed its work.
2625 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002626 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002627extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2628 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002629 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002630
2631/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002632 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002633 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002634 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002635 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002636 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002637 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002638 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002639 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002640 * @param work Address of delayed work item.
2641 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002642 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002643 * @retval -EINPROGRESS Work item is already pending.
2644 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002645 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002646extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002647
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002648/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002649 * @brief Submit a work item to the system workqueue.
2650 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002651 * This routine submits work item @a work to be processed by the system
2652 * workqueue. If the work item is already pending in the workqueue's queue
2653 * as a result of an earlier submission, this routine has no effect on the
2654 * work item. If the work item has already been processed, or is currently
2655 * being processed, its work is considered complete and the work item can be
2656 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002657 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002658 * @warning
2659 * Work items submitted to the system workqueue should avoid using handlers
2660 * that block or yield since this may prevent the system workqueue from
2661 * processing other work items in a timely manner.
2662 *
2663 * @note Can be called by ISRs.
2664 *
2665 * @param work Address of work item.
2666 *
2667 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002668 */
2669static inline void k_work_submit(struct k_work *work)
2670{
2671 k_work_submit_to_queue(&k_sys_work_q, work);
2672}
2673
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002674/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002675 * @brief Submit a delayed work item to the system workqueue.
2676 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002677 * This routine schedules work item @a work to be processed by the system
2678 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002679 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002680 * Only when the countdown completes is the work item actually submitted to
2681 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002682 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002683 * Submitting a previously submitted delayed work item that is still
2684 * counting down cancels the existing submission and restarts the countdown
2685 * using the new delay. If the work item is currently pending on the
2686 * workqueue's queue because the countdown has completed it is too late to
2687 * resubmit the item, and resubmission fails without impacting the work item.
2688 * If the work item has already been processed, or is currently being processed,
2689 * its work is considered complete and the work item can be resubmitted.
2690 *
2691 * @warning
2692 * Work items submitted to the system workqueue should avoid using handlers
2693 * that block or yield since this may prevent the system workqueue from
2694 * processing other work items in a timely manner.
2695 *
2696 * @note Can be called by ISRs.
2697 *
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.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002705 */
2706static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002707 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002708{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002709 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002710}
2711
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002712/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002713 * @brief Get time remaining before a delayed work gets scheduled.
2714 *
2715 * This routine computes the (approximate) time remaining before a
2716 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002717 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002718 *
2719 * @param work Delayed work item.
2720 *
2721 * @return Remaining time (in milliseconds).
2722 */
Kumar Galacc334c72017-04-21 10:55:34 -05002723static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002724{
2725 return _timeout_remaining_get(&work->timeout);
2726}
2727
Anas Nashif166f5192018-02-25 08:02:36 -06002728/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002729
Allan Stephensc98da842016-11-11 15:45:03 -05002730/**
2731 * @cond INTERNAL_HIDDEN
2732 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002733
2734struct k_mutex {
2735 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002736 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002737 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002738 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002739
Anas Nashif2f203c22016-12-18 06:57:45 -05002740 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002741};
2742
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002743#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002744 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002745 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002746 .owner = NULL, \
2747 .lock_count = 0, \
2748 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002749 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002750 }
2751
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002752#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2753
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002754/**
Allan Stephensc98da842016-11-11 15:45:03 -05002755 * INTERNAL_HIDDEN @endcond
2756 */
2757
2758/**
2759 * @defgroup mutex_apis Mutex APIs
2760 * @ingroup kernel_apis
2761 * @{
2762 */
2763
2764/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002765 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002766 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002767 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002768 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002769 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002770 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002771 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002772 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002773#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002774 struct k_mutex name \
2775 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002776 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002777
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002778/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002779 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002780 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002781 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002782 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002783 * Upon completion, the mutex is available and does not have an owner.
2784 *
2785 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002786 *
2787 * @return N/A
2788 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002789__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002790
2791/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002792 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002793 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002794 * This routine locks @a mutex. If the mutex is locked by another thread,
2795 * the calling thread waits until the mutex becomes available or until
2796 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002798 * A thread is permitted to lock a mutex it has already locked. The operation
2799 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002801 * @param mutex Address of the mutex.
2802 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002803 * or one of the special values K_NO_WAIT and K_FOREVER.
2804 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002805 * @retval 0 Mutex locked.
2806 * @retval -EBUSY Returned without waiting.
2807 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002808 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002809__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002810
2811/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002812 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002813 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002814 * This routine unlocks @a mutex. The mutex must already be locked by the
2815 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002816 *
2817 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002818 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002819 * thread.
2820 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002821 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002822 *
2823 * @return N/A
2824 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002825__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002826
Allan Stephensc98da842016-11-11 15:45:03 -05002827/**
Anas Nashif166f5192018-02-25 08:02:36 -06002828 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002829 */
2830
2831/**
2832 * @cond INTERNAL_HIDDEN
2833 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002834
2835struct k_sem {
2836 _wait_q_t wait_q;
2837 unsigned int count;
2838 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002839 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002840
Anas Nashif2f203c22016-12-18 06:57:45 -05002841 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002842};
2843
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002844#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002845 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002846 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002847 .count = initial_count, \
2848 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002849 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002850 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002851 }
2852
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002853#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2854
Allan Stephensc98da842016-11-11 15:45:03 -05002855/**
2856 * INTERNAL_HIDDEN @endcond
2857 */
2858
2859/**
2860 * @defgroup semaphore_apis Semaphore APIs
2861 * @ingroup kernel_apis
2862 * @{
2863 */
2864
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002865/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002866 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002868 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002870 * @param sem Address of the semaphore.
2871 * @param initial_count Initial semaphore count.
2872 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002873 *
2874 * @return N/A
2875 */
Andrew Boie99280232017-09-29 14:17:47 -07002876__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2877 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002878
2879/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002880 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002882 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002884 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2885 *
2886 * @param sem Address of the semaphore.
2887 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002888 * or one of the special values K_NO_WAIT and K_FOREVER.
2889 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002890 * @note When porting code from the nanokernel legacy API to the new API, be
2891 * careful with the return value of this function. The return value is the
2892 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2893 * non-zero means failure, while the nano_sem_take family returns 1 for success
2894 * and 0 for failure.
2895 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002896 * @retval 0 Semaphore taken.
2897 * @retval -EBUSY Returned without waiting.
2898 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002899 */
Andrew Boie99280232017-09-29 14:17:47 -07002900__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002901
2902/**
2903 * @brief Give a semaphore.
2904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002905 * This routine gives @a sem, unless the semaphore is already at its maximum
2906 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002907 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002908 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002909 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002910 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002911 *
2912 * @return N/A
2913 */
Andrew Boie99280232017-09-29 14:17:47 -07002914__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002915
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002916/**
2917 * @brief Reset a semaphore's count to zero.
2918 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002919 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002921 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002922 *
2923 * @return N/A
2924 */
Andrew Boie990bf162017-10-03 12:36:49 -07002925__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002926
Anas Nashif954d5502018-02-25 08:37:28 -06002927/**
2928 * @internal
2929 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002930static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002931{
2932 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002933}
2934
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002935/**
2936 * @brief Get a semaphore's count.
2937 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002938 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002939 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002940 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002941 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002942 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002943 */
Andrew Boie990bf162017-10-03 12:36:49 -07002944__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002945
Anas Nashif954d5502018-02-25 08:37:28 -06002946/**
2947 * @internal
2948 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002949static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002950{
2951 return sem->count;
2952}
2953
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002954/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002955 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002957 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002958 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002959 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002960 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002961 * @param name Name of the semaphore.
2962 * @param initial_count Initial semaphore count.
2963 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002964 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002965#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002966 struct k_sem name \
2967 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07002968 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05302969 BUILD_ASSERT(((count_limit) != 0) && \
2970 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002971
Anas Nashif166f5192018-02-25 08:02:36 -06002972/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002973
2974/**
2975 * @defgroup alert_apis Alert APIs
2976 * @ingroup kernel_apis
2977 * @{
2978 */
2979
Allan Stephens5eceb852016-11-16 10:16:30 -05002980/**
2981 * @typedef k_alert_handler_t
2982 * @brief Alert handler function type.
2983 *
2984 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002985 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002986 * and is only invoked if the alert has been initialized with one.
2987 *
2988 * @param alert Address of the alert.
2989 *
2990 * @return 0 if alert has been consumed; non-zero if alert should pend.
2991 */
2992typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002993
Anas Nashif166f5192018-02-25 08:02:36 -06002994/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002995
2996/**
2997 * @cond INTERNAL_HIDDEN
2998 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002999
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003000#define K_ALERT_DEFAULT NULL
3001#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003002
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003003struct k_alert {
3004 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003005 atomic_t send_count;
3006 struct k_work work_item;
3007 struct k_sem sem;
3008
Anas Nashif2f203c22016-12-18 06:57:45 -05003009 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003010};
3011
Anas Nashif954d5502018-02-25 08:37:28 -06003012/**
3013 * @internal
3014 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003015extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003016
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003017#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003018 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003019 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003020 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003021 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
3022 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003023 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003024 }
3025
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003026#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
3027
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003028/**
Allan Stephensc98da842016-11-11 15:45:03 -05003029 * INTERNAL_HIDDEN @endcond
3030 */
3031
3032/**
3033 * @addtogroup alert_apis
3034 * @{
3035 */
3036
3037/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003038 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003039 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003040 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003041 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003042 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003043 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003044 * @param name Name of the alert.
3045 * @param alert_handler Action to take when alert is sent. Specify either
3046 * the address of a function to be invoked by the system workqueue
3047 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
3048 * K_ALERT_DEFAULT (which causes the alert to pend).
3049 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003050 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003051#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003052 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003053 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003054 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003055 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003056
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003057/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003058 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003059 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003060 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003061 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003062 * @param alert Address of the alert.
3063 * @param handler Action to take when alert is sent. Specify either the address
3064 * of a function to be invoked by the system workqueue thread,
3065 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3066 * K_ALERT_DEFAULT (which causes the alert to pend).
3067 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003068 *
3069 * @return N/A
3070 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003071extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3072 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003073
3074/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003075 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003076 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003077 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003078 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003079 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3080 *
3081 * @param alert Address of the alert.
3082 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003083 * or one of the special values K_NO_WAIT and K_FOREVER.
3084 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003085 * @retval 0 Alert received.
3086 * @retval -EBUSY Returned without waiting.
3087 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003088 */
Andrew Boie310e9872017-09-29 04:41:15 -07003089__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003090
3091/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003092 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003094 * This routine signals @a alert. The action specified for @a alert will
3095 * be taken, which may trigger the execution of an alert handler function
3096 * and/or cause the alert to pend (assuming the alert has not reached its
3097 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003098 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003099 * @note Can be called by ISRs.
3100 *
3101 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003102 *
3103 * @return N/A
3104 */
Andrew Boie310e9872017-09-29 04:41:15 -07003105__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003106
3107/**
Anas Nashif166f5192018-02-25 08:02:36 -06003108 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003109 */
3110
Allan Stephensc98da842016-11-11 15:45:03 -05003111/**
3112 * @cond INTERNAL_HIDDEN
3113 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003114
3115struct k_msgq {
3116 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003117 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003118 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003119 char *buffer_start;
3120 char *buffer_end;
3121 char *read_ptr;
3122 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003123 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003124
Anas Nashif2f203c22016-12-18 06:57:45 -05003125 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003126 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003127};
3128
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003129#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003130 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003131 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003132 .max_msgs = q_max_msgs, \
3133 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003134 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003135 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003136 .read_ptr = q_buffer, \
3137 .write_ptr = q_buffer, \
3138 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003139 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003140 }
3141
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003142#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
3143
Andrew Boie0fe789f2018-04-12 18:35:56 -07003144#define K_MSGQ_FLAG_ALLOC BIT(0)
3145
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303146struct k_msgq_attrs {
3147 size_t msg_size;
3148 u32_t max_msgs;
3149 u32_t used_msgs;
3150};
3151
Peter Mitsis1da807e2016-10-06 11:36:59 -04003152/**
Allan Stephensc98da842016-11-11 15:45:03 -05003153 * INTERNAL_HIDDEN @endcond
3154 */
3155
3156/**
3157 * @defgroup msgq_apis Message Queue APIs
3158 * @ingroup kernel_apis
3159 * @{
3160 */
3161
3162/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003163 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003165 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3166 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003167 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3168 * message is similarly aligned to this boundary, @a q_msg_size must also be
3169 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003170 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003171 * The message queue can be accessed outside the module where it is defined
3172 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003173 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003174 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003175 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003176 * @param q_name Name of the message queue.
3177 * @param q_msg_size Message size (in bytes).
3178 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003179 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003180 */
3181#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003182 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003183 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003184 struct k_msgq q_name \
3185 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003186 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003187 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003188
Peter Mitsisd7a37502016-10-13 11:37:40 -04003189/**
3190 * @brief Initialize a message queue.
3191 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003192 * This routine initializes a message queue object, prior to its first use.
3193 *
Allan Stephensda827222016-11-09 14:23:58 -06003194 * The message queue's ring buffer must contain space for @a max_msgs messages,
3195 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3196 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3197 * that each message is similarly aligned to this boundary, @a q_msg_size
3198 * must also be a multiple of N.
3199 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003200 * @param q Address of the message queue.
3201 * @param buffer Pointer to ring buffer that holds queued messages.
3202 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003203 * @param max_msgs Maximum number of messages that can be queued.
3204 *
3205 * @return N/A
3206 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003207void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3208 u32_t max_msgs);
3209
3210/**
3211 * @brief Initialize a message queue.
3212 *
3213 * This routine initializes a message queue object, prior to its first use,
3214 * allocating its internal ring buffer from the calling thread's resource
3215 * pool.
3216 *
3217 * Memory allocated for the ring buffer can be released by calling
3218 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3219 * all of its references.
3220 *
3221 * @param q Address of the message queue.
3222 * @param msg_size Message size (in bytes).
3223 * @param max_msgs Maximum number of messages that can be queued.
3224 *
3225 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3226 * thread's resource pool, or -EINVAL if the size parameters cause
3227 * an integer overflow.
3228 */
3229__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3230 u32_t max_msgs);
3231
3232
3233void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003234
3235/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003236 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003237 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003238 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003239 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003240 * @note Can be called by ISRs.
3241 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003242 * @param q Address of the message queue.
3243 * @param data Pointer to the message.
3244 * @param timeout Waiting period to add the message (in milliseconds),
3245 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003246 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003247 * @retval 0 Message sent.
3248 * @retval -ENOMSG Returned without waiting or queue purged.
3249 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003250 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003251__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003252
3253/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003254 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003255 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003256 * This routine receives a message from message queue @a q in a "first in,
3257 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003258 *
Allan Stephensc98da842016-11-11 15:45:03 -05003259 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003260 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003261 * @param q Address of the message queue.
3262 * @param data Address of area to hold the received message.
3263 * @param timeout Waiting period to receive the message (in milliseconds),
3264 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003265 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003266 * @retval 0 Message received.
3267 * @retval -ENOMSG Returned without waiting.
3268 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003269 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003270__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003271
3272/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003273 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003274 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003275 * This routine discards all unreceived messages in a message queue's ring
3276 * buffer. Any threads that are blocked waiting to send a message to the
3277 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003278 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003279 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003280 *
3281 * @return N/A
3282 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003283__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003284
Peter Mitsis67be2492016-10-07 11:44:34 -04003285/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003286 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003287 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003288 * This routine returns the number of unused entries in a message queue's
3289 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003290 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003291 * @param q Address of the message queue.
3292 *
3293 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04003294 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003295__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3296
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303297/**
3298 * @brief Get basic attributes of a message queue.
3299 *
3300 * This routine fetches basic attributes of message queue into attr argument.
3301 *
3302 * @param q Address of the message queue.
3303 * @param attrs pointer to message queue attribute structure.
3304 *
3305 * @return N/A
3306 */
3307__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3308
3309
Andrew Boie82edb6e2017-10-02 10:53:06 -07003310static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003311{
3312 return q->max_msgs - q->used_msgs;
3313}
3314
Peter Mitsisd7a37502016-10-13 11:37:40 -04003315/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003316 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003318 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003319 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003320 * @param q Address of the message queue.
3321 *
3322 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003323 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003324__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3325
3326static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003327{
3328 return q->used_msgs;
3329}
3330
Anas Nashif166f5192018-02-25 08:02:36 -06003331/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003332
3333/**
3334 * @defgroup mem_pool_apis Memory Pool APIs
3335 * @ingroup kernel_apis
3336 * @{
3337 */
3338
Andy Ross73cb9582017-05-09 10:42:39 -07003339/* Note on sizing: the use of a 20 bit field for block means that,
3340 * assuming a reasonable minimum block size of 16 bytes, we're limited
3341 * to 16M of memory managed by a single pool. Long term it would be
3342 * good to move to a variable bit size based on configuration.
3343 */
3344struct k_mem_block_id {
3345 u32_t pool : 8;
3346 u32_t level : 4;
3347 u32_t block : 20;
3348};
3349
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003350struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003351 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003352 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003353};
3354
Anas Nashif166f5192018-02-25 08:02:36 -06003355/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003356
3357/**
3358 * @defgroup mailbox_apis Mailbox APIs
3359 * @ingroup kernel_apis
3360 * @{
3361 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003362
3363struct k_mbox_msg {
3364 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003365 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003366 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003367 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003368 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003369 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003370 /** sender's message data buffer */
3371 void *tx_data;
3372 /** internal use only - needed for legacy API support */
3373 void *_rx_data;
3374 /** message data block descriptor */
3375 struct k_mem_block tx_block;
3376 /** source thread id */
3377 k_tid_t rx_source_thread;
3378 /** target thread id */
3379 k_tid_t tx_target_thread;
3380 /** internal use only - thread waiting on send (may be a dummy) */
3381 k_tid_t _syncing_thread;
3382#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3383 /** internal use only - semaphore used during asynchronous send */
3384 struct k_sem *_async_sem;
3385#endif
3386};
3387
Allan Stephensc98da842016-11-11 15:45:03 -05003388/**
3389 * @cond INTERNAL_HIDDEN
3390 */
3391
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003392struct k_mbox {
3393 _wait_q_t tx_msg_queue;
3394 _wait_q_t rx_msg_queue;
3395
Anas Nashif2f203c22016-12-18 06:57:45 -05003396 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003397};
3398
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003399#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003400 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003401 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3402 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003403 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003404 }
3405
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003406#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3407
Peter Mitsis12092702016-10-14 12:57:23 -04003408/**
Allan Stephensc98da842016-11-11 15:45:03 -05003409 * INTERNAL_HIDDEN @endcond
3410 */
3411
3412/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003413 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003415 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003416 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003417 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003419 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003420 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003421#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003422 struct k_mbox name \
3423 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003424 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003425
Peter Mitsis12092702016-10-14 12:57:23 -04003426/**
3427 * @brief Initialize a mailbox.
3428 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003429 * This routine initializes a mailbox object, prior to its first use.
3430 *
3431 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003432 *
3433 * @return N/A
3434 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003435extern void k_mbox_init(struct k_mbox *mbox);
3436
Peter Mitsis12092702016-10-14 12:57:23 -04003437/**
3438 * @brief Send a mailbox message in a synchronous manner.
3439 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003440 * This routine sends a message to @a mbox and waits for a receiver to both
3441 * receive and process it. The message data may be in a buffer, in a memory
3442 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003443 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003444 * @param mbox Address of the mailbox.
3445 * @param tx_msg Address of the transmit message descriptor.
3446 * @param timeout Waiting period for the message to be received (in
3447 * milliseconds), or one of the special values K_NO_WAIT
3448 * and K_FOREVER. Once the message has been received,
3449 * this routine waits as long as necessary for the message
3450 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003451 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003452 * @retval 0 Message sent.
3453 * @retval -ENOMSG Returned without waiting.
3454 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003455 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003456extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003457 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003458
Peter Mitsis12092702016-10-14 12:57:23 -04003459/**
3460 * @brief Send a mailbox message in an asynchronous manner.
3461 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003462 * This routine sends a message to @a mbox without waiting for a receiver
3463 * to process it. The message data may be in a buffer, in a memory pool block,
3464 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3465 * will be given when the message has been both received and completely
3466 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003467 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003468 * @param mbox Address of the mailbox.
3469 * @param tx_msg Address of the transmit message descriptor.
3470 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003471 *
3472 * @return N/A
3473 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003474extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003475 struct k_sem *sem);
3476
Peter Mitsis12092702016-10-14 12:57:23 -04003477/**
3478 * @brief Receive a mailbox message.
3479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003480 * This routine receives a message from @a mbox, then optionally retrieves
3481 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003483 * @param mbox Address of the mailbox.
3484 * @param rx_msg Address of the receive message descriptor.
3485 * @param buffer Address of the buffer to receive data, or NULL to defer data
3486 * retrieval and message disposal until later.
3487 * @param timeout Waiting period for a message to be received (in
3488 * milliseconds), or one of the special values K_NO_WAIT
3489 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003490 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003491 * @retval 0 Message received.
3492 * @retval -ENOMSG Returned without waiting.
3493 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003494 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003495extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003496 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003497
3498/**
3499 * @brief Retrieve mailbox message data into a buffer.
3500 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003501 * This routine completes the processing of a received message by retrieving
3502 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003503 *
3504 * Alternatively, this routine can be used to dispose of a received message
3505 * without retrieving its data.
3506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003507 * @param rx_msg Address of the receive message descriptor.
3508 * @param buffer Address of the buffer to receive data, or NULL to discard
3509 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003510 *
3511 * @return N/A
3512 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003513extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003514
3515/**
3516 * @brief Retrieve mailbox message data into a memory pool block.
3517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003518 * This routine completes the processing of a received message by retrieving
3519 * its data into a memory pool block, then disposing of the message.
3520 * The memory pool block that results from successful retrieval must be
3521 * returned to the pool once the data has been processed, even in cases
3522 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003523 *
3524 * Alternatively, this routine can be used to dispose of a received message
3525 * without retrieving its data. In this case there is no need to return a
3526 * memory pool block to the pool.
3527 *
3528 * This routine allocates a new memory pool block for the data only if the
3529 * data is not already in one. If a new block cannot be allocated, the routine
3530 * returns a failure code and the received message is left unchanged. This
3531 * permits the caller to reattempt data retrieval at a later time or to dispose
3532 * of the received message without retrieving its data.
3533 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003534 * @param rx_msg Address of a receive message descriptor.
3535 * @param pool Address of memory pool, or NULL to discard data.
3536 * @param block Address of the area to hold memory pool block info.
3537 * @param timeout Waiting period to wait for a memory pool block (in
3538 * milliseconds), or one of the special values K_NO_WAIT
3539 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003540 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003541 * @retval 0 Data retrieved.
3542 * @retval -ENOMEM Returned without waiting.
3543 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003544 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003545extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003546 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003547 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003548
Anas Nashif166f5192018-02-25 08:02:36 -06003549/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003550
3551/**
3552 * @cond INTERNAL_HIDDEN
3553 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003554
Andrew Boie44fe8122018-04-12 17:38:12 -07003555#define K_PIPE_FLAG_ALLOC BIT(0) /* Buffer was allocated */
3556
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003557struct k_pipe {
3558 unsigned char *buffer; /* Pipe buffer: may be NULL */
3559 size_t size; /* Buffer size */
3560 size_t bytes_used; /* # bytes used in buffer */
3561 size_t read_index; /* Where in buffer to read from */
3562 size_t write_index; /* Where in buffer to write */
3563
3564 struct {
3565 _wait_q_t readers; /* Reader wait queue */
3566 _wait_q_t writers; /* Writer wait queue */
3567 } wait_q;
3568
Anas Nashif2f203c22016-12-18 06:57:45 -05003569 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Andrew Boie44fe8122018-04-12 17:38:12 -07003570 u8_t flags; /* Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003571};
3572
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003573#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003574 { \
3575 .buffer = pipe_buffer, \
3576 .size = pipe_buffer_size, \
3577 .bytes_used = 0, \
3578 .read_index = 0, \
3579 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003580 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3581 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003582 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003583 }
3584
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003585#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3586
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003587/**
Allan Stephensc98da842016-11-11 15:45:03 -05003588 * INTERNAL_HIDDEN @endcond
3589 */
3590
3591/**
3592 * @defgroup pipe_apis Pipe APIs
3593 * @ingroup kernel_apis
3594 * @{
3595 */
3596
3597/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003598 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003600 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003601 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003602 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003604 * @param name Name of the pipe.
3605 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3606 * or zero if no ring buffer is used.
3607 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003608 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003609#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3610 static unsigned char __kernel_noinit __aligned(pipe_align) \
3611 _k_pipe_buf_##name[pipe_buffer_size]; \
3612 struct k_pipe name \
3613 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003614 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003615
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003616/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003617 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003619 * This routine initializes a pipe object, prior to its first use.
3620 *
3621 * @param pipe Address of the pipe.
3622 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3623 * is used.
3624 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3625 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003626 *
3627 * @return N/A
3628 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003629void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3630
3631/**
3632 * @brief Release a pipe's allocated buffer
3633 *
3634 * If a pipe object was given a dynamically allocated buffer via
3635 * k_pipe_alloc_init(), this will free it. This function does nothing
3636 * if the buffer wasn't dynamically allocated.
3637 *
3638 * @param pipe Address of the pipe.
3639 */
3640void k_pipe_cleanup(struct k_pipe *pipe);
3641
3642/**
3643 * @brief Initialize a pipe and allocate a buffer for it
3644 *
3645 * Storage for the buffer region will be allocated from the calling thread's
3646 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3647 * or userspace is enabled and the pipe object loses all references to it.
3648 *
3649 * This function should only be called on uninitialized pipe objects.
3650 *
3651 * @param pipe Address of the pipe.
3652 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3653 * buffer is used.
3654 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003655 * @retval -ENOMEM if memory couldn't be allocated
Andrew Boie44fe8122018-04-12 17:38:12 -07003656 */
3657__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003658
3659/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003660 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003661 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003662 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003663 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003664 * @param pipe Address of the pipe.
3665 * @param data Address of data to write.
3666 * @param bytes_to_write Size of data (in bytes).
3667 * @param bytes_written Address of area to hold the number of bytes written.
3668 * @param min_xfer Minimum number of bytes to write.
3669 * @param timeout Waiting period to wait for the data to be written (in
3670 * milliseconds), or one of the special values K_NO_WAIT
3671 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003672 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003673 * @retval 0 At least @a min_xfer bytes of data were written.
3674 * @retval -EIO Returned without waiting; zero data bytes were written.
3675 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003676 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003677 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003678__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3679 size_t bytes_to_write, size_t *bytes_written,
3680 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003681
3682/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003683 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003685 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003686 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003687 * @param pipe Address of the pipe.
3688 * @param data Address to place the data read from pipe.
3689 * @param bytes_to_read Maximum number of data bytes to read.
3690 * @param bytes_read Address of area to hold the number of bytes read.
3691 * @param min_xfer Minimum number of data bytes to read.
3692 * @param timeout Waiting period to wait for the data to be read (in
3693 * milliseconds), or one of the special values K_NO_WAIT
3694 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003695 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003696 * @retval 0 At least @a min_xfer bytes of data were read.
3697 * @retval -EIO Returned without waiting; zero data bytes were read.
3698 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003699 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003700 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003701__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3702 size_t bytes_to_read, size_t *bytes_read,
3703 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003704
3705/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003706 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003708 * This routine writes the data contained in a memory block to @a pipe.
3709 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003710 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003711 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003712 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003713 * @param block Memory block containing data to send
3714 * @param size Number of data bytes in memory block to send
3715 * @param sem Semaphore to signal upon completion (else NULL)
3716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003717 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003718 */
3719extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3720 size_t size, struct k_sem *sem);
3721
Anas Nashif166f5192018-02-25 08:02:36 -06003722/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003723
Allan Stephensc98da842016-11-11 15:45:03 -05003724/**
3725 * @cond INTERNAL_HIDDEN
3726 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003727
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003728struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003729 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003730 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003731 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003732 char *buffer;
3733 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003734 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003735
Anas Nashif2f203c22016-12-18 06:57:45 -05003736 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003737};
3738
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003739#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003740 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003741 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003742 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003743 .num_blocks = slab_num_blocks, \
3744 .block_size = slab_block_size, \
3745 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003746 .free_list = NULL, \
3747 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003748 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003749 }
3750
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003751#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3752
3753
Peter Mitsis578f9112016-10-07 13:50:31 -04003754/**
Allan Stephensc98da842016-11-11 15:45:03 -05003755 * INTERNAL_HIDDEN @endcond
3756 */
3757
3758/**
3759 * @defgroup mem_slab_apis Memory Slab APIs
3760 * @ingroup kernel_apis
3761 * @{
3762 */
3763
3764/**
Allan Stephensda827222016-11-09 14:23:58 -06003765 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003766 *
Allan Stephensda827222016-11-09 14:23:58 -06003767 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003768 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003769 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3770 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003771 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003772 *
Allan Stephensda827222016-11-09 14:23:58 -06003773 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003774 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003775 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003776 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003777 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003778 * @param name Name of the memory slab.
3779 * @param slab_block_size Size of each memory block (in bytes).
3780 * @param slab_num_blocks Number memory blocks.
3781 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003782 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003783#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3784 char __noinit __aligned(slab_align) \
3785 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3786 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003787 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003788 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003789 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003790
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003791/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003792 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003793 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003794 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003795 *
Allan Stephensda827222016-11-09 14:23:58 -06003796 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3797 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3798 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3799 * To ensure that each memory block is similarly aligned to this boundary,
3800 * @a slab_block_size must also be a multiple of N.
3801 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003802 * @param slab Address of the memory slab.
3803 * @param buffer Pointer to buffer used for the memory blocks.
3804 * @param block_size Size of each memory block (in bytes).
3805 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003806 *
3807 * @return N/A
3808 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003809extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003810 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003811
3812/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003813 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003814 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003815 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003817 * @param slab Address of the memory slab.
3818 * @param mem Pointer to block address area.
3819 * @param timeout Maximum time to wait for operation to complete
3820 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3821 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003822 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003823 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003824 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003825 * @retval -ENOMEM Returned without waiting.
3826 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003827 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003828extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003829 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003830
3831/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003832 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003833 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003834 * This routine releases a previously allocated memory block back to its
3835 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003836 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003837 * @param slab Address of the memory slab.
3838 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003839 *
3840 * @return N/A
3841 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003842extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003843
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003844/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003845 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003847 * This routine gets the number of memory blocks that are currently
3848 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003849 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003850 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003852 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003853 */
Kumar Galacc334c72017-04-21 10:55:34 -05003854static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003855{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003856 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003857}
3858
Peter Mitsisc001aa82016-10-13 13:53:37 -04003859/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003860 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003862 * This routine gets the number of memory blocks that are currently
3863 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003865 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003867 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003868 */
Kumar Galacc334c72017-04-21 10:55:34 -05003869static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003870{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003871 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003872}
3873
Anas Nashif166f5192018-02-25 08:02:36 -06003874/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003875
3876/**
3877 * @cond INTERNAL_HIDDEN
3878 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003879
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003880struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003881 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003882 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003883};
3884
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003885/**
Allan Stephensc98da842016-11-11 15:45:03 -05003886 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003887 */
3888
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003889/**
Allan Stephensc98da842016-11-11 15:45:03 -05003890 * @addtogroup mem_pool_apis
3891 * @{
3892 */
3893
3894/**
3895 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003897 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3898 * long. The memory pool allows blocks to be repeatedly partitioned into
3899 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003900 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003901 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003902 * If the pool is to be accessed outside the module where it is defined, it
3903 * can be declared via
3904 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003905 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003906 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003907 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003908 * @param minsz Size of the smallest blocks in the pool (in bytes).
3909 * @param maxsz Size of the largest blocks in the pool (in bytes).
3910 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003911 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003912 */
Andy Ross73cb9582017-05-09 10:42:39 -07003913#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3914 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3915 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003916 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003917 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003918 .base = { \
3919 .buf = _mpool_buf_##name, \
3920 .max_sz = maxsz, \
3921 .n_max = nmax, \
3922 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3923 .levels = _mpool_lvls_##name, \
3924 .flags = SYS_MEM_POOL_KERNEL \
3925 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003926 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003927
Peter Mitsis937042c2016-10-13 13:18:26 -04003928/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003929 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003930 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003931 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003933 * @param pool Address of the memory pool.
3934 * @param block Pointer to block descriptor for the allocated memory.
3935 * @param size Amount of memory to allocate (in bytes).
3936 * @param timeout Maximum time to wait for operation to complete
3937 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3938 * or K_FOREVER to wait as long as necessary.
3939 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003940 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003941 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003942 * @retval -ENOMEM Returned without waiting.
3943 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003944 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003945extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003946 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003947
3948/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07003949 * @brief Allocate memory from a memory pool with malloc() semantics
3950 *
3951 * Such memory must be released using k_free().
3952 *
3953 * @param pool Address of the memory pool.
3954 * @param size Amount of memory to allocate (in bytes).
3955 * @return Address of the allocated memory if successful, otherwise NULL
3956 */
3957extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
3958
3959/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003960 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003962 * This routine releases a previously allocated memory block back to its
3963 * memory pool.
3964 *
3965 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003966 *
3967 * @return N/A
3968 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003969extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003970
3971/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003972 * @brief Free memory allocated from a memory pool.
3973 *
3974 * This routine releases a previously allocated memory block back to its
3975 * memory pool
3976 *
3977 * @param id Memory block identifier.
3978 *
3979 * @return N/A
3980 */
3981extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3982
3983/**
Anas Nashif166f5192018-02-25 08:02:36 -06003984 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003985 */
3986
3987/**
3988 * @defgroup heap_apis Heap Memory Pool APIs
3989 * @ingroup kernel_apis
3990 * @{
3991 */
3992
3993/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003994 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003996 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003997 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003999 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004001 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04004002 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004003extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004004
4005/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004006 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004007 *
4008 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004009 * returned must have been allocated from the heap memory pool or
4010 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004011 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004012 * If @a ptr is NULL, no operation is performed.
4013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004014 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004015 *
4016 * @return N/A
4017 */
4018extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004019
Allan Stephensc98da842016-11-11 15:45:03 -05004020/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004021 * @brief Allocate memory from heap, array style
4022 *
4023 * This routine provides traditional calloc() semantics. Memory is
4024 * allocated from the heap memory pool and zeroed.
4025 *
4026 * @param nmemb Number of elements in the requested array
4027 * @param size Size of each array element (in bytes).
4028 *
4029 * @return Address of the allocated memory if successful; otherwise NULL.
4030 */
4031extern void *k_calloc(size_t nmemb, size_t size);
4032
Anas Nashif166f5192018-02-25 08:02:36 -06004033/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004034
Benjamin Walshacc68c12017-01-29 18:57:45 -05004035/* polling API - PRIVATE */
4036
Benjamin Walshb0179862017-02-02 16:39:57 -05004037#ifdef CONFIG_POLL
4038#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
4039#else
4040#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
4041#endif
4042
Benjamin Walshacc68c12017-01-29 18:57:45 -05004043/* private - implementation data created as needed, per-type */
4044struct _poller {
4045 struct k_thread *thread;
4046};
4047
4048/* private - types bit positions */
4049enum _poll_types_bits {
4050 /* can be used to ignore an event */
4051 _POLL_TYPE_IGNORE,
4052
4053 /* to be signaled by k_poll_signal() */
4054 _POLL_TYPE_SIGNAL,
4055
4056 /* semaphore availability */
4057 _POLL_TYPE_SEM_AVAILABLE,
4058
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004059 /* queue/fifo/lifo data availability */
4060 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004061
4062 _POLL_NUM_TYPES
4063};
4064
4065#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4066
4067/* private - states bit positions */
4068enum _poll_states_bits {
4069 /* default state when creating event */
4070 _POLL_STATE_NOT_READY,
4071
Benjamin Walshacc68c12017-01-29 18:57:45 -05004072 /* signaled by k_poll_signal() */
4073 _POLL_STATE_SIGNALED,
4074
4075 /* semaphore is available */
4076 _POLL_STATE_SEM_AVAILABLE,
4077
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004078 /* data is available to read on queue/fifo/lifo */
4079 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004080
4081 _POLL_NUM_STATES
4082};
4083
4084#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4085
4086#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004087 (32 - (0 \
4088 + 8 /* tag */ \
4089 + _POLL_NUM_TYPES \
4090 + _POLL_NUM_STATES \
4091 + 1 /* modes */ \
4092 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004093
Benjamin Walshacc68c12017-01-29 18:57:45 -05004094/* end of polling API - PRIVATE */
4095
4096
4097/**
4098 * @defgroup poll_apis Async polling APIs
4099 * @ingroup kernel_apis
4100 * @{
4101 */
4102
4103/* Public polling API */
4104
4105/* public - values for k_poll_event.type bitfield */
4106#define K_POLL_TYPE_IGNORE 0
4107#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4108#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004109#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4110#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004111
4112/* public - polling modes */
4113enum k_poll_modes {
4114 /* polling thread does not take ownership of objects when available */
4115 K_POLL_MODE_NOTIFY_ONLY = 0,
4116
4117 K_POLL_NUM_MODES
4118};
4119
4120/* public - values for k_poll_event.state bitfield */
4121#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004122#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4123#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004124#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4125#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004126
4127/* public - poll signal object */
4128struct k_poll_signal {
4129 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004130 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004131
4132 /*
4133 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4134 * user resets it to 0.
4135 */
4136 unsigned int signaled;
4137
4138 /* custom result value passed to k_poll_signal() if needed */
4139 int result;
4140};
4141
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004142#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004143 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004144 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004145 .signaled = 0, \
4146 .result = 0, \
4147 }
4148
4149struct k_poll_event {
4150 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004151 sys_dnode_t _node;
4152
4153 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004154 struct _poller *poller;
4155
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004156 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004157 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004158
Benjamin Walshacc68c12017-01-29 18:57:45 -05004159 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004160 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004161
4162 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004163 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004164
4165 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004166 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004167
4168 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004169 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004170
4171 /* per-type data */
4172 union {
4173 void *obj;
4174 struct k_poll_signal *signal;
4175 struct k_sem *sem;
4176 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004177 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004178 };
4179};
4180
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004181#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004182 { \
4183 .poller = NULL, \
4184 .type = event_type, \
4185 .state = K_POLL_STATE_NOT_READY, \
4186 .mode = event_mode, \
4187 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004188 { .obj = event_obj }, \
4189 }
4190
4191#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4192 event_tag) \
4193 { \
4194 .type = event_type, \
4195 .tag = event_tag, \
4196 .state = K_POLL_STATE_NOT_READY, \
4197 .mode = event_mode, \
4198 .unused = 0, \
4199 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004200 }
4201
4202/**
4203 * @brief Initialize one struct k_poll_event instance
4204 *
4205 * After this routine is called on a poll event, the event it ready to be
4206 * placed in an event array to be passed to k_poll().
4207 *
4208 * @param event The event to initialize.
4209 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4210 * values. Only values that apply to the same object being polled
4211 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4212 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004213 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004214 * @param obj Kernel object or poll signal.
4215 *
4216 * @return N/A
4217 */
4218
Kumar Galacc334c72017-04-21 10:55:34 -05004219extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004220 int mode, void *obj);
4221
4222/**
4223 * @brief Wait for one or many of multiple poll events to occur
4224 *
4225 * This routine allows a thread to wait concurrently for one or many of
4226 * multiple poll events to have occurred. Such events can be a kernel object
4227 * being available, like a semaphore, or a poll signal event.
4228 *
4229 * When an event notifies that a kernel object is available, the kernel object
4230 * is not "given" to the thread calling k_poll(): it merely signals the fact
4231 * that the object was available when the k_poll() call was in effect. Also,
4232 * all threads trying to acquire an object the regular way, i.e. by pending on
4233 * the object, have precedence over the thread polling on the object. This
4234 * means that the polling thread will never get the poll event on an object
4235 * until the object becomes available and its pend queue is empty. For this
4236 * reason, the k_poll() call is more effective when the objects being polled
4237 * only have one thread, the polling thread, trying to acquire them.
4238 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004239 * When k_poll() returns 0, the caller should loop on all the events that were
4240 * passed to k_poll() and check the state field for the values that were
4241 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004242 *
4243 * Before being reused for another call to k_poll(), the user has to reset the
4244 * state field to K_POLL_STATE_NOT_READY.
4245 *
Andrew Boie3772f772018-05-07 16:52:57 -07004246 * When called from user mode, a temporary memory allocation is required from
4247 * the caller's resource pool.
4248 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004249 * @param events An array of pointers to events to be polled for.
4250 * @param num_events The number of events in the array.
4251 * @param timeout Waiting period for an event to be ready (in milliseconds),
4252 * or one of the special values K_NO_WAIT and K_FOREVER.
4253 *
4254 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004255 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02004256 * @retval -EINTR Poller thread has been interrupted.
Andrew Boie3772f772018-05-07 16:52:57 -07004257 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4258 * @retval -EINVAL Bad parameters (user mode only)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004259 */
4260
Andrew Boie3772f772018-05-07 16:52:57 -07004261__syscall int k_poll(struct k_poll_event *events, int num_events,
4262 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004263
4264/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004265 * @brief Initialize a poll signal object.
4266 *
4267 * Ready a poll signal object to be signaled via k_poll_signal().
4268 *
4269 * @param signal A poll signal.
4270 *
4271 * @return N/A
4272 */
4273
Andrew Boie3772f772018-05-07 16:52:57 -07004274__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4275
4276/*
4277 * @brief Reset a poll signal object's state to unsignaled.
4278 *
4279 * @param signal A poll signal object
4280 */
4281__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4282
4283static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4284{
4285 signal->signaled = 0;
4286}
4287
4288/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004289 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004290 *
4291 * @param signal A poll signal object
4292 * @param signaled An integer buffer which will be written nonzero if the
4293 * object was signaled
4294 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004295 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004296 * value if it was not.
4297 */
4298__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4299 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004300
4301/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004302 * @brief Signal a poll signal object.
4303 *
4304 * This routine makes ready a poll signal, which is basically a poll event of
4305 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4306 * made ready to run. A @a result value can be specified.
4307 *
4308 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004309 * k_poll_signal(), stays set until the user sets it back to 0 with
4310 * k_poll_signal_reset(). It thus has to be reset by the user before being
4311 * passed again to k_poll() or k_poll() will consider it being signaled, and
4312 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004313 *
4314 * @param signal A poll signal.
4315 * @param result The value to store in the result field of the signal.
4316 *
4317 * @retval 0 The signal was delivered successfully.
4318 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
4319 */
4320
Andrew Boie3772f772018-05-07 16:52:57 -07004321__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004322
Anas Nashif954d5502018-02-25 08:37:28 -06004323/**
4324 * @internal
4325 */
Andy Ross8606fab2018-03-26 10:54:40 -07004326extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004327
Anas Nashif166f5192018-02-25 08:02:36 -06004328/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004329
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004330/**
4331 * @brief Make the CPU idle.
4332 *
4333 * This function makes the CPU idle until an event wakes it up.
4334 *
4335 * In a regular system, the idle thread should be the only thread responsible
4336 * for making the CPU idle and triggering any type of power management.
4337 * However, in some more constrained systems, such as a single-threaded system,
4338 * the only thread would be responsible for this if needed.
4339 *
4340 * @return N/A
4341 */
4342extern void k_cpu_idle(void);
4343
4344/**
4345 * @brief Make the CPU idle in an atomic fashion.
4346 *
4347 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4348 * must be done atomically before making the CPU idle.
4349 *
4350 * @param key Interrupt locking key obtained from irq_lock().
4351 *
4352 * @return N/A
4353 */
4354extern void k_cpu_atomic_idle(unsigned int key);
4355
Anas Nashif954d5502018-02-25 08:37:28 -06004356
4357/**
4358 * @internal
4359 */
Kumar Galacc334c72017-04-21 10:55:34 -05004360extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004361
Andrew Boiecdb94d62017-04-18 15:22:05 -07004362#ifdef _ARCH_EXCEPT
4363/* This archtecture has direct support for triggering a CPU exception */
4364#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4365#else
4366
Andrew Boiecdb94d62017-04-18 15:22:05 -07004367/* NOTE: This is the implementation for arches that do not implement
4368 * _ARCH_EXCEPT() to generate a real CPU exception.
4369 *
4370 * We won't have a real exception frame to determine the PC value when
4371 * the oops occurred, so print file and line number before we jump into
4372 * the fatal error handler.
4373 */
4374#define _k_except_reason(reason) do { \
4375 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4376 _NanoFatalErrorHandler(reason, &_default_esf); \
4377 CODE_UNREACHABLE; \
4378 } while (0)
4379
4380#endif /* _ARCH__EXCEPT */
4381
4382/**
4383 * @brief Fatally terminate a thread
4384 *
4385 * This should be called when a thread has encountered an unrecoverable
4386 * runtime condition and needs to terminate. What this ultimately
4387 * means is determined by the _fatal_error_handler() implementation, which
4388 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4389 *
4390 * If this is called from ISR context, the default system fatal error handler
4391 * will treat it as an unrecoverable system error, just like k_panic().
4392 */
4393#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4394
4395/**
4396 * @brief Fatally terminate the system
4397 *
4398 * This should be called when the Zephyr kernel has encountered an
4399 * unrecoverable runtime condition and needs to terminate. What this ultimately
4400 * means is determined by the _fatal_error_handler() implementation, which
4401 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4402 */
4403#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4404
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004405/*
4406 * private APIs that are utilized by one or more public APIs
4407 */
4408
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004409#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004410/**
4411 * @internal
4412 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004413extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004414#else
Anas Nashif954d5502018-02-25 08:37:28 -06004415/**
4416 * @internal
4417 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004418#define _init_static_threads() do { } while ((0))
4419#endif
4420
Anas Nashif954d5502018-02-25 08:37:28 -06004421/**
4422 * @internal
4423 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004424extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004425/**
4426 * @internal
4427 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004428extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004429
Andrew Boiedc5d9352017-06-02 12:56:47 -07004430/* arch/cpu.h may declare an architecture or platform-specific macro
4431 * for properly declaring stacks, compatible with MMU/MPU constraints if
4432 * enabled
4433 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004434
4435/**
4436 * @brief Obtain an extern reference to a stack
4437 *
4438 * This macro properly brings the symbol of a thread stack declared
4439 * elsewhere into scope.
4440 *
4441 * @param sym Thread stack symbol name
4442 */
4443#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4444
Andrew Boiedc5d9352017-06-02 12:56:47 -07004445#ifdef _ARCH_THREAD_STACK_DEFINE
4446#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4447#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4448 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4449#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4450#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004451static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004452{
4453 return _ARCH_THREAD_STACK_BUFFER(sym);
4454}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004455#else
4456/**
4457 * @brief Declare a toplevel thread stack memory region
4458 *
4459 * This declares a region of memory suitable for use as a thread's stack.
4460 *
4461 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4462 * 'noinit' section so that it isn't zeroed at boot
4463 *
Andrew Boie507852a2017-07-25 18:47:07 -07004464 * The declared symbol will always be a k_thread_stack_t which can be passed to
4465 * k_thread_create, but should otherwise not be manipulated. If the buffer
4466 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004467 *
4468 * It is legal to precede this definition with the 'static' keyword.
4469 *
4470 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4471 * parameter of k_thread_create(), it may not be the same as the
4472 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4473 *
4474 * @param sym Thread stack symbol name
4475 * @param size Size of the stack memory region
4476 */
4477#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004478 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004479
4480/**
4481 * @brief Declare a toplevel array of thread stack memory regions
4482 *
4483 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4484 * definition for additional details and constraints.
4485 *
4486 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4487 * 'noinit' section so that it isn't zeroed at boot
4488 *
4489 * @param sym Thread stack symbol name
4490 * @param nmemb Number of stacks to declare
4491 * @param size Size of the stack memory region
4492 */
4493
4494#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004495 struct _k_thread_stack_element __noinit \
4496 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004497
4498/**
4499 * @brief Declare an embedded stack memory region
4500 *
4501 * Used for stacks embedded within other data structures. Use is highly
4502 * discouraged but in some cases necessary. For memory protection scenarios,
4503 * it is very important that any RAM preceding this member not be writable
4504 * by threads else a stack overflow will lead to silent corruption. In other
4505 * words, the containing data structure should live in RAM owned by the kernel.
4506 *
4507 * @param sym Thread stack symbol name
4508 * @param size Size of the stack memory region
4509 */
4510#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004511 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004512
4513/**
4514 * @brief Return the size in bytes of a stack memory region
4515 *
4516 * Convenience macro for passing the desired stack size to k_thread_create()
4517 * since the underlying implementation may actually create something larger
4518 * (for instance a guard area).
4519 *
4520 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004521 * passed to K_THREAD_STACK_DEFINE.
4522 *
4523 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4524 * it is not guaranteed to return the original value since each array
4525 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004526 *
4527 * @param sym Stack memory symbol
4528 * @return Size of the stack
4529 */
4530#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4531
4532/**
4533 * @brief Get a pointer to the physical stack buffer
4534 *
4535 * Convenience macro to get at the real underlying stack buffer used by
4536 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4537 * This is really only intended for diagnostic tools which want to examine
4538 * stack memory contents.
4539 *
4540 * @param sym Declared stack symbol name
4541 * @return The buffer itself, a char *
4542 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004543static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004544{
4545 return (char *)sym;
4546}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004547
4548#endif /* _ARCH_DECLARE_STACK */
4549
Chunlin Hane9c97022017-07-07 20:29:30 +08004550/**
4551 * @defgroup mem_domain_apis Memory domain APIs
4552 * @ingroup kernel_apis
4553 * @{
4554 */
4555
4556/** @def MEM_PARTITION_ENTRY
4557 * @brief Used to declare a memory partition entry
4558 */
4559#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4560 {\
4561 .start = _start, \
4562 .size = _size, \
4563 .attr = _attr, \
4564 }
4565
4566/** @def K_MEM_PARTITION_DEFINE
4567 * @brief Used to declare a memory partition
4568 */
4569#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4570#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4571 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304572 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004573 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4574#else
4575#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304576 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004577 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4578#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4579
Chunlin Hane9c97022017-07-07 20:29:30 +08004580/* memory partition */
4581struct k_mem_partition {
4582 /* start address of memory partition */
4583 u32_t start;
4584 /* size of memory partition */
4585 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304586#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004587 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304588 k_mem_partition_attr_t attr;
4589#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004590};
4591
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304592/* memory domian
4593 * Note: Always declare this structure with __kernel prefix
4594 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004595struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304596#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004597 /* partitions in the domain */
4598 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304599#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004600 /* domain q */
4601 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004602 /* number of partitions in the domain */
4603 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004604};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304605
Chunlin Hane9c97022017-07-07 20:29:30 +08004606
4607/**
4608 * @brief Initialize a memory domain.
4609 *
4610 * Initialize a memory domain with given name and memory partitions.
4611 *
4612 * @param domain The memory domain to be initialized.
4613 * @param num_parts The number of array items of "parts" parameter.
4614 * @param parts An array of pointers to the memory partitions. Can be NULL
4615 * if num_parts is zero.
4616 */
4617
Leandro Pereira08de6582018-02-28 14:22:57 -08004618extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004619 struct k_mem_partition *parts[]);
4620/**
4621 * @brief Destroy a memory domain.
4622 *
4623 * Destroy a memory domain.
4624 *
4625 * @param domain The memory domain to be destroyed.
4626 */
4627
4628extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4629
4630/**
4631 * @brief Add a memory partition into a memory domain.
4632 *
4633 * Add a memory partition into a memory domain.
4634 *
4635 * @param domain The memory domain to be added a memory partition.
4636 * @param part The memory partition to be added
4637 */
4638
4639extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4640 struct k_mem_partition *part);
4641
4642/**
4643 * @brief Remove a memory partition from a memory domain.
4644 *
4645 * Remove a memory partition from a memory domain.
4646 *
4647 * @param domain The memory domain to be removed a memory partition.
4648 * @param part The memory partition to be removed
4649 */
4650
4651extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4652 struct k_mem_partition *part);
4653
4654/**
4655 * @brief Add a thread into a memory domain.
4656 *
4657 * Add a thread into a memory domain.
4658 *
4659 * @param domain The memory domain that the thread is going to be added into.
4660 * @param thread ID of thread going to be added into the memory domain.
4661 */
4662
4663extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4664 k_tid_t thread);
4665
4666/**
4667 * @brief Remove a thread from its memory domain.
4668 *
4669 * Remove a thread from its memory domain.
4670 *
4671 * @param thread ID of thread going to be removed from its memory domain.
4672 */
4673
4674extern void k_mem_domain_remove_thread(k_tid_t thread);
4675
Anas Nashif166f5192018-02-25 08:02:36 -06004676/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004677
Andrew Boie756f9072017-10-10 16:01:49 -07004678/**
4679 * @brief Emit a character buffer to the console device
4680 *
4681 * @param c String of characters to print
4682 * @param n The length of the string
4683 */
4684__syscall void k_str_out(char *c, size_t n);
4685
Andy Rosse7172672018-01-24 15:48:32 -08004686/**
4687 * @brief Start a numbered CPU on a MP-capable system
4688
4689 * This starts and initializes a specific CPU. The main thread on
4690 * startup is running on CPU zero, other processors are numbered
4691 * sequentially. On return from this function, the CPU is known to
4692 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004693 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004694 * with the provided key will work to enable them.
4695 *
4696 * Normally, in SMP mode this function will be called by the kernel
4697 * initialization and should not be used as a user API. But it is
4698 * defined here for special-purpose apps which want Zephyr running on
4699 * one core and to use others for design-specific processing.
4700 *
4701 * @param cpu_num Integer number of the CPU
4702 * @param stack Stack memory for the CPU
4703 * @param sz Stack buffer size, in bytes
4704 * @param fn Function to begin running on the CPU. First argument is
4705 * an irq_unlock() key.
4706 * @param arg Untyped argument to be passed to "fn"
4707 */
4708extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4709 void (*fn)(int, void *), void *arg);
4710
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004711#ifdef __cplusplus
4712}
4713#endif
4714
Andrew Boiee004dec2016-11-07 09:01:19 -08004715#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4716/*
4717 * Define new and delete operators.
4718 * At this moment, the operators do nothing since objects are supposed
4719 * to be statically allocated.
4720 */
4721inline void operator delete(void *ptr)
4722{
4723 (void)ptr;
4724}
4725
4726inline void operator delete[](void *ptr)
4727{
4728 (void)ptr;
4729}
4730
4731inline void *operator new(size_t size)
4732{
4733 (void)size;
4734 return NULL;
4735}
4736
4737inline void *operator new[](size_t size)
4738{
4739 (void)size;
4740 return NULL;
4741}
4742
4743/* Placement versions of operator new and delete */
4744inline void operator delete(void *ptr1, void *ptr2)
4745{
4746 (void)ptr1;
4747 (void)ptr2;
4748}
4749
4750inline void operator delete[](void *ptr1, void *ptr2)
4751{
4752 (void)ptr1;
4753 (void)ptr2;
4754}
4755
4756inline void *operator new(size_t size, void *ptr)
4757{
4758 (void)size;
4759 return ptr;
4760}
4761
4762inline void *operator new[](size_t size, void *ptr)
4763{
4764 (void)size;
4765 return ptr;
4766}
4767
4768#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4769
Andrew Boiefa94ee72017-09-28 16:54:35 -07004770#include <syscalls/kernel.h>
4771
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004772#endif /* !_ASMLANGUAGE */
4773
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004774#endif /* _kernel__h_ */