blob: 1419a1f2f4cfd8e0a22857fb0bdce1b86c2f12b5 [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>
25#include <misc/dlist.h>
26#include <misc/slist.h>
Andrew Boie2b9b4b22018-04-27 13:21:22 -070027#include <misc/sflist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050028#include <misc/util.h>
Andrew Boieaa6de292018-03-06 17:12:37 -080029#include <misc/mempool_base.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050030#include <kernel_version.h>
Leandro Pereiraadce1d12017-10-13 15:45:02 -070031#include <random/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070032#include <kernel_arch_thread.h>
Andrew Boie13ca6fe2017-09-23 12:05:49 -070033#include <syscall.h>
Andrew Boie43263fc2017-11-02 11:07:31 -070034#include <misc/printk.h>
35#include <arch/cpu.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040036
37#ifdef __cplusplus
38extern "C" {
39#endif
40
Anas Nashifbbb157d2017-01-15 08:46:31 -050041/**
42 * @brief Kernel APIs
43 * @defgroup kernel_apis Kernel APIs
44 * @{
45 * @}
46 */
47
Anas Nashif61f4b242016-11-18 10:53:59 -050048#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040049#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
50#else
51#define K_DEBUG(fmt, ...)
52#endif
53
Benjamin Walsh2f280412017-01-14 19:23:46 -050054#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
55#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
56#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
57#elif defined(CONFIG_COOP_ENABLED)
58#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
59#define _NUM_PREEMPT_PRIO (0)
60#elif defined(CONFIG_PREEMPT_ENABLED)
61#define _NUM_COOP_PRIO (0)
62#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
63#else
64#error "invalid configuration"
65#endif
66
67#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040068#define K_PRIO_PREEMPT(x) (x)
69
Benjamin Walsh456c6da2016-09-02 18:55:39 -040070#define K_ANY NULL
71#define K_END NULL
72
Benjamin Walshedb35702017-01-14 18:47:22 -050073#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040074#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050075#elif defined(CONFIG_COOP_ENABLED)
76#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
77#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040078#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050079#else
80#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040081#endif
82
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050083#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040084#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
85#else
86#define K_LOWEST_THREAD_PRIO -1
87#endif
88
Benjamin Walshfab8d922016-11-08 15:36:36 -050089#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
90
Benjamin Walsh456c6da2016-09-02 18:55:39 -040091#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
92#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
93
Andy Rossccf3bf72018-05-10 11:10:34 -070094typedef struct {
95 sys_dlist_t waitq;
96} _wait_q_t;
97
98#define _WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
Benjamin Walsh456c6da2016-09-02 18:55:39 -040099
Anas Nashif2f203c22016-12-18 06:57:45 -0500100#ifdef CONFIG_OBJECT_TRACING
101#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
102#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400103#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500104#define _OBJECT_TRACING_INIT
105#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400106#endif
107
Benjamin Walshacc68c12017-01-29 18:57:45 -0500108#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300109#define _POLL_EVENT_OBJ_INIT(obj) \
110 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
111#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500112#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300113#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500114#define _POLL_EVENT
115#endif
116
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500117struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400118struct k_mutex;
119struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400120struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400121struct k_msgq;
122struct k_mbox;
123struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200124struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400125struct k_fifo;
126struct k_lifo;
127struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400128struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400129struct k_mem_pool;
130struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500131struct k_poll_event;
132struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800133struct k_mem_domain;
134struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400135
Andrew Boie5bd891d2017-09-27 12:59:28 -0700136/* This enumeration needs to be kept in sync with the lists of kernel objects
137 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
138 * function in kernel/userspace.c
139 */
Andrew Boie945af952017-08-22 13:15:23 -0700140enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700141 K_OBJ_ANY,
142
Leandro Pereirac2003672018-04-04 13:50:32 -0700143 /** @cond
144 * Doxygen should ignore this build-time generated include file
145 * when genrating API documentation. Enumeration values are
146 * generated during build by gen_kobject_list.py. It includes
147 * basic kernel objects (e.g. pipes and mutexes) and driver types.
148 */
149#include <kobj-types-enum.h>
150 /** @endcond
151 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700152
Andrew Boie945af952017-08-22 13:15:23 -0700153 K_OBJ_LAST
154};
155
156#ifdef CONFIG_USERSPACE
157/* Table generated by gperf, these objects are retrieved via
158 * _k_object_find() */
159struct _k_object {
160 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700161 u8_t perms[CONFIG_MAX_THREAD_BYTES];
162 u8_t type;
163 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700164 u32_t data;
Andrew Boie945af952017-08-22 13:15:23 -0700165} __packed;
166
Andrew Boie877f82e2017-10-17 11:20:22 -0700167struct _k_object_assignment {
168 struct k_thread *thread;
169 void * const *objects;
170};
171
172/**
173 * @brief Grant a static thread access to a list of kernel objects
174 *
175 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
176 * a set of kernel objects. These objects do not need to be in an initialized
177 * state. The permissions will be granted when the threads are initialized
178 * in the early boot sequence.
179 *
180 * All arguments beyond the first must be pointers to kernel objects.
181 *
182 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
183 */
184#define K_THREAD_ACCESS_GRANT(name_, ...) \
185 static void * const _CONCAT(_object_list_, name_)[] = \
186 { __VA_ARGS__, NULL }; \
187 static __used __in_section_unique(object_access) \
188 const struct _k_object_assignment \
189 _CONCAT(_object_access_, name_) = \
190 { (&_k_thread_obj_ ## name_), \
191 (_CONCAT(_object_list_, name_)) }
192
Andrew Boie945af952017-08-22 13:15:23 -0700193#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700194#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700195#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie945af952017-08-22 13:15:23 -0700196
197/**
198 * Lookup a kernel object and init its metadata if it exists
199 *
200 * Calling this on an object will make it usable from userspace.
201 * Intended to be called as the last statement in kernel object init
202 * functions.
203 *
204 * @param object Address of the kernel object
205 */
206void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700207#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700208
209#define K_THREAD_ACCESS_GRANT(thread, ...)
210
Anas Nashif954d5502018-02-25 08:37:28 -0600211/**
212 * @internal
213 */
Andrew Boie743e4682017-10-04 12:25:50 -0700214static inline void _k_object_init(void *obj)
215{
216 ARG_UNUSED(obj);
217}
218
Anas Nashif954d5502018-02-25 08:37:28 -0600219/**
220 * @internal
221 */
Andrew Boie743e4682017-10-04 12:25:50 -0700222static inline void _impl_k_object_access_grant(void *object,
223 struct k_thread *thread)
224{
225 ARG_UNUSED(object);
226 ARG_UNUSED(thread);
227}
228
Anas Nashif954d5502018-02-25 08:37:28 -0600229/**
230 * @internal
231 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700232static inline void k_object_access_revoke(void *object,
233 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700234{
235 ARG_UNUSED(object);
236 ARG_UNUSED(thread);
237}
238
Andrew Boiee9cfc542018-04-13 13:15:28 -0700239/**
240 * @internal
241 */
242static inline void _impl_k_object_release(void *object)
243{
244 ARG_UNUSED(object);
245}
246
Andrew Boie41bab6e2017-10-14 14:42:23 -0700247static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700248{
249 ARG_UNUSED(object);
250}
251#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700252
253/**
254 * grant a thread access to a kernel object
255 *
256 * The thread will be granted access to the object if the caller is from
257 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700258 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700259 *
260 * @param object Address of kernel object
261 * @param thread Thread to grant access to the object
262 */
Andrew Boie743e4682017-10-04 12:25:50 -0700263__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700264
Andrew Boiea89bf012017-10-09 14:47:55 -0700265/**
266 * grant a thread access to a kernel object
267 *
268 * The thread will lose access to the object if the caller is from
269 * supervisor mode, or the caller is from user mode AND has permissions
270 * on both the object and the thread whose access is being revoked.
271 *
272 * @param object Address of kernel object
273 * @param thread Thread to remove access to the object
274 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700275void k_object_access_revoke(void *object, struct k_thread *thread);
276
277
278__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700279
280/**
281 * grant all present and future threads access to an object
282 *
283 * If the caller is from supervisor mode, or the caller is from user mode and
284 * have sufficient permissions on the object, then that object will have
285 * permissions granted to it for *all* current and future threads running in
286 * the system, effectively becoming a public kernel object.
287 *
288 * Use of this API should be avoided on systems that are running untrusted code
289 * as it is possible for such code to derive the addresses of kernel objects
290 * and perform unwanted operations on them.
291 *
Andrew Boie04caa672017-10-13 13:57:07 -0700292 * It is not possible to revoke permissions on public objects; once public,
293 * any thread may use it.
294 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700295 * @param object Address of kernel object
296 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700297void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700298
Andrew Boie31bdfc02017-11-08 16:38:03 -0800299/**
300 * Allocate a kernel object of a designated type
301 *
302 * This will instantiate at runtime a kernel object of the specified type,
303 * returning a pointer to it. The object will be returned in an uninitialized
304 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700305 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800306 *
307 * Currently, allocation of thread stacks is not supported.
308 *
309 * @param otype Requested kernel object type
310 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
311 * available
312 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700313__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800314
Andrew Boie97bf0012018-04-24 17:01:37 -0700315#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800316/**
317 * Free a kernel object previously allocated with k_object_alloc()
318 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700319 * This will return memory for a kernel object back to resource pool it was
320 * allocated from. Care must be exercised that the object will not be used
321 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800322 *
323 * @param obj Pointer to the kernel object memory address.
324 */
325void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700326#else
327static inline void *_impl_k_object_alloc(enum k_objects otype)
328{
Kumar Gala85699f72018-05-17 09:26:03 -0500329 ARG_UNUSED(otype);
330
Andrew Boie97bf0012018-04-24 17:01:37 -0700331 return NULL;
332}
333
334static inline void k_obj_free(void *obj)
335{
336 ARG_UNUSED(obj);
337}
Andrew Boie31bdfc02017-11-08 16:38:03 -0800338#endif /* CONFIG_DYNAMIC_OBJECTS */
339
Andrew Boiebca15da2017-10-15 14:17:48 -0700340/* Using typedef deliberately here, this is quite intended to be an opaque
341 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
342 *
343 * The purpose of this data type is to clearly distinguish between the
344 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
345 * buffer which composes the stack data actually used by the underlying
346 * thread; they cannot be used interchangably as some arches precede the
347 * stack buffer region with guard areas that trigger a MPU or MMU fault
348 * if written to.
349 *
350 * APIs that want to work with the buffer inside should continue to use
351 * char *.
352 *
353 * Stacks should always be created with K_THREAD_STACK_DEFINE().
354 */
355struct __packed _k_thread_stack_element {
356 char data;
357};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700358typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700359
Andrew Boie73abd322017-04-04 13:19:13 -0700360/* timeouts */
361
362struct _timeout;
363typedef void (*_timeout_func_t)(struct _timeout *t);
364
365struct _timeout {
366 sys_dnode_t node;
367 struct k_thread *thread;
368 sys_dlist_t *wait_q;
369 s32_t delta_ticks_from_prev;
370 _timeout_func_t func;
371};
372
373extern s32_t _timeout_remaining_get(struct _timeout *timeout);
374
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700375/**
376 * @typedef k_thread_entry_t
377 * @brief Thread entry point function type.
378 *
379 * A thread's entry point function is invoked when the thread starts executing.
380 * Up to 3 argument values can be passed to the function.
381 *
382 * The thread terminates execution permanently if the entry point function
383 * returns. The thread is responsible for releasing any shared resources
384 * it may own (such as mutexes and dynamically allocated memory), prior to
385 * returning.
386 *
387 * @param p1 First argument.
388 * @param p2 Second argument.
389 * @param p3 Third argument.
390 *
391 * @return N/A
392 */
393typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700394
395#ifdef CONFIG_THREAD_MONITOR
396struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700397 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700398 void *parameter1;
399 void *parameter2;
400 void *parameter3;
401};
402#endif
403
404/* can be used for creating 'dummy' threads, e.g. for pending on objects */
405struct _thread_base {
406
407 /* this thread's entry in a ready/wait queue */
408 sys_dnode_t k_q_node;
409
410 /* user facing 'thread options'; values defined in include/kernel.h */
411 u8_t user_options;
412
413 /* thread state */
414 u8_t thread_state;
415
416 /*
417 * scheduler lock count and thread priority
418 *
419 * These two fields control the preemptibility of a thread.
420 *
421 * When the scheduler is locked, sched_locked is decremented, which
422 * means that the scheduler is locked for values from 0xff to 0x01. A
423 * thread is coop if its prio is negative, thus 0x80 to 0xff when
424 * looked at the value as unsigned.
425 *
426 * By putting them end-to-end, this means that a thread is
427 * non-preemptible if the bundled value is greater than or equal to
428 * 0x0080.
429 */
430 union {
431 struct {
432#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
433 u8_t sched_locked;
434 s8_t prio;
435#else /* LITTLE and PDP */
436 s8_t prio;
437 u8_t sched_locked;
438#endif
439 };
440 u16_t preempt;
441 };
442
Andy Ross2724fd12018-01-29 14:55:20 -0800443#ifdef CONFIG_SMP
444 /* True for the per-CPU idle threads */
445 u8_t is_idle;
446
447 /* Non-zero when actively running on a CPU */
448 u8_t active;
449
450 /* CPU index on which thread was last run */
451 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700452
453 /* Recursive count of irq_lock() calls */
454 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800455#endif
456
Andrew Boie73abd322017-04-04 13:19:13 -0700457 /* data returned by APIs */
458 void *swap_data;
459
460#ifdef CONFIG_SYS_CLOCK_EXISTS
461 /* this thread's entry in a timeout queue */
462 struct _timeout timeout;
463#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700464};
465
466typedef struct _thread_base _thread_base_t;
467
468#if defined(CONFIG_THREAD_STACK_INFO)
469/* Contains the stack information of a thread */
470struct _thread_stack_info {
471 /* Stack Start */
472 u32_t start;
473 /* Stack Size */
474 u32_t size;
475};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700476
477typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700478#endif /* CONFIG_THREAD_STACK_INFO */
479
Chunlin Hane9c97022017-07-07 20:29:30 +0800480#if defined(CONFIG_USERSPACE)
481struct _mem_domain_info {
482 /* memory domain queue node */
483 sys_dnode_t mem_domain_q_node;
484 /* memory domain of the thread */
485 struct k_mem_domain *mem_domain;
486};
487
488#endif /* CONFIG_USERSPACE */
489
Andrew Boie73abd322017-04-04 13:19:13 -0700490struct k_thread {
491
492 struct _thread_base base;
493
494 /* defined by the architecture, but all archs need these */
495 struct _caller_saved caller_saved;
496 struct _callee_saved callee_saved;
497
498 /* static thread init data */
499 void *init_data;
500
501 /* abort function */
502 void (*fn_abort)(void);
503
504#if defined(CONFIG_THREAD_MONITOR)
505 /* thread entry and parameters description */
506 struct __thread_entry *entry;
507
508 /* next item in list of all threads */
509 struct k_thread *next_thread;
510#endif
511
512#ifdef CONFIG_THREAD_CUSTOM_DATA
513 /* crude thread-local storage */
514 void *custom_data;
515#endif
516
517#ifdef CONFIG_ERRNO
518 /* per-thread errno variable */
519 int errno_var;
520#endif
521
522#if defined(CONFIG_THREAD_STACK_INFO)
523 /* Stack Info */
524 struct _thread_stack_info stack_info;
525#endif /* CONFIG_THREAD_STACK_INFO */
526
Chunlin Hane9c97022017-07-07 20:29:30 +0800527#if defined(CONFIG_USERSPACE)
528 /* memory domain info of the thread */
529 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700530 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700531 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800532#endif /* CONFIG_USERSPACE */
533
Andy Ross042d8ec2017-12-09 08:37:20 -0800534#if defined(CONFIG_USE_SWITCH)
535 /* When using __switch() a few previously arch-specific items
536 * become part of the core OS
537 */
538
539 /* _Swap() return value */
540 int swap_retval;
541
542 /* Context handle returned via _arch_switch() */
543 void *switch_handle;
544#endif
Andrew Boie92e5bd72018-04-12 17:12:15 -0700545 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800546
Andrew Boie73abd322017-04-04 13:19:13 -0700547 /* arch-specifics: must always be at the end */
548 struct _thread_arch arch;
549};
550
551typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400552typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700553#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400554
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400555enum execution_context_types {
556 K_ISR = 0,
557 K_COOP_THREAD,
558 K_PREEMPT_THREAD,
559};
560
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400561/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100562 * @defgroup profiling_apis Profiling APIs
563 * @ingroup kernel_apis
564 * @{
565 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530566typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
567 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100568
569/**
570 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
571 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700572 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100573 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
574 *
575 * CONFIG_MAIN_STACK_SIZE
576 * CONFIG_IDLE_STACK_SIZE
577 * CONFIG_ISR_STACK_SIZE
578 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
579 *
580 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
581 * produce output.
582 *
583 * @return N/A
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530584 *
585 * @deprecated This API is deprecated. Use k_thread_foreach().
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100586 */
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530587__deprecated extern void k_call_stacks_analyze(void);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100588
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530589/**
590 * @brief Iterate over all the threads in the system.
591 *
592 * This routine iterates over all the threads in the system and
593 * calls the user_cb function for each thread.
594 *
595 * @param user_cb Pointer to the user callback function.
596 * @param user_data Pointer to user data.
597 *
598 * @note CONFIG_THREAD_MONITOR must be set for this function
599 * to be effective. Also this API uses irq_lock to protect the
600 * _kernel.threads list which means creation of new threads and
601 * terminations of existing threads are blocked until this
602 * API returns.
603 *
604 * @return N/A
605 */
606extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
607
Anas Nashif166f5192018-02-25 08:02:36 -0600608/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100609
610/**
Allan Stephensc98da842016-11-11 15:45:03 -0500611 * @defgroup thread_apis Thread APIs
612 * @ingroup kernel_apis
613 * @{
614 */
615
Benjamin Walshed240f22017-01-22 13:05:08 -0500616#endif /* !_ASMLANGUAGE */
617
618
619/*
620 * Thread user options. May be needed by assembly code. Common part uses low
621 * bits, arch-specific use high bits.
622 */
623
624/* system thread that must not abort */
625#define K_ESSENTIAL (1 << 0)
626
627#if defined(CONFIG_FP_SHARING)
628/* thread uses floating point registers */
629#define K_FP_REGS (1 << 1)
630#endif
631
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700632/* This thread has dropped from supervisor mode to user mode and consequently
633 * has additional restrictions
634 */
635#define K_USER (1 << 2)
636
Andrew Boie47f8fd12017-10-05 11:11:02 -0700637/* Indicates that the thread being created should inherit all kernel object
638 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
639 * is not enabled.
640 */
641#define K_INHERIT_PERMS (1 << 3)
642
Benjamin Walshed240f22017-01-22 13:05:08 -0500643#ifdef CONFIG_X86
644/* x86 Bitmask definitions for threads user options */
645
646#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
647/* thread uses SSEx (and also FP) registers */
648#define K_SSE_REGS (1 << 7)
649#endif
650#endif
651
652/* end - thread options */
653
654#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400655/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700656 * @brief Create a thread.
657 *
658 * This routine initializes a thread, then schedules it for execution.
659 *
660 * The new thread may be scheduled for immediate execution or a delayed start.
661 * If the newly spawned thread does not have a delayed start the kernel
662 * scheduler may preempt the current thread to allow the new thread to
663 * execute.
664 *
665 * Thread options are architecture-specific, and can include K_ESSENTIAL,
666 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
667 * them using "|" (the logical OR operator).
668 *
669 * Historically, users often would use the beginning of the stack memory region
670 * to store the struct k_thread data, although corruption will occur if the
671 * stack overflows this region and stack protection features may not detect this
672 * situation.
673 *
674 * @param new_thread Pointer to uninitialized struct k_thread
675 * @param stack Pointer to the stack space.
676 * @param stack_size Stack size in bytes.
677 * @param entry Thread entry function.
678 * @param p1 1st entry point parameter.
679 * @param p2 2nd entry point parameter.
680 * @param p3 3rd entry point parameter.
681 * @param prio Thread priority.
682 * @param options Thread options.
683 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
684 *
685 * @return ID of new thread.
686 */
Andrew Boie662c3452017-10-02 10:51:18 -0700687__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700688 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700689 size_t stack_size,
690 k_thread_entry_t entry,
691 void *p1, void *p2, void *p3,
692 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700693
Andrew Boie3f091b52017-08-30 14:34:14 -0700694/**
695 * @brief Drop a thread's privileges permanently to user mode
696 *
697 * @param entry Function to start executing from
698 * @param p1 1st entry point parameter
699 * @param p2 2nd entry point parameter
700 * @param p3 3rd entry point parameter
701 */
702extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
703 void *p1, void *p2,
704 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700705
Andrew Boied26cf2d2017-03-30 13:07:02 -0700706/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700707 * @brief Grant a thread access to a NULL-terminated set of kernel objects
708 *
709 * This is a convenience function. For the provided thread, grant access to
710 * the remaining arguments, which must be pointers to kernel objects.
711 * The final argument must be a NULL.
712 *
713 * The thread object must be initialized (i.e. running). The objects don't
714 * need to be.
715 *
716 * @param thread Thread to grant access to objects
717 * @param ... NULL-terminated list of kernel object pointers
718 */
719extern void __attribute__((sentinel))
720 k_thread_access_grant(struct k_thread *thread, ...);
721
722/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700723 * @brief Assign a resource memory pool to a thread
724 *
725 * By default, threads have no resource pool assigned unless their parent
726 * thread has a resource pool, in which case it is inherited. Multiple
727 * threads may be assigned to the same memory pool.
728 *
729 * Changing a thread's resource pool will not migrate allocations from the
730 * previous pool.
731 *
732 * @param thread Target thread to assign a memory pool for resource requests,
733 * or NULL if the thread should no longer have a memory pool.
734 * @param pool Memory pool to use for resources.
735 */
736static inline void k_thread_resource_pool_assign(struct k_thread *thread,
737 struct k_mem_pool *pool)
738{
739 thread->resource_pool = pool;
740}
741
742#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
743/**
744 * @brief Assign the system heap as a thread's resource pool
745 *
746 * Similar to k_thread_resource_pool_assign(), but the thread will use
747 * the kernel heap to draw memory.
748 *
749 * Use with caution, as a malicious thread could perform DoS attacks on the
750 * kernel heap.
751 *
752 * @param thread Target thread to assign the system heap for resource requests
753 */
754void k_thread_system_pool_assign(struct k_thread *thread);
755#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
756
757/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500758 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400759 *
Allan Stephensc98da842016-11-11 15:45:03 -0500760 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500761 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400762 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500763 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400764 *
765 * @return N/A
766 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700767__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400768
769/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500770 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400771 *
772 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500773 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400774 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400775 * @return N/A
776 */
Kumar Galacc334c72017-04-21 10:55:34 -0500777extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400778
779/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500780 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500782 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400783 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500784 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400785 *
786 * @return N/A
787 */
Andrew Boie468190a2017-09-29 14:00:48 -0700788__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400789
790/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500791 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400792 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500793 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400794 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500795 * If @a thread is not currently sleeping, the routine has no effect.
796 *
797 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400798 *
799 * @return N/A
800 */
Andrew Boie468190a2017-09-29 14:00:48 -0700801__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400802
803/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500804 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400805 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500806 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400807 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700808__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400809
810/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500811 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500813 * This routine prevents @a thread from executing if it has not yet started
814 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400815 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500816 * @param thread ID of thread to cancel.
817 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700818 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500819 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700820 *
821 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400822 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700823__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400824
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400825/**
Allan Stephensc98da842016-11-11 15:45:03 -0500826 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400827 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500828 * This routine permanently stops execution of @a thread. The thread is taken
829 * off all kernel queues it is part of (i.e. the ready queue, the timeout
830 * queue, or a kernel object wait queue). However, any kernel resources the
831 * thread might currently own (such as mutexes or memory blocks) are not
832 * released. It is the responsibility of the caller of this routine to ensure
833 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400834 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500835 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836 *
837 * @return N/A
838 */
Andrew Boie468190a2017-09-29 14:00:48 -0700839__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400840
Andrew Boie7d627c52017-08-30 11:01:56 -0700841
842/**
843 * @brief Start an inactive thread
844 *
845 * If a thread was created with K_FOREVER in the delay parameter, it will
846 * not be added to the scheduling queue until this function is called
847 * on it.
848 *
849 * @param thread thread to start
850 */
Andrew Boie468190a2017-09-29 14:00:48 -0700851__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700852
Allan Stephensc98da842016-11-11 15:45:03 -0500853/**
854 * @cond INTERNAL_HIDDEN
855 */
856
Benjamin Walshd211a522016-12-06 11:44:01 -0500857/* timeout has timed out and is not on _timeout_q anymore */
858#define _EXPIRED (-2)
859
860/* timeout is not in use */
861#define _INACTIVE (-1)
862
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400863struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700864 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700865 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400866 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700867 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500868 void *init_p1;
869 void *init_p2;
870 void *init_p3;
871 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500872 u32_t init_options;
873 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500874 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400875};
876
Andrew Boied26cf2d2017-03-30 13:07:02 -0700877#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400878 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500879 prio, options, delay, abort, groups) \
880 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700881 .init_thread = (thread), \
882 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500883 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700884 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400885 .init_p1 = (void *)p1, \
886 .init_p2 = (void *)p2, \
887 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500888 .init_prio = (prio), \
889 .init_options = (options), \
890 .init_delay = (delay), \
891 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400892 }
893
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400894/**
Allan Stephensc98da842016-11-11 15:45:03 -0500895 * INTERNAL_HIDDEN @endcond
896 */
897
898/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500899 * @brief Statically define and initialize a thread.
900 *
901 * The thread may be scheduled for immediate execution or a delayed start.
902 *
903 * Thread options are architecture-specific, and can include K_ESSENTIAL,
904 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
905 * them using "|" (the logical OR operator).
906 *
907 * The ID of the thread can be accessed using:
908 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500909 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500910 *
911 * @param name Name of the thread.
912 * @param stack_size Stack size in bytes.
913 * @param entry Thread entry function.
914 * @param p1 1st entry point parameter.
915 * @param p2 2nd entry point parameter.
916 * @param p3 3rd entry point parameter.
917 * @param prio Thread priority.
918 * @param options Thread options.
919 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400920 *
921 * @internal It has been observed that the x86 compiler by default aligns
922 * these _static_thread_data structures to 32-byte boundaries, thereby
923 * wasting space. To work around this, force a 4-byte alignment.
924 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500925#define K_THREAD_DEFINE(name, stack_size, \
926 entry, p1, p2, p3, \
927 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700928 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700929 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500930 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500931 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700932 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
933 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500934 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700935 NULL, 0); \
936 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400937
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400938/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500939 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400940 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500941 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500943 * @param thread ID of thread whose priority is needed.
944 *
945 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400946 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700947__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400948
949/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500950 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500952 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400953 *
954 * Rescheduling can occur immediately depending on the priority @a thread is
955 * set to:
956 *
957 * - If its priority is raised above the priority of the caller of this
958 * function, and the caller is preemptible, @a thread will be scheduled in.
959 *
960 * - If the caller operates on itself, it lowers its priority below that of
961 * other threads in the system, and the caller is preemptible, the thread of
962 * highest priority will be scheduled in.
963 *
964 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
965 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
966 * highest priority.
967 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500968 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400969 * @param prio New priority.
970 *
971 * @warning Changing the priority of a thread currently involved in mutex
972 * priority inheritance may result in undefined behavior.
973 *
974 * @return N/A
975 */
Andrew Boie468190a2017-09-29 14:00:48 -0700976__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400977
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400978/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500979 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400980 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500981 * This routine prevents the kernel scheduler from making @a thread the
982 * current thread. All other internal operations on @a thread are still
983 * performed; for example, any timeout it is waiting on keeps ticking,
984 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500986 * If @a thread is already suspended, the routine has no effect.
987 *
988 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400989 *
990 * @return N/A
991 */
Andrew Boie468190a2017-09-29 14:00:48 -0700992__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400993
994/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500995 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400996 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500997 * This routine allows the kernel scheduler to make @a thread the current
998 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400999 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001000 * If @a thread is not currently suspended, the routine has no effect.
1001 *
1002 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001003 *
1004 * @return N/A
1005 */
Andrew Boie468190a2017-09-29 14:00:48 -07001006__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001008/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001009 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001011 * This routine specifies how the scheduler will perform time slicing of
1012 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001014 * To enable time slicing, @a slice must be non-zero. The scheduler
1015 * ensures that no thread runs for more than the specified time limit
1016 * before other threads of that priority are given a chance to execute.
1017 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001018 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001019 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001020 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001021 * execute. Once the scheduler selects a thread for execution, there is no
1022 * minimum guaranteed time the thread will execute before threads of greater or
1023 * equal priority are scheduled.
1024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001025 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001026 * for execution, this routine has no effect; the thread is immediately
1027 * rescheduled after the slice period expires.
1028 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001029 * To disable timeslicing, set both @a slice and @a prio to zero.
1030 *
1031 * @param slice Maximum time slice length (in milliseconds).
1032 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001033 *
1034 * @return N/A
1035 */
Kumar Galacc334c72017-04-21 10:55:34 -05001036extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001037
Anas Nashif166f5192018-02-25 08:02:36 -06001038/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001039
1040/**
1041 * @addtogroup isr_apis
1042 * @{
1043 */
1044
1045/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001046 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001047 *
Allan Stephensc98da842016-11-11 15:45:03 -05001048 * This routine allows the caller to customize its actions, depending on
1049 * whether it is a thread or an ISR.
1050 *
1051 * @note Can be called by ISRs.
1052 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001053 * @return 0 if invoked by a thread.
1054 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001055 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001056extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001057
Benjamin Walsh445830d2016-11-10 15:54:27 -05001058/**
1059 * @brief Determine if code is running in a preemptible thread.
1060 *
Allan Stephensc98da842016-11-11 15:45:03 -05001061 * This routine allows the caller to customize its actions, depending on
1062 * whether it can be preempted by another thread. The routine returns a 'true'
1063 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001064 *
Allan Stephensc98da842016-11-11 15:45:03 -05001065 * - The code is running in a thread, not at ISR.
1066 * - The thread's priority is in the preemptible range.
1067 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001068 *
Allan Stephensc98da842016-11-11 15:45:03 -05001069 * @note Can be called by ISRs.
1070 *
1071 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001072 * @return Non-zero if invoked by a preemptible thread.
1073 */
Andrew Boie468190a2017-09-29 14:00:48 -07001074__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001075
Allan Stephensc98da842016-11-11 15:45:03 -05001076/**
Anas Nashif166f5192018-02-25 08:02:36 -06001077 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001078 */
1079
1080/**
1081 * @addtogroup thread_apis
1082 * @{
1083 */
1084
1085/**
1086 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001087 *
Allan Stephensc98da842016-11-11 15:45:03 -05001088 * This routine prevents the current thread from being preempted by another
1089 * thread by instructing the scheduler to treat it as a cooperative thread.
1090 * If the thread subsequently performs an operation that makes it unready,
1091 * it will be context switched out in the normal manner. When the thread
1092 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001093 *
Allan Stephensc98da842016-11-11 15:45:03 -05001094 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001095 *
Allan Stephensc98da842016-11-11 15:45:03 -05001096 * @note k_sched_lock() and k_sched_unlock() should normally be used
1097 * when the operation being performed can be safely interrupted by ISRs.
1098 * However, if the amount of processing involved is very small, better
1099 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001100 *
1101 * @return N/A
1102 */
1103extern void k_sched_lock(void);
1104
Allan Stephensc98da842016-11-11 15:45:03 -05001105/**
1106 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001107 *
Allan Stephensc98da842016-11-11 15:45:03 -05001108 * This routine reverses the effect of a previous call to k_sched_lock().
1109 * A thread must call the routine once for each time it called k_sched_lock()
1110 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001111 *
1112 * @return N/A
1113 */
1114extern void k_sched_unlock(void);
1115
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001116/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001117 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001119 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001120 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001121 * Custom data is not used by the kernel itself, and is freely available
1122 * for a thread to use as it sees fit. It can be used as a framework
1123 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001125 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001126 *
1127 * @return N/A
1128 */
Andrew Boie468190a2017-09-29 14:00:48 -07001129__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001130
1131/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001132 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001133 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001134 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001135 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001136 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001137 */
Andrew Boie468190a2017-09-29 14:00:48 -07001138__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001139
1140/**
Anas Nashif166f5192018-02-25 08:02:36 -06001141 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001142 */
1143
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001144#include <sys_clock.h>
1145
Allan Stephensc2f15a42016-11-17 12:24:22 -05001146/**
1147 * @addtogroup clock_apis
1148 * @{
1149 */
1150
1151/**
1152 * @brief Generate null timeout delay.
1153 *
1154 * This macro generates a timeout delay that that instructs a kernel API
1155 * not to wait if the requested operation cannot be performed immediately.
1156 *
1157 * @return Timeout delay value.
1158 */
1159#define K_NO_WAIT 0
1160
1161/**
1162 * @brief Generate timeout delay from milliseconds.
1163 *
1164 * This macro generates a timeout delay that that instructs a kernel API
1165 * to wait up to @a ms milliseconds to perform the requested operation.
1166 *
1167 * @param ms Duration in milliseconds.
1168 *
1169 * @return Timeout delay value.
1170 */
Johan Hedberg14471692016-11-13 10:52:15 +02001171#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001172
1173/**
1174 * @brief Generate timeout delay from seconds.
1175 *
1176 * This macro generates a timeout delay that that instructs a kernel API
1177 * to wait up to @a s seconds to perform the requested operation.
1178 *
1179 * @param s Duration in seconds.
1180 *
1181 * @return Timeout delay value.
1182 */
Johan Hedberg14471692016-11-13 10:52:15 +02001183#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001184
1185/**
1186 * @brief Generate timeout delay from minutes.
1187 *
1188 * This macro generates a timeout delay that that instructs a kernel API
1189 * to wait up to @a m minutes to perform the requested operation.
1190 *
1191 * @param m Duration in minutes.
1192 *
1193 * @return Timeout delay value.
1194 */
Johan Hedberg14471692016-11-13 10:52:15 +02001195#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001196
1197/**
1198 * @brief Generate timeout delay from hours.
1199 *
1200 * This macro generates a timeout delay that that instructs a kernel API
1201 * to wait up to @a h hours to perform the requested operation.
1202 *
1203 * @param h Duration in hours.
1204 *
1205 * @return Timeout delay value.
1206 */
Johan Hedberg14471692016-11-13 10:52:15 +02001207#define K_HOURS(h) K_MINUTES((h) * 60)
1208
Allan Stephensc98da842016-11-11 15:45:03 -05001209/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001210 * @brief Generate infinite timeout delay.
1211 *
1212 * This macro generates a timeout delay that that instructs a kernel API
1213 * to wait as long as necessary to perform the requested operation.
1214 *
1215 * @return Timeout delay value.
1216 */
1217#define K_FOREVER (-1)
1218
1219/**
Anas Nashif166f5192018-02-25 08:02:36 -06001220 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001221 */
1222
1223/**
Allan Stephensc98da842016-11-11 15:45:03 -05001224 * @cond INTERNAL_HIDDEN
1225 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001226
Benjamin Walsh62092182016-12-20 14:39:08 -05001227/* kernel clocks */
1228
1229#if (sys_clock_ticks_per_sec == 1000) || \
1230 (sys_clock_ticks_per_sec == 500) || \
1231 (sys_clock_ticks_per_sec == 250) || \
1232 (sys_clock_ticks_per_sec == 125) || \
1233 (sys_clock_ticks_per_sec == 100) || \
1234 (sys_clock_ticks_per_sec == 50) || \
1235 (sys_clock_ticks_per_sec == 25) || \
1236 (sys_clock_ticks_per_sec == 20) || \
1237 (sys_clock_ticks_per_sec == 10) || \
1238 (sys_clock_ticks_per_sec == 1)
1239
1240 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1241#else
1242 /* yields horrible 64-bit math on many architectures: try to avoid */
1243 #define _NON_OPTIMIZED_TICKS_PER_SEC
1244#endif
1245
1246#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001247extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001248#else
Kumar Galacc334c72017-04-21 10:55:34 -05001249static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001250{
Kumar Galacc334c72017-04-21 10:55:34 -05001251 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001252}
1253#endif
1254
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001255/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001256#ifdef CONFIG_TICKLESS_KERNEL
1257#define _TICK_ALIGN 0
1258#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001259#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001260#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001261
Kumar Galacc334c72017-04-21 10:55:34 -05001262static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001263{
Benjamin Walsh62092182016-12-20 14:39:08 -05001264#ifdef CONFIG_SYS_CLOCK_EXISTS
1265
1266#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001267 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001268#else
Kumar Galacc334c72017-04-21 10:55:34 -05001269 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001270#endif
1271
1272#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001273 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001274 return 0;
1275#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001276}
1277
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001278struct k_timer {
1279 /*
1280 * _timeout structure must be first here if we want to use
1281 * dynamic timer allocation. timeout.node is used in the double-linked
1282 * list of free timers
1283 */
1284 struct _timeout timeout;
1285
Allan Stephens45bfa372016-10-12 12:39:42 -05001286 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001287 _wait_q_t wait_q;
1288
1289 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001290 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001291
1292 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001293 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001294
1295 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001296 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001297
Allan Stephens45bfa372016-10-12 12:39:42 -05001298 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001299 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001300
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001301 /* user-specific data, also used to support legacy features */
1302 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001303
Anas Nashif2f203c22016-12-18 06:57:45 -05001304 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001305};
1306
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001307#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001308 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001309 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001310 .timeout.wait_q = NULL, \
1311 .timeout.thread = NULL, \
1312 .timeout.func = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001313 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001314 .expiry_fn = expiry, \
1315 .stop_fn = stop, \
1316 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001317 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001318 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001319 }
1320
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001321#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1322
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001323/**
Allan Stephensc98da842016-11-11 15:45:03 -05001324 * INTERNAL_HIDDEN @endcond
1325 */
1326
1327/**
1328 * @defgroup timer_apis Timer APIs
1329 * @ingroup kernel_apis
1330 * @{
1331 */
1332
1333/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001334 * @typedef k_timer_expiry_t
1335 * @brief Timer expiry function type.
1336 *
1337 * A timer's expiry function is executed by the system clock interrupt handler
1338 * each time the timer expires. The expiry function is optional, and is only
1339 * invoked if the timer has been initialized with one.
1340 *
1341 * @param timer Address of timer.
1342 *
1343 * @return N/A
1344 */
1345typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1346
1347/**
1348 * @typedef k_timer_stop_t
1349 * @brief Timer stop function type.
1350 *
1351 * A timer's stop function is executed if the timer is stopped prematurely.
1352 * The function runs in the context of the thread that stops the timer.
1353 * The stop function is optional, and is only invoked if the timer has been
1354 * initialized with one.
1355 *
1356 * @param timer Address of timer.
1357 *
1358 * @return N/A
1359 */
1360typedef void (*k_timer_stop_t)(struct k_timer *timer);
1361
1362/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001363 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001364 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001365 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001366 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001367 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001368 *
1369 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001370 * @param expiry_fn Function to invoke each time the timer expires.
1371 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001372 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001373#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001374 struct k_timer name \
1375 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001376 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001377
Allan Stephens45bfa372016-10-12 12:39:42 -05001378/**
1379 * @brief Initialize a timer.
1380 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001381 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001382 *
1383 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001384 * @param expiry_fn Function to invoke each time the timer expires.
1385 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001386 *
1387 * @return N/A
1388 */
1389extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001390 k_timer_expiry_t expiry_fn,
1391 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001392
Allan Stephens45bfa372016-10-12 12:39:42 -05001393/**
1394 * @brief Start a timer.
1395 *
1396 * This routine starts a timer, and resets its status to zero. The timer
1397 * begins counting down using the specified duration and period values.
1398 *
1399 * Attempting to start a timer that is already running is permitted.
1400 * The timer's status is reset to zero and the timer begins counting down
1401 * using the new duration and period values.
1402 *
1403 * @param timer Address of timer.
1404 * @param duration Initial timer duration (in milliseconds).
1405 * @param period Timer period (in milliseconds).
1406 *
1407 * @return N/A
1408 */
Andrew Boiea354d492017-09-29 16:22:28 -07001409__syscall void k_timer_start(struct k_timer *timer,
1410 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001411
1412/**
1413 * @brief Stop a timer.
1414 *
1415 * This routine stops a running timer prematurely. The timer's stop function,
1416 * if one exists, is invoked by the caller.
1417 *
1418 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001419 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001420 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001421 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1422 * if @a k_timer_stop is to be called from ISRs.
1423 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001424 * @param timer Address of timer.
1425 *
1426 * @return N/A
1427 */
Andrew Boiea354d492017-09-29 16:22:28 -07001428__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001429
1430/**
1431 * @brief Read timer status.
1432 *
1433 * This routine reads the timer's status, which indicates the number of times
1434 * it has expired since its status was last read.
1435 *
1436 * Calling this routine resets the timer's status to zero.
1437 *
1438 * @param timer Address of timer.
1439 *
1440 * @return Timer status.
1441 */
Andrew Boiea354d492017-09-29 16:22:28 -07001442__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001443
1444/**
1445 * @brief Synchronize thread to timer expiration.
1446 *
1447 * This routine blocks the calling thread until the timer's status is non-zero
1448 * (indicating that it has expired at least once since it was last examined)
1449 * or the timer is stopped. If the timer status is already non-zero,
1450 * or the timer is already stopped, the caller continues without waiting.
1451 *
1452 * Calling this routine resets the timer's status to zero.
1453 *
1454 * This routine must not be used by interrupt handlers, since they are not
1455 * allowed to block.
1456 *
1457 * @param timer Address of timer.
1458 *
1459 * @return Timer status.
1460 */
Andrew Boiea354d492017-09-29 16:22:28 -07001461__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001462
1463/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001464 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001465 *
1466 * This routine computes the (approximate) time remaining before a running
1467 * timer next expires. If the timer is not running, it returns zero.
1468 *
1469 * @param timer Address of timer.
1470 *
1471 * @return Remaining time (in milliseconds).
1472 */
Andrew Boiea354d492017-09-29 16:22:28 -07001473__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1474
1475static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001476{
1477 return _timeout_remaining_get(&timer->timeout);
1478}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001479
Allan Stephensc98da842016-11-11 15:45:03 -05001480/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001481 * @brief Associate user-specific data with a timer.
1482 *
1483 * This routine records the @a user_data with the @a timer, to be retrieved
1484 * later.
1485 *
1486 * It can be used e.g. in a timer handler shared across multiple subsystems to
1487 * retrieve data specific to the subsystem this timer is associated with.
1488 *
1489 * @param timer Address of timer.
1490 * @param user_data User data to associate with the timer.
1491 *
1492 * @return N/A
1493 */
Andrew Boiea354d492017-09-29 16:22:28 -07001494__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1495
Anas Nashif954d5502018-02-25 08:37:28 -06001496/**
1497 * @internal
1498 */
Andrew Boiea354d492017-09-29 16:22:28 -07001499static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1500 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001501{
1502 timer->user_data = user_data;
1503}
1504
1505/**
1506 * @brief Retrieve the user-specific data from a timer.
1507 *
1508 * @param timer Address of timer.
1509 *
1510 * @return The user data.
1511 */
Andrew Boiea354d492017-09-29 16:22:28 -07001512__syscall void *k_timer_user_data_get(struct k_timer *timer);
1513
1514static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001515{
1516 return timer->user_data;
1517}
1518
Anas Nashif166f5192018-02-25 08:02:36 -06001519/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001520
Allan Stephensc98da842016-11-11 15:45:03 -05001521/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001522 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001523 * @{
1524 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001525
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001526/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001527 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001529 * This routine returns the elapsed time since the system booted,
1530 * in milliseconds.
1531 *
1532 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001533 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001534__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001535
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001536/**
1537 * @brief Enable clock always on in tickless kernel
1538 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001539 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001540 * there are no timer events programmed in tickless kernel
1541 * scheduling. This is necessary if the clock is used to track
1542 * passage of time.
1543 *
1544 * @retval prev_status Previous status of always on flag
1545 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301546#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001547static inline int k_enable_sys_clock_always_on(void)
1548{
1549 int prev_status = _sys_clock_always_on;
1550
1551 _sys_clock_always_on = 1;
1552 _enable_sys_clock();
1553
1554 return prev_status;
1555}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301556#else
1557#define k_enable_sys_clock_always_on() do { } while ((0))
1558#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001559
1560/**
1561 * @brief Disable clock always on in tickless kernel
1562 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001563 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001564 * there are no timer events programmed in tickless kernel
1565 * scheduling. To save power, this routine should be called
1566 * immediately when clock is not used to track time.
1567 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301568#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001569static inline void k_disable_sys_clock_always_on(void)
1570{
1571 _sys_clock_always_on = 0;
1572}
1573#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001574#define k_disable_sys_clock_always_on() do { } while ((0))
1575#endif
1576
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001577/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001578 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001579 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001580 * This routine returns the lower 32-bits of the elapsed time since the system
1581 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001583 * This routine can be more efficient than k_uptime_get(), as it reduces the
1584 * need for interrupt locking and 64-bit math. However, the 32-bit result
1585 * cannot hold a system uptime time larger than approximately 50 days, so the
1586 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001587 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001588 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001589 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001590__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001591
1592/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001593 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001594 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001595 * This routine computes the elapsed time between the current system uptime
1596 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001597 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001598 * @param reftime Pointer to a reference time, which is updated to the current
1599 * uptime upon return.
1600 *
1601 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001602 */
Kumar Galacc334c72017-04-21 10:55:34 -05001603extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001604
1605/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001606 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001608 * This routine computes the elapsed time between the current system uptime
1609 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001610 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001611 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1612 * need for interrupt locking and 64-bit math. However, the 32-bit result
1613 * cannot hold an elapsed time larger than approximately 50 days, so the
1614 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001615 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001616 * @param reftime Pointer to a reference time, which is updated to the current
1617 * uptime upon return.
1618 *
1619 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001620 */
Kumar Galacc334c72017-04-21 10:55:34 -05001621extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001622
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001623/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001624 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001626 * This routine returns the current time, as measured by the system's hardware
1627 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001628 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001629 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001630 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001631#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001632
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001633/**
Anas Nashif166f5192018-02-25 08:02:36 -06001634 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001635 */
1636
Allan Stephensc98da842016-11-11 15:45:03 -05001637/**
1638 * @cond INTERNAL_HIDDEN
1639 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001640
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001641struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001642 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001643 union {
1644 _wait_q_t wait_q;
1645
1646 _POLL_EVENT;
1647 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001648
1649 _OBJECT_TRACING_NEXT_PTR(k_queue);
1650};
1651
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001652#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001653 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001654 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001655 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001656 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001657 _OBJECT_TRACING_INIT \
1658 }
1659
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001660#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1661
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001662extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1663
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001664/**
1665 * INTERNAL_HIDDEN @endcond
1666 */
1667
1668/**
1669 * @defgroup queue_apis Queue APIs
1670 * @ingroup kernel_apis
1671 * @{
1672 */
1673
1674/**
1675 * @brief Initialize a queue.
1676 *
1677 * This routine initializes a queue object, prior to its first use.
1678 *
1679 * @param queue Address of the queue.
1680 *
1681 * @return N/A
1682 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001683__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001684
1685/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001686 * @brief Cancel waiting on a queue.
1687 *
1688 * This routine causes first thread pending on @a queue, if any, to
1689 * return from k_queue_get() call with NULL value (as if timeout expired).
1690 *
1691 * @note Can be called by ISRs.
1692 *
1693 * @param queue Address of the queue.
1694 *
1695 * @return N/A
1696 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001697__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001698
1699/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001700 * @brief Append an element to the end of a queue.
1701 *
1702 * This routine appends a data item to @a queue. A queue data item must be
1703 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1704 * reserved for the kernel's use.
1705 *
1706 * @note Can be called by ISRs.
1707 *
1708 * @param queue Address of the queue.
1709 * @param data Address of the data item.
1710 *
1711 * @return N/A
1712 */
1713extern void k_queue_append(struct k_queue *queue, void *data);
1714
1715/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001716 * @brief Append an element to a queue.
1717 *
1718 * This routine appends a data item to @a queue. There is an implicit
1719 * memory allocation from the calling thread's resource pool, which is
1720 * automatically freed when the item is removed from the queue.
1721 *
1722 * @note Can be called by ISRs.
1723 *
1724 * @param queue Address of the queue.
1725 * @param data Address of the data item.
1726 *
1727 * @retval 0 on success
1728 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1729 */
1730__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1731
1732/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001733 * @brief Prepend an element to a queue.
1734 *
1735 * This routine prepends a data item to @a queue. A queue data item must be
1736 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1737 * reserved for the kernel's use.
1738 *
1739 * @note Can be called by ISRs.
1740 *
1741 * @param queue Address of the queue.
1742 * @param data Address of the data item.
1743 *
1744 * @return N/A
1745 */
1746extern void k_queue_prepend(struct k_queue *queue, void *data);
1747
1748/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001749 * @brief Prepend an element to a queue.
1750 *
1751 * This routine prepends a data item to @a queue. There is an implicit
1752 * memory allocation from the calling thread's resource pool, which is
1753 * automatically freed when the item is removed from the queue.
1754 *
1755 * @note Can be called by ISRs.
1756 *
1757 * @param queue Address of the queue.
1758 * @param data Address of the data item.
1759 *
1760 * @retval 0 on success
1761 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1762 */
1763__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1764
1765/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001766 * @brief Inserts an element to a queue.
1767 *
1768 * This routine inserts a data item to @a queue after previous item. A queue
1769 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1770 * item are reserved for the kernel's use.
1771 *
1772 * @note Can be called by ISRs.
1773 *
1774 * @param queue Address of the queue.
1775 * @param prev Address of the previous data item.
1776 * @param data Address of the data item.
1777 *
1778 * @return N/A
1779 */
1780extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1781
1782/**
1783 * @brief Atomically append a list of elements to a queue.
1784 *
1785 * This routine adds a list of data items to @a queue in one operation.
1786 * The data items must be in a singly-linked list, with the first 32 bits
1787 * in each data item pointing to the next data item; the list must be
1788 * NULL-terminated.
1789 *
1790 * @note Can be called by ISRs.
1791 *
1792 * @param queue Address of the queue.
1793 * @param head Pointer to first node in singly-linked list.
1794 * @param tail Pointer to last node in singly-linked list.
1795 *
1796 * @return N/A
1797 */
1798extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1799
1800/**
1801 * @brief Atomically add a list of elements to a queue.
1802 *
1803 * This routine adds a list of data items to @a queue in one operation.
1804 * The data items must be in a singly-linked list implemented using a
1805 * sys_slist_t object. Upon completion, the original list is empty.
1806 *
1807 * @note Can be called by ISRs.
1808 *
1809 * @param queue Address of the queue.
1810 * @param list Pointer to sys_slist_t object.
1811 *
1812 * @return N/A
1813 */
1814extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1815
1816/**
1817 * @brief Get an element from a queue.
1818 *
1819 * This routine removes first data item from @a queue. The first 32 bits of the
1820 * data item are reserved for the kernel's use.
1821 *
1822 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1823 *
1824 * @param queue Address of the queue.
1825 * @param timeout Waiting period to obtain a data item (in milliseconds),
1826 * or one of the special values K_NO_WAIT and K_FOREVER.
1827 *
1828 * @return Address of the data item if successful; NULL if returned
1829 * without waiting, or waiting period timed out.
1830 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001831__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001832
1833/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001834 * @brief Remove an element from a queue.
1835 *
1836 * This routine removes data item from @a queue. The first 32 bits of the
1837 * data item are reserved for the kernel's use. Removing elements from k_queue
1838 * rely on sys_slist_find_and_remove which is not a constant time operation.
1839 *
1840 * @note Can be called by ISRs
1841 *
1842 * @param queue Address of the queue.
1843 * @param data Address of the data item.
1844 *
1845 * @return true if data item was removed
1846 */
1847static inline bool k_queue_remove(struct k_queue *queue, void *data)
1848{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001849 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001850}
1851
1852/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001853 * @brief Query a queue to see if it has data available.
1854 *
1855 * Note that the data might be already gone by the time this function returns
1856 * if other threads are also trying to read from the queue.
1857 *
1858 * @note Can be called by ISRs.
1859 *
1860 * @param queue Address of the queue.
1861 *
1862 * @return Non-zero if the queue is empty.
1863 * @return 0 if data is available.
1864 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001865__syscall int k_queue_is_empty(struct k_queue *queue);
1866
1867static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001868{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001869 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001870}
1871
1872/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001873 * @brief Peek element at the head of queue.
1874 *
1875 * Return element from the head of queue without removing it.
1876 *
1877 * @param queue Address of the queue.
1878 *
1879 * @return Head element, or NULL if queue is empty.
1880 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001881__syscall void *k_queue_peek_head(struct k_queue *queue);
1882
1883static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001884{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001885 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001886}
1887
1888/**
1889 * @brief Peek element at the tail of queue.
1890 *
1891 * Return element from the tail of queue without removing it.
1892 *
1893 * @param queue Address of the queue.
1894 *
1895 * @return Tail element, or NULL if queue is empty.
1896 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001897__syscall void *k_queue_peek_tail(struct k_queue *queue);
1898
1899static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001900{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001901 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001902}
1903
1904/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001905 * @brief Statically define and initialize a queue.
1906 *
1907 * The queue can be accessed outside the module where it is defined using:
1908 *
1909 * @code extern struct k_queue <name>; @endcode
1910 *
1911 * @param name Name of the queue.
1912 */
1913#define K_QUEUE_DEFINE(name) \
1914 struct k_queue name \
1915 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001916 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001917
Anas Nashif166f5192018-02-25 08:02:36 -06001918/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001919
1920/**
1921 * @cond INTERNAL_HIDDEN
1922 */
1923
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001924struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001925 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001926};
1927
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001928#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001929 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001930 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001931 }
1932
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001933#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1934
Allan Stephensc98da842016-11-11 15:45:03 -05001935/**
1936 * INTERNAL_HIDDEN @endcond
1937 */
1938
1939/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001940 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001941 * @ingroup kernel_apis
1942 * @{
1943 */
1944
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001945/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001946 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001947 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001948 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001949 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001950 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001951 *
1952 * @return N/A
1953 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001954#define k_fifo_init(fifo) \
1955 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001956
1957/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001958 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001959 *
1960 * This routine causes first thread pending on @a fifo, if any, to
1961 * return from k_fifo_get() call with NULL value (as if timeout
1962 * expired).
1963 *
1964 * @note Can be called by ISRs.
1965 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001966 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001967 *
1968 * @return N/A
1969 */
1970#define k_fifo_cancel_wait(fifo) \
1971 k_queue_cancel_wait((struct k_queue *) fifo)
1972
1973/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001974 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001975 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001976 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001977 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1978 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001979 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001980 * @note Can be called by ISRs.
1981 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001982 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001983 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001984 *
1985 * @return N/A
1986 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001987#define k_fifo_put(fifo, data) \
1988 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001989
1990/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001991 * @brief Add an element to a FIFO queue.
1992 *
1993 * This routine adds a data item to @a fifo. There is an implicit
1994 * memory allocation from the calling thread's resource pool, which is
1995 * automatically freed when the item is removed.
1996 *
1997 * @note Can be called by ISRs.
1998 *
1999 * @param fifo Address of the FIFO.
2000 * @param data Address of the data item.
2001 *
2002 * @retval 0 on success
2003 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2004 */
2005#define k_fifo_alloc_put(fifo, data) \
2006 k_queue_alloc_append((struct k_queue *) fifo, data)
2007
2008/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002009 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002010 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002011 * This routine adds a list of data items to @a fifo in one operation.
2012 * The data items must be in a singly-linked list, with the first 32 bits
2013 * each data item pointing to the next data item; the list must be
2014 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002016 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002017 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002018 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002019 * @param head Pointer to first node in singly-linked list.
2020 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002021 *
2022 * @return N/A
2023 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002024#define k_fifo_put_list(fifo, head, tail) \
2025 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002026
2027/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002028 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002029 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002030 * This routine adds a list of data items to @a fifo in one operation.
2031 * The data items must be in a singly-linked list implemented using a
2032 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002033 * and must be re-initialized via sys_slist_init().
2034 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002035 * @note Can be called by ISRs.
2036 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002037 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002038 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002039 *
2040 * @return N/A
2041 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002042#define k_fifo_put_slist(fifo, list) \
2043 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002044
2045/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002046 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002048 * This routine removes a data item from @a fifo in a "first in, first out"
2049 * manner. The first 32 bits of the data item are 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, but @a timeout must be set to K_NO_WAIT.
2052 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002053 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002054 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002055 * or one of the special values K_NO_WAIT and K_FOREVER.
2056 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002057 * @return Address of the data item if successful; NULL if returned
2058 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002059 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002060#define k_fifo_get(fifo, timeout) \
2061 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002062
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002063/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002064 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002065 *
2066 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002067 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002068 *
2069 * @note Can be called by ISRs.
2070 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002071 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002072 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002073 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002074 * @return 0 if data is available.
2075 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002076#define k_fifo_is_empty(fifo) \
2077 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002078
2079/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002080 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002081 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002082 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302083 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002084 * on each iteration of processing, a head container will be peeked,
2085 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002086 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002087 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002088 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002089 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002090 * @return Head element, or NULL if the FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002091 */
2092#define k_fifo_peek_head(fifo) \
2093 k_queue_peek_head((struct k_queue *) fifo)
2094
2095/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002096 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002097 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002098 * Return element from the tail of FIFO queue (without removing it). A usecase
2099 * for this is if elements of the FIFO queue are themselves containers. Then
2100 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002101 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002102 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002103 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002104 * @return Tail element, or NULL if a FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002105 */
2106#define k_fifo_peek_tail(fifo) \
2107 k_queue_peek_tail((struct k_queue *) fifo)
2108
2109/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002110 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002111 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002112 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002113 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002114 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002115 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002116 * @param name Name of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002117 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002118#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002119 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002120 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002121 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002122
Anas Nashif166f5192018-02-25 08:02:36 -06002123/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002124
2125/**
2126 * @cond INTERNAL_HIDDEN
2127 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002128
2129struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002130 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002131};
2132
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002133#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002134 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002135 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002136 }
2137
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002138#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2139
Allan Stephensc98da842016-11-11 15:45:03 -05002140/**
2141 * INTERNAL_HIDDEN @endcond
2142 */
2143
2144/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002145 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002146 * @ingroup kernel_apis
2147 * @{
2148 */
2149
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002150/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002151 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002152 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002153 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002154 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002155 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002156 *
2157 * @return N/A
2158 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002159#define k_lifo_init(lifo) \
2160 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002161
2162/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002163 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002164 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002165 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002166 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2167 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002168 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002169 * @note Can be called by ISRs.
2170 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002171 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002172 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002173 *
2174 * @return N/A
2175 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002176#define k_lifo_put(lifo, data) \
2177 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002178
2179/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002180 * @brief Add an element to a LIFO queue.
2181 *
2182 * This routine adds a data item to @a lifo. There is an implicit
2183 * memory allocation from the calling thread's resource pool, which is
2184 * automatically freed when the item is removed.
2185 *
2186 * @note Can be called by ISRs.
2187 *
2188 * @param lifo Address of the LIFO.
2189 * @param data Address of the data item.
2190 *
2191 * @retval 0 on success
2192 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2193 */
2194#define k_lifo_alloc_put(lifo, data) \
2195 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2196
2197/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002198 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002199 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002200 * This routine removes a data item from @a lifo in a "last in, first out"
2201 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002202 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002203 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2204 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002205 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002206 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002207 * or one of the special values K_NO_WAIT and K_FOREVER.
2208 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002209 * @return Address of the data item if successful; NULL if returned
2210 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002211 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002212#define k_lifo_get(lifo, timeout) \
2213 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002214
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002215/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002216 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002217 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002218 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002219 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002220 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002221 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002222 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002223 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002224#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002225 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002226 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002227 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002228
Anas Nashif166f5192018-02-25 08:02:36 -06002229/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002230
2231/**
2232 * @cond INTERNAL_HIDDEN
2233 */
Andrew Boief3bee952018-05-02 17:44:39 -07002234#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002235
2236struct k_stack {
2237 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002238 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002239
Anas Nashif2f203c22016-12-18 06:57:45 -05002240 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002241 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002242};
2243
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002244#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002245 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002246 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002247 .base = stack_buffer, \
2248 .next = stack_buffer, \
2249 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002250 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002251 }
2252
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002253#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2254
Allan Stephensc98da842016-11-11 15:45:03 -05002255/**
2256 * INTERNAL_HIDDEN @endcond
2257 */
2258
2259/**
2260 * @defgroup stack_apis Stack APIs
2261 * @ingroup kernel_apis
2262 * @{
2263 */
2264
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002265/**
2266 * @brief Initialize a stack.
2267 *
2268 * This routine initializes a stack object, prior to its first use.
2269 *
2270 * @param stack Address of the stack.
2271 * @param buffer Address of array used to hold stacked values.
2272 * @param num_entries Maximum number of values that can be stacked.
2273 *
2274 * @return N/A
2275 */
Andrew Boief3bee952018-05-02 17:44:39 -07002276void k_stack_init(struct k_stack *stack,
2277 u32_t *buffer, unsigned int num_entries);
2278
2279
2280/**
2281 * @brief Initialize a stack.
2282 *
2283 * This routine initializes a stack object, prior to its first use. Internal
2284 * buffers will be allocated from the calling thread's resource pool.
2285 * This memory will be released if k_stack_cleanup() is called, or
2286 * userspace is enabled and the stack object loses all references to it.
2287 *
2288 * @param stack Address of the stack.
2289 * @param num_entries Maximum number of values that can be stacked.
2290 *
2291 * @return -ENOMEM if memory couldn't be allocated
2292 */
2293
2294__syscall int k_stack_alloc_init(struct k_stack *stack,
2295 unsigned int num_entries);
2296
2297/**
2298 * @brief Release a stack's allocated buffer
2299 *
2300 * If a stack object was given a dynamically allocated buffer via
2301 * k_stack_alloc_init(), this will free it. This function does nothing
2302 * if the buffer wasn't dynamically allocated.
2303 *
2304 * @param stack Address of the stack.
2305 */
2306void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002307
2308/**
2309 * @brief Push an element onto a stack.
2310 *
2311 * This routine adds a 32-bit value @a data to @a stack.
2312 *
2313 * @note Can be called by ISRs.
2314 *
2315 * @param stack Address of the stack.
2316 * @param data Value to push onto the stack.
2317 *
2318 * @return N/A
2319 */
Andrew Boiee8734462017-09-29 16:42:07 -07002320__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002321
2322/**
2323 * @brief Pop an element from a stack.
2324 *
2325 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2326 * manner and stores the value in @a data.
2327 *
2328 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2329 *
2330 * @param stack Address of the stack.
2331 * @param data Address of area to hold the value popped from the stack.
2332 * @param timeout Waiting period to obtain a value (in milliseconds),
2333 * or one of the special values K_NO_WAIT and K_FOREVER.
2334 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002335 * @retval 0 Element popped from stack.
2336 * @retval -EBUSY Returned without waiting.
2337 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002338 */
Andrew Boiee8734462017-09-29 16:42:07 -07002339__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002340
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002341/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002342 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002343 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002344 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002345 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002346 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002347 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002348 * @param name Name of the stack.
2349 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002350 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002351#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002352 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002353 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002354 struct k_stack name \
2355 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002356 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002357 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002358
Anas Nashif166f5192018-02-25 08:02:36 -06002359/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002360
Allan Stephens6bba9b02016-11-16 14:56:54 -05002361struct k_work;
2362
Allan Stephensc98da842016-11-11 15:45:03 -05002363/**
2364 * @defgroup workqueue_apis Workqueue Thread APIs
2365 * @ingroup kernel_apis
2366 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002367 */
2368
Allan Stephens6bba9b02016-11-16 14:56:54 -05002369/**
2370 * @typedef k_work_handler_t
2371 * @brief Work item handler function type.
2372 *
2373 * A work item's handler function is executed by a workqueue's thread
2374 * when the work item is processed by the workqueue.
2375 *
2376 * @param work Address of the work item.
2377 *
2378 * @return N/A
2379 */
2380typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002381
2382/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002383 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002384 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002385
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002386struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002387 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002388 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002389};
2390
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002391enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002392 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002393};
2394
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002395struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002396 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002397 k_work_handler_t handler;
2398 atomic_t flags[1];
2399};
2400
Allan Stephens6bba9b02016-11-16 14:56:54 -05002401struct k_delayed_work {
2402 struct k_work work;
2403 struct _timeout timeout;
2404 struct k_work_q *work_q;
2405};
2406
2407extern struct k_work_q k_sys_work_q;
2408
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002409/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002410 * INTERNAL_HIDDEN @endcond
2411 */
2412
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002413#define _K_WORK_INITIALIZER(work_handler) \
2414 { \
2415 ._reserved = NULL, \
2416 .handler = work_handler, \
2417 .flags = { 0 } \
2418 }
2419
2420#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2421
Allan Stephens6bba9b02016-11-16 14:56:54 -05002422/**
2423 * @brief Initialize a statically-defined work item.
2424 *
2425 * This macro can be used to initialize a statically-defined workqueue work
2426 * item, prior to its first use. For example,
2427 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002428 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002429 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002430 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002431 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002432 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002433#define K_WORK_DEFINE(work, work_handler) \
2434 struct k_work work \
2435 __in_section(_k_work, static, work) = \
2436 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002437
2438/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002439 * @brief Initialize a work item.
2440 *
2441 * This routine initializes a workqueue work item, prior to its first use.
2442 *
2443 * @param work Address of work item.
2444 * @param handler Function to invoke each time work item is processed.
2445 *
2446 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002447 */
2448static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2449{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002450 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002451 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002452 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002453}
2454
2455/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002456 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002457 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002458 * This routine submits work item @a work to be processed by workqueue
2459 * @a work_q. If the work item is already pending in the workqueue's queue
2460 * as a result of an earlier submission, this routine has no effect on the
2461 * work item. If the work item has already been processed, or is currently
2462 * being processed, its work is considered complete and the work item can be
2463 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002464 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002465 * @warning
2466 * A submitted work item must not be modified until it has been processed
2467 * by the workqueue.
2468 *
2469 * @note Can be called by ISRs.
2470 *
2471 * @param work_q Address of workqueue.
2472 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002473 *
2474 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002475 */
2476static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2477 struct k_work *work)
2478{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002479 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002480 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002481 }
2482}
2483
2484/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002485 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002486 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002487 * This routine indicates if work item @a work is pending in a workqueue's
2488 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002489 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002490 * @note Can be called by ISRs.
2491 *
2492 * @param work Address of work item.
2493 *
2494 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002495 */
2496static inline int k_work_pending(struct k_work *work)
2497{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002498 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002499}
2500
2501/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002502 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002503 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002504 * This routine starts workqueue @a work_q. The workqueue spawns its work
2505 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002506 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002507 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002508 * @param stack Pointer to work queue thread's stack space, as defined by
2509 * K_THREAD_STACK_DEFINE()
2510 * @param stack_size Size of the work queue thread's stack (in bytes), which
2511 * should either be the same constant passed to
2512 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002513 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002514 *
2515 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002516 */
Andrew Boie507852a2017-07-25 18:47:07 -07002517extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002518 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002519 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002520
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002521/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002522 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002523 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002524 * This routine initializes a workqueue delayed work item, prior to
2525 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002526 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002527 * @param work Address of delayed work item.
2528 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002529 *
2530 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002531 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002532extern void k_delayed_work_init(struct k_delayed_work *work,
2533 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002534
2535/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002536 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002537 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002538 * This routine schedules work item @a work to be processed by workqueue
2539 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002540 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002541 * Only when the countdown completes is the work item actually submitted to
2542 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002543 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002544 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002545 * counting down cancels the existing submission and restarts the
2546 * countdown using the new delay. Note that this behavior is
2547 * inherently subject to race conditions with the pre-existing
2548 * timeouts and work queue, so care must be taken to synchronize such
2549 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002550 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002551 * @warning
2552 * A delayed work item must not be modified until it has been processed
2553 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002554 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002555 * @note Can be called by ISRs.
2556 *
2557 * @param work_q Address of workqueue.
2558 * @param work Address of delayed work item.
2559 * @param delay Delay before submitting the work item (in milliseconds).
2560 *
2561 * @retval 0 Work item countdown started.
2562 * @retval -EINPROGRESS Work item is already pending.
2563 * @retval -EINVAL Work item is being processed or has completed its work.
2564 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002565 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002566extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2567 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002568 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002569
2570/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002571 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002572 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002573 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002574 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002575 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002576 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002577 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002578 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002579 * @param work Address of delayed work item.
2580 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002581 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002582 * @retval -EINPROGRESS Work item is already pending.
2583 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002584 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002585extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002586
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002587/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002588 * @brief Submit a work item to the system workqueue.
2589 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002590 * This routine submits work item @a work to be processed by the system
2591 * workqueue. If the work item is already pending in the workqueue's queue
2592 * as a result of an earlier submission, this routine has no effect on the
2593 * work item. If the work item has already been processed, or is currently
2594 * being processed, its work is considered complete and the work item can be
2595 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002596 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002597 * @warning
2598 * Work items submitted to the system workqueue should avoid using handlers
2599 * that block or yield since this may prevent the system workqueue from
2600 * processing other work items in a timely manner.
2601 *
2602 * @note Can be called by ISRs.
2603 *
2604 * @param work Address of work item.
2605 *
2606 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002607 */
2608static inline void k_work_submit(struct k_work *work)
2609{
2610 k_work_submit_to_queue(&k_sys_work_q, work);
2611}
2612
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002613/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002614 * @brief Submit a delayed work item to the system workqueue.
2615 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002616 * This routine schedules work item @a work to be processed by the system
2617 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002618 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002619 * Only when the countdown completes is the work item actually submitted to
2620 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002621 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002622 * Submitting a previously submitted delayed work item that is still
2623 * counting down cancels the existing submission and restarts the countdown
2624 * using the new delay. If the work item is currently pending on the
2625 * workqueue's queue because the countdown has completed it is too late to
2626 * resubmit the item, and resubmission fails without impacting the work item.
2627 * If the work item has already been processed, or is currently being processed,
2628 * its work is considered complete and the work item can be resubmitted.
2629 *
2630 * @warning
2631 * Work items submitted to the system workqueue should avoid using handlers
2632 * that block or yield since this may prevent the system workqueue from
2633 * processing other work items in a timely manner.
2634 *
2635 * @note Can be called by ISRs.
2636 *
2637 * @param work Address of delayed work item.
2638 * @param delay Delay before submitting the work item (in milliseconds).
2639 *
2640 * @retval 0 Work item countdown started.
2641 * @retval -EINPROGRESS Work item is already pending.
2642 * @retval -EINVAL Work item is being processed or has completed its work.
2643 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002644 */
2645static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002646 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002647{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002648 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002649}
2650
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002651/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002652 * @brief Get time remaining before a delayed work gets scheduled.
2653 *
2654 * This routine computes the (approximate) time remaining before a
2655 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002656 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002657 *
2658 * @param work Delayed work item.
2659 *
2660 * @return Remaining time (in milliseconds).
2661 */
Kumar Galacc334c72017-04-21 10:55:34 -05002662static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002663{
2664 return _timeout_remaining_get(&work->timeout);
2665}
2666
Anas Nashif166f5192018-02-25 08:02:36 -06002667/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002668
Allan Stephensc98da842016-11-11 15:45:03 -05002669/**
2670 * @cond INTERNAL_HIDDEN
2671 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002672
2673struct k_mutex {
2674 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002675 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002676 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002677 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002678
Anas Nashif2f203c22016-12-18 06:57:45 -05002679 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002680};
2681
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002682#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002683 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002684 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002685 .owner = NULL, \
2686 .lock_count = 0, \
2687 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002688 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002689 }
2690
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002691#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2692
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002693/**
Allan Stephensc98da842016-11-11 15:45:03 -05002694 * INTERNAL_HIDDEN @endcond
2695 */
2696
2697/**
2698 * @defgroup mutex_apis Mutex APIs
2699 * @ingroup kernel_apis
2700 * @{
2701 */
2702
2703/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002704 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002706 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002707 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002708 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002709 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002710 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002711 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002712#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002713 struct k_mutex name \
2714 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002715 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002716
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002717/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002718 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002719 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002720 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002722 * Upon completion, the mutex is available and does not have an owner.
2723 *
2724 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002725 *
2726 * @return N/A
2727 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002728__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002729
2730/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002731 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002732 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002733 * This routine locks @a mutex. If the mutex is locked by another thread,
2734 * the calling thread waits until the mutex becomes available or until
2735 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002737 * A thread is permitted to lock a mutex it has already locked. The operation
2738 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002739 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002740 * @param mutex Address of the mutex.
2741 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002742 * or one of the special values K_NO_WAIT and K_FOREVER.
2743 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002744 * @retval 0 Mutex locked.
2745 * @retval -EBUSY Returned without waiting.
2746 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002747 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002748__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002749
2750/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002751 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002752 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002753 * This routine unlocks @a mutex. The mutex must already be locked by the
2754 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002755 *
2756 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002757 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002758 * thread.
2759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002760 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002761 *
2762 * @return N/A
2763 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002764__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002765
Allan Stephensc98da842016-11-11 15:45:03 -05002766/**
Anas Nashif166f5192018-02-25 08:02:36 -06002767 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002768 */
2769
2770/**
2771 * @cond INTERNAL_HIDDEN
2772 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002773
2774struct k_sem {
2775 _wait_q_t wait_q;
2776 unsigned int count;
2777 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002778 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002779
Anas Nashif2f203c22016-12-18 06:57:45 -05002780 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002781};
2782
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002783#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002784 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002785 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002786 .count = initial_count, \
2787 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002788 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002789 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002790 }
2791
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002792#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2793
Allan Stephensc98da842016-11-11 15:45:03 -05002794/**
2795 * INTERNAL_HIDDEN @endcond
2796 */
2797
2798/**
2799 * @defgroup semaphore_apis Semaphore APIs
2800 * @ingroup kernel_apis
2801 * @{
2802 */
2803
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002804/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002805 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002806 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002807 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002809 * @param sem Address of the semaphore.
2810 * @param initial_count Initial semaphore count.
2811 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002812 *
2813 * @return N/A
2814 */
Andrew Boie99280232017-09-29 14:17:47 -07002815__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2816 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002817
2818/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002819 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002820 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002821 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002823 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2824 *
2825 * @param sem Address of the semaphore.
2826 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002827 * or one of the special values K_NO_WAIT and K_FOREVER.
2828 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002829 * @note When porting code from the nanokernel legacy API to the new API, be
2830 * careful with the return value of this function. The return value is the
2831 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2832 * non-zero means failure, while the nano_sem_take family returns 1 for success
2833 * and 0 for failure.
2834 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002835 * @retval 0 Semaphore taken.
2836 * @retval -EBUSY Returned without waiting.
2837 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002838 */
Andrew Boie99280232017-09-29 14:17:47 -07002839__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002840
2841/**
2842 * @brief Give a semaphore.
2843 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002844 * This routine gives @a sem, unless the semaphore is already at its maximum
2845 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002847 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002849 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002850 *
2851 * @return N/A
2852 */
Andrew Boie99280232017-09-29 14:17:47 -07002853__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002854
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002855/**
2856 * @brief Reset a semaphore's count to zero.
2857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002858 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002860 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002861 *
2862 * @return N/A
2863 */
Andrew Boie990bf162017-10-03 12:36:49 -07002864__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002865
Anas Nashif954d5502018-02-25 08:37:28 -06002866/**
2867 * @internal
2868 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002869static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002870{
2871 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002872}
2873
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002874/**
2875 * @brief Get a semaphore's count.
2876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002877 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002878 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002879 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002881 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002882 */
Andrew Boie990bf162017-10-03 12:36:49 -07002883__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002884
Anas Nashif954d5502018-02-25 08:37:28 -06002885/**
2886 * @internal
2887 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002888static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002889{
2890 return sem->count;
2891}
2892
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002893/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002894 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002895 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002896 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002897 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002898 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002900 * @param name Name of the semaphore.
2901 * @param initial_count Initial semaphore count.
2902 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002903 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002904#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002905 struct k_sem name \
2906 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07002907 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05302908 BUILD_ASSERT(((count_limit) != 0) && \
2909 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002910
Anas Nashif166f5192018-02-25 08:02:36 -06002911/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002912
2913/**
2914 * @defgroup alert_apis Alert APIs
2915 * @ingroup kernel_apis
2916 * @{
2917 */
2918
Allan Stephens5eceb852016-11-16 10:16:30 -05002919/**
2920 * @typedef k_alert_handler_t
2921 * @brief Alert handler function type.
2922 *
2923 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002924 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002925 * and is only invoked if the alert has been initialized with one.
2926 *
2927 * @param alert Address of the alert.
2928 *
2929 * @return 0 if alert has been consumed; non-zero if alert should pend.
2930 */
2931typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002932
Anas Nashif166f5192018-02-25 08:02:36 -06002933/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002934
2935/**
2936 * @cond INTERNAL_HIDDEN
2937 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002938
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002939#define K_ALERT_DEFAULT NULL
2940#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002941
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002942struct k_alert {
2943 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002944 atomic_t send_count;
2945 struct k_work work_item;
2946 struct k_sem sem;
2947
Anas Nashif2f203c22016-12-18 06:57:45 -05002948 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002949};
2950
Anas Nashif954d5502018-02-25 08:37:28 -06002951/**
2952 * @internal
2953 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002954extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002955
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002956#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002957 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002958 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002959 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002960 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2961 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002962 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002963 }
2964
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002965#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2966
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002967/**
Allan Stephensc98da842016-11-11 15:45:03 -05002968 * INTERNAL_HIDDEN @endcond
2969 */
2970
2971/**
2972 * @addtogroup alert_apis
2973 * @{
2974 */
2975
2976/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002977 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002978 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002979 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002980 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002981 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002983 * @param name Name of the alert.
2984 * @param alert_handler Action to take when alert is sent. Specify either
2985 * the address of a function to be invoked by the system workqueue
2986 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2987 * K_ALERT_DEFAULT (which causes the alert to pend).
2988 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002989 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002990#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002991 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002992 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002993 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002994 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002995
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002996/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002997 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002999 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003001 * @param alert Address of the alert.
3002 * @param handler Action to take when alert is sent. Specify either the address
3003 * of a function to be invoked by the system workqueue thread,
3004 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3005 * K_ALERT_DEFAULT (which causes the alert to pend).
3006 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003007 *
3008 * @return N/A
3009 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003010extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3011 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003012
3013/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003014 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003016 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003017 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003018 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3019 *
3020 * @param alert Address of the alert.
3021 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003022 * or one of the special values K_NO_WAIT and K_FOREVER.
3023 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003024 * @retval 0 Alert received.
3025 * @retval -EBUSY Returned without waiting.
3026 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003027 */
Andrew Boie310e9872017-09-29 04:41:15 -07003028__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003029
3030/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003031 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003033 * This routine signals @a alert. The action specified for @a alert will
3034 * be taken, which may trigger the execution of an alert handler function
3035 * and/or cause the alert to pend (assuming the alert has not reached its
3036 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003038 * @note Can be called by ISRs.
3039 *
3040 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003041 *
3042 * @return N/A
3043 */
Andrew Boie310e9872017-09-29 04:41:15 -07003044__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003045
3046/**
Anas Nashif166f5192018-02-25 08:02:36 -06003047 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003048 */
3049
Allan Stephensc98da842016-11-11 15:45:03 -05003050/**
3051 * @cond INTERNAL_HIDDEN
3052 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003053
3054struct k_msgq {
3055 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003056 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003057 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003058 char *buffer_start;
3059 char *buffer_end;
3060 char *read_ptr;
3061 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003062 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003063
Anas Nashif2f203c22016-12-18 06:57:45 -05003064 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003065 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003066};
3067
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003068#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003069 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003070 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003071 .max_msgs = q_max_msgs, \
3072 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003073 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003074 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003075 .read_ptr = q_buffer, \
3076 .write_ptr = q_buffer, \
3077 .used_msgs = 0, \
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_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
3082
Andrew Boie0fe789f2018-04-12 18:35:56 -07003083#define K_MSGQ_FLAG_ALLOC BIT(0)
3084
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303085struct k_msgq_attrs {
3086 size_t msg_size;
3087 u32_t max_msgs;
3088 u32_t used_msgs;
3089};
3090
Peter Mitsis1da807e2016-10-06 11:36:59 -04003091/**
Allan Stephensc98da842016-11-11 15:45:03 -05003092 * INTERNAL_HIDDEN @endcond
3093 */
3094
3095/**
3096 * @defgroup msgq_apis Message Queue APIs
3097 * @ingroup kernel_apis
3098 * @{
3099 */
3100
3101/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003102 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003104 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3105 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003106 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3107 * message is similarly aligned to this boundary, @a q_msg_size must also be
3108 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003110 * The message queue can be accessed outside the module where it is defined
3111 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003112 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003113 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003114 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003115 * @param q_name Name of the message queue.
3116 * @param q_msg_size Message size (in bytes).
3117 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003118 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003119 */
3120#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003121 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003122 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003123 struct k_msgq q_name \
3124 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003125 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003126 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003127
Peter Mitsisd7a37502016-10-13 11:37:40 -04003128/**
3129 * @brief Initialize a message queue.
3130 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003131 * This routine initializes a message queue object, prior to its first use.
3132 *
Allan Stephensda827222016-11-09 14:23:58 -06003133 * The message queue's ring buffer must contain space for @a max_msgs messages,
3134 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3135 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3136 * that each message is similarly aligned to this boundary, @a q_msg_size
3137 * must also be a multiple of N.
3138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003139 * @param q Address of the message queue.
3140 * @param buffer Pointer to ring buffer that holds queued messages.
3141 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003142 * @param max_msgs Maximum number of messages that can be queued.
3143 *
3144 * @return N/A
3145 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003146void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3147 u32_t max_msgs);
3148
3149/**
3150 * @brief Initialize a message queue.
3151 *
3152 * This routine initializes a message queue object, prior to its first use,
3153 * allocating its internal ring buffer from the calling thread's resource
3154 * pool.
3155 *
3156 * Memory allocated for the ring buffer can be released by calling
3157 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3158 * all of its references.
3159 *
3160 * @param q Address of the message queue.
3161 * @param msg_size Message size (in bytes).
3162 * @param max_msgs Maximum number of messages that can be queued.
3163 *
3164 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3165 * thread's resource pool, or -EINVAL if the size parameters cause
3166 * an integer overflow.
3167 */
3168__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3169 u32_t max_msgs);
3170
3171
3172void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003173
3174/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003175 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003176 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003177 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003178 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003179 * @note Can be called by ISRs.
3180 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003181 * @param q Address of the message queue.
3182 * @param data Pointer to the message.
3183 * @param timeout Waiting period to add the message (in milliseconds),
3184 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003185 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003186 * @retval 0 Message sent.
3187 * @retval -ENOMSG Returned without waiting or queue purged.
3188 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003189 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003190__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003191
3192/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003193 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003194 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003195 * This routine receives a message from message queue @a q in a "first in,
3196 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003197 *
Allan Stephensc98da842016-11-11 15:45:03 -05003198 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003199 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003200 * @param q Address of the message queue.
3201 * @param data Address of area to hold the received message.
3202 * @param timeout Waiting period to receive the message (in milliseconds),
3203 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003204 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003205 * @retval 0 Message received.
3206 * @retval -ENOMSG Returned without waiting.
3207 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003208 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003209__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003210
3211/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003212 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003213 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003214 * This routine discards all unreceived messages in a message queue's ring
3215 * buffer. Any threads that are blocked waiting to send a message to the
3216 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003218 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003219 *
3220 * @return N/A
3221 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003222__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003223
Peter Mitsis67be2492016-10-07 11:44:34 -04003224/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003225 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003226 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003227 * This routine returns the number of unused entries in a message queue's
3228 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003229 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003230 * @param q Address of the message queue.
3231 *
3232 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04003233 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003234__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3235
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303236/**
3237 * @brief Get basic attributes of a message queue.
3238 *
3239 * This routine fetches basic attributes of message queue into attr argument.
3240 *
3241 * @param q Address of the message queue.
3242 * @param attrs pointer to message queue attribute structure.
3243 *
3244 * @return N/A
3245 */
3246__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3247
3248
Andrew Boie82edb6e2017-10-02 10:53:06 -07003249static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003250{
3251 return q->max_msgs - q->used_msgs;
3252}
3253
Peter Mitsisd7a37502016-10-13 11:37:40 -04003254/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003255 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003256 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003257 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003258 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003259 * @param q Address of the message queue.
3260 *
3261 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003262 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003263__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3264
3265static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003266{
3267 return q->used_msgs;
3268}
3269
Anas Nashif166f5192018-02-25 08:02:36 -06003270/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003271
3272/**
3273 * @defgroup mem_pool_apis Memory Pool APIs
3274 * @ingroup kernel_apis
3275 * @{
3276 */
3277
Andy Ross73cb9582017-05-09 10:42:39 -07003278/* Note on sizing: the use of a 20 bit field for block means that,
3279 * assuming a reasonable minimum block size of 16 bytes, we're limited
3280 * to 16M of memory managed by a single pool. Long term it would be
3281 * good to move to a variable bit size based on configuration.
3282 */
3283struct k_mem_block_id {
3284 u32_t pool : 8;
3285 u32_t level : 4;
3286 u32_t block : 20;
3287};
3288
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003289struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003290 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003291 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003292};
3293
Anas Nashif166f5192018-02-25 08:02:36 -06003294/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003295
3296/**
3297 * @defgroup mailbox_apis Mailbox APIs
3298 * @ingroup kernel_apis
3299 * @{
3300 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003301
3302struct k_mbox_msg {
3303 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003304 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003305 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003306 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003307 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003308 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003309 /** sender's message data buffer */
3310 void *tx_data;
3311 /** internal use only - needed for legacy API support */
3312 void *_rx_data;
3313 /** message data block descriptor */
3314 struct k_mem_block tx_block;
3315 /** source thread id */
3316 k_tid_t rx_source_thread;
3317 /** target thread id */
3318 k_tid_t tx_target_thread;
3319 /** internal use only - thread waiting on send (may be a dummy) */
3320 k_tid_t _syncing_thread;
3321#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3322 /** internal use only - semaphore used during asynchronous send */
3323 struct k_sem *_async_sem;
3324#endif
3325};
3326
Allan Stephensc98da842016-11-11 15:45:03 -05003327/**
3328 * @cond INTERNAL_HIDDEN
3329 */
3330
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003331struct k_mbox {
3332 _wait_q_t tx_msg_queue;
3333 _wait_q_t rx_msg_queue;
3334
Anas Nashif2f203c22016-12-18 06:57:45 -05003335 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003336};
3337
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003338#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003339 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003340 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3341 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003342 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003343 }
3344
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003345#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3346
Peter Mitsis12092702016-10-14 12:57:23 -04003347/**
Allan Stephensc98da842016-11-11 15:45:03 -05003348 * INTERNAL_HIDDEN @endcond
3349 */
3350
3351/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003352 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003353 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003354 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003355 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003356 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003357 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003358 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003359 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003360#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003361 struct k_mbox name \
3362 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003363 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003364
Peter Mitsis12092702016-10-14 12:57:23 -04003365/**
3366 * @brief Initialize a mailbox.
3367 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003368 * This routine initializes a mailbox object, prior to its first use.
3369 *
3370 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003371 *
3372 * @return N/A
3373 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003374extern void k_mbox_init(struct k_mbox *mbox);
3375
Peter Mitsis12092702016-10-14 12:57:23 -04003376/**
3377 * @brief Send a mailbox message in a synchronous manner.
3378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003379 * This routine sends a message to @a mbox and waits for a receiver to both
3380 * receive and process it. The message data may be in a buffer, in a memory
3381 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003382 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003383 * @param mbox Address of the mailbox.
3384 * @param tx_msg Address of the transmit message descriptor.
3385 * @param timeout Waiting period for the message to be received (in
3386 * milliseconds), or one of the special values K_NO_WAIT
3387 * and K_FOREVER. Once the message has been received,
3388 * this routine waits as long as necessary for the message
3389 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003390 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003391 * @retval 0 Message sent.
3392 * @retval -ENOMSG Returned without waiting.
3393 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003394 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003395extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003396 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003397
Peter Mitsis12092702016-10-14 12:57:23 -04003398/**
3399 * @brief Send a mailbox message in an asynchronous manner.
3400 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003401 * This routine sends a message to @a mbox without waiting for a receiver
3402 * to process it. The message data may be in a buffer, in a memory pool block,
3403 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3404 * will be given when the message has been both received and completely
3405 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003407 * @param mbox Address of the mailbox.
3408 * @param tx_msg Address of the transmit message descriptor.
3409 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003410 *
3411 * @return N/A
3412 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003413extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003414 struct k_sem *sem);
3415
Peter Mitsis12092702016-10-14 12:57:23 -04003416/**
3417 * @brief Receive a mailbox message.
3418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003419 * This routine receives a message from @a mbox, then optionally retrieves
3420 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003421 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003422 * @param mbox Address of the mailbox.
3423 * @param rx_msg Address of the receive message descriptor.
3424 * @param buffer Address of the buffer to receive data, or NULL to defer data
3425 * retrieval and message disposal until later.
3426 * @param timeout Waiting period for a message to be received (in
3427 * milliseconds), or one of the special values K_NO_WAIT
3428 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003429 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003430 * @retval 0 Message received.
3431 * @retval -ENOMSG Returned without waiting.
3432 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003433 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003434extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003435 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003436
3437/**
3438 * @brief Retrieve mailbox message data into a buffer.
3439 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003440 * This routine completes the processing of a received message by retrieving
3441 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003442 *
3443 * Alternatively, this routine can be used to dispose of a received message
3444 * without retrieving its data.
3445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003446 * @param rx_msg Address of the receive message descriptor.
3447 * @param buffer Address of the buffer to receive data, or NULL to discard
3448 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003449 *
3450 * @return N/A
3451 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003452extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003453
3454/**
3455 * @brief Retrieve mailbox message data into a memory pool block.
3456 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003457 * This routine completes the processing of a received message by retrieving
3458 * its data into a memory pool block, then disposing of the message.
3459 * The memory pool block that results from successful retrieval must be
3460 * returned to the pool once the data has been processed, even in cases
3461 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003462 *
3463 * Alternatively, this routine can be used to dispose of a received message
3464 * without retrieving its data. In this case there is no need to return a
3465 * memory pool block to the pool.
3466 *
3467 * This routine allocates a new memory pool block for the data only if the
3468 * data is not already in one. If a new block cannot be allocated, the routine
3469 * returns a failure code and the received message is left unchanged. This
3470 * permits the caller to reattempt data retrieval at a later time or to dispose
3471 * of the received message without retrieving its data.
3472 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003473 * @param rx_msg Address of a receive message descriptor.
3474 * @param pool Address of memory pool, or NULL to discard data.
3475 * @param block Address of the area to hold memory pool block info.
3476 * @param timeout Waiting period to wait for a memory pool block (in
3477 * milliseconds), or one of the special values K_NO_WAIT
3478 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003479 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003480 * @retval 0 Data retrieved.
3481 * @retval -ENOMEM Returned without waiting.
3482 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003483 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003484extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003485 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003486 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003487
Anas Nashif166f5192018-02-25 08:02:36 -06003488/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003489
3490/**
3491 * @cond INTERNAL_HIDDEN
3492 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003493
Andrew Boie44fe8122018-04-12 17:38:12 -07003494#define K_PIPE_FLAG_ALLOC BIT(0) /* Buffer was allocated */
3495
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003496struct k_pipe {
3497 unsigned char *buffer; /* Pipe buffer: may be NULL */
3498 size_t size; /* Buffer size */
3499 size_t bytes_used; /* # bytes used in buffer */
3500 size_t read_index; /* Where in buffer to read from */
3501 size_t write_index; /* Where in buffer to write */
3502
3503 struct {
3504 _wait_q_t readers; /* Reader wait queue */
3505 _wait_q_t writers; /* Writer wait queue */
3506 } wait_q;
3507
Anas Nashif2f203c22016-12-18 06:57:45 -05003508 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Andrew Boie44fe8122018-04-12 17:38:12 -07003509 u8_t flags; /* Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003510};
3511
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003512#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003513 { \
3514 .buffer = pipe_buffer, \
3515 .size = pipe_buffer_size, \
3516 .bytes_used = 0, \
3517 .read_index = 0, \
3518 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003519 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3520 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003521 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003522 }
3523
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003524#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3525
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003526/**
Allan Stephensc98da842016-11-11 15:45:03 -05003527 * INTERNAL_HIDDEN @endcond
3528 */
3529
3530/**
3531 * @defgroup pipe_apis Pipe APIs
3532 * @ingroup kernel_apis
3533 * @{
3534 */
3535
3536/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003537 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003539 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003540 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003541 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003542 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003543 * @param name Name of the pipe.
3544 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3545 * or zero if no ring buffer is used.
3546 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003547 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003548#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3549 static unsigned char __kernel_noinit __aligned(pipe_align) \
3550 _k_pipe_buf_##name[pipe_buffer_size]; \
3551 struct k_pipe name \
3552 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003553 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003554
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003555/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003556 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003557 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003558 * This routine initializes a pipe object, prior to its first use.
3559 *
3560 * @param pipe Address of the pipe.
3561 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3562 * is used.
3563 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3564 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003565 *
3566 * @return N/A
3567 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003568void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3569
3570/**
3571 * @brief Release a pipe's allocated buffer
3572 *
3573 * If a pipe object was given a dynamically allocated buffer via
3574 * k_pipe_alloc_init(), this will free it. This function does nothing
3575 * if the buffer wasn't dynamically allocated.
3576 *
3577 * @param pipe Address of the pipe.
3578 */
3579void k_pipe_cleanup(struct k_pipe *pipe);
3580
3581/**
3582 * @brief Initialize a pipe and allocate a buffer for it
3583 *
3584 * Storage for the buffer region will be allocated from the calling thread's
3585 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3586 * or userspace is enabled and the pipe object loses all references to it.
3587 *
3588 * This function should only be called on uninitialized pipe objects.
3589 *
3590 * @param pipe Address of the pipe.
3591 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3592 * buffer is used.
3593 * @retval 0 on success
3594 * @retval -ENOMEM if memory couln't be allocated
3595 */
3596__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003597
3598/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003599 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003600 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003601 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003603 * @param pipe Address of the pipe.
3604 * @param data Address of data to write.
3605 * @param bytes_to_write Size of data (in bytes).
3606 * @param bytes_written Address of area to hold the number of bytes written.
3607 * @param min_xfer Minimum number of bytes to write.
3608 * @param timeout Waiting period to wait for the data to be written (in
3609 * milliseconds), or one of the special values K_NO_WAIT
3610 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003611 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003612 * @retval 0 At least @a min_xfer bytes of data were written.
3613 * @retval -EIO Returned without waiting; zero data bytes were written.
3614 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003615 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003616 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003617__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3618 size_t bytes_to_write, size_t *bytes_written,
3619 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003620
3621/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003622 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003624 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003626 * @param pipe Address of the pipe.
3627 * @param data Address to place the data read from pipe.
3628 * @param bytes_to_read Maximum number of data bytes to read.
3629 * @param bytes_read Address of area to hold the number of bytes read.
3630 * @param min_xfer Minimum number of data bytes to read.
3631 * @param timeout Waiting period to wait for the data to be read (in
3632 * milliseconds), or one of the special values K_NO_WAIT
3633 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003634 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003635 * @retval 0 At least @a min_xfer bytes of data were read.
3636 * @retval -EIO Returned without waiting; zero data bytes were read.
3637 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003638 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003639 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003640__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3641 size_t bytes_to_read, size_t *bytes_read,
3642 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003643
3644/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003645 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003647 * This routine writes the data contained in a memory block to @a pipe.
3648 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003649 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003651 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003652 * @param block Memory block containing data to send
3653 * @param size Number of data bytes in memory block to send
3654 * @param sem Semaphore to signal upon completion (else NULL)
3655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003656 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003657 */
3658extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3659 size_t size, struct k_sem *sem);
3660
Anas Nashif166f5192018-02-25 08:02:36 -06003661/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003662
Allan Stephensc98da842016-11-11 15:45:03 -05003663/**
3664 * @cond INTERNAL_HIDDEN
3665 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003666
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003667struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003668 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003669 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003670 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003671 char *buffer;
3672 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003673 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003674
Anas Nashif2f203c22016-12-18 06:57:45 -05003675 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003676};
3677
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003678#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003679 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003680 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003681 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003682 .num_blocks = slab_num_blocks, \
3683 .block_size = slab_block_size, \
3684 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003685 .free_list = NULL, \
3686 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003687 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003688 }
3689
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003690#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3691
3692
Peter Mitsis578f9112016-10-07 13:50:31 -04003693/**
Allan Stephensc98da842016-11-11 15:45:03 -05003694 * INTERNAL_HIDDEN @endcond
3695 */
3696
3697/**
3698 * @defgroup mem_slab_apis Memory Slab APIs
3699 * @ingroup kernel_apis
3700 * @{
3701 */
3702
3703/**
Allan Stephensda827222016-11-09 14:23:58 -06003704 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003705 *
Allan Stephensda827222016-11-09 14:23:58 -06003706 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003707 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003708 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3709 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003710 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003711 *
Allan Stephensda827222016-11-09 14:23:58 -06003712 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003713 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003714 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003715 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003716 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003717 * @param name Name of the memory slab.
3718 * @param slab_block_size Size of each memory block (in bytes).
3719 * @param slab_num_blocks Number memory blocks.
3720 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003721 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003722#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3723 char __noinit __aligned(slab_align) \
3724 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3725 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003726 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003727 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003728 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003729
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003730/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003731 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003732 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003733 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003734 *
Allan Stephensda827222016-11-09 14:23:58 -06003735 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3736 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3737 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3738 * To ensure that each memory block is similarly aligned to this boundary,
3739 * @a slab_block_size must also be a multiple of N.
3740 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003741 * @param slab Address of the memory slab.
3742 * @param buffer Pointer to buffer used for the memory blocks.
3743 * @param block_size Size of each memory block (in bytes).
3744 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003745 *
3746 * @return N/A
3747 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003748extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003749 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003750
3751/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003752 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003753 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003754 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003755 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003756 * @param slab Address of the memory slab.
3757 * @param mem Pointer to block address area.
3758 * @param timeout Maximum time to wait for operation to complete
3759 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3760 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003761 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003762 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003763 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003764 * @retval -ENOMEM Returned without waiting.
3765 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003766 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003767extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003768 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003769
3770/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003771 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003772 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003773 * This routine releases a previously allocated memory block back to its
3774 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003775 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003776 * @param slab Address of the memory slab.
3777 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003778 *
3779 * @return N/A
3780 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003781extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003782
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003783/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003784 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003785 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003786 * This routine gets the number of memory blocks that are currently
3787 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003788 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003789 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003791 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003792 */
Kumar Galacc334c72017-04-21 10:55:34 -05003793static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003794{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003795 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003796}
3797
Peter Mitsisc001aa82016-10-13 13:53:37 -04003798/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003799 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003801 * This routine gets the number of memory blocks that are currently
3802 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003804 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003805 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003806 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003807 */
Kumar Galacc334c72017-04-21 10:55:34 -05003808static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003809{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003810 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003811}
3812
Anas Nashif166f5192018-02-25 08:02:36 -06003813/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003814
3815/**
3816 * @cond INTERNAL_HIDDEN
3817 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003818
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003819struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003820 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003821 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003822};
3823
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003824/**
Allan Stephensc98da842016-11-11 15:45:03 -05003825 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003826 */
3827
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003828/**
Allan Stephensc98da842016-11-11 15:45:03 -05003829 * @addtogroup mem_pool_apis
3830 * @{
3831 */
3832
3833/**
3834 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003835 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003836 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3837 * long. The memory pool allows blocks to be repeatedly partitioned into
3838 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003839 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003840 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003841 * If the pool is to be accessed outside the module where it is defined, it
3842 * can be declared via
3843 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003844 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003846 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003847 * @param minsz Size of the smallest blocks in the pool (in bytes).
3848 * @param maxsz Size of the largest blocks in the pool (in bytes).
3849 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003850 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003851 */
Andy Ross73cb9582017-05-09 10:42:39 -07003852#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3853 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3854 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003855 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003856 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003857 .base = { \
3858 .buf = _mpool_buf_##name, \
3859 .max_sz = maxsz, \
3860 .n_max = nmax, \
3861 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3862 .levels = _mpool_lvls_##name, \
3863 .flags = SYS_MEM_POOL_KERNEL \
3864 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003865 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003866
Peter Mitsis937042c2016-10-13 13:18:26 -04003867/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003868 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003870 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003872 * @param pool Address of the memory pool.
3873 * @param block Pointer to block descriptor for the allocated memory.
3874 * @param size Amount of memory to allocate (in bytes).
3875 * @param timeout Maximum time to wait for operation to complete
3876 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3877 * or K_FOREVER to wait as long as necessary.
3878 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003879 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003880 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003881 * @retval -ENOMEM Returned without waiting.
3882 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003883 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003884extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003885 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003886
3887/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07003888 * @brief Allocate memory from a memory pool with malloc() semantics
3889 *
3890 * Such memory must be released using k_free().
3891 *
3892 * @param pool Address of the memory pool.
3893 * @param size Amount of memory to allocate (in bytes).
3894 * @return Address of the allocated memory if successful, otherwise NULL
3895 */
3896extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
3897
3898/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003899 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003900 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003901 * This routine releases a previously allocated memory block back to its
3902 * memory pool.
3903 *
3904 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003905 *
3906 * @return N/A
3907 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003908extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003909
3910/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003911 * @brief Free memory allocated from a memory pool.
3912 *
3913 * This routine releases a previously allocated memory block back to its
3914 * memory pool
3915 *
3916 * @param id Memory block identifier.
3917 *
3918 * @return N/A
3919 */
3920extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3921
3922/**
Anas Nashif166f5192018-02-25 08:02:36 -06003923 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003924 */
3925
3926/**
3927 * @defgroup heap_apis Heap Memory Pool APIs
3928 * @ingroup kernel_apis
3929 * @{
3930 */
3931
3932/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003933 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003935 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003936 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003937 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003938 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003939 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003940 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003941 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003942extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003943
3944/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003945 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003946 *
3947 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07003948 * returned must have been allocated from the heap memory pool or
3949 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04003950 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003951 * If @a ptr is NULL, no operation is performed.
3952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003953 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003954 *
3955 * @return N/A
3956 */
3957extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003958
Allan Stephensc98da842016-11-11 15:45:03 -05003959/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003960 * @brief Allocate memory from heap, array style
3961 *
3962 * This routine provides traditional calloc() semantics. Memory is
3963 * allocated from the heap memory pool and zeroed.
3964 *
3965 * @param nmemb Number of elements in the requested array
3966 * @param size Size of each array element (in bytes).
3967 *
3968 * @return Address of the allocated memory if successful; otherwise NULL.
3969 */
3970extern void *k_calloc(size_t nmemb, size_t size);
3971
Anas Nashif166f5192018-02-25 08:02:36 -06003972/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003973
Benjamin Walshacc68c12017-01-29 18:57:45 -05003974/* polling API - PRIVATE */
3975
Benjamin Walshb0179862017-02-02 16:39:57 -05003976#ifdef CONFIG_POLL
3977#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3978#else
3979#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3980#endif
3981
Benjamin Walshacc68c12017-01-29 18:57:45 -05003982/* private - implementation data created as needed, per-type */
3983struct _poller {
3984 struct k_thread *thread;
3985};
3986
3987/* private - types bit positions */
3988enum _poll_types_bits {
3989 /* can be used to ignore an event */
3990 _POLL_TYPE_IGNORE,
3991
3992 /* to be signaled by k_poll_signal() */
3993 _POLL_TYPE_SIGNAL,
3994
3995 /* semaphore availability */
3996 _POLL_TYPE_SEM_AVAILABLE,
3997
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003998 /* queue/fifo/lifo data availability */
3999 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004000
4001 _POLL_NUM_TYPES
4002};
4003
4004#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4005
4006/* private - states bit positions */
4007enum _poll_states_bits {
4008 /* default state when creating event */
4009 _POLL_STATE_NOT_READY,
4010
Benjamin Walshacc68c12017-01-29 18:57:45 -05004011 /* signaled by k_poll_signal() */
4012 _POLL_STATE_SIGNALED,
4013
4014 /* semaphore is available */
4015 _POLL_STATE_SEM_AVAILABLE,
4016
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004017 /* data is available to read on queue/fifo/lifo */
4018 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004019
4020 _POLL_NUM_STATES
4021};
4022
4023#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4024
4025#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004026 (32 - (0 \
4027 + 8 /* tag */ \
4028 + _POLL_NUM_TYPES \
4029 + _POLL_NUM_STATES \
4030 + 1 /* modes */ \
4031 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004032
Benjamin Walshacc68c12017-01-29 18:57:45 -05004033/* end of polling API - PRIVATE */
4034
4035
4036/**
4037 * @defgroup poll_apis Async polling APIs
4038 * @ingroup kernel_apis
4039 * @{
4040 */
4041
4042/* Public polling API */
4043
4044/* public - values for k_poll_event.type bitfield */
4045#define K_POLL_TYPE_IGNORE 0
4046#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4047#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004048#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4049#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004050
4051/* public - polling modes */
4052enum k_poll_modes {
4053 /* polling thread does not take ownership of objects when available */
4054 K_POLL_MODE_NOTIFY_ONLY = 0,
4055
4056 K_POLL_NUM_MODES
4057};
4058
4059/* public - values for k_poll_event.state bitfield */
4060#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004061#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4062#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004063#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4064#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004065
4066/* public - poll signal object */
4067struct k_poll_signal {
4068 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004069 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004070
4071 /*
4072 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4073 * user resets it to 0.
4074 */
4075 unsigned int signaled;
4076
4077 /* custom result value passed to k_poll_signal() if needed */
4078 int result;
4079};
4080
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004081#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004082 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004083 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004084 .signaled = 0, \
4085 .result = 0, \
4086 }
4087
4088struct k_poll_event {
4089 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004090 sys_dnode_t _node;
4091
4092 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004093 struct _poller *poller;
4094
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004095 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004096 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004097
Benjamin Walshacc68c12017-01-29 18:57:45 -05004098 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004099 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004100
4101 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004102 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004103
4104 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004105 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004106
4107 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004108 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004109
4110 /* per-type data */
4111 union {
4112 void *obj;
4113 struct k_poll_signal *signal;
4114 struct k_sem *sem;
4115 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004116 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004117 };
4118};
4119
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004120#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004121 { \
4122 .poller = NULL, \
4123 .type = event_type, \
4124 .state = K_POLL_STATE_NOT_READY, \
4125 .mode = event_mode, \
4126 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004127 { .obj = event_obj }, \
4128 }
4129
4130#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4131 event_tag) \
4132 { \
4133 .type = event_type, \
4134 .tag = event_tag, \
4135 .state = K_POLL_STATE_NOT_READY, \
4136 .mode = event_mode, \
4137 .unused = 0, \
4138 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004139 }
4140
4141/**
4142 * @brief Initialize one struct k_poll_event instance
4143 *
4144 * After this routine is called on a poll event, the event it ready to be
4145 * placed in an event array to be passed to k_poll().
4146 *
4147 * @param event The event to initialize.
4148 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4149 * values. Only values that apply to the same object being polled
4150 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4151 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004152 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004153 * @param obj Kernel object or poll signal.
4154 *
4155 * @return N/A
4156 */
4157
Kumar Galacc334c72017-04-21 10:55:34 -05004158extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004159 int mode, void *obj);
4160
4161/**
4162 * @brief Wait for one or many of multiple poll events to occur
4163 *
4164 * This routine allows a thread to wait concurrently for one or many of
4165 * multiple poll events to have occurred. Such events can be a kernel object
4166 * being available, like a semaphore, or a poll signal event.
4167 *
4168 * When an event notifies that a kernel object is available, the kernel object
4169 * is not "given" to the thread calling k_poll(): it merely signals the fact
4170 * that the object was available when the k_poll() call was in effect. Also,
4171 * all threads trying to acquire an object the regular way, i.e. by pending on
4172 * the object, have precedence over the thread polling on the object. This
4173 * means that the polling thread will never get the poll event on an object
4174 * until the object becomes available and its pend queue is empty. For this
4175 * reason, the k_poll() call is more effective when the objects being polled
4176 * only have one thread, the polling thread, trying to acquire them.
4177 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004178 * When k_poll() returns 0, the caller should loop on all the events that were
4179 * passed to k_poll() and check the state field for the values that were
4180 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004181 *
4182 * Before being reused for another call to k_poll(), the user has to reset the
4183 * state field to K_POLL_STATE_NOT_READY.
4184 *
Andrew Boie3772f772018-05-07 16:52:57 -07004185 * When called from user mode, a temporary memory allocation is required from
4186 * the caller's resource pool.
4187 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004188 * @param events An array of pointers to events to be polled for.
4189 * @param num_events The number of events in the array.
4190 * @param timeout Waiting period for an event to be ready (in milliseconds),
4191 * or one of the special values K_NO_WAIT and K_FOREVER.
4192 *
4193 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004194 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02004195 * @retval -EINTR Poller thread has been interrupted.
Andrew Boie3772f772018-05-07 16:52:57 -07004196 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4197 * @retval -EINVAL Bad parameters (user mode only)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004198 */
4199
Andrew Boie3772f772018-05-07 16:52:57 -07004200__syscall int k_poll(struct k_poll_event *events, int num_events,
4201 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004202
4203/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004204 * @brief Initialize a poll signal object.
4205 *
4206 * Ready a poll signal object to be signaled via k_poll_signal().
4207 *
4208 * @param signal A poll signal.
4209 *
4210 * @return N/A
4211 */
4212
Andrew Boie3772f772018-05-07 16:52:57 -07004213__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4214
4215/*
4216 * @brief Reset a poll signal object's state to unsignaled.
4217 *
4218 * @param signal A poll signal object
4219 */
4220__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4221
4222static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4223{
4224 signal->signaled = 0;
4225}
4226
4227/**
4228 * @brief Fetch the signaled state and resylt value of a poll signal
4229 *
4230 * @param signal A poll signal object
4231 * @param signaled An integer buffer which will be written nonzero if the
4232 * object was signaled
4233 * @param result An integer destination buffer which will be written with the
4234 * result value if the object was signaed, or an undefined
4235 * value if it was not.
4236 */
4237__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4238 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004239
4240/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004241 * @brief Signal a poll signal object.
4242 *
4243 * This routine makes ready a poll signal, which is basically a poll event of
4244 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4245 * made ready to run. A @a result value can be specified.
4246 *
4247 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004248 * k_poll_signal(), stays set until the user sets it back to 0 with
4249 * k_poll_signal_reset(). It thus has to be reset by the user before being
4250 * passed again to k_poll() or k_poll() will consider it being signaled, and
4251 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004252 *
4253 * @param signal A poll signal.
4254 * @param result The value to store in the result field of the signal.
4255 *
4256 * @retval 0 The signal was delivered successfully.
4257 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
4258 */
4259
Andrew Boie3772f772018-05-07 16:52:57 -07004260__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004261
Anas Nashif954d5502018-02-25 08:37:28 -06004262/**
4263 * @internal
4264 */
Andy Ross8606fab2018-03-26 10:54:40 -07004265extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004266
Anas Nashif166f5192018-02-25 08:02:36 -06004267/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004268
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004269/**
4270 * @brief Make the CPU idle.
4271 *
4272 * This function makes the CPU idle until an event wakes it up.
4273 *
4274 * In a regular system, the idle thread should be the only thread responsible
4275 * for making the CPU idle and triggering any type of power management.
4276 * However, in some more constrained systems, such as a single-threaded system,
4277 * the only thread would be responsible for this if needed.
4278 *
4279 * @return N/A
4280 */
4281extern void k_cpu_idle(void);
4282
4283/**
4284 * @brief Make the CPU idle in an atomic fashion.
4285 *
4286 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4287 * must be done atomically before making the CPU idle.
4288 *
4289 * @param key Interrupt locking key obtained from irq_lock().
4290 *
4291 * @return N/A
4292 */
4293extern void k_cpu_atomic_idle(unsigned int key);
4294
Anas Nashif954d5502018-02-25 08:37:28 -06004295
4296/**
4297 * @internal
4298 */
Kumar Galacc334c72017-04-21 10:55:34 -05004299extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004300
Andrew Boiecdb94d62017-04-18 15:22:05 -07004301#ifdef _ARCH_EXCEPT
4302/* This archtecture has direct support for triggering a CPU exception */
4303#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4304#else
4305
Andrew Boiecdb94d62017-04-18 15:22:05 -07004306/* NOTE: This is the implementation for arches that do not implement
4307 * _ARCH_EXCEPT() to generate a real CPU exception.
4308 *
4309 * We won't have a real exception frame to determine the PC value when
4310 * the oops occurred, so print file and line number before we jump into
4311 * the fatal error handler.
4312 */
4313#define _k_except_reason(reason) do { \
4314 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4315 _NanoFatalErrorHandler(reason, &_default_esf); \
4316 CODE_UNREACHABLE; \
4317 } while (0)
4318
4319#endif /* _ARCH__EXCEPT */
4320
4321/**
4322 * @brief Fatally terminate a thread
4323 *
4324 * This should be called when a thread has encountered an unrecoverable
4325 * runtime condition and needs to terminate. What this ultimately
4326 * means is determined by the _fatal_error_handler() implementation, which
4327 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4328 *
4329 * If this is called from ISR context, the default system fatal error handler
4330 * will treat it as an unrecoverable system error, just like k_panic().
4331 */
4332#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4333
4334/**
4335 * @brief Fatally terminate the system
4336 *
4337 * This should be called when the Zephyr kernel has encountered an
4338 * unrecoverable runtime condition and needs to terminate. What this ultimately
4339 * means is determined by the _fatal_error_handler() implementation, which
4340 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4341 */
4342#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4343
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004344/*
4345 * private APIs that are utilized by one or more public APIs
4346 */
4347
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004348#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004349/**
4350 * @internal
4351 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004352extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004353#else
Anas Nashif954d5502018-02-25 08:37:28 -06004354/**
4355 * @internal
4356 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004357#define _init_static_threads() do { } while ((0))
4358#endif
4359
Anas Nashif954d5502018-02-25 08:37:28 -06004360/**
4361 * @internal
4362 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004363extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004364/**
4365 * @internal
4366 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004367extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004368
Andrew Boiedc5d9352017-06-02 12:56:47 -07004369/* arch/cpu.h may declare an architecture or platform-specific macro
4370 * for properly declaring stacks, compatible with MMU/MPU constraints if
4371 * enabled
4372 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004373
4374/**
4375 * @brief Obtain an extern reference to a stack
4376 *
4377 * This macro properly brings the symbol of a thread stack declared
4378 * elsewhere into scope.
4379 *
4380 * @param sym Thread stack symbol name
4381 */
4382#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4383
Andrew Boiedc5d9352017-06-02 12:56:47 -07004384#ifdef _ARCH_THREAD_STACK_DEFINE
4385#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4386#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4387 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4388#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4389#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004390static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004391{
4392 return _ARCH_THREAD_STACK_BUFFER(sym);
4393}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004394#else
4395/**
4396 * @brief Declare a toplevel thread stack memory region
4397 *
4398 * This declares a region of memory suitable for use as a thread's stack.
4399 *
4400 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4401 * 'noinit' section so that it isn't zeroed at boot
4402 *
Andrew Boie507852a2017-07-25 18:47:07 -07004403 * The declared symbol will always be a k_thread_stack_t which can be passed to
4404 * k_thread_create, but should otherwise not be manipulated. If the buffer
4405 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004406 *
4407 * It is legal to precede this definition with the 'static' keyword.
4408 *
4409 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4410 * parameter of k_thread_create(), it may not be the same as the
4411 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4412 *
4413 * @param sym Thread stack symbol name
4414 * @param size Size of the stack memory region
4415 */
4416#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004417 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004418
4419/**
4420 * @brief Declare a toplevel array of thread stack memory regions
4421 *
4422 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4423 * definition for additional details and constraints.
4424 *
4425 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4426 * 'noinit' section so that it isn't zeroed at boot
4427 *
4428 * @param sym Thread stack symbol name
4429 * @param nmemb Number of stacks to declare
4430 * @param size Size of the stack memory region
4431 */
4432
4433#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004434 struct _k_thread_stack_element __noinit \
4435 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004436
4437/**
4438 * @brief Declare an embedded stack memory region
4439 *
4440 * Used for stacks embedded within other data structures. Use is highly
4441 * discouraged but in some cases necessary. For memory protection scenarios,
4442 * it is very important that any RAM preceding this member not be writable
4443 * by threads else a stack overflow will lead to silent corruption. In other
4444 * words, the containing data structure should live in RAM owned by the kernel.
4445 *
4446 * @param sym Thread stack symbol name
4447 * @param size Size of the stack memory region
4448 */
4449#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004450 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004451
4452/**
4453 * @brief Return the size in bytes of a stack memory region
4454 *
4455 * Convenience macro for passing the desired stack size to k_thread_create()
4456 * since the underlying implementation may actually create something larger
4457 * (for instance a guard area).
4458 *
4459 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004460 * passed to K_THREAD_STACK_DEFINE.
4461 *
4462 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4463 * it is not guaranteed to return the original value since each array
4464 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004465 *
4466 * @param sym Stack memory symbol
4467 * @return Size of the stack
4468 */
4469#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4470
4471/**
4472 * @brief Get a pointer to the physical stack buffer
4473 *
4474 * Convenience macro to get at the real underlying stack buffer used by
4475 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4476 * This is really only intended for diagnostic tools which want to examine
4477 * stack memory contents.
4478 *
4479 * @param sym Declared stack symbol name
4480 * @return The buffer itself, a char *
4481 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004482static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004483{
4484 return (char *)sym;
4485}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004486
4487#endif /* _ARCH_DECLARE_STACK */
4488
Chunlin Hane9c97022017-07-07 20:29:30 +08004489/**
4490 * @defgroup mem_domain_apis Memory domain APIs
4491 * @ingroup kernel_apis
4492 * @{
4493 */
4494
4495/** @def MEM_PARTITION_ENTRY
4496 * @brief Used to declare a memory partition entry
4497 */
4498#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4499 {\
4500 .start = _start, \
4501 .size = _size, \
4502 .attr = _attr, \
4503 }
4504
4505/** @def K_MEM_PARTITION_DEFINE
4506 * @brief Used to declare a memory partition
4507 */
4508#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4509#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4510 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304511 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004512 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4513#else
4514#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304515 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004516 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4517#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4518
Chunlin Hane9c97022017-07-07 20:29:30 +08004519/* memory partition */
4520struct k_mem_partition {
4521 /* start address of memory partition */
4522 u32_t start;
4523 /* size of memory partition */
4524 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304525#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004526 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304527 k_mem_partition_attr_t attr;
4528#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004529};
4530
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304531/* memory domian
4532 * Note: Always declare this structure with __kernel prefix
4533 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004534struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304535#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004536 /* partitions in the domain */
4537 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304538#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004539 /* domain q */
4540 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004541 /* number of partitions in the domain */
4542 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004543};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304544
Chunlin Hane9c97022017-07-07 20:29:30 +08004545
4546/**
4547 * @brief Initialize a memory domain.
4548 *
4549 * Initialize a memory domain with given name and memory partitions.
4550 *
4551 * @param domain The memory domain to be initialized.
4552 * @param num_parts The number of array items of "parts" parameter.
4553 * @param parts An array of pointers to the memory partitions. Can be NULL
4554 * if num_parts is zero.
4555 */
4556
Leandro Pereira08de6582018-02-28 14:22:57 -08004557extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004558 struct k_mem_partition *parts[]);
4559/**
4560 * @brief Destroy a memory domain.
4561 *
4562 * Destroy a memory domain.
4563 *
4564 * @param domain The memory domain to be destroyed.
4565 */
4566
4567extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4568
4569/**
4570 * @brief Add a memory partition into a memory domain.
4571 *
4572 * Add a memory partition into a memory domain.
4573 *
4574 * @param domain The memory domain to be added a memory partition.
4575 * @param part The memory partition to be added
4576 */
4577
4578extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4579 struct k_mem_partition *part);
4580
4581/**
4582 * @brief Remove a memory partition from a memory domain.
4583 *
4584 * Remove a memory partition from a memory domain.
4585 *
4586 * @param domain The memory domain to be removed a memory partition.
4587 * @param part The memory partition to be removed
4588 */
4589
4590extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4591 struct k_mem_partition *part);
4592
4593/**
4594 * @brief Add a thread into a memory domain.
4595 *
4596 * Add a thread into a memory domain.
4597 *
4598 * @param domain The memory domain that the thread is going to be added into.
4599 * @param thread ID of thread going to be added into the memory domain.
4600 */
4601
4602extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4603 k_tid_t thread);
4604
4605/**
4606 * @brief Remove a thread from its memory domain.
4607 *
4608 * Remove a thread from its memory domain.
4609 *
4610 * @param thread ID of thread going to be removed from its memory domain.
4611 */
4612
4613extern void k_mem_domain_remove_thread(k_tid_t thread);
4614
Anas Nashif166f5192018-02-25 08:02:36 -06004615/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004616
Andrew Boie756f9072017-10-10 16:01:49 -07004617/**
4618 * @brief Emit a character buffer to the console device
4619 *
4620 * @param c String of characters to print
4621 * @param n The length of the string
4622 */
4623__syscall void k_str_out(char *c, size_t n);
4624
Andy Rosse7172672018-01-24 15:48:32 -08004625/**
4626 * @brief Start a numbered CPU on a MP-capable system
4627
4628 * This starts and initializes a specific CPU. The main thread on
4629 * startup is running on CPU zero, other processors are numbered
4630 * sequentially. On return from this function, the CPU is known to
4631 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004632 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004633 * with the provided key will work to enable them.
4634 *
4635 * Normally, in SMP mode this function will be called by the kernel
4636 * initialization and should not be used as a user API. But it is
4637 * defined here for special-purpose apps which want Zephyr running on
4638 * one core and to use others for design-specific processing.
4639 *
4640 * @param cpu_num Integer number of the CPU
4641 * @param stack Stack memory for the CPU
4642 * @param sz Stack buffer size, in bytes
4643 * @param fn Function to begin running on the CPU. First argument is
4644 * an irq_unlock() key.
4645 * @param arg Untyped argument to be passed to "fn"
4646 */
4647extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4648 void (*fn)(int, void *), void *arg);
4649
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004650#ifdef __cplusplus
4651}
4652#endif
4653
Andrew Boiee004dec2016-11-07 09:01:19 -08004654#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4655/*
4656 * Define new and delete operators.
4657 * At this moment, the operators do nothing since objects are supposed
4658 * to be statically allocated.
4659 */
4660inline void operator delete(void *ptr)
4661{
4662 (void)ptr;
4663}
4664
4665inline void operator delete[](void *ptr)
4666{
4667 (void)ptr;
4668}
4669
4670inline void *operator new(size_t size)
4671{
4672 (void)size;
4673 return NULL;
4674}
4675
4676inline void *operator new[](size_t size)
4677{
4678 (void)size;
4679 return NULL;
4680}
4681
4682/* Placement versions of operator new and delete */
4683inline void operator delete(void *ptr1, void *ptr2)
4684{
4685 (void)ptr1;
4686 (void)ptr2;
4687}
4688
4689inline void operator delete[](void *ptr1, void *ptr2)
4690{
4691 (void)ptr1;
4692 (void)ptr2;
4693}
4694
4695inline void *operator new(size_t size, void *ptr)
4696{
4697 (void)size;
4698 return ptr;
4699}
4700
4701inline void *operator new[](size_t size, void *ptr)
4702{
4703 (void)size;
4704 return ptr;
4705}
4706
4707#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4708
Andrew Boiefa94ee72017-09-28 16:54:35 -07004709#include <syscalls/kernel.h>
4710
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004711#endif /* !_ASMLANGUAGE */
4712
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004713#endif /* _kernel__h_ */