blob: ae5c02b550ca21eadd4bec379f12c252e7798580 [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 Ross225c74b2018-06-27 11:20:50 -070076#ifdef CONFIG_WAITQ_SCALABLE
Andy Ross1acd8c22018-05-03 14:51:49 -070077
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
Andy Ross225c74b2018-06-27 11:20:50 -0700409#ifdef CONFIG_WAITQ_SCALABLE
Andy Ross1acd8c22018-05-03 14:51:49 -0700410 /* 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
Andrew Boie7f4d0062018-07-19 11:09:33 -0700541#ifdef CONFIG_USERSPACE
542 /* Set to the lowest area in the thread stack since this needs to
543 * be directly read/writable by user mode. Not ideal, but best we
544 * can do until we have thread-local storage
545 */
546 int *errno_location;
547#else
Anas Nashifce78d162018-05-24 12:43:11 -0500548 /** per-thread errno variable */
Andrew Boie73abd322017-04-04 13:19:13 -0700549 int errno_var;
550#endif
Andrew Boie7f4d0062018-07-19 11:09:33 -0700551#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700552
553#if defined(CONFIG_THREAD_STACK_INFO)
Anas Nashifce78d162018-05-24 12:43:11 -0500554 /** Stack Info */
Andrew Boie73abd322017-04-04 13:19:13 -0700555 struct _thread_stack_info stack_info;
556#endif /* CONFIG_THREAD_STACK_INFO */
557
Chunlin Hane9c97022017-07-07 20:29:30 +0800558#if defined(CONFIG_USERSPACE)
Anas Nashifce78d162018-05-24 12:43:11 -0500559 /** memory domain info of the thread */
Chunlin Hane9c97022017-07-07 20:29:30 +0800560 struct _mem_domain_info mem_domain_info;
Anas Nashifce78d162018-05-24 12:43:11 -0500561 /** Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700562 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800563#endif /* CONFIG_USERSPACE */
564
Andy Ross042d8ec2017-12-09 08:37:20 -0800565#if defined(CONFIG_USE_SWITCH)
566 /* When using __switch() a few previously arch-specific items
567 * become part of the core OS
568 */
569
Anas Nashifce78d162018-05-24 12:43:11 -0500570 /** _Swap() return value */
Andy Ross042d8ec2017-12-09 08:37:20 -0800571 int swap_retval;
572
Anas Nashifce78d162018-05-24 12:43:11 -0500573 /** Context handle returned via _arch_switch() */
Andy Ross042d8ec2017-12-09 08:37:20 -0800574 void *switch_handle;
575#endif
Anas Nashifce78d162018-05-24 12:43:11 -0500576 /** resource pool */
Andrew Boie92e5bd72018-04-12 17:12:15 -0700577 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800578
Anas Nashifce78d162018-05-24 12:43:11 -0500579 /** arch-specifics: must always be at the end */
Andrew Boie73abd322017-04-04 13:19:13 -0700580 struct _thread_arch arch;
581};
582
583typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400584typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700585#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400586
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400587enum execution_context_types {
588 K_ISR = 0,
589 K_COOP_THREAD,
590 K_PREEMPT_THREAD,
591};
592
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400593/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100594 * @defgroup profiling_apis Profiling APIs
595 * @ingroup kernel_apis
596 * @{
597 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530598typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
599 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100600
601/**
602 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
603 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700604 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100605 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
606 *
607 * CONFIG_MAIN_STACK_SIZE
608 * CONFIG_IDLE_STACK_SIZE
609 * CONFIG_ISR_STACK_SIZE
610 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
611 *
612 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
613 * produce output.
614 *
615 * @return N/A
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530616 *
617 * @deprecated This API is deprecated. Use k_thread_foreach().
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100618 */
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530619__deprecated extern void k_call_stacks_analyze(void);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100620
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530621/**
622 * @brief Iterate over all the threads in the system.
623 *
624 * This routine iterates over all the threads in the system and
625 * calls the user_cb function for each thread.
626 *
627 * @param user_cb Pointer to the user callback function.
628 * @param user_data Pointer to user data.
629 *
630 * @note CONFIG_THREAD_MONITOR must be set for this function
631 * to be effective. Also this API uses irq_lock to protect the
632 * _kernel.threads list which means creation of new threads and
633 * terminations of existing threads are blocked until this
634 * API returns.
635 *
636 * @return N/A
637 */
638extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
639
Anas Nashif166f5192018-02-25 08:02:36 -0600640/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100641
642/**
Allan Stephensc98da842016-11-11 15:45:03 -0500643 * @defgroup thread_apis Thread APIs
644 * @ingroup kernel_apis
645 * @{
646 */
647
Benjamin Walshed240f22017-01-22 13:05:08 -0500648#endif /* !_ASMLANGUAGE */
649
650
651/*
652 * Thread user options. May be needed by assembly code. Common part uses low
653 * bits, arch-specific use high bits.
654 */
655
Anas Nashifa541e932018-05-24 11:19:16 -0500656/**
657 * @brief system thread that must not abort
658 * @req K-THREAD-000
659 * */
Benjamin Walshed240f22017-01-22 13:05:08 -0500660#define K_ESSENTIAL (1 << 0)
661
662#if defined(CONFIG_FP_SHARING)
Anas Nashifa541e932018-05-24 11:19:16 -0500663/**
664 * @brief thread uses floating point registers
665 */
Benjamin Walshed240f22017-01-22 13:05:08 -0500666#define K_FP_REGS (1 << 1)
667#endif
668
Anas Nashifa541e932018-05-24 11:19:16 -0500669/**
670 * @brief user mode thread
671 *
672 * This thread has dropped from supervisor mode to user mode and consequently
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700673 * has additional restrictions
674 */
675#define K_USER (1 << 2)
676
Anas Nashifa541e932018-05-24 11:19:16 -0500677/**
678 * @brief Inherit Permissions
679 *
680 * @details
681 * Indicates that the thread being created should inherit all kernel object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700682 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
683 * is not enabled.
684 */
685#define K_INHERIT_PERMS (1 << 3)
686
Benjamin Walshed240f22017-01-22 13:05:08 -0500687#ifdef CONFIG_X86
688/* x86 Bitmask definitions for threads user options */
689
690#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
691/* thread uses SSEx (and also FP) registers */
692#define K_SSE_REGS (1 << 7)
693#endif
694#endif
695
696/* end - thread options */
697
698#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400699/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700700 * @brief Create a thread.
701 *
702 * This routine initializes a thread, then schedules it for execution.
703 *
704 * The new thread may be scheduled for immediate execution or a delayed start.
705 * If the newly spawned thread does not have a delayed start the kernel
706 * scheduler may preempt the current thread to allow the new thread to
707 * execute.
708 *
709 * Thread options are architecture-specific, and can include K_ESSENTIAL,
710 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
711 * them using "|" (the logical OR operator).
712 *
713 * Historically, users often would use the beginning of the stack memory region
714 * to store the struct k_thread data, although corruption will occur if the
715 * stack overflows this region and stack protection features may not detect this
716 * situation.
717 *
718 * @param new_thread Pointer to uninitialized struct k_thread
719 * @param stack Pointer to the stack space.
720 * @param stack_size Stack size in bytes.
721 * @param entry Thread entry function.
722 * @param p1 1st entry point parameter.
723 * @param p2 2nd entry point parameter.
724 * @param p3 3rd entry point parameter.
725 * @param prio Thread priority.
726 * @param options Thread options.
727 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
728 *
729 * @return ID of new thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400730 *
731 * @req K-THREAD-001
Andrew Boied26cf2d2017-03-30 13:07:02 -0700732 */
Andrew Boie662c3452017-10-02 10:51:18 -0700733__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700734 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700735 size_t stack_size,
736 k_thread_entry_t entry,
737 void *p1, void *p2, void *p3,
738 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700739
Andrew Boie3f091b52017-08-30 14:34:14 -0700740/**
741 * @brief Drop a thread's privileges permanently to user mode
742 *
743 * @param entry Function to start executing from
744 * @param p1 1st entry point parameter
745 * @param p2 2nd entry point parameter
746 * @param p3 3rd entry point parameter
Anas Nashif47420d02018-05-24 14:20:56 -0400747 * @req K-THREAD-003
Andrew Boie3f091b52017-08-30 14:34:14 -0700748 */
749extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
750 void *p1, void *p2,
751 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700752
Andrew Boied26cf2d2017-03-30 13:07:02 -0700753/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700754 * @brief Grant a thread access to a NULL-terminated set of kernel objects
755 *
756 * This is a convenience function. For the provided thread, grant access to
757 * the remaining arguments, which must be pointers to kernel objects.
758 * The final argument must be a NULL.
759 *
760 * The thread object must be initialized (i.e. running). The objects don't
761 * need to be.
762 *
763 * @param thread Thread to grant access to objects
764 * @param ... NULL-terminated list of kernel object pointers
Anas Nashif47420d02018-05-24 14:20:56 -0400765 * @req K-THREAD-004
Andrew Boiee12857a2017-10-17 11:38:26 -0700766 */
767extern void __attribute__((sentinel))
768 k_thread_access_grant(struct k_thread *thread, ...);
769
770/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700771 * @brief Assign a resource memory pool to a thread
772 *
773 * By default, threads have no resource pool assigned unless their parent
774 * thread has a resource pool, in which case it is inherited. Multiple
775 * threads may be assigned to the same memory pool.
776 *
777 * Changing a thread's resource pool will not migrate allocations from the
778 * previous pool.
779 *
780 * @param thread Target thread to assign a memory pool for resource requests,
781 * or NULL if the thread should no longer have a memory pool.
782 * @param pool Memory pool to use for resources.
Anas Nashif47420d02018-05-24 14:20:56 -0400783 * @req K-THREAD-005
Andrew Boie92e5bd72018-04-12 17:12:15 -0700784 */
785static inline void k_thread_resource_pool_assign(struct k_thread *thread,
786 struct k_mem_pool *pool)
787{
788 thread->resource_pool = pool;
789}
790
791#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
792/**
793 * @brief Assign the system heap as a thread's resource pool
794 *
795 * Similar to k_thread_resource_pool_assign(), but the thread will use
796 * the kernel heap to draw memory.
797 *
798 * Use with caution, as a malicious thread could perform DoS attacks on the
799 * kernel heap.
800 *
801 * @param thread Target thread to assign the system heap for resource requests
Anas Nashif47420d02018-05-24 14:20:56 -0400802 *
803 * @req K-THREAD-004
Andrew Boie92e5bd72018-04-12 17:12:15 -0700804 */
805void k_thread_system_pool_assign(struct k_thread *thread);
806#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
807
808/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500809 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810 *
Allan Stephensc98da842016-11-11 15:45:03 -0500811 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500812 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400813 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500814 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400815 *
816 * @return N/A
817 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700818__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819
820/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500821 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400822 *
823 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500824 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400825 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400826 * @return N/A
827 */
Kumar Galacc334c72017-04-21 10:55:34 -0500828extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829
830/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500831 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400832 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500833 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400834 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500835 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836 *
837 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400838 * @req K-THREAD-015
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400839 */
Andrew Boie468190a2017-09-29 14:00:48 -0700840__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400841
842/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500843 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500845 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500847 * If @a thread is not currently sleeping, the routine has no effect.
848 *
849 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400850 *
851 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400852 * @req K-THREAD-014
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400853 */
Andrew Boie468190a2017-09-29 14:00:48 -0700854__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400855
856/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500857 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400858 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500859 * @return ID of current thread.
Anas Nashif47420d02018-05-24 14:20:56 -0400860 *
861 * @req K-THREAD-013
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400862 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700863__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400864
865/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500866 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500868 * This routine prevents @a thread from executing if it has not yet started
869 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400870 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500871 * @param thread ID of thread to cancel.
872 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700873 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500874 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700875 *
876 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400877 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700878__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400879
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400880/**
Allan Stephensc98da842016-11-11 15:45:03 -0500881 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500883 * This routine permanently stops execution of @a thread. The thread is taken
884 * off all kernel queues it is part of (i.e. the ready queue, the timeout
885 * queue, or a kernel object wait queue). However, any kernel resources the
886 * thread might currently own (such as mutexes or memory blocks) are not
887 * released. It is the responsibility of the caller of this routine to ensure
888 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400889 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500890 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400891 *
892 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -0400893 * @req K-THREAD-012
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400894 */
Andrew Boie468190a2017-09-29 14:00:48 -0700895__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400896
Andrew Boie7d627c52017-08-30 11:01:56 -0700897
898/**
899 * @brief Start an inactive thread
900 *
901 * If a thread was created with K_FOREVER in the delay parameter, it will
902 * not be added to the scheduling queue until this function is called
903 * on it.
904 *
905 * @param thread thread to start
Anas Nashif47420d02018-05-24 14:20:56 -0400906 * @req K-THREAD-011
Andrew Boie7d627c52017-08-30 11:01:56 -0700907 */
Andrew Boie468190a2017-09-29 14:00:48 -0700908__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700909
Allan Stephensc98da842016-11-11 15:45:03 -0500910/**
911 * @cond INTERNAL_HIDDEN
912 */
913
Benjamin Walshd211a522016-12-06 11:44:01 -0500914/* timeout has timed out and is not on _timeout_q anymore */
915#define _EXPIRED (-2)
916
917/* timeout is not in use */
918#define _INACTIVE (-1)
919
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400920struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700921 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700922 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400923 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700924 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500925 void *init_p1;
926 void *init_p2;
927 void *init_p3;
928 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500929 u32_t init_options;
930 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500931 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400932};
933
Andrew Boied26cf2d2017-03-30 13:07:02 -0700934#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400935 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500936 prio, options, delay, abort, groups) \
937 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700938 .init_thread = (thread), \
939 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500940 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700941 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400942 .init_p1 = (void *)p1, \
943 .init_p2 = (void *)p2, \
944 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500945 .init_prio = (prio), \
946 .init_options = (options), \
947 .init_delay = (delay), \
948 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400949 }
950
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400951/**
Allan Stephensc98da842016-11-11 15:45:03 -0500952 * INTERNAL_HIDDEN @endcond
953 */
954
955/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500956 * @brief Statically define and initialize a thread.
957 *
958 * The thread may be scheduled for immediate execution or a delayed start.
959 *
960 * Thread options are architecture-specific, and can include K_ESSENTIAL,
961 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
962 * them using "|" (the logical OR operator).
963 *
964 * The ID of the thread can be accessed using:
965 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500966 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500967 *
968 * @param name Name of the thread.
969 * @param stack_size Stack size in bytes.
970 * @param entry Thread entry function.
971 * @param p1 1st entry point parameter.
972 * @param p2 2nd entry point parameter.
973 * @param p3 3rd entry point parameter.
974 * @param prio Thread priority.
975 * @param options Thread options.
976 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400977 *
Anas Nashif47420d02018-05-24 14:20:56 -0400978 * @req K-THREAD-010
979 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400980 * @internal It has been observed that the x86 compiler by default aligns
981 * these _static_thread_data structures to 32-byte boundaries, thereby
982 * wasting space. To work around this, force a 4-byte alignment.
Anas Nashif47420d02018-05-24 14:20:56 -0400983 *
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400984 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500985#define K_THREAD_DEFINE(name, stack_size, \
986 entry, p1, p2, p3, \
987 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700988 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700989 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500990 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500991 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700992 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
993 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500994 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700995 NULL, 0); \
996 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400997
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400998/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500999 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001000 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001001 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001002 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001003 * @param thread ID of thread whose priority is needed.
1004 *
1005 * @return Priority of @a thread.
Anas Nashif47420d02018-05-24 14:20:56 -04001006 * @req K-THREAD-009
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001007 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001008__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001009
1010/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001011 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001013 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001014 *
1015 * Rescheduling can occur immediately depending on the priority @a thread is
1016 * set to:
1017 *
1018 * - If its priority is raised above the priority of the caller of this
1019 * function, and the caller is preemptible, @a thread will be scheduled in.
1020 *
1021 * - If the caller operates on itself, it lowers its priority below that of
1022 * other threads in the system, and the caller is preemptible, the thread of
1023 * highest priority will be scheduled in.
1024 *
1025 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
1026 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
1027 * highest priority.
1028 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001029 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001030 * @param prio New priority.
1031 *
1032 * @warning Changing the priority of a thread currently involved in mutex
1033 * priority inheritance may result in undefined behavior.
1034 *
1035 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001036 * @req K-THREAD-008
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001037 */
Andrew Boie468190a2017-09-29 14:00:48 -07001038__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001039
Andy Ross4a2e50f2018-05-15 11:06:25 -07001040
1041#ifdef CONFIG_SCHED_DEADLINE
1042/**
1043 * @brief Set deadline expiration time for scheduler
1044 *
1045 * This sets the "deadline" expiration as a time delta from the
1046 * current time, in the same units used by k_cycle_get_32(). The
1047 * scheduler (when deadline scheduling is enabled) will choose the
1048 * next expiring thread when selecting between threads at the same
1049 * static priority. Threads at different priorities will be scheduled
1050 * according to their static priority.
1051 *
1052 * @note Deadlines that are negative (i.e. in the past) are still seen
1053 * as higher priority than others, even if the thread has "finished"
1054 * its work. If you don't want it scheduled anymore, you have to
1055 * reset the deadline into the future, block/pend the thread, or
1056 * modify its priority with k_thread_priority_set().
1057 *
1058 * @note Despite the API naming, the scheduler makes no guarantees the
1059 * the thread WILL be scheduled within that deadline, nor does it take
1060 * extra metadata (like e.g. the "runtime" and "period" parameters in
1061 * Linux sched_setattr()) that allows the kernel to validate the
1062 * scheduling for achievability. Such features could be implemented
1063 * above this call, which is simply input to the priority selection
1064 * logic.
1065 *
1066 * @param thread A thread on which to set the deadline
1067 * @param deadline A time delta, in cycle units
Anas Nashif47420d02018-05-24 14:20:56 -04001068 *
1069 * @req K-THREAD-007
Andy Ross4a2e50f2018-05-15 11:06:25 -07001070 */
1071__syscall void k_thread_deadline_set(k_tid_t thread, int deadline);
1072#endif
1073
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001074/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001075 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001076 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001077 * This routine prevents the kernel scheduler from making @a thread the
1078 * current thread. All other internal operations on @a thread are still
1079 * performed; for example, any timeout it is waiting on keeps ticking,
1080 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001081 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001082 * If @a thread is already suspended, the routine has no effect.
1083 *
1084 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001085 *
1086 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001087 * @req K-THREAD-005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001088 */
Andrew Boie468190a2017-09-29 14:00:48 -07001089__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001090
1091/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001092 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001094 * This routine allows the kernel scheduler to make @a thread the current
1095 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001096 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001097 * If @a thread is not currently suspended, the routine has no effect.
1098 *
1099 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001100 *
1101 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001102 * @req K-THREAD-006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001103 */
Andrew Boie468190a2017-09-29 14:00:48 -07001104__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001105
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001106/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001107 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001108 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001109 * This routine specifies how the scheduler will perform time slicing of
1110 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001112 * To enable time slicing, @a slice must be non-zero. The scheduler
1113 * ensures that no thread runs for more than the specified time limit
1114 * before other threads of that priority are given a chance to execute.
1115 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001116 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001117 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001118 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001119 * execute. Once the scheduler selects a thread for execution, there is no
1120 * minimum guaranteed time the thread will execute before threads of greater or
1121 * equal priority are scheduled.
1122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001123 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001124 * for execution, this routine has no effect; the thread is immediately
1125 * rescheduled after the slice period expires.
1126 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001127 * To disable timeslicing, set both @a slice and @a prio to zero.
1128 *
1129 * @param slice Maximum time slice length (in milliseconds).
1130 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001131 *
1132 * @return N/A
1133 */
Kumar Galacc334c72017-04-21 10:55:34 -05001134extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001135
Anas Nashif166f5192018-02-25 08:02:36 -06001136/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001137
1138/**
1139 * @addtogroup isr_apis
1140 * @{
1141 */
1142
1143/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001144 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001145 *
Allan Stephensc98da842016-11-11 15:45:03 -05001146 * This routine allows the caller to customize its actions, depending on
1147 * whether it is a thread or an ISR.
1148 *
1149 * @note Can be called by ISRs.
1150 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001151 * @return 0 if invoked by a thread.
1152 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001153 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001154extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001155
Benjamin Walsh445830d2016-11-10 15:54:27 -05001156/**
1157 * @brief Determine if code is running in a preemptible thread.
1158 *
Allan Stephensc98da842016-11-11 15:45:03 -05001159 * This routine allows the caller to customize its actions, depending on
1160 * whether it can be preempted by another thread. The routine returns a 'true'
1161 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001162 *
Allan Stephensc98da842016-11-11 15:45:03 -05001163 * - The code is running in a thread, not at ISR.
1164 * - The thread's priority is in the preemptible range.
1165 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001166 *
Allan Stephensc98da842016-11-11 15:45:03 -05001167 * @note Can be called by ISRs.
1168 *
1169 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001170 * @return Non-zero if invoked by a preemptible thread.
1171 */
Andrew Boie468190a2017-09-29 14:00:48 -07001172__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001173
Allan Stephensc98da842016-11-11 15:45:03 -05001174/**
Anas Nashif166f5192018-02-25 08:02:36 -06001175 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001176 */
1177
1178/**
1179 * @addtogroup thread_apis
1180 * @{
1181 */
1182
1183/**
1184 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001185 *
Allan Stephensc98da842016-11-11 15:45:03 -05001186 * This routine prevents the current thread from being preempted by another
1187 * thread by instructing the scheduler to treat it as a cooperative thread.
1188 * If the thread subsequently performs an operation that makes it unready,
1189 * it will be context switched out in the normal manner. When the thread
1190 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001191 *
Allan Stephensc98da842016-11-11 15:45:03 -05001192 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001193 *
Allan Stephensc98da842016-11-11 15:45:03 -05001194 * @note k_sched_lock() and k_sched_unlock() should normally be used
1195 * when the operation being performed can be safely interrupted by ISRs.
1196 * However, if the amount of processing involved is very small, better
1197 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001198 *
1199 * @return N/A
1200 */
1201extern void k_sched_lock(void);
1202
Allan Stephensc98da842016-11-11 15:45:03 -05001203/**
1204 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001205 *
Allan Stephensc98da842016-11-11 15:45:03 -05001206 * This routine reverses the effect of a previous call to k_sched_lock().
1207 * A thread must call the routine once for each time it called k_sched_lock()
1208 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001209 *
1210 * @return N/A
1211 */
1212extern void k_sched_unlock(void);
1213
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001214/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001215 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001216 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001217 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001218 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001219 * Custom data is not used by the kernel itself, and is freely available
1220 * for a thread to use as it sees fit. It can be used as a framework
1221 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001222 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001223 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001224 *
1225 * @return N/A
Anas Nashif47420d02018-05-24 14:20:56 -04001226 *
1227 * @req K-THREAD-016
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001228 */
Andrew Boie468190a2017-09-29 14:00:48 -07001229__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001230
1231/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001232 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001233 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001234 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001235 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001236 * @return Current custom data value.
Anas Nashif47420d02018-05-24 14:20:56 -04001237 * @req K-THREAD-007
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001238 */
Andrew Boie468190a2017-09-29 14:00:48 -07001239__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001240
1241/**
Anas Nashif166f5192018-02-25 08:02:36 -06001242 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001243 */
1244
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001245#include <sys_clock.h>
1246
Allan Stephensc2f15a42016-11-17 12:24:22 -05001247/**
1248 * @addtogroup clock_apis
1249 * @{
1250 */
1251
1252/**
1253 * @brief Generate null timeout delay.
1254 *
1255 * This macro generates a timeout delay that that instructs a kernel API
1256 * not to wait if the requested operation cannot be performed immediately.
1257 *
1258 * @return Timeout delay value.
1259 */
1260#define K_NO_WAIT 0
1261
1262/**
1263 * @brief Generate timeout delay from milliseconds.
1264 *
1265 * This macro generates a timeout delay that that instructs a kernel API
1266 * to wait up to @a ms milliseconds to perform the requested operation.
1267 *
1268 * @param ms Duration in milliseconds.
1269 *
1270 * @return Timeout delay value.
1271 */
Johan Hedberg14471692016-11-13 10:52:15 +02001272#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001273
1274/**
1275 * @brief Generate timeout delay from seconds.
1276 *
1277 * This macro generates a timeout delay that that instructs a kernel API
1278 * to wait up to @a s seconds to perform the requested operation.
1279 *
1280 * @param s Duration in seconds.
1281 *
1282 * @return Timeout delay value.
1283 */
Johan Hedberg14471692016-11-13 10:52:15 +02001284#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001285
1286/**
1287 * @brief Generate timeout delay from minutes.
1288 *
1289 * This macro generates a timeout delay that that instructs a kernel API
1290 * to wait up to @a m minutes to perform the requested operation.
1291 *
1292 * @param m Duration in minutes.
1293 *
1294 * @return Timeout delay value.
1295 */
Johan Hedberg14471692016-11-13 10:52:15 +02001296#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001297
1298/**
1299 * @brief Generate timeout delay from hours.
1300 *
1301 * This macro generates a timeout delay that that instructs a kernel API
1302 * to wait up to @a h hours to perform the requested operation.
1303 *
1304 * @param h Duration in hours.
1305 *
1306 * @return Timeout delay value.
1307 */
Johan Hedberg14471692016-11-13 10:52:15 +02001308#define K_HOURS(h) K_MINUTES((h) * 60)
1309
Allan Stephensc98da842016-11-11 15:45:03 -05001310/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001311 * @brief Generate infinite timeout delay.
1312 *
1313 * This macro generates a timeout delay that that instructs a kernel API
1314 * to wait as long as necessary to perform the requested operation.
1315 *
1316 * @return Timeout delay value.
1317 */
1318#define K_FOREVER (-1)
1319
1320/**
Anas Nashif166f5192018-02-25 08:02:36 -06001321 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001322 */
1323
1324/**
Allan Stephensc98da842016-11-11 15:45:03 -05001325 * @cond INTERNAL_HIDDEN
1326 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001327
Benjamin Walsh62092182016-12-20 14:39:08 -05001328/* kernel clocks */
1329
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001330#ifdef CONFIG_SYS_CLOCK_EXISTS
1331#if (CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC % sys_clock_ticks_per_sec) != 0
1332 #define _NEED_PRECISE_TICK_MS_CONVERSION
1333#elif (MSEC_PER_SEC % sys_clock_ticks_per_sec) != 0
Benjamin Walsh62092182016-12-20 14:39:08 -05001334 #define _NON_OPTIMIZED_TICKS_PER_SEC
1335#endif
1336
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001337#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
1338 #define _NEED_PRECISE_TICK_MS_CONVERSION
1339#endif
1340#endif
1341
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{
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001344#ifdef CONFIG_SYS_CLOCK_EXISTS
1345
1346#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
1347 /* use 64-bit math to keep precision */
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001348 s64_t ms_ticks_per_sec = (s64_t)ms * sys_clock_ticks_per_sec;
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001349
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001350 return (s32_t)ceiling_fraction(ms_ticks_per_sec, MSEC_PER_SEC);
1351#else
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001352 /* simple division keeps precision */
1353 s32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
1354
1355 return (s32_t)ceiling_fraction(ms, ms_per_tick);
1356#endif
1357
1358#else
1359 __ASSERT(ms == 0, "ms not zero");
1360 return 0;
Benjamin Walsh62092182016-12-20 14:39:08 -05001361#endif
Piotr Zięcikfe2ac392018-06-11 13:47:39 +02001362}
Benjamin Walsh62092182016-12-20 14:39:08 -05001363
Kumar Galacc334c72017-04-21 10:55:34 -05001364static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001365{
Benjamin Walsh62092182016-12-20 14:39:08 -05001366#ifdef CONFIG_SYS_CLOCK_EXISTS
1367
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001368#ifdef _NEED_PRECISE_TICK_MS_CONVERSION
1369 /* use 64-bit math to keep precision */
1370 return (u64_t)ticks * sys_clock_hw_cycles_per_tick * MSEC_PER_SEC /
1371 CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001372#else
Piotr Zięcik91fe22e2018-06-11 14:24:41 +02001373 /* simple multiplication keeps precision */
1374 u32_t ms_per_tick = MSEC_PER_SEC / sys_clock_ticks_per_sec;
1375
1376 return (u64_t)ticks * ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001377#endif
1378
1379#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001380 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001381 return 0;
1382#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001383}
1384
Piotr Zięcik77f42f82018-06-11 14:26:29 +02001385/* added tick needed to account for tick in progress */
1386#ifdef CONFIG_TICKLESS_KERNEL
1387#define _TICK_ALIGN 0
1388#else
1389#define _TICK_ALIGN 1
1390#endif
1391
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001392struct k_timer {
1393 /*
1394 * _timeout structure must be first here if we want to use
1395 * dynamic timer allocation. timeout.node is used in the double-linked
1396 * list of free timers
1397 */
1398 struct _timeout timeout;
1399
Allan Stephens45bfa372016-10-12 12:39:42 -05001400 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001401 _wait_q_t wait_q;
1402
1403 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001404 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001405
1406 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001407 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001408
1409 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001410 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001411
Allan Stephens45bfa372016-10-12 12:39:42 -05001412 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001413 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001414
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001415 /* user-specific data, also used to support legacy features */
1416 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001417
Anas Nashif2f203c22016-12-18 06:57:45 -05001418 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001419};
1420
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001421#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001422 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001423 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001424 .timeout.wait_q = NULL, \
1425 .timeout.thread = NULL, \
1426 .timeout.func = _timer_expiration_handler, \
Andy Rossccf3bf72018-05-10 11:10:34 -07001427 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001428 .expiry_fn = expiry, \
1429 .stop_fn = stop, \
1430 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001431 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001432 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001433 }
1434
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001435#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1436
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001437/**
Allan Stephensc98da842016-11-11 15:45:03 -05001438 * INTERNAL_HIDDEN @endcond
1439 */
1440
1441/**
1442 * @defgroup timer_apis Timer APIs
1443 * @ingroup kernel_apis
1444 * @{
1445 */
1446
1447/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001448 * @typedef k_timer_expiry_t
1449 * @brief Timer expiry function type.
1450 *
1451 * A timer's expiry function is executed by the system clock interrupt handler
1452 * each time the timer expires. The expiry function is optional, and is only
1453 * invoked if the timer has been initialized with one.
1454 *
1455 * @param timer Address of timer.
1456 *
1457 * @return N/A
1458 */
1459typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1460
1461/**
1462 * @typedef k_timer_stop_t
1463 * @brief Timer stop function type.
1464 *
1465 * A timer's stop function is executed if the timer is stopped prematurely.
1466 * The function runs in the context of the thread that stops the timer.
1467 * The stop function is optional, and is only invoked if the timer has been
1468 * initialized with one.
1469 *
1470 * @param timer Address of timer.
1471 *
1472 * @return N/A
1473 */
1474typedef void (*k_timer_stop_t)(struct k_timer *timer);
1475
1476/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001477 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001479 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001480 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001481 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001482 *
1483 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001484 * @param expiry_fn Function to invoke each time the timer expires.
1485 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001486 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001487#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001488 struct k_timer name \
1489 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001490 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001491
Allan Stephens45bfa372016-10-12 12:39:42 -05001492/**
1493 * @brief Initialize a timer.
1494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001495 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001496 *
1497 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001498 * @param expiry_fn Function to invoke each time the timer expires.
1499 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001500 *
1501 * @return N/A
1502 */
1503extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001504 k_timer_expiry_t expiry_fn,
1505 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001506
Allan Stephens45bfa372016-10-12 12:39:42 -05001507/**
1508 * @brief Start a timer.
1509 *
1510 * This routine starts a timer, and resets its status to zero. The timer
1511 * begins counting down using the specified duration and period values.
1512 *
1513 * Attempting to start a timer that is already running is permitted.
1514 * The timer's status is reset to zero and the timer begins counting down
1515 * using the new duration and period values.
1516 *
1517 * @param timer Address of timer.
1518 * @param duration Initial timer duration (in milliseconds).
1519 * @param period Timer period (in milliseconds).
1520 *
1521 * @return N/A
1522 */
Andrew Boiea354d492017-09-29 16:22:28 -07001523__syscall void k_timer_start(struct k_timer *timer,
1524 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001525
1526/**
1527 * @brief Stop a timer.
1528 *
1529 * This routine stops a running timer prematurely. The timer's stop function,
1530 * if one exists, is invoked by the caller.
1531 *
1532 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001533 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001534 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001535 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1536 * if @a k_timer_stop is to be called from ISRs.
1537 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001538 * @param timer Address of timer.
1539 *
1540 * @return N/A
1541 */
Andrew Boiea354d492017-09-29 16:22:28 -07001542__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001543
1544/**
1545 * @brief Read timer status.
1546 *
1547 * This routine reads the timer's status, which indicates the number of times
1548 * it has expired since its status was last read.
1549 *
1550 * Calling this routine resets the timer's status to zero.
1551 *
1552 * @param timer Address of timer.
1553 *
1554 * @return Timer status.
1555 */
Andrew Boiea354d492017-09-29 16:22:28 -07001556__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001557
1558/**
1559 * @brief Synchronize thread to timer expiration.
1560 *
1561 * This routine blocks the calling thread until the timer's status is non-zero
1562 * (indicating that it has expired at least once since it was last examined)
1563 * or the timer is stopped. If the timer status is already non-zero,
1564 * or the timer is already stopped, the caller continues without waiting.
1565 *
1566 * Calling this routine resets the timer's status to zero.
1567 *
1568 * This routine must not be used by interrupt handlers, since they are not
1569 * allowed to block.
1570 *
1571 * @param timer Address of timer.
1572 *
1573 * @return Timer status.
1574 */
Andrew Boiea354d492017-09-29 16:22:28 -07001575__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001576
1577/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001578 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001579 *
1580 * This routine computes the (approximate) time remaining before a running
1581 * timer next expires. If the timer is not running, it returns zero.
1582 *
1583 * @param timer Address of timer.
1584 *
1585 * @return Remaining time (in milliseconds).
1586 */
Andrew Boiea354d492017-09-29 16:22:28 -07001587__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1588
1589static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001590{
1591 return _timeout_remaining_get(&timer->timeout);
1592}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001593
Allan Stephensc98da842016-11-11 15:45:03 -05001594/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001595 * @brief Associate user-specific data with a timer.
1596 *
1597 * This routine records the @a user_data with the @a timer, to be retrieved
1598 * later.
1599 *
1600 * It can be used e.g. in a timer handler shared across multiple subsystems to
1601 * retrieve data specific to the subsystem this timer is associated with.
1602 *
1603 * @param timer Address of timer.
1604 * @param user_data User data to associate with the timer.
1605 *
1606 * @return N/A
1607 */
Andrew Boiea354d492017-09-29 16:22:28 -07001608__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1609
Anas Nashif954d5502018-02-25 08:37:28 -06001610/**
1611 * @internal
1612 */
Andrew Boiea354d492017-09-29 16:22:28 -07001613static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1614 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001615{
1616 timer->user_data = user_data;
1617}
1618
1619/**
1620 * @brief Retrieve the user-specific data from a timer.
1621 *
1622 * @param timer Address of timer.
1623 *
1624 * @return The user data.
1625 */
Andrew Boiea354d492017-09-29 16:22:28 -07001626__syscall void *k_timer_user_data_get(struct k_timer *timer);
1627
1628static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001629{
1630 return timer->user_data;
1631}
1632
Anas Nashif166f5192018-02-25 08:02:36 -06001633/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001634
Allan Stephensc98da842016-11-11 15:45:03 -05001635/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001636 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001637 * @{
1638 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001639
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001640/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001641 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001642 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001643 * This routine returns the elapsed time since the system booted,
1644 * in milliseconds.
1645 *
1646 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001647 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001648__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001649
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001650/**
1651 * @brief Enable clock always on in tickless kernel
1652 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001653 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001654 * there are no timer events programmed in tickless kernel
1655 * scheduling. This is necessary if the clock is used to track
1656 * passage of time.
1657 *
1658 * @retval prev_status Previous status of always on flag
1659 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301660#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001661static inline int k_enable_sys_clock_always_on(void)
1662{
1663 int prev_status = _sys_clock_always_on;
1664
1665 _sys_clock_always_on = 1;
1666 _enable_sys_clock();
1667
1668 return prev_status;
1669}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301670#else
1671#define k_enable_sys_clock_always_on() do { } while ((0))
1672#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001673
1674/**
1675 * @brief Disable clock always on in tickless kernel
1676 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001677 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001678 * there are no timer events programmed in tickless kernel
1679 * scheduling. To save power, this routine should be called
1680 * immediately when clock is not used to track time.
1681 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301682#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001683static inline void k_disable_sys_clock_always_on(void)
1684{
1685 _sys_clock_always_on = 0;
1686}
1687#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001688#define k_disable_sys_clock_always_on() do { } while ((0))
1689#endif
1690
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001691/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001692 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001694 * This routine returns the lower 32-bits of the elapsed time since the system
1695 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001696 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001697 * This routine can be more efficient than k_uptime_get(), as it reduces the
1698 * need for interrupt locking and 64-bit math. However, the 32-bit result
1699 * cannot hold a system uptime time larger than approximately 50 days, so the
1700 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001702 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001703 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001704__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001705
1706/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001707 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001708 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001709 * This routine computes the elapsed time between the current system uptime
1710 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001711 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001712 * @param reftime Pointer to a reference time, which is updated to the current
1713 * uptime upon return.
1714 *
1715 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001716 */
Kumar Galacc334c72017-04-21 10:55:34 -05001717extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001718
1719/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001720 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001721 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001722 * This routine computes the elapsed time between the current system uptime
1723 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001724 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001725 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1726 * need for interrupt locking and 64-bit math. However, the 32-bit result
1727 * cannot hold an elapsed time larger than approximately 50 days, so the
1728 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001729 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001730 * @param reftime Pointer to a reference time, which is updated to the current
1731 * uptime upon return.
1732 *
1733 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001734 */
Kumar Galacc334c72017-04-21 10:55:34 -05001735extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001736
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001737/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001738 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001739 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001740 * This routine returns the current time, as measured by the system's hardware
1741 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001742 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001743 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001744 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001745#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001746
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001747/**
Anas Nashif166f5192018-02-25 08:02:36 -06001748 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001749 */
1750
Allan Stephensc98da842016-11-11 15:45:03 -05001751/**
1752 * @cond INTERNAL_HIDDEN
1753 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001754
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001755struct k_queue {
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001756 sys_sflist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001757 union {
1758 _wait_q_t wait_q;
1759
1760 _POLL_EVENT;
1761 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001762
1763 _OBJECT_TRACING_NEXT_PTR(k_queue);
1764};
1765
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001766#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001767 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001768 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Andy Rossccf3bf72018-05-10 11:10:34 -07001769 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001770 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001771 _OBJECT_TRACING_INIT \
1772 }
1773
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001774#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1775
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001776extern void *z_queue_node_peek(sys_sfnode_t *node, bool needs_free);
1777
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001778/**
1779 * INTERNAL_HIDDEN @endcond
1780 */
1781
1782/**
1783 * @defgroup queue_apis Queue APIs
1784 * @ingroup kernel_apis
1785 * @{
1786 */
1787
1788/**
1789 * @brief Initialize a queue.
1790 *
1791 * This routine initializes a queue object, prior to its first use.
1792 *
1793 * @param queue Address of the queue.
1794 *
1795 * @return N/A
1796 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001797__syscall void k_queue_init(struct k_queue *queue);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001798
1799/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001800 * @brief Cancel waiting on a queue.
1801 *
1802 * This routine causes first thread pending on @a queue, if any, to
1803 * return from k_queue_get() call with NULL value (as if timeout expired).
1804 *
1805 * @note Can be called by ISRs.
1806 *
1807 * @param queue Address of the queue.
1808 *
1809 * @return N/A
1810 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001811__syscall void k_queue_cancel_wait(struct k_queue *queue);
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001812
1813/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001814 * @brief Append an element to the end of a queue.
1815 *
1816 * This routine appends a data item to @a queue. A queue data item must be
1817 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1818 * reserved for the kernel's use.
1819 *
1820 * @note Can be called by ISRs.
1821 *
1822 * @param queue Address of the queue.
1823 * @param data Address of the data item.
1824 *
1825 * @return N/A
1826 */
1827extern void k_queue_append(struct k_queue *queue, void *data);
1828
1829/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001830 * @brief Append an element to a queue.
1831 *
1832 * This routine appends a data item to @a queue. There is an implicit
1833 * memory allocation from the calling thread's resource pool, which is
1834 * automatically freed when the item is removed from the queue.
1835 *
1836 * @note Can be called by ISRs.
1837 *
1838 * @param queue Address of the queue.
1839 * @param data Address of the data item.
1840 *
1841 * @retval 0 on success
1842 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1843 */
1844__syscall int k_queue_alloc_append(struct k_queue *queue, void *data);
1845
1846/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001847 * @brief Prepend an element to a queue.
1848 *
1849 * This routine prepends a data item to @a queue. A queue data item must be
1850 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1851 * reserved for the kernel's use.
1852 *
1853 * @note Can be called by ISRs.
1854 *
1855 * @param queue Address of the queue.
1856 * @param data Address of the data item.
1857 *
1858 * @return N/A
1859 */
1860extern void k_queue_prepend(struct k_queue *queue, void *data);
1861
1862/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001863 * @brief Prepend an element to a queue.
1864 *
1865 * This routine prepends a data item to @a queue. There is an implicit
1866 * memory allocation from the calling thread's resource pool, which is
1867 * automatically freed when the item is removed from the queue.
1868 *
1869 * @note Can be called by ISRs.
1870 *
1871 * @param queue Address of the queue.
1872 * @param data Address of the data item.
1873 *
1874 * @retval 0 on success
1875 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
1876 */
1877__syscall int k_queue_alloc_prepend(struct k_queue *queue, void *data);
1878
1879/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001880 * @brief Inserts an element to a queue.
1881 *
1882 * This routine inserts a data item to @a queue after previous item. A queue
1883 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1884 * item are reserved for the kernel's use.
1885 *
1886 * @note Can be called by ISRs.
1887 *
1888 * @param queue Address of the queue.
1889 * @param prev Address of the previous data item.
1890 * @param data Address of the data item.
1891 *
1892 * @return N/A
1893 */
1894extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1895
1896/**
1897 * @brief Atomically append a list of elements to a queue.
1898 *
1899 * This routine adds a list of data items to @a queue in one operation.
1900 * The data items must be in a singly-linked list, with the first 32 bits
1901 * in each data item pointing to the next data item; the list must be
1902 * NULL-terminated.
1903 *
1904 * @note Can be called by ISRs.
1905 *
1906 * @param queue Address of the queue.
1907 * @param head Pointer to first node in singly-linked list.
1908 * @param tail Pointer to last node in singly-linked list.
1909 *
1910 * @return N/A
1911 */
1912extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1913
1914/**
1915 * @brief Atomically add a list of elements to a queue.
1916 *
1917 * This routine adds a list of data items to @a queue in one operation.
1918 * The data items must be in a singly-linked list implemented using a
1919 * sys_slist_t object. Upon completion, the original list is empty.
1920 *
1921 * @note Can be called by ISRs.
1922 *
1923 * @param queue Address of the queue.
1924 * @param list Pointer to sys_slist_t object.
1925 *
1926 * @return N/A
1927 */
1928extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1929
1930/**
1931 * @brief Get an element from a queue.
1932 *
1933 * This routine removes first data item from @a queue. The first 32 bits of the
1934 * data item are reserved for the kernel's use.
1935 *
1936 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1937 *
1938 * @param queue Address of the queue.
1939 * @param timeout Waiting period to obtain a data item (in milliseconds),
1940 * or one of the special values K_NO_WAIT and K_FOREVER.
1941 *
1942 * @return Address of the data item if successful; NULL if returned
1943 * without waiting, or waiting period timed out.
1944 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001945__syscall void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001946
1947/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001948 * @brief Remove an element from a queue.
1949 *
1950 * This routine removes data item from @a queue. The first 32 bits of the
1951 * data item are reserved for the kernel's use. Removing elements from k_queue
1952 * rely on sys_slist_find_and_remove which is not a constant time operation.
1953 *
1954 * @note Can be called by ISRs
1955 *
1956 * @param queue Address of the queue.
1957 * @param data Address of the data item.
1958 *
1959 * @return true if data item was removed
1960 */
1961static inline bool k_queue_remove(struct k_queue *queue, void *data)
1962{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001963 return sys_sflist_find_and_remove(&queue->data_q, (sys_sfnode_t *)data);
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001964}
1965
1966/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001967 * @brief Query a queue to see if it has data available.
1968 *
1969 * Note that the data might be already gone by the time this function returns
1970 * if other threads are also trying to read from the queue.
1971 *
1972 * @note Can be called by ISRs.
1973 *
1974 * @param queue Address of the queue.
1975 *
1976 * @return Non-zero if the queue is empty.
1977 * @return 0 if data is available.
1978 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001979__syscall int k_queue_is_empty(struct k_queue *queue);
1980
1981static inline int _impl_k_queue_is_empty(struct k_queue *queue)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001982{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001983 return (int)sys_sflist_is_empty(&queue->data_q);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001984}
1985
1986/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001987 * @brief Peek element at the head of queue.
1988 *
1989 * Return element from the head of queue without removing it.
1990 *
1991 * @param queue Address of the queue.
1992 *
1993 * @return Head element, or NULL if queue is empty.
1994 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001995__syscall void *k_queue_peek_head(struct k_queue *queue);
1996
1997static inline void *_impl_k_queue_peek_head(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001998{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07001999 return z_queue_node_peek(sys_sflist_peek_head(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002000}
2001
2002/**
2003 * @brief Peek element at the tail of queue.
2004 *
2005 * Return element from the tail of queue without removing it.
2006 *
2007 * @param queue Address of the queue.
2008 *
2009 * @return Tail element, or NULL if queue is empty.
2010 */
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002011__syscall void *k_queue_peek_tail(struct k_queue *queue);
2012
2013static inline void *_impl_k_queue_peek_tail(struct k_queue *queue)
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002014{
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002015 return z_queue_node_peek(sys_sflist_peek_tail(&queue->data_q), false);
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002016}
2017
2018/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002019 * @brief Statically define and initialize a queue.
2020 *
2021 * The queue can be accessed outside the module where it is defined using:
2022 *
2023 * @code extern struct k_queue <name>; @endcode
2024 *
2025 * @param name Name of the queue.
2026 */
2027#define K_QUEUE_DEFINE(name) \
2028 struct k_queue name \
2029 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002030 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002031
Anas Nashif166f5192018-02-25 08:02:36 -06002032/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02002033
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002034struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002035 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002036};
2037
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002038/**
2039 * @cond INTERNAL_HIDDEN
2040 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002041#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002042 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002043 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002044 }
2045
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002046#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
2047
Allan Stephensc98da842016-11-11 15:45:03 -05002048/**
2049 * INTERNAL_HIDDEN @endcond
2050 */
2051
2052/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002053 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002054 * @ingroup kernel_apis
2055 * @{
2056 */
2057
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002058/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002059 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002060 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002061 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002062 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002063 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002064 *
2065 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002066 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002067 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002068#define k_fifo_init(fifo) \
2069 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002070
2071/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002072 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002073 *
2074 * This routine causes first thread pending on @a fifo, if any, to
2075 * return from k_fifo_get() call with NULL value (as if timeout
2076 * expired).
2077 *
2078 * @note Can be called by ISRs.
2079 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002080 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002081 *
2082 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002083 * @req K-FIFO-001
Paul Sokolovsky3f507072017-04-25 17:54:31 +03002084 */
2085#define k_fifo_cancel_wait(fifo) \
2086 k_queue_cancel_wait((struct k_queue *) fifo)
2087
2088/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002089 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002090 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002091 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002092 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2093 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002095 * @note Can be called by ISRs.
2096 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002097 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002098 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002099 *
2100 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002101 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002102 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002103#define k_fifo_put(fifo, data) \
2104 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002105
2106/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002107 * @brief Add an element to a FIFO queue.
2108 *
2109 * This routine adds a data item to @a fifo. There is an implicit
2110 * memory allocation from the calling thread's resource pool, which is
2111 * automatically freed when the item is removed.
2112 *
2113 * @note Can be called by ISRs.
2114 *
2115 * @param fifo Address of the FIFO.
2116 * @param data Address of the data item.
2117 *
2118 * @retval 0 on success
2119 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002120 * @req K-FIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002121 */
2122#define k_fifo_alloc_put(fifo, data) \
2123 k_queue_alloc_append((struct k_queue *) fifo, data)
2124
2125/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002126 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002127 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002128 * This routine adds a list of data items to @a fifo in one operation.
2129 * The data items must be in a singly-linked list, with the first 32 bits
2130 * each data item pointing to the next data item; the list must be
2131 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002133 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002134 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002135 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002136 * @param head Pointer to first node in singly-linked list.
2137 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002138 *
2139 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002140 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002141 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002142#define k_fifo_put_list(fifo, head, tail) \
2143 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002144
2145/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002146 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002147 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002148 * This routine adds a list of data items to @a fifo in one operation.
2149 * The data items must be in a singly-linked list implemented using a
2150 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002151 * and must be re-initialized via sys_slist_init().
2152 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002153 * @note Can be called by ISRs.
2154 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002155 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002156 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002157 *
2158 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002159 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002160 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002161#define k_fifo_put_slist(fifo, list) \
2162 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002163
2164/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002165 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002166 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002167 * This routine removes a data item from @a fifo in a "first in, first out"
2168 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002169 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002170 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2171 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002172 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002173 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002174 * or one of the special values K_NO_WAIT and K_FOREVER.
2175 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002176 * @return Address of the data item if successful; NULL if returned
2177 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002178 * @req K-FIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002179 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002180#define k_fifo_get(fifo, timeout) \
2181 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002182
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002183/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002184 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002185 *
2186 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002187 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002188 *
2189 * @note Can be called by ISRs.
2190 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002191 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002192 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002193 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002194 * @return 0 if data is available.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002195 * @req K-FIFO-001
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002196 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002197#define k_fifo_is_empty(fifo) \
2198 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002199
2200/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002201 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002202 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002203 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302204 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002205 * on each iteration of processing, a head container will be peeked,
2206 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002207 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002208 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002209 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002210 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002211 * @return Head element, or NULL if the FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002212 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002213 */
2214#define k_fifo_peek_head(fifo) \
2215 k_queue_peek_head((struct k_queue *) fifo)
2216
2217/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002218 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002219 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002220 * Return element from the tail of FIFO queue (without removing it). A usecase
2221 * for this is if elements of the FIFO queue are themselves containers. Then
2222 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002223 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002224 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002225 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002226 * @return Tail element, or NULL if a FIFO queue is empty.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002227 * @req K-FIFO-001
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002228 */
2229#define k_fifo_peek_tail(fifo) \
2230 k_queue_peek_tail((struct k_queue *) fifo)
2231
2232/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002233 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002234 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002235 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002236 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002237 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002238 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002239 * @param name Name of the FIFO queue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002240 * @req K-FIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002241 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002242#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002243 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002244 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002245 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002246
Anas Nashif166f5192018-02-25 08:02:36 -06002247/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002248
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002249struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002250 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002251};
2252
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002253/**
2254 * @cond INTERNAL_HIDDEN
2255 */
2256
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002257#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002258 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002259 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002260 }
2261
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002262#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2263
Allan Stephensc98da842016-11-11 15:45:03 -05002264/**
2265 * INTERNAL_HIDDEN @endcond
2266 */
2267
2268/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002269 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002270 * @ingroup kernel_apis
2271 * @{
2272 */
2273
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002274/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002275 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002276 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002277 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002278 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002279 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002280 *
2281 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002282 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002283 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002284#define k_lifo_init(lifo) \
2285 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002286
2287/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002288 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002289 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002290 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002291 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2292 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002293 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002294 * @note Can be called by ISRs.
2295 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002296 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002297 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002298 *
2299 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002300 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002301 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002302#define k_lifo_put(lifo, data) \
2303 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002304
2305/**
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002306 * @brief Add an element to a LIFO queue.
2307 *
2308 * This routine adds a data item to @a lifo. There is an implicit
2309 * memory allocation from the calling thread's resource pool, which is
2310 * automatically freed when the item is removed.
2311 *
2312 * @note Can be called by ISRs.
2313 *
2314 * @param lifo Address of the LIFO.
2315 * @param data Address of the data item.
2316 *
2317 * @retval 0 on success
2318 * @retval -ENOMEM if there isn't sufficient RAM in the caller's resource pool
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002319 * @req K-LIFO-001
Andrew Boie2b9b4b22018-04-27 13:21:22 -07002320 */
2321#define k_lifo_alloc_put(lifo, data) \
2322 k_queue_alloc_prepend((struct k_queue *) lifo, data)
2323
2324/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002325 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002326 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002327 * This routine removes a data item from @a lifo in a "last in, first out"
2328 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002329 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002330 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2331 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002332 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002333 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002334 * or one of the special values K_NO_WAIT and K_FOREVER.
2335 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002336 * @return Address of the data item if successful; NULL if returned
2337 * without waiting, or waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002338 * @req K-LIFO-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002339 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002340#define k_lifo_get(lifo, timeout) \
2341 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002342
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002343/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002344 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002345 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002346 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002347 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002348 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002349 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002350 * @param name Name of the fifo.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002351 * @req K-LIFO-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002352 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002353#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002354 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002355 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002356 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002357
Anas Nashif166f5192018-02-25 08:02:36 -06002358/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002359
2360/**
2361 * @cond INTERNAL_HIDDEN
2362 */
Andrew Boief3bee952018-05-02 17:44:39 -07002363#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002364
2365struct k_stack {
2366 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002367 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002368
Anas Nashif2f203c22016-12-18 06:57:45 -05002369 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002370 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002371};
2372
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002373#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002374 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002375 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002376 .base = stack_buffer, \
2377 .next = stack_buffer, \
2378 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002379 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002380 }
2381
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002382#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2383
Allan Stephensc98da842016-11-11 15:45:03 -05002384/**
2385 * INTERNAL_HIDDEN @endcond
2386 */
2387
2388/**
2389 * @defgroup stack_apis Stack APIs
2390 * @ingroup kernel_apis
2391 * @{
2392 */
2393
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002394/**
2395 * @brief Initialize a stack.
2396 *
2397 * This routine initializes a stack object, prior to its first use.
2398 *
2399 * @param stack Address of the stack.
2400 * @param buffer Address of array used to hold stacked values.
2401 * @param num_entries Maximum number of values that can be stacked.
2402 *
2403 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002404 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002405 */
Andrew Boief3bee952018-05-02 17:44:39 -07002406void k_stack_init(struct k_stack *stack,
2407 u32_t *buffer, unsigned int num_entries);
2408
2409
2410/**
2411 * @brief Initialize a stack.
2412 *
2413 * This routine initializes a stack object, prior to its first use. Internal
2414 * buffers will be allocated from the calling thread's resource pool.
2415 * This memory will be released if k_stack_cleanup() is called, or
2416 * userspace is enabled and the stack object loses all references to it.
2417 *
2418 * @param stack Address of the stack.
2419 * @param num_entries Maximum number of values that can be stacked.
2420 *
2421 * @return -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002422 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002423 */
2424
2425__syscall int k_stack_alloc_init(struct k_stack *stack,
2426 unsigned int num_entries);
2427
2428/**
2429 * @brief Release a stack's allocated buffer
2430 *
2431 * If a stack object was given a dynamically allocated buffer via
2432 * k_stack_alloc_init(), this will free it. This function does nothing
2433 * if the buffer wasn't dynamically allocated.
2434 *
2435 * @param stack Address of the stack.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002436 * @req K-STACK-001
Andrew Boief3bee952018-05-02 17:44:39 -07002437 */
2438void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002439
2440/**
2441 * @brief Push an element onto a stack.
2442 *
2443 * This routine adds a 32-bit value @a data to @a stack.
2444 *
2445 * @note Can be called by ISRs.
2446 *
2447 * @param stack Address of the stack.
2448 * @param data Value to push onto the stack.
2449 *
2450 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002451 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002452 */
Andrew Boiee8734462017-09-29 16:42:07 -07002453__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002454
2455/**
2456 * @brief Pop an element from a stack.
2457 *
2458 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2459 * manner and stores the value in @a data.
2460 *
2461 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2462 *
2463 * @param stack Address of the stack.
2464 * @param data Address of area to hold the value popped from the stack.
2465 * @param timeout Waiting period to obtain a value (in milliseconds),
2466 * or one of the special values K_NO_WAIT and K_FOREVER.
2467 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002468 * @retval 0 Element popped from stack.
2469 * @retval -EBUSY Returned without waiting.
2470 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002471 * @req K-STACK-001
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002472 */
Andrew Boiee8734462017-09-29 16:42:07 -07002473__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002474
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002475/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002476 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002478 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002479 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002480 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002482 * @param name Name of the stack.
2483 * @param stack_num_entries Maximum number of values that can be stacked.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002484 * @req K-STACK-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002485 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002486#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002487 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002488 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002489 struct k_stack name \
2490 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002491 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002492 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002493
Anas Nashif166f5192018-02-25 08:02:36 -06002494/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002495
Allan Stephens6bba9b02016-11-16 14:56:54 -05002496struct k_work;
2497
Allan Stephensc98da842016-11-11 15:45:03 -05002498/**
2499 * @defgroup workqueue_apis Workqueue Thread APIs
2500 * @ingroup kernel_apis
2501 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002502 */
2503
Allan Stephens6bba9b02016-11-16 14:56:54 -05002504/**
2505 * @typedef k_work_handler_t
2506 * @brief Work item handler function type.
2507 *
2508 * A work item's handler function is executed by a workqueue's thread
2509 * when the work item is processed by the workqueue.
2510 *
2511 * @param work Address of the work item.
2512 *
2513 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002514 * @req K-WORK-001
Allan Stephens6bba9b02016-11-16 14:56:54 -05002515 */
2516typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002517
2518/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002519 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002520 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002521
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002522struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002523 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002524 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002525};
2526
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002527enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002528 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002529};
2530
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002531struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002532 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002533 k_work_handler_t handler;
2534 atomic_t flags[1];
2535};
2536
Allan Stephens6bba9b02016-11-16 14:56:54 -05002537struct k_delayed_work {
2538 struct k_work work;
2539 struct _timeout timeout;
2540 struct k_work_q *work_q;
2541};
2542
2543extern struct k_work_q k_sys_work_q;
2544
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002545/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002546 * INTERNAL_HIDDEN @endcond
2547 */
2548
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002549#define _K_WORK_INITIALIZER(work_handler) \
2550 { \
2551 ._reserved = NULL, \
2552 .handler = work_handler, \
2553 .flags = { 0 } \
2554 }
2555
2556#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2557
Allan Stephens6bba9b02016-11-16 14:56:54 -05002558/**
2559 * @brief Initialize a statically-defined work item.
2560 *
2561 * This macro can be used to initialize a statically-defined workqueue work
2562 * item, prior to its first use. For example,
2563 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002564 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002565 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002566 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002567 * @param work_handler Function to invoke each time work item is processed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002568 * @req K-WORK-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002569 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002570#define K_WORK_DEFINE(work, work_handler) \
2571 struct k_work work \
2572 __in_section(_k_work, static, work) = \
2573 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002574
2575/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002576 * @brief Initialize a work item.
2577 *
2578 * This routine initializes a workqueue work item, prior to its first use.
2579 *
2580 * @param work Address of work item.
2581 * @param handler Function to invoke each time work item is processed.
2582 *
2583 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002584 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002585 */
2586static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2587{
Leandro Pereira0e23ad82018-05-29 10:42:17 -07002588 *work = (struct k_work)_K_WORK_INITIALIZER(handler);
Andrew Boie945af952017-08-22 13:15:23 -07002589 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002590}
2591
2592/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002593 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002594 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002595 * This routine submits work item @a work to be processed by workqueue
2596 * @a work_q. If the work item is already pending in the workqueue's queue
2597 * as a result of an earlier submission, this routine has no effect on the
2598 * work item. If the work item has already been processed, or is currently
2599 * being processed, its work is considered complete and the work item can be
2600 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002601 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002602 * @warning
2603 * A submitted work item must not be modified until it has been processed
2604 * by the workqueue.
2605 *
2606 * @note Can be called by ISRs.
2607 *
2608 * @param work_q Address of workqueue.
2609 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002610 *
2611 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002612 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002613 */
2614static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2615 struct k_work *work)
2616{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002617 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002618 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002619 }
2620}
2621
2622/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002623 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002624 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002625 * This routine indicates if work item @a work is pending in a workqueue's
2626 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002627 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002628 * @note Can be called by ISRs.
2629 *
2630 * @param work Address of work item.
2631 *
2632 * @return 1 if work item is pending, or 0 if it is not pending.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002633 * @req K-WORK-001
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002634 */
2635static inline int k_work_pending(struct k_work *work)
2636{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002637 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002638}
2639
2640/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002641 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002642 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002643 * This routine starts workqueue @a work_q. The workqueue spawns its work
2644 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002645 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002646 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002647 * @param stack Pointer to work queue thread's stack space, as defined by
2648 * K_THREAD_STACK_DEFINE()
2649 * @param stack_size Size of the work queue thread's stack (in bytes), which
2650 * should either be the same constant passed to
2651 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002652 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002653 *
2654 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002655 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002656 */
Andrew Boie507852a2017-07-25 18:47:07 -07002657extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002658 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002659 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002660
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002661/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002662 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002663 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002664 * This routine initializes a workqueue delayed work item, prior to
2665 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002666 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002667 * @param work Address of delayed work item.
2668 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002669 *
2670 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002671 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002672 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002673extern void k_delayed_work_init(struct k_delayed_work *work,
2674 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002675
2676/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002677 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002678 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002679 * This routine schedules work item @a work to be processed by workqueue
2680 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002681 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002682 * Only when the countdown completes is the work item actually submitted to
2683 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002684 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002685 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002686 * counting down cancels the existing submission and restarts the
2687 * countdown using the new delay. Note that this behavior is
2688 * inherently subject to race conditions with the pre-existing
2689 * timeouts and work queue, so care must be taken to synchronize such
2690 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002691 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002692 * @warning
2693 * A delayed work item must not be modified until it has been processed
2694 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002695 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002696 * @note Can be called by ISRs.
2697 *
2698 * @param work_q Address of workqueue.
2699 * @param work Address of delayed work item.
2700 * @param delay Delay before submitting the work item (in milliseconds).
2701 *
2702 * @retval 0 Work item countdown started.
2703 * @retval -EINPROGRESS Work item is already pending.
2704 * @retval -EINVAL Work item is being processed or has completed its work.
2705 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002706 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002707 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002708extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2709 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002710 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002711
2712/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002713 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002714 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002715 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002716 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002717 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002718 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002719 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002720 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002721 * @param work Address of delayed work item.
2722 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002723 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002724 * @retval -EINPROGRESS Work item is already pending.
2725 * @retval -EINVAL Work item is being processed or has completed its work.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002726 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002727 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002728extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002729
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002730/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002731 * @brief Submit a work item to the system workqueue.
2732 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002733 * This routine submits work item @a work to be processed by the system
2734 * workqueue. If the work item is already pending in the workqueue's queue
2735 * as a result of an earlier submission, this routine has no effect on the
2736 * work item. If the work item has already been processed, or is currently
2737 * being processed, its work is considered complete and the work item can be
2738 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002739 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002740 * @warning
2741 * Work items submitted to the system workqueue should avoid using handlers
2742 * that block or yield since this may prevent the system workqueue from
2743 * processing other work items in a timely manner.
2744 *
2745 * @note Can be called by ISRs.
2746 *
2747 * @param work Address of work item.
2748 *
2749 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002750 * @req K-WORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002751 */
2752static inline void k_work_submit(struct k_work *work)
2753{
2754 k_work_submit_to_queue(&k_sys_work_q, work);
2755}
2756
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002757/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002758 * @brief Submit a delayed work item to the system workqueue.
2759 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002760 * This routine schedules work item @a work to be processed by the system
2761 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002762 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002763 * Only when the countdown completes is the work item actually submitted to
2764 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002765 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002766 * Submitting a previously submitted delayed work item that is still
2767 * counting down cancels the existing submission and restarts the countdown
2768 * using the new delay. If the work item is currently pending on the
2769 * workqueue's queue because the countdown has completed it is too late to
2770 * resubmit the item, and resubmission fails without impacting the work item.
2771 * If the work item has already been processed, or is currently being processed,
2772 * its work is considered complete and the work item can be resubmitted.
2773 *
2774 * @warning
2775 * Work items submitted to the system workqueue should avoid using handlers
2776 * that block or yield since this may prevent the system workqueue from
2777 * processing other work items in a timely manner.
2778 *
2779 * @note Can be called by ISRs.
2780 *
2781 * @param work Address of delayed work item.
2782 * @param delay Delay before submitting the work item (in milliseconds).
2783 *
2784 * @retval 0 Work item countdown started.
2785 * @retval -EINPROGRESS Work item is already pending.
2786 * @retval -EINVAL Work item is being processed or has completed its work.
2787 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002788 * @req K-DWORK-001
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002789 */
2790static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002791 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002792{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002793 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002794}
2795
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002796/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002797 * @brief Get time remaining before a delayed work gets scheduled.
2798 *
2799 * This routine computes the (approximate) time remaining before a
2800 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002801 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002802 *
2803 * @param work Delayed work item.
2804 *
2805 * @return Remaining time (in milliseconds).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002806 * @req K-DWORK-001
Johan Hedbergc8201b22016-12-09 10:42:22 +02002807 */
Kumar Galacc334c72017-04-21 10:55:34 -05002808static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002809{
2810 return _timeout_remaining_get(&work->timeout);
2811}
2812
Anas Nashif166f5192018-02-25 08:02:36 -06002813/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002814/**
Anas Nashifce78d162018-05-24 12:43:11 -05002815 * @defgroup mutex_apis Mutex APIs
2816 * @ingroup kernel_apis
2817 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05002818 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002819
Anas Nashifce78d162018-05-24 12:43:11 -05002820/**
2821 * Mutex Structure
2822 * @ingroup mutex_apis
2823 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002824struct k_mutex {
2825 _wait_q_t wait_q;
Anas Nashifce78d162018-05-24 12:43:11 -05002826 /** Mutex owner */
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002827 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002828 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002829 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002830
Anas Nashif2f203c22016-12-18 06:57:45 -05002831 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002832};
2833
Anas Nashifce78d162018-05-24 12:43:11 -05002834/**
2835 * @cond INTERNAL_HIDDEN
2836 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002837#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002838 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002839 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002840 .owner = NULL, \
2841 .lock_count = 0, \
2842 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002843 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002844 }
2845
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002846#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2847
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002848/**
Allan Stephensc98da842016-11-11 15:45:03 -05002849 * INTERNAL_HIDDEN @endcond
2850 */
2851
2852/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002853 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002855 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002856 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002857 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002858 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002859 * @param name Name of the mutex.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002860 * @req K-MUTEX-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002861 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002862#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002863 struct k_mutex name \
2864 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002865 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002866
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002867/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002868 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002870 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002872 * Upon completion, the mutex is available and does not have an owner.
2873 *
2874 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002875 *
2876 * @return N/A
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 void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002880
2881/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002882 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002884 * This routine locks @a mutex. If the mutex is locked by another thread,
2885 * the calling thread waits until the mutex becomes available or until
2886 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002888 * A thread is permitted to lock a mutex it has already locked. The operation
2889 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002891 * @param mutex Address of the mutex.
2892 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002893 * or one of the special values K_NO_WAIT and K_FOREVER.
2894 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002895 * @retval 0 Mutex locked.
2896 * @retval -EBUSY Returned without waiting.
2897 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002898 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002899 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002900__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002901
2902/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002903 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002904 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002905 * This routine unlocks @a mutex. The mutex must already be locked by the
2906 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002907 *
2908 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002909 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002910 * thread.
2911 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002912 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002913 *
2914 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002915 * @req K-MUTEX-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002916 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002917__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002918
Allan Stephensc98da842016-11-11 15:45:03 -05002919/**
Anas Nashif166f5192018-02-25 08:02:36 -06002920 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002921 */
2922
2923/**
2924 * @cond INTERNAL_HIDDEN
2925 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002926
2927struct k_sem {
2928 _wait_q_t wait_q;
2929 unsigned int count;
2930 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002931 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002932
Anas Nashif2f203c22016-12-18 06:57:45 -05002933 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002934};
2935
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002936#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002937 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07002938 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Allan Stephensc98da842016-11-11 15:45:03 -05002939 .count = initial_count, \
2940 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002941 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002942 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002943 }
2944
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002945#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2946
Allan Stephensc98da842016-11-11 15:45:03 -05002947/**
2948 * INTERNAL_HIDDEN @endcond
2949 */
2950
2951/**
2952 * @defgroup semaphore_apis Semaphore APIs
2953 * @ingroup kernel_apis
2954 * @{
2955 */
2956
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002957/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002958 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002959 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002960 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002962 * @param sem Address of the semaphore.
2963 * @param initial_count Initial semaphore count.
2964 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002965 *
2966 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002967 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002968 */
Andrew Boie99280232017-09-29 14:17:47 -07002969__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2970 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002971
2972/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002973 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002975 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002977 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2978 *
2979 * @param sem Address of the semaphore.
2980 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002981 * or one of the special values K_NO_WAIT and K_FOREVER.
2982 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002983 * @note When porting code from the nanokernel legacy API to the new API, be
2984 * careful with the return value of this function. The return value is the
2985 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2986 * non-zero means failure, while the nano_sem_take family returns 1 for success
2987 * and 0 for failure.
2988 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002989 * @retval 0 Semaphore taken.
2990 * @retval -EBUSY Returned without waiting.
2991 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04002992 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002993 */
Andrew Boie99280232017-09-29 14:17:47 -07002994__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002995
2996/**
2997 * @brief Give a semaphore.
2998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002999 * This routine gives @a sem, unless the semaphore is already at its maximum
3000 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003001 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003002 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003004 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003005 *
3006 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003007 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003008 */
Andrew Boie99280232017-09-29 14:17:47 -07003009__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003010
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003011/**
3012 * @brief Reset a semaphore's count to zero.
3013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003014 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003015 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003016 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003017 *
3018 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003019 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003020 */
Andrew Boie990bf162017-10-03 12:36:49 -07003021__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003022
Anas Nashif954d5502018-02-25 08:37:28 -06003023/**
3024 * @internal
3025 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003026static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003027{
3028 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003029}
3030
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003031/**
3032 * @brief Get a semaphore's count.
3033 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003034 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003035 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003036 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003038 * @return Current semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003039 * @req K-SEM-001
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003040 */
Andrew Boie990bf162017-10-03 12:36:49 -07003041__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07003042
Anas Nashif954d5502018-02-25 08:37:28 -06003043/**
3044 * @internal
3045 */
Andrew Boiefc273c02017-09-23 12:51:23 -07003046static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003047{
3048 return sem->count;
3049}
3050
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003051/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003052 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003053 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003054 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003055 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003056 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003057 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003058 * @param name Name of the semaphore.
3059 * @param initial_count Initial semaphore count.
3060 * @param count_limit Maximum permitted semaphore count.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003061 * @req K-SEM-002
Benjamin Walshb9c1a062016-10-15 17:12:35 -04003062 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003063#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003064 struct k_sem name \
3065 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07003066 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05303067 BUILD_ASSERT(((count_limit) != 0) && \
3068 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003069
Anas Nashif166f5192018-02-25 08:02:36 -06003070/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003071
3072/**
3073 * @defgroup alert_apis Alert APIs
3074 * @ingroup kernel_apis
3075 * @{
3076 */
3077
Allan Stephens5eceb852016-11-16 10:16:30 -05003078/**
3079 * @typedef k_alert_handler_t
3080 * @brief Alert handler function type.
3081 *
3082 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07003083 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05003084 * and is only invoked if the alert has been initialized with one.
3085 *
3086 * @param alert Address of the alert.
3087 *
3088 * @return 0 if alert has been consumed; non-zero if alert should pend.
3089 */
3090typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05003091
Anas Nashif166f5192018-02-25 08:02:36 -06003092/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003093
3094/**
3095 * @cond INTERNAL_HIDDEN
3096 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003097
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003098#define K_ALERT_DEFAULT NULL
3099#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003100
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003101struct k_alert {
3102 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003103 atomic_t send_count;
3104 struct k_work work_item;
3105 struct k_sem sem;
3106
Anas Nashif2f203c22016-12-18 06:57:45 -05003107 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003108};
3109
Anas Nashif954d5502018-02-25 08:37:28 -06003110/**
3111 * @internal
3112 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003113extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003114
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003115#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003116 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003117 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003118 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003119 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
3120 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003121 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003122 }
3123
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003124#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
3125
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003126/**
Allan Stephensc98da842016-11-11 15:45:03 -05003127 * INTERNAL_HIDDEN @endcond
3128 */
3129
3130/**
3131 * @addtogroup alert_apis
3132 * @{
3133 */
3134
3135/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003136 * @def K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts)
3137 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003138 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003139 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003140 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003141 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003142 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003144 * @param name Name of the alert.
3145 * @param alert_handler Action to take when alert is sent. Specify either
3146 * the address of a function to be invoked by the system workqueue
3147 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
3148 * K_ALERT_DEFAULT (which causes the alert to pend).
3149 * @param max_num_pending_alerts Maximum number of pending alerts.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003150 *
3151 * @req K-ALERT-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003152 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003153#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04003154 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003155 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003156 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003157 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003158
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003159/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003160 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003161 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003162 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003164 * @param alert Address of the alert.
3165 * @param handler Action to take when alert is sent. Specify either the address
3166 * of a function to be invoked by the system workqueue thread,
3167 * K_ALERT_IGNORE (which causes the alert to be ignored), or
3168 * K_ALERT_DEFAULT (which causes the alert to pend).
3169 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003170 *
3171 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003172 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003173 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04003174extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
3175 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003176
3177/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003178 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003179 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003180 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003181 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003182 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
3183 *
3184 * @param alert Address of the alert.
3185 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003186 * or one of the special values K_NO_WAIT and K_FOREVER.
3187 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003188 * @retval 0 Alert received.
3189 * @retval -EBUSY Returned without waiting.
3190 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003191 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003192 */
Andrew Boie310e9872017-09-29 04:41:15 -07003193__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003194
3195/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003196 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003197 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003198 * This routine signals @a alert. The action specified for @a alert will
3199 * be taken, which may trigger the execution of an alert handler function
3200 * and/or cause the alert to pend (assuming the alert has not reached its
3201 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003202 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003203 * @note Can be called by ISRs.
3204 *
3205 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003206 *
3207 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003208 * @req K-ALERT-002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003209 */
Andrew Boie310e9872017-09-29 04:41:15 -07003210__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003211
3212/**
Anas Nashif166f5192018-02-25 08:02:36 -06003213 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003214 */
3215
Allan Stephensc98da842016-11-11 15:45:03 -05003216/**
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003217 * @defgroup msgq_apis Message Queue APIs
3218 * @ingroup kernel_apis
3219 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003220 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003221
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003222/**
3223 * @brief Message Queue Structure
3224 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003225struct k_msgq {
3226 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04003227 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05003228 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003229 char *buffer_start;
3230 char *buffer_end;
3231 char *read_ptr;
3232 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05003233 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003234
Anas Nashif2f203c22016-12-18 06:57:45 -05003235 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07003236 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003237};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003238/**
3239 * @cond INTERNAL_HIDDEN
3240 */
3241
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003242
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003243#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003244 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003245 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003246 .max_msgs = q_max_msgs, \
3247 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003248 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003249 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003250 .read_ptr = q_buffer, \
3251 .write_ptr = q_buffer, \
3252 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003253 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003254 }
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003255#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003256/**
3257 * INTERNAL_HIDDEN @endcond
3258 */
3259
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003260
Andrew Boie0fe789f2018-04-12 18:35:56 -07003261#define K_MSGQ_FLAG_ALLOC BIT(0)
3262
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003263/**
3264 * @brief Message Queue Attributes
3265 */
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303266struct k_msgq_attrs {
3267 size_t msg_size;
3268 u32_t max_msgs;
3269 u32_t used_msgs;
3270};
3271
Allan Stephensc98da842016-11-11 15:45:03 -05003272
3273/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003274 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003275 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003276 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3277 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003278 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3279 * message is similarly aligned to this boundary, @a q_msg_size must also be
3280 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003281 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003282 * The message queue can be accessed outside the module where it is defined
3283 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003284 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003285 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003286 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003287 * @param q_name Name of the message queue.
3288 * @param q_msg_size Message size (in bytes).
3289 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003290 * @param q_align Alignment of the message queue's ring buffer.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003291 *
3292 * @req K-MSGQ-001
Peter Mitsis1da807e2016-10-06 11:36:59 -04003293 */
3294#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003295 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003296 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003297 struct k_msgq q_name \
3298 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003299 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003300 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003301
Peter Mitsisd7a37502016-10-13 11:37:40 -04003302/**
3303 * @brief Initialize a message queue.
3304 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003305 * This routine initializes a message queue object, prior to its first use.
3306 *
Allan Stephensda827222016-11-09 14:23:58 -06003307 * The message queue's ring buffer must contain space for @a max_msgs messages,
3308 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3309 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3310 * that each message is similarly aligned to this boundary, @a q_msg_size
3311 * must also be a multiple of N.
3312 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003313 * @param q Address of the message queue.
3314 * @param buffer Pointer to ring buffer that holds queued messages.
3315 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003316 * @param max_msgs Maximum number of messages that can be queued.
3317 *
3318 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003319 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003320 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003321void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3322 u32_t max_msgs);
3323
3324/**
3325 * @brief Initialize a message queue.
3326 *
3327 * This routine initializes a message queue object, prior to its first use,
3328 * allocating its internal ring buffer from the calling thread's resource
3329 * pool.
3330 *
3331 * Memory allocated for the ring buffer can be released by calling
3332 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3333 * all of its references.
3334 *
3335 * @param q Address of the message queue.
3336 * @param msg_size Message size (in bytes).
3337 * @param max_msgs Maximum number of messages that can be queued.
3338 *
3339 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3340 * thread's resource pool, or -EINVAL if the size parameters cause
3341 * an integer overflow.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003342 * @req K-MSGQ-002
Andrew Boie0fe789f2018-04-12 18:35:56 -07003343 */
3344__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3345 u32_t max_msgs);
3346
3347
3348void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003349
3350/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003351 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003352 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003353 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003354 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003355 * @note Can be called by ISRs.
3356 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003357 * @param q Address of the message queue.
3358 * @param data Pointer to the message.
3359 * @param timeout Waiting period to add the message (in milliseconds),
3360 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003361 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003362 * @retval 0 Message sent.
3363 * @retval -ENOMSG Returned without waiting or queue purged.
3364 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003365 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003366 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003367__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003368
3369/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003370 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003371 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003372 * This routine receives a message from message queue @a q in a "first in,
3373 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003374 *
Allan Stephensc98da842016-11-11 15:45:03 -05003375 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003376 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003377 * @param q Address of the message queue.
3378 * @param data Address of area to hold the received message.
3379 * @param timeout Waiting period to receive the message (in milliseconds),
3380 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003381 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003382 * @retval 0 Message received.
3383 * @retval -ENOMSG Returned without waiting.
3384 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003385 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003386 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003387__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003388
3389/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003390 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003391 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003392 * This routine discards all unreceived messages in a message queue's ring
3393 * buffer. Any threads that are blocked waiting to send a message to the
3394 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003395 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003396 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003397 *
3398 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003399 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003400 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003401__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003402
Peter Mitsis67be2492016-10-07 11:44:34 -04003403/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003404 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003405 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003406 * This routine returns the number of unused entries in a message queue's
3407 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003409 * @param q Address of the message queue.
3410 *
3411 * @return Number of unused ring buffer entries.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003412 * @req K-MSGQ-002
Peter Mitsis67be2492016-10-07 11:44:34 -04003413 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003414__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3415
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303416/**
3417 * @brief Get basic attributes of a message queue.
3418 *
3419 * This routine fetches basic attributes of message queue into attr argument.
3420 *
3421 * @param q Address of the message queue.
3422 * @param attrs pointer to message queue attribute structure.
3423 *
3424 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003425 * @req K-MSGQ-003
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303426 */
3427__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3428
3429
Andrew Boie82edb6e2017-10-02 10:53:06 -07003430static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003431{
3432 return q->max_msgs - q->used_msgs;
3433}
3434
Peter Mitsisd7a37502016-10-13 11:37:40 -04003435/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003436 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003437 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003438 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003439 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003440 * @param q Address of the message queue.
3441 *
3442 * @return Number of messages.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003443 * @req K-MSGQ-002
Peter Mitsisd7a37502016-10-13 11:37:40 -04003444 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003445__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3446
3447static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003448{
3449 return q->used_msgs;
3450}
3451
Anas Nashif166f5192018-02-25 08:02:36 -06003452/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003453
3454/**
3455 * @defgroup mem_pool_apis Memory Pool APIs
3456 * @ingroup kernel_apis
3457 * @{
3458 */
3459
Andy Ross73cb9582017-05-09 10:42:39 -07003460/* Note on sizing: the use of a 20 bit field for block means that,
3461 * assuming a reasonable minimum block size of 16 bytes, we're limited
3462 * to 16M of memory managed by a single pool. Long term it would be
3463 * good to move to a variable bit size based on configuration.
3464 */
3465struct k_mem_block_id {
3466 u32_t pool : 8;
3467 u32_t level : 4;
3468 u32_t block : 20;
3469};
3470
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003471struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003472 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003473 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003474};
3475
Anas Nashif166f5192018-02-25 08:02:36 -06003476/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003477
3478/**
3479 * @defgroup mailbox_apis Mailbox APIs
3480 * @ingroup kernel_apis
3481 * @{
3482 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003483
3484struct k_mbox_msg {
3485 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003486 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003487 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003488 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003489 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003490 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003491 /** sender's message data buffer */
3492 void *tx_data;
3493 /** internal use only - needed for legacy API support */
3494 void *_rx_data;
3495 /** message data block descriptor */
3496 struct k_mem_block tx_block;
3497 /** source thread id */
3498 k_tid_t rx_source_thread;
3499 /** target thread id */
3500 k_tid_t tx_target_thread;
3501 /** internal use only - thread waiting on send (may be a dummy) */
3502 k_tid_t _syncing_thread;
3503#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3504 /** internal use only - semaphore used during asynchronous send */
3505 struct k_sem *_async_sem;
3506#endif
3507};
3508
3509struct k_mbox {
3510 _wait_q_t tx_msg_queue;
3511 _wait_q_t rx_msg_queue;
3512
Anas Nashif2f203c22016-12-18 06:57:45 -05003513 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003514};
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003515/**
3516 * @cond INTERNAL_HIDDEN
3517 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003518
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003519#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003520 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003521 .tx_msg_queue = _WAIT_Q_INIT(&obj.tx_msg_queue), \
3522 .rx_msg_queue = _WAIT_Q_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003523 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003524 }
3525
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003526#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3527
Peter Mitsis12092702016-10-14 12:57:23 -04003528/**
Allan Stephensc98da842016-11-11 15:45:03 -05003529 * INTERNAL_HIDDEN @endcond
3530 */
3531
3532/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003533 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003534 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003535 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003536 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003537 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003538 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003539 * @param name Name of the mailbox.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003540 * @req K-MBOX-001
Peter Mitsis12092702016-10-14 12:57:23 -04003541 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003542#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003543 struct k_mbox name \
3544 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003545 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003546
Peter Mitsis12092702016-10-14 12:57:23 -04003547/**
3548 * @brief Initialize a mailbox.
3549 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003550 * This routine initializes a mailbox object, prior to its first use.
3551 *
3552 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003553 *
3554 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003555 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003556 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003557extern void k_mbox_init(struct k_mbox *mbox);
3558
Peter Mitsis12092702016-10-14 12:57:23 -04003559/**
3560 * @brief Send a mailbox message in a synchronous manner.
3561 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003562 * This routine sends a message to @a mbox and waits for a receiver to both
3563 * receive and process it. The message data may be in a buffer, in a memory
3564 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003565 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003566 * @param mbox Address of the mailbox.
3567 * @param tx_msg Address of the transmit message descriptor.
3568 * @param timeout Waiting period for the message to be received (in
3569 * milliseconds), or one of the special values K_NO_WAIT
3570 * and K_FOREVER. Once the message has been received,
3571 * this routine waits as long as necessary for the message
3572 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003573 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003574 * @retval 0 Message sent.
3575 * @retval -ENOMSG Returned without waiting.
3576 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003577 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003578 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003579extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003580 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003581
Peter Mitsis12092702016-10-14 12:57:23 -04003582/**
3583 * @brief Send a mailbox message in an asynchronous manner.
3584 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003585 * This routine sends a message to @a mbox without waiting for a receiver
3586 * to process it. The message data may be in a buffer, in a memory pool block,
3587 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3588 * will be given when the message has been both received and completely
3589 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003591 * @param mbox Address of the mailbox.
3592 * @param tx_msg Address of the transmit message descriptor.
3593 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003594 *
3595 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003596 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003597 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003598extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003599 struct k_sem *sem);
3600
Peter Mitsis12092702016-10-14 12:57:23 -04003601/**
3602 * @brief Receive a mailbox message.
3603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003604 * This routine receives a message from @a mbox, then optionally retrieves
3605 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003606 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003607 * @param mbox Address of the mailbox.
3608 * @param rx_msg Address of the receive message descriptor.
3609 * @param buffer Address of the buffer to receive data, or NULL to defer data
3610 * retrieval and message disposal until later.
3611 * @param timeout Waiting period for a message to be received (in
3612 * milliseconds), or one of the special values K_NO_WAIT
3613 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003614 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003615 * @retval 0 Message received.
3616 * @retval -ENOMSG Returned without waiting.
3617 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003618 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003619 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003620extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003621 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003622
3623/**
3624 * @brief Retrieve mailbox message data into a buffer.
3625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003626 * This routine completes the processing of a received message by retrieving
3627 * its data into a buffer, then disposing of the message.
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.
3631 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003632 * @param rx_msg Address of the receive message descriptor.
3633 * @param buffer Address of the buffer to receive data, or NULL to discard
3634 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003635 *
3636 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003637 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003638 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003639extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003640
3641/**
3642 * @brief Retrieve mailbox message data into a memory pool block.
3643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003644 * This routine completes the processing of a received message by retrieving
3645 * its data into a memory pool block, then disposing of the message.
3646 * The memory pool block that results from successful retrieval must be
3647 * returned to the pool once the data has been processed, even in cases
3648 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003649 *
3650 * Alternatively, this routine can be used to dispose of a received message
3651 * without retrieving its data. In this case there is no need to return a
3652 * memory pool block to the pool.
3653 *
3654 * This routine allocates a new memory pool block for the data only if the
3655 * data is not already in one. If a new block cannot be allocated, the routine
3656 * returns a failure code and the received message is left unchanged. This
3657 * permits the caller to reattempt data retrieval at a later time or to dispose
3658 * of the received message without retrieving its data.
3659 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003660 * @param rx_msg Address of a receive message descriptor.
3661 * @param pool Address of memory pool, or NULL to discard data.
3662 * @param block Address of the area to hold memory pool block info.
3663 * @param timeout Waiting period to wait for a memory pool block (in
3664 * milliseconds), or one of the special values K_NO_WAIT
3665 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003666 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003667 * @retval 0 Data retrieved.
3668 * @retval -ENOMEM Returned without waiting.
3669 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003670 * @req K-MBOX-002
Peter Mitsis12092702016-10-14 12:57:23 -04003671 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003672extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003673 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003674 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003675
Anas Nashif166f5192018-02-25 08:02:36 -06003676/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003677
3678/**
Anas Nashifce78d162018-05-24 12:43:11 -05003679 * @defgroup pipe_apis Pipe APIs
3680 * @ingroup kernel_apis
3681 * @{
Allan Stephensc98da842016-11-11 15:45:03 -05003682 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003683
Anas Nashifce78d162018-05-24 12:43:11 -05003684/** Pipe Structure */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003685struct k_pipe {
Anas Nashifce78d162018-05-24 12:43:11 -05003686 unsigned char *buffer; /**< Pipe buffer: may be NULL */
3687 size_t size; /**< Buffer size */
3688 size_t bytes_used; /**< # bytes used in buffer */
3689 size_t read_index; /**< Where in buffer to read from */
3690 size_t write_index; /**< Where in buffer to write */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003691
3692 struct {
Anas Nashifce78d162018-05-24 12:43:11 -05003693 _wait_q_t readers; /**< Reader wait queue */
3694 _wait_q_t writers; /**< Writer wait queue */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003695 } wait_q;
3696
Anas Nashif2f203c22016-12-18 06:57:45 -05003697 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Anas Nashifce78d162018-05-24 12:43:11 -05003698 u8_t flags; /**< Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003699};
3700
Anas Nashifce78d162018-05-24 12:43:11 -05003701/**
3702 * @cond INTERNAL_HIDDEN
3703 */
3704#define K_PIPE_FLAG_ALLOC BIT(0) /** Buffer was allocated */
3705
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003706#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003707 { \
3708 .buffer = pipe_buffer, \
3709 .size = pipe_buffer_size, \
3710 .bytes_used = 0, \
3711 .read_index = 0, \
3712 .write_index = 0, \
Andy Rossccf3bf72018-05-10 11:10:34 -07003713 .wait_q.writers = _WAIT_Q_INIT(&obj.wait_q.writers), \
3714 .wait_q.readers = _WAIT_Q_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003715 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003716 }
3717
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003718#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3719
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003720/**
Allan Stephensc98da842016-11-11 15:45:03 -05003721 * INTERNAL_HIDDEN @endcond
3722 */
3723
3724/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003725 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003726 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003727 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003728 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003729 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003730 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003731 * @param name Name of the pipe.
3732 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3733 * or zero if no ring buffer is used.
3734 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003735 *
3736 * @req K-PIPE-001
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003737 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003738#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3739 static unsigned char __kernel_noinit __aligned(pipe_align) \
3740 _k_pipe_buf_##name[pipe_buffer_size]; \
3741 struct k_pipe name \
3742 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003743 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003744
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003745/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003746 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003747 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003748 * This routine initializes a pipe object, prior to its first use.
3749 *
3750 * @param pipe Address of the pipe.
3751 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3752 * is used.
3753 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3754 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003755 *
3756 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003757 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003758 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003759void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3760
3761/**
3762 * @brief Release a pipe's allocated buffer
3763 *
3764 * If a pipe object was given a dynamically allocated buffer via
3765 * k_pipe_alloc_init(), this will free it. This function does nothing
3766 * if the buffer wasn't dynamically allocated.
3767 *
3768 * @param pipe Address of the pipe.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003769 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003770 */
3771void k_pipe_cleanup(struct k_pipe *pipe);
3772
3773/**
3774 * @brief Initialize a pipe and allocate a buffer for it
3775 *
3776 * Storage for the buffer region will be allocated from the calling thread's
3777 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3778 * or userspace is enabled and the pipe object loses all references to it.
3779 *
3780 * This function should only be called on uninitialized pipe objects.
3781 *
3782 * @param pipe Address of the pipe.
3783 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3784 * buffer is used.
3785 * @retval 0 on success
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07003786 * @retval -ENOMEM if memory couldn't be allocated
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003787 * @req K-PIPE-002
Andrew Boie44fe8122018-04-12 17:38:12 -07003788 */
3789__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003790
3791/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003792 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003793 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003794 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003796 * @param pipe Address of the pipe.
3797 * @param data Address of data to write.
3798 * @param bytes_to_write Size of data (in bytes).
3799 * @param bytes_written Address of area to hold the number of bytes written.
3800 * @param min_xfer Minimum number of bytes to write.
3801 * @param timeout Waiting period to wait for the data to be written (in
3802 * milliseconds), or one of the special values K_NO_WAIT
3803 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003804 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003805 * @retval 0 At least @a min_xfer bytes of data were written.
3806 * @retval -EIO Returned without waiting; zero data bytes were written.
3807 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003808 * minus one data bytes were written.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003809 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003810 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003811__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3812 size_t bytes_to_write, size_t *bytes_written,
3813 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003814
3815/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003816 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003817 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003818 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003820 * @param pipe Address of the pipe.
3821 * @param data Address to place the data read from pipe.
3822 * @param bytes_to_read Maximum number of data bytes to read.
3823 * @param bytes_read Address of area to hold the number of bytes read.
3824 * @param min_xfer Minimum number of data bytes to read.
3825 * @param timeout Waiting period to wait for the data to be read (in
3826 * milliseconds), or one of the special values K_NO_WAIT
3827 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003828 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003829 * @retval 0 At least @a min_xfer bytes of data were read.
3830 * @retval -EIO Returned without waiting; zero data bytes were read.
3831 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003832 * minus one data bytes were read.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003833 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003834 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003835__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3836 size_t bytes_to_read, size_t *bytes_read,
3837 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003838
3839/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003840 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003842 * This routine writes the data contained in a memory block to @a pipe.
3843 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003844 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003845 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003846 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003847 * @param block Memory block containing data to send
3848 * @param size Number of data bytes in memory block to send
3849 * @param sem Semaphore to signal upon completion (else NULL)
3850 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003851 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003852 * @req K-PIPE-002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003853 */
3854extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3855 size_t size, struct k_sem *sem);
3856
Anas Nashif166f5192018-02-25 08:02:36 -06003857/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003858
Allan Stephensc98da842016-11-11 15:45:03 -05003859/**
3860 * @cond INTERNAL_HIDDEN
3861 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003862
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003863struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003864 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003865 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003866 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003867 char *buffer;
3868 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003869 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003870
Anas Nashif2f203c22016-12-18 06:57:45 -05003871 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003872};
3873
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003874#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003875 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003876 { \
Andy Rossccf3bf72018-05-10 11:10:34 -07003877 .wait_q = _WAIT_Q_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003878 .num_blocks = slab_num_blocks, \
3879 .block_size = slab_block_size, \
3880 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003881 .free_list = NULL, \
3882 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003883 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003884 }
3885
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003886#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3887
3888
Peter Mitsis578f9112016-10-07 13:50:31 -04003889/**
Allan Stephensc98da842016-11-11 15:45:03 -05003890 * INTERNAL_HIDDEN @endcond
3891 */
3892
3893/**
3894 * @defgroup mem_slab_apis Memory Slab APIs
3895 * @ingroup kernel_apis
3896 * @{
3897 */
3898
3899/**
Allan Stephensda827222016-11-09 14:23:58 -06003900 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003901 *
Allan Stephensda827222016-11-09 14:23:58 -06003902 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003903 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003904 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3905 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003906 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003907 *
Allan Stephensda827222016-11-09 14:23:58 -06003908 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003909 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003910 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003911 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003912 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003913 * @param name Name of the memory slab.
3914 * @param slab_block_size Size of each memory block (in bytes).
3915 * @param slab_num_blocks Number memory blocks.
3916 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003917 * @req K-MSLAB-001
Peter Mitsis578f9112016-10-07 13:50:31 -04003918 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003919#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3920 char __noinit __aligned(slab_align) \
3921 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3922 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003923 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003924 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003925 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003926
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003927/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003928 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003929 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003930 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003931 *
Allan Stephensda827222016-11-09 14:23:58 -06003932 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3933 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3934 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3935 * To ensure that each memory block is similarly aligned to this boundary,
3936 * @a slab_block_size must also be a multiple of N.
3937 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003938 * @param slab Address of the memory slab.
3939 * @param buffer Pointer to buffer used for the memory blocks.
3940 * @param block_size Size of each memory block (in bytes).
3941 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003942 *
3943 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003944 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003945 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003946extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003947 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003948
3949/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003950 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003952 * This routine allocates a memory block from a 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.
3956 * @param timeout Maximum time to wait for operation to complete
3957 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3958 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003959 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003960 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003961 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003962 * @retval -ENOMEM Returned without waiting.
3963 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003964 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003965 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003966extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003967 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003968
3969/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003970 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003972 * This routine releases a previously allocated memory block back to its
3973 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003975 * @param slab Address of the memory slab.
3976 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003977 *
3978 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003979 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003980 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003981extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003982
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003983/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003984 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003986 * This routine gets the number of memory blocks that are currently
3987 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003989 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003990 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003991 * @return Number of allocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04003992 * @req K-MSLAB-002
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003993 */
Kumar Galacc334c72017-04-21 10:55:34 -05003994static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003995{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003996 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003997}
3998
Peter Mitsisc001aa82016-10-13 13:53:37 -04003999/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004000 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004001 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004002 * This routine gets the number of memory blocks that are currently
4003 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004005 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04004006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004007 * @return Number of unallocated memory blocks.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004008 * @req K-MSLAB-002
Peter Mitsisc001aa82016-10-13 13:53:37 -04004009 */
Kumar Galacc334c72017-04-21 10:55:34 -05004010static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04004011{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04004012 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04004013}
4014
Anas Nashif166f5192018-02-25 08:02:36 -06004015/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004016
4017/**
4018 * @cond INTERNAL_HIDDEN
4019 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004020
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004021struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08004022 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004023 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004024};
4025
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004026/**
Allan Stephensc98da842016-11-11 15:45:03 -05004027 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04004028 */
4029
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004030/**
Allan Stephensc98da842016-11-11 15:45:03 -05004031 * @addtogroup mem_pool_apis
4032 * @{
4033 */
4034
4035/**
4036 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004037 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004038 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
4039 * long. The memory pool allows blocks to be repeatedly partitioned into
4040 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07004041 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004042 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004043 * If the pool is to be accessed outside the module where it is defined, it
4044 * can be declared via
4045 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05004046 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04004047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004048 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07004049 * @param minsz Size of the smallest blocks in the pool (in bytes).
4050 * @param maxsz Size of the largest blocks in the pool (in bytes).
4051 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004052 * @param align Alignment of the pool's buffer (power of 2).
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004053 * @req K-MPOOL-001
Peter Mitsis2a2b0752016-10-06 16:27:01 -04004054 */
Andy Ross73cb9582017-05-09 10:42:39 -07004055#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
4056 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
4057 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08004058 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07004059 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08004060 .base = { \
4061 .buf = _mpool_buf_##name, \
4062 .max_sz = maxsz, \
4063 .n_max = nmax, \
4064 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
4065 .levels = _mpool_lvls_##name, \
4066 .flags = SYS_MEM_POOL_KERNEL \
4067 } \
Andy Ross73cb9582017-05-09 10:42:39 -07004068 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004069
Peter Mitsis937042c2016-10-13 13:18:26 -04004070/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004071 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004072 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004073 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004075 * @param pool Address of the memory pool.
4076 * @param block Pointer to block descriptor for the allocated memory.
4077 * @param size Amount of memory to allocate (in bytes).
4078 * @param timeout Maximum time to wait for operation to complete
4079 * (in milliseconds). Use K_NO_WAIT to return without waiting,
4080 * or K_FOREVER to wait as long as necessary.
4081 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05004082 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004083 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05004084 * @retval -ENOMEM Returned without waiting.
4085 * @retval -EAGAIN Waiting period timed out.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004086 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004087 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04004088extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05004089 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04004090
4091/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07004092 * @brief Allocate memory from a memory pool with malloc() semantics
4093 *
4094 * Such memory must be released using k_free().
4095 *
4096 * @param pool Address of the memory pool.
4097 * @param size Amount of memory to allocate (in bytes).
4098 * @return Address of the allocated memory if successful, otherwise NULL
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004099 * @req K-MPOOL-002
Andrew Boiea2480bd2018-04-12 16:59:02 -07004100 */
4101extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
4102
4103/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004104 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004105 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004106 * This routine releases a previously allocated memory block back to its
4107 * memory pool.
4108 *
4109 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004110 *
4111 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004112 * @req K-MPOOL-002
Peter Mitsis937042c2016-10-13 13:18:26 -04004113 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004114extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04004115
4116/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004117 * @brief Free memory allocated from a memory pool.
4118 *
4119 * This routine releases a previously allocated memory block back to its
4120 * memory pool
4121 *
4122 * @param id Memory block identifier.
4123 *
4124 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004125 * @req K-MPOOL-002
Johan Hedberg7d887cb2018-01-11 20:45:27 +02004126 */
4127extern void k_mem_pool_free_id(struct k_mem_block_id *id);
4128
4129/**
Anas Nashif166f5192018-02-25 08:02:36 -06004130 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05004131 */
4132
4133/**
4134 * @defgroup heap_apis Heap Memory Pool APIs
4135 * @ingroup kernel_apis
4136 * @{
4137 */
4138
4139/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004140 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04004141 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004142 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05004143 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04004144 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004145 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04004146 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004147 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004148 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004149 */
Peter Mitsis5f399242016-10-13 13:26:25 -04004150extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04004151
4152/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004153 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05004154 *
4155 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07004156 * returned must have been allocated from the heap memory pool or
4157 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04004158 *
Anas Nashif345fdd52016-12-20 08:36:04 -05004159 * If @a ptr is NULL, no operation is performed.
4160 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05004161 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04004162 *
4163 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004164 * @req K-HEAP-001
Peter Mitsis937042c2016-10-13 13:18:26 -04004165 */
4166extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004167
Allan Stephensc98da842016-11-11 15:45:03 -05004168/**
Andrew Boie7f95e832017-11-08 14:40:01 -08004169 * @brief Allocate memory from heap, array style
4170 *
4171 * This routine provides traditional calloc() semantics. Memory is
4172 * allocated from the heap memory pool and zeroed.
4173 *
4174 * @param nmemb Number of elements in the requested array
4175 * @param size Size of each array element (in bytes).
4176 *
4177 * @return Address of the allocated memory if successful; otherwise NULL.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004178 * @req K-HEAP-001
Andrew Boie7f95e832017-11-08 14:40:01 -08004179 */
4180extern void *k_calloc(size_t nmemb, size_t size);
4181
Anas Nashif166f5192018-02-25 08:02:36 -06004182/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05004183
Benjamin Walshacc68c12017-01-29 18:57:45 -05004184/* polling API - PRIVATE */
4185
Benjamin Walshb0179862017-02-02 16:39:57 -05004186#ifdef CONFIG_POLL
4187#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
4188#else
4189#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
4190#endif
4191
Benjamin Walshacc68c12017-01-29 18:57:45 -05004192/* private - implementation data created as needed, per-type */
4193struct _poller {
4194 struct k_thread *thread;
Andy Ross55a7e462018-05-31 11:58:09 -07004195 volatile int is_polling;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004196};
4197
4198/* private - types bit positions */
4199enum _poll_types_bits {
4200 /* can be used to ignore an event */
4201 _POLL_TYPE_IGNORE,
4202
4203 /* to be signaled by k_poll_signal() */
4204 _POLL_TYPE_SIGNAL,
4205
4206 /* semaphore availability */
4207 _POLL_TYPE_SEM_AVAILABLE,
4208
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004209 /* queue/fifo/lifo data availability */
4210 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004211
4212 _POLL_NUM_TYPES
4213};
4214
4215#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
4216
4217/* private - states bit positions */
4218enum _poll_states_bits {
4219 /* default state when creating event */
4220 _POLL_STATE_NOT_READY,
4221
Benjamin Walshacc68c12017-01-29 18:57:45 -05004222 /* signaled by k_poll_signal() */
4223 _POLL_STATE_SIGNALED,
4224
4225 /* semaphore is available */
4226 _POLL_STATE_SEM_AVAILABLE,
4227
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004228 /* data is available to read on queue/fifo/lifo */
4229 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004230
4231 _POLL_NUM_STATES
4232};
4233
4234#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
4235
4236#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004237 (32 - (0 \
4238 + 8 /* tag */ \
4239 + _POLL_NUM_TYPES \
4240 + _POLL_NUM_STATES \
4241 + 1 /* modes */ \
4242 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05004243
Benjamin Walshacc68c12017-01-29 18:57:45 -05004244/* end of polling API - PRIVATE */
4245
4246
4247/**
4248 * @defgroup poll_apis Async polling APIs
4249 * @ingroup kernel_apis
4250 * @{
4251 */
4252
4253/* Public polling API */
4254
4255/* public - values for k_poll_event.type bitfield */
4256#define K_POLL_TYPE_IGNORE 0
4257#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
4258#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004259#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
4260#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004261
4262/* public - polling modes */
4263enum k_poll_modes {
4264 /* polling thread does not take ownership of objects when available */
4265 K_POLL_MODE_NOTIFY_ONLY = 0,
4266
4267 K_POLL_NUM_MODES
4268};
4269
4270/* public - values for k_poll_event.state bitfield */
4271#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05004272#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
4273#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02004274#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
4275#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05004276
4277/* public - poll signal object */
4278struct k_poll_signal {
4279 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004280 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004281
4282 /*
4283 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
4284 * user resets it to 0.
4285 */
4286 unsigned int signaled;
4287
4288 /* custom result value passed to k_poll_signal() if needed */
4289 int result;
4290};
4291
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004292#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004293 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004294 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004295 .signaled = 0, \
4296 .result = 0, \
4297 }
4298
4299struct k_poll_event {
4300 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004301 sys_dnode_t _node;
4302
4303 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004304 struct _poller *poller;
4305
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004306 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004307 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004308
Benjamin Walshacc68c12017-01-29 18:57:45 -05004309 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004310 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004311
4312 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004313 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004314
4315 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004316 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004317
4318 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004319 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004320
4321 /* per-type data */
4322 union {
4323 void *obj;
4324 struct k_poll_signal *signal;
4325 struct k_sem *sem;
4326 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004327 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004328 };
4329};
4330
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004331#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004332 { \
4333 .poller = NULL, \
4334 .type = event_type, \
4335 .state = K_POLL_STATE_NOT_READY, \
4336 .mode = event_mode, \
4337 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004338 { .obj = event_obj }, \
4339 }
4340
4341#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4342 event_tag) \
4343 { \
4344 .type = event_type, \
4345 .tag = event_tag, \
4346 .state = K_POLL_STATE_NOT_READY, \
4347 .mode = event_mode, \
4348 .unused = 0, \
4349 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004350 }
4351
4352/**
4353 * @brief Initialize one struct k_poll_event instance
4354 *
4355 * After this routine is called on a poll event, the event it ready to be
4356 * placed in an event array to be passed to k_poll().
4357 *
4358 * @param event The event to initialize.
4359 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4360 * values. Only values that apply to the same object being polled
4361 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4362 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004363 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004364 * @param obj Kernel object or poll signal.
4365 *
4366 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004367 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004368 */
4369
Kumar Galacc334c72017-04-21 10:55:34 -05004370extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004371 int mode, void *obj);
4372
4373/**
4374 * @brief Wait for one or many of multiple poll events to occur
4375 *
4376 * This routine allows a thread to wait concurrently for one or many of
4377 * multiple poll events to have occurred. Such events can be a kernel object
4378 * being available, like a semaphore, or a poll signal event.
4379 *
4380 * When an event notifies that a kernel object is available, the kernel object
4381 * is not "given" to the thread calling k_poll(): it merely signals the fact
4382 * that the object was available when the k_poll() call was in effect. Also,
4383 * all threads trying to acquire an object the regular way, i.e. by pending on
4384 * the object, have precedence over the thread polling on the object. This
4385 * means that the polling thread will never get the poll event on an object
4386 * until the object becomes available and its pend queue is empty. For this
4387 * reason, the k_poll() call is more effective when the objects being polled
4388 * only have one thread, the polling thread, trying to acquire them.
4389 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004390 * When k_poll() returns 0, the caller should loop on all the events that were
4391 * passed to k_poll() and check the state field for the values that were
4392 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004393 *
4394 * Before being reused for another call to k_poll(), the user has to reset the
4395 * state field to K_POLL_STATE_NOT_READY.
4396 *
Andrew Boie3772f772018-05-07 16:52:57 -07004397 * When called from user mode, a temporary memory allocation is required from
4398 * the caller's resource pool.
4399 *
Benjamin Walshacc68c12017-01-29 18:57:45 -05004400 * @param events An array of pointers to events to be polled for.
4401 * @param num_events The number of events in the array.
4402 * @param timeout Waiting period for an event to be ready (in milliseconds),
4403 * or one of the special values K_NO_WAIT and K_FOREVER.
4404 *
4405 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004406 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02004407 * @retval -EINTR Poller thread has been interrupted.
Andrew Boie3772f772018-05-07 16:52:57 -07004408 * @retval -ENOMEM Thread resource pool insufficient memory (user mode only)
4409 * @retval -EINVAL Bad parameters (user mode only)
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004410 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004411 */
4412
Andrew Boie3772f772018-05-07 16:52:57 -07004413__syscall int k_poll(struct k_poll_event *events, int num_events,
4414 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004415
4416/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004417 * @brief Initialize a poll signal object.
4418 *
4419 * Ready a poll signal object to be signaled via k_poll_signal().
4420 *
4421 * @param signal A poll signal.
4422 *
4423 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004424 * @req K-POLL-001
Benjamin Walsha304f162017-02-02 16:46:09 -05004425 */
4426
Andrew Boie3772f772018-05-07 16:52:57 -07004427__syscall void k_poll_signal_init(struct k_poll_signal *signal);
4428
4429/*
4430 * @brief Reset a poll signal object's state to unsignaled.
4431 *
4432 * @param signal A poll signal object
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004433 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004434 */
4435__syscall void k_poll_signal_reset(struct k_poll_signal *signal);
4436
4437static inline void _impl_k_poll_signal_reset(struct k_poll_signal *signal)
4438{
4439 signal->signaled = 0;
4440}
4441
4442/**
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004443 * @brief Fetch the signaled state and result value of a poll signal
Andrew Boie3772f772018-05-07 16:52:57 -07004444 *
4445 * @param signal A poll signal object
4446 * @param signaled An integer buffer which will be written nonzero if the
4447 * object was signaled
4448 * @param result An integer destination buffer which will be written with the
David B. Kinderfcbd8fb2018-05-23 12:06:24 -07004449 * result value if the object was signaled, or an undefined
Andrew Boie3772f772018-05-07 16:52:57 -07004450 * value if it was not.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004451 * @req K-POLL-001
Andrew Boie3772f772018-05-07 16:52:57 -07004452 */
4453__syscall void k_poll_signal_check(struct k_poll_signal *signal,
4454 unsigned int *signaled, int *result);
Benjamin Walsha304f162017-02-02 16:46:09 -05004455
4456/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004457 * @brief Signal a poll signal object.
4458 *
4459 * This routine makes ready a poll signal, which is basically a poll event of
4460 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4461 * made ready to run. A @a result value can be specified.
4462 *
4463 * The poll signal contains a 'signaled' field that, when set by
Andrew Boie3772f772018-05-07 16:52:57 -07004464 * k_poll_signal(), stays set until the user sets it back to 0 with
4465 * k_poll_signal_reset(). It thus has to be reset by the user before being
4466 * passed again to k_poll() or k_poll() will consider it being signaled, and
4467 * will return immediately.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004468 *
4469 * @param signal A poll signal.
4470 * @param result The value to store in the result field of the signal.
4471 *
4472 * @retval 0 The signal was delivered successfully.
4473 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004474 * @req K-POLL-001
Benjamin Walshacc68c12017-01-29 18:57:45 -05004475 */
4476
Andrew Boie3772f772018-05-07 16:52:57 -07004477__syscall int k_poll_signal(struct k_poll_signal *signal, int result);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004478
Anas Nashif954d5502018-02-25 08:37:28 -06004479/**
4480 * @internal
4481 */
Andy Ross8606fab2018-03-26 10:54:40 -07004482extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004483
Anas Nashif166f5192018-02-25 08:02:36 -06004484/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004485
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004486/**
4487 * @brief Make the CPU idle.
4488 *
4489 * This function makes the CPU idle until an event wakes it up.
4490 *
4491 * In a regular system, the idle thread should be the only thread responsible
4492 * for making the CPU idle and triggering any type of power management.
4493 * However, in some more constrained systems, such as a single-threaded system,
4494 * the only thread would be responsible for this if needed.
4495 *
4496 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004497 * @req K-MISC-001
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004498 */
4499extern void k_cpu_idle(void);
4500
4501/**
4502 * @brief Make the CPU idle in an atomic fashion.
4503 *
4504 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4505 * must be done atomically before making the CPU idle.
4506 *
4507 * @param key Interrupt locking key obtained from irq_lock().
4508 *
4509 * @return N/A
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004510 * @req K-MISC-002
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004511 */
4512extern void k_cpu_atomic_idle(unsigned int key);
4513
Anas Nashif954d5502018-02-25 08:37:28 -06004514
4515/**
4516 * @internal
4517 */
Kumar Galacc334c72017-04-21 10:55:34 -05004518extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004519
Andrew Boiecdb94d62017-04-18 15:22:05 -07004520#ifdef _ARCH_EXCEPT
4521/* This archtecture has direct support for triggering a CPU exception */
4522#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4523#else
4524
Andrew Boiecdb94d62017-04-18 15:22:05 -07004525/* NOTE: This is the implementation for arches that do not implement
4526 * _ARCH_EXCEPT() to generate a real CPU exception.
4527 *
4528 * We won't have a real exception frame to determine the PC value when
4529 * the oops occurred, so print file and line number before we jump into
4530 * the fatal error handler.
4531 */
4532#define _k_except_reason(reason) do { \
4533 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4534 _NanoFatalErrorHandler(reason, &_default_esf); \
4535 CODE_UNREACHABLE; \
4536 } while (0)
4537
4538#endif /* _ARCH__EXCEPT */
4539
4540/**
4541 * @brief Fatally terminate a thread
4542 *
4543 * This should be called when a thread has encountered an unrecoverable
4544 * runtime condition and needs to terminate. What this ultimately
4545 * means is determined by the _fatal_error_handler() implementation, which
4546 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4547 *
4548 * If this is called from ISR context, the default system fatal error handler
4549 * will treat it as an unrecoverable system error, just like k_panic().
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004550 * @req K-MISC-003
Andrew Boiecdb94d62017-04-18 15:22:05 -07004551 */
4552#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4553
4554/**
4555 * @brief Fatally terminate the system
4556 *
4557 * This should be called when the Zephyr kernel has encountered an
4558 * unrecoverable runtime condition and needs to terminate. What this ultimately
4559 * means is determined by the _fatal_error_handler() implementation, which
4560 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004561 * @req K-MISC-004
Andrew Boiecdb94d62017-04-18 15:22:05 -07004562 */
4563#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4564
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004565/*
4566 * private APIs that are utilized by one or more public APIs
4567 */
4568
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004569#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004570/**
4571 * @internal
4572 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004573extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004574#else
Anas Nashif954d5502018-02-25 08:37:28 -06004575/**
4576 * @internal
4577 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004578#define _init_static_threads() do { } while ((0))
4579#endif
4580
Anas Nashif954d5502018-02-25 08:37:28 -06004581/**
4582 * @internal
4583 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004584extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004585/**
4586 * @internal
4587 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004588extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004589
Andrew Boiedc5d9352017-06-02 12:56:47 -07004590/* arch/cpu.h may declare an architecture or platform-specific macro
4591 * for properly declaring stacks, compatible with MMU/MPU constraints if
4592 * enabled
4593 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004594
4595/**
4596 * @brief Obtain an extern reference to a stack
4597 *
4598 * This macro properly brings the symbol of a thread stack declared
4599 * elsewhere into scope.
4600 *
4601 * @param sym Thread stack symbol name
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004602 * @req K-MISC-005
Andrew Boiec5c104f2017-10-16 14:46:34 -07004603 */
4604#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4605
Andrew Boiedc5d9352017-06-02 12:56:47 -07004606#ifdef _ARCH_THREAD_STACK_DEFINE
4607#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4608#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4609 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304610#define K_THREAD_STACK_LEN(size) _ARCH_THREAD_STACK_LEN(size)
Andrew Boiedc5d9352017-06-02 12:56:47 -07004611#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4612#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004613static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004614{
4615 return _ARCH_THREAD_STACK_BUFFER(sym);
4616}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004617#else
4618/**
4619 * @brief Declare a toplevel thread stack memory region
4620 *
4621 * This declares a region of memory suitable for use as a thread's stack.
4622 *
4623 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4624 * 'noinit' section so that it isn't zeroed at boot
4625 *
Andrew Boie507852a2017-07-25 18:47:07 -07004626 * The declared symbol will always be a k_thread_stack_t which can be passed to
Anas Nashif47420d02018-05-24 14:20:56 -04004627 * k_thread_create(), but should otherwise not be manipulated. If the buffer
Andrew Boie507852a2017-07-25 18:47:07 -07004628 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004629 *
4630 * It is legal to precede this definition with the 'static' keyword.
4631 *
4632 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4633 * parameter of k_thread_create(), it may not be the same as the
4634 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4635 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004636 * Some arches may round the size of the usable stack region up to satisfy
4637 * alignment constraints. K_THREAD_STACK_SIZEOF() will return the aligned
4638 * size.
4639 *
Andrew Boiedc5d9352017-06-02 12:56:47 -07004640 * @param sym Thread stack symbol name
4641 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004642 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004643 */
4644#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004645 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004646
4647/**
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304648 * @brief Calculate size of stacks to be allocated in a stack array
4649 *
4650 * This macro calculates the size to be allocated for the stacks
4651 * inside a stack array. It accepts the indicated "size" as a parameter
4652 * and if required, pads some extra bytes (e.g. for MPU scenarios). Refer
4653 * K_THREAD_STACK_ARRAY_DEFINE definition to see how this is used.
4654 *
4655 * @param size Size of the stack memory region
4656 * @req K-TSTACK-001
4657 */
4658#define K_THREAD_STACK_LEN(size) (size)
4659
4660/**
Andrew Boiedc5d9352017-06-02 12:56:47 -07004661 * @brief Declare a toplevel array of thread stack memory regions
4662 *
4663 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4664 * definition for additional details and constraints.
4665 *
4666 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4667 * 'noinit' section so that it isn't zeroed at boot
4668 *
4669 * @param sym Thread stack symbol name
4670 * @param nmemb Number of stacks to declare
4671 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004672 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004673 */
Andrew Boiedc5d9352017-06-02 12:56:47 -07004674#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004675 struct _k_thread_stack_element __noinit \
Rajavardhan Gundid4dd9282018-06-27 13:26:20 +05304676 __aligned(STACK_ALIGN) sym[nmemb][K_THREAD_STACK_LEN(size)]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004677
4678/**
4679 * @brief Declare an embedded stack memory region
4680 *
4681 * Used for stacks embedded within other data structures. Use is highly
4682 * discouraged but in some cases necessary. For memory protection scenarios,
4683 * it is very important that any RAM preceding this member not be writable
4684 * by threads else a stack overflow will lead to silent corruption. In other
4685 * words, the containing data structure should live in RAM owned by the kernel.
4686 *
4687 * @param sym Thread stack symbol name
4688 * @param size Size of the stack memory region
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004689 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004690 */
4691#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004692 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004693
4694/**
4695 * @brief Return the size in bytes of a stack memory region
4696 *
4697 * Convenience macro for passing the desired stack size to k_thread_create()
4698 * since the underlying implementation may actually create something larger
4699 * (for instance a guard area).
4700 *
Andrew Boiee2d77912018-05-30 09:45:35 -07004701 * The value returned here is not guaranteed to match the 'size' parameter
4702 * passed to K_THREAD_STACK_DEFINE and may be larger.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004703 *
4704 * @param sym Stack memory symbol
4705 * @return Size of the stack
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004706 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004707 */
4708#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4709
4710/**
4711 * @brief Get a pointer to the physical stack buffer
4712 *
4713 * Convenience macro to get at the real underlying stack buffer used by
4714 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4715 * This is really only intended for diagnostic tools which want to examine
4716 * stack memory contents.
4717 *
4718 * @param sym Declared stack symbol name
4719 * @return The buffer itself, a char *
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004720 * @req K-TSTACK-001
Andrew Boiedc5d9352017-06-02 12:56:47 -07004721 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004722static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004723{
4724 return (char *)sym;
4725}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004726
4727#endif /* _ARCH_DECLARE_STACK */
4728
Chunlin Hane9c97022017-07-07 20:29:30 +08004729/**
4730 * @defgroup mem_domain_apis Memory domain APIs
4731 * @ingroup kernel_apis
4732 * @{
4733 */
4734
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004735/**
4736 * @def MEM_PARTITION_ENTRY
4737 * @brief Used to declare a memory partition entry
4738 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004739 */
4740#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4741 {\
4742 .start = _start, \
4743 .size = _size, \
4744 .attr = _attr, \
4745 }
4746
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004747/**
4748 * @def K_MEM_PARTITION_DEFINE
4749 * @brief Used to declare a memory partition
4750 * @req K-MP-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004751 */
4752#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4753#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4754 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304755 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004756 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4757#else
4758#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304759 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004760 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4761#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4762
Chunlin Hane9c97022017-07-07 20:29:30 +08004763/* memory partition */
4764struct k_mem_partition {
4765 /* start address of memory partition */
4766 u32_t start;
4767 /* size of memory partition */
4768 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304769#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004770 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304771 k_mem_partition_attr_t attr;
4772#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004773};
4774
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304775/* memory domian
4776 * Note: Always declare this structure with __kernel prefix
4777 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004778struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304779#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004780 /* partitions in the domain */
4781 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304782#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004783 /* domain q */
4784 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004785 /* number of partitions in the domain */
4786 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004787};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304788
Chunlin Hane9c97022017-07-07 20:29:30 +08004789
4790/**
4791 * @brief Initialize a memory domain.
4792 *
4793 * Initialize a memory domain with given name and memory partitions.
4794 *
4795 * @param domain The memory domain to be initialized.
4796 * @param num_parts The number of array items of "parts" parameter.
4797 * @param parts An array of pointers to the memory partitions. Can be NULL
4798 * if num_parts is zero.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004799 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004800 */
Leandro Pereira08de6582018-02-28 14:22:57 -08004801extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004802 struct k_mem_partition *parts[]);
4803/**
4804 * @brief Destroy a memory domain.
4805 *
4806 * Destroy a memory domain.
4807 *
4808 * @param domain The memory domain to be destroyed.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004809 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004810 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004811extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4812
4813/**
4814 * @brief Add a memory partition into a memory domain.
4815 *
4816 * Add a memory partition into a memory domain.
4817 *
4818 * @param domain The memory domain to be added a memory partition.
4819 * @param part The memory partition to be added
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004820 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004821 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004822extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4823 struct k_mem_partition *part);
4824
4825/**
4826 * @brief Remove a memory partition from a memory domain.
4827 *
4828 * Remove a memory partition from a memory domain.
4829 *
4830 * @param domain The memory domain to be removed a memory partition.
4831 * @param part The memory partition to be removed
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004832 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004833 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004834extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4835 struct k_mem_partition *part);
4836
4837/**
4838 * @brief Add a thread into a memory domain.
4839 *
4840 * Add a thread into a memory domain.
4841 *
4842 * @param domain The memory domain that the thread is going to be added into.
4843 * @param thread ID of thread going to be added into the memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004844 *
4845 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004846 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004847extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4848 k_tid_t thread);
4849
4850/**
4851 * @brief Remove a thread from its memory domain.
4852 *
4853 * Remove a thread from its memory domain.
4854 *
4855 * @param thread ID of thread going to be removed from its memory domain.
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004856 * @req K-MD-001
Chunlin Hane9c97022017-07-07 20:29:30 +08004857 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004858extern void k_mem_domain_remove_thread(k_tid_t thread);
4859
Anas Nashif166f5192018-02-25 08:02:36 -06004860/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004861
Andrew Boie756f9072017-10-10 16:01:49 -07004862/**
4863 * @brief Emit a character buffer to the console device
4864 *
4865 * @param c String of characters to print
4866 * @param n The length of the string
Anas Nashifc8e0d0c2018-05-21 11:09:59 -04004867 *
4868 * @req K-MISC-006
Andrew Boie756f9072017-10-10 16:01:49 -07004869 */
4870__syscall void k_str_out(char *c, size_t n);
4871
Andy Rosse7172672018-01-24 15:48:32 -08004872/**
4873 * @brief Start a numbered CPU on a MP-capable system
4874
4875 * This starts and initializes a specific CPU. The main thread on
4876 * startup is running on CPU zero, other processors are numbered
4877 * sequentially. On return from this function, the CPU is known to
4878 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004879 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004880 * with the provided key will work to enable them.
4881 *
4882 * Normally, in SMP mode this function will be called by the kernel
4883 * initialization and should not be used as a user API. But it is
4884 * defined here for special-purpose apps which want Zephyr running on
4885 * one core and to use others for design-specific processing.
4886 *
4887 * @param cpu_num Integer number of the CPU
4888 * @param stack Stack memory for the CPU
4889 * @param sz Stack buffer size, in bytes
4890 * @param fn Function to begin running on the CPU. First argument is
4891 * an irq_unlock() key.
4892 * @param arg Untyped argument to be passed to "fn"
4893 */
4894extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4895 void (*fn)(int, void *), void *arg);
4896
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004897#ifdef __cplusplus
4898}
4899#endif
4900
Andrew Boiee004dec2016-11-07 09:01:19 -08004901#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4902/*
4903 * Define new and delete operators.
4904 * At this moment, the operators do nothing since objects are supposed
4905 * to be statically allocated.
4906 */
4907inline void operator delete(void *ptr)
4908{
4909 (void)ptr;
4910}
4911
4912inline void operator delete[](void *ptr)
4913{
4914 (void)ptr;
4915}
4916
4917inline void *operator new(size_t size)
4918{
4919 (void)size;
4920 return NULL;
4921}
4922
4923inline void *operator new[](size_t size)
4924{
4925 (void)size;
4926 return NULL;
4927}
4928
4929/* Placement versions of operator new and delete */
4930inline void operator delete(void *ptr1, void *ptr2)
4931{
4932 (void)ptr1;
4933 (void)ptr2;
4934}
4935
4936inline void operator delete[](void *ptr1, void *ptr2)
4937{
4938 (void)ptr1;
4939 (void)ptr2;
4940}
4941
4942inline void *operator new(size_t size, void *ptr)
4943{
4944 (void)size;
4945 return ptr;
4946}
4947
4948inline void *operator new[](size_t size, void *ptr)
4949{
4950 (void)size;
4951 return ptr;
4952}
4953
4954#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4955
Andrew Boiefa94ee72017-09-28 16:54:35 -07004956#include <syscalls/kernel.h>
4957
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004958#endif /* !_ASMLANGUAGE */
4959
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004960#endif /* _kernel__h_ */