blob: f9ca7879ab7323d1a0c7fb8c5006e28424f904a0 [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
13#ifndef _kernel__h_
14#define _kernel__h_
15
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040017#include <stddef.h>
Kumar Gala78908162017-04-19 10:32:08 -050018#include <zephyr/types.h>
Anas Nashif173902f2017-01-17 07:08:56 -050019#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020#include <toolchain.h>
Anas Nashif397d29d2017-06-17 11:30:47 -040021#include <linker/sections.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040022#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
Andy Ross1acd8c22018-05-03 14:51:49 -070025#include <sched_priq.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040026#include <misc/dlist.h>
27#include <misc/slist.h>
Andrew Boie2b9b4b22018-04-27 13:21:22 -070028#include <misc/sflist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050029#include <misc/util.h>
Andrew Boieaa6de292018-03-06 17:12:37 -080030#include <misc/mempool_base.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050031#include <kernel_version.h>
Leandro Pereiraadce1d12017-10-13 15:45:02 -070032#include <random/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070033#include <kernel_arch_thread.h>
Andrew Boie13ca6fe2017-09-23 12:05:49 -070034#include <syscall.h>
Andrew Boie43263fc2017-11-02 11:07:31 -070035#include <misc/printk.h>
36#include <arch/cpu.h>
Andy Ross1acd8c22018-05-03 14:51:49 -070037#include <misc/rb.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040038
39#ifdef __cplusplus
40extern "C" {
41#endif
42
Anas Nashifbbb157d2017-01-15 08:46:31 -050043/**
44 * @brief Kernel APIs
45 * @defgroup kernel_apis Kernel APIs
46 * @{
47 * @}
48 */
49
Anas Nashif61f4b242016-11-18 10:53:59 -050050#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040051#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
52#else
53#define K_DEBUG(fmt, ...)
54#endif
55
Benjamin Walsh2f280412017-01-14 19:23:46 -050056#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
57#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
58#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
59#elif defined(CONFIG_COOP_ENABLED)
60#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
61#define _NUM_PREEMPT_PRIO (0)
62#elif defined(CONFIG_PREEMPT_ENABLED)
63#define _NUM_COOP_PRIO (0)
64#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
65#else
66#error "invalid configuration"
67#endif
68
69#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040070#define K_PRIO_PREEMPT(x) (x)
71
Benjamin Walsh456c6da2016-09-02 18:55:39 -040072#define K_ANY NULL
73#define K_END NULL
74
Benjamin Walshedb35702017-01-14 18:47:22 -050075#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040076#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050077#elif defined(CONFIG_COOP_ENABLED)
78#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
79#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050081#else
82#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040083#endif
84
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050085#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040086#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
87#else
88#define K_LOWEST_THREAD_PRIO -1
89#endif
90
Benjamin Walshfab8d922016-11-08 15:36:36 -050091#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
92
Benjamin Walsh456c6da2016-09-02 18:55:39 -040093#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
94#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
95
Andy Ross1acd8c22018-05-03 14:51:49 -070096#ifdef CONFIG_WAITQ_FAST
97
98typedef struct {
99 struct _priq_rb waitq;
100} _wait_q_t;
101
102extern int _priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
103
104#define _WAIT_Q_INIT(wait_q) { { { .lessthan_fn = _priq_rb_lessthan } } }
105
106#else
107
Andy Rossccf3bf72018-05-10 11:10:34 -0700108typedef struct {
109 sys_dlist_t waitq;
110} _wait_q_t;
111
112#define _WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400113
Andy Ross1acd8c22018-05-03 14:51:49 -0700114#endif
115
Anas Nashif2f203c22016-12-18 06:57:45 -0500116#ifdef CONFIG_OBJECT_TRACING
117#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
118#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400119#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500120#define _OBJECT_TRACING_INIT
121#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122#endif
123
Benjamin Walshacc68c12017-01-29 18:57:45 -0500124#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300125#define _POLL_EVENT_OBJ_INIT(obj) \
126 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
127#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500128#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300129#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500130#define _POLL_EVENT
131#endif
132
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500133struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400134struct k_mutex;
135struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400136struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400137struct k_msgq;
138struct k_mbox;
139struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200140struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400141struct k_fifo;
142struct k_lifo;
143struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400144struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400145struct k_mem_pool;
146struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500147struct k_poll_event;
148struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800149struct k_mem_domain;
150struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400151
Andrew Boie5bd891d2017-09-27 12:59:28 -0700152/* This enumeration needs to be kept in sync with the lists of kernel objects
153 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
154 * function in kernel/userspace.c
155 */
Andrew Boie945af952017-08-22 13:15:23 -0700156enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700157 K_OBJ_ANY,
158
Leandro Pereirac2003672018-04-04 13:50:32 -0700159 /** @cond
160 * Doxygen should ignore this build-time generated include file
161 * when genrating API documentation. Enumeration values are
162 * generated during build by gen_kobject_list.py. It includes
163 * basic kernel objects (e.g. pipes and mutexes) and driver types.
164 */
165#include <kobj-types-enum.h>
166 /** @endcond
167 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700168
Andrew Boie945af952017-08-22 13:15:23 -0700169 K_OBJ_LAST
170};
171
172#ifdef CONFIG_USERSPACE
173/* Table generated by gperf, these objects are retrieved via
174 * _k_object_find() */
175struct _k_object {
176 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700177 u8_t perms[CONFIG_MAX_THREAD_BYTES];
178 u8_t type;
179 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700180 u32_t data;
Andrew Boiedf555242018-05-25 07:28:54 -0700181} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700182
Andrew Boie877f82e2017-10-17 11:20:22 -0700183struct _k_object_assignment {
184 struct k_thread *thread;
185 void * const *objects;
186};
187
188/**
189 * @brief Grant a static thread access to a list of kernel objects
190 *
191 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
192 * a set of kernel objects. These objects do not need to be in an initialized
193 * state. The permissions will be granted when the threads are initialized
194 * in the early boot sequence.
195 *
196 * All arguments beyond the first must be pointers to kernel objects.
197 *
198 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
199 */
200#define K_THREAD_ACCESS_GRANT(name_, ...) \
201 static void * const _CONCAT(_object_list_, name_)[] = \
202 { __VA_ARGS__, NULL }; \
203 static __used __in_section_unique(object_access) \
204 const struct _k_object_assignment \
205 _CONCAT(_object_access_, name_) = \
206 { (&_k_thread_obj_ ## name_), \
207 (_CONCAT(_object_list_, name_)) }
208
Andrew Boie945af952017-08-22 13:15:23 -0700209#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700210#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700211#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie945af952017-08-22 13:15:23 -0700212
213/**
214 * Lookup a kernel object and init its metadata if it exists
215 *
216 * Calling this on an object will make it usable from userspace.
217 * Intended to be called as the last statement in kernel object init
218 * functions.
219 *
220 * @param object Address of the kernel object
221 */
222void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700223#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700224
225#define K_THREAD_ACCESS_GRANT(thread, ...)
226
Anas Nashif954d5502018-02-25 08:37:28 -0600227/**
228 * @internal
229 */
Andrew Boie743e4682017-10-04 12:25:50 -0700230static inline void _k_object_init(void *obj)
231{
232 ARG_UNUSED(obj);
233}
234
Anas Nashif954d5502018-02-25 08:37:28 -0600235/**
236 * @internal
237 */
Andrew Boie743e4682017-10-04 12:25:50 -0700238static inline void _impl_k_object_access_grant(void *object,
239 struct k_thread *thread)
240{
241 ARG_UNUSED(object);
242 ARG_UNUSED(thread);
243}
244
Anas Nashif954d5502018-02-25 08:37:28 -0600245/**
246 * @internal
247 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700248static inline void k_object_access_revoke(void *object,
249 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700250{
251 ARG_UNUSED(object);
252 ARG_UNUSED(thread);
253}
254
Andrew Boiee9cfc542018-04-13 13:15:28 -0700255/**
256 * @internal
257 */
258static inline void _impl_k_object_release(void *object)
259{
260 ARG_UNUSED(object);
261}
262
Andrew Boie41bab6e2017-10-14 14:42:23 -0700263static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700264{
265 ARG_UNUSED(object);
266}
267#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700268
269/**
270 * grant a thread access to a kernel object
271 *
272 * The thread will be granted access to the object if the caller is from
273 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700274 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700275 *
276 * @param object Address of kernel object
277 * @param thread Thread to grant access to the object
278 */
Andrew Boie743e4682017-10-04 12:25:50 -0700279__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700280
Andrew Boiea89bf012017-10-09 14:47:55 -0700281/**
282 * grant a thread access to a kernel object
283 *
284 * The thread will lose access to the object if the caller is from
285 * supervisor mode, or the caller is from user mode AND has permissions
286 * on both the object and the thread whose access is being revoked.
287 *
288 * @param object Address of kernel object
289 * @param thread Thread to remove access to the object
290 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700291void k_object_access_revoke(void *object, struct k_thread *thread);
292
293
294__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700295
296/**
297 * grant all present and future threads access to an object
298 *
299 * If the caller is from supervisor mode, or the caller is from user mode and
300 * have sufficient permissions on the object, then that object will have
301 * permissions granted to it for *all* current and future threads running in
302 * the system, effectively becoming a public kernel object.
303 *
304 * Use of this API should be avoided on systems that are running untrusted code
305 * as it is possible for such code to derive the addresses of kernel objects
306 * and perform unwanted operations on them.
307 *
Andrew Boie04caa672017-10-13 13:57:07 -0700308 * It is not possible to revoke permissions on public objects; once public,
309 * any thread may use it.
310 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700311 * @param object Address of kernel object
312 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700313void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700314
Andrew Boie31bdfc02017-11-08 16:38:03 -0800315/**
316 * Allocate a kernel object of a designated type
317 *
318 * This will instantiate at runtime a kernel object of the specified type,
319 * returning a pointer to it. The object will be returned in an uninitialized
320 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700321 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800322 *
323 * Currently, allocation of thread stacks is not supported.
324 *
325 * @param otype Requested kernel object type
326 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
327 * available
328 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700329__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800330
Andrew Boie97bf0012018-04-24 17:01:37 -0700331#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800332/**
333 * Free a kernel object previously allocated with k_object_alloc()
334 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700335 * This will return memory for a kernel object back to resource pool it was
336 * allocated from. Care must be exercised that the object will not be used
337 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800338 *
339 * @param obj Pointer to the kernel object memory address.
340 */
341void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700342#else
343static inline void *_impl_k_object_alloc(enum k_objects otype)
344{
Kumar Gala85699f72018-05-17 09:26:03 -0500345 ARG_UNUSED(otype);
346
Andrew Boie97bf0012018-04-24 17:01:37 -0700347 return NULL;
348}
349
350static inline void k_obj_free(void *obj)
351{
352 ARG_UNUSED(obj);
353}
Andrew Boie31bdfc02017-11-08 16:38:03 -0800354#endif /* CONFIG_DYNAMIC_OBJECTS */
355
Andrew Boiebca15da2017-10-15 14:17:48 -0700356/* Using typedef deliberately here, this is quite intended to be an opaque
357 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
358 *
359 * The purpose of this data type is to clearly distinguish between the
360 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
361 * buffer which composes the stack data actually used by the underlying
362 * thread; they cannot be used interchangably as some arches precede the
363 * stack buffer region with guard areas that trigger a MPU or MMU fault
364 * if written to.
365 *
366 * APIs that want to work with the buffer inside should continue to use
367 * char *.
368 *
369 * Stacks should always be created with K_THREAD_STACK_DEFINE().
370 */
371struct __packed _k_thread_stack_element {
372 char data;
373};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700374typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700375
Andrew Boie73abd322017-04-04 13:19:13 -0700376/* timeouts */
377
378struct _timeout;
379typedef void (*_timeout_func_t)(struct _timeout *t);
380
381struct _timeout {
382 sys_dnode_t node;
383 struct k_thread *thread;
384 sys_dlist_t *wait_q;
385 s32_t delta_ticks_from_prev;
386 _timeout_func_t func;
387};
388
389extern s32_t _timeout_remaining_get(struct _timeout *timeout);
390
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700391/**
392 * @typedef k_thread_entry_t
393 * @brief Thread entry point function type.
394 *
395 * A thread's entry point function is invoked when the thread starts executing.
396 * Up to 3 argument values can be passed to the function.
397 *
398 * The thread terminates execution permanently if the entry point function
399 * returns. The thread is responsible for releasing any shared resources
400 * it may own (such as mutexes and dynamically allocated memory), prior to
401 * returning.
402 *
403 * @param p1 First argument.
404 * @param p2 Second argument.
405 * @param p3 Third argument.
406 *
407 * @return N/A
408 */
409typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700410
411#ifdef CONFIG_THREAD_MONITOR
412struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700413 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700414 void *parameter1;
415 void *parameter2;
416 void *parameter3;
417};
418#endif
419
420/* can be used for creating 'dummy' threads, e.g. for pending on objects */
421struct _thread_base {
422
423 /* this thread's entry in a ready/wait queue */
Andy Ross1acd8c22018-05-03 14:51:49 -0700424 union {
425 sys_dlist_t qnode_dlist;
426 struct rbnode qnode_rb;
427 };
428
429#ifdef CONFIG_WAITQ_FAST
430 /* wait queue on which the thread is pended (needed only for
431 * trees, not dumb lists)
432 */
433 _wait_q_t *pended_on;
434#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700435
436 /* user facing 'thread options'; values defined in include/kernel.h */
437 u8_t user_options;
438
439 /* thread state */
440 u8_t thread_state;
441
442 /*
443 * scheduler lock count and thread priority
444 *
445 * These two fields control the preemptibility of a thread.
446 *
447 * When the scheduler is locked, sched_locked is decremented, which
448 * means that the scheduler is locked for values from 0xff to 0x01. A
449 * thread is coop if its prio is negative, thus 0x80 to 0xff when
450 * looked at the value as unsigned.
451 *
452 * By putting them end-to-end, this means that a thread is
453 * non-preemptible if the bundled value is greater than or equal to
454 * 0x0080.
455 */
456 union {
457 struct {
458#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
459 u8_t sched_locked;
460 s8_t prio;
461#else /* LITTLE and PDP */
462 s8_t prio;
463 u8_t sched_locked;
464#endif
465 };
466 u16_t preempt;
467 };
468
Andy Ross4a2e50f2018-05-15 11:06:25 -0700469#ifdef CONFIG_SCHED_DEADLINE
470 int prio_deadline;
471#endif
472
Andy Ross1acd8c22018-05-03 14:51:49 -0700473 u32_t order_key;
474
Andy Ross2724fd12018-01-29 14:55:20 -0800475#ifdef CONFIG_SMP
476 /* True for the per-CPU idle threads */
477 u8_t is_idle;
478
Andy Ross2724fd12018-01-29 14:55:20 -0800479 /* CPU index on which thread was last run */
480 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700481
482 /* Recursive count of irq_lock() calls */
483 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800484#endif
485
Andrew Boie73abd322017-04-04 13:19:13 -0700486 /* data returned by APIs */
487 void *swap_data;
488
489#ifdef CONFIG_SYS_CLOCK_EXISTS
490 /* this thread's entry in a timeout queue */
491 struct _timeout timeout;
492#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700493};
494
495typedef struct _thread_base _thread_base_t;
496
497#if defined(CONFIG_THREAD_STACK_INFO)
498/* Contains the stack information of a thread */
499struct _thread_stack_info {
500 /* Stack Start */
501 u32_t start;
502 /* Stack Size */
503 u32_t size;
504};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700505
506typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700507#endif /* CONFIG_THREAD_STACK_INFO */
508
Chunlin Hane9c97022017-07-07 20:29:30 +0800509#if defined(CONFIG_USERSPACE)
510struct _mem_domain_info {
511 /* memory domain queue node */
512 sys_dnode_t mem_domain_q_node;
513 /* memory domain of the thread */
514 struct k_mem_domain *mem_domain;
515};
516
517#endif /* CONFIG_USERSPACE */
518
Anas Nashifce78d162018-05-24 12:43:11 -0500519/**
520 * @ingroup thread_apis
521 * Thread Structure
522 */
Andrew Boie73abd322017-04-04 13:19:13 -0700523struct k_thread {
524
525 struct _thread_base base;
526
Anas Nashifce78d162018-05-24 12:43:11 -0500527 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700528 struct _caller_saved caller_saved;
Anas Nashifce78d162018-05-24 12:43:11 -0500529 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700530 struct _callee_saved callee_saved;
531
Anas Nashifce78d162018-05-24 12:43:11 -0500532 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700533 void *init_data;
534
Anas Nashifce78d162018-05-24 12:43:11 -0500535 /**
536 * abort function
537 * @req K-THREAD-002
538 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700539 void (*fn_abort)(void);
540
541#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500542 /** thread entry and parameters description */
Andrew Boie73abd322017-04-04 13:19:13 -0700543 struct __thread_entry *entry;
544
Anas Nashifce78d162018-05-24 12:43:11 -0500545 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700546 struct k_thread *next_thread;
547#endif
548
549#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500550 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700551 void *custom_data;
552#endif
553
554#ifdef CONFIG_ERRNO
Anas Nashifce78d162018-05-24 12:43:11 -0500555 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700556 int errno_var;
557#endif
558
559#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500560 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700561 struct _thread_stack_info stack_info;
562#endif /* CONFIG_THREAD_STACK_INFO */
563
Chunlin Hane9c97022017-07-07 20:29:30 +0800564#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500565 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800566 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500567 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700568 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800569#endif /* CONFIG_USERSPACE */
570
Andy Ross042d8ec2017-12-09 08:37:20 -0800571#if defined(CONFIG_USE_SWITCH)
572 /* When using __switch() a few previously arch-specific items
573 * become part of the core OS
574 */
575
Anas Nashifce78d162018-05-24 12:43:11 -0500576 /** _Swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800577 int swap_retval;
578
Anas Nashifce78d162018-05-24 12:43:11 -0500579 /** Context handle returned via _arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800580 void *switch_handle;
581#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500582 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700583 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800584
Anas Nashifce78d162018-05-24 12:43:11 -0500585 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700586 struct _thread_arch arch;
587};
588
589typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400590typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700591#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400592
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400593enum execution_context_types {
594 K_ISR = 0,
595 K_COOP_THREAD,
596 K_PREEMPT_THREAD,
597};
598
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400599/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100600 * @defgroup profiling_apis Profiling APIs
601 * @ingroup kernel_apis
602 * @{
603 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530604typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
605 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100606
607/**
608 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
609 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700610 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100611 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
612 *
613 * CONFIG_MAIN_STACK_SIZE
614 * CONFIG_IDLE_STACK_SIZE
615 * CONFIG_ISR_STACK_SIZE
616 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
617 *
618 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
619 * produce output.
620 *
621 * @return N/A
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530622 *
623 * @deprecated This API is deprecated. Use k_thread_foreach().
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100624 */
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530625__deprecated extern void k_call_stacks_analyze(void);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100626
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530627/**
628 * @brief Iterate over all the threads in the system.
629 *
630 * This routine iterates over all the threads in the system and
631 * calls the user_cb function for each thread.
632 *
633 * @param user_cb Pointer to the user callback function.
634 * @param user_data Pointer to user data.
635 *
636 * @note CONFIG_THREAD_MONITOR must be set for this function
637 * to be effective. Also this API uses irq_lock to protect the
638 * _kernel.threads list which means creation of new threads and
639 * terminations of existing threads are blocked until this
640 * API returns.
641 *
642 * @return N/A
643 */
644extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
645
Anas Nashif166f5192018-02-25 08:02:36 -0600646/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100647
648/**
Allan Stephensc98da842016-11-11 15:45:03 -0500649 * @defgroup thread_apis Thread APIs
650 * @ingroup kernel_apis
651 * @{
652 */
653
Benjamin Walshed240f22017-01-22 13:05:08 -0500654#endif /* !_ASMLANGUAGE */
655
656
657/*
658 * Thread user options. May be needed by assembly code. Common part uses low
659 * bits, arch-specific use high bits.
660 */
661
662/* system thread that must not abort */
663#define K_ESSENTIAL (1 << 0)
664
665#if defined(CONFIG_FP_SHARING)
666/* thread uses floating point registers */
667#define K_FP_REGS (1 << 1)
668#endif
669
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700670/* This thread has dropped from supervisor mode to user mode and consequently
671 * has additional restrictions
672 */
673#define K_USER (1 << 2)
674
Andrew Boie47f8fd12017-10-05 11:11:02 -0700675/* Indicates that the thread being created should inherit all kernel object
676 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
677 * is not enabled.
678 */
679#define K_INHERIT_PERMS (1 << 3)
680
Benjamin Walshed240f22017-01-22 13:05:08 -0500681#ifdef CONFIG_X86
682/* x86 Bitmask definitions for threads user options */
683
684#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
685/* thread uses SSEx (and also FP) registers */
686#define K_SSE_REGS (1 << 7)
687#endif
688#endif
689
690/* end - thread options */
691
692#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400693/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700694 * @brief Create a thread.
695 *
696 * This routine initializes a thread, then schedules it for execution.
697 *
698 * The new thread may be scheduled for immediate execution or a delayed start.
699 * If the newly spawned thread does not have a delayed start the kernel
700 * scheduler may preempt the current thread to allow the new thread to
701 * execute.
702 *
703 * Thread options are architecture-specific, and can include K_ESSENTIAL,
704 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
705 * them using "|" (the logical OR operator).
706 *
707 * Historically, users often would use the beginning of the stack memory region
708 * to store the struct k_thread data, although corruption will occur if the
709 * stack overflows this region and stack protection features may not detect this
710 * situation.
711 *
712 * @param new_thread Pointer to uninitialized struct k_thread
713 * @param stack Pointer to the stack space.
714 * @param stack_size Stack size in bytes.
715 * @param entry Thread entry function.
716 * @param p1 1st entry point parameter.
717 * @param p2 2nd entry point parameter.
718 * @param p3 3rd entry point parameter.
719 * @param prio Thread priority.
720 * @param options Thread options.
721 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
722 *
723 * @return ID of new thread.
724 */
Andrew Boie662c3452017-10-02 10:51:18 -0700725__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700726 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700727 size_t stack_size,
728 k_thread_entry_t entry,
729 void *p1, void *p2, void *p3,
730 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700731
Andrew Boie3f091b52017-08-30 14:34:14 -0700732/**
733 * @brief Drop a thread's privileges permanently to user mode
734 *
735 * @param entry Function to start executing from
736 * @param p1 1st entry point parameter
737 * @param p2 2nd entry point parameter
738 * @param p3 3rd entry point parameter
739 */
740extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
741 void *p1, void *p2,
742 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700743
Andrew Boied26cf2d2017-03-30 13:07:02 -0700744/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700745 * @brief Grant a thread access to a NULL-terminated set of kernel objects
746 *
747 * This is a convenience function. For the provided thread, grant access to
748 * the remaining arguments, which must be pointers to kernel objects.
749 * The final argument must be a NULL.
750 *
751 * The thread object must be initialized (i.e. running). The objects don't
752 * need to be.
753 *
754 * @param thread Thread to grant access to objects
755 * @param ... NULL-terminated list of kernel object pointers
756 */
757extern void __attribute__((sentinel))
758 k_thread_access_grant(struct k_thread *thread, ...);
759
760/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700761 * @brief Assign a resource memory pool to a thread
762 *
763 * By default, threads have no resource pool assigned unless their parent
764 * thread has a resource pool, in which case it is inherited. Multiple
765 * threads may be assigned to the same memory pool.
766 *
767 * Changing a thread's resource pool will not migrate allocations from the
768 * previous pool.
769 *
770 * @param thread Target thread to assign a memory pool for resource requests,
771 * or NULL if the thread should no longer have a memory pool.
772 * @param pool Memory pool to use for resources.
773 */
774static inline void k_thread_resource_pool_assign(struct k_thread *thread,
775 struct k_mem_pool *pool)
776{
777 thread->resource_pool = pool;
778}
779
780#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
781/**
782 * @brief Assign the system heap as a thread's resource pool
783 *
784 * Similar to k_thread_resource_pool_assign(), but the thread will use
785 * the kernel heap to draw memory.
786 *
787 * Use with caution, as a malicious thread could perform DoS attacks on the
788 * kernel heap.
789 *
790 * @param thread Target thread to assign the system heap for resource requests
791 */
792void k_thread_system_pool_assign(struct k_thread *thread);
793#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
794
795/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500796 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400797 *
Allan Stephensc98da842016-11-11 15:45:03 -0500798 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500799 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500801 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400802 *
803 * @return N/A
804 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700805__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400806
807/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500808 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400809 *
810 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500811 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400812 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400813 * @return N/A
814 */
Kumar Galacc334c72017-04-21 10:55:34 -0500815extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400816
817/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500818 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500820 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400821 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500822 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400823 *
824 * @return N/A
825 */
Andrew Boie468190a2017-09-29 14:00:48 -0700826__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827
828/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500829 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500831 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500833 * If @a thread is not currently sleeping, the routine has no effect.
834 *
835 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836 *
837 * @return N/A
838 */
Andrew Boie468190a2017-09-29 14:00:48 -0700839__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400840
841/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500842 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400843 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500844 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400845 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700846__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400847
848/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500849 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400850 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500851 * This routine prevents @a thread from executing if it has not yet started
852 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500854 * @param thread ID of thread to cancel.
855 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700856 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500857 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700858 *
859 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400860 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700861__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400862
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400863/**
Allan Stephensc98da842016-11-11 15:45:03 -0500864 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500866 * This routine permanently stops execution of @a thread. The thread is taken
867 * off all kernel queues it is part of (i.e. the ready queue, the timeout
868 * queue, or a kernel object wait queue). However, any kernel resources the
869 * thread might currently own (such as mutexes or memory blocks) are not
870 * released. It is the responsibility of the caller of this routine to ensure
871 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400872 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500873 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400874 *
875 * @return N/A
876 */
Andrew Boie468190a2017-09-29 14:00:48 -0700877__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400878
Andrew Boie7d627c52017-08-30 11:01:56 -0700879
880/**
881 * @brief Start an inactive thread
882 *
883 * If a thread was created with K_FOREVER in the delay parameter, it will
884 * not be added to the scheduling queue until this function is called
885 * on it.
886 *
887 * @param thread thread to start
888 */
Andrew Boie468190a2017-09-29 14:00:48 -0700889__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700890
Allan Stephensc98da842016-11-11 15:45:03 -0500891/**
892 * @cond INTERNAL_HIDDEN
893 */
894
Benjamin Walshd211a522016-12-06 11:44:01 -0500895/* timeout has timed out and is not on _timeout_q anymore */
896#define _EXPIRED (-2)
897
898/* timeout is not in use */
899#define _INACTIVE (-1)
900
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400901struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700902 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700903 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400904 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700905 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500906 void *init_p1;
907 void *init_p2;
908 void *init_p3;
909 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500910 u32_t init_options;
911 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500912 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400913};
914
Andrew Boied26cf2d2017-03-30 13:07:02 -0700915#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400916 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500917 prio, options, delay, abort, groups) \
918 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700919 .init_thread = (thread), \
920 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500921 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700922 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400923 .init_p1 = (void *)p1, \
924 .init_p2 = (void *)p2, \
925 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500926 .init_prio = (prio), \
927 .init_options = (options), \
928 .init_delay = (delay), \
929 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400930 }
931
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400932/**
Allan Stephensc98da842016-11-11 15:45:03 -0500933 * INTERNAL_HIDDEN @endcond
934 */
935
936/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500937 * @brief Statically define and initialize a thread.
938 *
939 * The thread may be scheduled for immediate execution or a delayed start.
940 *
941 * Thread options are architecture-specific, and can include K_ESSENTIAL,
942 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
943 * them using "|" (the logical OR operator).
944 *
945 * The ID of the thread can be accessed using:
946 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500947 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500948 *
949 * @param name Name of the thread.
950 * @param stack_size Stack size in bytes.
951 * @param entry Thread entry function.
952 * @param p1 1st entry point parameter.
953 * @param p2 2nd entry point parameter.
954 * @param p3 3rd entry point parameter.
955 * @param prio Thread priority.
956 * @param options Thread options.
957 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400958 *
959 * @internal It has been observed that the x86 compiler by default aligns
960 * these _static_thread_data structures to 32-byte boundaries, thereby
961 * wasting space. To work around this, force a 4-byte alignment.
962 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500963#define K_THREAD_DEFINE(name, stack_size, \
964 entry, p1, p2, p3, \
965 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700966 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700967 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500968 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500969 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700970 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
971 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500972 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700973 NULL, 0); \
974 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400975
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400976/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500977 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500979 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400980 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500981 * @param thread ID of thread whose priority is needed.
982 *
983 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400984 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700985__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400986
987/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500988 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400989 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500990 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400991 *
992 * Rescheduling can occur immediately depending on the priority @a thread is
993 * set to:
994 *
995 * - If its priority is raised above the priority of the caller of this
996 * function, and the caller is preemptible, @a thread will be scheduled in.
997 *
998 * - If the caller operates on itself, it lowers its priority below that of
999 * other threads in the system, and the caller is preemptible, the thread of
1000 * highest priority will be scheduled in.
1001 *
1002 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1003 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1004 * highest priority.
1005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001006 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001007 * @param prio New priority.
1008 *
1009 * @warning Changing the priority of a thread currently involved in mutex
1010 * priority inheritance may result in undefined behavior.
1011 *
1012 * @return N/A
1013 */
Andrew Boie468190a2017-09-29 14:00:48 -07001014__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001015
Andy Ross4a2e50f2018-05-15 11:06:25 -07001016
1017#ifdef CONFIG_SCHED_DEADLINE
1018/**
1019 * @brief Set deadline expiration time for scheduler
1020 *
1021 * This sets the "deadline" expiration as a time delta from the
1022 * current time, in the same units used by k_cycle_get_32(). The
1023 * scheduler (when deadline scheduling is enabled) will choose the
1024 * next expiring thread when selecting between threads at the same
1025 * static priority. Threads at different priorities will be scheduled
1026 * according to their static priority.
1027 *
1028 * @note Deadlines that are negative (i.e. in the past) are still seen
1029 * as higher priority than others, even if the thread has "finished"
1030 * its work. If you don't want it scheduled anymore, you have to
1031 * reset the deadline into the future, block/pend the thread, or
1032 * modify its priority with k_thread_priority_set().
1033 *
1034 * @note Despite the API naming, the scheduler makes no guarantees the
1035 * the thread WILL be scheduled within that deadline, nor does it take
1036 * extra metadata (like e.g. the "runtime" and "period" parameters in
1037 * Linux sched_setattr()) that allows the kernel to validate the
1038 * scheduling for achievability. Such features could be implemented
1039 * above this call, which is simply input to the priority selection
1040 * logic.
1041 *
1042 * @param thread A thread on which to set the deadline
1043 * @param deadline A time delta, in cycle units
1044 */
1045__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1046#endif
1047
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001048/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001049 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001050 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001051 * This routine prevents the kernel scheduler from making @a thread the
1052 * current thread. All other internal operations on @a thread are still
1053 * performed; for example, any timeout it is waiting on keeps ticking,
1054 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001055 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001056 * If @a thread is already suspended, the routine has no effect.
1057 *
1058 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001059 *
1060 * @return N/A
1061 */
Andrew Boie468190a2017-09-29 14:00:48 -07001062__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001063
1064/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001065 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001066 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001067 * This routine allows the kernel scheduler to make @a thread the current
1068 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001069 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001070 * If @a thread is not currently suspended, the routine has no effect.
1071 *
1072 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001073 *
1074 * @return N/A
1075 */
Andrew Boie468190a2017-09-29 14:00:48 -07001076__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001077
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001078/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001079 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001080 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001081 * This routine specifies how the scheduler will perform time slicing of
1082 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001083 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001084 * To enable time slicing, @a slice must be non-zero. The scheduler
1085 * ensures that no thread runs for more than the specified time limit
1086 * before other threads of that priority are given a chance to execute.
1087 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001088 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001090 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001091 * execute. Once the scheduler selects a thread for execution, there is no
1092 * minimum guaranteed time the thread will execute before threads of greater or
1093 * equal priority are scheduled.
1094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001095 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001096 * for execution, this routine has no effect; the thread is immediately
1097 * rescheduled after the slice period expires.
1098 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001099 * To disable timeslicing, set both @a slice and @a prio to zero.
1100 *
1101 * @param slice Maximum time slice length (in milliseconds).
1102 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001103 *
1104 * @return N/A
1105 */
Kumar Galacc334c72017-04-21 10:55:34 -05001106extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001107
Anas Nashif166f5192018-02-25 08:02:36 -06001108/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001109
1110/**
1111 * @addtogroup isr_apis
1112 * @{
1113 */
1114
1115/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001116 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001117 *
Allan Stephensc98da842016-11-11 15:45:03 -05001118 * This routine allows the caller to customize its actions, depending on
1119 * whether it is a thread or an ISR.
1120 *
1121 * @note Can be called by ISRs.
1122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001123 * @return 0 if invoked by a thread.
1124 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001125 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001126extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001127
Benjamin Walsh445830d2016-11-10 15:54:27 -05001128/**
1129 * @brief Determine if code is running in a preemptible thread.
1130 *
Allan Stephensc98da842016-11-11 15:45:03 -05001131 * This routine allows the caller to customize its actions, depending on
1132 * whether it can be preempted by another thread. The routine returns a 'true'
1133 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001134 *
Allan Stephensc98da842016-11-11 15:45:03 -05001135 * - The code is running in a thread, not at ISR.
1136 * - The thread's priority is in the preemptible range.
1137 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001138 *
Allan Stephensc98da842016-11-11 15:45:03 -05001139 * @note Can be called by ISRs.
1140 *
1141 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001142 * @return Non-zero if invoked by a preemptible thread.
1143 */
Andrew Boie468190a2017-09-29 14:00:48 -07001144__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001145
Allan Stephensc98da842016-11-11 15:45:03 -05001146/**
Anas Nashif166f5192018-02-25 08:02:36 -06001147 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001148 */
1149
1150/**
1151 * @addtogroup thread_apis
1152 * @{
1153 */
1154
1155/**
1156 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001157 *
Allan Stephensc98da842016-11-11 15:45:03 -05001158 * This routine prevents the current thread from being preempted by another
1159 * thread by instructing the scheduler to treat it as a cooperative thread.
1160 * If the thread subsequently performs an operation that makes it unready,
1161 * it will be context switched out in the normal manner. When the thread
1162 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001163 *
Allan Stephensc98da842016-11-11 15:45:03 -05001164 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001165 *
Allan Stephensc98da842016-11-11 15:45:03 -05001166 * @note k_sched_lock() and k_sched_unlock() should normally be used
1167 * when the operation being performed can be safely interrupted by ISRs.
1168 * However, if the amount of processing involved is very small, better
1169 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001170 *
1171 * @return N/A
1172 */
1173extern void k_sched_lock(void);
1174
Allan Stephensc98da842016-11-11 15:45:03 -05001175/**
1176 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001177 *
Allan Stephensc98da842016-11-11 15:45:03 -05001178 * This routine reverses the effect of a previous call to k_sched_lock().
1179 * A thread must call the routine once for each time it called k_sched_lock()
1180 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001181 *
1182 * @return N/A
1183 */
1184extern void k_sched_unlock(void);
1185
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001186/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001187 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001188 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001189 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001190 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001191 * Custom data is not used by the kernel itself, and is freely available
1192 * for a thread to use as it sees fit. It can be used as a framework
1193 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001195 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001196 *
1197 * @return N/A
1198 */
Andrew Boie468190a2017-09-29 14:00:48 -07001199__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001200
1201/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001202 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001203 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001204 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001205 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001206 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001207 */
Andrew Boie468190a2017-09-29 14:00:48 -07001208__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001209
1210/**
Anas Nashif166f5192018-02-25 08:02:36 -06001211 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001212 */
1213
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001214#include <sys_clock.h>
1215
Allan Stephensc2f15a42016-11-17 12:24:22 -05001216/**
1217 * @addtogroup clock_apis
1218 * @{
1219 */
1220
1221/**
1222 * @brief Generate null timeout delay.
1223 *
1224 * This macro generates a timeout delay that that instructs a kernel API
1225 * not to wait if the requested operation cannot be performed immediately.
1226 *
1227 * @return Timeout delay value.
1228 */
1229#define K_NO_WAIT 0
1230
1231/**
1232 * @brief Generate timeout delay from milliseconds.
1233 *
1234 * This macro generates a timeout delay that that instructs a kernel API
1235 * to wait up to @a ms milliseconds to perform the requested operation.
1236 *
1237 * @param ms Duration in milliseconds.
1238 *
1239 * @return Timeout delay value.
1240 */
Johan Hedberg14471692016-11-13 10:52:15 +02001241#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001242
1243/**
1244 * @brief Generate timeout delay from seconds.
1245 *
1246 * This macro generates a timeout delay that that instructs a kernel API
1247 * to wait up to @a s seconds to perform the requested operation.
1248 *
1249 * @param s Duration in seconds.
1250 *
1251 * @return Timeout delay value.
1252 */
Johan Hedberg14471692016-11-13 10:52:15 +02001253#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001254
1255/**
1256 * @brief Generate timeout delay from minutes.
1257 *
1258 * This macro generates a timeout delay that that instructs a kernel API
1259 * to wait up to @a m minutes to perform the requested operation.
1260 *
1261 * @param m Duration in minutes.
1262 *
1263 * @return Timeout delay value.
1264 */
Johan Hedberg14471692016-11-13 10:52:15 +02001265#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001266
1267/**
1268 * @brief Generate timeout delay from hours.
1269 *
1270 * This macro generates a timeout delay that that instructs a kernel API
1271 * to wait up to @a h hours to perform the requested operation.
1272 *
1273 * @param h Duration in hours.
1274 *
1275 * @return Timeout delay value.
1276 */
Johan Hedberg14471692016-11-13 10:52:15 +02001277#define K_HOURS(h) K_MINUTES((h) * 60)
1278
Allan Stephensc98da842016-11-11 15:45:03 -05001279/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001280 * @brief Generate infinite timeout delay.
1281 *
1282 * This macro generates a timeout delay that that instructs a kernel API
1283 * to wait as long as necessary to perform the requested operation.
1284 *
1285 * @return Timeout delay value.
1286 */
1287#define K_FOREVER (-1)
1288
1289/**
Anas Nashif166f5192018-02-25 08:02:36 -06001290 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001291 */
1292
1293/**
Allan Stephensc98da842016-11-11 15:45:03 -05001294 * @cond INTERNAL_HIDDEN
1295 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001296
Benjamin Walsh62092182016-12-20 14:39:08 -05001297/* kernel clocks */
1298
1299#if (sys_clock_ticks_per_sec == 1000) || \
1300 (sys_clock_ticks_per_sec == 500) || \
1301 (sys_clock_ticks_per_sec == 250) || \
1302 (sys_clock_ticks_per_sec == 125) || \
1303 (sys_clock_ticks_per_sec == 100) || \
1304 (sys_clock_ticks_per_sec == 50) || \
1305 (sys_clock_ticks_per_sec == 25) || \
1306 (sys_clock_ticks_per_sec == 20) || \
1307 (sys_clock_ticks_per_sec == 10) || \
1308 (sys_clock_ticks_per_sec == 1)
1309
1310 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1311#else
1312 /* yields horrible 64-bit math on many architectures: try to avoid */
1313 #define _NON_OPTIMIZED_TICKS_PER_SEC
1314#endif
1315
1316#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001317extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001318#else
Kumar Galacc334c72017-04-21 10:55:34 -05001319static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001320{
Kumar Galacc334c72017-04-21 10:55:34 -05001321 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001322}
1323#endif
1324
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001325/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001326#ifdef CONFIG_TICKLESS_KERNEL
1327#define _TICK_ALIGN 0
1328#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001329#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001330#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001331
Kumar Galacc334c72017-04-21 10:55:34 -05001332static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001333{
Benjamin Walsh62092182016-12-20 14:39:08 -05001334#ifdef CONFIG_SYS_CLOCK_EXISTS
1335
1336#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001337 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001338#else
Kumar Galacc334c72017-04-21 10:55:34 -05001339 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001340#endif
1341
1342#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001343 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001344 return 0;
1345#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001346}
1347
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001348struct k_timer {
1349 /*
1350 * _timeout structure must be first here if we want to use
1351 * dynamic timer allocation. timeout.node is used in the double-linked
1352 * list of free timers
1353 */
1354 struct _timeout timeout;
1355
Allan Stephens45bfa372016-10-12 12:39:42 -05001356 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001357 _wait_q_t wait_q;
1358
1359 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001360 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001361
1362 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001363 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001364
1365 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001366 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001367
Allan Stephens45bfa372016-10-12 12:39:42 -05001368 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001369 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001370
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001371 /* user-specific data, also used to support legacy features */
1372 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001373
Anas Nashif2f203c22016-12-18 06:57:45 -05001374 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001375};
1376
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001377#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001378 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001379 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001380 .timeout.wait_q = NULL, \
1381 .timeout.thread = NULL, \
1382 .timeout.func = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001383 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001384 .expiry_fn = expiry, \
1385 .stop_fn = stop, \
1386 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001387 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001388 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001389 }
1390
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001391#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1392
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001393/**
Allan Stephensc98da842016-11-11 15:45:03 -05001394 * INTERNAL_HIDDEN @endcond
1395 */
1396
1397/**
1398 * @defgroup timer_apis Timer APIs
1399 * @ingroup kernel_apis
1400 * @{
1401 */
1402
1403/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001404 * @typedef k_timer_expiry_t
1405 * @brief Timer expiry function type.
1406 *
1407 * A timer's expiry function is executed by the system clock interrupt handler
1408 * each time the timer expires. The expiry function is optional, and is only
1409 * invoked if the timer has been initialized with one.
1410 *
1411 * @param timer Address of timer.
1412 *
1413 * @return N/A
1414 */
1415typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1416
1417/**
1418 * @typedef k_timer_stop_t
1419 * @brief Timer stop function type.
1420 *
1421 * A timer's stop function is executed if the timer is stopped prematurely.
1422 * The function runs in the context of the thread that stops the timer.
1423 * The stop function is optional, and is only invoked if the timer has been
1424 * initialized with one.
1425 *
1426 * @param timer Address of timer.
1427 *
1428 * @return N/A
1429 */
1430typedef void (*k_timer_stop_t)(struct k_timer *timer);
1431
1432/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001433 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001434 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001435 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001436 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001437 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001438 *
1439 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001440 * @param expiry_fn Function to invoke each time the timer expires.
1441 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001442 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001443#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001444 struct k_timer name \
1445 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001446 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001447
Allan Stephens45bfa372016-10-12 12:39:42 -05001448/**
1449 * @brief Initialize a timer.
1450 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001451 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001452 *
1453 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001454 * @param expiry_fn Function to invoke each time the timer expires.
1455 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001456 *
1457 * @return N/A
1458 */
1459extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001460 k_timer_expiry_t expiry_fn,
1461 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001462
Allan Stephens45bfa372016-10-12 12:39:42 -05001463/**
1464 * @brief Start a timer.
1465 *
1466 * This routine starts a timer, and resets its status to zero. The timer
1467 * begins counting down using the specified duration and period values.
1468 *
1469 * Attempting to start a timer that is already running is permitted.
1470 * The timer's status is reset to zero and the timer begins counting down
1471 * using the new duration and period values.
1472 *
1473 * @param timer Address of timer.
1474 * @param duration Initial timer duration (in milliseconds).
1475 * @param period Timer period (in milliseconds).
1476 *
1477 * @return N/A
1478 */
Andrew Boiea354d492017-09-29 16:22:28 -07001479__syscall void k_timer_start(struct k_timer *timer,
1480 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001481
1482/**
1483 * @brief Stop a timer.
1484 *
1485 * This routine stops a running timer prematurely. The timer's stop function,
1486 * if one exists, is invoked by the caller.
1487 *
1488 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001489 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001490 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001491 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1492 * if @a k_timer_stop is to be called from ISRs.
1493 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001494 * @param timer Address of timer.
1495 *
1496 * @return N/A
1497 */
Andrew Boiea354d492017-09-29 16:22:28 -07001498__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001499
1500/**
1501 * @brief Read timer status.
1502 *
1503 * This routine reads the timer's status, which indicates the number of times
1504 * it has expired since its status was last read.
1505 *
1506 * Calling this routine resets the timer's status to zero.
1507 *
1508 * @param timer Address of timer.
1509 *
1510 * @return Timer status.
1511 */
Andrew Boiea354d492017-09-29 16:22:28 -07001512__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001513
1514/**
1515 * @brief Synchronize thread to timer expiration.
1516 *
1517 * This routine blocks the calling thread until the timer's status is non-zero
1518 * (indicating that it has expired at least once since it was last examined)
1519 * or the timer is stopped. If the timer status is already non-zero,
1520 * or the timer is already stopped, the caller continues without waiting.
1521 *
1522 * Calling this routine resets the timer's status to zero.
1523 *
1524 * This routine must not be used by interrupt handlers, since they are not
1525 * allowed to block.
1526 *
1527 * @param timer Address of timer.
1528 *
1529 * @return Timer status.
1530 */
Andrew Boiea354d492017-09-29 16:22:28 -07001531__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001532
1533/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001534 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001535 *
1536 * This routine computes the (approximate) time remaining before a running
1537 * timer next expires. If the timer is not running, it returns zero.
1538 *
1539 * @param timer Address of timer.
1540 *
1541 * @return Remaining time (in milliseconds).
1542 */
Andrew Boiea354d492017-09-29 16:22:28 -07001543__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1544
1545static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001546{
1547 return _timeout_remaining_get(&timer->timeout);
1548}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001549
Allan Stephensc98da842016-11-11 15:45:03 -05001550/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001551 * @brief Associate user-specific data with a timer.
1552 *
1553 * This routine records the @a user_data with the @a timer, to be retrieved
1554 * later.
1555 *
1556 * It can be used e.g. in a timer handler shared across multiple subsystems to
1557 * retrieve data specific to the subsystem this timer is associated with.
1558 *
1559 * @param timer Address of timer.
1560 * @param user_data User data to associate with the timer.
1561 *
1562 * @return N/A
1563 */
Andrew Boiea354d492017-09-29 16:22:28 -07001564__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1565
Anas Nashif954d5502018-02-25 08:37:28 -06001566/**
1567 * @internal
1568 */
Andrew Boiea354d492017-09-29 16:22:28 -07001569static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1570 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001571{
1572 timer->user_data = user_data;
1573}
1574
1575/**
1576 * @brief Retrieve the user-specific data from a timer.
1577 *
1578 * @param timer Address of timer.
1579 *
1580 * @return The user data.
1581 */
Andrew Boiea354d492017-09-29 16:22:28 -07001582__syscall void *k_timer_user_data_get(struct k_timer *timer);
1583
1584static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001585{
1586 return timer->user_data;
1587}
1588
Anas Nashif166f5192018-02-25 08:02:36 -06001589/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001590
Allan Stephensc98da842016-11-11 15:45:03 -05001591/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001592 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001593 * @{
1594 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001595
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001596/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001597 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001598 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001599 * This routine returns the elapsed time since the system booted,
1600 * in milliseconds.
1601 *
1602 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001603 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001604__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001605
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001606/**
1607 * @brief Enable clock always on in tickless kernel
1608 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001609 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001610 * there are no timer events programmed in tickless kernel
1611 * scheduling. This is necessary if the clock is used to track
1612 * passage of time.
1613 *
1614 * @retval prev_status Previous status of always on flag
1615 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301616#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001617static inline int k_enable_sys_clock_always_on(void)
1618{
1619 int prev_status = _sys_clock_always_on;
1620
1621 _sys_clock_always_on = 1;
1622 _enable_sys_clock();
1623
1624 return prev_status;
1625}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301626#else
1627#define k_enable_sys_clock_always_on() do { } while ((0))
1628#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001629
1630/**
1631 * @brief Disable clock always on in tickless kernel
1632 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001633 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001634 * there are no timer events programmed in tickless kernel
1635 * scheduling. To save power, this routine should be called
1636 * immediately when clock is not used to track time.
1637 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301638#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001639static inline void k_disable_sys_clock_always_on(void)
1640{
1641 _sys_clock_always_on = 0;
1642}
1643#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001644#define k_disable_sys_clock_always_on() do { } while ((0))
1645#endif
1646
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001647/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001648 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001649 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001650 * This routine returns the lower 32-bits of the elapsed time since the system
1651 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001652 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001653 * This routine can be more efficient than k_uptime_get(), as it reduces the
1654 * need for interrupt locking and 64-bit math. However, the 32-bit result
1655 * cannot hold a system uptime time larger than approximately 50 days, so the
1656 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001657 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001658 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001659 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001660__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001661
1662/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001663 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001664 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001665 * This routine computes the elapsed time between the current system uptime
1666 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001667 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001668 * @param reftime Pointer to a reference time, which is updated to the current
1669 * uptime upon return.
1670 *
1671 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001672 */
Kumar Galacc334c72017-04-21 10:55:34 -05001673extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001674
1675/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001676 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001677 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001678 * This routine computes the elapsed time between the current system uptime
1679 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001680 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001681 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1682 * need for interrupt locking and 64-bit math. However, the 32-bit result
1683 * cannot hold an elapsed time larger than approximately 50 days, so the
1684 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001685 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001686 * @param reftime Pointer to a reference time, which is updated to the current
1687 * uptime upon return.
1688 *
1689 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001690 */
Kumar Galacc334c72017-04-21 10:55:34 -05001691extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001692
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001693/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001694 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001695 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001696 * This routine returns the current time, as measured by the system's hardware
1697 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001698 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001699 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001700 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001701#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001702
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001703/**
Anas Nashif166f5192018-02-25 08:02:36 -06001704 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001705 */
1706
Allan Stephensc98da842016-11-11 15:45:03 -05001707/**
1708 * @cond INTERNAL_HIDDEN
1709 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001710
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001711struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001712 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001713 union {
1714 _wait_q_t wait_q;
1715
1716 _POLL_EVENT;
1717 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001718
1719 _OBJECT_TRACING_NEXT_PTR(k_queue);
1720};
1721
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001722#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001723 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001724 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001725 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001726 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001727 _OBJECT_TRACING_INIT \
1728 }
1729
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001730#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1731
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001732extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1733
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001734/**
1735 * INTERNAL_HIDDEN @endcond
1736 */
1737
1738/**
1739 * @defgroup queue_apis Queue APIs
1740 * @ingroup kernel_apis
1741 * @{
1742 */
1743
1744/**
1745 * @brief Initialize a queue.
1746 *
1747 * This routine initializes a queue object, prior to its first use.
1748 *
1749 * @param queue Address of the queue.
1750 *
1751 * @return N/A
1752 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001753__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001754
1755/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001756 * @brief Cancel waiting on a queue.
1757 *
1758 * This routine causes first thread pending on @a queue, if any, to
1759 * return from k_queue_get() call with NULL value (as if timeout expired).
1760 *
1761 * @note Can be called by ISRs.
1762 *
1763 * @param queue Address of the queue.
1764 *
1765 * @return N/A
1766 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001767__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001768
1769/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001770 * @brief Append an element to the end of a queue.
1771 *
1772 * This routine appends a data item to @a queue. A queue data item must be
1773 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1774 * reserved for the kernel's use.
1775 *
1776 * @note Can be called by ISRs.
1777 *
1778 * @param queue Address of the queue.
1779 * @param data Address of the data item.
1780 *
1781 * @return N/A
1782 */
1783extern void k_queue_append(struct k_queue *queue, void *data);
1784
1785/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001786 * @brief Append an element to a queue.
1787 *
1788 * This routine appends a data item to @a queue. There is an implicit
1789 * memory allocation from the calling thread's resource pool, which is
1790 * automatically freed when the item is removed from the queue.
1791 *
1792 * @note Can be called by ISRs.
1793 *
1794 * @param queue Address of the queue.
1795 * @param data Address of the data item.
1796 *
1797 * @retval 0 on success
1798 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1799 */
1800__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1801
1802/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001803 * @brief Prepend an element to a queue.
1804 *
1805 * This routine prepends a data item to @a queue. A queue data item must be
1806 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1807 * reserved for the kernel's use.
1808 *
1809 * @note Can be called by ISRs.
1810 *
1811 * @param queue Address of the queue.
1812 * @param data Address of the data item.
1813 *
1814 * @return N/A
1815 */
1816extern void k_queue_prepend(struct k_queue *queue, void *data);
1817
1818/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001819 * @brief Prepend an element to a queue.
1820 *
1821 * This routine prepends a data item to @a queue. There is an implicit
1822 * memory allocation from the calling thread's resource pool, which is
1823 * automatically freed when the item is removed from the queue.
1824 *
1825 * @note Can be called by ISRs.
1826 *
1827 * @param queue Address of the queue.
1828 * @param data Address of the data item.
1829 *
1830 * @retval 0 on success
1831 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1832 */
1833__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1834
1835/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001836 * @brief Inserts an element to a queue.
1837 *
1838 * This routine inserts a data item to @a queue after previous item. A queue
1839 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1840 * item are reserved for the kernel's use.
1841 *
1842 * @note Can be called by ISRs.
1843 *
1844 * @param queue Address of the queue.
1845 * @param prev Address of the previous data item.
1846 * @param data Address of the data item.
1847 *
1848 * @return N/A
1849 */
1850extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1851
1852/**
1853 * @brief Atomically append a list of elements to a queue.
1854 *
1855 * This routine adds a list of data items to @a queue in one operation.
1856 * The data items must be in a singly-linked list, with the first 32 bits
1857 * in each data item pointing to the next data item; the list must be
1858 * NULL-terminated.
1859 *
1860 * @note Can be called by ISRs.
1861 *
1862 * @param queue Address of the queue.
1863 * @param head Pointer to first node in singly-linked list.
1864 * @param tail Pointer to last node in singly-linked list.
1865 *
1866 * @return N/A
1867 */
1868extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1869
1870/**
1871 * @brief Atomically add a list of elements to a queue.
1872 *
1873 * This routine adds a list of data items to @a queue in one operation.
1874 * The data items must be in a singly-linked list implemented using a
1875 * sys_slist_t object. Upon completion, the original list is empty.
1876 *
1877 * @note Can be called by ISRs.
1878 *
1879 * @param queue Address of the queue.
1880 * @param list Pointer to sys_slist_t object.
1881 *
1882 * @return N/A
1883 */
1884extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1885
1886/**
1887 * @brief Get an element from a queue.
1888 *
1889 * This routine removes first data item from @a queue. The first 32 bits of the
1890 * data item are reserved for the kernel's use.
1891 *
1892 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1893 *
1894 * @param queue Address of the queue.
1895 * @param timeout Waiting period to obtain a data item (in milliseconds),
1896 * or one of the special values K_NO_WAIT and K_FOREVER.
1897 *
1898 * @return Address of the data item if successful; NULL if returned
1899 * without waiting, or waiting period timed out.
1900 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001901__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001902
1903/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001904 * @brief Remove an element from a queue.
1905 *
1906 * This routine removes data item from @a queue. The first 32 bits of the
1907 * data item are reserved for the kernel's use. Removing elements from k_queue
1908 * rely on sys_slist_find_and_remove which is not a constant time operation.
1909 *
1910 * @note Can be called by ISRs
1911 *
1912 * @param queue Address of the queue.
1913 * @param data Address of the data item.
1914 *
1915 * @return true if data item was removed
1916 */
1917static inline bool k_queue_remove(struct k_queue *queue, void *data)
1918{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001919 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001920}
1921
1922/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001923 * @brief Query a queue to see if it has data available.
1924 *
1925 * Note that the data might be already gone by the time this function returns
1926 * if other threads are also trying to read from the queue.
1927 *
1928 * @note Can be called by ISRs.
1929 *
1930 * @param queue Address of the queue.
1931 *
1932 * @return Non-zero if the queue is empty.
1933 * @return 0 if data is available.
1934 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001935__syscall int k_queue_is_empty(struct k_queue *queue);
1936
1937static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001938{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001939 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001940}
1941
1942/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001943 * @brief Peek element at the head of queue.
1944 *
1945 * Return element from the head of queue without removing it.
1946 *
1947 * @param queue Address of the queue.
1948 *
1949 * @return Head element, or NULL if queue is empty.
1950 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001951__syscall void *k_queue_peek_head(struct k_queue *queue);
1952
1953static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001954{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001955 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001956}
1957
1958/**
1959 * @brief Peek element at the tail of queue.
1960 *
1961 * Return element from the tail of queue without removing it.
1962 *
1963 * @param queue Address of the queue.
1964 *
1965 * @return Tail element, or NULL if queue is empty.
1966 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001967__syscall void *k_queue_peek_tail(struct k_queue *queue);
1968
1969static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001970{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001971 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001972}
1973
1974/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001975 * @brief Statically define and initialize a queue.
1976 *
1977 * The queue can be accessed outside the module where it is defined using:
1978 *
1979 * @code extern struct k_queue <name>; @endcode
1980 *
1981 * @param name Name of the queue.
1982 */
1983#define K_QUEUE_DEFINE(name) \
1984 struct k_queue name \
1985 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001986 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001987
Anas Nashif166f5192018-02-25 08:02:36 -06001988/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001989
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001990struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001991 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001992};
1993
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04001994/**
1995 * @cond INTERNAL_HIDDEN
1996 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001997#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001998 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001999 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002000 }
2001
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002002#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
2003
Allan Stephensc98da842016-11-11 15:45:03 -05002004/**
2005 * INTERNAL_HIDDEN @endcond
2006 */
2007
2008/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002009 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002010 * @ingroup kernel_apis
2011 * @{
2012 */
2013
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002014/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002015 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002016 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002017 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002018 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002019 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002020 *
2021 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002022 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002023 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002024#define k_fifo_init(fifo) \
2025 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002026
2027/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002028 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002029 *
2030 * This routine causes first thread pending on @a fifo, if any, to
2031 * return from k_fifo_get() call with NULL value (as if timeout
2032 * expired).
2033 *
2034 * @note Can be called by ISRs.
2035 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002036 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002037 *
2038 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002039 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002040 */
2041#define k_fifo_cancel_wait(fifo) \
2042 k_queue_cancel_wait((struct k_queue *) fifo)
2043
2044/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002045 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002046 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002047 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002048 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2049 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002050 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002051 * @note Can be called by ISRs.
2052 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002053 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002054 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002055 *
2056 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002057 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002058 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002059#define k_fifo_put(fifo, data) \
2060 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002061
2062/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002063 * @brief Add an element to a FIFO queue.
2064 *
2065 * This routine adds a data item to @a fifo. There is an implicit
2066 * memory allocation from the calling thread's resource pool, which is
2067 * automatically freed when the item is removed.
2068 *
2069 * @note Can be called by ISRs.
2070 *
2071 * @param fifo Address of the FIFO.
2072 * @param data Address of the data item.
2073 *
2074 * @retval 0 on success
2075 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002076 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002077 */
2078#define k_fifo_alloc_put(fifo, data) \
2079 k_queue_alloc_append((struct k_queue *) fifo, data)
2080
2081/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002082 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002083 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002084 * This routine adds a list of data items to @a fifo in one operation.
2085 * The data items must be in a singly-linked list, with the first 32 bits
2086 * each data item pointing to the next data item; the list must be
2087 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002089 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002090 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002091 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002092 * @param head Pointer to first node in singly-linked list.
2093 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002094 *
2095 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002096 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002097 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002098#define k_fifo_put_list(fifo, head, tail) \
2099 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002100
2101/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002102 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002104 * This routine adds a list of data items to @a fifo in one operation.
2105 * The data items must be in a singly-linked list implemented using a
2106 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002107 * and must be re-initialized via sys_slist_init().
2108 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002109 * @note Can be called by ISRs.
2110 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002111 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002112 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002113 *
2114 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002115 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002116 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002117#define k_fifo_put_slist(fifo, list) \
2118 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002119
2120/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002121 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002123 * This routine removes a data item from @a fifo in a "first in, first out"
2124 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002125 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002126 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2127 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002128 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002129 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002130 * or one of the special values K_NO_WAIT and K_FOREVER.
2131 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002132 * @return Address of the data item if successful; NULL if returned
2133 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002134 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002135 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002136#define k_fifo_get(fifo, timeout) \
2137 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002138
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002139/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002140 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002141 *
2142 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002143 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002144 *
2145 * @note Can be called by ISRs.
2146 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002147 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002148 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002149 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002150 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002151 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002152 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002153#define k_fifo_is_empty(fifo) \
2154 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002155
2156/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002157 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002158 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002159 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302160 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002161 * on each iteration of processing, a head container will be peeked,
2162 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002163 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002164 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002165 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002166 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002167 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002168 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002169 */
2170#define k_fifo_peek_head(fifo) \
2171 k_queue_peek_head((struct k_queue *) fifo)
2172
2173/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002174 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002175 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002176 * Return element from the tail of FIFO queue (without removing it). A usecase
2177 * for this is if elements of the FIFO queue are themselves containers. Then
2178 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002179 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002180 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002181 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002182 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002183 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002184 */
2185#define k_fifo_peek_tail(fifo) \
2186 k_queue_peek_tail((struct k_queue *) fifo)
2187
2188/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002189 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002190 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002191 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002192 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002193 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002194 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002195 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002196 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002197 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002198#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002199 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002200 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002201 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002202
Anas Nashif166f5192018-02-25 08:02:36 -06002203/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002204
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002205struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002206 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002207};
2208
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002209/**
2210 * @cond INTERNAL_HIDDEN
2211 */
2212
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002213#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002214 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002215 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002216 }
2217
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002218#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2219
Allan Stephensc98da842016-11-11 15:45:03 -05002220/**
2221 * INTERNAL_HIDDEN @endcond
2222 */
2223
2224/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002225 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002226 * @ingroup kernel_apis
2227 * @{
2228 */
2229
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002230/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002231 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002232 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002233 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002234 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002235 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002236 *
2237 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002238 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002239 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002240#define k_lifo_init(lifo) \
2241 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002242
2243/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002244 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002245 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002246 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002247 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2248 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002249 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002250 * @note Can be called by ISRs.
2251 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002252 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002253 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002254 *
2255 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002256 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002257 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002258#define k_lifo_put(lifo, data) \
2259 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002260
2261/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002262 * @brief Add an element to a LIFO queue.
2263 *
2264 * This routine adds a data item to @a lifo. There is an implicit
2265 * memory allocation from the calling thread's resource pool, which is
2266 * automatically freed when the item is removed.
2267 *
2268 * @note Can be called by ISRs.
2269 *
2270 * @param lifo Address of the LIFO.
2271 * @param data Address of the data item.
2272 *
2273 * @retval 0 on success
2274 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002275 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002276 */
2277#define k_lifo_alloc_put(lifo, data) \
2278 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2279
2280/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002281 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002282 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002283 * This routine removes a data item from @a lifo in a "last in, first out"
2284 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002285 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002286 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2287 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002288 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002289 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002290 * or one of the special values K_NO_WAIT and K_FOREVER.
2291 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002292 * @return Address of the data item if successful; NULL if returned
2293 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002294 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002295 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002296#define k_lifo_get(lifo, timeout) \
2297 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002298
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002299/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002300 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002301 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002302 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002303 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002304 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002305 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002306 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002307 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002308 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002309#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002310 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002311 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002312 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002313
Anas Nashif166f5192018-02-25 08:02:36 -06002314/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002315
2316/**
2317 * @cond INTERNAL_HIDDEN
2318 */
Andrew Boief3bee952018-05-02 17:44:39 -07002319#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002320
2321struct k_stack {
2322 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002323 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002324
Anas Nashif2f203c22016-12-18 06:57:45 -05002325 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002326 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002327};
2328
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002329#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002330 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002331 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002332 .base = stack_buffer, \
2333 .next = stack_buffer, \
2334 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002335 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002336 }
2337
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002338#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2339
Allan Stephensc98da842016-11-11 15:45:03 -05002340/**
2341 * INTERNAL_HIDDEN @endcond
2342 */
2343
2344/**
2345 * @defgroup stack_apis Stack APIs
2346 * @ingroup kernel_apis
2347 * @{
2348 */
2349
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002350/**
2351 * @brief Initialize a stack.
2352 *
2353 * This routine initializes a stack object, prior to its first use.
2354 *
2355 * @param stack Address of the stack.
2356 * @param buffer Address of array used to hold stacked values.
2357 * @param num_entries Maximum number of values that can be stacked.
2358 *
2359 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002360 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002361 */
Andrew Boief3bee952018-05-02 17:44:39 -07002362void k_stack_init(struct k_stack *stack,
2363 u32_t *buffer, unsigned int num_entries);
2364
2365
2366/**
2367 * @brief Initialize a stack.
2368 *
2369 * This routine initializes a stack object, prior to its first use. Internal
2370 * buffers will be allocated from the calling thread's resource pool.
2371 * This memory will be released if k_stack_cleanup() is called, or
2372 * userspace is enabled and the stack object loses all references to it.
2373 *
2374 * @param stack Address of the stack.
2375 * @param num_entries Maximum number of values that can be stacked.
2376 *
2377 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002378 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002379 */
2380
2381__syscall int k_stack_alloc_init(struct k_stack *stack,
2382 unsigned int num_entries);
2383
2384/**
2385 * @brief Release a stack's allocated buffer
2386 *
2387 * If a stack object was given a dynamically allocated buffer via
2388 * k_stack_alloc_init(), this will free it. This function does nothing
2389 * if the buffer wasn't dynamically allocated.
2390 *
2391 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002392 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002393 */
2394void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002395
2396/**
2397 * @brief Push an element onto a stack.
2398 *
2399 * This routine adds a 32-bit value @a data to @a stack.
2400 *
2401 * @note Can be called by ISRs.
2402 *
2403 * @param stack Address of the stack.
2404 * @param data Value to push onto the stack.
2405 *
2406 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002407 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002408 */
Andrew Boiee8734462017-09-29 16:42:07 -07002409__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002410
2411/**
2412 * @brief Pop an element from a stack.
2413 *
2414 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2415 * manner and stores the value in @a data.
2416 *
2417 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2418 *
2419 * @param stack Address of the stack.
2420 * @param data Address of area to hold the value popped from the stack.
2421 * @param timeout Waiting period to obtain a value (in milliseconds),
2422 * or one of the special values K_NO_WAIT and K_FOREVER.
2423 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002424 * @retval 0 Element popped from stack.
2425 * @retval -EBUSY Returned without waiting.
2426 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002427 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002428 */
Andrew Boiee8734462017-09-29 16:42:07 -07002429__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002430
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002431/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002432 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002433 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002434 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002435 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002436 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002437 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002438 * @param name Name of the stack.
2439 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002440 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002441 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002442#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002443 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002444 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002445 struct k_stack name \
2446 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002447 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002448 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002449
Anas Nashif166f5192018-02-25 08:02:36 -06002450/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002451
Allan Stephens6bba9b02016-11-16 14:56:54 -05002452struct k_work;
2453
Allan Stephensc98da842016-11-11 15:45:03 -05002454/**
2455 * @defgroup workqueue_apis Workqueue Thread APIs
2456 * @ingroup kernel_apis
2457 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002458 */
2459
Allan Stephens6bba9b02016-11-16 14:56:54 -05002460/**
2461 * @typedef k_work_handler_t
2462 * @brief Work item handler function type.
2463 *
2464 * A work item's handler function is executed by a workqueue's thread
2465 * when the work item is processed by the workqueue.
2466 *
2467 * @param work Address of the work item.
2468 *
2469 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002470 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002471 */
2472typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002473
2474/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002475 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002476 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002477
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002478struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002479 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002480 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002481};
2482
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002483enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002484 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002485};
2486
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002487struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002488 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002489 k_work_handler_t handler;
2490 atomic_t flags[1];
2491};
2492
Allan Stephens6bba9b02016-11-16 14:56:54 -05002493struct k_delayed_work {
2494 struct k_work work;
2495 struct _timeout timeout;
2496 struct k_work_q *work_q;
2497};
2498
2499extern struct k_work_q k_sys_work_q;
2500
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002501/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002502 * INTERNAL_HIDDEN @endcond
2503 */
2504
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002505#define _K_WORK_INITIALIZER(work_handler) \
2506 { \
2507 ._reserved = NULL, \
2508 .handler = work_handler, \
2509 .flags = { 0 } \
2510 }
2511
2512#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2513
Allan Stephens6bba9b02016-11-16 14:56:54 -05002514/**
2515 * @brief Initialize a statically-defined work item.
2516 *
2517 * This macro can be used to initialize a statically-defined workqueue work
2518 * item, prior to its first use. For example,
2519 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002520 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002521 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002522 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002523 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002524 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002525 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002526#define K_WORK_DEFINE(work, work_handler) \
2527 struct k_work work \
2528 __in_section(_k_work, static, work) = \
2529 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002530
2531/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002532 * @brief Initialize a work item.
2533 *
2534 * This routine initializes a workqueue work item, prior to its first use.
2535 *
2536 * @param work Address of work item.
2537 * @param handler Function to invoke each time work item is processed.
2538 *
2539 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002540 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002541 */
2542static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2543{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002544 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002545 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002546 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002547}
2548
2549/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002550 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002551 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002552 * This routine submits work item @a work to be processed by workqueue
2553 * @a work_q. If the work item is already pending in the workqueue's queue
2554 * as a result of an earlier submission, this routine has no effect on the
2555 * work item. If the work item has already been processed, or is currently
2556 * being processed, its work is considered complete and the work item can be
2557 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002558 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002559 * @warning
2560 * A submitted work item must not be modified until it has been processed
2561 * by the workqueue.
2562 *
2563 * @note Can be called by ISRs.
2564 *
2565 * @param work_q Address of workqueue.
2566 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002567 *
2568 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002569 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002570 */
2571static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2572 struct k_work *work)
2573{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002574 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002575 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002576 }
2577}
2578
2579/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002580 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002581 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002582 * This routine indicates if work item @a work is pending in a workqueue's
2583 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002584 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002585 * @note Can be called by ISRs.
2586 *
2587 * @param work Address of work item.
2588 *
2589 * @return 1 if work item is pending, or 0 if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002590 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002591 */
2592static inline int k_work_pending(struct k_work *work)
2593{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002594 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002595}
2596
2597/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002598 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002599 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002600 * This routine starts workqueue @a work_q. The workqueue spawns its work
2601 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002602 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002603 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002604 * @param stack Pointer to work queue thread's stack space, as defined by
2605 * K_THREAD_STACK_DEFINE()
2606 * @param stack_size Size of the work queue thread's stack (in bytes), which
2607 * should either be the same constant passed to
2608 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002609 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002610 *
2611 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002612 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002613 */
Andrew Boie507852a2017-07-25 18:47:07 -07002614extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002615 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002616 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002617
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002618/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002619 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002620 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002621 * This routine initializes a workqueue delayed work item, prior to
2622 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002623 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002624 * @param work Address of delayed work item.
2625 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002626 *
2627 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002628 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002629 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002630extern void k_delayed_work_init(struct k_delayed_work *work,
2631 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002632
2633/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002634 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002635 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002636 * This routine schedules work item @a work to be processed by workqueue
2637 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002638 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002639 * Only when the countdown completes is the work item actually submitted to
2640 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002641 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002642 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002643 * counting down cancels the existing submission and restarts the
2644 * countdown using the new delay. Note that this behavior is
2645 * inherently subject to race conditions with the pre-existing
2646 * timeouts and work queue, so care must be taken to synchronize such
2647 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002648 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002649 * @warning
2650 * A delayed work item must not be modified until it has been processed
2651 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002652 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002653 * @note Can be called by ISRs.
2654 *
2655 * @param work_q Address of workqueue.
2656 * @param work Address of delayed work item.
2657 * @param delay Delay before submitting the work item (in milliseconds).
2658 *
2659 * @retval 0 Work item countdown started.
2660 * @retval -EINPROGRESS Work item is already pending.
2661 * @retval -EINVAL Work item is being processed or has completed its work.
2662 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002663 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002664 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002665extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2666 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002667 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002668
2669/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002670 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002671 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002672 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002673 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002674 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002675 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002676 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002677 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002678 * @param work Address of delayed work item.
2679 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002680 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002681 * @retval -EINPROGRESS Work item is already pending.
2682 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002683 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002684 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002685extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002686
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002687/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002688 * @brief Submit a work item to the system workqueue.
2689 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002690 * This routine submits work item @a work to be processed by the system
2691 * workqueue. If the work item is already pending in the workqueue's queue
2692 * as a result of an earlier submission, this routine has no effect on the
2693 * work item. If the work item has already been processed, or is currently
2694 * being processed, its work is considered complete and the work item can be
2695 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002696 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002697 * @warning
2698 * Work items submitted to the system workqueue should avoid using handlers
2699 * that block or yield since this may prevent the system workqueue from
2700 * processing other work items in a timely manner.
2701 *
2702 * @note Can be called by ISRs.
2703 *
2704 * @param work Address of work item.
2705 *
2706 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002707 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002708 */
2709static inline void k_work_submit(struct k_work *work)
2710{
2711 k_work_submit_to_queue(&k_sys_work_q, work);
2712}
2713
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002714/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002715 * @brief Submit a delayed work item to the system workqueue.
2716 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002717 * This routine schedules work item @a work to be processed by the system
2718 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002719 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002720 * Only when the countdown completes is the work item actually submitted to
2721 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002722 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002723 * Submitting a previously submitted delayed work item that is still
2724 * counting down cancels the existing submission and restarts the countdown
2725 * using the new delay. If the work item is currently pending on the
2726 * workqueue's queue because the countdown has completed it is too late to
2727 * resubmit the item, and resubmission fails without impacting the work item.
2728 * If the work item has already been processed, or is currently being processed,
2729 * its work is considered complete and the work item can be resubmitted.
2730 *
2731 * @warning
2732 * Work items submitted to the system workqueue should avoid using handlers
2733 * that block or yield since this may prevent the system workqueue from
2734 * processing other work items in a timely manner.
2735 *
2736 * @note Can be called by ISRs.
2737 *
2738 * @param work Address of delayed work item.
2739 * @param delay Delay before submitting the work item (in milliseconds).
2740 *
2741 * @retval 0 Work item countdown started.
2742 * @retval -EINPROGRESS Work item is already pending.
2743 * @retval -EINVAL Work item is being processed or has completed its work.
2744 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002745 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002746 */
2747static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002748 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002749{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002750 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002751}
2752
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002753/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002754 * @brief Get time remaining before a delayed work gets scheduled.
2755 *
2756 * This routine computes the (approximate) time remaining before a
2757 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002758 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002759 *
2760 * @param work Delayed work item.
2761 *
2762 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002763 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002764 */
Kumar Galacc334c72017-04-21 10:55:34 -05002765static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002766{
2767 return _timeout_remaining_get(&work->timeout);
2768}
2769
Anas Nashif166f5192018-02-25 08:02:36 -06002770/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002771/**
Anas Nashifce78d162018-05-24 12:43:11 -05002772 * @defgroup mutex_apis Mutex APIs
2773 * @ingroup kernel_apis
2774 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002775 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002776
Anas Nashifce78d162018-05-24 12:43:11 -05002777/**
2778 * Mutex Structure
2779 * @ingroup mutex_apis
2780 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002781struct k_mutex {
2782 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002783 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002784 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002785 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002786 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002787
Anas Nashif2f203c22016-12-18 06:57:45 -05002788 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002789};
2790
Anas Nashifce78d162018-05-24 12:43:11 -05002791/**
2792 * @cond INTERNAL_HIDDEN
2793 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002794#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002795 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002796 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002797 .owner = NULL, \
2798 .lock_count = 0, \
2799 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002800 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002801 }
2802
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002803#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2804
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002805/**
Allan Stephensc98da842016-11-11 15:45:03 -05002806 * INTERNAL_HIDDEN @endcond
2807 */
2808
2809/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002810 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002812 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002813 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002814 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002816 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002817 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002818 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002819#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002820 struct k_mutex name \
2821 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002822 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002823
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002824/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002825 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002826 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002827 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002828 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002829 * Upon completion, the mutex is available and does not have an owner.
2830 *
2831 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002832 *
2833 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002834 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002835 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002836__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002837
2838/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002839 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002841 * This routine locks @a mutex. If the mutex is locked by another thread,
2842 * the calling thread waits until the mutex becomes available or until
2843 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002845 * A thread is permitted to lock a mutex it has already locked. The operation
2846 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002848 * @param mutex Address of the mutex.
2849 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002850 * or one of the special values K_NO_WAIT and K_FOREVER.
2851 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002852 * @retval 0 Mutex locked.
2853 * @retval -EBUSY Returned without waiting.
2854 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002855 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002856 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002857__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002858
2859/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002860 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002862 * This routine unlocks @a mutex. The mutex must already be locked by the
2863 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002864 *
2865 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002866 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002867 * thread.
2868 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002869 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002870 *
2871 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002872 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002873 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002874__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002875
Allan Stephensc98da842016-11-11 15:45:03 -05002876/**
Anas Nashif166f5192018-02-25 08:02:36 -06002877 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002878 */
2879
2880/**
2881 * @cond INTERNAL_HIDDEN
2882 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002883
2884struct k_sem {
2885 _wait_q_t wait_q;
2886 unsigned int count;
2887 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002888 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002889
Anas Nashif2f203c22016-12-18 06:57:45 -05002890 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002891};
2892
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002893#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002894 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002895 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002896 .count = initial_count, \
2897 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002898 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002899 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002900 }
2901
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002902#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2903
Allan Stephensc98da842016-11-11 15:45:03 -05002904/**
2905 * INTERNAL_HIDDEN @endcond
2906 */
2907
2908/**
2909 * @defgroup semaphore_apis Semaphore APIs
2910 * @ingroup kernel_apis
2911 * @{
2912 */
2913
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002914/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002915 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002917 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002918 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002919 * @param sem Address of the semaphore.
2920 * @param initial_count Initial semaphore count.
2921 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002922 *
2923 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002924 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002925 */
Andrew Boie99280232017-09-29 14:17:47 -07002926__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2927 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002928
2929/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002930 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002932 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002934 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2935 *
2936 * @param sem Address of the semaphore.
2937 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002938 * or one of the special values K_NO_WAIT and K_FOREVER.
2939 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002940 * @note When porting code from the nanokernel legacy API to the new API, be
2941 * careful with the return value of this function. The return value is the
2942 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2943 * non-zero means failure, while the nano_sem_take family returns 1 for success
2944 * and 0 for failure.
2945 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002946 * @retval 0 Semaphore taken.
2947 * @retval -EBUSY Returned without waiting.
2948 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002949 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002950 */
Andrew Boie99280232017-09-29 14:17:47 -07002951__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002952
2953/**
2954 * @brief Give a semaphore.
2955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002956 * This routine gives @a sem, unless the semaphore is already at its maximum
2957 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002958 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002959 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002960 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002961 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002962 *
2963 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002964 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002965 */
Andrew Boie99280232017-09-29 14:17:47 -07002966__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002967
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002968/**
2969 * @brief Reset a semaphore's count to zero.
2970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002971 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002973 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002974 *
2975 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002976 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002977 */
Andrew Boie990bf162017-10-03 12:36:49 -07002978__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002979
Anas Nashif954d5502018-02-25 08:37:28 -06002980/**
2981 * @internal
2982 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002983static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002984{
2985 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002986}
2987
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002988/**
2989 * @brief Get a semaphore's count.
2990 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002991 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002993 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002994 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002995 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002996 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002997 */
Andrew Boie990bf162017-10-03 12:36:49 -07002998__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002999
Anas Nashif954d5502018-02-25 08:37:28 -06003000/**
3001 * @internal
3002 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003003static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003004{
3005 return sem->count;
3006}
3007
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003008/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003009 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003011 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003012 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003013 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003015 * @param name Name of the semaphore.
3016 * @param initial_count Initial semaphore count.
3017 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003018 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003019 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003020#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003021 struct k_sem name \
3022 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003023 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303024 BUILD_ASSERT(((count_limit) != 0) && \
3025 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003026
Anas Nashif166f5192018-02-25 08:02:36 -06003027/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003028
3029/**
3030 * @defgroup alert_apis Alert APIs
3031 * @ingroup kernel_apis
3032 * @{
3033 */
3034
Allan Stephens5eceb852016-11-16 10:16:30 -05003035/**
3036 * @typedef k_alert_handler_t
3037 * @brief Alert handler function type.
3038 *
3039 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07003040 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05003041 * and is only invoked if the alert has been initialized with one.
3042 *
3043 * @param alert Address of the alert.
3044 *
3045 * @return 0 if alert has been consumed; non-zero if alert should pend.
3046 */
3047typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05003048
Anas Nashif166f5192018-02-25 08:02:36 -06003049/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003050
3051/**
3052 * @cond INTERNAL_HIDDEN
3053 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003054
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003055#define K_ALERT_DEFAULT NULL
3056#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003057
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003058struct k_alert {
3059 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003060 atomic_t send_count;
3061 struct k_work work_item;
3062 struct k_sem sem;
3063
Anas Nashif2f203c22016-12-18 06:57:45 -05003064 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003065};
3066
Anas Nashif954d5502018-02-25 08:37:28 -06003067/**
3068 * @internal
3069 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003070extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003071
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003072#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003073 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003074 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003075 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003076 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
3077 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003078 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003079 }
3080
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003081#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
3082
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003083/**
Allan Stephensc98da842016-11-11 15:45:03 -05003084 * INTERNAL_HIDDEN @endcond
3085 */
3086
3087/**
3088 * @addtogroup alert_apis
3089 * @{
3090 */
3091
3092/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003093 * @def K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts)
3094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003095 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003096 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003097 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003098 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003099 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003101 * @param name Name of the alert.
3102 * @param alert_handler Action to take when alert is sent. Specify either
3103 * the address of a function to be invoked by the system workqueue
3104 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
3105 * K_ALERT_DEFAULT (which causes the alert to pend).
3106 * @param max_num_pending_alerts Maximum number of pending alerts.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003107 *
3108 * @req K-ALERT-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003109 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003110#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003111 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003112 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003113 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003114 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003115
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003116/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003117 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003119 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003120 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003121 * @param alert Address of the alert.
3122 * @param handler Action to take when alert is sent. Specify either the address
3123 * of a function to be invoked by the system workqueue thread,
3124 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3125 * K_ALERT_DEFAULT (which causes the alert to pend).
3126 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003127 *
3128 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003129 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003130 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003131extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3132 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003133
3134/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003135 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003137 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003139 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3140 *
3141 * @param alert Address of the alert.
3142 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003143 * or one of the special values K_NO_WAIT and K_FOREVER.
3144 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003145 * @retval 0 Alert received.
3146 * @retval -EBUSY Returned without waiting.
3147 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003148 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003149 */
Andrew Boie310e9872017-09-29 04:41:15 -07003150__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003151
3152/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003153 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003154 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003155 * This routine signals @a alert. The action specified for @a alert will
3156 * be taken, which may trigger the execution of an alert handler function
3157 * and/or cause the alert to pend (assuming the alert has not reached its
3158 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003159 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003160 * @note Can be called by ISRs.
3161 *
3162 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003163 *
3164 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003165 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003166 */
Andrew Boie310e9872017-09-29 04:41:15 -07003167__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003168
3169/**
Anas Nashif166f5192018-02-25 08:02:36 -06003170 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003171 */
3172
Allan Stephensc98da842016-11-11 15:45:03 -05003173/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003174 * @defgroup msgq_apis Message Queue APIs
3175 * @ingroup kernel_apis
3176 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003177 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003178
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003179/**
3180 * @brief Message Queue Structure
3181 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003182struct k_msgq {
3183 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003184 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003185 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003186 char *buffer_start;
3187 char *buffer_end;
3188 char *read_ptr;
3189 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003190 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003191
Anas Nashif2f203c22016-12-18 06:57:45 -05003192 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003193 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003194};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003195/**
3196 * @cond INTERNAL_HIDDEN
3197 */
3198
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003199
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003200#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003201 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003202 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003203 .max_msgs = q_max_msgs, \
3204 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003205 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003206 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003207 .read_ptr = q_buffer, \
3208 .write_ptr = q_buffer, \
3209 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003210 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003211 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003212#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003213/**
3214 * INTERNAL_HIDDEN @endcond
3215 */
3216
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003217
Andrew Boie0fe789f2018-04-12 18:35:56 -07003218#define K_MSGQ_FLAG_ALLOC BIT(0)
3219
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003220/**
3221 * @brief Message Queue Attributes
3222 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303223struct k_msgq_attrs {
3224 size_t msg_size;
3225 u32_t max_msgs;
3226 u32_t used_msgs;
3227};
3228
Allan Stephensc98da842016-11-11 15:45:03 -05003229
3230/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003231 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003233 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3234 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003235 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3236 * message is similarly aligned to this boundary, @a q_msg_size must also be
3237 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003238 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003239 * The message queue can be accessed outside the module where it is defined
3240 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003241 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003242 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003243 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003244 * @param q_name Name of the message queue.
3245 * @param q_msg_size Message size (in bytes).
3246 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003247 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003248 *
3249 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003250 */
3251#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003252 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003253 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003254 struct k_msgq q_name \
3255 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003256 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003257 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003258
Peter Mitsisd7a37502016-10-13 11:37:40 -04003259/**
3260 * @brief Initialize a message queue.
3261 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003262 * This routine initializes a message queue object, prior to its first use.
3263 *
Allan Stephensda827222016-11-09 14:23:58 -06003264 * The message queue's ring buffer must contain space for @a max_msgs messages,
3265 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3266 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3267 * that each message is similarly aligned to this boundary, @a q_msg_size
3268 * must also be a multiple of N.
3269 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003270 * @param q Address of the message queue.
3271 * @param buffer Pointer to ring buffer that holds queued messages.
3272 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003273 * @param max_msgs Maximum number of messages that can be queued.
3274 *
3275 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003276 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003277 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003278void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3279 u32_t max_msgs);
3280
3281/**
3282 * @brief Initialize a message queue.
3283 *
3284 * This routine initializes a message queue object, prior to its first use,
3285 * allocating its internal ring buffer from the calling thread's resource
3286 * pool.
3287 *
3288 * Memory allocated for the ring buffer can be released by calling
3289 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3290 * all of its references.
3291 *
3292 * @param q Address of the message queue.
3293 * @param msg_size Message size (in bytes).
3294 * @param max_msgs Maximum number of messages that can be queued.
3295 *
3296 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3297 * thread's resource pool, or -EINVAL if the size parameters cause
3298 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003299 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003300 */
3301__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3302 u32_t max_msgs);
3303
3304
3305void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003306
3307/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003308 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003309 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003310 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003311 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003312 * @note Can be called by ISRs.
3313 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003314 * @param q Address of the message queue.
3315 * @param data Pointer to the message.
3316 * @param timeout Waiting period to add the message (in milliseconds),
3317 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003318 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003319 * @retval 0 Message sent.
3320 * @retval -ENOMSG Returned without waiting or queue purged.
3321 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003322 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003323 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003324__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003325
3326/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003327 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003328 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003329 * This routine receives a message from message queue @a q in a "first in,
3330 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003331 *
Allan Stephensc98da842016-11-11 15:45:03 -05003332 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003333 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003334 * @param q Address of the message queue.
3335 * @param data Address of area to hold the received message.
3336 * @param timeout Waiting period to receive the message (in milliseconds),
3337 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003338 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003339 * @retval 0 Message received.
3340 * @retval -ENOMSG Returned without waiting.
3341 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003342 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003343 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003344__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003345
3346/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003347 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003348 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003349 * This routine discards all unreceived messages in a message queue's ring
3350 * buffer. Any threads that are blocked waiting to send a message to the
3351 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003353 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003354 *
3355 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003356 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003357 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003358__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003359
Peter Mitsis67be2492016-10-07 11:44:34 -04003360/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003361 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003362 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003363 * This routine returns the number of unused entries in a message queue's
3364 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003365 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003366 * @param q Address of the message queue.
3367 *
3368 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003369 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003370 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003371__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3372
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303373/**
3374 * @brief Get basic attributes of a message queue.
3375 *
3376 * This routine fetches basic attributes of message queue into attr argument.
3377 *
3378 * @param q Address of the message queue.
3379 * @param attrs pointer to message queue attribute structure.
3380 *
3381 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003382 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303383 */
3384__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3385
3386
Andrew Boie82edb6e2017-10-02 10:53:06 -07003387static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003388{
3389 return q->max_msgs - q->used_msgs;
3390}
3391
Peter Mitsisd7a37502016-10-13 11:37:40 -04003392/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003393 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003394 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003395 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003397 * @param q Address of the message queue.
3398 *
3399 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003400 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003401 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003402__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3403
3404static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003405{
3406 return q->used_msgs;
3407}
3408
Anas Nashif166f5192018-02-25 08:02:36 -06003409/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003410
3411/**
3412 * @defgroup mem_pool_apis Memory Pool APIs
3413 * @ingroup kernel_apis
3414 * @{
3415 */
3416
Andy Ross73cb9582017-05-09 10:42:39 -07003417/* Note on sizing: the use of a 20 bit field for block means that,
3418 * assuming a reasonable minimum block size of 16 bytes, we're limited
3419 * to 16M of memory managed by a single pool. Long term it would be
3420 * good to move to a variable bit size based on configuration.
3421 */
3422struct k_mem_block_id {
3423 u32_t pool : 8;
3424 u32_t level : 4;
3425 u32_t block : 20;
3426};
3427
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003428struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003429 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003430 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003431};
3432
Anas Nashif166f5192018-02-25 08:02:36 -06003433/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003434
3435/**
3436 * @defgroup mailbox_apis Mailbox APIs
3437 * @ingroup kernel_apis
3438 * @{
3439 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003440
3441struct k_mbox_msg {
3442 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003443 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003444 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003445 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003446 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003447 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003448 /** sender's message data buffer */
3449 void *tx_data;
3450 /** internal use only - needed for legacy API support */
3451 void *_rx_data;
3452 /** message data block descriptor */
3453 struct k_mem_block tx_block;
3454 /** source thread id */
3455 k_tid_t rx_source_thread;
3456 /** target thread id */
3457 k_tid_t tx_target_thread;
3458 /** internal use only - thread waiting on send (may be a dummy) */
3459 k_tid_t _syncing_thread;
3460#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3461 /** internal use only - semaphore used during asynchronous send */
3462 struct k_sem *_async_sem;
3463#endif
3464};
3465
3466struct k_mbox {
3467 _wait_q_t tx_msg_queue;
3468 _wait_q_t rx_msg_queue;
3469
Anas Nashif2f203c22016-12-18 06:57:45 -05003470 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003471};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003472/**
3473 * @cond INTERNAL_HIDDEN
3474 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003475
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003476#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003477 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003478 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3479 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003480 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003481 }
3482
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003483#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3484
Peter Mitsis12092702016-10-14 12:57:23 -04003485/**
Allan Stephensc98da842016-11-11 15:45:03 -05003486 * INTERNAL_HIDDEN @endcond
3487 */
3488
3489/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003490 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003492 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003493 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003494 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003495 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003496 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003497 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003498 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003499#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003500 struct k_mbox name \
3501 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003502 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003503
Peter Mitsis12092702016-10-14 12:57:23 -04003504/**
3505 * @brief Initialize a mailbox.
3506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003507 * This routine initializes a mailbox object, prior to its first use.
3508 *
3509 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003510 *
3511 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003512 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003513 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003514extern void k_mbox_init(struct k_mbox *mbox);
3515
Peter Mitsis12092702016-10-14 12:57:23 -04003516/**
3517 * @brief Send a mailbox message in a synchronous manner.
3518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003519 * This routine sends a message to @a mbox and waits for a receiver to both
3520 * receive and process it. The message data may be in a buffer, in a memory
3521 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003522 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003523 * @param mbox Address of the mailbox.
3524 * @param tx_msg Address of the transmit message descriptor.
3525 * @param timeout Waiting period for the message to be received (in
3526 * milliseconds), or one of the special values K_NO_WAIT
3527 * and K_FOREVER. Once the message has been received,
3528 * this routine waits as long as necessary for the message
3529 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003530 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003531 * @retval 0 Message sent.
3532 * @retval -ENOMSG Returned without waiting.
3533 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003534 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003535 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003536extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003537 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003538
Peter Mitsis12092702016-10-14 12:57:23 -04003539/**
3540 * @brief Send a mailbox message in an asynchronous manner.
3541 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003542 * This routine sends a message to @a mbox without waiting for a receiver
3543 * to process it. The message data may be in a buffer, in a memory pool block,
3544 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3545 * will be given when the message has been both received and completely
3546 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003547 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003548 * @param mbox Address of the mailbox.
3549 * @param tx_msg Address of the transmit message descriptor.
3550 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003551 *
3552 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003553 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003554 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003555extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003556 struct k_sem *sem);
3557
Peter Mitsis12092702016-10-14 12:57:23 -04003558/**
3559 * @brief Receive a mailbox message.
3560 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003561 * This routine receives a message from @a mbox, then optionally retrieves
3562 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003563 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003564 * @param mbox Address of the mailbox.
3565 * @param rx_msg Address of the receive message descriptor.
3566 * @param buffer Address of the buffer to receive data, or NULL to defer data
3567 * retrieval and message disposal until later.
3568 * @param timeout Waiting period for a message to be received (in
3569 * milliseconds), or one of the special values K_NO_WAIT
3570 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003571 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003572 * @retval 0 Message received.
3573 * @retval -ENOMSG Returned without waiting.
3574 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003575 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003576 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003577extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003578 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003579
3580/**
3581 * @brief Retrieve mailbox message data into a buffer.
3582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003583 * This routine completes the processing of a received message by retrieving
3584 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003585 *
3586 * Alternatively, this routine can be used to dispose of a received message
3587 * without retrieving its data.
3588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003589 * @param rx_msg Address of the receive message descriptor.
3590 * @param buffer Address of the buffer to receive data, or NULL to discard
3591 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003592 *
3593 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003594 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003595 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003596extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003597
3598/**
3599 * @brief Retrieve mailbox message data into a memory pool block.
3600 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003601 * This routine completes the processing of a received message by retrieving
3602 * its data into a memory pool block, then disposing of the message.
3603 * The memory pool block that results from successful retrieval must be
3604 * returned to the pool once the data has been processed, even in cases
3605 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003606 *
3607 * Alternatively, this routine can be used to dispose of a received message
3608 * without retrieving its data. In this case there is no need to return a
3609 * memory pool block to the pool.
3610 *
3611 * This routine allocates a new memory pool block for the data only if the
3612 * data is not already in one. If a new block cannot be allocated, the routine
3613 * returns a failure code and the received message is left unchanged. This
3614 * permits the caller to reattempt data retrieval at a later time or to dispose
3615 * of the received message without retrieving its data.
3616 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003617 * @param rx_msg Address of a receive message descriptor.
3618 * @param pool Address of memory pool, or NULL to discard data.
3619 * @param block Address of the area to hold memory pool block info.
3620 * @param timeout Waiting period to wait for a memory pool block (in
3621 * milliseconds), or one of the special values K_NO_WAIT
3622 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003623 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003624 * @retval 0 Data retrieved.
3625 * @retval -ENOMEM Returned without waiting.
3626 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003627 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003628 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003629extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003630 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003631 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003632
Anas Nashif166f5192018-02-25 08:02:36 -06003633/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003634
3635/**
Anas Nashifce78d162018-05-24 12:43:11 -05003636 * @defgroup pipe_apis Pipe APIs
3637 * @ingroup kernel_apis
3638 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003639 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003640
Anas Nashifce78d162018-05-24 12:43:11 -05003641/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003642struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003643 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3644 size_t size; /**< Buffer size */
3645 size_t bytes_used; /**< # bytes used in buffer */
3646 size_t read_index; /**< Where in buffer to read from */
3647 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003648
3649 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003650 _wait_q_t readers; /**< Reader wait queue */
3651 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003652 } wait_q;
3653
Anas Nashif2f203c22016-12-18 06:57:45 -05003654 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Anas Nashifce78d162018-05-24 12:43:11 -05003655 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003656};
3657
Anas Nashifce78d162018-05-24 12:43:11 -05003658/**
3659 * @cond INTERNAL_HIDDEN
3660 */
3661#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3662
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003663#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003664 { \
3665 .buffer = pipe_buffer, \
3666 .size = pipe_buffer_size, \
3667 .bytes_used = 0, \
3668 .read_index = 0, \
3669 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003670 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3671 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003672 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003673 }
3674
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003675#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3676
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003677/**
Allan Stephensc98da842016-11-11 15:45:03 -05003678 * INTERNAL_HIDDEN @endcond
3679 */
3680
3681/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003682 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003683 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003684 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003685 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003686 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003688 * @param name Name of the pipe.
3689 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3690 * or zero if no ring buffer is used.
3691 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003692 *
3693 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003694 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003695#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3696 static unsigned char __kernel_noinit __aligned(pipe_align) \
3697 _k_pipe_buf_##name[pipe_buffer_size]; \
3698 struct k_pipe name \
3699 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003700 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003701
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003702/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003703 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003705 * This routine initializes a pipe object, prior to its first use.
3706 *
3707 * @param pipe Address of the pipe.
3708 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3709 * is used.
3710 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3711 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003712 *
3713 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003714 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003715 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003716void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3717
3718/**
3719 * @brief Release a pipe's allocated buffer
3720 *
3721 * If a pipe object was given a dynamically allocated buffer via
3722 * k_pipe_alloc_init(), this will free it. This function does nothing
3723 * if the buffer wasn't dynamically allocated.
3724 *
3725 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003726 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003727 */
3728void k_pipe_cleanup(struct k_pipe *pipe);
3729
3730/**
3731 * @brief Initialize a pipe and allocate a buffer for it
3732 *
3733 * Storage for the buffer region will be allocated from the calling thread's
3734 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3735 * or userspace is enabled and the pipe object loses all references to it.
3736 *
3737 * This function should only be called on uninitialized pipe objects.
3738 *
3739 * @param pipe Address of the pipe.
3740 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3741 * buffer is used.
3742 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003743 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003744 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003745 */
3746__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003747
3748/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003749 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003750 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003751 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003752 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003753 * @param pipe Address of the pipe.
3754 * @param data Address of data to write.
3755 * @param bytes_to_write Size of data (in bytes).
3756 * @param bytes_written Address of area to hold the number of bytes written.
3757 * @param min_xfer Minimum number of bytes to write.
3758 * @param timeout Waiting period to wait for the data to be written (in
3759 * milliseconds), or one of the special values K_NO_WAIT
3760 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003761 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003762 * @retval 0 At least @a min_xfer bytes of data were written.
3763 * @retval -EIO Returned without waiting; zero data bytes were written.
3764 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003765 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003766 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003767 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003768__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3769 size_t bytes_to_write, size_t *bytes_written,
3770 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003771
3772/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003773 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003775 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003776 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003777 * @param pipe Address of the pipe.
3778 * @param data Address to place the data read from pipe.
3779 * @param bytes_to_read Maximum number of data bytes to read.
3780 * @param bytes_read Address of area to hold the number of bytes read.
3781 * @param min_xfer Minimum number of data bytes to read.
3782 * @param timeout Waiting period to wait for the data to be read (in
3783 * milliseconds), or one of the special values K_NO_WAIT
3784 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003785 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003786 * @retval 0 At least @a min_xfer bytes of data were read.
3787 * @retval -EIO Returned without waiting; zero data bytes were read.
3788 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003789 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003790 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003791 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003792__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3793 size_t bytes_to_read, size_t *bytes_read,
3794 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003795
3796/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003797 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003799 * This routine writes the data contained in a memory block to @a pipe.
3800 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003801 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003802 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003803 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003804 * @param block Memory block containing data to send
3805 * @param size Number of data bytes in memory block to send
3806 * @param sem Semaphore to signal upon completion (else NULL)
3807 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003808 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003809 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003810 */
3811extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3812 size_t size, struct k_sem *sem);
3813
Anas Nashif166f5192018-02-25 08:02:36 -06003814/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003815
Allan Stephensc98da842016-11-11 15:45:03 -05003816/**
3817 * @cond INTERNAL_HIDDEN
3818 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003819
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003820struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003821 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003822 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003823 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003824 char *buffer;
3825 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003826 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003827
Anas Nashif2f203c22016-12-18 06:57:45 -05003828 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003829};
3830
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003831#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003832 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003833 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003834 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003835 .num_blocks = slab_num_blocks, \
3836 .block_size = slab_block_size, \
3837 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003838 .free_list = NULL, \
3839 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003840 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003841 }
3842
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003843#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3844
3845
Peter Mitsis578f9112016-10-07 13:50:31 -04003846/**
Allan Stephensc98da842016-11-11 15:45:03 -05003847 * INTERNAL_HIDDEN @endcond
3848 */
3849
3850/**
3851 * @defgroup mem_slab_apis Memory Slab APIs
3852 * @ingroup kernel_apis
3853 * @{
3854 */
3855
3856/**
Allan Stephensda827222016-11-09 14:23:58 -06003857 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003858 *
Allan Stephensda827222016-11-09 14:23:58 -06003859 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003860 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003861 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3862 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003863 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003864 *
Allan Stephensda827222016-11-09 14:23:58 -06003865 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003866 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003867 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003868 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003870 * @param name Name of the memory slab.
3871 * @param slab_block_size Size of each memory block (in bytes).
3872 * @param slab_num_blocks Number memory blocks.
3873 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003874 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003875 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003876#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3877 char __noinit __aligned(slab_align) \
3878 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3879 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003880 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003881 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003882 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003883
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003884/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003885 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003887 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003888 *
Allan Stephensda827222016-11-09 14:23:58 -06003889 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3890 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3891 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3892 * To ensure that each memory block is similarly aligned to this boundary,
3893 * @a slab_block_size must also be a multiple of N.
3894 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003895 * @param slab Address of the memory slab.
3896 * @param buffer Pointer to buffer used for the memory blocks.
3897 * @param block_size Size of each memory block (in bytes).
3898 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003899 *
3900 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003901 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003902 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003903extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003904 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003905
3906/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003907 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003909 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003910 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003911 * @param slab Address of the memory slab.
3912 * @param mem Pointer to block address area.
3913 * @param timeout Maximum time to wait for operation to complete
3914 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3915 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003916 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003917 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003918 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003919 * @retval -ENOMEM Returned without waiting.
3920 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003921 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003922 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003923extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003924 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003925
3926/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003927 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003928 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003929 * This routine releases a previously allocated memory block back to its
3930 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003931 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003932 * @param slab Address of the memory slab.
3933 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003934 *
3935 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003936 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003937 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003938extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003939
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003940/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003941 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003943 * This routine gets the number of memory blocks that are currently
3944 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003946 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003947 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003948 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003949 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003950 */
Kumar Galacc334c72017-04-21 10:55:34 -05003951static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003952{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003953 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003954}
3955
Peter Mitsisc001aa82016-10-13 13:53:37 -04003956/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003957 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003958 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003959 * This routine gets the number of memory blocks that are currently
3960 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003962 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003963 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003964 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003965 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04003966 */
Kumar Galacc334c72017-04-21 10:55:34 -05003967static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003968{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003969 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003970}
3971
Anas Nashif166f5192018-02-25 08:02:36 -06003972/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003973
3974/**
3975 * @cond INTERNAL_HIDDEN
3976 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003977
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003978struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003979 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003980 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003981};
3982
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003983/**
Allan Stephensc98da842016-11-11 15:45:03 -05003984 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003985 */
3986
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003987/**
Allan Stephensc98da842016-11-11 15:45:03 -05003988 * @addtogroup mem_pool_apis
3989 * @{
3990 */
3991
3992/**
3993 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003994 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003995 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3996 * long. The memory pool allows blocks to be repeatedly partitioned into
3997 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003998 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003999 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004000 * If the pool is to be accessed outside the module where it is defined, it
4001 * can be declared via
4002 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004003 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004005 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004006 * @param minsz Size of the smallest blocks in the pool (in bytes).
4007 * @param maxsz Size of the largest blocks in the pool (in bytes).
4008 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004009 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004010 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004011 */
Andy Ross73cb9582017-05-09 10:42:39 -07004012#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4013 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4014 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08004015 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07004016 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004017 .base = { \
4018 .buf = _mpool_buf_##name, \
4019 .max_sz = maxsz, \
4020 .n_max = nmax, \
4021 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
4022 .levels = _mpool_lvls_##name, \
4023 .flags = SYS_MEM_POOL_KERNEL \
4024 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004025 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004026
Peter Mitsis937042c2016-10-13 13:18:26 -04004027/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004028 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004029 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004030 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004032 * @param pool Address of the memory pool.
4033 * @param block Pointer to block descriptor for the allocated memory.
4034 * @param size Amount of memory to allocate (in bytes).
4035 * @param timeout Maximum time to wait for operation to complete
4036 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4037 * or K_FOREVER to wait as long as necessary.
4038 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004039 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004040 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004041 * @retval -ENOMEM Returned without waiting.
4042 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004043 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004044 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004045extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004046 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004047
4048/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004049 * @brief Allocate memory from a memory pool with malloc() semantics
4050 *
4051 * Such memory must be released using k_free().
4052 *
4053 * @param pool Address of the memory pool.
4054 * @param size Amount of memory to allocate (in bytes).
4055 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004056 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004057 */
4058extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4059
4060/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004061 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004062 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004063 * This routine releases a previously allocated memory block back to its
4064 * memory pool.
4065 *
4066 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004067 *
4068 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004069 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004070 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004071extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004072
4073/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004074 * @brief Free memory allocated from a memory pool.
4075 *
4076 * This routine releases a previously allocated memory block back to its
4077 * memory pool
4078 *
4079 * @param id Memory block identifier.
4080 *
4081 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004082 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004083 */
4084extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4085
4086/**
Anas Nashif166f5192018-02-25 08:02:36 -06004087 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004088 */
4089
4090/**
4091 * @defgroup heap_apis Heap Memory Pool APIs
4092 * @ingroup kernel_apis
4093 * @{
4094 */
4095
4096/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004097 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004098 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004099 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004100 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004101 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004102 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004104 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004105 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004106 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004107extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004108
4109/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004110 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004111 *
4112 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004113 * returned must have been allocated from the heap memory pool or
4114 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004115 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004116 * If @a ptr is NULL, no operation is performed.
4117 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004118 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004119 *
4120 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004121 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004122 */
4123extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004124
Allan Stephensc98da842016-11-11 15:45:03 -05004125/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004126 * @brief Allocate memory from heap, array style
4127 *
4128 * This routine provides traditional calloc() semantics. Memory is
4129 * allocated from the heap memory pool and zeroed.
4130 *
4131 * @param nmemb Number of elements in the requested array
4132 * @param size Size of each array element (in bytes).
4133 *
4134 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004135 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004136 */
4137extern void *k_calloc(size_t nmemb, size_t size);
4138
Anas Nashif166f5192018-02-25 08:02:36 -06004139/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004140
Benjamin Walshacc68c12017-01-29 18:57:45 -05004141/* polling API - PRIVATE */
4142
Benjamin Walshb0179862017-02-02 16:39:57 -05004143#ifdef CONFIG_POLL
4144#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
4145#else
4146#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
4147#endif
4148
Benjamin Walshacc68c12017-01-29 18:57:45 -05004149/* private - implementation data created as needed, per-type */
4150struct _poller {
4151 struct k_thread *thread;
4152};
4153
4154/* private - types bit positions */
4155enum _poll_types_bits {
4156 /* can be used to ignore an event */
4157 _POLL_TYPE_IGNORE,
4158
4159 /* to be signaled by k_poll_signal() */
4160 _POLL_TYPE_SIGNAL,
4161
4162 /* semaphore availability */
4163 _POLL_TYPE_SEM_AVAILABLE,
4164
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004165 /* queue/fifo/lifo data availability */
4166 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004167
4168 _POLL_NUM_TYPES
4169};
4170
4171#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4172
4173/* private - states bit positions */
4174enum _poll_states_bits {
4175 /* default state when creating event */
4176 _POLL_STATE_NOT_READY,
4177
Benjamin Walshacc68c12017-01-29 18:57:45 -05004178 /* signaled by k_poll_signal() */
4179 _POLL_STATE_SIGNALED,
4180
4181 /* semaphore is available */
4182 _POLL_STATE_SEM_AVAILABLE,
4183
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004184 /* data is available to read on queue/fifo/lifo */
4185 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004186
4187 _POLL_NUM_STATES
4188};
4189
4190#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4191
4192#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004193 (32 - (0 \
4194 + 8 /* tag */ \
4195 + _POLL_NUM_TYPES \
4196 + _POLL_NUM_STATES \
4197 + 1 /* modes */ \
4198 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004199
Benjamin Walshacc68c12017-01-29 18:57:45 -05004200/* end of polling API - PRIVATE */
4201
4202
4203/**
4204 * @defgroup poll_apis Async polling APIs
4205 * @ingroup kernel_apis
4206 * @{
4207 */
4208
4209/* Public polling API */
4210
4211/* public - values for k_poll_event.type bitfield */
4212#define K_POLL_TYPE_IGNORE 0
4213#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4214#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004215#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4216#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004217
4218/* public - polling modes */
4219enum k_poll_modes {
4220 /* polling thread does not take ownership of objects when available */
4221 K_POLL_MODE_NOTIFY_ONLY = 0,
4222
4223 K_POLL_NUM_MODES
4224};
4225
4226/* public - values for k_poll_event.state bitfield */
4227#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004228#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4229#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004230#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4231#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004232
4233/* public - poll signal object */
4234struct k_poll_signal {
4235 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004236 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004237
4238 /*
4239 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4240 * user resets it to 0.
4241 */
4242 unsigned int signaled;
4243
4244 /* custom result value passed to k_poll_signal() if needed */
4245 int result;
4246};
4247
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004248#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004249 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004250 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004251 .signaled = 0, \
4252 .result = 0, \
4253 }
4254
4255struct k_poll_event {
4256 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004257 sys_dnode_t _node;
4258
4259 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004260 struct _poller *poller;
4261
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004262 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004263 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004264
Benjamin Walshacc68c12017-01-29 18:57:45 -05004265 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004266 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004267
4268 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004269 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004270
4271 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004272 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004273
4274 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004275 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004276
4277 /* per-type data */
4278 union {
4279 void *obj;
4280 struct k_poll_signal *signal;
4281 struct k_sem *sem;
4282 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004283 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004284 };
4285};
4286
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004287#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004288 { \
4289 .poller = NULL, \
4290 .type = event_type, \
4291 .state = K_POLL_STATE_NOT_READY, \
4292 .mode = event_mode, \
4293 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004294 { .obj = event_obj }, \
4295 }
4296
4297#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4298 event_tag) \
4299 { \
4300 .type = event_type, \
4301 .tag = event_tag, \
4302 .state = K_POLL_STATE_NOT_READY, \
4303 .mode = event_mode, \
4304 .unused = 0, \
4305 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004306 }
4307
4308/**
4309 * @brief Initialize one struct k_poll_event instance
4310 *
4311 * After this routine is called on a poll event, the event it ready to be
4312 * placed in an event array to be passed to k_poll().
4313 *
4314 * @param event The event to initialize.
4315 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4316 * values. Only values that apply to the same object being polled
4317 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4318 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004319 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004320 * @param obj Kernel object or poll signal.
4321 *
4322 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004323 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004324 */
4325
Kumar Galacc334c72017-04-21 10:55:34 -05004326extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004327 int mode, void *obj);
4328
4329/**
4330 * @brief Wait for one or many of multiple poll events to occur
4331 *
4332 * This routine allows a thread to wait concurrently for one or many of
4333 * multiple poll events to have occurred. Such events can be a kernel object
4334 * being available, like a semaphore, or a poll signal event.
4335 *
4336 * When an event notifies that a kernel object is available, the kernel object
4337 * is not "given" to the thread calling k_poll(): it merely signals the fact
4338 * that the object was available when the k_poll() call was in effect. Also,
4339 * all threads trying to acquire an object the regular way, i.e. by pending on
4340 * the object, have precedence over the thread polling on the object. This
4341 * means that the polling thread will never get the poll event on an object
4342 * until the object becomes available and its pend queue is empty. For this
4343 * reason, the k_poll() call is more effective when the objects being polled
4344 * only have one thread, the polling thread, trying to acquire them.
4345 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004346 * When k_poll() returns 0, the caller should loop on all the events that were
4347 * passed to k_poll() and check the state field for the values that were
4348 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004349 *
4350 * Before being reused for another call to k_poll(), the user has to reset the
4351 * state field to K_POLL_STATE_NOT_READY.
4352 *
Andrew Boie3772f772018-05-07 16:52:57 -07004353 * When called from user mode, a temporary memory allocation is required from
4354 * the caller's resource pool.
4355 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004356 * @param events An array of pointers to events to be polled for.
4357 * @param num_events The number of events in the array.
4358 * @param timeout Waiting period for an event to be ready (in milliseconds),
4359 * or one of the special values K_NO_WAIT and K_FOREVER.
4360 *
4361 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004362 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02004363 * @retval -EINTR Poller thread has been interrupted.
Andrew Boie3772f772018-05-07 16:52:57 -07004364 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4365 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004366 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004367 */
4368
Andrew Boie3772f772018-05-07 16:52:57 -07004369__syscall int k_poll(struct k_poll_event *events, int num_events,
4370 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004371
4372/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004373 * @brief Initialize a poll signal object.
4374 *
4375 * Ready a poll signal object to be signaled via k_poll_signal().
4376 *
4377 * @param signal A poll signal.
4378 *
4379 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004380 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004381 */
4382
Andrew Boie3772f772018-05-07 16:52:57 -07004383__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4384
4385/*
4386 * @brief Reset a poll signal object's state to unsignaled.
4387 *
4388 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004389 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004390 */
4391__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4392
4393static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4394{
4395 signal->signaled = 0;
4396}
4397
4398/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004399 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004400 *
4401 * @param signal A poll signal object
4402 * @param signaled An integer buffer which will be written nonzero if the
4403 * object was signaled
4404 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004405 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004406 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004407 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004408 */
4409__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4410 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004411
4412/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004413 * @brief Signal a poll signal object.
4414 *
4415 * This routine makes ready a poll signal, which is basically a poll event of
4416 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4417 * made ready to run. A @a result value can be specified.
4418 *
4419 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004420 * k_poll_signal(), stays set until the user sets it back to 0 with
4421 * k_poll_signal_reset(). It thus has to be reset by the user before being
4422 * passed again to k_poll() or k_poll() will consider it being signaled, and
4423 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004424 *
4425 * @param signal A poll signal.
4426 * @param result The value to store in the result field of the signal.
4427 *
4428 * @retval 0 The signal was delivered successfully.
4429 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004430 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004431 */
4432
Andrew Boie3772f772018-05-07 16:52:57 -07004433__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004434
Anas Nashif954d5502018-02-25 08:37:28 -06004435/**
4436 * @internal
4437 */
Andy Ross8606fab2018-03-26 10:54:40 -07004438extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004439
Anas Nashif166f5192018-02-25 08:02:36 -06004440/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004441
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004442/**
4443 * @brief Make the CPU idle.
4444 *
4445 * This function makes the CPU idle until an event wakes it up.
4446 *
4447 * In a regular system, the idle thread should be the only thread responsible
4448 * for making the CPU idle and triggering any type of power management.
4449 * However, in some more constrained systems, such as a single-threaded system,
4450 * the only thread would be responsible for this if needed.
4451 *
4452 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004453 * @req K-MISC-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004454 */
4455extern void k_cpu_idle(void);
4456
4457/**
4458 * @brief Make the CPU idle in an atomic fashion.
4459 *
4460 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4461 * must be done atomically before making the CPU idle.
4462 *
4463 * @param key Interrupt locking key obtained from irq_lock().
4464 *
4465 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004466 * @req K-MISC-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004467 */
4468extern void k_cpu_atomic_idle(unsigned int key);
4469
Anas Nashif954d5502018-02-25 08:37:28 -06004470
4471/**
4472 * @internal
4473 */
Kumar Galacc334c72017-04-21 10:55:34 -05004474extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004475
Andrew Boiecdb94d62017-04-18 15:22:05 -07004476#ifdef _ARCH_EXCEPT
4477/* This archtecture has direct support for triggering a CPU exception */
4478#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4479#else
4480
Andrew Boiecdb94d62017-04-18 15:22:05 -07004481/* NOTE: This is the implementation for arches that do not implement
4482 * _ARCH_EXCEPT() to generate a real CPU exception.
4483 *
4484 * We won't have a real exception frame to determine the PC value when
4485 * the oops occurred, so print file and line number before we jump into
4486 * the fatal error handler.
4487 */
4488#define _k_except_reason(reason) do { \
4489 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4490 _NanoFatalErrorHandler(reason, &_default_esf); \
4491 CODE_UNREACHABLE; \
4492 } while (0)
4493
4494#endif /* _ARCH__EXCEPT */
4495
4496/**
4497 * @brief Fatally terminate a thread
4498 *
4499 * This should be called when a thread has encountered an unrecoverable
4500 * runtime condition and needs to terminate. What this ultimately
4501 * means is determined by the _fatal_error_handler() implementation, which
4502 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4503 *
4504 * If this is called from ISR context, the default system fatal error handler
4505 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004506 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004507 */
4508#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4509
4510/**
4511 * @brief Fatally terminate the system
4512 *
4513 * This should be called when the Zephyr kernel has encountered an
4514 * unrecoverable runtime condition and needs to terminate. What this ultimately
4515 * means is determined by the _fatal_error_handler() implementation, which
4516 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004517 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004518 */
4519#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4520
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004521/*
4522 * private APIs that are utilized by one or more public APIs
4523 */
4524
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004525#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004526/**
4527 * @internal
4528 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004529extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004530#else
Anas Nashif954d5502018-02-25 08:37:28 -06004531/**
4532 * @internal
4533 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004534#define _init_static_threads() do { } while ((0))
4535#endif
4536
Anas Nashif954d5502018-02-25 08:37:28 -06004537/**
4538 * @internal
4539 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004540extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004541/**
4542 * @internal
4543 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004544extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004545
Andrew Boiedc5d9352017-06-02 12:56:47 -07004546/* arch/cpu.h may declare an architecture or platform-specific macro
4547 * for properly declaring stacks, compatible with MMU/MPU constraints if
4548 * enabled
4549 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004550
4551/**
4552 * @brief Obtain an extern reference to a stack
4553 *
4554 * This macro properly brings the symbol of a thread stack declared
4555 * elsewhere into scope.
4556 *
4557 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004558 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004559 */
4560#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4561
Andrew Boiedc5d9352017-06-02 12:56:47 -07004562#ifdef _ARCH_THREAD_STACK_DEFINE
4563#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4564#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4565 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4566#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4567#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004568static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004569{
4570 return _ARCH_THREAD_STACK_BUFFER(sym);
4571}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004572#else
4573/**
4574 * @brief Declare a toplevel thread stack memory region
4575 *
4576 * This declares a region of memory suitable for use as a thread's stack.
4577 *
4578 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4579 * 'noinit' section so that it isn't zeroed at boot
4580 *
Andrew Boie507852a2017-07-25 18:47:07 -07004581 * The declared symbol will always be a k_thread_stack_t which can be passed to
4582 * k_thread_create, but should otherwise not be manipulated. If the buffer
4583 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004584 *
4585 * It is legal to precede this definition with the 'static' keyword.
4586 *
4587 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4588 * parameter of k_thread_create(), it may not be the same as the
4589 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4590 *
4591 * @param sym Thread stack symbol name
4592 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004593 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004594 */
4595#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004596 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004597
4598/**
4599 * @brief Declare a toplevel array of thread stack memory regions
4600 *
4601 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4602 * definition for additional details and constraints.
4603 *
4604 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4605 * 'noinit' section so that it isn't zeroed at boot
4606 *
4607 * @param sym Thread stack symbol name
4608 * @param nmemb Number of stacks to declare
4609 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004610 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004611 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004612#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004613 struct _k_thread_stack_element __noinit \
4614 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004615
4616/**
4617 * @brief Declare an embedded stack memory region
4618 *
4619 * Used for stacks embedded within other data structures. Use is highly
4620 * discouraged but in some cases necessary. For memory protection scenarios,
4621 * it is very important that any RAM preceding this member not be writable
4622 * by threads else a stack overflow will lead to silent corruption. In other
4623 * words, the containing data structure should live in RAM owned by the kernel.
4624 *
4625 * @param sym Thread stack symbol name
4626 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004627 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004628 */
4629#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004630 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004631
4632/**
4633 * @brief Return the size in bytes of a stack memory region
4634 *
4635 * Convenience macro for passing the desired stack size to k_thread_create()
4636 * since the underlying implementation may actually create something larger
4637 * (for instance a guard area).
4638 *
4639 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004640 * passed to K_THREAD_STACK_DEFINE.
4641 *
4642 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4643 * it is not guaranteed to return the original value since each array
4644 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004645 *
4646 * @param sym Stack memory symbol
4647 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004648 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004649 */
4650#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4651
4652/**
4653 * @brief Get a pointer to the physical stack buffer
4654 *
4655 * Convenience macro to get at the real underlying stack buffer used by
4656 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4657 * This is really only intended for diagnostic tools which want to examine
4658 * stack memory contents.
4659 *
4660 * @param sym Declared stack symbol name
4661 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004662 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004663 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004664static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004665{
4666 return (char *)sym;
4667}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004668
4669#endif /* _ARCH_DECLARE_STACK */
4670
Chunlin Hane9c97022017-07-07 20:29:30 +08004671/**
4672 * @defgroup mem_domain_apis Memory domain APIs
4673 * @ingroup kernel_apis
4674 * @{
4675 */
4676
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004677/**
4678 * @def MEM_PARTITION_ENTRY
4679 * @brief Used to declare a memory partition entry
4680 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004681 */
4682#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4683 {\
4684 .start = _start, \
4685 .size = _size, \
4686 .attr = _attr, \
4687 }
4688
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004689/**
4690 * @def K_MEM_PARTITION_DEFINE
4691 * @brief Used to declare a memory partition
4692 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004693 */
4694#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4695#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4696 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304697 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004698 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4699#else
4700#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304701 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004702 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4703#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4704
Chunlin Hane9c97022017-07-07 20:29:30 +08004705/* memory partition */
4706struct k_mem_partition {
4707 /* start address of memory partition */
4708 u32_t start;
4709 /* size of memory partition */
4710 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304711#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004712 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304713 k_mem_partition_attr_t attr;
4714#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004715};
4716
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304717/* memory domian
4718 * Note: Always declare this structure with __kernel prefix
4719 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004720struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304721#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004722 /* partitions in the domain */
4723 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304724#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004725 /* domain q */
4726 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004727 /* number of partitions in the domain */
4728 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004729};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304730
Chunlin Hane9c97022017-07-07 20:29:30 +08004731
4732/**
4733 * @brief Initialize a memory domain.
4734 *
4735 * Initialize a memory domain with given name and memory partitions.
4736 *
4737 * @param domain The memory domain to be initialized.
4738 * @param num_parts The number of array items of "parts" parameter.
4739 * @param parts An array of pointers to the memory partitions. Can be NULL
4740 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004741 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004742 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004743extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004744 struct k_mem_partition *parts[]);
4745/**
4746 * @brief Destroy a memory domain.
4747 *
4748 * Destroy a memory domain.
4749 *
4750 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004751 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004752 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004753extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4754
4755/**
4756 * @brief Add a memory partition into a memory domain.
4757 *
4758 * Add a memory partition into a memory domain.
4759 *
4760 * @param domain The memory domain to be added a memory partition.
4761 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004762 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004763 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004764extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4765 struct k_mem_partition *part);
4766
4767/**
4768 * @brief Remove a memory partition from a memory domain.
4769 *
4770 * Remove a memory partition from a memory domain.
4771 *
4772 * @param domain The memory domain to be removed a memory partition.
4773 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004774 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004775 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004776extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4777 struct k_mem_partition *part);
4778
4779/**
4780 * @brief Add a thread into a memory domain.
4781 *
4782 * Add a thread into a memory domain.
4783 *
4784 * @param domain The memory domain that the thread is going to be added into.
4785 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004786 *
4787 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004788 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004789extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4790 k_tid_t thread);
4791
4792/**
4793 * @brief Remove a thread from its memory domain.
4794 *
4795 * Remove a thread from its memory domain.
4796 *
4797 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004798 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004799 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004800extern void k_mem_domain_remove_thread(k_tid_t thread);
4801
Anas Nashif166f5192018-02-25 08:02:36 -06004802/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004803
Andrew Boie756f9072017-10-10 16:01:49 -07004804/**
4805 * @brief Emit a character buffer to the console device
4806 *
4807 * @param c String of characters to print
4808 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004809 *
4810 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004811 */
4812__syscall void k_str_out(char *c, size_t n);
4813
Andy Rosse7172672018-01-24 15:48:32 -08004814/**
4815 * @brief Start a numbered CPU on a MP-capable system
4816
4817 * This starts and initializes a specific CPU. The main thread on
4818 * startup is running on CPU zero, other processors are numbered
4819 * sequentially. On return from this function, the CPU is known to
4820 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004821 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004822 * with the provided key will work to enable them.
4823 *
4824 * Normally, in SMP mode this function will be called by the kernel
4825 * initialization and should not be used as a user API. But it is
4826 * defined here for special-purpose apps which want Zephyr running on
4827 * one core and to use others for design-specific processing.
4828 *
4829 * @param cpu_num Integer number of the CPU
4830 * @param stack Stack memory for the CPU
4831 * @param sz Stack buffer size, in bytes
4832 * @param fn Function to begin running on the CPU. First argument is
4833 * an irq_unlock() key.
4834 * @param arg Untyped argument to be passed to "fn"
4835 */
4836extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4837 void (*fn)(int, void *), void *arg);
4838
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004839#ifdef __cplusplus
4840}
4841#endif
4842
Andrew Boiee004dec2016-11-07 09:01:19 -08004843#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4844/*
4845 * Define new and delete operators.
4846 * At this moment, the operators do nothing since objects are supposed
4847 * to be statically allocated.
4848 */
4849inline void operator delete(void *ptr)
4850{
4851 (void)ptr;
4852}
4853
4854inline void operator delete[](void *ptr)
4855{
4856 (void)ptr;
4857}
4858
4859inline void *operator new(size_t size)
4860{
4861 (void)size;
4862 return NULL;
4863}
4864
4865inline void *operator new[](size_t size)
4866{
4867 (void)size;
4868 return NULL;
4869}
4870
4871/* Placement versions of operator new and delete */
4872inline void operator delete(void *ptr1, void *ptr2)
4873{
4874 (void)ptr1;
4875 (void)ptr2;
4876}
4877
4878inline void operator delete[](void *ptr1, void *ptr2)
4879{
4880 (void)ptr1;
4881 (void)ptr2;
4882}
4883
4884inline void *operator new(size_t size, void *ptr)
4885{
4886 (void)size;
4887 return ptr;
4888}
4889
4890inline void *operator new[](size_t size, void *ptr)
4891{
4892 (void)size;
4893 return ptr;
4894}
4895
4896#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4897
Andrew Boiefa94ee72017-09-28 16:54:35 -07004898#include <syscalls/kernel.h>
4899
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004900#endif /* !_ASMLANGUAGE */
4901
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004902#endif /* _kernel__h_ */