blob: 1b69cab80b505f569c882a5aaa016a81159a6834 [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
94typedef sys_dlist_t _wait_q_t;
95
Anas Nashif2f203c22016-12-18 06:57:45 -050096#ifdef CONFIG_OBJECT_TRACING
97#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
98#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040099#else
Anas Nashif2f203c22016-12-18 06:57:45 -0500100#define _OBJECT_TRACING_INIT
101#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400102#endif
103
Benjamin Walshacc68c12017-01-29 18:57:45 -0500104#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300105#define _POLL_EVENT_OBJ_INIT(obj) \
106 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
107#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500108#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300109#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500110#define _POLL_EVENT
111#endif
112
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500113struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400114struct k_mutex;
115struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400116struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400117struct k_msgq;
118struct k_mbox;
119struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200120struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400121struct k_fifo;
122struct k_lifo;
123struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400124struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400125struct k_mem_pool;
126struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500127struct k_poll_event;
128struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800129struct k_mem_domain;
130struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400131
Andrew Boie5bd891d2017-09-27 12:59:28 -0700132/* This enumeration needs to be kept in sync with the lists of kernel objects
133 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
134 * function in kernel/userspace.c
135 */
Andrew Boie945af952017-08-22 13:15:23 -0700136enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700137 K_OBJ_ANY,
138
Leandro Pereirac2003672018-04-04 13:50:32 -0700139 /** @cond
140 * Doxygen should ignore this build-time generated include file
141 * when genrating API documentation. Enumeration values are
142 * generated during build by gen_kobject_list.py. It includes
143 * basic kernel objects (e.g. pipes and mutexes) and driver types.
144 */
145#include <kobj-types-enum.h>
146 /** @endcond
147 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700148
Andrew Boie945af952017-08-22 13:15:23 -0700149 K_OBJ_LAST
150};
151
152#ifdef CONFIG_USERSPACE
153/* Table generated by gperf, these objects are retrieved via
154 * _k_object_find() */
155struct _k_object {
156 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700157 u8_t perms[CONFIG_MAX_THREAD_BYTES];
158 u8_t type;
159 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700160 u32_t data;
Andrew Boie945af952017-08-22 13:15:23 -0700161} __packed;
162
Andrew Boie877f82e2017-10-17 11:20:22 -0700163struct _k_object_assignment {
164 struct k_thread *thread;
165 void * const *objects;
166};
167
168/**
169 * @brief Grant a static thread access to a list of kernel objects
170 *
171 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
172 * a set of kernel objects. These objects do not need to be in an initialized
173 * state. The permissions will be granted when the threads are initialized
174 * in the early boot sequence.
175 *
176 * All arguments beyond the first must be pointers to kernel objects.
177 *
178 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
179 */
180#define K_THREAD_ACCESS_GRANT(name_, ...) \
181 static void * const _CONCAT(_object_list_, name_)[] = \
182 { __VA_ARGS__, NULL }; \
183 static __used __in_section_unique(object_access) \
184 const struct _k_object_assignment \
185 _CONCAT(_object_access_, name_) = \
186 { (&_k_thread_obj_ ## name_), \
187 (_CONCAT(_object_list_, name_)) }
188
Andrew Boie945af952017-08-22 13:15:23 -0700189#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700190#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700191#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie945af952017-08-22 13:15:23 -0700192
193/**
194 * Lookup a kernel object and init its metadata if it exists
195 *
196 * Calling this on an object will make it usable from userspace.
197 * Intended to be called as the last statement in kernel object init
198 * functions.
199 *
200 * @param object Address of the kernel object
201 */
202void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700203#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700204
205#define K_THREAD_ACCESS_GRANT(thread, ...)
206
Anas Nashif954d5502018-02-25 08:37:28 -0600207/**
208 * @internal
209 */
Andrew Boie743e4682017-10-04 12:25:50 -0700210static inline void _k_object_init(void *obj)
211{
212 ARG_UNUSED(obj);
213}
214
Anas Nashif954d5502018-02-25 08:37:28 -0600215/**
216 * @internal
217 */
Andrew Boie743e4682017-10-04 12:25:50 -0700218static inline void _impl_k_object_access_grant(void *object,
219 struct k_thread *thread)
220{
221 ARG_UNUSED(object);
222 ARG_UNUSED(thread);
223}
224
Anas Nashif954d5502018-02-25 08:37:28 -0600225/**
226 * @internal
227 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700228static inline void k_object_access_revoke(void *object,
229 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700230{
231 ARG_UNUSED(object);
232 ARG_UNUSED(thread);
233}
234
Andrew Boiee9cfc542018-04-13 13:15:28 -0700235/**
236 * @internal
237 */
238static inline void _impl_k_object_release(void *object)
239{
240 ARG_UNUSED(object);
241}
242
Andrew Boie41bab6e2017-10-14 14:42:23 -0700243static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700244{
245 ARG_UNUSED(object);
246}
247#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700248
249/**
250 * grant a thread access to a kernel object
251 *
252 * The thread will be granted access to the object if the caller is from
253 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700254 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700255 *
256 * @param object Address of kernel object
257 * @param thread Thread to grant access to the object
258 */
Andrew Boie743e4682017-10-04 12:25:50 -0700259__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700260
Andrew Boiea89bf012017-10-09 14:47:55 -0700261/**
262 * grant a thread access to a kernel object
263 *
264 * The thread will lose access to the object if the caller is from
265 * supervisor mode, or the caller is from user mode AND has permissions
266 * on both the object and the thread whose access is being revoked.
267 *
268 * @param object Address of kernel object
269 * @param thread Thread to remove access to the object
270 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700271void k_object_access_revoke(void *object, struct k_thread *thread);
272
273
274__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700275
276/**
277 * grant all present and future threads access to an object
278 *
279 * If the caller is from supervisor mode, or the caller is from user mode and
280 * have sufficient permissions on the object, then that object will have
281 * permissions granted to it for *all* current and future threads running in
282 * the system, effectively becoming a public kernel object.
283 *
284 * Use of this API should be avoided on systems that are running untrusted code
285 * as it is possible for such code to derive the addresses of kernel objects
286 * and perform unwanted operations on them.
287 *
Andrew Boie04caa672017-10-13 13:57:07 -0700288 * It is not possible to revoke permissions on public objects; once public,
289 * any thread may use it.
290 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700291 * @param object Address of kernel object
292 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700293void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700294
Andrew Boie31bdfc02017-11-08 16:38:03 -0800295/**
296 * Allocate a kernel object of a designated type
297 *
298 * This will instantiate at runtime a kernel object of the specified type,
299 * returning a pointer to it. The object will be returned in an uninitialized
300 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700301 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800302 *
303 * Currently, allocation of thread stacks is not supported.
304 *
305 * @param otype Requested kernel object type
306 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
307 * available
308 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700309__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800310
Andrew Boie97bf0012018-04-24 17:01:37 -0700311#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800312/**
313 * Free a kernel object previously allocated with k_object_alloc()
314 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700315 * This will return memory for a kernel object back to resource pool it was
316 * allocated from. Care must be exercised that the object will not be used
317 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800318 *
319 * @param obj Pointer to the kernel object memory address.
320 */
321void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700322#else
323static inline void *_impl_k_object_alloc(enum k_objects otype)
324{
Kumar Gala85699f72018-05-17 09:26:03 -0500325 ARG_UNUSED(otype);
326
Andrew Boie97bf0012018-04-24 17:01:37 -0700327 return NULL;
328}
329
330static inline void k_obj_free(void *obj)
331{
332 ARG_UNUSED(obj);
333}
Andrew Boie31bdfc02017-11-08 16:38:03 -0800334#endif /* CONFIG_DYNAMIC_OBJECTS */
335
Andrew Boiebca15da2017-10-15 14:17:48 -0700336/* Using typedef deliberately here, this is quite intended to be an opaque
337 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
338 *
339 * The purpose of this data type is to clearly distinguish between the
340 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
341 * buffer which composes the stack data actually used by the underlying
342 * thread; they cannot be used interchangably as some arches precede the
343 * stack buffer region with guard areas that trigger a MPU or MMU fault
344 * if written to.
345 *
346 * APIs that want to work with the buffer inside should continue to use
347 * char *.
348 *
349 * Stacks should always be created with K_THREAD_STACK_DEFINE().
350 */
351struct __packed _k_thread_stack_element {
352 char data;
353};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700354typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700355
Andrew Boie73abd322017-04-04 13:19:13 -0700356/* timeouts */
357
358struct _timeout;
359typedef void (*_timeout_func_t)(struct _timeout *t);
360
361struct _timeout {
362 sys_dnode_t node;
363 struct k_thread *thread;
364 sys_dlist_t *wait_q;
365 s32_t delta_ticks_from_prev;
366 _timeout_func_t func;
367};
368
369extern s32_t _timeout_remaining_get(struct _timeout *timeout);
370
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700371/**
372 * @typedef k_thread_entry_t
373 * @brief Thread entry point function type.
374 *
375 * A thread's entry point function is invoked when the thread starts executing.
376 * Up to 3 argument values can be passed to the function.
377 *
378 * The thread terminates execution permanently if the entry point function
379 * returns. The thread is responsible for releasing any shared resources
380 * it may own (such as mutexes and dynamically allocated memory), prior to
381 * returning.
382 *
383 * @param p1 First argument.
384 * @param p2 Second argument.
385 * @param p3 Third argument.
386 *
387 * @return N/A
388 */
389typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700390
391#ifdef CONFIG_THREAD_MONITOR
392struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700393 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700394 void *parameter1;
395 void *parameter2;
396 void *parameter3;
397};
398#endif
399
400/* can be used for creating 'dummy' threads, e.g. for pending on objects */
401struct _thread_base {
402
403 /* this thread's entry in a ready/wait queue */
404 sys_dnode_t k_q_node;
405
406 /* user facing 'thread options'; values defined in include/kernel.h */
407 u8_t user_options;
408
409 /* thread state */
410 u8_t thread_state;
411
412 /*
413 * scheduler lock count and thread priority
414 *
415 * These two fields control the preemptibility of a thread.
416 *
417 * When the scheduler is locked, sched_locked is decremented, which
418 * means that the scheduler is locked for values from 0xff to 0x01. A
419 * thread is coop if its prio is negative, thus 0x80 to 0xff when
420 * looked at the value as unsigned.
421 *
422 * By putting them end-to-end, this means that a thread is
423 * non-preemptible if the bundled value is greater than or equal to
424 * 0x0080.
425 */
426 union {
427 struct {
428#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
429 u8_t sched_locked;
430 s8_t prio;
431#else /* LITTLE and PDP */
432 s8_t prio;
433 u8_t sched_locked;
434#endif
435 };
436 u16_t preempt;
437 };
438
Andy Ross2724fd12018-01-29 14:55:20 -0800439#ifdef CONFIG_SMP
440 /* True for the per-CPU idle threads */
441 u8_t is_idle;
442
443 /* Non-zero when actively running on a CPU */
444 u8_t active;
445
446 /* CPU index on which thread was last run */
447 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700448
449 /* Recursive count of irq_lock() calls */
450 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800451#endif
452
Andrew Boie73abd322017-04-04 13:19:13 -0700453 /* data returned by APIs */
454 void *swap_data;
455
456#ifdef CONFIG_SYS_CLOCK_EXISTS
457 /* this thread's entry in a timeout queue */
458 struct _timeout timeout;
459#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700460};
461
462typedef struct _thread_base _thread_base_t;
463
464#if defined(CONFIG_THREAD_STACK_INFO)
465/* Contains the stack information of a thread */
466struct _thread_stack_info {
467 /* Stack Start */
468 u32_t start;
469 /* Stack Size */
470 u32_t size;
471};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700472
473typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700474#endif /* CONFIG_THREAD_STACK_INFO */
475
Chunlin Hane9c97022017-07-07 20:29:30 +0800476#if defined(CONFIG_USERSPACE)
477struct _mem_domain_info {
478 /* memory domain queue node */
479 sys_dnode_t mem_domain_q_node;
480 /* memory domain of the thread */
481 struct k_mem_domain *mem_domain;
482};
483
484#endif /* CONFIG_USERSPACE */
485
Andrew Boie73abd322017-04-04 13:19:13 -0700486struct k_thread {
487
488 struct _thread_base base;
489
490 /* defined by the architecture, but all archs need these */
491 struct _caller_saved caller_saved;
492 struct _callee_saved callee_saved;
493
494 /* static thread init data */
495 void *init_data;
496
497 /* abort function */
498 void (*fn_abort)(void);
499
500#if defined(CONFIG_THREAD_MONITOR)
501 /* thread entry and parameters description */
502 struct __thread_entry *entry;
503
504 /* next item in list of all threads */
505 struct k_thread *next_thread;
506#endif
507
508#ifdef CONFIG_THREAD_CUSTOM_DATA
509 /* crude thread-local storage */
510 void *custom_data;
511#endif
512
513#ifdef CONFIG_ERRNO
514 /* per-thread errno variable */
515 int errno_var;
516#endif
517
518#if defined(CONFIG_THREAD_STACK_INFO)
519 /* Stack Info */
520 struct _thread_stack_info stack_info;
521#endif /* CONFIG_THREAD_STACK_INFO */
522
Chunlin Hane9c97022017-07-07 20:29:30 +0800523#if defined(CONFIG_USERSPACE)
524 /* memory domain info of the thread */
525 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700526 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700527 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800528#endif /* CONFIG_USERSPACE */
529
Andy Ross042d8ec2017-12-09 08:37:20 -0800530#if defined(CONFIG_USE_SWITCH)
531 /* When using __switch() a few previously arch-specific items
532 * become part of the core OS
533 */
534
535 /* _Swap() return value */
536 int swap_retval;
537
538 /* Context handle returned via _arch_switch() */
539 void *switch_handle;
540#endif
Andrew Boie92e5bd72018-04-12 17:12:15 -0700541 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800542
Andrew Boie73abd322017-04-04 13:19:13 -0700543 /* arch-specifics: must always be at the end */
544 struct _thread_arch arch;
545};
546
547typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400548typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700549#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400550
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400551enum execution_context_types {
552 K_ISR = 0,
553 K_COOP_THREAD,
554 K_PREEMPT_THREAD,
555};
556
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400557/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100558 * @defgroup profiling_apis Profiling APIs
559 * @ingroup kernel_apis
560 * @{
561 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530562typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
563 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100564
565/**
566 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
567 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700568 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100569 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
570 *
571 * CONFIG_MAIN_STACK_SIZE
572 * CONFIG_IDLE_STACK_SIZE
573 * CONFIG_ISR_STACK_SIZE
574 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
575 *
576 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
577 * produce output.
578 *
579 * @return N/A
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530580 *
581 * @deprecated This API is deprecated. Use k_thread_foreach().
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100582 */
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530583__deprecated extern void k_call_stacks_analyze(void);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100584
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530585/**
586 * @brief Iterate over all the threads in the system.
587 *
588 * This routine iterates over all the threads in the system and
589 * calls the user_cb function for each thread.
590 *
591 * @param user_cb Pointer to the user callback function.
592 * @param user_data Pointer to user data.
593 *
594 * @note CONFIG_THREAD_MONITOR must be set for this function
595 * to be effective. Also this API uses irq_lock to protect the
596 * _kernel.threads list which means creation of new threads and
597 * terminations of existing threads are blocked until this
598 * API returns.
599 *
600 * @return N/A
601 */
602extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
603
Anas Nashif166f5192018-02-25 08:02:36 -0600604/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100605
606/**
Allan Stephensc98da842016-11-11 15:45:03 -0500607 * @defgroup thread_apis Thread APIs
608 * @ingroup kernel_apis
609 * @{
610 */
611
Benjamin Walshed240f22017-01-22 13:05:08 -0500612#endif /* !_ASMLANGUAGE */
613
614
615/*
616 * Thread user options. May be needed by assembly code. Common part uses low
617 * bits, arch-specific use high bits.
618 */
619
620/* system thread that must not abort */
621#define K_ESSENTIAL (1 << 0)
622
623#if defined(CONFIG_FP_SHARING)
624/* thread uses floating point registers */
625#define K_FP_REGS (1 << 1)
626#endif
627
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700628/* This thread has dropped from supervisor mode to user mode and consequently
629 * has additional restrictions
630 */
631#define K_USER (1 << 2)
632
Andrew Boie47f8fd12017-10-05 11:11:02 -0700633/* Indicates that the thread being created should inherit all kernel object
634 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
635 * is not enabled.
636 */
637#define K_INHERIT_PERMS (1 << 3)
638
Benjamin Walshed240f22017-01-22 13:05:08 -0500639#ifdef CONFIG_X86
640/* x86 Bitmask definitions for threads user options */
641
642#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
643/* thread uses SSEx (and also FP) registers */
644#define K_SSE_REGS (1 << 7)
645#endif
646#endif
647
648/* end - thread options */
649
650#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400651/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700652 * @brief Create a thread.
653 *
654 * This routine initializes a thread, then schedules it for execution.
655 *
656 * The new thread may be scheduled for immediate execution or a delayed start.
657 * If the newly spawned thread does not have a delayed start the kernel
658 * scheduler may preempt the current thread to allow the new thread to
659 * execute.
660 *
661 * Thread options are architecture-specific, and can include K_ESSENTIAL,
662 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
663 * them using "|" (the logical OR operator).
664 *
665 * Historically, users often would use the beginning of the stack memory region
666 * to store the struct k_thread data, although corruption will occur if the
667 * stack overflows this region and stack protection features may not detect this
668 * situation.
669 *
670 * @param new_thread Pointer to uninitialized struct k_thread
671 * @param stack Pointer to the stack space.
672 * @param stack_size Stack size in bytes.
673 * @param entry Thread entry function.
674 * @param p1 1st entry point parameter.
675 * @param p2 2nd entry point parameter.
676 * @param p3 3rd entry point parameter.
677 * @param prio Thread priority.
678 * @param options Thread options.
679 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
680 *
681 * @return ID of new thread.
682 */
Andrew Boie662c3452017-10-02 10:51:18 -0700683__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700684 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700685 size_t stack_size,
686 k_thread_entry_t entry,
687 void *p1, void *p2, void *p3,
688 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700689
Andrew Boie3f091b52017-08-30 14:34:14 -0700690/**
691 * @brief Drop a thread's privileges permanently to user mode
692 *
693 * @param entry Function to start executing from
694 * @param p1 1st entry point parameter
695 * @param p2 2nd entry point parameter
696 * @param p3 3rd entry point parameter
697 */
698extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
699 void *p1, void *p2,
700 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700701
Andrew Boied26cf2d2017-03-30 13:07:02 -0700702/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700703 * @brief Grant a thread access to a NULL-terminated set of kernel objects
704 *
705 * This is a convenience function. For the provided thread, grant access to
706 * the remaining arguments, which must be pointers to kernel objects.
707 * The final argument must be a NULL.
708 *
709 * The thread object must be initialized (i.e. running). The objects don't
710 * need to be.
711 *
712 * @param thread Thread to grant access to objects
713 * @param ... NULL-terminated list of kernel object pointers
714 */
715extern void __attribute__((sentinel))
716 k_thread_access_grant(struct k_thread *thread, ...);
717
718/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700719 * @brief Assign a resource memory pool to a thread
720 *
721 * By default, threads have no resource pool assigned unless their parent
722 * thread has a resource pool, in which case it is inherited. Multiple
723 * threads may be assigned to the same memory pool.
724 *
725 * Changing a thread's resource pool will not migrate allocations from the
726 * previous pool.
727 *
728 * @param thread Target thread to assign a memory pool for resource requests,
729 * or NULL if the thread should no longer have a memory pool.
730 * @param pool Memory pool to use for resources.
731 */
732static inline void k_thread_resource_pool_assign(struct k_thread *thread,
733 struct k_mem_pool *pool)
734{
735 thread->resource_pool = pool;
736}
737
738#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
739/**
740 * @brief Assign the system heap as a thread's resource pool
741 *
742 * Similar to k_thread_resource_pool_assign(), but the thread will use
743 * the kernel heap to draw memory.
744 *
745 * Use with caution, as a malicious thread could perform DoS attacks on the
746 * kernel heap.
747 *
748 * @param thread Target thread to assign the system heap for resource requests
749 */
750void k_thread_system_pool_assign(struct k_thread *thread);
751#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
752
753/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500754 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400755 *
Allan Stephensc98da842016-11-11 15:45:03 -0500756 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500757 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400758 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500759 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400760 *
761 * @return N/A
762 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700763__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400764
765/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500766 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400767 *
768 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500769 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400770 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400771 * @return N/A
772 */
Kumar Galacc334c72017-04-21 10:55:34 -0500773extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400774
775/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500776 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400777 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500778 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400779 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500780 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400781 *
782 * @return N/A
783 */
Andrew Boie468190a2017-09-29 14:00:48 -0700784__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400785
786/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500787 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400788 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500789 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400790 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500791 * If @a thread is not currently sleeping, the routine has no effect.
792 *
793 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400794 *
795 * @return N/A
796 */
Andrew Boie468190a2017-09-29 14:00:48 -0700797__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400798
799/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500800 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400801 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500802 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400803 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700804__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400805
806/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500807 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500809 * This routine prevents @a thread from executing if it has not yet started
810 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500812 * @param thread ID of thread to cancel.
813 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700814 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500815 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700816 *
817 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400818 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700819__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400820
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400821/**
Allan Stephensc98da842016-11-11 15:45:03 -0500822 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500824 * This routine permanently stops execution of @a thread. The thread is taken
825 * off all kernel queues it is part of (i.e. the ready queue, the timeout
826 * queue, or a kernel object wait queue). However, any kernel resources the
827 * thread might currently own (such as mutexes or memory blocks) are not
828 * released. It is the responsibility of the caller of this routine to ensure
829 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500831 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832 *
833 * @return N/A
834 */
Andrew Boie468190a2017-09-29 14:00:48 -0700835__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400836
Andrew Boie7d627c52017-08-30 11:01:56 -0700837
838/**
839 * @brief Start an inactive thread
840 *
841 * If a thread was created with K_FOREVER in the delay parameter, it will
842 * not be added to the scheduling queue until this function is called
843 * on it.
844 *
845 * @param thread thread to start
846 */
Andrew Boie468190a2017-09-29 14:00:48 -0700847__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700848
Allan Stephensc98da842016-11-11 15:45:03 -0500849/**
850 * @cond INTERNAL_HIDDEN
851 */
852
Benjamin Walshd211a522016-12-06 11:44:01 -0500853/* timeout has timed out and is not on _timeout_q anymore */
854#define _EXPIRED (-2)
855
856/* timeout is not in use */
857#define _INACTIVE (-1)
858
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400859struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700860 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700861 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400862 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700863 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500864 void *init_p1;
865 void *init_p2;
866 void *init_p3;
867 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500868 u32_t init_options;
869 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500870 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400871};
872
Andrew Boied26cf2d2017-03-30 13:07:02 -0700873#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400874 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500875 prio, options, delay, abort, groups) \
876 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700877 .init_thread = (thread), \
878 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500879 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700880 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400881 .init_p1 = (void *)p1, \
882 .init_p2 = (void *)p2, \
883 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500884 .init_prio = (prio), \
885 .init_options = (options), \
886 .init_delay = (delay), \
887 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400888 }
889
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400890/**
Allan Stephensc98da842016-11-11 15:45:03 -0500891 * INTERNAL_HIDDEN @endcond
892 */
893
894/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500895 * @brief Statically define and initialize a thread.
896 *
897 * The thread may be scheduled for immediate execution or a delayed start.
898 *
899 * Thread options are architecture-specific, and can include K_ESSENTIAL,
900 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
901 * them using "|" (the logical OR operator).
902 *
903 * The ID of the thread can be accessed using:
904 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500905 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500906 *
907 * @param name Name of the thread.
908 * @param stack_size Stack size in bytes.
909 * @param entry Thread entry function.
910 * @param p1 1st entry point parameter.
911 * @param p2 2nd entry point parameter.
912 * @param p3 3rd entry point parameter.
913 * @param prio Thread priority.
914 * @param options Thread options.
915 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400916 *
917 * @internal It has been observed that the x86 compiler by default aligns
918 * these _static_thread_data structures to 32-byte boundaries, thereby
919 * wasting space. To work around this, force a 4-byte alignment.
920 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500921#define K_THREAD_DEFINE(name, stack_size, \
922 entry, p1, p2, p3, \
923 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700924 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700925 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500926 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500927 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700928 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
929 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500930 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700931 NULL, 0); \
932 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400933
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400934/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500935 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400936 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500937 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500939 * @param thread ID of thread whose priority is needed.
940 *
941 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400942 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700943__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400944
945/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500946 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400947 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500948 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400949 *
950 * Rescheduling can occur immediately depending on the priority @a thread is
951 * set to:
952 *
953 * - If its priority is raised above the priority of the caller of this
954 * function, and the caller is preemptible, @a thread will be scheduled in.
955 *
956 * - If the caller operates on itself, it lowers its priority below that of
957 * other threads in the system, and the caller is preemptible, the thread of
958 * highest priority will be scheduled in.
959 *
960 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
961 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
962 * highest priority.
963 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500964 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400965 * @param prio New priority.
966 *
967 * @warning Changing the priority of a thread currently involved in mutex
968 * priority inheritance may result in undefined behavior.
969 *
970 * @return N/A
971 */
Andrew Boie468190a2017-09-29 14:00:48 -0700972__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400973
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400974/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500975 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500977 * This routine prevents the kernel scheduler from making @a thread the
978 * current thread. All other internal operations on @a thread are still
979 * performed; for example, any timeout it is waiting on keeps ticking,
980 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400981 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500982 * If @a thread is already suspended, the routine has no effect.
983 *
984 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400985 *
986 * @return N/A
987 */
Andrew Boie468190a2017-09-29 14:00:48 -0700988__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400989
990/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500991 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500993 * This routine allows the kernel scheduler to make @a thread the current
994 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400995 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500996 * If @a thread is not currently suspended, the routine has no effect.
997 *
998 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400999 *
1000 * @return N/A
1001 */
Andrew Boie468190a2017-09-29 14:00:48 -07001002__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001003
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001004/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001005 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001007 * This routine specifies how the scheduler will perform time slicing of
1008 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001010 * To enable time slicing, @a slice must be non-zero. The scheduler
1011 * ensures that no thread runs for more than the specified time limit
1012 * before other threads of that priority are given a chance to execute.
1013 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001014 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001016 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001017 * execute. Once the scheduler selects a thread for execution, there is no
1018 * minimum guaranteed time the thread will execute before threads of greater or
1019 * equal priority are scheduled.
1020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001021 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001022 * for execution, this routine has no effect; the thread is immediately
1023 * rescheduled after the slice period expires.
1024 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001025 * To disable timeslicing, set both @a slice and @a prio to zero.
1026 *
1027 * @param slice Maximum time slice length (in milliseconds).
1028 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001029 *
1030 * @return N/A
1031 */
Kumar Galacc334c72017-04-21 10:55:34 -05001032extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001033
Anas Nashif166f5192018-02-25 08:02:36 -06001034/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001035
1036/**
1037 * @addtogroup isr_apis
1038 * @{
1039 */
1040
1041/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001042 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001043 *
Allan Stephensc98da842016-11-11 15:45:03 -05001044 * This routine allows the caller to customize its actions, depending on
1045 * whether it is a thread or an ISR.
1046 *
1047 * @note Can be called by ISRs.
1048 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001049 * @return 0 if invoked by a thread.
1050 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001051 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001052extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001053
Benjamin Walsh445830d2016-11-10 15:54:27 -05001054/**
1055 * @brief Determine if code is running in a preemptible thread.
1056 *
Allan Stephensc98da842016-11-11 15:45:03 -05001057 * This routine allows the caller to customize its actions, depending on
1058 * whether it can be preempted by another thread. The routine returns a 'true'
1059 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001060 *
Allan Stephensc98da842016-11-11 15:45:03 -05001061 * - The code is running in a thread, not at ISR.
1062 * - The thread's priority is in the preemptible range.
1063 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001064 *
Allan Stephensc98da842016-11-11 15:45:03 -05001065 * @note Can be called by ISRs.
1066 *
1067 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001068 * @return Non-zero if invoked by a preemptible thread.
1069 */
Andrew Boie468190a2017-09-29 14:00:48 -07001070__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001071
Allan Stephensc98da842016-11-11 15:45:03 -05001072/**
Anas Nashif166f5192018-02-25 08:02:36 -06001073 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001074 */
1075
1076/**
1077 * @addtogroup thread_apis
1078 * @{
1079 */
1080
1081/**
1082 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001083 *
Allan Stephensc98da842016-11-11 15:45:03 -05001084 * This routine prevents the current thread from being preempted by another
1085 * thread by instructing the scheduler to treat it as a cooperative thread.
1086 * If the thread subsequently performs an operation that makes it unready,
1087 * it will be context switched out in the normal manner. When the thread
1088 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001089 *
Allan Stephensc98da842016-11-11 15:45:03 -05001090 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001091 *
Allan Stephensc98da842016-11-11 15:45:03 -05001092 * @note k_sched_lock() and k_sched_unlock() should normally be used
1093 * when the operation being performed can be safely interrupted by ISRs.
1094 * However, if the amount of processing involved is very small, better
1095 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001096 *
1097 * @return N/A
1098 */
1099extern void k_sched_lock(void);
1100
Allan Stephensc98da842016-11-11 15:45:03 -05001101/**
1102 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001103 *
Allan Stephensc98da842016-11-11 15:45:03 -05001104 * This routine reverses the effect of a previous call to k_sched_lock().
1105 * A thread must call the routine once for each time it called k_sched_lock()
1106 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001107 *
1108 * @return N/A
1109 */
1110extern void k_sched_unlock(void);
1111
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001112/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001113 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001114 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001115 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001117 * Custom data is not used by the kernel itself, and is freely available
1118 * for a thread to use as it sees fit. It can be used as a framework
1119 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001120 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001121 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001122 *
1123 * @return N/A
1124 */
Andrew Boie468190a2017-09-29 14:00:48 -07001125__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001126
1127/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001128 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001130 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001132 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001133 */
Andrew Boie468190a2017-09-29 14:00:48 -07001134__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001135
1136/**
Anas Nashif166f5192018-02-25 08:02:36 -06001137 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001138 */
1139
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001140#include <sys_clock.h>
1141
Allan Stephensc2f15a42016-11-17 12:24:22 -05001142/**
1143 * @addtogroup clock_apis
1144 * @{
1145 */
1146
1147/**
1148 * @brief Generate null timeout delay.
1149 *
1150 * This macro generates a timeout delay that that instructs a kernel API
1151 * not to wait if the requested operation cannot be performed immediately.
1152 *
1153 * @return Timeout delay value.
1154 */
1155#define K_NO_WAIT 0
1156
1157/**
1158 * @brief Generate timeout delay from milliseconds.
1159 *
1160 * This macro generates a timeout delay that that instructs a kernel API
1161 * to wait up to @a ms milliseconds to perform the requested operation.
1162 *
1163 * @param ms Duration in milliseconds.
1164 *
1165 * @return Timeout delay value.
1166 */
Johan Hedberg14471692016-11-13 10:52:15 +02001167#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001168
1169/**
1170 * @brief Generate timeout delay from seconds.
1171 *
1172 * This macro generates a timeout delay that that instructs a kernel API
1173 * to wait up to @a s seconds to perform the requested operation.
1174 *
1175 * @param s Duration in seconds.
1176 *
1177 * @return Timeout delay value.
1178 */
Johan Hedberg14471692016-11-13 10:52:15 +02001179#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001180
1181/**
1182 * @brief Generate timeout delay from minutes.
1183 *
1184 * This macro generates a timeout delay that that instructs a kernel API
1185 * to wait up to @a m minutes to perform the requested operation.
1186 *
1187 * @param m Duration in minutes.
1188 *
1189 * @return Timeout delay value.
1190 */
Johan Hedberg14471692016-11-13 10:52:15 +02001191#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001192
1193/**
1194 * @brief Generate timeout delay from hours.
1195 *
1196 * This macro generates a timeout delay that that instructs a kernel API
1197 * to wait up to @a h hours to perform the requested operation.
1198 *
1199 * @param h Duration in hours.
1200 *
1201 * @return Timeout delay value.
1202 */
Johan Hedberg14471692016-11-13 10:52:15 +02001203#define K_HOURS(h) K_MINUTES((h) * 60)
1204
Allan Stephensc98da842016-11-11 15:45:03 -05001205/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001206 * @brief Generate infinite timeout delay.
1207 *
1208 * This macro generates a timeout delay that that instructs a kernel API
1209 * to wait as long as necessary to perform the requested operation.
1210 *
1211 * @return Timeout delay value.
1212 */
1213#define K_FOREVER (-1)
1214
1215/**
Anas Nashif166f5192018-02-25 08:02:36 -06001216 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001217 */
1218
1219/**
Allan Stephensc98da842016-11-11 15:45:03 -05001220 * @cond INTERNAL_HIDDEN
1221 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001222
Benjamin Walsh62092182016-12-20 14:39:08 -05001223/* kernel clocks */
1224
1225#if (sys_clock_ticks_per_sec == 1000) || \
1226 (sys_clock_ticks_per_sec == 500) || \
1227 (sys_clock_ticks_per_sec == 250) || \
1228 (sys_clock_ticks_per_sec == 125) || \
1229 (sys_clock_ticks_per_sec == 100) || \
1230 (sys_clock_ticks_per_sec == 50) || \
1231 (sys_clock_ticks_per_sec == 25) || \
1232 (sys_clock_ticks_per_sec == 20) || \
1233 (sys_clock_ticks_per_sec == 10) || \
1234 (sys_clock_ticks_per_sec == 1)
1235
1236 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1237#else
1238 /* yields horrible 64-bit math on many architectures: try to avoid */
1239 #define _NON_OPTIMIZED_TICKS_PER_SEC
1240#endif
1241
1242#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001243extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001244#else
Kumar Galacc334c72017-04-21 10:55:34 -05001245static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001246{
Kumar Galacc334c72017-04-21 10:55:34 -05001247 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001248}
1249#endif
1250
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001251/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001252#ifdef CONFIG_TICKLESS_KERNEL
1253#define _TICK_ALIGN 0
1254#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001255#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001256#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001257
Kumar Galacc334c72017-04-21 10:55:34 -05001258static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001259{
Benjamin Walsh62092182016-12-20 14:39:08 -05001260#ifdef CONFIG_SYS_CLOCK_EXISTS
1261
1262#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001263 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001264#else
Kumar Galacc334c72017-04-21 10:55:34 -05001265 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001266#endif
1267
1268#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001269 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001270 return 0;
1271#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001272}
1273
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001274struct k_timer {
1275 /*
1276 * _timeout structure must be first here if we want to use
1277 * dynamic timer allocation. timeout.node is used in the double-linked
1278 * list of free timers
1279 */
1280 struct _timeout timeout;
1281
Allan Stephens45bfa372016-10-12 12:39:42 -05001282 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001283 _wait_q_t wait_q;
1284
1285 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001286 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001287
1288 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001289 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001290
1291 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001292 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001293
Allan Stephens45bfa372016-10-12 12:39:42 -05001294 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001295 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001296
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001297 /* user-specific data, also used to support legacy features */
1298 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001299
Anas Nashif2f203c22016-12-18 06:57:45 -05001300 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001301};
1302
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001303#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001304 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001305 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001306 .timeout.wait_q = NULL, \
1307 .timeout.thread = NULL, \
1308 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001309 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001310 .expiry_fn = expiry, \
1311 .stop_fn = stop, \
1312 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001313 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001314 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001315 }
1316
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001317#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1318
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001319/**
Allan Stephensc98da842016-11-11 15:45:03 -05001320 * INTERNAL_HIDDEN @endcond
1321 */
1322
1323/**
1324 * @defgroup timer_apis Timer APIs
1325 * @ingroup kernel_apis
1326 * @{
1327 */
1328
1329/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001330 * @typedef k_timer_expiry_t
1331 * @brief Timer expiry function type.
1332 *
1333 * A timer's expiry function is executed by the system clock interrupt handler
1334 * each time the timer expires. The expiry function is optional, and is only
1335 * invoked if the timer has been initialized with one.
1336 *
1337 * @param timer Address of timer.
1338 *
1339 * @return N/A
1340 */
1341typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1342
1343/**
1344 * @typedef k_timer_stop_t
1345 * @brief Timer stop function type.
1346 *
1347 * A timer's stop function is executed if the timer is stopped prematurely.
1348 * The function runs in the context of the thread that stops the timer.
1349 * The stop function is optional, and is only invoked if the timer has been
1350 * initialized with one.
1351 *
1352 * @param timer Address of timer.
1353 *
1354 * @return N/A
1355 */
1356typedef void (*k_timer_stop_t)(struct k_timer *timer);
1357
1358/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001359 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001360 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001361 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001362 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001363 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001364 *
1365 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001366 * @param expiry_fn Function to invoke each time the timer expires.
1367 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001368 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001369#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001370 struct k_timer name \
1371 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001372 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001373
Allan Stephens45bfa372016-10-12 12:39:42 -05001374/**
1375 * @brief Initialize a timer.
1376 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001377 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001378 *
1379 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001380 * @param expiry_fn Function to invoke each time the timer expires.
1381 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001382 *
1383 * @return N/A
1384 */
1385extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001386 k_timer_expiry_t expiry_fn,
1387 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001388
Allan Stephens45bfa372016-10-12 12:39:42 -05001389/**
1390 * @brief Start a timer.
1391 *
1392 * This routine starts a timer, and resets its status to zero. The timer
1393 * begins counting down using the specified duration and period values.
1394 *
1395 * Attempting to start a timer that is already running is permitted.
1396 * The timer's status is reset to zero and the timer begins counting down
1397 * using the new duration and period values.
1398 *
1399 * @param timer Address of timer.
1400 * @param duration Initial timer duration (in milliseconds).
1401 * @param period Timer period (in milliseconds).
1402 *
1403 * @return N/A
1404 */
Andrew Boiea354d492017-09-29 16:22:28 -07001405__syscall void k_timer_start(struct k_timer *timer,
1406 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001407
1408/**
1409 * @brief Stop a timer.
1410 *
1411 * This routine stops a running timer prematurely. The timer's stop function,
1412 * if one exists, is invoked by the caller.
1413 *
1414 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001415 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001416 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001417 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1418 * if @a k_timer_stop is to be called from ISRs.
1419 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001420 * @param timer Address of timer.
1421 *
1422 * @return N/A
1423 */
Andrew Boiea354d492017-09-29 16:22:28 -07001424__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001425
1426/**
1427 * @brief Read timer status.
1428 *
1429 * This routine reads the timer's status, which indicates the number of times
1430 * it has expired since its status was last read.
1431 *
1432 * Calling this routine resets the timer's status to zero.
1433 *
1434 * @param timer Address of timer.
1435 *
1436 * @return Timer status.
1437 */
Andrew Boiea354d492017-09-29 16:22:28 -07001438__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001439
1440/**
1441 * @brief Synchronize thread to timer expiration.
1442 *
1443 * This routine blocks the calling thread until the timer's status is non-zero
1444 * (indicating that it has expired at least once since it was last examined)
1445 * or the timer is stopped. If the timer status is already non-zero,
1446 * or the timer is already stopped, the caller continues without waiting.
1447 *
1448 * Calling this routine resets the timer's status to zero.
1449 *
1450 * This routine must not be used by interrupt handlers, since they are not
1451 * allowed to block.
1452 *
1453 * @param timer Address of timer.
1454 *
1455 * @return Timer status.
1456 */
Andrew Boiea354d492017-09-29 16:22:28 -07001457__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001458
1459/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001460 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001461 *
1462 * This routine computes the (approximate) time remaining before a running
1463 * timer next expires. If the timer is not running, it returns zero.
1464 *
1465 * @param timer Address of timer.
1466 *
1467 * @return Remaining time (in milliseconds).
1468 */
Andrew Boiea354d492017-09-29 16:22:28 -07001469__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1470
1471static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001472{
1473 return _timeout_remaining_get(&timer->timeout);
1474}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001475
Allan Stephensc98da842016-11-11 15:45:03 -05001476/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001477 * @brief Associate user-specific data with a timer.
1478 *
1479 * This routine records the @a user_data with the @a timer, to be retrieved
1480 * later.
1481 *
1482 * It can be used e.g. in a timer handler shared across multiple subsystems to
1483 * retrieve data specific to the subsystem this timer is associated with.
1484 *
1485 * @param timer Address of timer.
1486 * @param user_data User data to associate with the timer.
1487 *
1488 * @return N/A
1489 */
Andrew Boiea354d492017-09-29 16:22:28 -07001490__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1491
Anas Nashif954d5502018-02-25 08:37:28 -06001492/**
1493 * @internal
1494 */
Andrew Boiea354d492017-09-29 16:22:28 -07001495static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1496 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001497{
1498 timer->user_data = user_data;
1499}
1500
1501/**
1502 * @brief Retrieve the user-specific data from a timer.
1503 *
1504 * @param timer Address of timer.
1505 *
1506 * @return The user data.
1507 */
Andrew Boiea354d492017-09-29 16:22:28 -07001508__syscall void *k_timer_user_data_get(struct k_timer *timer);
1509
1510static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001511{
1512 return timer->user_data;
1513}
1514
Anas Nashif166f5192018-02-25 08:02:36 -06001515/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001516
Allan Stephensc98da842016-11-11 15:45:03 -05001517/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001518 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001519 * @{
1520 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001521
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001522/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001523 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001524 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001525 * This routine returns the elapsed time since the system booted,
1526 * in milliseconds.
1527 *
1528 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001529 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001530__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001531
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001532/**
1533 * @brief Enable clock always on in tickless kernel
1534 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001535 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001536 * there are no timer events programmed in tickless kernel
1537 * scheduling. This is necessary if the clock is used to track
1538 * passage of time.
1539 *
1540 * @retval prev_status Previous status of always on flag
1541 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301542#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001543static inline int k_enable_sys_clock_always_on(void)
1544{
1545 int prev_status = _sys_clock_always_on;
1546
1547 _sys_clock_always_on = 1;
1548 _enable_sys_clock();
1549
1550 return prev_status;
1551}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301552#else
1553#define k_enable_sys_clock_always_on() do { } while ((0))
1554#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001555
1556/**
1557 * @brief Disable clock always on in tickless kernel
1558 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001559 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001560 * there are no timer events programmed in tickless kernel
1561 * scheduling. To save power, this routine should be called
1562 * immediately when clock is not used to track time.
1563 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301564#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001565static inline void k_disable_sys_clock_always_on(void)
1566{
1567 _sys_clock_always_on = 0;
1568}
1569#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001570#define k_disable_sys_clock_always_on() do { } while ((0))
1571#endif
1572
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001573/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001574 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001575 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001576 * This routine returns the lower 32-bits of the elapsed time since the system
1577 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001578 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001579 * This routine can be more efficient than k_uptime_get(), as it reduces the
1580 * need for interrupt locking and 64-bit math. However, the 32-bit result
1581 * cannot hold a system uptime time larger than approximately 50 days, so the
1582 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001584 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001585 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001586__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001587
1588/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001589 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001591 * This routine computes the elapsed time between the current system uptime
1592 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001593 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001594 * @param reftime Pointer to a reference time, which is updated to the current
1595 * uptime upon return.
1596 *
1597 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001598 */
Kumar Galacc334c72017-04-21 10:55:34 -05001599extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001600
1601/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001602 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001604 * This routine computes the elapsed time between the current system uptime
1605 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001606 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001607 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1608 * need for interrupt locking and 64-bit math. However, the 32-bit result
1609 * cannot hold an elapsed time larger than approximately 50 days, so the
1610 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001612 * @param reftime Pointer to a reference time, which is updated to the current
1613 * uptime upon return.
1614 *
1615 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001616 */
Kumar Galacc334c72017-04-21 10:55:34 -05001617extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001618
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001619/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001620 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001622 * This routine returns the current time, as measured by the system's hardware
1623 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001624 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001625 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001626 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001627#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001628
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001629/**
Anas Nashif166f5192018-02-25 08:02:36 -06001630 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001631 */
1632
Allan Stephensc98da842016-11-11 15:45:03 -05001633/**
1634 * @cond INTERNAL_HIDDEN
1635 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001636
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001637struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001638 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001639 union {
1640 _wait_q_t wait_q;
1641
1642 _POLL_EVENT;
1643 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001644
1645 _OBJECT_TRACING_NEXT_PTR(k_queue);
1646};
1647
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001648#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001649 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001650 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Michael Hope5f67a612018-03-17 12:44:40 +01001651 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001652 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001653 _OBJECT_TRACING_INIT \
1654 }
1655
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001656#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1657
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001658extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1659
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001660/**
1661 * INTERNAL_HIDDEN @endcond
1662 */
1663
1664/**
1665 * @defgroup queue_apis Queue APIs
1666 * @ingroup kernel_apis
1667 * @{
1668 */
1669
1670/**
1671 * @brief Initialize a queue.
1672 *
1673 * This routine initializes a queue object, prior to its first use.
1674 *
1675 * @param queue Address of the queue.
1676 *
1677 * @return N/A
1678 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001679__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001680
1681/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001682 * @brief Cancel waiting on a queue.
1683 *
1684 * This routine causes first thread pending on @a queue, if any, to
1685 * return from k_queue_get() call with NULL value (as if timeout expired).
1686 *
1687 * @note Can be called by ISRs.
1688 *
1689 * @param queue Address of the queue.
1690 *
1691 * @return N/A
1692 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001693__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001694
1695/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001696 * @brief Append an element to the end of a queue.
1697 *
1698 * This routine appends a data item to @a queue. A queue data item must be
1699 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1700 * reserved for the kernel's use.
1701 *
1702 * @note Can be called by ISRs.
1703 *
1704 * @param queue Address of the queue.
1705 * @param data Address of the data item.
1706 *
1707 * @return N/A
1708 */
1709extern void k_queue_append(struct k_queue *queue, void *data);
1710
1711/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001712 * @brief Append an element to a queue.
1713 *
1714 * This routine appends a data item to @a queue. There is an implicit
1715 * memory allocation from the calling thread's resource pool, which is
1716 * automatically freed when the item is removed from the queue.
1717 *
1718 * @note Can be called by ISRs.
1719 *
1720 * @param queue Address of the queue.
1721 * @param data Address of the data item.
1722 *
1723 * @retval 0 on success
1724 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1725 */
1726__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1727
1728/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001729 * @brief Prepend an element to a queue.
1730 *
1731 * This routine prepends a data item to @a queue. A queue data item must be
1732 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1733 * reserved for the kernel's use.
1734 *
1735 * @note Can be called by ISRs.
1736 *
1737 * @param queue Address of the queue.
1738 * @param data Address of the data item.
1739 *
1740 * @return N/A
1741 */
1742extern void k_queue_prepend(struct k_queue *queue, void *data);
1743
1744/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001745 * @brief Prepend an element to a queue.
1746 *
1747 * This routine prepends a data item to @a queue. There is an implicit
1748 * memory allocation from the calling thread's resource pool, which is
1749 * automatically freed when the item is removed from the queue.
1750 *
1751 * @note Can be called by ISRs.
1752 *
1753 * @param queue Address of the queue.
1754 * @param data Address of the data item.
1755 *
1756 * @retval 0 on success
1757 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1758 */
1759__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1760
1761/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001762 * @brief Inserts an element to a queue.
1763 *
1764 * This routine inserts a data item to @a queue after previous item. A queue
1765 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1766 * item are reserved for the kernel's use.
1767 *
1768 * @note Can be called by ISRs.
1769 *
1770 * @param queue Address of the queue.
1771 * @param prev Address of the previous data item.
1772 * @param data Address of the data item.
1773 *
1774 * @return N/A
1775 */
1776extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1777
1778/**
1779 * @brief Atomically append a list of elements to a queue.
1780 *
1781 * This routine adds a list of data items to @a queue in one operation.
1782 * The data items must be in a singly-linked list, with the first 32 bits
1783 * in each data item pointing to the next data item; the list must be
1784 * NULL-terminated.
1785 *
1786 * @note Can be called by ISRs.
1787 *
1788 * @param queue Address of the queue.
1789 * @param head Pointer to first node in singly-linked list.
1790 * @param tail Pointer to last node in singly-linked list.
1791 *
1792 * @return N/A
1793 */
1794extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1795
1796/**
1797 * @brief Atomically add a list of elements to a queue.
1798 *
1799 * This routine adds a list of data items to @a queue in one operation.
1800 * The data items must be in a singly-linked list implemented using a
1801 * sys_slist_t object. Upon completion, the original list is empty.
1802 *
1803 * @note Can be called by ISRs.
1804 *
1805 * @param queue Address of the queue.
1806 * @param list Pointer to sys_slist_t object.
1807 *
1808 * @return N/A
1809 */
1810extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1811
1812/**
1813 * @brief Get an element from a queue.
1814 *
1815 * This routine removes first data item from @a queue. The first 32 bits of the
1816 * data item are reserved for the kernel's use.
1817 *
1818 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1819 *
1820 * @param queue Address of the queue.
1821 * @param timeout Waiting period to obtain a data item (in milliseconds),
1822 * or one of the special values K_NO_WAIT and K_FOREVER.
1823 *
1824 * @return Address of the data item if successful; NULL if returned
1825 * without waiting, or waiting period timed out.
1826 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001827__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001828
1829/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001830 * @brief Remove an element from a queue.
1831 *
1832 * This routine removes data item from @a queue. The first 32 bits of the
1833 * data item are reserved for the kernel's use. Removing elements from k_queue
1834 * rely on sys_slist_find_and_remove which is not a constant time operation.
1835 *
1836 * @note Can be called by ISRs
1837 *
1838 * @param queue Address of the queue.
1839 * @param data Address of the data item.
1840 *
1841 * @return true if data item was removed
1842 */
1843static inline bool k_queue_remove(struct k_queue *queue, void *data)
1844{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001845 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001846}
1847
1848/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001849 * @brief Query a queue to see if it has data available.
1850 *
1851 * Note that the data might be already gone by the time this function returns
1852 * if other threads are also trying to read from the queue.
1853 *
1854 * @note Can be called by ISRs.
1855 *
1856 * @param queue Address of the queue.
1857 *
1858 * @return Non-zero if the queue is empty.
1859 * @return 0 if data is available.
1860 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001861__syscall int k_queue_is_empty(struct k_queue *queue);
1862
1863static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001864{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001865 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001866}
1867
1868/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001869 * @brief Peek element at the head of queue.
1870 *
1871 * Return element from the head of queue without removing it.
1872 *
1873 * @param queue Address of the queue.
1874 *
1875 * @return Head element, or NULL if queue is empty.
1876 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001877__syscall void *k_queue_peek_head(struct k_queue *queue);
1878
1879static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001880{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001881 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001882}
1883
1884/**
1885 * @brief Peek element at the tail of queue.
1886 *
1887 * Return element from the tail of queue without removing it.
1888 *
1889 * @param queue Address of the queue.
1890 *
1891 * @return Tail element, or NULL if queue is empty.
1892 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001893__syscall void *k_queue_peek_tail(struct k_queue *queue);
1894
1895static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001896{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001897 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001898}
1899
1900/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001901 * @brief Statically define and initialize a queue.
1902 *
1903 * The queue can be accessed outside the module where it is defined using:
1904 *
1905 * @code extern struct k_queue <name>; @endcode
1906 *
1907 * @param name Name of the queue.
1908 */
1909#define K_QUEUE_DEFINE(name) \
1910 struct k_queue name \
1911 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001912 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001913
Anas Nashif166f5192018-02-25 08:02:36 -06001914/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001915
1916/**
1917 * @cond INTERNAL_HIDDEN
1918 */
1919
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001920struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001921 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001922};
1923
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001924#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001925 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001926 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001927 }
1928
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001929#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1930
Allan Stephensc98da842016-11-11 15:45:03 -05001931/**
1932 * INTERNAL_HIDDEN @endcond
1933 */
1934
1935/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001936 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001937 * @ingroup kernel_apis
1938 * @{
1939 */
1940
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001941/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001942 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001943 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001944 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001945 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001946 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001947 *
1948 * @return N/A
1949 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001950#define k_fifo_init(fifo) \
1951 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001952
1953/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001954 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001955 *
1956 * This routine causes first thread pending on @a fifo, if any, to
1957 * return from k_fifo_get() call with NULL value (as if timeout
1958 * expired).
1959 *
1960 * @note Can be called by ISRs.
1961 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001962 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001963 *
1964 * @return N/A
1965 */
1966#define k_fifo_cancel_wait(fifo) \
1967 k_queue_cancel_wait((struct k_queue *) fifo)
1968
1969/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001970 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001971 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001972 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001973 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1974 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001975 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001976 * @note Can be called by ISRs.
1977 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001978 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001979 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001980 *
1981 * @return N/A
1982 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001983#define k_fifo_put(fifo, data) \
1984 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001985
1986/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001987 * @brief Add an element to a FIFO queue.
1988 *
1989 * This routine adds a data item to @a fifo. There is an implicit
1990 * memory allocation from the calling thread's resource pool, which is
1991 * automatically freed when the item is removed.
1992 *
1993 * @note Can be called by ISRs.
1994 *
1995 * @param fifo Address of the FIFO.
1996 * @param data Address of the data item.
1997 *
1998 * @retval 0 on success
1999 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2000 */
2001#define k_fifo_alloc_put(fifo, data) \
2002 k_queue_alloc_append((struct k_queue *) fifo, data)
2003
2004/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002005 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002007 * This routine adds a list of data items to @a fifo in one operation.
2008 * The data items must be in a singly-linked list, with the first 32 bits
2009 * each data item pointing to the next data item; the list must be
2010 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002011 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002012 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002013 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002014 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002015 * @param head Pointer to first node in singly-linked list.
2016 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002017 *
2018 * @return N/A
2019 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002020#define k_fifo_put_list(fifo, head, tail) \
2021 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002022
2023/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002024 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002025 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002026 * This routine adds a list of data items to @a fifo in one operation.
2027 * The data items must be in a singly-linked list implemented using a
2028 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002029 * and must be re-initialized via sys_slist_init().
2030 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002031 * @note Can be called by ISRs.
2032 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002033 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002034 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002035 *
2036 * @return N/A
2037 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002038#define k_fifo_put_slist(fifo, list) \
2039 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002040
2041/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002042 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002043 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002044 * This routine removes a data item from @a fifo in a "first in, first out"
2045 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002046 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002047 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2048 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002049 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002050 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002051 * or one of the special values K_NO_WAIT and K_FOREVER.
2052 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002053 * @return Address of the data item if successful; NULL if returned
2054 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002055 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002056#define k_fifo_get(fifo, timeout) \
2057 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002058
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002059/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002060 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002061 *
2062 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002063 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002064 *
2065 * @note Can be called by ISRs.
2066 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002067 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002068 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002069 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002070 * @return 0 if data is available.
2071 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002072#define k_fifo_is_empty(fifo) \
2073 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002074
2075/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002076 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002077 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002078 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302079 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002080 * on each iteration of processing, a head container will be peeked,
2081 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002082 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002083 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002084 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002085 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002086 * @return Head element, or NULL if the FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002087 */
2088#define k_fifo_peek_head(fifo) \
2089 k_queue_peek_head((struct k_queue *) fifo)
2090
2091/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002092 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002093 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002094 * Return element from the tail of FIFO queue (without removing it). A usecase
2095 * for this is if elements of the FIFO queue are themselves containers. Then
2096 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002097 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002098 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002099 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002100 * @return Tail element, or NULL if a FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002101 */
2102#define k_fifo_peek_tail(fifo) \
2103 k_queue_peek_tail((struct k_queue *) fifo)
2104
2105/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002106 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002107 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002108 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002109 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002110 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002111 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002112 * @param name Name of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002113 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002114#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002115 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002116 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002117 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002118
Anas Nashif166f5192018-02-25 08:02:36 -06002119/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002120
2121/**
2122 * @cond INTERNAL_HIDDEN
2123 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002124
2125struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002126 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002127};
2128
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002129#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002130 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002131 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002132 }
2133
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002134#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2135
Allan Stephensc98da842016-11-11 15:45:03 -05002136/**
2137 * INTERNAL_HIDDEN @endcond
2138 */
2139
2140/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002141 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002142 * @ingroup kernel_apis
2143 * @{
2144 */
2145
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002146/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002147 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002148 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002149 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002150 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002151 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002152 *
2153 * @return N/A
2154 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002155#define k_lifo_init(lifo) \
2156 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002157
2158/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002159 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002160 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002161 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002162 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2163 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002164 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002165 * @note Can be called by ISRs.
2166 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002167 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002168 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002169 *
2170 * @return N/A
2171 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002172#define k_lifo_put(lifo, data) \
2173 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002174
2175/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002176 * @brief Add an element to a LIFO queue.
2177 *
2178 * This routine adds a data item to @a lifo. There is an implicit
2179 * memory allocation from the calling thread's resource pool, which is
2180 * automatically freed when the item is removed.
2181 *
2182 * @note Can be called by ISRs.
2183 *
2184 * @param lifo Address of the LIFO.
2185 * @param data Address of the data item.
2186 *
2187 * @retval 0 on success
2188 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
2189 */
2190#define k_lifo_alloc_put(lifo, data) \
2191 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2192
2193/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002194 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002195 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002196 * This routine removes a data item from @a lifo in a "last in, first out"
2197 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002198 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002199 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2200 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002201 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002202 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002203 * or one of the special values K_NO_WAIT and K_FOREVER.
2204 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002205 * @return Address of the data item if successful; NULL if returned
2206 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002207 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002208#define k_lifo_get(lifo, timeout) \
2209 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002210
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002211/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002212 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002213 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002214 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002215 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002216 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002217 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002218 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002219 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002220#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002221 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002222 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002223 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002224
Anas Nashif166f5192018-02-25 08:02:36 -06002225/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002226
2227/**
2228 * @cond INTERNAL_HIDDEN
2229 */
Andrew Boief3bee952018-05-02 17:44:39 -07002230#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002231
2232struct k_stack {
2233 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002234 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002235
Anas Nashif2f203c22016-12-18 06:57:45 -05002236 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002237 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002238};
2239
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002240#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002241 { \
2242 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2243 .base = stack_buffer, \
2244 .next = stack_buffer, \
2245 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002246 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002247 }
2248
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002249#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2250
Allan Stephensc98da842016-11-11 15:45:03 -05002251/**
2252 * INTERNAL_HIDDEN @endcond
2253 */
2254
2255/**
2256 * @defgroup stack_apis Stack APIs
2257 * @ingroup kernel_apis
2258 * @{
2259 */
2260
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002261/**
2262 * @brief Initialize a stack.
2263 *
2264 * This routine initializes a stack object, prior to its first use.
2265 *
2266 * @param stack Address of the stack.
2267 * @param buffer Address of array used to hold stacked values.
2268 * @param num_entries Maximum number of values that can be stacked.
2269 *
2270 * @return N/A
2271 */
Andrew Boief3bee952018-05-02 17:44:39 -07002272void k_stack_init(struct k_stack *stack,
2273 u32_t *buffer, unsigned int num_entries);
2274
2275
2276/**
2277 * @brief Initialize a stack.
2278 *
2279 * This routine initializes a stack object, prior to its first use. Internal
2280 * buffers will be allocated from the calling thread's resource pool.
2281 * This memory will be released if k_stack_cleanup() is called, or
2282 * userspace is enabled and the stack object loses all references to it.
2283 *
2284 * @param stack Address of the stack.
2285 * @param num_entries Maximum number of values that can be stacked.
2286 *
2287 * @return -ENOMEM if memory couldn't be allocated
2288 */
2289
2290__syscall int k_stack_alloc_init(struct k_stack *stack,
2291 unsigned int num_entries);
2292
2293/**
2294 * @brief Release a stack's allocated buffer
2295 *
2296 * If a stack object was given a dynamically allocated buffer via
2297 * k_stack_alloc_init(), this will free it. This function does nothing
2298 * if the buffer wasn't dynamically allocated.
2299 *
2300 * @param stack Address of the stack.
2301 */
2302void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002303
2304/**
2305 * @brief Push an element onto a stack.
2306 *
2307 * This routine adds a 32-bit value @a data to @a stack.
2308 *
2309 * @note Can be called by ISRs.
2310 *
2311 * @param stack Address of the stack.
2312 * @param data Value to push onto the stack.
2313 *
2314 * @return N/A
2315 */
Andrew Boiee8734462017-09-29 16:42:07 -07002316__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002317
2318/**
2319 * @brief Pop an element from a stack.
2320 *
2321 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2322 * manner and stores the value in @a data.
2323 *
2324 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2325 *
2326 * @param stack Address of the stack.
2327 * @param data Address of area to hold the value popped from the stack.
2328 * @param timeout Waiting period to obtain a value (in milliseconds),
2329 * or one of the special values K_NO_WAIT and K_FOREVER.
2330 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002331 * @retval 0 Element popped from stack.
2332 * @retval -EBUSY Returned without waiting.
2333 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002334 */
Andrew Boiee8734462017-09-29 16:42:07 -07002335__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002336
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002337/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002338 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002339 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002340 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002341 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002342 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002343 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002344 * @param name Name of the stack.
2345 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002346 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002347#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002348 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002349 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002350 struct k_stack name \
2351 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002352 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002353 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002354
Anas Nashif166f5192018-02-25 08:02:36 -06002355/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002356
Allan Stephens6bba9b02016-11-16 14:56:54 -05002357struct k_work;
2358
Allan Stephensc98da842016-11-11 15:45:03 -05002359/**
2360 * @defgroup workqueue_apis Workqueue Thread APIs
2361 * @ingroup kernel_apis
2362 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002363 */
2364
Allan Stephens6bba9b02016-11-16 14:56:54 -05002365/**
2366 * @typedef k_work_handler_t
2367 * @brief Work item handler function type.
2368 *
2369 * A work item's handler function is executed by a workqueue's thread
2370 * when the work item is processed by the workqueue.
2371 *
2372 * @param work Address of the work item.
2373 *
2374 * @return N/A
2375 */
2376typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002377
2378/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002379 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002380 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002381
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002382struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002383 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002384 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002385};
2386
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002387enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002388 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002389};
2390
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002391struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002392 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002393 k_work_handler_t handler;
2394 atomic_t flags[1];
2395};
2396
Allan Stephens6bba9b02016-11-16 14:56:54 -05002397struct k_delayed_work {
2398 struct k_work work;
2399 struct _timeout timeout;
2400 struct k_work_q *work_q;
2401};
2402
2403extern struct k_work_q k_sys_work_q;
2404
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002405/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002406 * INTERNAL_HIDDEN @endcond
2407 */
2408
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002409#define _K_WORK_INITIALIZER(work_handler) \
2410 { \
2411 ._reserved = NULL, \
2412 .handler = work_handler, \
2413 .flags = { 0 } \
2414 }
2415
2416#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2417
Allan Stephens6bba9b02016-11-16 14:56:54 -05002418/**
2419 * @brief Initialize a statically-defined work item.
2420 *
2421 * This macro can be used to initialize a statically-defined workqueue work
2422 * item, prior to its first use. For example,
2423 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002424 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002425 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002426 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002427 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002428 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002429#define K_WORK_DEFINE(work, work_handler) \
2430 struct k_work work \
2431 __in_section(_k_work, static, work) = \
2432 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002433
2434/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002435 * @brief Initialize a work item.
2436 *
2437 * This routine initializes a workqueue work item, prior to its first use.
2438 *
2439 * @param work Address of work item.
2440 * @param handler Function to invoke each time work item is processed.
2441 *
2442 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002443 */
2444static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2445{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002446 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002447 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002448 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002449}
2450
2451/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002452 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002453 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002454 * This routine submits work item @a work to be processed by workqueue
2455 * @a work_q. If the work item is already pending in the workqueue's queue
2456 * as a result of an earlier submission, this routine has no effect on the
2457 * work item. If the work item has already been processed, or is currently
2458 * being processed, its work is considered complete and the work item can be
2459 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002460 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002461 * @warning
2462 * A submitted work item must not be modified until it has been processed
2463 * by the workqueue.
2464 *
2465 * @note Can be called by ISRs.
2466 *
2467 * @param work_q Address of workqueue.
2468 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002469 *
2470 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002471 */
2472static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2473 struct k_work *work)
2474{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002475 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002476 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002477 }
2478}
2479
2480/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002481 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002482 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002483 * This routine indicates if work item @a work is pending in a workqueue's
2484 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002485 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002486 * @note Can be called by ISRs.
2487 *
2488 * @param work Address of work item.
2489 *
2490 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002491 */
2492static inline int k_work_pending(struct k_work *work)
2493{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002494 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002495}
2496
2497/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002498 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002499 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002500 * This routine starts workqueue @a work_q. The workqueue spawns its work
2501 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002502 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002503 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002504 * @param stack Pointer to work queue thread's stack space, as defined by
2505 * K_THREAD_STACK_DEFINE()
2506 * @param stack_size Size of the work queue thread's stack (in bytes), which
2507 * should either be the same constant passed to
2508 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002509 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002510 *
2511 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002512 */
Andrew Boie507852a2017-07-25 18:47:07 -07002513extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002514 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002515 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002516
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002517/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002518 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002519 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002520 * This routine initializes a workqueue delayed work item, prior to
2521 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002522 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002523 * @param work Address of delayed work item.
2524 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002525 *
2526 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002527 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002528extern void k_delayed_work_init(struct k_delayed_work *work,
2529 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002530
2531/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002532 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002533 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002534 * This routine schedules work item @a work to be processed by workqueue
2535 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002536 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002537 * Only when the countdown completes is the work item actually submitted to
2538 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002539 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002540 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002541 * counting down cancels the existing submission and restarts the
2542 * countdown using the new delay. Note that this behavior is
2543 * inherently subject to race conditions with the pre-existing
2544 * timeouts and work queue, so care must be taken to synchronize such
2545 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002546 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002547 * @warning
2548 * A delayed work item must not be modified until it has been processed
2549 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002550 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002551 * @note Can be called by ISRs.
2552 *
2553 * @param work_q Address of workqueue.
2554 * @param work Address of delayed work item.
2555 * @param delay Delay before submitting the work item (in milliseconds).
2556 *
2557 * @retval 0 Work item countdown started.
2558 * @retval -EINPROGRESS Work item is already pending.
2559 * @retval -EINVAL Work item is being processed or has completed its work.
2560 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002561 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002562extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2563 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002564 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002565
2566/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002567 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002568 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002569 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002570 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002571 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002572 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002573 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002574 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002575 * @param work Address of delayed work item.
2576 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002577 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002578 * @retval -EINPROGRESS Work item is already pending.
2579 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002580 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002581extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002582
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002583/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002584 * @brief Submit a work item to the system workqueue.
2585 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002586 * This routine submits work item @a work to be processed by the system
2587 * workqueue. If the work item is already pending in the workqueue's queue
2588 * as a result of an earlier submission, this routine has no effect on the
2589 * work item. If the work item has already been processed, or is currently
2590 * being processed, its work is considered complete and the work item can be
2591 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002592 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002593 * @warning
2594 * Work items submitted to the system workqueue should avoid using handlers
2595 * that block or yield since this may prevent the system workqueue from
2596 * processing other work items in a timely manner.
2597 *
2598 * @note Can be called by ISRs.
2599 *
2600 * @param work Address of work item.
2601 *
2602 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002603 */
2604static inline void k_work_submit(struct k_work *work)
2605{
2606 k_work_submit_to_queue(&k_sys_work_q, work);
2607}
2608
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002609/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002610 * @brief Submit a delayed work item to the system workqueue.
2611 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002612 * This routine schedules work item @a work to be processed by the system
2613 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002614 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002615 * Only when the countdown completes is the work item actually submitted to
2616 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002617 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002618 * Submitting a previously submitted delayed work item that is still
2619 * counting down cancels the existing submission and restarts the countdown
2620 * using the new delay. If the work item is currently pending on the
2621 * workqueue's queue because the countdown has completed it is too late to
2622 * resubmit the item, and resubmission fails without impacting the work item.
2623 * If the work item has already been processed, or is currently being processed,
2624 * its work is considered complete and the work item can be resubmitted.
2625 *
2626 * @warning
2627 * Work items submitted to the system workqueue should avoid using handlers
2628 * that block or yield since this may prevent the system workqueue from
2629 * processing other work items in a timely manner.
2630 *
2631 * @note Can be called by ISRs.
2632 *
2633 * @param work Address of delayed work item.
2634 * @param delay Delay before submitting the work item (in milliseconds).
2635 *
2636 * @retval 0 Work item countdown started.
2637 * @retval -EINPROGRESS Work item is already pending.
2638 * @retval -EINVAL Work item is being processed or has completed its work.
2639 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002640 */
2641static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002642 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002643{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002644 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002645}
2646
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002647/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002648 * @brief Get time remaining before a delayed work gets scheduled.
2649 *
2650 * This routine computes the (approximate) time remaining before a
2651 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002652 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002653 *
2654 * @param work Delayed work item.
2655 *
2656 * @return Remaining time (in milliseconds).
2657 */
Kumar Galacc334c72017-04-21 10:55:34 -05002658static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002659{
2660 return _timeout_remaining_get(&work->timeout);
2661}
2662
Anas Nashif166f5192018-02-25 08:02:36 -06002663/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002664
Allan Stephensc98da842016-11-11 15:45:03 -05002665/**
2666 * @cond INTERNAL_HIDDEN
2667 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002668
2669struct k_mutex {
2670 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002671 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002672 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002673 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002674
Anas Nashif2f203c22016-12-18 06:57:45 -05002675 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002676};
2677
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002678#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002679 { \
2680 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2681 .owner = NULL, \
2682 .lock_count = 0, \
2683 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002684 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002685 }
2686
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002687#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2688
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002689/**
Allan Stephensc98da842016-11-11 15:45:03 -05002690 * INTERNAL_HIDDEN @endcond
2691 */
2692
2693/**
2694 * @defgroup mutex_apis Mutex APIs
2695 * @ingroup kernel_apis
2696 * @{
2697 */
2698
2699/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002700 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002702 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002703 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002704 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002706 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002707 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002708#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002709 struct k_mutex name \
2710 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002711 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002712
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002713/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002714 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002715 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002716 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002717 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002718 * Upon completion, the mutex is available and does not have an owner.
2719 *
2720 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002721 *
2722 * @return N/A
2723 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002724__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002725
2726/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002727 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002728 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002729 * This routine locks @a mutex. If the mutex is locked by another thread,
2730 * the calling thread waits until the mutex becomes available or until
2731 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002732 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002733 * A thread is permitted to lock a mutex it has already locked. The operation
2734 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002735 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002736 * @param mutex Address of the mutex.
2737 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002738 * or one of the special values K_NO_WAIT and K_FOREVER.
2739 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002740 * @retval 0 Mutex locked.
2741 * @retval -EBUSY Returned without waiting.
2742 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002743 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002744__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002745
2746/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002747 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002748 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002749 * This routine unlocks @a mutex. The mutex must already be locked by the
2750 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002751 *
2752 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002753 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002754 * thread.
2755 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002756 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002757 *
2758 * @return N/A
2759 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002760__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002761
Allan Stephensc98da842016-11-11 15:45:03 -05002762/**
Anas Nashif166f5192018-02-25 08:02:36 -06002763 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002764 */
2765
2766/**
2767 * @cond INTERNAL_HIDDEN
2768 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002769
2770struct k_sem {
2771 _wait_q_t wait_q;
2772 unsigned int count;
2773 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002774 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002775
Anas Nashif2f203c22016-12-18 06:57:45 -05002776 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002777};
2778
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002779#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002780 { \
2781 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2782 .count = initial_count, \
2783 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002784 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002785 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002786 }
2787
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002788#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2789
Allan Stephensc98da842016-11-11 15:45:03 -05002790/**
2791 * INTERNAL_HIDDEN @endcond
2792 */
2793
2794/**
2795 * @defgroup semaphore_apis Semaphore APIs
2796 * @ingroup kernel_apis
2797 * @{
2798 */
2799
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002800/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002801 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002802 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002803 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002804 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002805 * @param sem Address of the semaphore.
2806 * @param initial_count Initial semaphore count.
2807 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002808 *
2809 * @return N/A
2810 */
Andrew Boie99280232017-09-29 14:17:47 -07002811__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2812 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002813
2814/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002815 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002817 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002819 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2820 *
2821 * @param sem Address of the semaphore.
2822 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002823 * or one of the special values K_NO_WAIT and K_FOREVER.
2824 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002825 * @note When porting code from the nanokernel legacy API to the new API, be
2826 * careful with the return value of this function. The return value is the
2827 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2828 * non-zero means failure, while the nano_sem_take family returns 1 for success
2829 * and 0 for failure.
2830 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002831 * @retval 0 Semaphore taken.
2832 * @retval -EBUSY Returned without waiting.
2833 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002834 */
Andrew Boie99280232017-09-29 14:17:47 -07002835__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002836
2837/**
2838 * @brief Give a semaphore.
2839 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002840 * This routine gives @a sem, unless the semaphore is already at its maximum
2841 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002842 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002843 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002845 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002846 *
2847 * @return N/A
2848 */
Andrew Boie99280232017-09-29 14:17:47 -07002849__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002850
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002851/**
2852 * @brief Reset a semaphore's count to zero.
2853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002854 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002856 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002857 *
2858 * @return N/A
2859 */
Andrew Boie990bf162017-10-03 12:36:49 -07002860__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002861
Anas Nashif954d5502018-02-25 08:37:28 -06002862/**
2863 * @internal
2864 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002865static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002866{
2867 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002868}
2869
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002870/**
2871 * @brief Get a semaphore's count.
2872 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002873 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002875 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002877 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002878 */
Andrew Boie990bf162017-10-03 12:36:49 -07002879__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002880
Anas Nashif954d5502018-02-25 08:37:28 -06002881/**
2882 * @internal
2883 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002884static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002885{
2886 return sem->count;
2887}
2888
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002889/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002890 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002892 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002893 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002894 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002895 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002896 * @param name Name of the semaphore.
2897 * @param initial_count Initial semaphore count.
2898 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002899 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002900#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002901 struct k_sem name \
2902 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07002903 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05302904 BUILD_ASSERT(((count_limit) != 0) && \
2905 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002906
Anas Nashif166f5192018-02-25 08:02:36 -06002907/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002908
2909/**
2910 * @defgroup alert_apis Alert APIs
2911 * @ingroup kernel_apis
2912 * @{
2913 */
2914
Allan Stephens5eceb852016-11-16 10:16:30 -05002915/**
2916 * @typedef k_alert_handler_t
2917 * @brief Alert handler function type.
2918 *
2919 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002920 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002921 * and is only invoked if the alert has been initialized with one.
2922 *
2923 * @param alert Address of the alert.
2924 *
2925 * @return 0 if alert has been consumed; non-zero if alert should pend.
2926 */
2927typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002928
Anas Nashif166f5192018-02-25 08:02:36 -06002929/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002930
2931/**
2932 * @cond INTERNAL_HIDDEN
2933 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002934
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002935#define K_ALERT_DEFAULT NULL
2936#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002937
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002938struct k_alert {
2939 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002940 atomic_t send_count;
2941 struct k_work work_item;
2942 struct k_sem sem;
2943
Anas Nashif2f203c22016-12-18 06:57:45 -05002944 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002945};
2946
Anas Nashif954d5502018-02-25 08:37:28 -06002947/**
2948 * @internal
2949 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002950extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002951
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002952#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002953 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002954 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002955 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002956 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2957 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002958 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002959 }
2960
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002961#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2962
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002963/**
Allan Stephensc98da842016-11-11 15:45:03 -05002964 * INTERNAL_HIDDEN @endcond
2965 */
2966
2967/**
2968 * @addtogroup alert_apis
2969 * @{
2970 */
2971
2972/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002973 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002974 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002975 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002976 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002977 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002979 * @param name Name of the alert.
2980 * @param alert_handler Action to take when alert is sent. Specify either
2981 * the address of a function to be invoked by the system workqueue
2982 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2983 * K_ALERT_DEFAULT (which causes the alert to pend).
2984 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002985 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002986#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002987 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002988 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002989 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002990 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002991
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002992/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002993 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002994 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002995 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002996 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002997 * @param alert Address of the alert.
2998 * @param handler Action to take when alert is sent. Specify either the address
2999 * of a function to be invoked by the system workqueue thread,
3000 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3001 * K_ALERT_DEFAULT (which causes the alert to pend).
3002 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003003 *
3004 * @return N/A
3005 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003006extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3007 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003008
3009/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003010 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003011 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003012 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003014 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3015 *
3016 * @param alert Address of the alert.
3017 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003018 * or one of the special values K_NO_WAIT and K_FOREVER.
3019 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003020 * @retval 0 Alert received.
3021 * @retval -EBUSY Returned without waiting.
3022 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003023 */
Andrew Boie310e9872017-09-29 04:41:15 -07003024__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003025
3026/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003027 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003028 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003029 * This routine signals @a alert. The action specified for @a alert will
3030 * be taken, which may trigger the execution of an alert handler function
3031 * and/or cause the alert to pend (assuming the alert has not reached its
3032 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003033 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003034 * @note Can be called by ISRs.
3035 *
3036 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003037 *
3038 * @return N/A
3039 */
Andrew Boie310e9872017-09-29 04:41:15 -07003040__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003041
3042/**
Anas Nashif166f5192018-02-25 08:02:36 -06003043 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003044 */
3045
Allan Stephensc98da842016-11-11 15:45:03 -05003046/**
3047 * @cond INTERNAL_HIDDEN
3048 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003049
3050struct k_msgq {
3051 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003052 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003053 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003054 char *buffer_start;
3055 char *buffer_end;
3056 char *read_ptr;
3057 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003058 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003059
Anas Nashif2f203c22016-12-18 06:57:45 -05003060 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003061 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003062};
3063
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003064#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003065 { \
3066 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003067 .max_msgs = q_max_msgs, \
3068 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003069 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003070 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003071 .read_ptr = q_buffer, \
3072 .write_ptr = q_buffer, \
3073 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003074 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003075 }
3076
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003077#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
3078
Andrew Boie0fe789f2018-04-12 18:35:56 -07003079#define K_MSGQ_FLAG_ALLOC BIT(0)
3080
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303081struct k_msgq_attrs {
3082 size_t msg_size;
3083 u32_t max_msgs;
3084 u32_t used_msgs;
3085};
3086
Peter Mitsis1da807e2016-10-06 11:36:59 -04003087/**
Allan Stephensc98da842016-11-11 15:45:03 -05003088 * INTERNAL_HIDDEN @endcond
3089 */
3090
3091/**
3092 * @defgroup msgq_apis Message Queue APIs
3093 * @ingroup kernel_apis
3094 * @{
3095 */
3096
3097/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003098 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003099 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003100 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3101 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003102 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3103 * message is similarly aligned to this boundary, @a q_msg_size must also be
3104 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003105 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003106 * The message queue can be accessed outside the module where it is defined
3107 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003108 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003109 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003110 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003111 * @param q_name Name of the message queue.
3112 * @param q_msg_size Message size (in bytes).
3113 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003114 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003115 */
3116#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003117 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003118 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003119 struct k_msgq q_name \
3120 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003121 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003122 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003123
Peter Mitsisd7a37502016-10-13 11:37:40 -04003124/**
3125 * @brief Initialize a message queue.
3126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003127 * This routine initializes a message queue object, prior to its first use.
3128 *
Allan Stephensda827222016-11-09 14:23:58 -06003129 * The message queue's ring buffer must contain space for @a max_msgs messages,
3130 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3131 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3132 * that each message is similarly aligned to this boundary, @a q_msg_size
3133 * must also be a multiple of N.
3134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003135 * @param q Address of the message queue.
3136 * @param buffer Pointer to ring buffer that holds queued messages.
3137 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003138 * @param max_msgs Maximum number of messages that can be queued.
3139 *
3140 * @return N/A
3141 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003142void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3143 u32_t max_msgs);
3144
3145/**
3146 * @brief Initialize a message queue.
3147 *
3148 * This routine initializes a message queue object, prior to its first use,
3149 * allocating its internal ring buffer from the calling thread's resource
3150 * pool.
3151 *
3152 * Memory allocated for the ring buffer can be released by calling
3153 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3154 * all of its references.
3155 *
3156 * @param q Address of the message queue.
3157 * @param msg_size Message size (in bytes).
3158 * @param max_msgs Maximum number of messages that can be queued.
3159 *
3160 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3161 * thread's resource pool, or -EINVAL if the size parameters cause
3162 * an integer overflow.
3163 */
3164__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3165 u32_t max_msgs);
3166
3167
3168void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003169
3170/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003171 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003172 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003173 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003174 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003175 * @note Can be called by ISRs.
3176 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003177 * @param q Address of the message queue.
3178 * @param data Pointer to the message.
3179 * @param timeout Waiting period to add the message (in milliseconds),
3180 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003181 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003182 * @retval 0 Message sent.
3183 * @retval -ENOMSG Returned without waiting or queue purged.
3184 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003185 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003186__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003187
3188/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003189 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003190 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003191 * This routine receives a message from message queue @a q in a "first in,
3192 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003193 *
Allan Stephensc98da842016-11-11 15:45:03 -05003194 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003195 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003196 * @param q Address of the message queue.
3197 * @param data Address of area to hold the received message.
3198 * @param timeout Waiting period to receive the message (in milliseconds),
3199 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003200 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003201 * @retval 0 Message received.
3202 * @retval -ENOMSG Returned without waiting.
3203 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003204 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003205__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003206
3207/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003208 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003209 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003210 * This routine discards all unreceived messages in a message queue's ring
3211 * buffer. Any threads that are blocked waiting to send a message to the
3212 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003213 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003214 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003215 *
3216 * @return N/A
3217 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003218__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003219
Peter Mitsis67be2492016-10-07 11:44:34 -04003220/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003221 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003222 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003223 * This routine returns the number of unused entries in a message queue's
3224 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003225 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003226 * @param q Address of the message queue.
3227 *
3228 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04003229 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003230__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3231
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303232/**
3233 * @brief Get basic attributes of a message queue.
3234 *
3235 * This routine fetches basic attributes of message queue into attr argument.
3236 *
3237 * @param q Address of the message queue.
3238 * @param attrs pointer to message queue attribute structure.
3239 *
3240 * @return N/A
3241 */
3242__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3243
3244
Andrew Boie82edb6e2017-10-02 10:53:06 -07003245static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003246{
3247 return q->max_msgs - q->used_msgs;
3248}
3249
Peter Mitsisd7a37502016-10-13 11:37:40 -04003250/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003251 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003252 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003253 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003255 * @param q Address of the message queue.
3256 *
3257 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003258 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003259__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3260
3261static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003262{
3263 return q->used_msgs;
3264}
3265
Anas Nashif166f5192018-02-25 08:02:36 -06003266/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003267
3268/**
3269 * @defgroup mem_pool_apis Memory Pool APIs
3270 * @ingroup kernel_apis
3271 * @{
3272 */
3273
Andy Ross73cb9582017-05-09 10:42:39 -07003274/* Note on sizing: the use of a 20 bit field for block means that,
3275 * assuming a reasonable minimum block size of 16 bytes, we're limited
3276 * to 16M of memory managed by a single pool. Long term it would be
3277 * good to move to a variable bit size based on configuration.
3278 */
3279struct k_mem_block_id {
3280 u32_t pool : 8;
3281 u32_t level : 4;
3282 u32_t block : 20;
3283};
3284
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003285struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003286 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003287 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003288};
3289
Anas Nashif166f5192018-02-25 08:02:36 -06003290/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003291
3292/**
3293 * @defgroup mailbox_apis Mailbox APIs
3294 * @ingroup kernel_apis
3295 * @{
3296 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003297
3298struct k_mbox_msg {
3299 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003300 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003301 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003302 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003303 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003304 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003305 /** sender's message data buffer */
3306 void *tx_data;
3307 /** internal use only - needed for legacy API support */
3308 void *_rx_data;
3309 /** message data block descriptor */
3310 struct k_mem_block tx_block;
3311 /** source thread id */
3312 k_tid_t rx_source_thread;
3313 /** target thread id */
3314 k_tid_t tx_target_thread;
3315 /** internal use only - thread waiting on send (may be a dummy) */
3316 k_tid_t _syncing_thread;
3317#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3318 /** internal use only - semaphore used during asynchronous send */
3319 struct k_sem *_async_sem;
3320#endif
3321};
3322
Allan Stephensc98da842016-11-11 15:45:03 -05003323/**
3324 * @cond INTERNAL_HIDDEN
3325 */
3326
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003327struct k_mbox {
3328 _wait_q_t tx_msg_queue;
3329 _wait_q_t rx_msg_queue;
3330
Anas Nashif2f203c22016-12-18 06:57:45 -05003331 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003332};
3333
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003334#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003335 { \
3336 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3337 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003338 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003339 }
3340
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003341#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3342
Peter Mitsis12092702016-10-14 12:57:23 -04003343/**
Allan Stephensc98da842016-11-11 15:45:03 -05003344 * INTERNAL_HIDDEN @endcond
3345 */
3346
3347/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003348 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003350 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003351 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003352 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003353 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003354 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003355 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003356#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003357 struct k_mbox name \
3358 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003359 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003360
Peter Mitsis12092702016-10-14 12:57:23 -04003361/**
3362 * @brief Initialize a mailbox.
3363 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003364 * This routine initializes a mailbox object, prior to its first use.
3365 *
3366 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003367 *
3368 * @return N/A
3369 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003370extern void k_mbox_init(struct k_mbox *mbox);
3371
Peter Mitsis12092702016-10-14 12:57:23 -04003372/**
3373 * @brief Send a mailbox message in a synchronous manner.
3374 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003375 * This routine sends a message to @a mbox and waits for a receiver to both
3376 * receive and process it. The message data may be in a buffer, in a memory
3377 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003378 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003379 * @param mbox Address of the mailbox.
3380 * @param tx_msg Address of the transmit message descriptor.
3381 * @param timeout Waiting period for the message to be received (in
3382 * milliseconds), or one of the special values K_NO_WAIT
3383 * and K_FOREVER. Once the message has been received,
3384 * this routine waits as long as necessary for the message
3385 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003386 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003387 * @retval 0 Message sent.
3388 * @retval -ENOMSG Returned without waiting.
3389 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003390 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003391extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003392 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003393
Peter Mitsis12092702016-10-14 12:57:23 -04003394/**
3395 * @brief Send a mailbox message in an asynchronous manner.
3396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003397 * This routine sends a message to @a mbox without waiting for a receiver
3398 * to process it. The message data may be in a buffer, in a memory pool block,
3399 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3400 * will be given when the message has been both received and completely
3401 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003402 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003403 * @param mbox Address of the mailbox.
3404 * @param tx_msg Address of the transmit message descriptor.
3405 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003406 *
3407 * @return N/A
3408 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003409extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003410 struct k_sem *sem);
3411
Peter Mitsis12092702016-10-14 12:57:23 -04003412/**
3413 * @brief Receive a mailbox message.
3414 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003415 * This routine receives a message from @a mbox, then optionally retrieves
3416 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003418 * @param mbox Address of the mailbox.
3419 * @param rx_msg Address of the receive message descriptor.
3420 * @param buffer Address of the buffer to receive data, or NULL to defer data
3421 * retrieval and message disposal until later.
3422 * @param timeout Waiting period for a message to be received (in
3423 * milliseconds), or one of the special values K_NO_WAIT
3424 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003425 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003426 * @retval 0 Message received.
3427 * @retval -ENOMSG Returned without waiting.
3428 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003429 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003430extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003431 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003432
3433/**
3434 * @brief Retrieve mailbox message data into a buffer.
3435 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003436 * This routine completes the processing of a received message by retrieving
3437 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003438 *
3439 * Alternatively, this routine can be used to dispose of a received message
3440 * without retrieving its data.
3441 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003442 * @param rx_msg Address of the receive message descriptor.
3443 * @param buffer Address of the buffer to receive data, or NULL to discard
3444 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003445 *
3446 * @return N/A
3447 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003448extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003449
3450/**
3451 * @brief Retrieve mailbox message data into a memory pool block.
3452 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003453 * This routine completes the processing of a received message by retrieving
3454 * its data into a memory pool block, then disposing of the message.
3455 * The memory pool block that results from successful retrieval must be
3456 * returned to the pool once the data has been processed, even in cases
3457 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003458 *
3459 * Alternatively, this routine can be used to dispose of a received message
3460 * without retrieving its data. In this case there is no need to return a
3461 * memory pool block to the pool.
3462 *
3463 * This routine allocates a new memory pool block for the data only if the
3464 * data is not already in one. If a new block cannot be allocated, the routine
3465 * returns a failure code and the received message is left unchanged. This
3466 * permits the caller to reattempt data retrieval at a later time or to dispose
3467 * of the received message without retrieving its data.
3468 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003469 * @param rx_msg Address of a receive message descriptor.
3470 * @param pool Address of memory pool, or NULL to discard data.
3471 * @param block Address of the area to hold memory pool block info.
3472 * @param timeout Waiting period to wait for a memory pool block (in
3473 * milliseconds), or one of the special values K_NO_WAIT
3474 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003475 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003476 * @retval 0 Data retrieved.
3477 * @retval -ENOMEM Returned without waiting.
3478 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003479 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003480extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003481 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003482 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003483
Anas Nashif166f5192018-02-25 08:02:36 -06003484/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003485
3486/**
3487 * @cond INTERNAL_HIDDEN
3488 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003489
Andrew Boie44fe8122018-04-12 17:38:12 -07003490#define K_PIPE_FLAG_ALLOC BIT(0) /* Buffer was allocated */
3491
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003492struct k_pipe {
3493 unsigned char *buffer; /* Pipe buffer: may be NULL */
3494 size_t size; /* Buffer size */
3495 size_t bytes_used; /* # bytes used in buffer */
3496 size_t read_index; /* Where in buffer to read from */
3497 size_t write_index; /* Where in buffer to write */
3498
3499 struct {
3500 _wait_q_t readers; /* Reader wait queue */
3501 _wait_q_t writers; /* Writer wait queue */
3502 } wait_q;
3503
Anas Nashif2f203c22016-12-18 06:57:45 -05003504 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Andrew Boie44fe8122018-04-12 17:38:12 -07003505 u8_t flags; /* Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003506};
3507
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003508#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003509 { \
3510 .buffer = pipe_buffer, \
3511 .size = pipe_buffer_size, \
3512 .bytes_used = 0, \
3513 .read_index = 0, \
3514 .write_index = 0, \
3515 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3516 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003517 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003518 }
3519
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003520#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3521
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003522/**
Allan Stephensc98da842016-11-11 15:45:03 -05003523 * INTERNAL_HIDDEN @endcond
3524 */
3525
3526/**
3527 * @defgroup pipe_apis Pipe APIs
3528 * @ingroup kernel_apis
3529 * @{
3530 */
3531
3532/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003533 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003534 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003535 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003536 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003537 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003539 * @param name Name of the pipe.
3540 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3541 * or zero if no ring buffer is used.
3542 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003543 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003544#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3545 static unsigned char __kernel_noinit __aligned(pipe_align) \
3546 _k_pipe_buf_##name[pipe_buffer_size]; \
3547 struct k_pipe name \
3548 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003549 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003550
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003551/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003552 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003553 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003554 * This routine initializes a pipe object, prior to its first use.
3555 *
3556 * @param pipe Address of the pipe.
3557 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3558 * is used.
3559 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3560 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003561 *
3562 * @return N/A
3563 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003564void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3565
3566/**
3567 * @brief Release a pipe's allocated buffer
3568 *
3569 * If a pipe object was given a dynamically allocated buffer via
3570 * k_pipe_alloc_init(), this will free it. This function does nothing
3571 * if the buffer wasn't dynamically allocated.
3572 *
3573 * @param pipe Address of the pipe.
3574 */
3575void k_pipe_cleanup(struct k_pipe *pipe);
3576
3577/**
3578 * @brief Initialize a pipe and allocate a buffer for it
3579 *
3580 * Storage for the buffer region will be allocated from the calling thread's
3581 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3582 * or userspace is enabled and the pipe object loses all references to it.
3583 *
3584 * This function should only be called on uninitialized pipe objects.
3585 *
3586 * @param pipe Address of the pipe.
3587 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3588 * buffer is used.
3589 * @retval 0 on success
3590 * @retval -ENOMEM if memory couln't be allocated
3591 */
3592__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003593
3594/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003595 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003596 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003597 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003598 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003599 * @param pipe Address of the pipe.
3600 * @param data Address of data to write.
3601 * @param bytes_to_write Size of data (in bytes).
3602 * @param bytes_written Address of area to hold the number of bytes written.
3603 * @param min_xfer Minimum number of bytes to write.
3604 * @param timeout Waiting period to wait for the data to be written (in
3605 * milliseconds), or one of the special values K_NO_WAIT
3606 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003607 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003608 * @retval 0 At least @a min_xfer bytes of data were written.
3609 * @retval -EIO Returned without waiting; zero data bytes were written.
3610 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003611 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003612 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003613__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3614 size_t bytes_to_write, size_t *bytes_written,
3615 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003616
3617/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003618 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003619 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003620 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003622 * @param pipe Address of the pipe.
3623 * @param data Address to place the data read from pipe.
3624 * @param bytes_to_read Maximum number of data bytes to read.
3625 * @param bytes_read Address of area to hold the number of bytes read.
3626 * @param min_xfer Minimum number of data bytes to read.
3627 * @param timeout Waiting period to wait for the data to be read (in
3628 * milliseconds), or one of the special values K_NO_WAIT
3629 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003630 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003631 * @retval 0 At least @a min_xfer bytes of data were read.
3632 * @retval -EIO Returned without waiting; zero data bytes were read.
3633 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003634 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003635 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003636__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3637 size_t bytes_to_read, size_t *bytes_read,
3638 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003639
3640/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003641 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003642 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003643 * This routine writes the data contained in a memory block to @a pipe.
3644 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003645 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003646 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003647 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003648 * @param block Memory block containing data to send
3649 * @param size Number of data bytes in memory block to send
3650 * @param sem Semaphore to signal upon completion (else NULL)
3651 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003652 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003653 */
3654extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3655 size_t size, struct k_sem *sem);
3656
Anas Nashif166f5192018-02-25 08:02:36 -06003657/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003658
Allan Stephensc98da842016-11-11 15:45:03 -05003659/**
3660 * @cond INTERNAL_HIDDEN
3661 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003662
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003663struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003664 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003665 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003666 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003667 char *buffer;
3668 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003669 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003670
Anas Nashif2f203c22016-12-18 06:57:45 -05003671 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003672};
3673
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003674#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003675 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003676 { \
3677 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003678 .num_blocks = slab_num_blocks, \
3679 .block_size = slab_block_size, \
3680 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003681 .free_list = NULL, \
3682 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003683 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003684 }
3685
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003686#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3687
3688
Peter Mitsis578f9112016-10-07 13:50:31 -04003689/**
Allan Stephensc98da842016-11-11 15:45:03 -05003690 * INTERNAL_HIDDEN @endcond
3691 */
3692
3693/**
3694 * @defgroup mem_slab_apis Memory Slab APIs
3695 * @ingroup kernel_apis
3696 * @{
3697 */
3698
3699/**
Allan Stephensda827222016-11-09 14:23:58 -06003700 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003701 *
Allan Stephensda827222016-11-09 14:23:58 -06003702 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003703 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003704 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3705 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003706 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003707 *
Allan Stephensda827222016-11-09 14:23:58 -06003708 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003709 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003710 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003711 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003712 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003713 * @param name Name of the memory slab.
3714 * @param slab_block_size Size of each memory block (in bytes).
3715 * @param slab_num_blocks Number memory blocks.
3716 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003717 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003718#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3719 char __noinit __aligned(slab_align) \
3720 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3721 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003722 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003723 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003724 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003725
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003726/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003727 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003728 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003729 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003730 *
Allan Stephensda827222016-11-09 14:23:58 -06003731 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3732 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3733 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3734 * To ensure that each memory block is similarly aligned to this boundary,
3735 * @a slab_block_size must also be a multiple of N.
3736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003737 * @param slab Address of the memory slab.
3738 * @param buffer Pointer to buffer used for the memory blocks.
3739 * @param block_size Size of each memory block (in bytes).
3740 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003741 *
3742 * @return N/A
3743 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003744extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003745 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003746
3747/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003748 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003749 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003750 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003751 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003752 * @param slab Address of the memory slab.
3753 * @param mem Pointer to block address area.
3754 * @param timeout Maximum time to wait for operation to complete
3755 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3756 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003757 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003758 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003759 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003760 * @retval -ENOMEM Returned without waiting.
3761 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003762 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003763extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003764 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003765
3766/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003767 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003768 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003769 * This routine releases a previously allocated memory block back to its
3770 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003771 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003772 * @param slab Address of the memory slab.
3773 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003774 *
3775 * @return N/A
3776 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003777extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003778
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003779/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003780 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003781 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003782 * This routine gets the number of memory blocks that are currently
3783 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003784 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003785 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003786 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003787 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003788 */
Kumar Galacc334c72017-04-21 10:55:34 -05003789static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003790{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003791 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003792}
3793
Peter Mitsisc001aa82016-10-13 13:53:37 -04003794/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003795 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003796 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003797 * This routine gets the number of memory blocks that are currently
3798 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003800 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003801 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003802 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003803 */
Kumar Galacc334c72017-04-21 10:55:34 -05003804static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003805{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003806 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003807}
3808
Anas Nashif166f5192018-02-25 08:02:36 -06003809/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003810
3811/**
3812 * @cond INTERNAL_HIDDEN
3813 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003814
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003815struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003816 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003817 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003818};
3819
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003820/**
Allan Stephensc98da842016-11-11 15:45:03 -05003821 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003822 */
3823
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003824/**
Allan Stephensc98da842016-11-11 15:45:03 -05003825 * @addtogroup mem_pool_apis
3826 * @{
3827 */
3828
3829/**
3830 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003831 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003832 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3833 * long. The memory pool allows blocks to be repeatedly partitioned into
3834 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003835 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003836 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003837 * If the pool is to be accessed outside the module where it is defined, it
3838 * can be declared via
3839 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003840 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003842 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003843 * @param minsz Size of the smallest blocks in the pool (in bytes).
3844 * @param maxsz Size of the largest blocks in the pool (in bytes).
3845 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003846 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003847 */
Andy Ross73cb9582017-05-09 10:42:39 -07003848#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3849 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3850 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003851 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003852 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003853 .base = { \
3854 .buf = _mpool_buf_##name, \
3855 .max_sz = maxsz, \
3856 .n_max = nmax, \
3857 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3858 .levels = _mpool_lvls_##name, \
3859 .flags = SYS_MEM_POOL_KERNEL \
3860 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003861 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003862
Peter Mitsis937042c2016-10-13 13:18:26 -04003863/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003864 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003866 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003868 * @param pool Address of the memory pool.
3869 * @param block Pointer to block descriptor for the allocated memory.
3870 * @param size Amount of memory to allocate (in bytes).
3871 * @param timeout Maximum time to wait for operation to complete
3872 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3873 * or K_FOREVER to wait as long as necessary.
3874 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003875 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003876 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003877 * @retval -ENOMEM Returned without waiting.
3878 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003879 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003880extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003881 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003882
3883/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07003884 * @brief Allocate memory from a memory pool with malloc() semantics
3885 *
3886 * Such memory must be released using k_free().
3887 *
3888 * @param pool Address of the memory pool.
3889 * @param size Amount of memory to allocate (in bytes).
3890 * @return Address of the allocated memory if successful, otherwise NULL
3891 */
3892extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
3893
3894/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003895 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003897 * This routine releases a previously allocated memory block back to its
3898 * memory pool.
3899 *
3900 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003901 *
3902 * @return N/A
3903 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003904extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003905
3906/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003907 * @brief Free memory allocated from a memory pool.
3908 *
3909 * This routine releases a previously allocated memory block back to its
3910 * memory pool
3911 *
3912 * @param id Memory block identifier.
3913 *
3914 * @return N/A
3915 */
3916extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3917
3918/**
Anas Nashif166f5192018-02-25 08:02:36 -06003919 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003920 */
3921
3922/**
3923 * @defgroup heap_apis Heap Memory Pool APIs
3924 * @ingroup kernel_apis
3925 * @{
3926 */
3927
3928/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003929 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003930 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003931 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003932 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003934 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003935 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003936 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003937 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003938extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003939
3940/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003941 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003942 *
3943 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07003944 * returned must have been allocated from the heap memory pool or
3945 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04003946 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003947 * If @a ptr is NULL, no operation is performed.
3948 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003949 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003950 *
3951 * @return N/A
3952 */
3953extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003954
Allan Stephensc98da842016-11-11 15:45:03 -05003955/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003956 * @brief Allocate memory from heap, array style
3957 *
3958 * This routine provides traditional calloc() semantics. Memory is
3959 * allocated from the heap memory pool and zeroed.
3960 *
3961 * @param nmemb Number of elements in the requested array
3962 * @param size Size of each array element (in bytes).
3963 *
3964 * @return Address of the allocated memory if successful; otherwise NULL.
3965 */
3966extern void *k_calloc(size_t nmemb, size_t size);
3967
Anas Nashif166f5192018-02-25 08:02:36 -06003968/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003969
Benjamin Walshacc68c12017-01-29 18:57:45 -05003970/* polling API - PRIVATE */
3971
Benjamin Walshb0179862017-02-02 16:39:57 -05003972#ifdef CONFIG_POLL
3973#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3974#else
3975#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3976#endif
3977
Benjamin Walshacc68c12017-01-29 18:57:45 -05003978/* private - implementation data created as needed, per-type */
3979struct _poller {
3980 struct k_thread *thread;
3981};
3982
3983/* private - types bit positions */
3984enum _poll_types_bits {
3985 /* can be used to ignore an event */
3986 _POLL_TYPE_IGNORE,
3987
3988 /* to be signaled by k_poll_signal() */
3989 _POLL_TYPE_SIGNAL,
3990
3991 /* semaphore availability */
3992 _POLL_TYPE_SEM_AVAILABLE,
3993
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003994 /* queue/fifo/lifo data availability */
3995 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003996
3997 _POLL_NUM_TYPES
3998};
3999
4000#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4001
4002/* private - states bit positions */
4003enum _poll_states_bits {
4004 /* default state when creating event */
4005 _POLL_STATE_NOT_READY,
4006
Benjamin Walshacc68c12017-01-29 18:57:45 -05004007 /* signaled by k_poll_signal() */
4008 _POLL_STATE_SIGNALED,
4009
4010 /* semaphore is available */
4011 _POLL_STATE_SEM_AVAILABLE,
4012
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004013 /* data is available to read on queue/fifo/lifo */
4014 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004015
4016 _POLL_NUM_STATES
4017};
4018
4019#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4020
4021#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004022 (32 - (0 \
4023 + 8 /* tag */ \
4024 + _POLL_NUM_TYPES \
4025 + _POLL_NUM_STATES \
4026 + 1 /* modes */ \
4027 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004028
Benjamin Walshacc68c12017-01-29 18:57:45 -05004029/* end of polling API - PRIVATE */
4030
4031
4032/**
4033 * @defgroup poll_apis Async polling APIs
4034 * @ingroup kernel_apis
4035 * @{
4036 */
4037
4038/* Public polling API */
4039
4040/* public - values for k_poll_event.type bitfield */
4041#define K_POLL_TYPE_IGNORE 0
4042#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4043#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004044#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4045#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004046
4047/* public - polling modes */
4048enum k_poll_modes {
4049 /* polling thread does not take ownership of objects when available */
4050 K_POLL_MODE_NOTIFY_ONLY = 0,
4051
4052 K_POLL_NUM_MODES
4053};
4054
4055/* public - values for k_poll_event.state bitfield */
4056#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004057#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4058#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004059#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4060#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004061
4062/* public - poll signal object */
4063struct k_poll_signal {
4064 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004065 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004066
4067 /*
4068 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4069 * user resets it to 0.
4070 */
4071 unsigned int signaled;
4072
4073 /* custom result value passed to k_poll_signal() if needed */
4074 int result;
4075};
4076
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004077#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004078 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004079 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004080 .signaled = 0, \
4081 .result = 0, \
4082 }
4083
4084struct k_poll_event {
4085 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004086 sys_dnode_t _node;
4087
4088 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004089 struct _poller *poller;
4090
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004091 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004092 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004093
Benjamin Walshacc68c12017-01-29 18:57:45 -05004094 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004095 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004096
4097 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004098 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004099
4100 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004101 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004102
4103 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004104 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004105
4106 /* per-type data */
4107 union {
4108 void *obj;
4109 struct k_poll_signal *signal;
4110 struct k_sem *sem;
4111 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004112 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004113 };
4114};
4115
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004116#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004117 { \
4118 .poller = NULL, \
4119 .type = event_type, \
4120 .state = K_POLL_STATE_NOT_READY, \
4121 .mode = event_mode, \
4122 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004123 { .obj = event_obj }, \
4124 }
4125
4126#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4127 event_tag) \
4128 { \
4129 .type = event_type, \
4130 .tag = event_tag, \
4131 .state = K_POLL_STATE_NOT_READY, \
4132 .mode = event_mode, \
4133 .unused = 0, \
4134 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004135 }
4136
4137/**
4138 * @brief Initialize one struct k_poll_event instance
4139 *
4140 * After this routine is called on a poll event, the event it ready to be
4141 * placed in an event array to be passed to k_poll().
4142 *
4143 * @param event The event to initialize.
4144 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4145 * values. Only values that apply to the same object being polled
4146 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4147 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004148 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004149 * @param obj Kernel object or poll signal.
4150 *
4151 * @return N/A
4152 */
4153
Kumar Galacc334c72017-04-21 10:55:34 -05004154extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004155 int mode, void *obj);
4156
4157/**
4158 * @brief Wait for one or many of multiple poll events to occur
4159 *
4160 * This routine allows a thread to wait concurrently for one or many of
4161 * multiple poll events to have occurred. Such events can be a kernel object
4162 * being available, like a semaphore, or a poll signal event.
4163 *
4164 * When an event notifies that a kernel object is available, the kernel object
4165 * is not "given" to the thread calling k_poll(): it merely signals the fact
4166 * that the object was available when the k_poll() call was in effect. Also,
4167 * all threads trying to acquire an object the regular way, i.e. by pending on
4168 * the object, have precedence over the thread polling on the object. This
4169 * means that the polling thread will never get the poll event on an object
4170 * until the object becomes available and its pend queue is empty. For this
4171 * reason, the k_poll() call is more effective when the objects being polled
4172 * only have one thread, the polling thread, trying to acquire them.
4173 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004174 * When k_poll() returns 0, the caller should loop on all the events that were
4175 * passed to k_poll() and check the state field for the values that were
4176 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004177 *
4178 * Before being reused for another call to k_poll(), the user has to reset the
4179 * state field to K_POLL_STATE_NOT_READY.
4180 *
Andrew Boie3772f772018-05-07 16:52:57 -07004181 * When called from user mode, a temporary memory allocation is required from
4182 * the caller's resource pool.
4183 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004184 * @param events An array of pointers to events to be polled for.
4185 * @param num_events The number of events in the array.
4186 * @param timeout Waiting period for an event to be ready (in milliseconds),
4187 * or one of the special values K_NO_WAIT and K_FOREVER.
4188 *
4189 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004190 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02004191 * @retval -EINTR Poller thread has been interrupted.
Andrew Boie3772f772018-05-07 16:52:57 -07004192 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4193 * @retval -EINVAL Bad parameters (user mode only)
Benjamin Walshacc68c12017-01-29 18:57:45 -05004194 */
4195
Andrew Boie3772f772018-05-07 16:52:57 -07004196__syscall int k_poll(struct k_poll_event *events, int num_events,
4197 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004198
4199/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004200 * @brief Initialize a poll signal object.
4201 *
4202 * Ready a poll signal object to be signaled via k_poll_signal().
4203 *
4204 * @param signal A poll signal.
4205 *
4206 * @return N/A
4207 */
4208
Andrew Boie3772f772018-05-07 16:52:57 -07004209__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4210
4211/*
4212 * @brief Reset a poll signal object's state to unsignaled.
4213 *
4214 * @param signal A poll signal object
4215 */
4216__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4217
4218static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4219{
4220 signal->signaled = 0;
4221}
4222
4223/**
4224 * @brief Fetch the signaled state and resylt value of a poll signal
4225 *
4226 * @param signal A poll signal object
4227 * @param signaled An integer buffer which will be written nonzero if the
4228 * object was signaled
4229 * @param result An integer destination buffer which will be written with the
4230 * result value if the object was signaed, or an undefined
4231 * value if it was not.
4232 */
4233__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4234 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004235
4236/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004237 * @brief Signal a poll signal object.
4238 *
4239 * This routine makes ready a poll signal, which is basically a poll event of
4240 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4241 * made ready to run. A @a result value can be specified.
4242 *
4243 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004244 * k_poll_signal(), stays set until the user sets it back to 0 with
4245 * k_poll_signal_reset(). It thus has to be reset by the user before being
4246 * passed again to k_poll() or k_poll() will consider it being signaled, and
4247 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004248 *
4249 * @param signal A poll signal.
4250 * @param result The value to store in the result field of the signal.
4251 *
4252 * @retval 0 The signal was delivered successfully.
4253 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
4254 */
4255
Andrew Boie3772f772018-05-07 16:52:57 -07004256__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004257
Anas Nashif954d5502018-02-25 08:37:28 -06004258/**
4259 * @internal
4260 */
Andy Ross8606fab2018-03-26 10:54:40 -07004261extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004262
Anas Nashif166f5192018-02-25 08:02:36 -06004263/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004264
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004265/**
4266 * @brief Make the CPU idle.
4267 *
4268 * This function makes the CPU idle until an event wakes it up.
4269 *
4270 * In a regular system, the idle thread should be the only thread responsible
4271 * for making the CPU idle and triggering any type of power management.
4272 * However, in some more constrained systems, such as a single-threaded system,
4273 * the only thread would be responsible for this if needed.
4274 *
4275 * @return N/A
4276 */
4277extern void k_cpu_idle(void);
4278
4279/**
4280 * @brief Make the CPU idle in an atomic fashion.
4281 *
4282 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4283 * must be done atomically before making the CPU idle.
4284 *
4285 * @param key Interrupt locking key obtained from irq_lock().
4286 *
4287 * @return N/A
4288 */
4289extern void k_cpu_atomic_idle(unsigned int key);
4290
Anas Nashif954d5502018-02-25 08:37:28 -06004291
4292/**
4293 * @internal
4294 */
Kumar Galacc334c72017-04-21 10:55:34 -05004295extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004296
Andrew Boiecdb94d62017-04-18 15:22:05 -07004297#ifdef _ARCH_EXCEPT
4298/* This archtecture has direct support for triggering a CPU exception */
4299#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4300#else
4301
Andrew Boiecdb94d62017-04-18 15:22:05 -07004302/* NOTE: This is the implementation for arches that do not implement
4303 * _ARCH_EXCEPT() to generate a real CPU exception.
4304 *
4305 * We won't have a real exception frame to determine the PC value when
4306 * the oops occurred, so print file and line number before we jump into
4307 * the fatal error handler.
4308 */
4309#define _k_except_reason(reason) do { \
4310 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4311 _NanoFatalErrorHandler(reason, &_default_esf); \
4312 CODE_UNREACHABLE; \
4313 } while (0)
4314
4315#endif /* _ARCH__EXCEPT */
4316
4317/**
4318 * @brief Fatally terminate a thread
4319 *
4320 * This should be called when a thread has encountered an unrecoverable
4321 * runtime condition and needs to terminate. What this ultimately
4322 * means is determined by the _fatal_error_handler() implementation, which
4323 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4324 *
4325 * If this is called from ISR context, the default system fatal error handler
4326 * will treat it as an unrecoverable system error, just like k_panic().
4327 */
4328#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4329
4330/**
4331 * @brief Fatally terminate the system
4332 *
4333 * This should be called when the Zephyr kernel has encountered an
4334 * unrecoverable runtime condition and needs to terminate. What this ultimately
4335 * means is determined by the _fatal_error_handler() implementation, which
4336 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4337 */
4338#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4339
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004340/*
4341 * private APIs that are utilized by one or more public APIs
4342 */
4343
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004344#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004345/**
4346 * @internal
4347 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004348extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004349#else
Anas Nashif954d5502018-02-25 08:37:28 -06004350/**
4351 * @internal
4352 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004353#define _init_static_threads() do { } while ((0))
4354#endif
4355
Anas Nashif954d5502018-02-25 08:37:28 -06004356/**
4357 * @internal
4358 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004359extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004360/**
4361 * @internal
4362 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004363extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004364
Andrew Boiedc5d9352017-06-02 12:56:47 -07004365/* arch/cpu.h may declare an architecture or platform-specific macro
4366 * for properly declaring stacks, compatible with MMU/MPU constraints if
4367 * enabled
4368 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004369
4370/**
4371 * @brief Obtain an extern reference to a stack
4372 *
4373 * This macro properly brings the symbol of a thread stack declared
4374 * elsewhere into scope.
4375 *
4376 * @param sym Thread stack symbol name
4377 */
4378#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4379
Andrew Boiedc5d9352017-06-02 12:56:47 -07004380#ifdef _ARCH_THREAD_STACK_DEFINE
4381#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4382#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4383 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4384#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4385#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004386static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004387{
4388 return _ARCH_THREAD_STACK_BUFFER(sym);
4389}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004390#else
4391/**
4392 * @brief Declare a toplevel thread stack memory region
4393 *
4394 * This declares a region of memory suitable for use as a thread's stack.
4395 *
4396 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4397 * 'noinit' section so that it isn't zeroed at boot
4398 *
Andrew Boie507852a2017-07-25 18:47:07 -07004399 * The declared symbol will always be a k_thread_stack_t which can be passed to
4400 * k_thread_create, but should otherwise not be manipulated. If the buffer
4401 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004402 *
4403 * It is legal to precede this definition with the 'static' keyword.
4404 *
4405 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4406 * parameter of k_thread_create(), it may not be the same as the
4407 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4408 *
4409 * @param sym Thread stack symbol name
4410 * @param size Size of the stack memory region
4411 */
4412#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004413 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004414
4415/**
4416 * @brief Declare a toplevel array of thread stack memory regions
4417 *
4418 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4419 * definition for additional details and constraints.
4420 *
4421 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4422 * 'noinit' section so that it isn't zeroed at boot
4423 *
4424 * @param sym Thread stack symbol name
4425 * @param nmemb Number of stacks to declare
4426 * @param size Size of the stack memory region
4427 */
4428
4429#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004430 struct _k_thread_stack_element __noinit \
4431 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004432
4433/**
4434 * @brief Declare an embedded stack memory region
4435 *
4436 * Used for stacks embedded within other data structures. Use is highly
4437 * discouraged but in some cases necessary. For memory protection scenarios,
4438 * it is very important that any RAM preceding this member not be writable
4439 * by threads else a stack overflow will lead to silent corruption. In other
4440 * words, the containing data structure should live in RAM owned by the kernel.
4441 *
4442 * @param sym Thread stack symbol name
4443 * @param size Size of the stack memory region
4444 */
4445#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004446 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004447
4448/**
4449 * @brief Return the size in bytes of a stack memory region
4450 *
4451 * Convenience macro for passing the desired stack size to k_thread_create()
4452 * since the underlying implementation may actually create something larger
4453 * (for instance a guard area).
4454 *
4455 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004456 * passed to K_THREAD_STACK_DEFINE.
4457 *
4458 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4459 * it is not guaranteed to return the original value since each array
4460 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004461 *
4462 * @param sym Stack memory symbol
4463 * @return Size of the stack
4464 */
4465#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4466
4467/**
4468 * @brief Get a pointer to the physical stack buffer
4469 *
4470 * Convenience macro to get at the real underlying stack buffer used by
4471 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4472 * This is really only intended for diagnostic tools which want to examine
4473 * stack memory contents.
4474 *
4475 * @param sym Declared stack symbol name
4476 * @return The buffer itself, a char *
4477 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004478static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004479{
4480 return (char *)sym;
4481}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004482
4483#endif /* _ARCH_DECLARE_STACK */
4484
Chunlin Hane9c97022017-07-07 20:29:30 +08004485/**
4486 * @defgroup mem_domain_apis Memory domain APIs
4487 * @ingroup kernel_apis
4488 * @{
4489 */
4490
4491/** @def MEM_PARTITION_ENTRY
4492 * @brief Used to declare a memory partition entry
4493 */
4494#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4495 {\
4496 .start = _start, \
4497 .size = _size, \
4498 .attr = _attr, \
4499 }
4500
4501/** @def K_MEM_PARTITION_DEFINE
4502 * @brief Used to declare a memory partition
4503 */
4504#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4505#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4506 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304507 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004508 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4509#else
4510#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
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#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4514
Chunlin Hane9c97022017-07-07 20:29:30 +08004515/* memory partition */
4516struct k_mem_partition {
4517 /* start address of memory partition */
4518 u32_t start;
4519 /* size of memory partition */
4520 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304521#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004522 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304523 k_mem_partition_attr_t attr;
4524#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004525};
4526
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304527/* memory domian
4528 * Note: Always declare this structure with __kernel prefix
4529 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004530struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304531#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004532 /* partitions in the domain */
4533 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304534#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004535 /* domain q */
4536 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004537 /* number of partitions in the domain */
4538 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004539};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304540
Chunlin Hane9c97022017-07-07 20:29:30 +08004541
4542/**
4543 * @brief Initialize a memory domain.
4544 *
4545 * Initialize a memory domain with given name and memory partitions.
4546 *
4547 * @param domain The memory domain to be initialized.
4548 * @param num_parts The number of array items of "parts" parameter.
4549 * @param parts An array of pointers to the memory partitions. Can be NULL
4550 * if num_parts is zero.
4551 */
4552
Leandro Pereira08de6582018-02-28 14:22:57 -08004553extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004554 struct k_mem_partition *parts[]);
4555/**
4556 * @brief Destroy a memory domain.
4557 *
4558 * Destroy a memory domain.
4559 *
4560 * @param domain The memory domain to be destroyed.
4561 */
4562
4563extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4564
4565/**
4566 * @brief Add a memory partition into a memory domain.
4567 *
4568 * Add a memory partition into a memory domain.
4569 *
4570 * @param domain The memory domain to be added a memory partition.
4571 * @param part The memory partition to be added
4572 */
4573
4574extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4575 struct k_mem_partition *part);
4576
4577/**
4578 * @brief Remove a memory partition from a memory domain.
4579 *
4580 * Remove a memory partition from a memory domain.
4581 *
4582 * @param domain The memory domain to be removed a memory partition.
4583 * @param part The memory partition to be removed
4584 */
4585
4586extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4587 struct k_mem_partition *part);
4588
4589/**
4590 * @brief Add a thread into a memory domain.
4591 *
4592 * Add a thread into a memory domain.
4593 *
4594 * @param domain The memory domain that the thread is going to be added into.
4595 * @param thread ID of thread going to be added into the memory domain.
4596 */
4597
4598extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4599 k_tid_t thread);
4600
4601/**
4602 * @brief Remove a thread from its memory domain.
4603 *
4604 * Remove a thread from its memory domain.
4605 *
4606 * @param thread ID of thread going to be removed from its memory domain.
4607 */
4608
4609extern void k_mem_domain_remove_thread(k_tid_t thread);
4610
Anas Nashif166f5192018-02-25 08:02:36 -06004611/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004612
Andrew Boie756f9072017-10-10 16:01:49 -07004613/**
4614 * @brief Emit a character buffer to the console device
4615 *
4616 * @param c String of characters to print
4617 * @param n The length of the string
4618 */
4619__syscall void k_str_out(char *c, size_t n);
4620
Andy Rosse7172672018-01-24 15:48:32 -08004621/**
4622 * @brief Start a numbered CPU on a MP-capable system
4623
4624 * This starts and initializes a specific CPU. The main thread on
4625 * startup is running on CPU zero, other processors are numbered
4626 * sequentially. On return from this function, the CPU is known to
4627 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004628 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004629 * with the provided key will work to enable them.
4630 *
4631 * Normally, in SMP mode this function will be called by the kernel
4632 * initialization and should not be used as a user API. But it is
4633 * defined here for special-purpose apps which want Zephyr running on
4634 * one core and to use others for design-specific processing.
4635 *
4636 * @param cpu_num Integer number of the CPU
4637 * @param stack Stack memory for the CPU
4638 * @param sz Stack buffer size, in bytes
4639 * @param fn Function to begin running on the CPU. First argument is
4640 * an irq_unlock() key.
4641 * @param arg Untyped argument to be passed to "fn"
4642 */
4643extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4644 void (*fn)(int, void *), void *arg);
4645
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004646#ifdef __cplusplus
4647}
4648#endif
4649
Andrew Boiee004dec2016-11-07 09:01:19 -08004650#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4651/*
4652 * Define new and delete operators.
4653 * At this moment, the operators do nothing since objects are supposed
4654 * to be statically allocated.
4655 */
4656inline void operator delete(void *ptr)
4657{
4658 (void)ptr;
4659}
4660
4661inline void operator delete[](void *ptr)
4662{
4663 (void)ptr;
4664}
4665
4666inline void *operator new(size_t size)
4667{
4668 (void)size;
4669 return NULL;
4670}
4671
4672inline void *operator new[](size_t size)
4673{
4674 (void)size;
4675 return NULL;
4676}
4677
4678/* Placement versions of operator new and delete */
4679inline void operator delete(void *ptr1, void *ptr2)
4680{
4681 (void)ptr1;
4682 (void)ptr2;
4683}
4684
4685inline void operator delete[](void *ptr1, void *ptr2)
4686{
4687 (void)ptr1;
4688 (void)ptr2;
4689}
4690
4691inline void *operator new(size_t size, void *ptr)
4692{
4693 (void)size;
4694 return ptr;
4695}
4696
4697inline void *operator new[](size_t size, void *ptr)
4698{
4699 (void)size;
4700 return ptr;
4701}
4702
4703#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4704
Andrew Boiefa94ee72017-09-28 16:54:35 -07004705#include <syscalls/kernel.h>
4706
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004707#endif /* !_ASMLANGUAGE */
4708
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004709#endif /* _kernel__h_ */