blob: 3aef5cccbeb988c643637b69a79378ad681fc85b [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)
Ioannis Glaropoulos92b8a412018-06-20 17:30:48 +020017#include <kernel_includes.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040018
19#ifdef __cplusplus
20extern "C" {
21#endif
22
Anas Nashifbbb157d2017-01-15 08:46:31 -050023/**
24 * @brief Kernel APIs
25 * @defgroup kernel_apis Kernel APIs
26 * @{
27 * @}
28 */
29
Anas Nashif61f4b242016-11-18 10:53:59 -050030#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040031#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
32#else
33#define K_DEBUG(fmt, ...)
34#endif
35
Benjamin Walsh2f280412017-01-14 19:23:46 -050036#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
37#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
38#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
39#elif defined(CONFIG_COOP_ENABLED)
40#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
41#define _NUM_PREEMPT_PRIO (0)
42#elif defined(CONFIG_PREEMPT_ENABLED)
43#define _NUM_COOP_PRIO (0)
44#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
45#else
46#error "invalid configuration"
47#endif
48
49#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040050#define K_PRIO_PREEMPT(x) (x)
51
Benjamin Walsh456c6da2016-09-02 18:55:39 -040052#define K_ANY NULL
53#define K_END NULL
54
Benjamin Walshedb35702017-01-14 18:47:22 -050055#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040056#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050057#elif defined(CONFIG_COOP_ENABLED)
58#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
59#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040060#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050061#else
62#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040063#endif
64
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050065#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040066#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
67#else
68#define K_LOWEST_THREAD_PRIO -1
69#endif
70
Benjamin Walshfab8d922016-11-08 15:36:36 -050071#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
72
Benjamin Walsh456c6da2016-09-02 18:55:39 -040073#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
74#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
75
Andy Ross1acd8c22018-05-03 14:51:49 -070076#ifdef CONFIG_WAITQ_FAST
77
78typedef struct {
79 struct _priq_rb waitq;
80} _wait_q_t;
81
82extern int _priq_rb_lessthan(struct rbnode *a, struct rbnode *b);
83
84#define _WAIT_Q_INIT(wait_q) { { { .lessthan_fn = _priq_rb_lessthan } } }
85
86#else
87
Andy Rossccf3bf72018-05-10 11:10:34 -070088typedef struct {
89 sys_dlist_t waitq;
90} _wait_q_t;
91
92#define _WAIT_Q_INIT(wait_q) { SYS_DLIST_STATIC_INIT(&(wait_q)->waitq) }
Benjamin Walsh456c6da2016-09-02 18:55:39 -040093
Andy Ross1acd8c22018-05-03 14:51:49 -070094#endif
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 Boiedf555242018-05-25 07:28:54 -0700161} __packed __aligned(4);
Andrew Boie945af952017-08-22 13:15:23 -0700162
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 */
Andy Ross1acd8c22018-05-03 14:51:49 -0700404 union {
405 sys_dlist_t qnode_dlist;
406 struct rbnode qnode_rb;
407 };
408
409#ifdef CONFIG_WAITQ_FAST
410 /* wait queue on which the thread is pended (needed only for
411 * trees, not dumb lists)
412 */
413 _wait_q_t *pended_on;
414#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700415
416 /* user facing 'thread options'; values defined in include/kernel.h */
417 u8_t user_options;
418
419 /* thread state */
420 u8_t thread_state;
421
422 /*
423 * scheduler lock count and thread priority
424 *
425 * These two fields control the preemptibility of a thread.
426 *
427 * When the scheduler is locked, sched_locked is decremented, which
428 * means that the scheduler is locked for values from 0xff to 0x01. A
429 * thread is coop if its prio is negative, thus 0x80 to 0xff when
430 * looked at the value as unsigned.
431 *
432 * By putting them end-to-end, this means that a thread is
433 * non-preemptible if the bundled value is greater than or equal to
434 * 0x0080.
435 */
436 union {
437 struct {
438#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
439 u8_t sched_locked;
440 s8_t prio;
441#else /* LITTLE and PDP */
442 s8_t prio;
443 u8_t sched_locked;
444#endif
445 };
446 u16_t preempt;
447 };
448
Andy Ross4a2e50f2018-05-15 11:06:25 -0700449#ifdef CONFIG_SCHED_DEADLINE
450 int prio_deadline;
451#endif
452
Andy Ross1acd8c22018-05-03 14:51:49 -0700453 u32_t order_key;
454
Andy Ross2724fd12018-01-29 14:55:20 -0800455#ifdef CONFIG_SMP
456 /* True for the per-CPU idle threads */
457 u8_t is_idle;
458
Andy Ross2724fd12018-01-29 14:55:20 -0800459 /* CPU index on which thread was last run */
460 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700461
462 /* Recursive count of irq_lock() calls */
463 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800464#endif
465
Andrew Boie73abd322017-04-04 13:19:13 -0700466 /* data returned by APIs */
467 void *swap_data;
468
469#ifdef CONFIG_SYS_CLOCK_EXISTS
470 /* this thread's entry in a timeout queue */
471 struct _timeout timeout;
472#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700473};
474
475typedef struct _thread_base _thread_base_t;
476
477#if defined(CONFIG_THREAD_STACK_INFO)
478/* Contains the stack information of a thread */
479struct _thread_stack_info {
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700480 /* Stack Start - Identical to K_THREAD_STACK_BUFFER() on the stack
481 * object. Represents thread-writable stack area without any extras.
482 */
Andrew Boie73abd322017-04-04 13:19:13 -0700483 u32_t start;
Andrew Boieb85ac3e2018-06-01 12:15:13 -0700484
485 /* Stack Size - Thread writable stack buffer size. Represents
486 * the size of the actual area, starting from the start member,
487 * that should be writable by the thread
488 */
Andrew Boie73abd322017-04-04 13:19:13 -0700489 u32_t size;
490};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700491
492typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700493#endif /* CONFIG_THREAD_STACK_INFO */
494
Chunlin Hane9c97022017-07-07 20:29:30 +0800495#if defined(CONFIG_USERSPACE)
496struct _mem_domain_info {
497 /* memory domain queue node */
498 sys_dnode_t mem_domain_q_node;
499 /* memory domain of the thread */
500 struct k_mem_domain *mem_domain;
501};
502
503#endif /* CONFIG_USERSPACE */
504
Anas Nashifce78d162018-05-24 12:43:11 -0500505/**
506 * @ingroup thread_apis
507 * Thread Structure
508 */
Andrew Boie73abd322017-04-04 13:19:13 -0700509struct k_thread {
510
511 struct _thread_base base;
512
Anas Nashifce78d162018-05-24 12:43:11 -0500513 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700514 struct _caller_saved caller_saved;
Anas Nashifce78d162018-05-24 12:43:11 -0500515 /** defined by the architecture, but all archs need these */
Andrew Boie73abd322017-04-04 13:19:13 -0700516 struct _callee_saved callee_saved;
517
Anas Nashifce78d162018-05-24 12:43:11 -0500518 /** static thread init data */
Andrew Boie73abd322017-04-04 13:19:13 -0700519 void *init_data;
520
Anas Nashifce78d162018-05-24 12:43:11 -0500521 /**
522 * abort function
523 * @req K-THREAD-002
524 * */
Andrew Boie73abd322017-04-04 13:19:13 -0700525 void (*fn_abort)(void);
526
527#if defined(CONFIG_THREAD_MONITOR)
Anas Nashifce78d162018-05-24 12:43:11 -0500528 /** thread entry and parameters description */
Andrew Boie2dd91ec2018-06-06 08:45:01 -0700529 struct __thread_entry entry;
Andrew Boie73abd322017-04-04 13:19:13 -0700530
Anas Nashifce78d162018-05-24 12:43:11 -0500531 /** next item in list of all threads */
Andrew Boie73abd322017-04-04 13:19:13 -0700532 struct k_thread *next_thread;
533#endif
534
535#ifdef CONFIG_THREAD_CUSTOM_DATA
Anas Nashifce78d162018-05-24 12:43:11 -0500536 /** crude thread-local storage */
Andrew Boie73abd322017-04-04 13:19:13 -0700537 void *custom_data;
538#endif
539
540#ifdef CONFIG_ERRNO
Anas Nashifce78d162018-05-24 12:43:11 -0500541 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700542 int errno_var;
543#endif
544
545#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500546 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700547 struct _thread_stack_info stack_info;
548#endif /* CONFIG_THREAD_STACK_INFO */
549
Chunlin Hane9c97022017-07-07 20:29:30 +0800550#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500551 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800552 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500553 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700554 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800555#endif /* CONFIG_USERSPACE */
556
Andy Ross042d8ec2017-12-09 08:37:20 -0800557#if defined(CONFIG_USE_SWITCH)
558 /* When using __switch() a few previously arch-specific items
559 * become part of the core OS
560 */
561
Anas Nashifce78d162018-05-24 12:43:11 -0500562 /** _Swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800563 int swap_retval;
564
Anas Nashifce78d162018-05-24 12:43:11 -0500565 /** Context handle returned via _arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800566 void *switch_handle;
567#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500568 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700569 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800570
Anas Nashifce78d162018-05-24 12:43:11 -0500571 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700572 struct _thread_arch arch;
573};
574
575typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400576typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700577#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400578
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400579enum execution_context_types {
580 K_ISR = 0,
581 K_COOP_THREAD,
582 K_PREEMPT_THREAD,
583};
584
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400585/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100586 * @defgroup profiling_apis Profiling APIs
587 * @ingroup kernel_apis
588 * @{
589 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530590typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
591 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100592
593/**
594 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
595 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700596 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100597 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
598 *
599 * CONFIG_MAIN_STACK_SIZE
600 * CONFIG_IDLE_STACK_SIZE
601 * CONFIG_ISR_STACK_SIZE
602 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
603 *
604 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
605 * produce output.
606 *
607 * @return N/A
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530608 *
609 * @deprecated This API is deprecated. Use k_thread_foreach().
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100610 */
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530611__deprecated extern void k_call_stacks_analyze(void);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100612
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530613/**
614 * @brief Iterate over all the threads in the system.
615 *
616 * This routine iterates over all the threads in the system and
617 * calls the user_cb function for each thread.
618 *
619 * @param user_cb Pointer to the user callback function.
620 * @param user_data Pointer to user data.
621 *
622 * @note CONFIG_THREAD_MONITOR must be set for this function
623 * to be effective. Also this API uses irq_lock to protect the
624 * _kernel.threads list which means creation of new threads and
625 * terminations of existing threads are blocked until this
626 * API returns.
627 *
628 * @return N/A
629 */
630extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
631
Anas Nashif166f5192018-02-25 08:02:36 -0600632/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100633
634/**
Allan Stephensc98da842016-11-11 15:45:03 -0500635 * @defgroup thread_apis Thread APIs
636 * @ingroup kernel_apis
637 * @{
638 */
639
Benjamin Walshed240f22017-01-22 13:05:08 -0500640#endif /* !_ASMLANGUAGE */
641
642
643/*
644 * Thread user options. May be needed by assembly code. Common part uses low
645 * bits, arch-specific use high bits.
646 */
647
Anas Nashifa541e932018-05-24 11:19:16 -0500648/**
649 * @brief system thread that must not abort
650 * @req K-THREAD-000
651 * */
Benjamin Walshed240f22017-01-22 13:05:08 -0500652#define K_ESSENTIAL (1 << 0)
653
654#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500655/**
656 * @brief thread uses floating point registers
657 */
Benjamin Walshed240f22017-01-22 13:05:08 -0500658#define K_FP_REGS (1 << 1)
659#endif
660
Anas Nashifa541e932018-05-24 11:19:16 -0500661/**
662 * @brief user mode thread
663 *
664 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700665 * has additional restrictions
666 */
667#define K_USER (1 << 2)
668
Anas Nashifa541e932018-05-24 11:19:16 -0500669/**
670 * @brief Inherit Permissions
671 *
672 * @details
673 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700674 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
675 * is not enabled.
676 */
677#define K_INHERIT_PERMS (1 << 3)
678
Benjamin Walshed240f22017-01-22 13:05:08 -0500679#ifdef CONFIG_X86
680/* x86 Bitmask definitions for threads user options */
681
682#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
683/* thread uses SSEx (and also FP) registers */
684#define K_SSE_REGS (1 << 7)
685#endif
686#endif
687
688/* end - thread options */
689
690#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400691/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700692 * @brief Create a thread.
693 *
694 * This routine initializes a thread, then schedules it for execution.
695 *
696 * The new thread may be scheduled for immediate execution or a delayed start.
697 * If the newly spawned thread does not have a delayed start the kernel
698 * scheduler may preempt the current thread to allow the new thread to
699 * execute.
700 *
701 * Thread options are architecture-specific, and can include K_ESSENTIAL,
702 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
703 * them using "|" (the logical OR operator).
704 *
705 * Historically, users often would use the beginning of the stack memory region
706 * to store the struct k_thread data, although corruption will occur if the
707 * stack overflows this region and stack protection features may not detect this
708 * situation.
709 *
710 * @param new_thread Pointer to uninitialized struct k_thread
711 * @param stack Pointer to the stack space.
712 * @param stack_size Stack size in bytes.
713 * @param entry Thread entry function.
714 * @param p1 1st entry point parameter.
715 * @param p2 2nd entry point parameter.
716 * @param p3 3rd entry point parameter.
717 * @param prio Thread priority.
718 * @param options Thread options.
719 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
720 *
721 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400722 *
723 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700724 */
Andrew Boie662c3452017-10-02 10:51:18 -0700725__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700726 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700727 size_t stack_size,
728 k_thread_entry_t entry,
729 void *p1, void *p2, void *p3,
730 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700731
Andrew Boie3f091b52017-08-30 14:34:14 -0700732/**
733 * @brief Drop a thread's privileges permanently to user mode
734 *
735 * @param entry Function to start executing from
736 * @param p1 1st entry point parameter
737 * @param p2 2nd entry point parameter
738 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400739 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700740 */
741extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
742 void *p1, void *p2,
743 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700744
Andrew Boied26cf2d2017-03-30 13:07:02 -0700745/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700746 * @brief Grant a thread access to a NULL-terminated set of kernel objects
747 *
748 * This is a convenience function. For the provided thread, grant access to
749 * the remaining arguments, which must be pointers to kernel objects.
750 * The final argument must be a NULL.
751 *
752 * The thread object must be initialized (i.e. running). The objects don't
753 * need to be.
754 *
755 * @param thread Thread to grant access to objects
756 * @param ... NULL-terminated list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400757 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700758 */
759extern void __attribute__((sentinel))
760 k_thread_access_grant(struct k_thread *thread, ...);
761
762/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700763 * @brief Assign a resource memory pool to a thread
764 *
765 * By default, threads have no resource pool assigned unless their parent
766 * thread has a resource pool, in which case it is inherited. Multiple
767 * threads may be assigned to the same memory pool.
768 *
769 * Changing a thread's resource pool will not migrate allocations from the
770 * previous pool.
771 *
772 * @param thread Target thread to assign a memory pool for resource requests,
773 * or NULL if the thread should no longer have a memory pool.
774 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400775 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700776 */
777static inline void k_thread_resource_pool_assign(struct k_thread *thread,
778 struct k_mem_pool *pool)
779{
780 thread->resource_pool = pool;
781}
782
783#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
784/**
785 * @brief Assign the system heap as a thread's resource pool
786 *
787 * Similar to k_thread_resource_pool_assign(), but the thread will use
788 * the kernel heap to draw memory.
789 *
790 * Use with caution, as a malicious thread could perform DoS attacks on the
791 * kernel heap.
792 *
793 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400794 *
795 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700796 */
797void k_thread_system_pool_assign(struct k_thread *thread);
798#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
799
800/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500801 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400802 *
Allan Stephensc98da842016-11-11 15:45:03 -0500803 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500804 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400805 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500806 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400807 *
808 * @return N/A
809 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700810__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400811
812/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500813 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400814 *
815 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500816 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400817 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400818 * @return N/A
819 */
Kumar Galacc334c72017-04-21 10:55:34 -0500820extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400821
822/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500823 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500825 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400826 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500827 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400828 *
829 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400830 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400831 */
Andrew Boie468190a2017-09-29 14:00:48 -0700832__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400833
834/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500835 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500837 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400838 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500839 * If @a thread is not currently sleeping, the routine has no effect.
840 *
841 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400842 *
843 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400844 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400845 */
Andrew Boie468190a2017-09-29 14:00:48 -0700846__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400847
848/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500849 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400850 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500851 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400852 *
853 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400854 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700855__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856
857/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500858 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500860 * This routine prevents @a thread from executing if it has not yet started
861 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400862 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500863 * @param thread ID of thread to cancel.
864 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700865 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500866 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700867 *
868 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400869 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700870__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400871
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400872/**
Allan Stephensc98da842016-11-11 15:45:03 -0500873 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500875 * This routine permanently stops execution of @a thread. The thread is taken
876 * off all kernel queues it is part of (i.e. the ready queue, the timeout
877 * queue, or a kernel object wait queue). However, any kernel resources the
878 * thread might currently own (such as mutexes or memory blocks) are not
879 * released. It is the responsibility of the caller of this routine to ensure
880 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500882 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400883 *
884 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400885 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400886 */
Andrew Boie468190a2017-09-29 14:00:48 -0700887__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400888
Andrew Boie7d627c52017-08-30 11:01:56 -0700889
890/**
891 * @brief Start an inactive thread
892 *
893 * If a thread was created with K_FOREVER in the delay parameter, it will
894 * not be added to the scheduling queue until this function is called
895 * on it.
896 *
897 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400898 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700899 */
Andrew Boie468190a2017-09-29 14:00:48 -0700900__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700901
Allan Stephensc98da842016-11-11 15:45:03 -0500902/**
903 * @cond INTERNAL_HIDDEN
904 */
905
Benjamin Walshd211a522016-12-06 11:44:01 -0500906/* timeout has timed out and is not on _timeout_q anymore */
907#define _EXPIRED (-2)
908
909/* timeout is not in use */
910#define _INACTIVE (-1)
911
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400912struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700913 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700914 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400915 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700916 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500917 void *init_p1;
918 void *init_p2;
919 void *init_p3;
920 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500921 u32_t init_options;
922 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500923 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400924};
925
Andrew Boied26cf2d2017-03-30 13:07:02 -0700926#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400927 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500928 prio, options, delay, abort, groups) \
929 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700930 .init_thread = (thread), \
931 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500932 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700933 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400934 .init_p1 = (void *)p1, \
935 .init_p2 = (void *)p2, \
936 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500937 .init_prio = (prio), \
938 .init_options = (options), \
939 .init_delay = (delay), \
940 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400941 }
942
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400943/**
Allan Stephensc98da842016-11-11 15:45:03 -0500944 * INTERNAL_HIDDEN @endcond
945 */
946
947/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500948 * @brief Statically define and initialize a thread.
949 *
950 * The thread may be scheduled for immediate execution or a delayed start.
951 *
952 * Thread options are architecture-specific, and can include K_ESSENTIAL,
953 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
954 * them using "|" (the logical OR operator).
955 *
956 * The ID of the thread can be accessed using:
957 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500958 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500959 *
960 * @param name Name of the thread.
961 * @param stack_size Stack size in bytes.
962 * @param entry Thread entry function.
963 * @param p1 1st entry point parameter.
964 * @param p2 2nd entry point parameter.
965 * @param p3 3rd entry point parameter.
966 * @param prio Thread priority.
967 * @param options Thread options.
968 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400969 *
Anas Nashif47420d02018-05-24 14:20:56 -0400970 * @req K-THREAD-010
971 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400972 * @internal It has been observed that the x86 compiler by default aligns
973 * these _static_thread_data structures to 32-byte boundaries, thereby
974 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400975 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400976 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500977#define K_THREAD_DEFINE(name, stack_size, \
978 entry, p1, p2, p3, \
979 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700980 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700981 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500982 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500983 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700984 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
985 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500986 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700987 NULL, 0); \
988 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400989
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400990/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500991 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500993 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400994 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500995 * @param thread ID of thread whose priority is needed.
996 *
997 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400998 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400999 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001000__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001001
1002/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001003 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001005 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001006 *
1007 * Rescheduling can occur immediately depending on the priority @a thread is
1008 * set to:
1009 *
1010 * - If its priority is raised above the priority of the caller of this
1011 * function, and the caller is preemptible, @a thread will be scheduled in.
1012 *
1013 * - If the caller operates on itself, it lowers its priority below that of
1014 * other threads in the system, and the caller is preemptible, the thread of
1015 * highest priority will be scheduled in.
1016 *
1017 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1018 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1019 * highest priority.
1020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001021 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001022 * @param prio New priority.
1023 *
1024 * @warning Changing the priority of a thread currently involved in mutex
1025 * priority inheritance may result in undefined behavior.
1026 *
1027 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001028 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001029 */
Andrew Boie468190a2017-09-29 14:00:48 -07001030__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001031
Andy Ross4a2e50f2018-05-15 11:06:25 -07001032
1033#ifdef CONFIG_SCHED_DEADLINE
1034/**
1035 * @brief Set deadline expiration time for scheduler
1036 *
1037 * This sets the "deadline" expiration as a time delta from the
1038 * current time, in the same units used by k_cycle_get_32(). The
1039 * scheduler (when deadline scheduling is enabled) will choose the
1040 * next expiring thread when selecting between threads at the same
1041 * static priority. Threads at different priorities will be scheduled
1042 * according to their static priority.
1043 *
1044 * @note Deadlines that are negative (i.e. in the past) are still seen
1045 * as higher priority than others, even if the thread has "finished"
1046 * its work. If you don't want it scheduled anymore, you have to
1047 * reset the deadline into the future, block/pend the thread, or
1048 * modify its priority with k_thread_priority_set().
1049 *
1050 * @note Despite the API naming, the scheduler makes no guarantees the
1051 * the thread WILL be scheduled within that deadline, nor does it take
1052 * extra metadata (like e.g. the "runtime" and "period" parameters in
1053 * Linux sched_setattr()) that allows the kernel to validate the
1054 * scheduling for achievability. Such features could be implemented
1055 * above this call, which is simply input to the priority selection
1056 * logic.
1057 *
1058 * @param thread A thread on which to set the deadline
1059 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001060 *
1061 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001062 */
1063__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1064#endif
1065
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001066/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001067 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001069 * This routine prevents the kernel scheduler from making @a thread the
1070 * current thread. All other internal operations on @a thread are still
1071 * performed; for example, any timeout it is waiting on keeps ticking,
1072 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001074 * If @a thread is already suspended, the routine has no effect.
1075 *
1076 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001077 *
1078 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001079 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001080 */
Andrew Boie468190a2017-09-29 14:00:48 -07001081__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001082
1083/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001084 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001085 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001086 * This routine allows the kernel scheduler to make @a thread the current
1087 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001088 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001089 * If @a thread is not currently suspended, the routine has no effect.
1090 *
1091 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001092 *
1093 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001094 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001095 */
Andrew Boie468190a2017-09-29 14:00:48 -07001096__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001097
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001098/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001099 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001101 * This routine specifies how the scheduler will perform time slicing of
1102 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001104 * To enable time slicing, @a slice must be non-zero. The scheduler
1105 * ensures that no thread runs for more than the specified time limit
1106 * before other threads of that priority are given a chance to execute.
1107 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001108 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001110 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001111 * execute. Once the scheduler selects a thread for execution, there is no
1112 * minimum guaranteed time the thread will execute before threads of greater or
1113 * equal priority are scheduled.
1114 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001115 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001116 * for execution, this routine has no effect; the thread is immediately
1117 * rescheduled after the slice period expires.
1118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001119 * To disable timeslicing, set both @a slice and @a prio to zero.
1120 *
1121 * @param slice Maximum time slice length (in milliseconds).
1122 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001123 *
1124 * @return N/A
1125 */
Kumar Galacc334c72017-04-21 10:55:34 -05001126extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001127
Anas Nashif166f5192018-02-25 08:02:36 -06001128/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001129
1130/**
1131 * @addtogroup isr_apis
1132 * @{
1133 */
1134
1135/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001136 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001137 *
Allan Stephensc98da842016-11-11 15:45:03 -05001138 * This routine allows the caller to customize its actions, depending on
1139 * whether it is a thread or an ISR.
1140 *
1141 * @note Can be called by ISRs.
1142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001143 * @return 0 if invoked by a thread.
1144 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001145 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001146extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001147
Benjamin Walsh445830d2016-11-10 15:54:27 -05001148/**
1149 * @brief Determine if code is running in a preemptible thread.
1150 *
Allan Stephensc98da842016-11-11 15:45:03 -05001151 * This routine allows the caller to customize its actions, depending on
1152 * whether it can be preempted by another thread. The routine returns a 'true'
1153 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001154 *
Allan Stephensc98da842016-11-11 15:45:03 -05001155 * - The code is running in a thread, not at ISR.
1156 * - The thread's priority is in the preemptible range.
1157 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001158 *
Allan Stephensc98da842016-11-11 15:45:03 -05001159 * @note Can be called by ISRs.
1160 *
1161 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001162 * @return Non-zero if invoked by a preemptible thread.
1163 */
Andrew Boie468190a2017-09-29 14:00:48 -07001164__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001165
Allan Stephensc98da842016-11-11 15:45:03 -05001166/**
Anas Nashif166f5192018-02-25 08:02:36 -06001167 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001168 */
1169
1170/**
1171 * @addtogroup thread_apis
1172 * @{
1173 */
1174
1175/**
1176 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001177 *
Allan Stephensc98da842016-11-11 15:45:03 -05001178 * This routine prevents the current thread from being preempted by another
1179 * thread by instructing the scheduler to treat it as a cooperative thread.
1180 * If the thread subsequently performs an operation that makes it unready,
1181 * it will be context switched out in the normal manner. When the thread
1182 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001183 *
Allan Stephensc98da842016-11-11 15:45:03 -05001184 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001185 *
Allan Stephensc98da842016-11-11 15:45:03 -05001186 * @note k_sched_lock() and k_sched_unlock() should normally be used
1187 * when the operation being performed can be safely interrupted by ISRs.
1188 * However, if the amount of processing involved is very small, better
1189 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001190 *
1191 * @return N/A
1192 */
1193extern void k_sched_lock(void);
1194
Allan Stephensc98da842016-11-11 15:45:03 -05001195/**
1196 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001197 *
Allan Stephensc98da842016-11-11 15:45:03 -05001198 * This routine reverses the effect of a previous call to k_sched_lock().
1199 * A thread must call the routine once for each time it called k_sched_lock()
1200 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001201 *
1202 * @return N/A
1203 */
1204extern void k_sched_unlock(void);
1205
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001206/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001207 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001208 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001209 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001210 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001211 * Custom data is not used by the kernel itself, and is freely available
1212 * for a thread to use as it sees fit. It can be used as a framework
1213 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001214 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001215 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001216 *
1217 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001218 *
1219 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001220 */
Andrew Boie468190a2017-09-29 14:00:48 -07001221__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001222
1223/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001224 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001225 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001226 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001227 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001228 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001229 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001230 */
Andrew Boie468190a2017-09-29 14:00:48 -07001231__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001232
1233/**
Anas Nashif166f5192018-02-25 08:02:36 -06001234 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001235 */
1236
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001237#include <sys_clock.h>
1238
Allan Stephensc2f15a42016-11-17 12:24:22 -05001239/**
1240 * @addtogroup clock_apis
1241 * @{
1242 */
1243
1244/**
1245 * @brief Generate null timeout delay.
1246 *
1247 * This macro generates a timeout delay that that instructs a kernel API
1248 * not to wait if the requested operation cannot be performed immediately.
1249 *
1250 * @return Timeout delay value.
1251 */
1252#define K_NO_WAIT 0
1253
1254/**
1255 * @brief Generate timeout delay from milliseconds.
1256 *
1257 * This macro generates a timeout delay that that instructs a kernel API
1258 * to wait up to @a ms milliseconds to perform the requested operation.
1259 *
1260 * @param ms Duration in milliseconds.
1261 *
1262 * @return Timeout delay value.
1263 */
Johan Hedberg14471692016-11-13 10:52:15 +02001264#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001265
1266/**
1267 * @brief Generate timeout delay from seconds.
1268 *
1269 * This macro generates a timeout delay that that instructs a kernel API
1270 * to wait up to @a s seconds to perform the requested operation.
1271 *
1272 * @param s Duration in seconds.
1273 *
1274 * @return Timeout delay value.
1275 */
Johan Hedberg14471692016-11-13 10:52:15 +02001276#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001277
1278/**
1279 * @brief Generate timeout delay from minutes.
1280 *
1281 * This macro generates a timeout delay that that instructs a kernel API
1282 * to wait up to @a m minutes to perform the requested operation.
1283 *
1284 * @param m Duration in minutes.
1285 *
1286 * @return Timeout delay value.
1287 */
Johan Hedberg14471692016-11-13 10:52:15 +02001288#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001289
1290/**
1291 * @brief Generate timeout delay from hours.
1292 *
1293 * This macro generates a timeout delay that that instructs a kernel API
1294 * to wait up to @a h hours to perform the requested operation.
1295 *
1296 * @param h Duration in hours.
1297 *
1298 * @return Timeout delay value.
1299 */
Johan Hedberg14471692016-11-13 10:52:15 +02001300#define K_HOURS(h) K_MINUTES((h) * 60)
1301
Allan Stephensc98da842016-11-11 15:45:03 -05001302/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001303 * @brief Generate infinite timeout delay.
1304 *
1305 * This macro generates a timeout delay that that instructs a kernel API
1306 * to wait as long as necessary to perform the requested operation.
1307 *
1308 * @return Timeout delay value.
1309 */
1310#define K_FOREVER (-1)
1311
1312/**
Anas Nashif166f5192018-02-25 08:02:36 -06001313 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001314 */
1315
1316/**
Allan Stephensc98da842016-11-11 15:45:03 -05001317 * @cond INTERNAL_HIDDEN
1318 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001319
Benjamin Walsh62092182016-12-20 14:39:08 -05001320/* kernel clocks */
1321
1322#if (sys_clock_ticks_per_sec == 1000) || \
1323 (sys_clock_ticks_per_sec == 500) || \
1324 (sys_clock_ticks_per_sec == 250) || \
1325 (sys_clock_ticks_per_sec == 125) || \
1326 (sys_clock_ticks_per_sec == 100) || \
1327 (sys_clock_ticks_per_sec == 50) || \
1328 (sys_clock_ticks_per_sec == 25) || \
1329 (sys_clock_ticks_per_sec == 20) || \
1330 (sys_clock_ticks_per_sec == 10) || \
1331 (sys_clock_ticks_per_sec == 1)
1332
1333 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1334#else
1335 /* yields horrible 64-bit math on many architectures: try to avoid */
1336 #define _NON_OPTIMIZED_TICKS_PER_SEC
1337#endif
1338
1339#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001340extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001341#else
Kumar Galacc334c72017-04-21 10:55:34 -05001342static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001343{
Kumar Galacc334c72017-04-21 10:55:34 -05001344 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001345}
1346#endif
1347
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001348/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001349#ifdef CONFIG_TICKLESS_KERNEL
1350#define _TICK_ALIGN 0
1351#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001352#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001353#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001354
Kumar Galacc334c72017-04-21 10:55:34 -05001355static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001356{
Benjamin Walsh62092182016-12-20 14:39:08 -05001357#ifdef CONFIG_SYS_CLOCK_EXISTS
1358
1359#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001360 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001361#else
Kumar Galacc334c72017-04-21 10:55:34 -05001362 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001363#endif
1364
1365#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001366 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001367 return 0;
1368#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001369}
1370
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001371struct k_timer {
1372 /*
1373 * _timeout structure must be first here if we want to use
1374 * dynamic timer allocation. timeout.node is used in the double-linked
1375 * list of free timers
1376 */
1377 struct _timeout timeout;
1378
Allan Stephens45bfa372016-10-12 12:39:42 -05001379 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001380 _wait_q_t wait_q;
1381
1382 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001383 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001384
1385 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001386 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001387
1388 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001389 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001390
Allan Stephens45bfa372016-10-12 12:39:42 -05001391 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001392 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001393
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001394 /* user-specific data, also used to support legacy features */
1395 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001396
Anas Nashif2f203c22016-12-18 06:57:45 -05001397 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001398};
1399
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001400#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001401 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001402 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001403 .timeout.wait_q = NULL, \
1404 .timeout.thread = NULL, \
1405 .timeout.func = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001406 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001407 .expiry_fn = expiry, \
1408 .stop_fn = stop, \
1409 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001410 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001411 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001412 }
1413
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001414#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1415
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001416/**
Allan Stephensc98da842016-11-11 15:45:03 -05001417 * INTERNAL_HIDDEN @endcond
1418 */
1419
1420/**
1421 * @defgroup timer_apis Timer APIs
1422 * @ingroup kernel_apis
1423 * @{
1424 */
1425
1426/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001427 * @typedef k_timer_expiry_t
1428 * @brief Timer expiry function type.
1429 *
1430 * A timer's expiry function is executed by the system clock interrupt handler
1431 * each time the timer expires. The expiry function is optional, and is only
1432 * invoked if the timer has been initialized with one.
1433 *
1434 * @param timer Address of timer.
1435 *
1436 * @return N/A
1437 */
1438typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1439
1440/**
1441 * @typedef k_timer_stop_t
1442 * @brief Timer stop function type.
1443 *
1444 * A timer's stop function is executed if the timer is stopped prematurely.
1445 * The function runs in the context of the thread that stops the timer.
1446 * The stop function is optional, and is only invoked if the timer has been
1447 * initialized with one.
1448 *
1449 * @param timer Address of timer.
1450 *
1451 * @return N/A
1452 */
1453typedef void (*k_timer_stop_t)(struct k_timer *timer);
1454
1455/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001456 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001457 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001458 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001459 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001460 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001461 *
1462 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001463 * @param expiry_fn Function to invoke each time the timer expires.
1464 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001465 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001466#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001467 struct k_timer name \
1468 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001469 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001470
Allan Stephens45bfa372016-10-12 12:39:42 -05001471/**
1472 * @brief Initialize a timer.
1473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001474 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001475 *
1476 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001477 * @param expiry_fn Function to invoke each time the timer expires.
1478 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001479 *
1480 * @return N/A
1481 */
1482extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001483 k_timer_expiry_t expiry_fn,
1484 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001485
Allan Stephens45bfa372016-10-12 12:39:42 -05001486/**
1487 * @brief Start a timer.
1488 *
1489 * This routine starts a timer, and resets its status to zero. The timer
1490 * begins counting down using the specified duration and period values.
1491 *
1492 * Attempting to start a timer that is already running is permitted.
1493 * The timer's status is reset to zero and the timer begins counting down
1494 * using the new duration and period values.
1495 *
1496 * @param timer Address of timer.
1497 * @param duration Initial timer duration (in milliseconds).
1498 * @param period Timer period (in milliseconds).
1499 *
1500 * @return N/A
1501 */
Andrew Boiea354d492017-09-29 16:22:28 -07001502__syscall void k_timer_start(struct k_timer *timer,
1503 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001504
1505/**
1506 * @brief Stop a timer.
1507 *
1508 * This routine stops a running timer prematurely. The timer's stop function,
1509 * if one exists, is invoked by the caller.
1510 *
1511 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001512 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001513 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001514 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1515 * if @a k_timer_stop is to be called from ISRs.
1516 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001517 * @param timer Address of timer.
1518 *
1519 * @return N/A
1520 */
Andrew Boiea354d492017-09-29 16:22:28 -07001521__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001522
1523/**
1524 * @brief Read timer status.
1525 *
1526 * This routine reads the timer's status, which indicates the number of times
1527 * it has expired since its status was last read.
1528 *
1529 * Calling this routine resets the timer's status to zero.
1530 *
1531 * @param timer Address of timer.
1532 *
1533 * @return Timer status.
1534 */
Andrew Boiea354d492017-09-29 16:22:28 -07001535__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001536
1537/**
1538 * @brief Synchronize thread to timer expiration.
1539 *
1540 * This routine blocks the calling thread until the timer's status is non-zero
1541 * (indicating that it has expired at least once since it was last examined)
1542 * or the timer is stopped. If the timer status is already non-zero,
1543 * or the timer is already stopped, the caller continues without waiting.
1544 *
1545 * Calling this routine resets the timer's status to zero.
1546 *
1547 * This routine must not be used by interrupt handlers, since they are not
1548 * allowed to block.
1549 *
1550 * @param timer Address of timer.
1551 *
1552 * @return Timer status.
1553 */
Andrew Boiea354d492017-09-29 16:22:28 -07001554__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001555
1556/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001557 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001558 *
1559 * This routine computes the (approximate) time remaining before a running
1560 * timer next expires. If the timer is not running, it returns zero.
1561 *
1562 * @param timer Address of timer.
1563 *
1564 * @return Remaining time (in milliseconds).
1565 */
Andrew Boiea354d492017-09-29 16:22:28 -07001566__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1567
1568static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001569{
1570 return _timeout_remaining_get(&timer->timeout);
1571}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001572
Allan Stephensc98da842016-11-11 15:45:03 -05001573/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001574 * @brief Associate user-specific data with a timer.
1575 *
1576 * This routine records the @a user_data with the @a timer, to be retrieved
1577 * later.
1578 *
1579 * It can be used e.g. in a timer handler shared across multiple subsystems to
1580 * retrieve data specific to the subsystem this timer is associated with.
1581 *
1582 * @param timer Address of timer.
1583 * @param user_data User data to associate with the timer.
1584 *
1585 * @return N/A
1586 */
Andrew Boiea354d492017-09-29 16:22:28 -07001587__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1588
Anas Nashif954d5502018-02-25 08:37:28 -06001589/**
1590 * @internal
1591 */
Andrew Boiea354d492017-09-29 16:22:28 -07001592static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1593 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001594{
1595 timer->user_data = user_data;
1596}
1597
1598/**
1599 * @brief Retrieve the user-specific data from a timer.
1600 *
1601 * @param timer Address of timer.
1602 *
1603 * @return The user data.
1604 */
Andrew Boiea354d492017-09-29 16:22:28 -07001605__syscall void *k_timer_user_data_get(struct k_timer *timer);
1606
1607static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001608{
1609 return timer->user_data;
1610}
1611
Anas Nashif166f5192018-02-25 08:02:36 -06001612/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001613
Allan Stephensc98da842016-11-11 15:45:03 -05001614/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001615 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001616 * @{
1617 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001618
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001619/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001620 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001621 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001622 * This routine returns the elapsed time since the system booted,
1623 * in milliseconds.
1624 *
1625 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001626 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001627__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001628
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001629/**
1630 * @brief Enable clock always on in tickless kernel
1631 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001632 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001633 * there are no timer events programmed in tickless kernel
1634 * scheduling. This is necessary if the clock is used to track
1635 * passage of time.
1636 *
1637 * @retval prev_status Previous status of always on flag
1638 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301639#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001640static inline int k_enable_sys_clock_always_on(void)
1641{
1642 int prev_status = _sys_clock_always_on;
1643
1644 _sys_clock_always_on = 1;
1645 _enable_sys_clock();
1646
1647 return prev_status;
1648}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301649#else
1650#define k_enable_sys_clock_always_on() do { } while ((0))
1651#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001652
1653/**
1654 * @brief Disable clock always on in tickless kernel
1655 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001656 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001657 * there are no timer events programmed in tickless kernel
1658 * scheduling. To save power, this routine should be called
1659 * immediately when clock is not used to track time.
1660 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301661#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001662static inline void k_disable_sys_clock_always_on(void)
1663{
1664 _sys_clock_always_on = 0;
1665}
1666#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001667#define k_disable_sys_clock_always_on() do { } while ((0))
1668#endif
1669
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001670/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001671 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001672 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001673 * This routine returns the lower 32-bits of the elapsed time since the system
1674 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001675 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001676 * This routine can be more efficient than k_uptime_get(), as it reduces the
1677 * need for interrupt locking and 64-bit math. However, the 32-bit result
1678 * cannot hold a system uptime time larger than approximately 50 days, so the
1679 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001680 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001681 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001682 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001683__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001684
1685/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001686 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001688 * This routine computes the elapsed time between the current system uptime
1689 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001691 * @param reftime Pointer to a reference time, which is updated to the current
1692 * uptime upon return.
1693 *
1694 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001695 */
Kumar Galacc334c72017-04-21 10:55:34 -05001696extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001697
1698/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001699 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001701 * This routine computes the elapsed time between the current system uptime
1702 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001703 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001704 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1705 * need for interrupt locking and 64-bit math. However, the 32-bit result
1706 * cannot hold an elapsed time larger than approximately 50 days, so the
1707 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001708 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001709 * @param reftime Pointer to a reference time, which is updated to the current
1710 * uptime upon return.
1711 *
1712 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001713 */
Kumar Galacc334c72017-04-21 10:55:34 -05001714extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001715
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001716/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001717 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001718 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001719 * This routine returns the current time, as measured by the system's hardware
1720 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001722 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001723 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001724#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001725
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001726/**
Anas Nashif166f5192018-02-25 08:02:36 -06001727 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001728 */
1729
Allan Stephensc98da842016-11-11 15:45:03 -05001730/**
1731 * @cond INTERNAL_HIDDEN
1732 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001733
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001734struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001735 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001736 union {
1737 _wait_q_t wait_q;
1738
1739 _POLL_EVENT;
1740 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001741
1742 _OBJECT_TRACING_NEXT_PTR(k_queue);
1743};
1744
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001745#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001746 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001747 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001748 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001749 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001750 _OBJECT_TRACING_INIT \
1751 }
1752
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001753#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1754
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001755extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1756
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001757/**
1758 * INTERNAL_HIDDEN @endcond
1759 */
1760
1761/**
1762 * @defgroup queue_apis Queue APIs
1763 * @ingroup kernel_apis
1764 * @{
1765 */
1766
1767/**
1768 * @brief Initialize a queue.
1769 *
1770 * This routine initializes a queue object, prior to its first use.
1771 *
1772 * @param queue Address of the queue.
1773 *
1774 * @return N/A
1775 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001776__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001777
1778/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001779 * @brief Cancel waiting on a queue.
1780 *
1781 * This routine causes first thread pending on @a queue, if any, to
1782 * return from k_queue_get() call with NULL value (as if timeout expired).
1783 *
1784 * @note Can be called by ISRs.
1785 *
1786 * @param queue Address of the queue.
1787 *
1788 * @return N/A
1789 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001790__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001791
1792/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001793 * @brief Append an element to the end of a queue.
1794 *
1795 * This routine appends a data item to @a queue. A queue data item must be
1796 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1797 * reserved for the kernel's use.
1798 *
1799 * @note Can be called by ISRs.
1800 *
1801 * @param queue Address of the queue.
1802 * @param data Address of the data item.
1803 *
1804 * @return N/A
1805 */
1806extern void k_queue_append(struct k_queue *queue, void *data);
1807
1808/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001809 * @brief Append an element to a queue.
1810 *
1811 * This routine appends a data item to @a queue. There is an implicit
1812 * memory allocation from the calling thread's resource pool, which is
1813 * automatically freed when the item is removed from the queue.
1814 *
1815 * @note Can be called by ISRs.
1816 *
1817 * @param queue Address of the queue.
1818 * @param data Address of the data item.
1819 *
1820 * @retval 0 on success
1821 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1822 */
1823__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1824
1825/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001826 * @brief Prepend an element to a queue.
1827 *
1828 * This routine prepends a data item to @a queue. A queue data item must be
1829 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1830 * reserved for the kernel's use.
1831 *
1832 * @note Can be called by ISRs.
1833 *
1834 * @param queue Address of the queue.
1835 * @param data Address of the data item.
1836 *
1837 * @return N/A
1838 */
1839extern void k_queue_prepend(struct k_queue *queue, void *data);
1840
1841/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001842 * @brief Prepend an element to a queue.
1843 *
1844 * This routine prepends a data item to @a queue. There is an implicit
1845 * memory allocation from the calling thread's resource pool, which is
1846 * automatically freed when the item is removed from the queue.
1847 *
1848 * @note Can be called by ISRs.
1849 *
1850 * @param queue Address of the queue.
1851 * @param data Address of the data item.
1852 *
1853 * @retval 0 on success
1854 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1855 */
1856__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1857
1858/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001859 * @brief Inserts an element to a queue.
1860 *
1861 * This routine inserts a data item to @a queue after previous item. A queue
1862 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1863 * item are reserved for the kernel's use.
1864 *
1865 * @note Can be called by ISRs.
1866 *
1867 * @param queue Address of the queue.
1868 * @param prev Address of the previous data item.
1869 * @param data Address of the data item.
1870 *
1871 * @return N/A
1872 */
1873extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1874
1875/**
1876 * @brief Atomically append a list of elements to a queue.
1877 *
1878 * This routine adds a list of data items to @a queue in one operation.
1879 * The data items must be in a singly-linked list, with the first 32 bits
1880 * in each data item pointing to the next data item; the list must be
1881 * NULL-terminated.
1882 *
1883 * @note Can be called by ISRs.
1884 *
1885 * @param queue Address of the queue.
1886 * @param head Pointer to first node in singly-linked list.
1887 * @param tail Pointer to last node in singly-linked list.
1888 *
1889 * @return N/A
1890 */
1891extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1892
1893/**
1894 * @brief Atomically add a list of elements to a queue.
1895 *
1896 * This routine adds a list of data items to @a queue in one operation.
1897 * The data items must be in a singly-linked list implemented using a
1898 * sys_slist_t object. Upon completion, the original list is empty.
1899 *
1900 * @note Can be called by ISRs.
1901 *
1902 * @param queue Address of the queue.
1903 * @param list Pointer to sys_slist_t object.
1904 *
1905 * @return N/A
1906 */
1907extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1908
1909/**
1910 * @brief Get an element from a queue.
1911 *
1912 * This routine removes first data item from @a queue. The first 32 bits of the
1913 * data item are reserved for the kernel's use.
1914 *
1915 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1916 *
1917 * @param queue Address of the queue.
1918 * @param timeout Waiting period to obtain a data item (in milliseconds),
1919 * or one of the special values K_NO_WAIT and K_FOREVER.
1920 *
1921 * @return Address of the data item if successful; NULL if returned
1922 * without waiting, or waiting period timed out.
1923 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001924__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001925
1926/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001927 * @brief Remove an element from a queue.
1928 *
1929 * This routine removes data item from @a queue. The first 32 bits of the
1930 * data item are reserved for the kernel's use. Removing elements from k_queue
1931 * rely on sys_slist_find_and_remove which is not a constant time operation.
1932 *
1933 * @note Can be called by ISRs
1934 *
1935 * @param queue Address of the queue.
1936 * @param data Address of the data item.
1937 *
1938 * @return true if data item was removed
1939 */
1940static inline bool k_queue_remove(struct k_queue *queue, void *data)
1941{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001942 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001943}
1944
1945/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001946 * @brief Query a queue to see if it has data available.
1947 *
1948 * Note that the data might be already gone by the time this function returns
1949 * if other threads are also trying to read from the queue.
1950 *
1951 * @note Can be called by ISRs.
1952 *
1953 * @param queue Address of the queue.
1954 *
1955 * @return Non-zero if the queue is empty.
1956 * @return 0 if data is available.
1957 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001958__syscall int k_queue_is_empty(struct k_queue *queue);
1959
1960static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001961{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001962 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001963}
1964
1965/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001966 * @brief Peek element at the head of queue.
1967 *
1968 * Return element from the head of queue without removing it.
1969 *
1970 * @param queue Address of the queue.
1971 *
1972 * @return Head element, or NULL if queue is empty.
1973 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001974__syscall void *k_queue_peek_head(struct k_queue *queue);
1975
1976static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001977{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001978 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001979}
1980
1981/**
1982 * @brief Peek element at the tail of queue.
1983 *
1984 * Return element from the tail of queue without removing it.
1985 *
1986 * @param queue Address of the queue.
1987 *
1988 * @return Tail element, or NULL if queue is empty.
1989 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001990__syscall void *k_queue_peek_tail(struct k_queue *queue);
1991
1992static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001993{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001994 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001995}
1996
1997/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001998 * @brief Statically define and initialize a queue.
1999 *
2000 * The queue can be accessed outside the module where it is defined using:
2001 *
2002 * @code extern struct k_queue <name>; @endcode
2003 *
2004 * @param name Name of the queue.
2005 */
2006#define K_QUEUE_DEFINE(name) \
2007 struct k_queue name \
2008 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002009 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002010
Anas Nashif166f5192018-02-25 08:02:36 -06002011/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002012
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002013struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002014 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002015};
2016
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002017/**
2018 * @cond INTERNAL_HIDDEN
2019 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002020#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002021 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002022 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002023 }
2024
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002025#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
2026
Allan Stephensc98da842016-11-11 15:45:03 -05002027/**
2028 * INTERNAL_HIDDEN @endcond
2029 */
2030
2031/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002032 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002033 * @ingroup kernel_apis
2034 * @{
2035 */
2036
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002037/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002038 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002039 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002040 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002041 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002042 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002043 *
2044 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002045 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002046 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002047#define k_fifo_init(fifo) \
2048 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002049
2050/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002051 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002052 *
2053 * This routine causes first thread pending on @a fifo, if any, to
2054 * return from k_fifo_get() call with NULL value (as if timeout
2055 * expired).
2056 *
2057 * @note Can be called by ISRs.
2058 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002059 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002060 *
2061 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002062 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002063 */
2064#define k_fifo_cancel_wait(fifo) \
2065 k_queue_cancel_wait((struct k_queue *) fifo)
2066
2067/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002068 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002069 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002070 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002071 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2072 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002074 * @note Can be called by ISRs.
2075 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002076 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002077 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002078 *
2079 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002080 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002081 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002082#define k_fifo_put(fifo, data) \
2083 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002084
2085/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002086 * @brief Add an element to a FIFO queue.
2087 *
2088 * This routine adds a data item to @a fifo. There is an implicit
2089 * memory allocation from the calling thread's resource pool, which is
2090 * automatically freed when the item is removed.
2091 *
2092 * @note Can be called by ISRs.
2093 *
2094 * @param fifo Address of the FIFO.
2095 * @param data Address of the data item.
2096 *
2097 * @retval 0 on success
2098 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002099 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002100 */
2101#define k_fifo_alloc_put(fifo, data) \
2102 k_queue_alloc_append((struct k_queue *) fifo, data)
2103
2104/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002105 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002107 * This routine adds a list of data items to @a fifo in one operation.
2108 * The data items must be in a singly-linked list, with the first 32 bits
2109 * each data item pointing to the next data item; the list must be
2110 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002112 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002113 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002114 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002115 * @param head Pointer to first node in singly-linked list.
2116 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002117 *
2118 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002119 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002120 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002121#define k_fifo_put_list(fifo, head, tail) \
2122 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002123
2124/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002125 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002127 * This routine adds a list of data items to @a fifo in one operation.
2128 * The data items must be in a singly-linked list implemented using a
2129 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002130 * and must be re-initialized via sys_slist_init().
2131 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002132 * @note Can be called by ISRs.
2133 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002134 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002135 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002136 *
2137 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002138 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002139 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002140#define k_fifo_put_slist(fifo, list) \
2141 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002142
2143/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002144 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002145 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002146 * This routine removes a data item from @a fifo in a "first in, first out"
2147 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002148 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002149 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2150 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002151 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002152 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002153 * or one of the special values K_NO_WAIT and K_FOREVER.
2154 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002155 * @return Address of the data item if successful; NULL if returned
2156 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002157 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002158 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002159#define k_fifo_get(fifo, timeout) \
2160 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002161
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002162/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002163 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002164 *
2165 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002166 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002167 *
2168 * @note Can be called by ISRs.
2169 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002170 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002171 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002172 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002173 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002174 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002175 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002176#define k_fifo_is_empty(fifo) \
2177 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002178
2179/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002180 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002181 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002182 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302183 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002184 * on each iteration of processing, a head container will be peeked,
2185 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002186 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002187 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002188 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002189 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002190 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002191 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002192 */
2193#define k_fifo_peek_head(fifo) \
2194 k_queue_peek_head((struct k_queue *) fifo)
2195
2196/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002197 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002198 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002199 * Return element from the tail of FIFO queue (without removing it). A usecase
2200 * for this is if elements of the FIFO queue are themselves containers. Then
2201 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002202 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002203 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002204 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002205 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002206 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002207 */
2208#define k_fifo_peek_tail(fifo) \
2209 k_queue_peek_tail((struct k_queue *) fifo)
2210
2211/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002212 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002213 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002214 * The FIFO 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_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002217 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002218 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002219 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002220 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002221#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002222 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002223 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002224 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002225
Anas Nashif166f5192018-02-25 08:02:36 -06002226/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002227
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002228struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002229 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002230};
2231
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002232/**
2233 * @cond INTERNAL_HIDDEN
2234 */
2235
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002236#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002237 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002238 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002239 }
2240
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002241#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2242
Allan Stephensc98da842016-11-11 15:45:03 -05002243/**
2244 * INTERNAL_HIDDEN @endcond
2245 */
2246
2247/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002248 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002249 * @ingroup kernel_apis
2250 * @{
2251 */
2252
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002253/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002254 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002255 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002256 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002257 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002258 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002259 *
2260 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002261 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002262 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002263#define k_lifo_init(lifo) \
2264 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002265
2266/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002267 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002268 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002269 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002270 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2271 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002273 * @note Can be called by ISRs.
2274 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002275 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002276 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002277 *
2278 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002279 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002280 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002281#define k_lifo_put(lifo, data) \
2282 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002283
2284/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002285 * @brief Add an element to a LIFO queue.
2286 *
2287 * This routine adds a data item to @a lifo. There is an implicit
2288 * memory allocation from the calling thread's resource pool, which is
2289 * automatically freed when the item is removed.
2290 *
2291 * @note Can be called by ISRs.
2292 *
2293 * @param lifo Address of the LIFO.
2294 * @param data Address of the data item.
2295 *
2296 * @retval 0 on success
2297 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002298 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002299 */
2300#define k_lifo_alloc_put(lifo, data) \
2301 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2302
2303/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002304 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002305 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002306 * This routine removes a data item from @a lifo in a "last in, first out"
2307 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002308 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002309 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2310 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002311 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002312 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002313 * or one of the special values K_NO_WAIT and K_FOREVER.
2314 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002315 * @return Address of the data item if successful; NULL if returned
2316 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002317 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002318 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002319#define k_lifo_get(lifo, timeout) \
2320 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002321
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002322/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002323 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002324 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002325 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002326 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002327 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002328 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002329 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002330 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002331 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002332#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002333 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002334 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002335 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002336
Anas Nashif166f5192018-02-25 08:02:36 -06002337/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002338
2339/**
2340 * @cond INTERNAL_HIDDEN
2341 */
Andrew Boief3bee952018-05-02 17:44:39 -07002342#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002343
2344struct k_stack {
2345 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002346 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002347
Anas Nashif2f203c22016-12-18 06:57:45 -05002348 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002349 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002350};
2351
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002352#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002353 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002354 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002355 .base = stack_buffer, \
2356 .next = stack_buffer, \
2357 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002358 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002359 }
2360
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002361#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2362
Allan Stephensc98da842016-11-11 15:45:03 -05002363/**
2364 * INTERNAL_HIDDEN @endcond
2365 */
2366
2367/**
2368 * @defgroup stack_apis Stack APIs
2369 * @ingroup kernel_apis
2370 * @{
2371 */
2372
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002373/**
2374 * @brief Initialize a stack.
2375 *
2376 * This routine initializes a stack object, prior to its first use.
2377 *
2378 * @param stack Address of the stack.
2379 * @param buffer Address of array used to hold stacked values.
2380 * @param num_entries Maximum number of values that can be stacked.
2381 *
2382 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002383 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002384 */
Andrew Boief3bee952018-05-02 17:44:39 -07002385void k_stack_init(struct k_stack *stack,
2386 u32_t *buffer, unsigned int num_entries);
2387
2388
2389/**
2390 * @brief Initialize a stack.
2391 *
2392 * This routine initializes a stack object, prior to its first use. Internal
2393 * buffers will be allocated from the calling thread's resource pool.
2394 * This memory will be released if k_stack_cleanup() is called, or
2395 * userspace is enabled and the stack object loses all references to it.
2396 *
2397 * @param stack Address of the stack.
2398 * @param num_entries Maximum number of values that can be stacked.
2399 *
2400 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002401 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002402 */
2403
2404__syscall int k_stack_alloc_init(struct k_stack *stack,
2405 unsigned int num_entries);
2406
2407/**
2408 * @brief Release a stack's allocated buffer
2409 *
2410 * If a stack object was given a dynamically allocated buffer via
2411 * k_stack_alloc_init(), this will free it. This function does nothing
2412 * if the buffer wasn't dynamically allocated.
2413 *
2414 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002415 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002416 */
2417void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002418
2419/**
2420 * @brief Push an element onto a stack.
2421 *
2422 * This routine adds a 32-bit value @a data to @a stack.
2423 *
2424 * @note Can be called by ISRs.
2425 *
2426 * @param stack Address of the stack.
2427 * @param data Value to push onto the stack.
2428 *
2429 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002430 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002431 */
Andrew Boiee8734462017-09-29 16:42:07 -07002432__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002433
2434/**
2435 * @brief Pop an element from a stack.
2436 *
2437 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2438 * manner and stores the value in @a data.
2439 *
2440 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2441 *
2442 * @param stack Address of the stack.
2443 * @param data Address of area to hold the value popped from the stack.
2444 * @param timeout Waiting period to obtain a value (in milliseconds),
2445 * or one of the special values K_NO_WAIT and K_FOREVER.
2446 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002447 * @retval 0 Element popped from stack.
2448 * @retval -EBUSY Returned without waiting.
2449 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002450 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002451 */
Andrew Boiee8734462017-09-29 16:42:07 -07002452__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002453
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002454/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002455 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002456 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002457 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002458 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002459 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002460 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002461 * @param name Name of the stack.
2462 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002463 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002464 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002465#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002466 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002467 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002468 struct k_stack name \
2469 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002470 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002471 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002472
Anas Nashif166f5192018-02-25 08:02:36 -06002473/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002474
Allan Stephens6bba9b02016-11-16 14:56:54 -05002475struct k_work;
2476
Allan Stephensc98da842016-11-11 15:45:03 -05002477/**
2478 * @defgroup workqueue_apis Workqueue Thread APIs
2479 * @ingroup kernel_apis
2480 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002481 */
2482
Allan Stephens6bba9b02016-11-16 14:56:54 -05002483/**
2484 * @typedef k_work_handler_t
2485 * @brief Work item handler function type.
2486 *
2487 * A work item's handler function is executed by a workqueue's thread
2488 * when the work item is processed by the workqueue.
2489 *
2490 * @param work Address of the work item.
2491 *
2492 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002493 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002494 */
2495typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002496
2497/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002498 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002499 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002500
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002501struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002502 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002503 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002504};
2505
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002506enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002507 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002508};
2509
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002510struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002511 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002512 k_work_handler_t handler;
2513 atomic_t flags[1];
2514};
2515
Allan Stephens6bba9b02016-11-16 14:56:54 -05002516struct k_delayed_work {
2517 struct k_work work;
2518 struct _timeout timeout;
2519 struct k_work_q *work_q;
2520};
2521
2522extern struct k_work_q k_sys_work_q;
2523
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002524/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002525 * INTERNAL_HIDDEN @endcond
2526 */
2527
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002528#define _K_WORK_INITIALIZER(work_handler) \
2529 { \
2530 ._reserved = NULL, \
2531 .handler = work_handler, \
2532 .flags = { 0 } \
2533 }
2534
2535#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2536
Allan Stephens6bba9b02016-11-16 14:56:54 -05002537/**
2538 * @brief Initialize a statically-defined work item.
2539 *
2540 * This macro can be used to initialize a statically-defined workqueue work
2541 * item, prior to its first use. For example,
2542 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002543 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002544 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002545 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002546 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002547 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002548 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002549#define K_WORK_DEFINE(work, work_handler) \
2550 struct k_work work \
2551 __in_section(_k_work, static, work) = \
2552 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002553
2554/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002555 * @brief Initialize a work item.
2556 *
2557 * This routine initializes a workqueue work item, prior to its first use.
2558 *
2559 * @param work Address of work item.
2560 * @param handler Function to invoke each time work item is processed.
2561 *
2562 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002563 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002564 */
2565static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2566{
Leandro Pereira0e23ad82018-05-29 10:42:17 -07002567 *work = (struct k_work)_K_WORK_INITIALIZER(handler);
Andrew Boie945af952017-08-22 13:15:23 -07002568 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002569}
2570
2571/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002572 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002573 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002574 * This routine submits work item @a work to be processed by workqueue
2575 * @a work_q. If the work item is already pending in the workqueue's queue
2576 * as a result of an earlier submission, this routine has no effect on the
2577 * work item. If the work item has already been processed, or is currently
2578 * being processed, its work is considered complete and the work item can be
2579 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002580 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002581 * @warning
2582 * A submitted work item must not be modified until it has been processed
2583 * by the workqueue.
2584 *
2585 * @note Can be called by ISRs.
2586 *
2587 * @param work_q Address of workqueue.
2588 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002589 *
2590 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002591 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002592 */
2593static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2594 struct k_work *work)
2595{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002596 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002597 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002598 }
2599}
2600
2601/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002602 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002603 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002604 * This routine indicates if work item @a work is pending in a workqueue's
2605 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002606 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002607 * @note Can be called by ISRs.
2608 *
2609 * @param work Address of work item.
2610 *
2611 * @return 1 if work item is pending, or 0 if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002612 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002613 */
2614static inline int k_work_pending(struct k_work *work)
2615{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002616 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002617}
2618
2619/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002620 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002621 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002622 * This routine starts workqueue @a work_q. The workqueue spawns its work
2623 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002624 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002625 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002626 * @param stack Pointer to work queue thread's stack space, as defined by
2627 * K_THREAD_STACK_DEFINE()
2628 * @param stack_size Size of the work queue thread's stack (in bytes), which
2629 * should either be the same constant passed to
2630 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002631 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002632 *
2633 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002634 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002635 */
Andrew Boie507852a2017-07-25 18:47:07 -07002636extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002637 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002638 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002639
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002640/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002641 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002642 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002643 * This routine initializes a workqueue delayed work item, prior to
2644 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002645 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002646 * @param work Address of delayed work item.
2647 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002648 *
2649 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002650 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002651 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002652extern void k_delayed_work_init(struct k_delayed_work *work,
2653 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002654
2655/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002656 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002657 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002658 * This routine schedules work item @a work to be processed by workqueue
2659 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002660 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002661 * Only when the countdown completes is the work item actually submitted to
2662 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002663 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002664 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002665 * counting down cancels the existing submission and restarts the
2666 * countdown using the new delay. Note that this behavior is
2667 * inherently subject to race conditions with the pre-existing
2668 * timeouts and work queue, so care must be taken to synchronize such
2669 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002670 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002671 * @warning
2672 * A delayed work item must not be modified until it has been processed
2673 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002674 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002675 * @note Can be called by ISRs.
2676 *
2677 * @param work_q Address of workqueue.
2678 * @param work Address of delayed work item.
2679 * @param delay Delay before submitting the work item (in milliseconds).
2680 *
2681 * @retval 0 Work item countdown started.
2682 * @retval -EINPROGRESS Work item is already pending.
2683 * @retval -EINVAL Work item is being processed or has completed its work.
2684 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002685 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002686 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002687extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2688 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002689 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002690
2691/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002692 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002693 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002694 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002695 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002696 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002697 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002698 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002699 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002700 * @param work Address of delayed work item.
2701 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002702 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002703 * @retval -EINPROGRESS Work item is already pending.
2704 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002705 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002706 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002707extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002708
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002709/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002710 * @brief Submit a work item to the system workqueue.
2711 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002712 * This routine submits work item @a work to be processed by the system
2713 * workqueue. If the work item is already pending in the workqueue's queue
2714 * as a result of an earlier submission, this routine has no effect on the
2715 * work item. If the work item has already been processed, or is currently
2716 * being processed, its work is considered complete and the work item can be
2717 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002718 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002719 * @warning
2720 * Work items submitted to the system workqueue should avoid using handlers
2721 * that block or yield since this may prevent the system workqueue from
2722 * processing other work items in a timely manner.
2723 *
2724 * @note Can be called by ISRs.
2725 *
2726 * @param work Address of work item.
2727 *
2728 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002729 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002730 */
2731static inline void k_work_submit(struct k_work *work)
2732{
2733 k_work_submit_to_queue(&k_sys_work_q, work);
2734}
2735
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002736/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002737 * @brief Submit a delayed work item to the system workqueue.
2738 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002739 * This routine schedules work item @a work to be processed by the system
2740 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002741 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002742 * Only when the countdown completes is the work item actually submitted to
2743 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002744 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002745 * Submitting a previously submitted delayed work item that is still
2746 * counting down cancels the existing submission and restarts the countdown
2747 * using the new delay. If the work item is currently pending on the
2748 * workqueue's queue because the countdown has completed it is too late to
2749 * resubmit the item, and resubmission fails without impacting the work item.
2750 * If the work item has already been processed, or is currently being processed,
2751 * its work is considered complete and the work item can be resubmitted.
2752 *
2753 * @warning
2754 * Work items submitted to the system workqueue should avoid using handlers
2755 * that block or yield since this may prevent the system workqueue from
2756 * processing other work items in a timely manner.
2757 *
2758 * @note Can be called by ISRs.
2759 *
2760 * @param work Address of delayed work item.
2761 * @param delay Delay before submitting the work item (in milliseconds).
2762 *
2763 * @retval 0 Work item countdown started.
2764 * @retval -EINPROGRESS Work item is already pending.
2765 * @retval -EINVAL Work item is being processed or has completed its work.
2766 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002767 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002768 */
2769static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002770 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002771{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002772 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002773}
2774
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002775/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002776 * @brief Get time remaining before a delayed work gets scheduled.
2777 *
2778 * This routine computes the (approximate) time remaining before a
2779 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002780 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002781 *
2782 * @param work Delayed work item.
2783 *
2784 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002785 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002786 */
Kumar Galacc334c72017-04-21 10:55:34 -05002787static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002788{
2789 return _timeout_remaining_get(&work->timeout);
2790}
2791
Anas Nashif166f5192018-02-25 08:02:36 -06002792/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002793/**
Anas Nashifce78d162018-05-24 12:43:11 -05002794 * @defgroup mutex_apis Mutex APIs
2795 * @ingroup kernel_apis
2796 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002797 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002798
Anas Nashifce78d162018-05-24 12:43:11 -05002799/**
2800 * Mutex Structure
2801 * @ingroup mutex_apis
2802 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002803struct k_mutex {
2804 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002805 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002806 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002807 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002808 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002809
Anas Nashif2f203c22016-12-18 06:57:45 -05002810 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002811};
2812
Anas Nashifce78d162018-05-24 12:43:11 -05002813/**
2814 * @cond INTERNAL_HIDDEN
2815 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002816#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002817 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002818 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002819 .owner = NULL, \
2820 .lock_count = 0, \
2821 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002822 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002823 }
2824
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002825#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2826
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002827/**
Allan Stephensc98da842016-11-11 15:45:03 -05002828 * INTERNAL_HIDDEN @endcond
2829 */
2830
2831/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002832 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002833 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002834 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002835 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002836 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002838 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002839 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002840 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002841#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002842 struct k_mutex name \
2843 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002844 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002845
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002846/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002847 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002849 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002850 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002851 * Upon completion, the mutex is available and does not have an owner.
2852 *
2853 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002854 *
2855 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002856 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002857 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002858__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002859
2860/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002861 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002862 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002863 * This routine locks @a mutex. If the mutex is locked by another thread,
2864 * the calling thread waits until the mutex becomes available or until
2865 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002867 * A thread is permitted to lock a mutex it has already locked. The operation
2868 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002870 * @param mutex Address of the mutex.
2871 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002872 * or one of the special values K_NO_WAIT and K_FOREVER.
2873 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002874 * @retval 0 Mutex locked.
2875 * @retval -EBUSY Returned without waiting.
2876 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002877 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002878 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002879__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002880
2881/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002882 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002884 * This routine unlocks @a mutex. The mutex must already be locked by the
2885 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002886 *
2887 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002888 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002889 * thread.
2890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002891 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002892 *
2893 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002894 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002895 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002896__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002897
Allan Stephensc98da842016-11-11 15:45:03 -05002898/**
Anas Nashif166f5192018-02-25 08:02:36 -06002899 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002900 */
2901
2902/**
2903 * @cond INTERNAL_HIDDEN
2904 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002905
2906struct k_sem {
2907 _wait_q_t wait_q;
2908 unsigned int count;
2909 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002910 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002911
Anas Nashif2f203c22016-12-18 06:57:45 -05002912 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002913};
2914
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002915#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002916 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002917 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002918 .count = initial_count, \
2919 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002920 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002921 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002922 }
2923
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002924#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2925
Allan Stephensc98da842016-11-11 15:45:03 -05002926/**
2927 * INTERNAL_HIDDEN @endcond
2928 */
2929
2930/**
2931 * @defgroup semaphore_apis Semaphore APIs
2932 * @ingroup kernel_apis
2933 * @{
2934 */
2935
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002936/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002937 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002939 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002940 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002941 * @param sem Address of the semaphore.
2942 * @param initial_count Initial semaphore count.
2943 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002944 *
2945 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002946 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002947 */
Andrew Boie99280232017-09-29 14:17:47 -07002948__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2949 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002950
2951/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002952 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002954 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002956 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2957 *
2958 * @param sem Address of the semaphore.
2959 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002960 * or one of the special values K_NO_WAIT and K_FOREVER.
2961 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002962 * @note When porting code from the nanokernel legacy API to the new API, be
2963 * careful with the return value of this function. The return value is the
2964 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2965 * non-zero means failure, while the nano_sem_take family returns 1 for success
2966 * and 0 for failure.
2967 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002968 * @retval 0 Semaphore taken.
2969 * @retval -EBUSY Returned without waiting.
2970 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002971 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002972 */
Andrew Boie99280232017-09-29 14:17:47 -07002973__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002974
2975/**
2976 * @brief Give a semaphore.
2977 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002978 * This routine gives @a sem, unless the semaphore is already at its maximum
2979 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002980 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002981 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002983 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002984 *
2985 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002986 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002987 */
Andrew Boie99280232017-09-29 14:17:47 -07002988__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002989
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002990/**
2991 * @brief Reset a semaphore's count to zero.
2992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002993 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002994 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002995 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002996 *
2997 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002998 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002999 */
Andrew Boie990bf162017-10-03 12:36:49 -07003000__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003001
Anas Nashif954d5502018-02-25 08:37:28 -06003002/**
3003 * @internal
3004 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003005static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003006{
3007 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003008}
3009
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003010/**
3011 * @brief Get a semaphore's count.
3012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003013 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003015 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003016 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003017 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003018 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003019 */
Andrew Boie990bf162017-10-03 12:36:49 -07003020__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003021
Anas Nashif954d5502018-02-25 08:37:28 -06003022/**
3023 * @internal
3024 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003025static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003026{
3027 return sem->count;
3028}
3029
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003030/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003031 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003032 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003033 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003034 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003035 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003036 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003037 * @param name Name of the semaphore.
3038 * @param initial_count Initial semaphore count.
3039 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003040 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003041 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003042#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003043 struct k_sem name \
3044 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003045 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303046 BUILD_ASSERT(((count_limit) != 0) && \
3047 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003048
Anas Nashif166f5192018-02-25 08:02:36 -06003049/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003050
3051/**
3052 * @defgroup alert_apis Alert APIs
3053 * @ingroup kernel_apis
3054 * @{
3055 */
3056
Allan Stephens5eceb852016-11-16 10:16:30 -05003057/**
3058 * @typedef k_alert_handler_t
3059 * @brief Alert handler function type.
3060 *
3061 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07003062 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05003063 * and is only invoked if the alert has been initialized with one.
3064 *
3065 * @param alert Address of the alert.
3066 *
3067 * @return 0 if alert has been consumed; non-zero if alert should pend.
3068 */
3069typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05003070
Anas Nashif166f5192018-02-25 08:02:36 -06003071/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003072
3073/**
3074 * @cond INTERNAL_HIDDEN
3075 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003076
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003077#define K_ALERT_DEFAULT NULL
3078#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003079
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003080struct k_alert {
3081 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003082 atomic_t send_count;
3083 struct k_work work_item;
3084 struct k_sem sem;
3085
Anas Nashif2f203c22016-12-18 06:57:45 -05003086 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003087};
3088
Anas Nashif954d5502018-02-25 08:37:28 -06003089/**
3090 * @internal
3091 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003092extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003093
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003094#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003095 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003096 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003097 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003098 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
3099 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003100 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003101 }
3102
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003103#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
3104
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003105/**
Allan Stephensc98da842016-11-11 15:45:03 -05003106 * INTERNAL_HIDDEN @endcond
3107 */
3108
3109/**
3110 * @addtogroup alert_apis
3111 * @{
3112 */
3113
3114/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003115 * @def K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts)
3116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003117 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003118 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003119 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003120 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003121 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003123 * @param name Name of the alert.
3124 * @param alert_handler Action to take when alert is sent. Specify either
3125 * the address of a function to be invoked by the system workqueue
3126 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
3127 * K_ALERT_DEFAULT (which causes the alert to pend).
3128 * @param max_num_pending_alerts Maximum number of pending alerts.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003129 *
3130 * @req K-ALERT-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003131 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003132#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003133 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003134 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003135 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003136 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003137
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003138/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003139 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003140 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003141 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003142 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003143 * @param alert Address of the alert.
3144 * @param handler Action to take when alert is sent. Specify either the address
3145 * of a function to be invoked by the system workqueue thread,
3146 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3147 * K_ALERT_DEFAULT (which causes the alert to pend).
3148 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003149 *
3150 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003151 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003152 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003153extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3154 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003155
3156/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003157 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003158 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003159 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003160 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003161 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3162 *
3163 * @param alert Address of the alert.
3164 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003165 * or one of the special values K_NO_WAIT and K_FOREVER.
3166 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003167 * @retval 0 Alert received.
3168 * @retval -EBUSY Returned without waiting.
3169 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003170 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003171 */
Andrew Boie310e9872017-09-29 04:41:15 -07003172__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003173
3174/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003175 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003176 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003177 * This routine signals @a alert. The action specified for @a alert will
3178 * be taken, which may trigger the execution of an alert handler function
3179 * and/or cause the alert to pend (assuming the alert has not reached its
3180 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003182 * @note Can be called by ISRs.
3183 *
3184 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003185 *
3186 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003187 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003188 */
Andrew Boie310e9872017-09-29 04:41:15 -07003189__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003190
3191/**
Anas Nashif166f5192018-02-25 08:02:36 -06003192 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003193 */
3194
Allan Stephensc98da842016-11-11 15:45:03 -05003195/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003196 * @defgroup msgq_apis Message Queue APIs
3197 * @ingroup kernel_apis
3198 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003199 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003200
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003201/**
3202 * @brief Message Queue Structure
3203 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003204struct k_msgq {
3205 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003206 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003207 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003208 char *buffer_start;
3209 char *buffer_end;
3210 char *read_ptr;
3211 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003212 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003213
Anas Nashif2f203c22016-12-18 06:57:45 -05003214 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003215 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003216};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003217/**
3218 * @cond INTERNAL_HIDDEN
3219 */
3220
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003221
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003222#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003223 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003224 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003225 .max_msgs = q_max_msgs, \
3226 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003227 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003228 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003229 .read_ptr = q_buffer, \
3230 .write_ptr = q_buffer, \
3231 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003232 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003233 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003234#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003235/**
3236 * INTERNAL_HIDDEN @endcond
3237 */
3238
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003239
Andrew Boie0fe789f2018-04-12 18:35:56 -07003240#define K_MSGQ_FLAG_ALLOC BIT(0)
3241
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003242/**
3243 * @brief Message Queue Attributes
3244 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303245struct k_msgq_attrs {
3246 size_t msg_size;
3247 u32_t max_msgs;
3248 u32_t used_msgs;
3249};
3250
Allan Stephensc98da842016-11-11 15:45:03 -05003251
3252/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003253 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003255 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3256 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003257 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3258 * message is similarly aligned to this boundary, @a q_msg_size must also be
3259 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003260 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003261 * The message queue can be accessed outside the module where it is defined
3262 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003263 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003264 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003265 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003266 * @param q_name Name of the message queue.
3267 * @param q_msg_size Message size (in bytes).
3268 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003269 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003270 *
3271 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003272 */
3273#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003274 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003275 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003276 struct k_msgq q_name \
3277 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003278 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003279 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003280
Peter Mitsisd7a37502016-10-13 11:37:40 -04003281/**
3282 * @brief Initialize a message queue.
3283 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003284 * This routine initializes a message queue object, prior to its first use.
3285 *
Allan Stephensda827222016-11-09 14:23:58 -06003286 * The message queue's ring buffer must contain space for @a max_msgs messages,
3287 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3288 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3289 * that each message is similarly aligned to this boundary, @a q_msg_size
3290 * must also be a multiple of N.
3291 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003292 * @param q Address of the message queue.
3293 * @param buffer Pointer to ring buffer that holds queued messages.
3294 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003295 * @param max_msgs Maximum number of messages that can be queued.
3296 *
3297 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003298 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003299 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003300void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3301 u32_t max_msgs);
3302
3303/**
3304 * @brief Initialize a message queue.
3305 *
3306 * This routine initializes a message queue object, prior to its first use,
3307 * allocating its internal ring buffer from the calling thread's resource
3308 * pool.
3309 *
3310 * Memory allocated for the ring buffer can be released by calling
3311 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3312 * all of its references.
3313 *
3314 * @param q Address of the message queue.
3315 * @param msg_size Message size (in bytes).
3316 * @param max_msgs Maximum number of messages that can be queued.
3317 *
3318 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3319 * thread's resource pool, or -EINVAL if the size parameters cause
3320 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003321 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003322 */
3323__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3324 u32_t max_msgs);
3325
3326
3327void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003328
3329/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003330 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003331 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003332 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003333 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003334 * @note Can be called by ISRs.
3335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003336 * @param q Address of the message queue.
3337 * @param data Pointer to the message.
3338 * @param timeout Waiting period to add the message (in milliseconds),
3339 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003340 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003341 * @retval 0 Message sent.
3342 * @retval -ENOMSG Returned without waiting or queue purged.
3343 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003344 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003345 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003346__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003347
3348/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003349 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003350 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003351 * This routine receives a message from message queue @a q in a "first in,
3352 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003353 *
Allan Stephensc98da842016-11-11 15:45:03 -05003354 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003355 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003356 * @param q Address of the message queue.
3357 * @param data Address of area to hold the received message.
3358 * @param timeout Waiting period to receive the message (in milliseconds),
3359 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003360 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003361 * @retval 0 Message received.
3362 * @retval -ENOMSG Returned without waiting.
3363 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003364 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003365 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003366__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003367
3368/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003369 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003370 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003371 * This routine discards all unreceived messages in a message queue's ring
3372 * buffer. Any threads that are blocked waiting to send a message to the
3373 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003374 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003375 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003376 *
3377 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003378 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003379 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003380__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003381
Peter Mitsis67be2492016-10-07 11:44:34 -04003382/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003383 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003384 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003385 * This routine returns the number of unused entries in a message queue's
3386 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003387 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003388 * @param q Address of the message queue.
3389 *
3390 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003391 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003392 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003393__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3394
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303395/**
3396 * @brief Get basic attributes of a message queue.
3397 *
3398 * This routine fetches basic attributes of message queue into attr argument.
3399 *
3400 * @param q Address of the message queue.
3401 * @param attrs pointer to message queue attribute structure.
3402 *
3403 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003404 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303405 */
3406__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3407
3408
Andrew Boie82edb6e2017-10-02 10:53:06 -07003409static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003410{
3411 return q->max_msgs - q->used_msgs;
3412}
3413
Peter Mitsisd7a37502016-10-13 11:37:40 -04003414/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003415 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003416 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003417 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003418 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003419 * @param q Address of the message queue.
3420 *
3421 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003422 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003423 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003424__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3425
3426static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003427{
3428 return q->used_msgs;
3429}
3430
Anas Nashif166f5192018-02-25 08:02:36 -06003431/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003432
3433/**
3434 * @defgroup mem_pool_apis Memory Pool APIs
3435 * @ingroup kernel_apis
3436 * @{
3437 */
3438
Andy Ross73cb9582017-05-09 10:42:39 -07003439/* Note on sizing: the use of a 20 bit field for block means that,
3440 * assuming a reasonable minimum block size of 16 bytes, we're limited
3441 * to 16M of memory managed by a single pool. Long term it would be
3442 * good to move to a variable bit size based on configuration.
3443 */
3444struct k_mem_block_id {
3445 u32_t pool : 8;
3446 u32_t level : 4;
3447 u32_t block : 20;
3448};
3449
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003450struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003451 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003452 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003453};
3454
Anas Nashif166f5192018-02-25 08:02:36 -06003455/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003456
3457/**
3458 * @defgroup mailbox_apis Mailbox APIs
3459 * @ingroup kernel_apis
3460 * @{
3461 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003462
3463struct k_mbox_msg {
3464 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003465 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003466 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003467 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003468 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003469 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003470 /** sender's message data buffer */
3471 void *tx_data;
3472 /** internal use only - needed for legacy API support */
3473 void *_rx_data;
3474 /** message data block descriptor */
3475 struct k_mem_block tx_block;
3476 /** source thread id */
3477 k_tid_t rx_source_thread;
3478 /** target thread id */
3479 k_tid_t tx_target_thread;
3480 /** internal use only - thread waiting on send (may be a dummy) */
3481 k_tid_t _syncing_thread;
3482#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3483 /** internal use only - semaphore used during asynchronous send */
3484 struct k_sem *_async_sem;
3485#endif
3486};
3487
3488struct k_mbox {
3489 _wait_q_t tx_msg_queue;
3490 _wait_q_t rx_msg_queue;
3491
Anas Nashif2f203c22016-12-18 06:57:45 -05003492 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003493};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003494/**
3495 * @cond INTERNAL_HIDDEN
3496 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003497
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003498#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003499 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003500 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3501 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003502 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003503 }
3504
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003505#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3506
Peter Mitsis12092702016-10-14 12:57:23 -04003507/**
Allan Stephensc98da842016-11-11 15:45:03 -05003508 * INTERNAL_HIDDEN @endcond
3509 */
3510
3511/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003512 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003513 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003514 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003515 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003516 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003518 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003519 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003520 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003521#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003522 struct k_mbox name \
3523 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003524 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003525
Peter Mitsis12092702016-10-14 12:57:23 -04003526/**
3527 * @brief Initialize a mailbox.
3528 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003529 * This routine initializes a mailbox object, prior to its first use.
3530 *
3531 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003532 *
3533 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003534 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003535 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003536extern void k_mbox_init(struct k_mbox *mbox);
3537
Peter Mitsis12092702016-10-14 12:57:23 -04003538/**
3539 * @brief Send a mailbox message in a synchronous manner.
3540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003541 * This routine sends a message to @a mbox and waits for a receiver to both
3542 * receive and process it. The message data may be in a buffer, in a memory
3543 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003544 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003545 * @param mbox Address of the mailbox.
3546 * @param tx_msg Address of the transmit message descriptor.
3547 * @param timeout Waiting period for the message to be received (in
3548 * milliseconds), or one of the special values K_NO_WAIT
3549 * and K_FOREVER. Once the message has been received,
3550 * this routine waits as long as necessary for the message
3551 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003552 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003553 * @retval 0 Message sent.
3554 * @retval -ENOMSG Returned without waiting.
3555 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003556 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003557 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003558extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003559 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003560
Peter Mitsis12092702016-10-14 12:57:23 -04003561/**
3562 * @brief Send a mailbox message in an asynchronous manner.
3563 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003564 * This routine sends a message to @a mbox without waiting for a receiver
3565 * to process it. The message data may be in a buffer, in a memory pool block,
3566 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3567 * will be given when the message has been both received and completely
3568 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003569 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003570 * @param mbox Address of the mailbox.
3571 * @param tx_msg Address of the transmit message descriptor.
3572 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003573 *
3574 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003575 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003576 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003577extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003578 struct k_sem *sem);
3579
Peter Mitsis12092702016-10-14 12:57:23 -04003580/**
3581 * @brief Receive a mailbox message.
3582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003583 * This routine receives a message from @a mbox, then optionally retrieves
3584 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003585 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003586 * @param mbox Address of the mailbox.
3587 * @param rx_msg Address of the receive message descriptor.
3588 * @param buffer Address of the buffer to receive data, or NULL to defer data
3589 * retrieval and message disposal until later.
3590 * @param timeout Waiting period for a message to be received (in
3591 * milliseconds), or one of the special values K_NO_WAIT
3592 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003593 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003594 * @retval 0 Message received.
3595 * @retval -ENOMSG Returned without waiting.
3596 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003597 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003598 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003599extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003600 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003601
3602/**
3603 * @brief Retrieve mailbox message data into a buffer.
3604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003605 * This routine completes the processing of a received message by retrieving
3606 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003607 *
3608 * Alternatively, this routine can be used to dispose of a received message
3609 * without retrieving its data.
3610 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003611 * @param rx_msg Address of the receive message descriptor.
3612 * @param buffer Address of the buffer to receive data, or NULL to discard
3613 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003614 *
3615 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003616 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003617 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003618extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003619
3620/**
3621 * @brief Retrieve mailbox message data into a memory pool block.
3622 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003623 * This routine completes the processing of a received message by retrieving
3624 * its data into a memory pool block, then disposing of the message.
3625 * The memory pool block that results from successful retrieval must be
3626 * returned to the pool once the data has been processed, even in cases
3627 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003628 *
3629 * Alternatively, this routine can be used to dispose of a received message
3630 * without retrieving its data. In this case there is no need to return a
3631 * memory pool block to the pool.
3632 *
3633 * This routine allocates a new memory pool block for the data only if the
3634 * data is not already in one. If a new block cannot be allocated, the routine
3635 * returns a failure code and the received message is left unchanged. This
3636 * permits the caller to reattempt data retrieval at a later time or to dispose
3637 * of the received message without retrieving its data.
3638 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003639 * @param rx_msg Address of a receive message descriptor.
3640 * @param pool Address of memory pool, or NULL to discard data.
3641 * @param block Address of the area to hold memory pool block info.
3642 * @param timeout Waiting period to wait for a memory pool block (in
3643 * milliseconds), or one of the special values K_NO_WAIT
3644 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003645 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003646 * @retval 0 Data retrieved.
3647 * @retval -ENOMEM Returned without waiting.
3648 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003649 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003650 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003651extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003652 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003653 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003654
Anas Nashif166f5192018-02-25 08:02:36 -06003655/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003656
3657/**
Anas Nashifce78d162018-05-24 12:43:11 -05003658 * @defgroup pipe_apis Pipe APIs
3659 * @ingroup kernel_apis
3660 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003661 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003662
Anas Nashifce78d162018-05-24 12:43:11 -05003663/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003664struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003665 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3666 size_t size; /**< Buffer size */
3667 size_t bytes_used; /**< # bytes used in buffer */
3668 size_t read_index; /**< Where in buffer to read from */
3669 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003670
3671 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003672 _wait_q_t readers; /**< Reader wait queue */
3673 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003674 } wait_q;
3675
Anas Nashif2f203c22016-12-18 06:57:45 -05003676 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Anas Nashifce78d162018-05-24 12:43:11 -05003677 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003678};
3679
Anas Nashifce78d162018-05-24 12:43:11 -05003680/**
3681 * @cond INTERNAL_HIDDEN
3682 */
3683#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3684
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003685#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003686 { \
3687 .buffer = pipe_buffer, \
3688 .size = pipe_buffer_size, \
3689 .bytes_used = 0, \
3690 .read_index = 0, \
3691 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003692 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3693 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003694 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003695 }
3696
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003697#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3698
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003699/**
Allan Stephensc98da842016-11-11 15:45:03 -05003700 * INTERNAL_HIDDEN @endcond
3701 */
3702
3703/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003704 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003706 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003707 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003708 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003709 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003710 * @param name Name of the pipe.
3711 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3712 * or zero if no ring buffer is used.
3713 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003714 *
3715 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003716 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003717#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3718 static unsigned char __kernel_noinit __aligned(pipe_align) \
3719 _k_pipe_buf_##name[pipe_buffer_size]; \
3720 struct k_pipe name \
3721 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003722 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003723
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003724/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003725 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003726 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003727 * This routine initializes a pipe object, prior to its first use.
3728 *
3729 * @param pipe Address of the pipe.
3730 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3731 * is used.
3732 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3733 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003734 *
3735 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003736 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003737 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003738void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3739
3740/**
3741 * @brief Release a pipe's allocated buffer
3742 *
3743 * If a pipe object was given a dynamically allocated buffer via
3744 * k_pipe_alloc_init(), this will free it. This function does nothing
3745 * if the buffer wasn't dynamically allocated.
3746 *
3747 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003748 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003749 */
3750void k_pipe_cleanup(struct k_pipe *pipe);
3751
3752/**
3753 * @brief Initialize a pipe and allocate a buffer for it
3754 *
3755 * Storage for the buffer region will be allocated from the calling thread's
3756 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3757 * or userspace is enabled and the pipe object loses all references to it.
3758 *
3759 * This function should only be called on uninitialized pipe objects.
3760 *
3761 * @param pipe Address of the pipe.
3762 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3763 * buffer is used.
3764 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003765 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003766 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003767 */
3768__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003769
3770/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003771 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003772 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003773 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003775 * @param pipe Address of the pipe.
3776 * @param data Address of data to write.
3777 * @param bytes_to_write Size of data (in bytes).
3778 * @param bytes_written Address of area to hold the number of bytes written.
3779 * @param min_xfer Minimum number of bytes to write.
3780 * @param timeout Waiting period to wait for the data to be written (in
3781 * milliseconds), or one of the special values K_NO_WAIT
3782 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003783 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003784 * @retval 0 At least @a min_xfer bytes of data were written.
3785 * @retval -EIO Returned without waiting; zero data bytes were written.
3786 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003787 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003788 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003789 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003790__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3791 size_t bytes_to_write, size_t *bytes_written,
3792 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003793
3794/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003795 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003796 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003797 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003799 * @param pipe Address of the pipe.
3800 * @param data Address to place the data read from pipe.
3801 * @param bytes_to_read Maximum number of data bytes to read.
3802 * @param bytes_read Address of area to hold the number of bytes read.
3803 * @param min_xfer Minimum number of data bytes to read.
3804 * @param timeout Waiting period to wait for the data to be read (in
3805 * milliseconds), or one of the special values K_NO_WAIT
3806 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003807 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003808 * @retval 0 At least @a min_xfer bytes of data were read.
3809 * @retval -EIO Returned without waiting; zero data bytes were read.
3810 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003811 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003812 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003813 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003814__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3815 size_t bytes_to_read, size_t *bytes_read,
3816 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003817
3818/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003819 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003820 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003821 * This routine writes the data contained in a memory block to @a pipe.
3822 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003823 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003825 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003826 * @param block Memory block containing data to send
3827 * @param size Number of data bytes in memory block to send
3828 * @param sem Semaphore to signal upon completion (else NULL)
3829 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003830 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003831 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003832 */
3833extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3834 size_t size, struct k_sem *sem);
3835
Anas Nashif166f5192018-02-25 08:02:36 -06003836/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003837
Allan Stephensc98da842016-11-11 15:45:03 -05003838/**
3839 * @cond INTERNAL_HIDDEN
3840 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003841
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003842struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003843 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003844 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003845 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003846 char *buffer;
3847 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003848 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003849
Anas Nashif2f203c22016-12-18 06:57:45 -05003850 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003851};
3852
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003853#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003854 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003855 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003856 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003857 .num_blocks = slab_num_blocks, \
3858 .block_size = slab_block_size, \
3859 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003860 .free_list = NULL, \
3861 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003862 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003863 }
3864
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003865#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3866
3867
Peter Mitsis578f9112016-10-07 13:50:31 -04003868/**
Allan Stephensc98da842016-11-11 15:45:03 -05003869 * INTERNAL_HIDDEN @endcond
3870 */
3871
3872/**
3873 * @defgroup mem_slab_apis Memory Slab APIs
3874 * @ingroup kernel_apis
3875 * @{
3876 */
3877
3878/**
Allan Stephensda827222016-11-09 14:23:58 -06003879 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003880 *
Allan Stephensda827222016-11-09 14:23:58 -06003881 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003882 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003883 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3884 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003885 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003886 *
Allan Stephensda827222016-11-09 14:23:58 -06003887 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003888 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003889 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003890 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003892 * @param name Name of the memory slab.
3893 * @param slab_block_size Size of each memory block (in bytes).
3894 * @param slab_num_blocks Number memory blocks.
3895 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003896 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003897 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003898#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3899 char __noinit __aligned(slab_align) \
3900 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3901 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003902 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003903 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003904 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003905
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003906/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003907 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003908 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003909 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003910 *
Allan Stephensda827222016-11-09 14:23:58 -06003911 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3912 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3913 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3914 * To ensure that each memory block is similarly aligned to this boundary,
3915 * @a slab_block_size must also be a multiple of N.
3916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003917 * @param slab Address of the memory slab.
3918 * @param buffer Pointer to buffer used for the memory blocks.
3919 * @param block_size Size of each memory block (in bytes).
3920 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003921 *
3922 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003923 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003924 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003925extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003926 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003927
3928/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003929 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003930 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003931 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003933 * @param slab Address of the memory slab.
3934 * @param mem Pointer to block address area.
3935 * @param timeout Maximum time to wait for operation to complete
3936 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3937 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003938 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003939 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003940 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003941 * @retval -ENOMEM Returned without waiting.
3942 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003943 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003944 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003945extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003946 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003947
3948/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003949 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003950 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003951 * This routine releases a previously allocated memory block back to its
3952 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003954 * @param slab Address of the memory slab.
3955 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003956 *
3957 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003958 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003959 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003960extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003961
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003962/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003963 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003964 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003965 * This routine gets the number of memory blocks that are currently
3966 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003967 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003968 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003969 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003970 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003971 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003972 */
Kumar Galacc334c72017-04-21 10:55:34 -05003973static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003974{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003975 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003976}
3977
Peter Mitsisc001aa82016-10-13 13:53:37 -04003978/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003979 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003980 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003981 * This routine gets the number of memory blocks that are currently
3982 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003983 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003984 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003986 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003987 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04003988 */
Kumar Galacc334c72017-04-21 10:55:34 -05003989static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003990{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003991 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003992}
3993
Anas Nashif166f5192018-02-25 08:02:36 -06003994/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003995
3996/**
3997 * @cond INTERNAL_HIDDEN
3998 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003999
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004000struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004001 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004002 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004003};
4004
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004005/**
Allan Stephensc98da842016-11-11 15:45:03 -05004006 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004007 */
4008
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004009/**
Allan Stephensc98da842016-11-11 15:45:03 -05004010 * @addtogroup mem_pool_apis
4011 * @{
4012 */
4013
4014/**
4015 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004016 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004017 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4018 * long. The memory pool allows blocks to be repeatedly partitioned into
4019 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004020 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004021 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004022 * If the pool is to be accessed outside the module where it is defined, it
4023 * can be declared via
4024 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004025 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004026 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004027 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004028 * @param minsz Size of the smallest blocks in the pool (in bytes).
4029 * @param maxsz Size of the largest blocks in the pool (in bytes).
4030 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004031 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004032 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004033 */
Andy Ross73cb9582017-05-09 10:42:39 -07004034#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4035 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4036 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08004037 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07004038 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004039 .base = { \
4040 .buf = _mpool_buf_##name, \
4041 .max_sz = maxsz, \
4042 .n_max = nmax, \
4043 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
4044 .levels = _mpool_lvls_##name, \
4045 .flags = SYS_MEM_POOL_KERNEL \
4046 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004047 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004048
Peter Mitsis937042c2016-10-13 13:18:26 -04004049/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004050 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004051 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004052 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004053 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004054 * @param pool Address of the memory pool.
4055 * @param block Pointer to block descriptor for the allocated memory.
4056 * @param size Amount of memory to allocate (in bytes).
4057 * @param timeout Maximum time to wait for operation to complete
4058 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4059 * or K_FOREVER to wait as long as necessary.
4060 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004061 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004062 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004063 * @retval -ENOMEM Returned without waiting.
4064 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004065 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004066 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004067extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004068 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004069
4070/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004071 * @brief Allocate memory from a memory pool with malloc() semantics
4072 *
4073 * Such memory must be released using k_free().
4074 *
4075 * @param pool Address of the memory pool.
4076 * @param size Amount of memory to allocate (in bytes).
4077 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004078 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004079 */
4080extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4081
4082/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004083 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004084 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004085 * This routine releases a previously allocated memory block back to its
4086 * memory pool.
4087 *
4088 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004089 *
4090 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004091 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004092 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004093extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004094
4095/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004096 * @brief Free memory allocated from a memory pool.
4097 *
4098 * This routine releases a previously allocated memory block back to its
4099 * memory pool
4100 *
4101 * @param id Memory block identifier.
4102 *
4103 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004104 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004105 */
4106extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4107
4108/**
Anas Nashif166f5192018-02-25 08:02:36 -06004109 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004110 */
4111
4112/**
4113 * @defgroup heap_apis Heap Memory Pool APIs
4114 * @ingroup kernel_apis
4115 * @{
4116 */
4117
4118/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004119 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004120 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004121 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004122 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004124 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004125 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004126 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004127 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004128 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004129extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004130
4131/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004132 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004133 *
4134 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004135 * returned must have been allocated from the heap memory pool or
4136 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004137 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004138 * If @a ptr is NULL, no operation is performed.
4139 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004140 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004141 *
4142 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004143 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004144 */
4145extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004146
Allan Stephensc98da842016-11-11 15:45:03 -05004147/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004148 * @brief Allocate memory from heap, array style
4149 *
4150 * This routine provides traditional calloc() semantics. Memory is
4151 * allocated from the heap memory pool and zeroed.
4152 *
4153 * @param nmemb Number of elements in the requested array
4154 * @param size Size of each array element (in bytes).
4155 *
4156 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004157 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004158 */
4159extern void *k_calloc(size_t nmemb, size_t size);
4160
Anas Nashif166f5192018-02-25 08:02:36 -06004161/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004162
Benjamin Walshacc68c12017-01-29 18:57:45 -05004163/* polling API - PRIVATE */
4164
Benjamin Walshb0179862017-02-02 16:39:57 -05004165#ifdef CONFIG_POLL
4166#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
4167#else
4168#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
4169#endif
4170
Benjamin Walshacc68c12017-01-29 18:57:45 -05004171/* private - implementation data created as needed, per-type */
4172struct _poller {
4173 struct k_thread *thread;
Andy Ross55a7e462018-05-31 11:58:09 -07004174 volatile int is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004175};
4176
4177/* private - types bit positions */
4178enum _poll_types_bits {
4179 /* can be used to ignore an event */
4180 _POLL_TYPE_IGNORE,
4181
4182 /* to be signaled by k_poll_signal() */
4183 _POLL_TYPE_SIGNAL,
4184
4185 /* semaphore availability */
4186 _POLL_TYPE_SEM_AVAILABLE,
4187
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004188 /* queue/fifo/lifo data availability */
4189 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004190
4191 _POLL_NUM_TYPES
4192};
4193
4194#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4195
4196/* private - states bit positions */
4197enum _poll_states_bits {
4198 /* default state when creating event */
4199 _POLL_STATE_NOT_READY,
4200
Benjamin Walshacc68c12017-01-29 18:57:45 -05004201 /* signaled by k_poll_signal() */
4202 _POLL_STATE_SIGNALED,
4203
4204 /* semaphore is available */
4205 _POLL_STATE_SEM_AVAILABLE,
4206
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004207 /* data is available to read on queue/fifo/lifo */
4208 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004209
4210 _POLL_NUM_STATES
4211};
4212
4213#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4214
4215#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004216 (32 - (0 \
4217 + 8 /* tag */ \
4218 + _POLL_NUM_TYPES \
4219 + _POLL_NUM_STATES \
4220 + 1 /* modes */ \
4221 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004222
Benjamin Walshacc68c12017-01-29 18:57:45 -05004223/* end of polling API - PRIVATE */
4224
4225
4226/**
4227 * @defgroup poll_apis Async polling APIs
4228 * @ingroup kernel_apis
4229 * @{
4230 */
4231
4232/* Public polling API */
4233
4234/* public - values for k_poll_event.type bitfield */
4235#define K_POLL_TYPE_IGNORE 0
4236#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4237#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004238#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4239#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004240
4241/* public - polling modes */
4242enum k_poll_modes {
4243 /* polling thread does not take ownership of objects when available */
4244 K_POLL_MODE_NOTIFY_ONLY = 0,
4245
4246 K_POLL_NUM_MODES
4247};
4248
4249/* public - values for k_poll_event.state bitfield */
4250#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004251#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4252#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004253#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4254#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004255
4256/* public - poll signal object */
4257struct k_poll_signal {
4258 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004259 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004260
4261 /*
4262 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4263 * user resets it to 0.
4264 */
4265 unsigned int signaled;
4266
4267 /* custom result value passed to k_poll_signal() if needed */
4268 int result;
4269};
4270
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004271#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004272 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004273 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004274 .signaled = 0, \
4275 .result = 0, \
4276 }
4277
4278struct k_poll_event {
4279 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004280 sys_dnode_t _node;
4281
4282 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004283 struct _poller *poller;
4284
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004285 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004286 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004287
Benjamin Walshacc68c12017-01-29 18:57:45 -05004288 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004289 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004290
4291 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004292 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004293
4294 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004295 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004296
4297 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004298 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004299
4300 /* per-type data */
4301 union {
4302 void *obj;
4303 struct k_poll_signal *signal;
4304 struct k_sem *sem;
4305 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004306 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004307 };
4308};
4309
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004310#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004311 { \
4312 .poller = NULL, \
4313 .type = event_type, \
4314 .state = K_POLL_STATE_NOT_READY, \
4315 .mode = event_mode, \
4316 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004317 { .obj = event_obj }, \
4318 }
4319
4320#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4321 event_tag) \
4322 { \
4323 .type = event_type, \
4324 .tag = event_tag, \
4325 .state = K_POLL_STATE_NOT_READY, \
4326 .mode = event_mode, \
4327 .unused = 0, \
4328 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004329 }
4330
4331/**
4332 * @brief Initialize one struct k_poll_event instance
4333 *
4334 * After this routine is called on a poll event, the event it ready to be
4335 * placed in an event array to be passed to k_poll().
4336 *
4337 * @param event The event to initialize.
4338 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4339 * values. Only values that apply to the same object being polled
4340 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4341 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004342 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004343 * @param obj Kernel object or poll signal.
4344 *
4345 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004346 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004347 */
4348
Kumar Galacc334c72017-04-21 10:55:34 -05004349extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004350 int mode, void *obj);
4351
4352/**
4353 * @brief Wait for one or many of multiple poll events to occur
4354 *
4355 * This routine allows a thread to wait concurrently for one or many of
4356 * multiple poll events to have occurred. Such events can be a kernel object
4357 * being available, like a semaphore, or a poll signal event.
4358 *
4359 * When an event notifies that a kernel object is available, the kernel object
4360 * is not "given" to the thread calling k_poll(): it merely signals the fact
4361 * that the object was available when the k_poll() call was in effect. Also,
4362 * all threads trying to acquire an object the regular way, i.e. by pending on
4363 * the object, have precedence over the thread polling on the object. This
4364 * means that the polling thread will never get the poll event on an object
4365 * until the object becomes available and its pend queue is empty. For this
4366 * reason, the k_poll() call is more effective when the objects being polled
4367 * only have one thread, the polling thread, trying to acquire them.
4368 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004369 * When k_poll() returns 0, the caller should loop on all the events that were
4370 * passed to k_poll() and check the state field for the values that were
4371 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004372 *
4373 * Before being reused for another call to k_poll(), the user has to reset the
4374 * state field to K_POLL_STATE_NOT_READY.
4375 *
Andrew Boie3772f772018-05-07 16:52:57 -07004376 * When called from user mode, a temporary memory allocation is required from
4377 * the caller's resource pool.
4378 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004379 * @param events An array of pointers to events to be polled for.
4380 * @param num_events The number of events in the array.
4381 * @param timeout Waiting period for an event to be ready (in milliseconds),
4382 * or one of the special values K_NO_WAIT and K_FOREVER.
4383 *
4384 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004385 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02004386 * @retval -EINTR Poller thread has been interrupted.
Andrew Boie3772f772018-05-07 16:52:57 -07004387 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4388 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004389 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004390 */
4391
Andrew Boie3772f772018-05-07 16:52:57 -07004392__syscall int k_poll(struct k_poll_event *events, int num_events,
4393 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004394
4395/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004396 * @brief Initialize a poll signal object.
4397 *
4398 * Ready a poll signal object to be signaled via k_poll_signal().
4399 *
4400 * @param signal A poll signal.
4401 *
4402 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004403 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004404 */
4405
Andrew Boie3772f772018-05-07 16:52:57 -07004406__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4407
4408/*
4409 * @brief Reset a poll signal object's state to unsignaled.
4410 *
4411 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004412 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004413 */
4414__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4415
4416static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4417{
4418 signal->signaled = 0;
4419}
4420
4421/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004422 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004423 *
4424 * @param signal A poll signal object
4425 * @param signaled An integer buffer which will be written nonzero if the
4426 * object was signaled
4427 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004428 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004429 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004430 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004431 */
4432__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4433 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004434
4435/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004436 * @brief Signal a poll signal object.
4437 *
4438 * This routine makes ready a poll signal, which is basically a poll event of
4439 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4440 * made ready to run. A @a result value can be specified.
4441 *
4442 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004443 * k_poll_signal(), stays set until the user sets it back to 0 with
4444 * k_poll_signal_reset(). It thus has to be reset by the user before being
4445 * passed again to k_poll() or k_poll() will consider it being signaled, and
4446 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004447 *
4448 * @param signal A poll signal.
4449 * @param result The value to store in the result field of the signal.
4450 *
4451 * @retval 0 The signal was delivered successfully.
4452 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004453 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004454 */
4455
Andrew Boie3772f772018-05-07 16:52:57 -07004456__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004457
Anas Nashif954d5502018-02-25 08:37:28 -06004458/**
4459 * @internal
4460 */
Andy Ross8606fab2018-03-26 10:54:40 -07004461extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004462
Anas Nashif166f5192018-02-25 08:02:36 -06004463/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004464
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004465/**
4466 * @brief Make the CPU idle.
4467 *
4468 * This function makes the CPU idle until an event wakes it up.
4469 *
4470 * In a regular system, the idle thread should be the only thread responsible
4471 * for making the CPU idle and triggering any type of power management.
4472 * However, in some more constrained systems, such as a single-threaded system,
4473 * the only thread would be responsible for this if needed.
4474 *
4475 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004476 * @req K-MISC-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004477 */
4478extern void k_cpu_idle(void);
4479
4480/**
4481 * @brief Make the CPU idle in an atomic fashion.
4482 *
4483 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4484 * must be done atomically before making the CPU idle.
4485 *
4486 * @param key Interrupt locking key obtained from irq_lock().
4487 *
4488 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004489 * @req K-MISC-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004490 */
4491extern void k_cpu_atomic_idle(unsigned int key);
4492
Anas Nashif954d5502018-02-25 08:37:28 -06004493
4494/**
4495 * @internal
4496 */
Kumar Galacc334c72017-04-21 10:55:34 -05004497extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004498
Andrew Boiecdb94d62017-04-18 15:22:05 -07004499#ifdef _ARCH_EXCEPT
4500/* This archtecture has direct support for triggering a CPU exception */
4501#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4502#else
4503
Andrew Boiecdb94d62017-04-18 15:22:05 -07004504/* NOTE: This is the implementation for arches that do not implement
4505 * _ARCH_EXCEPT() to generate a real CPU exception.
4506 *
4507 * We won't have a real exception frame to determine the PC value when
4508 * the oops occurred, so print file and line number before we jump into
4509 * the fatal error handler.
4510 */
4511#define _k_except_reason(reason) do { \
4512 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4513 _NanoFatalErrorHandler(reason, &_default_esf); \
4514 CODE_UNREACHABLE; \
4515 } while (0)
4516
4517#endif /* _ARCH__EXCEPT */
4518
4519/**
4520 * @brief Fatally terminate a thread
4521 *
4522 * This should be called when a thread has encountered an unrecoverable
4523 * runtime condition and needs to terminate. What this ultimately
4524 * means is determined by the _fatal_error_handler() implementation, which
4525 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4526 *
4527 * If this is called from ISR context, the default system fatal error handler
4528 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004529 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004530 */
4531#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4532
4533/**
4534 * @brief Fatally terminate the system
4535 *
4536 * This should be called when the Zephyr kernel has encountered an
4537 * unrecoverable runtime condition and needs to terminate. What this ultimately
4538 * means is determined by the _fatal_error_handler() implementation, which
4539 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004540 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004541 */
4542#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4543
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004544/*
4545 * private APIs that are utilized by one or more public APIs
4546 */
4547
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004548#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004549/**
4550 * @internal
4551 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004552extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004553#else
Anas Nashif954d5502018-02-25 08:37:28 -06004554/**
4555 * @internal
4556 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004557#define _init_static_threads() do { } while ((0))
4558#endif
4559
Anas Nashif954d5502018-02-25 08:37:28 -06004560/**
4561 * @internal
4562 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004563extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004564/**
4565 * @internal
4566 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004567extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004568
Andrew Boiedc5d9352017-06-02 12:56:47 -07004569/* arch/cpu.h may declare an architecture or platform-specific macro
4570 * for properly declaring stacks, compatible with MMU/MPU constraints if
4571 * enabled
4572 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004573
4574/**
4575 * @brief Obtain an extern reference to a stack
4576 *
4577 * This macro properly brings the symbol of a thread stack declared
4578 * elsewhere into scope.
4579 *
4580 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004581 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004582 */
4583#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4584
Andrew Boiedc5d9352017-06-02 12:56:47 -07004585#ifdef _ARCH_THREAD_STACK_DEFINE
4586#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4587#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4588 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4589#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4590#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004591static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004592{
4593 return _ARCH_THREAD_STACK_BUFFER(sym);
4594}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004595#else
4596/**
4597 * @brief Declare a toplevel thread stack memory region
4598 *
4599 * This declares a region of memory suitable for use as a thread's stack.
4600 *
4601 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4602 * 'noinit' section so that it isn't zeroed at boot
4603 *
Andrew Boie507852a2017-07-25 18:47:07 -07004604 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004605 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004606 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004607 *
4608 * It is legal to precede this definition with the 'static' keyword.
4609 *
4610 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4611 * parameter of k_thread_create(), it may not be the same as the
4612 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4613 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004614 * Some arches may round the size of the usable stack region up to satisfy
4615 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4616 * size.
4617 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004618 * @param sym Thread stack symbol name
4619 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004620 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004621 */
4622#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004623 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004624
4625/**
4626 * @brief Declare a toplevel array of thread stack memory regions
4627 *
4628 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4629 * definition for additional details and constraints.
4630 *
4631 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4632 * 'noinit' section so that it isn't zeroed at boot
4633 *
4634 * @param sym Thread stack symbol name
4635 * @param nmemb Number of stacks to declare
4636 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004637 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004638 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004639#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004640 struct _k_thread_stack_element __noinit \
4641 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004642
4643/**
4644 * @brief Declare an embedded stack memory region
4645 *
4646 * Used for stacks embedded within other data structures. Use is highly
4647 * discouraged but in some cases necessary. For memory protection scenarios,
4648 * it is very important that any RAM preceding this member not be writable
4649 * by threads else a stack overflow will lead to silent corruption. In other
4650 * words, the containing data structure should live in RAM owned by the kernel.
4651 *
4652 * @param sym Thread stack symbol name
4653 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004654 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004655 */
4656#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004657 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004658
4659/**
4660 * @brief Return the size in bytes of a stack memory region
4661 *
4662 * Convenience macro for passing the desired stack size to k_thread_create()
4663 * since the underlying implementation may actually create something larger
4664 * (for instance a guard area).
4665 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004666 * The value returned here is not guaranteed to match the 'size' parameter
4667 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004668 *
4669 * @param sym Stack memory symbol
4670 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004671 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004672 */
4673#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4674
4675/**
4676 * @brief Get a pointer to the physical stack buffer
4677 *
4678 * Convenience macro to get at the real underlying stack buffer used by
4679 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4680 * This is really only intended for diagnostic tools which want to examine
4681 * stack memory contents.
4682 *
4683 * @param sym Declared stack symbol name
4684 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004685 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004686 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004687static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004688{
4689 return (char *)sym;
4690}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004691
4692#endif /* _ARCH_DECLARE_STACK */
4693
Chunlin Hane9c97022017-07-07 20:29:30 +08004694/**
4695 * @defgroup mem_domain_apis Memory domain APIs
4696 * @ingroup kernel_apis
4697 * @{
4698 */
4699
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004700/**
4701 * @def MEM_PARTITION_ENTRY
4702 * @brief Used to declare a memory partition entry
4703 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004704 */
4705#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4706 {\
4707 .start = _start, \
4708 .size = _size, \
4709 .attr = _attr, \
4710 }
4711
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004712/**
4713 * @def K_MEM_PARTITION_DEFINE
4714 * @brief Used to declare a memory partition
4715 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004716 */
4717#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4718#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4719 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304720 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004721 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4722#else
4723#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304724 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004725 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4726#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4727
Chunlin Hane9c97022017-07-07 20:29:30 +08004728/* memory partition */
4729struct k_mem_partition {
4730 /* start address of memory partition */
4731 u32_t start;
4732 /* size of memory partition */
4733 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304734#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004735 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304736 k_mem_partition_attr_t attr;
4737#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004738};
4739
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304740/* memory domian
4741 * Note: Always declare this structure with __kernel prefix
4742 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004743struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304744#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004745 /* partitions in the domain */
4746 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304747#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004748 /* domain q */
4749 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004750 /* number of partitions in the domain */
4751 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004752};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304753
Chunlin Hane9c97022017-07-07 20:29:30 +08004754
4755/**
4756 * @brief Initialize a memory domain.
4757 *
4758 * Initialize a memory domain with given name and memory partitions.
4759 *
4760 * @param domain The memory domain to be initialized.
4761 * @param num_parts The number of array items of "parts" parameter.
4762 * @param parts An array of pointers to the memory partitions. Can be NULL
4763 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004764 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004765 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004766extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004767 struct k_mem_partition *parts[]);
4768/**
4769 * @brief Destroy a memory domain.
4770 *
4771 * Destroy a memory domain.
4772 *
4773 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004774 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004775 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004776extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4777
4778/**
4779 * @brief Add a memory partition into a memory domain.
4780 *
4781 * Add a memory partition into a memory domain.
4782 *
4783 * @param domain The memory domain to be added a memory partition.
4784 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004785 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004786 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004787extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4788 struct k_mem_partition *part);
4789
4790/**
4791 * @brief Remove a memory partition from a memory domain.
4792 *
4793 * Remove a memory partition from a memory domain.
4794 *
4795 * @param domain The memory domain to be removed a memory partition.
4796 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004797 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004798 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004799extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4800 struct k_mem_partition *part);
4801
4802/**
4803 * @brief Add a thread into a memory domain.
4804 *
4805 * Add a thread into a memory domain.
4806 *
4807 * @param domain The memory domain that the thread is going to be added into.
4808 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004809 *
4810 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004811 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004812extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4813 k_tid_t thread);
4814
4815/**
4816 * @brief Remove a thread from its memory domain.
4817 *
4818 * Remove a thread from its memory domain.
4819 *
4820 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004821 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004822 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004823extern void k_mem_domain_remove_thread(k_tid_t thread);
4824
Anas Nashif166f5192018-02-25 08:02:36 -06004825/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004826
Andrew Boie756f9072017-10-10 16:01:49 -07004827/**
4828 * @brief Emit a character buffer to the console device
4829 *
4830 * @param c String of characters to print
4831 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004832 *
4833 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004834 */
4835__syscall void k_str_out(char *c, size_t n);
4836
Andy Rosse7172672018-01-24 15:48:32 -08004837/**
4838 * @brief Start a numbered CPU on a MP-capable system
4839
4840 * This starts and initializes a specific CPU. The main thread on
4841 * startup is running on CPU zero, other processors are numbered
4842 * sequentially. On return from this function, the CPU is known to
4843 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004844 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004845 * with the provided key will work to enable them.
4846 *
4847 * Normally, in SMP mode this function will be called by the kernel
4848 * initialization and should not be used as a user API. But it is
4849 * defined here for special-purpose apps which want Zephyr running on
4850 * one core and to use others for design-specific processing.
4851 *
4852 * @param cpu_num Integer number of the CPU
4853 * @param stack Stack memory for the CPU
4854 * @param sz Stack buffer size, in bytes
4855 * @param fn Function to begin running on the CPU. First argument is
4856 * an irq_unlock() key.
4857 * @param arg Untyped argument to be passed to "fn"
4858 */
4859extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4860 void (*fn)(int, void *), void *arg);
4861
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004862#ifdef __cplusplus
4863}
4864#endif
4865
Andrew Boiee004dec2016-11-07 09:01:19 -08004866#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4867/*
4868 * Define new and delete operators.
4869 * At this moment, the operators do nothing since objects are supposed
4870 * to be statically allocated.
4871 */
4872inline void operator delete(void *ptr)
4873{
4874 (void)ptr;
4875}
4876
4877inline void operator delete[](void *ptr)
4878{
4879 (void)ptr;
4880}
4881
4882inline void *operator new(size_t size)
4883{
4884 (void)size;
4885 return NULL;
4886}
4887
4888inline void *operator new[](size_t size)
4889{
4890 (void)size;
4891 return NULL;
4892}
4893
4894/* Placement versions of operator new and delete */
4895inline void operator delete(void *ptr1, void *ptr2)
4896{
4897 (void)ptr1;
4898 (void)ptr2;
4899}
4900
4901inline void operator delete[](void *ptr1, void *ptr2)
4902{
4903 (void)ptr1;
4904 (void)ptr2;
4905}
4906
4907inline void *operator new(size_t size, void *ptr)
4908{
4909 (void)size;
4910 return ptr;
4911}
4912
4913inline void *operator new[](size_t size, void *ptr)
4914{
4915 (void)size;
4916 return ptr;
4917}
4918
4919#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4920
Andrew Boiefa94ee72017-09-28 16:54:35 -07004921#include <syscalls/kernel.h>
4922
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004923#endif /* !_ASMLANGUAGE */
4924
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004925#endif /* _kernel__h_ */