blob: 8d4f415b43974a5781a95f44fd79c904327f4a6f [file] [log] [blame]
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001/*
2 * Copyright (c) 2016, Wind River Systems, Inc.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Benjamin Walsh456c6da2016-09-02 18:55:39 -04005 */
6
7/**
8 * @file
9 *
10 * @brief Public kernel APIs.
11 */
12
13#ifndef _kernel__h_
14#define _kernel__h_
15
Benjamin Walshdfa7ce52017-01-22 17:06:05 -050016#if !defined(_ASMLANGUAGE)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040017#include <stddef.h>
Kumar Gala78908162017-04-19 10:32:08 -050018#include <zephyr/types.h>
Anas Nashif173902f2017-01-17 07:08:56 -050019#include <limits.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040020#include <toolchain.h>
Anas Nashif397d29d2017-06-17 11:30:47 -040021#include <linker/sections.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040022#include <atomic.h>
23#include <errno.h>
24#include <misc/__assert.h>
25#include <misc/dlist.h>
26#include <misc/slist.h>
Benjamin Walsh62092182016-12-20 14:39:08 -050027#include <misc/util.h>
Andrew Boieaa6de292018-03-06 17:12:37 -080028#include <misc/mempool_base.h>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050029#include <kernel_version.h>
Leandro Pereiraadce1d12017-10-13 15:45:02 -070030#include <random/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070031#include <kernel_arch_thread.h>
Andrew Boie13ca6fe2017-09-23 12:05:49 -070032#include <syscall.h>
Andrew Boie43263fc2017-11-02 11:07:31 -070033#include <misc/printk.h>
34#include <arch/cpu.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040035
36#ifdef __cplusplus
37extern "C" {
38#endif
39
Anas Nashifbbb157d2017-01-15 08:46:31 -050040/**
41 * @brief Kernel APIs
42 * @defgroup kernel_apis Kernel APIs
43 * @{
44 * @}
45 */
46
Anas Nashif61f4b242016-11-18 10:53:59 -050047#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040048#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
49#else
50#define K_DEBUG(fmt, ...)
51#endif
52
Benjamin Walsh2f280412017-01-14 19:23:46 -050053#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
54#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
55#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
56#elif defined(CONFIG_COOP_ENABLED)
57#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
58#define _NUM_PREEMPT_PRIO (0)
59#elif defined(CONFIG_PREEMPT_ENABLED)
60#define _NUM_COOP_PRIO (0)
61#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
62#else
63#error "invalid configuration"
64#endif
65
66#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040067#define K_PRIO_PREEMPT(x) (x)
68
Benjamin Walsh456c6da2016-09-02 18:55:39 -040069#define K_ANY NULL
70#define K_END NULL
71
Benjamin Walshedb35702017-01-14 18:47:22 -050072#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040073#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050074#elif defined(CONFIG_COOP_ENABLED)
75#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
76#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040077#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050078#else
79#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040080#endif
81
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050082#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040083#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
84#else
85#define K_LOWEST_THREAD_PRIO -1
86#endif
87
Benjamin Walshfab8d922016-11-08 15:36:36 -050088#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
89
Benjamin Walsh456c6da2016-09-02 18:55:39 -040090#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
91#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
92
93typedef sys_dlist_t _wait_q_t;
94
Anas Nashif2f203c22016-12-18 06:57:45 -050095#ifdef CONFIG_OBJECT_TRACING
96#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
97#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040098#else
Anas Nashif2f203c22016-12-18 06:57:45 -050099#define _OBJECT_TRACING_INIT
100#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400101#endif
102
Benjamin Walshacc68c12017-01-29 18:57:45 -0500103#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300104#define _POLL_EVENT_OBJ_INIT(obj) \
105 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
106#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500107#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300108#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500109#define _POLL_EVENT
110#endif
111
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500112struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400113struct k_mutex;
114struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400115struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400116struct k_msgq;
117struct k_mbox;
118struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200119struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400120struct k_fifo;
121struct k_lifo;
122struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400123struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400124struct k_mem_pool;
125struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500126struct k_poll_event;
127struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800128struct k_mem_domain;
129struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400130
Andrew Boie5bd891d2017-09-27 12:59:28 -0700131/* This enumeration needs to be kept in sync with the lists of kernel objects
132 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
133 * function in kernel/userspace.c
134 */
Andrew Boie945af952017-08-22 13:15:23 -0700135enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700136 K_OBJ_ANY,
137
Leandro Pereirac2003672018-04-04 13:50:32 -0700138 /** @cond
139 * Doxygen should ignore this build-time generated include file
140 * when genrating API documentation. Enumeration values are
141 * generated during build by gen_kobject_list.py. It includes
142 * basic kernel objects (e.g. pipes and mutexes) and driver types.
143 */
144#include <kobj-types-enum.h>
145 /** @endcond
146 */
Andrew Boie5bd891d2017-09-27 12:59:28 -0700147
Andrew Boie945af952017-08-22 13:15:23 -0700148 K_OBJ_LAST
149};
150
151#ifdef CONFIG_USERSPACE
152/* Table generated by gperf, these objects are retrieved via
153 * _k_object_find() */
154struct _k_object {
155 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700156 u8_t perms[CONFIG_MAX_THREAD_BYTES];
157 u8_t type;
158 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700159 u32_t data;
Andrew Boie945af952017-08-22 13:15:23 -0700160} __packed;
161
Andrew Boie877f82e2017-10-17 11:20:22 -0700162struct _k_object_assignment {
163 struct k_thread *thread;
164 void * const *objects;
165};
166
167/**
168 * @brief Grant a static thread access to a list of kernel objects
169 *
170 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
171 * a set of kernel objects. These objects do not need to be in an initialized
172 * state. The permissions will be granted when the threads are initialized
173 * in the early boot sequence.
174 *
175 * All arguments beyond the first must be pointers to kernel objects.
176 *
177 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
178 */
179#define K_THREAD_ACCESS_GRANT(name_, ...) \
180 static void * const _CONCAT(_object_list_, name_)[] = \
181 { __VA_ARGS__, NULL }; \
182 static __used __in_section_unique(object_access) \
183 const struct _k_object_assignment \
184 _CONCAT(_object_access_, name_) = \
185 { (&_k_thread_obj_ ## name_), \
186 (_CONCAT(_object_list_, name_)) }
187
Andrew Boie945af952017-08-22 13:15:23 -0700188#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700189#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie97bf0012018-04-24 17:01:37 -0700190#define K_OBJ_FLAG_ALLOC BIT(2)
Andrew Boie945af952017-08-22 13:15:23 -0700191
192/**
193 * Lookup a kernel object and init its metadata if it exists
194 *
195 * Calling this on an object will make it usable from userspace.
196 * Intended to be called as the last statement in kernel object init
197 * functions.
198 *
199 * @param object Address of the kernel object
200 */
201void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700202#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700203
204#define K_THREAD_ACCESS_GRANT(thread, ...)
205
Anas Nashif954d5502018-02-25 08:37:28 -0600206/**
207 * @internal
208 */
Andrew Boie743e4682017-10-04 12:25:50 -0700209static inline void _k_object_init(void *obj)
210{
211 ARG_UNUSED(obj);
212}
213
Anas Nashif954d5502018-02-25 08:37:28 -0600214/**
215 * @internal
216 */
Andrew Boie743e4682017-10-04 12:25:50 -0700217static inline void _impl_k_object_access_grant(void *object,
218 struct k_thread *thread)
219{
220 ARG_UNUSED(object);
221 ARG_UNUSED(thread);
222}
223
Anas Nashif954d5502018-02-25 08:37:28 -0600224/**
225 * @internal
226 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700227static inline void k_object_access_revoke(void *object,
228 struct k_thread *thread)
Andrew Boiea89bf012017-10-09 14:47:55 -0700229{
230 ARG_UNUSED(object);
231 ARG_UNUSED(thread);
232}
233
Andrew Boiee9cfc542018-04-13 13:15:28 -0700234/**
235 * @internal
236 */
237static inline void _impl_k_object_release(void *object)
238{
239 ARG_UNUSED(object);
240}
241
Andrew Boie41bab6e2017-10-14 14:42:23 -0700242static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700243{
244 ARG_UNUSED(object);
245}
246#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700247
248/**
249 * grant a thread access to a kernel object
250 *
251 * The thread will be granted access to the object if the caller is from
252 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700253 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700254 *
255 * @param object Address of kernel object
256 * @param thread Thread to grant access to the object
257 */
Andrew Boie743e4682017-10-04 12:25:50 -0700258__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700259
Andrew Boiea89bf012017-10-09 14:47:55 -0700260/**
261 * grant a thread access to a kernel object
262 *
263 * The thread will lose access to the object if the caller is from
264 * supervisor mode, or the caller is from user mode AND has permissions
265 * on both the object and the thread whose access is being revoked.
266 *
267 * @param object Address of kernel object
268 * @param thread Thread to remove access to the object
269 */
Andrew Boiee9cfc542018-04-13 13:15:28 -0700270void k_object_access_revoke(void *object, struct k_thread *thread);
271
272
273__syscall void k_object_release(void *object);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700274
275/**
276 * grant all present and future threads access to an object
277 *
278 * If the caller is from supervisor mode, or the caller is from user mode and
279 * have sufficient permissions on the object, then that object will have
280 * permissions granted to it for *all* current and future threads running in
281 * the system, effectively becoming a public kernel object.
282 *
283 * Use of this API should be avoided on systems that are running untrusted code
284 * as it is possible for such code to derive the addresses of kernel objects
285 * and perform unwanted operations on them.
286 *
Andrew Boie04caa672017-10-13 13:57:07 -0700287 * It is not possible to revoke permissions on public objects; once public,
288 * any thread may use it.
289 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700290 * @param object Address of kernel object
291 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700292void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700293
Andrew Boie31bdfc02017-11-08 16:38:03 -0800294/**
295 * Allocate a kernel object of a designated type
296 *
297 * This will instantiate at runtime a kernel object of the specified type,
298 * returning a pointer to it. The object will be returned in an uninitialized
299 * state, with the calling thread being granted permission on it. The memory
Andrew Boie97bf0012018-04-24 17:01:37 -0700300 * for the object will be allocated out of the calling thread's resource pool.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800301 *
302 * Currently, allocation of thread stacks is not supported.
303 *
304 * @param otype Requested kernel object type
305 * @return A pointer to the allocated kernel object, or NULL if memory wasn't
306 * available
307 */
Andrew Boie97bf0012018-04-24 17:01:37 -0700308__syscall void *k_object_alloc(enum k_objects otype);
Andrew Boie31bdfc02017-11-08 16:38:03 -0800309
Andrew Boie97bf0012018-04-24 17:01:37 -0700310#ifdef CONFIG_DYNAMIC_OBJECTS
Andrew Boie31bdfc02017-11-08 16:38:03 -0800311/**
312 * Free a kernel object previously allocated with k_object_alloc()
313 *
Andrew Boie97bf0012018-04-24 17:01:37 -0700314 * This will return memory for a kernel object back to resource pool it was
315 * allocated from. Care must be exercised that the object will not be used
316 * during or after when this call is made.
Andrew Boie31bdfc02017-11-08 16:38:03 -0800317 *
318 * @param obj Pointer to the kernel object memory address.
319 */
320void k_object_free(void *obj);
Andrew Boie97bf0012018-04-24 17:01:37 -0700321#else
322static inline void *_impl_k_object_alloc(enum k_objects otype)
323{
Kumar Gala85699f72018-05-17 09:26:03 -0500324 ARG_UNUSED(otype);
325
Andrew Boie97bf0012018-04-24 17:01:37 -0700326 return NULL;
327}
328
329static inline void k_obj_free(void *obj)
330{
331 ARG_UNUSED(obj);
332}
Andrew Boie31bdfc02017-11-08 16:38:03 -0800333#endif /* CONFIG_DYNAMIC_OBJECTS */
334
Andrew Boiebca15da2017-10-15 14:17:48 -0700335/* Using typedef deliberately here, this is quite intended to be an opaque
336 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
337 *
338 * The purpose of this data type is to clearly distinguish between the
339 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
340 * buffer which composes the stack data actually used by the underlying
341 * thread; they cannot be used interchangably as some arches precede the
342 * stack buffer region with guard areas that trigger a MPU or MMU fault
343 * if written to.
344 *
345 * APIs that want to work with the buffer inside should continue to use
346 * char *.
347 *
348 * Stacks should always be created with K_THREAD_STACK_DEFINE().
349 */
350struct __packed _k_thread_stack_element {
351 char data;
352};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700353typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700354
Andrew Boie73abd322017-04-04 13:19:13 -0700355/* timeouts */
356
357struct _timeout;
358typedef void (*_timeout_func_t)(struct _timeout *t);
359
360struct _timeout {
361 sys_dnode_t node;
362 struct k_thread *thread;
363 sys_dlist_t *wait_q;
364 s32_t delta_ticks_from_prev;
365 _timeout_func_t func;
366};
367
368extern s32_t _timeout_remaining_get(struct _timeout *timeout);
369
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700370/**
371 * @typedef k_thread_entry_t
372 * @brief Thread entry point function type.
373 *
374 * A thread's entry point function is invoked when the thread starts executing.
375 * Up to 3 argument values can be passed to the function.
376 *
377 * The thread terminates execution permanently if the entry point function
378 * returns. The thread is responsible for releasing any shared resources
379 * it may own (such as mutexes and dynamically allocated memory), prior to
380 * returning.
381 *
382 * @param p1 First argument.
383 * @param p2 Second argument.
384 * @param p3 Third argument.
385 *
386 * @return N/A
387 */
388typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700389
390#ifdef CONFIG_THREAD_MONITOR
391struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700392 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700393 void *parameter1;
394 void *parameter2;
395 void *parameter3;
396};
397#endif
398
399/* can be used for creating 'dummy' threads, e.g. for pending on objects */
400struct _thread_base {
401
402 /* this thread's entry in a ready/wait queue */
403 sys_dnode_t k_q_node;
404
405 /* user facing 'thread options'; values defined in include/kernel.h */
406 u8_t user_options;
407
408 /* thread state */
409 u8_t thread_state;
410
411 /*
412 * scheduler lock count and thread priority
413 *
414 * These two fields control the preemptibility of a thread.
415 *
416 * When the scheduler is locked, sched_locked is decremented, which
417 * means that the scheduler is locked for values from 0xff to 0x01. A
418 * thread is coop if its prio is negative, thus 0x80 to 0xff when
419 * looked at the value as unsigned.
420 *
421 * By putting them end-to-end, this means that a thread is
422 * non-preemptible if the bundled value is greater than or equal to
423 * 0x0080.
424 */
425 union {
426 struct {
427#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
428 u8_t sched_locked;
429 s8_t prio;
430#else /* LITTLE and PDP */
431 s8_t prio;
432 u8_t sched_locked;
433#endif
434 };
435 u16_t preempt;
436 };
437
Andy Ross2724fd12018-01-29 14:55:20 -0800438#ifdef CONFIG_SMP
439 /* True for the per-CPU idle threads */
440 u8_t is_idle;
441
442 /* Non-zero when actively running on a CPU */
443 u8_t active;
444
445 /* CPU index on which thread was last run */
446 u8_t cpu;
Andy Ross15c40072018-04-12 12:50:05 -0700447
448 /* Recursive count of irq_lock() calls */
449 u8_t global_lock_count;
Andy Ross2724fd12018-01-29 14:55:20 -0800450#endif
451
Andrew Boie73abd322017-04-04 13:19:13 -0700452 /* data returned by APIs */
453 void *swap_data;
454
455#ifdef CONFIG_SYS_CLOCK_EXISTS
456 /* this thread's entry in a timeout queue */
457 struct _timeout timeout;
458#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700459};
460
461typedef struct _thread_base _thread_base_t;
462
463#if defined(CONFIG_THREAD_STACK_INFO)
464/* Contains the stack information of a thread */
465struct _thread_stack_info {
466 /* Stack Start */
467 u32_t start;
468 /* Stack Size */
469 u32_t size;
470};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700471
472typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700473#endif /* CONFIG_THREAD_STACK_INFO */
474
Chunlin Hane9c97022017-07-07 20:29:30 +0800475#if defined(CONFIG_USERSPACE)
476struct _mem_domain_info {
477 /* memory domain queue node */
478 sys_dnode_t mem_domain_q_node;
479 /* memory domain of the thread */
480 struct k_mem_domain *mem_domain;
481};
482
483#endif /* CONFIG_USERSPACE */
484
Andrew Boie73abd322017-04-04 13:19:13 -0700485struct k_thread {
486
487 struct _thread_base base;
488
489 /* defined by the architecture, but all archs need these */
490 struct _caller_saved caller_saved;
491 struct _callee_saved callee_saved;
492
493 /* static thread init data */
494 void *init_data;
495
496 /* abort function */
497 void (*fn_abort)(void);
498
499#if defined(CONFIG_THREAD_MONITOR)
500 /* thread entry and parameters description */
501 struct __thread_entry *entry;
502
503 /* next item in list of all threads */
504 struct k_thread *next_thread;
505#endif
506
507#ifdef CONFIG_THREAD_CUSTOM_DATA
508 /* crude thread-local storage */
509 void *custom_data;
510#endif
511
512#ifdef CONFIG_ERRNO
513 /* per-thread errno variable */
514 int errno_var;
515#endif
516
517#if defined(CONFIG_THREAD_STACK_INFO)
518 /* Stack Info */
519 struct _thread_stack_info stack_info;
520#endif /* CONFIG_THREAD_STACK_INFO */
521
Chunlin Hane9c97022017-07-07 20:29:30 +0800522#if defined(CONFIG_USERSPACE)
523 /* memory domain info of the thread */
524 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700525 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700526 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800527#endif /* CONFIG_USERSPACE */
528
Andy Ross042d8ec2017-12-09 08:37:20 -0800529#if defined(CONFIG_USE_SWITCH)
530 /* When using __switch() a few previously arch-specific items
531 * become part of the core OS
532 */
533
534 /* _Swap() return value */
535 int swap_retval;
536
537 /* Context handle returned via _arch_switch() */
538 void *switch_handle;
539#endif
Andrew Boie92e5bd72018-04-12 17:12:15 -0700540 struct k_mem_pool *resource_pool;
Andy Ross042d8ec2017-12-09 08:37:20 -0800541
Andrew Boie73abd322017-04-04 13:19:13 -0700542 /* arch-specifics: must always be at the end */
543 struct _thread_arch arch;
544};
545
546typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400547typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700548#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400549
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400550enum execution_context_types {
551 K_ISR = 0,
552 K_COOP_THREAD,
553 K_PREEMPT_THREAD,
554};
555
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400556/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100557 * @defgroup profiling_apis Profiling APIs
558 * @ingroup kernel_apis
559 * @{
560 */
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530561typedef void (*k_thread_user_cb_t)(const struct k_thread *thread,
562 void *user_data);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100563
564/**
565 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
566 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700567 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100568 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
569 *
570 * CONFIG_MAIN_STACK_SIZE
571 * CONFIG_IDLE_STACK_SIZE
572 * CONFIG_ISR_STACK_SIZE
573 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
574 *
575 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
576 * produce output.
577 *
578 * @return N/A
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530579 *
580 * @deprecated This API is deprecated. Use k_thread_foreach().
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100581 */
Ramakrishna Pallala149a3292018-05-09 00:38:33 +0530582__deprecated extern void k_call_stacks_analyze(void);
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100583
Ramakrishna Pallala110b8e42018-04-27 12:55:43 +0530584/**
585 * @brief Iterate over all the threads in the system.
586 *
587 * This routine iterates over all the threads in the system and
588 * calls the user_cb function for each thread.
589 *
590 * @param user_cb Pointer to the user callback function.
591 * @param user_data Pointer to user data.
592 *
593 * @note CONFIG_THREAD_MONITOR must be set for this function
594 * to be effective. Also this API uses irq_lock to protect the
595 * _kernel.threads list which means creation of new threads and
596 * terminations of existing threads are blocked until this
597 * API returns.
598 *
599 * @return N/A
600 */
601extern void k_thread_foreach(k_thread_user_cb_t user_cb, void *user_data);
602
Anas Nashif166f5192018-02-25 08:02:36 -0600603/** @} */
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100604
605/**
Allan Stephensc98da842016-11-11 15:45:03 -0500606 * @defgroup thread_apis Thread APIs
607 * @ingroup kernel_apis
608 * @{
609 */
610
Benjamin Walshed240f22017-01-22 13:05:08 -0500611#endif /* !_ASMLANGUAGE */
612
613
614/*
615 * Thread user options. May be needed by assembly code. Common part uses low
616 * bits, arch-specific use high bits.
617 */
618
619/* system thread that must not abort */
620#define K_ESSENTIAL (1 << 0)
621
622#if defined(CONFIG_FP_SHARING)
623/* thread uses floating point registers */
624#define K_FP_REGS (1 << 1)
625#endif
626
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700627/* This thread has dropped from supervisor mode to user mode and consequently
628 * has additional restrictions
629 */
630#define K_USER (1 << 2)
631
Andrew Boie47f8fd12017-10-05 11:11:02 -0700632/* Indicates that the thread being created should inherit all kernel object
633 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
634 * is not enabled.
635 */
636#define K_INHERIT_PERMS (1 << 3)
637
Benjamin Walshed240f22017-01-22 13:05:08 -0500638#ifdef CONFIG_X86
639/* x86 Bitmask definitions for threads user options */
640
641#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
642/* thread uses SSEx (and also FP) registers */
643#define K_SSE_REGS (1 << 7)
644#endif
645#endif
646
647/* end - thread options */
648
649#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400650/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700651 * @brief Create a thread.
652 *
653 * This routine initializes a thread, then schedules it for execution.
654 *
655 * The new thread may be scheduled for immediate execution or a delayed start.
656 * If the newly spawned thread does not have a delayed start the kernel
657 * scheduler may preempt the current thread to allow the new thread to
658 * execute.
659 *
660 * Thread options are architecture-specific, and can include K_ESSENTIAL,
661 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
662 * them using "|" (the logical OR operator).
663 *
664 * Historically, users often would use the beginning of the stack memory region
665 * to store the struct k_thread data, although corruption will occur if the
666 * stack overflows this region and stack protection features may not detect this
667 * situation.
668 *
669 * @param new_thread Pointer to uninitialized struct k_thread
670 * @param stack Pointer to the stack space.
671 * @param stack_size Stack size in bytes.
672 * @param entry Thread entry function.
673 * @param p1 1st entry point parameter.
674 * @param p2 2nd entry point parameter.
675 * @param p3 3rd entry point parameter.
676 * @param prio Thread priority.
677 * @param options Thread options.
678 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
679 *
680 * @return ID of new thread.
681 */
Andrew Boie662c3452017-10-02 10:51:18 -0700682__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700683 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700684 size_t stack_size,
685 k_thread_entry_t entry,
686 void *p1, void *p2, void *p3,
687 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700688
Andrew Boie3f091b52017-08-30 14:34:14 -0700689/**
690 * @brief Drop a thread's privileges permanently to user mode
691 *
692 * @param entry Function to start executing from
693 * @param p1 1st entry point parameter
694 * @param p2 2nd entry point parameter
695 * @param p3 3rd entry point parameter
696 */
697extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
698 void *p1, void *p2,
699 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700700
Andrew Boied26cf2d2017-03-30 13:07:02 -0700701/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700702 * @brief Grant a thread access to a NULL-terminated set of kernel objects
703 *
704 * This is a convenience function. For the provided thread, grant access to
705 * the remaining arguments, which must be pointers to kernel objects.
706 * The final argument must be a NULL.
707 *
708 * The thread object must be initialized (i.e. running). The objects don't
709 * need to be.
710 *
711 * @param thread Thread to grant access to objects
712 * @param ... NULL-terminated list of kernel object pointers
713 */
714extern void __attribute__((sentinel))
715 k_thread_access_grant(struct k_thread *thread, ...);
716
717/**
Andrew Boie92e5bd72018-04-12 17:12:15 -0700718 * @brief Assign a resource memory pool to a thread
719 *
720 * By default, threads have no resource pool assigned unless their parent
721 * thread has a resource pool, in which case it is inherited. Multiple
722 * threads may be assigned to the same memory pool.
723 *
724 * Changing a thread's resource pool will not migrate allocations from the
725 * previous pool.
726 *
727 * @param thread Target thread to assign a memory pool for resource requests,
728 * or NULL if the thread should no longer have a memory pool.
729 * @param pool Memory pool to use for resources.
730 */
731static inline void k_thread_resource_pool_assign(struct k_thread *thread,
732 struct k_mem_pool *pool)
733{
734 thread->resource_pool = pool;
735}
736
737#if (CONFIG_HEAP_MEM_POOL_SIZE > 0)
738/**
739 * @brief Assign the system heap as a thread's resource pool
740 *
741 * Similar to k_thread_resource_pool_assign(), but the thread will use
742 * the kernel heap to draw memory.
743 *
744 * Use with caution, as a malicious thread could perform DoS attacks on the
745 * kernel heap.
746 *
747 * @param thread Target thread to assign the system heap for resource requests
748 */
749void k_thread_system_pool_assign(struct k_thread *thread);
750#endif /* (CONFIG_HEAP_MEM_POOL_SIZE > 0) */
751
752/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500753 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400754 *
Allan Stephensc98da842016-11-11 15:45:03 -0500755 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500756 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400757 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500758 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400759 *
760 * @return N/A
761 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700762__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400763
764/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500765 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400766 *
767 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500768 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400769 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400770 * @return N/A
771 */
Kumar Galacc334c72017-04-21 10:55:34 -0500772extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400773
774/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500775 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400776 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500777 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400778 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500779 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400780 *
781 * @return N/A
782 */
Andrew Boie468190a2017-09-29 14:00:48 -0700783__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400784
785/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500786 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400787 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500788 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400789 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500790 * If @a thread is not currently sleeping, the routine has no effect.
791 *
792 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400793 *
794 * @return N/A
795 */
Andrew Boie468190a2017-09-29 14:00:48 -0700796__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400797
798/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500799 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500801 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400802 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700803__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400804
805/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500806 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400807 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500808 * This routine prevents @a thread from executing if it has not yet started
809 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500811 * @param thread ID of thread to cancel.
812 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700813 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500814 * @retval -EINVAL Thread has already started executing.
Andy Ross3f55daf2018-04-03 09:42:40 -0700815 *
816 * @deprecated This API is deprecated. Use k_thread_abort().
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400817 */
Andy Ross3f55daf2018-04-03 09:42:40 -0700818__deprecated __syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400819
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400820/**
Allan Stephensc98da842016-11-11 15:45:03 -0500821 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500823 * This routine permanently stops execution of @a thread. The thread is taken
824 * off all kernel queues it is part of (i.e. the ready queue, the timeout
825 * queue, or a kernel object wait queue). However, any kernel resources the
826 * thread might currently own (such as mutexes or memory blocks) are not
827 * released. It is the responsibility of the caller of this routine to ensure
828 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400829 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500830 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400831 *
832 * @return N/A
833 */
Andrew Boie468190a2017-09-29 14:00:48 -0700834__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400835
Andrew Boie7d627c52017-08-30 11:01:56 -0700836
837/**
838 * @brief Start an inactive thread
839 *
840 * If a thread was created with K_FOREVER in the delay parameter, it will
841 * not be added to the scheduling queue until this function is called
842 * on it.
843 *
844 * @param thread thread to start
845 */
Andrew Boie468190a2017-09-29 14:00:48 -0700846__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700847
Allan Stephensc98da842016-11-11 15:45:03 -0500848/**
849 * @cond INTERNAL_HIDDEN
850 */
851
Benjamin Walshd211a522016-12-06 11:44:01 -0500852/* timeout has timed out and is not on _timeout_q anymore */
853#define _EXPIRED (-2)
854
855/* timeout is not in use */
856#define _INACTIVE (-1)
857
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400858struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700859 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700860 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400861 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700862 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500863 void *init_p1;
864 void *init_p2;
865 void *init_p3;
866 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500867 u32_t init_options;
868 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500869 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400870};
871
Andrew Boied26cf2d2017-03-30 13:07:02 -0700872#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400873 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500874 prio, options, delay, abort, groups) \
875 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700876 .init_thread = (thread), \
877 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500878 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700879 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400880 .init_p1 = (void *)p1, \
881 .init_p2 = (void *)p2, \
882 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500883 .init_prio = (prio), \
884 .init_options = (options), \
885 .init_delay = (delay), \
886 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400887 }
888
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400889/**
Allan Stephensc98da842016-11-11 15:45:03 -0500890 * INTERNAL_HIDDEN @endcond
891 */
892
893/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500894 * @brief Statically define and initialize a thread.
895 *
896 * The thread may be scheduled for immediate execution or a delayed start.
897 *
898 * Thread options are architecture-specific, and can include K_ESSENTIAL,
899 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
900 * them using "|" (the logical OR operator).
901 *
902 * The ID of the thread can be accessed using:
903 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500904 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500905 *
906 * @param name Name of the thread.
907 * @param stack_size Stack size in bytes.
908 * @param entry Thread entry function.
909 * @param p1 1st entry point parameter.
910 * @param p2 2nd entry point parameter.
911 * @param p3 3rd entry point parameter.
912 * @param prio Thread priority.
913 * @param options Thread options.
914 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400915 *
916 * @internal It has been observed that the x86 compiler by default aligns
917 * these _static_thread_data structures to 32-byte boundaries, thereby
918 * wasting space. To work around this, force a 4-byte alignment.
919 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500920#define K_THREAD_DEFINE(name, stack_size, \
921 entry, p1, p2, p3, \
922 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700923 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700924 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500925 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500926 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700927 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
928 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500929 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700930 NULL, 0); \
931 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400932
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400933/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500934 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400935 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500936 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400937 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500938 * @param thread ID of thread whose priority is needed.
939 *
940 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400941 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700942__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400943
944/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500945 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400946 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500947 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400948 *
949 * Rescheduling can occur immediately depending on the priority @a thread is
950 * set to:
951 *
952 * - If its priority is raised above the priority of the caller of this
953 * function, and the caller is preemptible, @a thread will be scheduled in.
954 *
955 * - If the caller operates on itself, it lowers its priority below that of
956 * other threads in the system, and the caller is preemptible, the thread of
957 * highest priority will be scheduled in.
958 *
959 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
960 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
961 * highest priority.
962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500963 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400964 * @param prio New priority.
965 *
966 * @warning Changing the priority of a thread currently involved in mutex
967 * priority inheritance may result in undefined behavior.
968 *
969 * @return N/A
970 */
Andrew Boie468190a2017-09-29 14:00:48 -0700971__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400972
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400973/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500974 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400975 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500976 * This routine prevents the kernel scheduler from making @a thread the
977 * current thread. All other internal operations on @a thread are still
978 * performed; for example, any timeout it is waiting on keeps ticking,
979 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400980 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500981 * If @a thread is already suspended, the routine has no effect.
982 *
983 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400984 *
985 * @return N/A
986 */
Andrew Boie468190a2017-09-29 14:00:48 -0700987__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400988
989/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500990 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400991 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500992 * This routine allows the kernel scheduler to make @a thread the current
993 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400994 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500995 * If @a thread is not currently suspended, the routine has no effect.
996 *
997 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400998 *
999 * @return N/A
1000 */
Andrew Boie468190a2017-09-29 14:00:48 -07001001__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001002
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001003/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001004 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001006 * This routine specifies how the scheduler will perform time slicing of
1007 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001008 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001009 * To enable time slicing, @a slice must be non-zero. The scheduler
1010 * ensures that no thread runs for more than the specified time limit
1011 * before other threads of that priority are given a chance to execute.
1012 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -07001013 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001014 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001015 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001016 * execute. Once the scheduler selects a thread for execution, there is no
1017 * minimum guaranteed time the thread will execute before threads of greater or
1018 * equal priority are scheduled.
1019 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001020 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001021 * for execution, this routine has no effect; the thread is immediately
1022 * rescheduled after the slice period expires.
1023 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001024 * To disable timeslicing, set both @a slice and @a prio to zero.
1025 *
1026 * @param slice Maximum time slice length (in milliseconds).
1027 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001028 *
1029 * @return N/A
1030 */
Kumar Galacc334c72017-04-21 10:55:34 -05001031extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001032
Anas Nashif166f5192018-02-25 08:02:36 -06001033/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05001034
1035/**
1036 * @addtogroup isr_apis
1037 * @{
1038 */
1039
1040/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001041 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001042 *
Allan Stephensc98da842016-11-11 15:45:03 -05001043 * This routine allows the caller to customize its actions, depending on
1044 * whether it is a thread or an ISR.
1045 *
1046 * @note Can be called by ISRs.
1047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001048 * @return 0 if invoked by a thread.
1049 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001050 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -05001051extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001052
Benjamin Walsh445830d2016-11-10 15:54:27 -05001053/**
1054 * @brief Determine if code is running in a preemptible thread.
1055 *
Allan Stephensc98da842016-11-11 15:45:03 -05001056 * This routine allows the caller to customize its actions, depending on
1057 * whether it can be preempted by another thread. The routine returns a 'true'
1058 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -05001059 *
Allan Stephensc98da842016-11-11 15:45:03 -05001060 * - The code is running in a thread, not at ISR.
1061 * - The thread's priority is in the preemptible range.
1062 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001063 *
Allan Stephensc98da842016-11-11 15:45:03 -05001064 * @note Can be called by ISRs.
1065 *
1066 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -05001067 * @return Non-zero if invoked by a preemptible thread.
1068 */
Andrew Boie468190a2017-09-29 14:00:48 -07001069__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -05001070
Allan Stephensc98da842016-11-11 15:45:03 -05001071/**
Anas Nashif166f5192018-02-25 08:02:36 -06001072 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001073 */
1074
1075/**
1076 * @addtogroup thread_apis
1077 * @{
1078 */
1079
1080/**
1081 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001082 *
Allan Stephensc98da842016-11-11 15:45:03 -05001083 * This routine prevents the current thread from being preempted by another
1084 * thread by instructing the scheduler to treat it as a cooperative thread.
1085 * If the thread subsequently performs an operation that makes it unready,
1086 * it will be context switched out in the normal manner. When the thread
1087 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001088 *
Allan Stephensc98da842016-11-11 15:45:03 -05001089 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001090 *
Allan Stephensc98da842016-11-11 15:45:03 -05001091 * @note k_sched_lock() and k_sched_unlock() should normally be used
1092 * when the operation being performed can be safely interrupted by ISRs.
1093 * However, if the amount of processing involved is very small, better
1094 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001095 *
1096 * @return N/A
1097 */
1098extern void k_sched_lock(void);
1099
Allan Stephensc98da842016-11-11 15:45:03 -05001100/**
1101 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001102 *
Allan Stephensc98da842016-11-11 15:45:03 -05001103 * This routine reverses the effect of a previous call to k_sched_lock().
1104 * A thread must call the routine once for each time it called k_sched_lock()
1105 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -05001106 *
1107 * @return N/A
1108 */
1109extern void k_sched_unlock(void);
1110
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001111/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001112 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001113 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001114 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001115 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001116 * Custom data is not used by the kernel itself, and is freely available
1117 * for a thread to use as it sees fit. It can be used as a framework
1118 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001119 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001120 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001121 *
1122 * @return N/A
1123 */
Andrew Boie468190a2017-09-29 14:00:48 -07001124__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001125
1126/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001127 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001128 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001129 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001130 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001131 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001132 */
Andrew Boie468190a2017-09-29 14:00:48 -07001133__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001134
1135/**
Anas Nashif166f5192018-02-25 08:02:36 -06001136 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05001137 */
1138
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001139#include <sys_clock.h>
1140
Allan Stephensc2f15a42016-11-17 12:24:22 -05001141/**
1142 * @addtogroup clock_apis
1143 * @{
1144 */
1145
1146/**
1147 * @brief Generate null timeout delay.
1148 *
1149 * This macro generates a timeout delay that that instructs a kernel API
1150 * not to wait if the requested operation cannot be performed immediately.
1151 *
1152 * @return Timeout delay value.
1153 */
1154#define K_NO_WAIT 0
1155
1156/**
1157 * @brief Generate timeout delay from milliseconds.
1158 *
1159 * This macro generates a timeout delay that that instructs a kernel API
1160 * to wait up to @a ms milliseconds to perform the requested operation.
1161 *
1162 * @param ms Duration in milliseconds.
1163 *
1164 * @return Timeout delay value.
1165 */
Johan Hedberg14471692016-11-13 10:52:15 +02001166#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001167
1168/**
1169 * @brief Generate timeout delay from seconds.
1170 *
1171 * This macro generates a timeout delay that that instructs a kernel API
1172 * to wait up to @a s seconds to perform the requested operation.
1173 *
1174 * @param s Duration in seconds.
1175 *
1176 * @return Timeout delay value.
1177 */
Johan Hedberg14471692016-11-13 10:52:15 +02001178#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001179
1180/**
1181 * @brief Generate timeout delay from minutes.
1182 *
1183 * This macro generates a timeout delay that that instructs a kernel API
1184 * to wait up to @a m minutes to perform the requested operation.
1185 *
1186 * @param m Duration in minutes.
1187 *
1188 * @return Timeout delay value.
1189 */
Johan Hedberg14471692016-11-13 10:52:15 +02001190#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001191
1192/**
1193 * @brief Generate timeout delay from hours.
1194 *
1195 * This macro generates a timeout delay that that instructs a kernel API
1196 * to wait up to @a h hours to perform the requested operation.
1197 *
1198 * @param h Duration in hours.
1199 *
1200 * @return Timeout delay value.
1201 */
Johan Hedberg14471692016-11-13 10:52:15 +02001202#define K_HOURS(h) K_MINUTES((h) * 60)
1203
Allan Stephensc98da842016-11-11 15:45:03 -05001204/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001205 * @brief Generate infinite timeout delay.
1206 *
1207 * This macro generates a timeout delay that that instructs a kernel API
1208 * to wait as long as necessary to perform the requested operation.
1209 *
1210 * @return Timeout delay value.
1211 */
1212#define K_FOREVER (-1)
1213
1214/**
Anas Nashif166f5192018-02-25 08:02:36 -06001215 * @}
Allan Stephensc2f15a42016-11-17 12:24:22 -05001216 */
1217
1218/**
Allan Stephensc98da842016-11-11 15:45:03 -05001219 * @cond INTERNAL_HIDDEN
1220 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001221
Benjamin Walsh62092182016-12-20 14:39:08 -05001222/* kernel clocks */
1223
1224#if (sys_clock_ticks_per_sec == 1000) || \
1225 (sys_clock_ticks_per_sec == 500) || \
1226 (sys_clock_ticks_per_sec == 250) || \
1227 (sys_clock_ticks_per_sec == 125) || \
1228 (sys_clock_ticks_per_sec == 100) || \
1229 (sys_clock_ticks_per_sec == 50) || \
1230 (sys_clock_ticks_per_sec == 25) || \
1231 (sys_clock_ticks_per_sec == 20) || \
1232 (sys_clock_ticks_per_sec == 10) || \
1233 (sys_clock_ticks_per_sec == 1)
1234
1235 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1236#else
1237 /* yields horrible 64-bit math on many architectures: try to avoid */
1238 #define _NON_OPTIMIZED_TICKS_PER_SEC
1239#endif
1240
1241#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001242extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001243#else
Kumar Galacc334c72017-04-21 10:55:34 -05001244static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001245{
Kumar Galacc334c72017-04-21 10:55:34 -05001246 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001247}
1248#endif
1249
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001250/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001251#ifdef CONFIG_TICKLESS_KERNEL
1252#define _TICK_ALIGN 0
1253#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001254#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001255#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001256
Kumar Galacc334c72017-04-21 10:55:34 -05001257static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001258{
Benjamin Walsh62092182016-12-20 14:39:08 -05001259#ifdef CONFIG_SYS_CLOCK_EXISTS
1260
1261#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001262 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001263#else
Kumar Galacc334c72017-04-21 10:55:34 -05001264 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001265#endif
1266
1267#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001268 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001269 return 0;
1270#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001271}
1272
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001273struct k_timer {
1274 /*
1275 * _timeout structure must be first here if we want to use
1276 * dynamic timer allocation. timeout.node is used in the double-linked
1277 * list of free timers
1278 */
1279 struct _timeout timeout;
1280
Allan Stephens45bfa372016-10-12 12:39:42 -05001281 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001282 _wait_q_t wait_q;
1283
1284 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001285 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001286
1287 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001288 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001289
1290 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001291 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001292
Allan Stephens45bfa372016-10-12 12:39:42 -05001293 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001294 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001295
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001296 /* user-specific data, also used to support legacy features */
1297 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001298
Anas Nashif2f203c22016-12-18 06:57:45 -05001299 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001300};
1301
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001302#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001303 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001304 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001305 .timeout.wait_q = NULL, \
1306 .timeout.thread = NULL, \
1307 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001308 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001309 .expiry_fn = expiry, \
1310 .stop_fn = stop, \
1311 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001312 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001313 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001314 }
1315
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001316#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1317
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001318/**
Allan Stephensc98da842016-11-11 15:45:03 -05001319 * INTERNAL_HIDDEN @endcond
1320 */
1321
1322/**
1323 * @defgroup timer_apis Timer APIs
1324 * @ingroup kernel_apis
1325 * @{
1326 */
1327
1328/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001329 * @typedef k_timer_expiry_t
1330 * @brief Timer expiry function type.
1331 *
1332 * A timer's expiry function is executed by the system clock interrupt handler
1333 * each time the timer expires. The expiry function is optional, and is only
1334 * invoked if the timer has been initialized with one.
1335 *
1336 * @param timer Address of timer.
1337 *
1338 * @return N/A
1339 */
1340typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1341
1342/**
1343 * @typedef k_timer_stop_t
1344 * @brief Timer stop function type.
1345 *
1346 * A timer's stop function is executed if the timer is stopped prematurely.
1347 * The function runs in the context of the thread that stops the timer.
1348 * The stop function is optional, and is only invoked if the timer has been
1349 * initialized with one.
1350 *
1351 * @param timer Address of timer.
1352 *
1353 * @return N/A
1354 */
1355typedef void (*k_timer_stop_t)(struct k_timer *timer);
1356
1357/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001358 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001359 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001360 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001361 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001362 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001363 *
1364 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001365 * @param expiry_fn Function to invoke each time the timer expires.
1366 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001367 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001368#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001369 struct k_timer name \
1370 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001371 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001372
Allan Stephens45bfa372016-10-12 12:39:42 -05001373/**
1374 * @brief Initialize a timer.
1375 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001376 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001377 *
1378 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001379 * @param expiry_fn Function to invoke each time the timer expires.
1380 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001381 *
1382 * @return N/A
1383 */
1384extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001385 k_timer_expiry_t expiry_fn,
1386 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001387
Allan Stephens45bfa372016-10-12 12:39:42 -05001388/**
1389 * @brief Start a timer.
1390 *
1391 * This routine starts a timer, and resets its status to zero. The timer
1392 * begins counting down using the specified duration and period values.
1393 *
1394 * Attempting to start a timer that is already running is permitted.
1395 * The timer's status is reset to zero and the timer begins counting down
1396 * using the new duration and period values.
1397 *
1398 * @param timer Address of timer.
1399 * @param duration Initial timer duration (in milliseconds).
1400 * @param period Timer period (in milliseconds).
1401 *
1402 * @return N/A
1403 */
Andrew Boiea354d492017-09-29 16:22:28 -07001404__syscall void k_timer_start(struct k_timer *timer,
1405 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001406
1407/**
1408 * @brief Stop a timer.
1409 *
1410 * This routine stops a running timer prematurely. The timer's stop function,
1411 * if one exists, is invoked by the caller.
1412 *
1413 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001414 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001415 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001416 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1417 * if @a k_timer_stop is to be called from ISRs.
1418 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001419 * @param timer Address of timer.
1420 *
1421 * @return N/A
1422 */
Andrew Boiea354d492017-09-29 16:22:28 -07001423__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001424
1425/**
1426 * @brief Read timer status.
1427 *
1428 * This routine reads the timer's status, which indicates the number of times
1429 * it has expired since its status was last read.
1430 *
1431 * Calling this routine resets the timer's status to zero.
1432 *
1433 * @param timer Address of timer.
1434 *
1435 * @return Timer status.
1436 */
Andrew Boiea354d492017-09-29 16:22:28 -07001437__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001438
1439/**
1440 * @brief Synchronize thread to timer expiration.
1441 *
1442 * This routine blocks the calling thread until the timer's status is non-zero
1443 * (indicating that it has expired at least once since it was last examined)
1444 * or the timer is stopped. If the timer status is already non-zero,
1445 * or the timer is already stopped, the caller continues without waiting.
1446 *
1447 * Calling this routine resets the timer's status to zero.
1448 *
1449 * This routine must not be used by interrupt handlers, since they are not
1450 * allowed to block.
1451 *
1452 * @param timer Address of timer.
1453 *
1454 * @return Timer status.
1455 */
Andrew Boiea354d492017-09-29 16:22:28 -07001456__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001457
1458/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001459 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001460 *
1461 * This routine computes the (approximate) time remaining before a running
1462 * timer next expires. If the timer is not running, it returns zero.
1463 *
1464 * @param timer Address of timer.
1465 *
1466 * @return Remaining time (in milliseconds).
1467 */
Andrew Boiea354d492017-09-29 16:22:28 -07001468__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1469
1470static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001471{
1472 return _timeout_remaining_get(&timer->timeout);
1473}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001474
Allan Stephensc98da842016-11-11 15:45:03 -05001475/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001476 * @brief Associate user-specific data with a timer.
1477 *
1478 * This routine records the @a user_data with the @a timer, to be retrieved
1479 * later.
1480 *
1481 * It can be used e.g. in a timer handler shared across multiple subsystems to
1482 * retrieve data specific to the subsystem this timer is associated with.
1483 *
1484 * @param timer Address of timer.
1485 * @param user_data User data to associate with the timer.
1486 *
1487 * @return N/A
1488 */
Andrew Boiea354d492017-09-29 16:22:28 -07001489__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1490
Anas Nashif954d5502018-02-25 08:37:28 -06001491/**
1492 * @internal
1493 */
Andrew Boiea354d492017-09-29 16:22:28 -07001494static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1495 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001496{
1497 timer->user_data = user_data;
1498}
1499
1500/**
1501 * @brief Retrieve the user-specific data from a timer.
1502 *
1503 * @param timer Address of timer.
1504 *
1505 * @return The user data.
1506 */
Andrew Boiea354d492017-09-29 16:22:28 -07001507__syscall void *k_timer_user_data_get(struct k_timer *timer);
1508
1509static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001510{
1511 return timer->user_data;
1512}
1513
Anas Nashif166f5192018-02-25 08:02:36 -06001514/** @} */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001515
Allan Stephensc98da842016-11-11 15:45:03 -05001516/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001517 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001518 * @{
1519 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001520
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001521/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001522 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001523 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001524 * This routine returns the elapsed time since the system booted,
1525 * in milliseconds.
1526 *
1527 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001528 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001529__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001530
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001531/**
1532 * @brief Enable clock always on in tickless kernel
1533 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001534 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001535 * there are no timer events programmed in tickless kernel
1536 * scheduling. This is necessary if the clock is used to track
1537 * passage of time.
1538 *
1539 * @retval prev_status Previous status of always on flag
1540 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301541#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001542static inline int k_enable_sys_clock_always_on(void)
1543{
1544 int prev_status = _sys_clock_always_on;
1545
1546 _sys_clock_always_on = 1;
1547 _enable_sys_clock();
1548
1549 return prev_status;
1550}
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301551#else
1552#define k_enable_sys_clock_always_on() do { } while ((0))
1553#endif
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001554
1555/**
1556 * @brief Disable clock always on in tickless kernel
1557 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001558 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001559 * there are no timer events programmed in tickless kernel
1560 * scheduling. To save power, this routine should be called
1561 * immediately when clock is not used to track time.
1562 */
Ramakrishna Pallala2b8cf4c2018-03-29 22:54:36 +05301563#ifdef CONFIG_TICKLESS_KERNEL
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001564static inline void k_disable_sys_clock_always_on(void)
1565{
1566 _sys_clock_always_on = 0;
1567}
1568#else
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001569#define k_disable_sys_clock_always_on() do { } while ((0))
1570#endif
1571
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001572/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001573 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001574 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001575 * This routine returns the lower 32-bits of the elapsed time since the system
1576 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001577 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001578 * This routine can be more efficient than k_uptime_get(), as it reduces the
1579 * need for interrupt locking and 64-bit math. However, the 32-bit result
1580 * cannot hold a system uptime time larger than approximately 50 days, so the
1581 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001582 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001583 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001584 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001585__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001586
1587/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001588 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001590 * This routine computes the elapsed time between the current system uptime
1591 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001592 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001593 * @param reftime Pointer to a reference time, which is updated to the current
1594 * uptime upon return.
1595 *
1596 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001597 */
Kumar Galacc334c72017-04-21 10:55:34 -05001598extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001599
1600/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001601 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001603 * This routine computes the elapsed time between the current system uptime
1604 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001605 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001606 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1607 * need for interrupt locking and 64-bit math. However, the 32-bit result
1608 * cannot hold an elapsed time larger than approximately 50 days, so the
1609 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001610 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001611 * @param reftime Pointer to a reference time, which is updated to the current
1612 * uptime upon return.
1613 *
1614 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001615 */
Kumar Galacc334c72017-04-21 10:55:34 -05001616extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001617
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001618/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001619 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001621 * This routine returns the current time, as measured by the system's hardware
1622 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001624 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001625 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001626#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001627
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001628/**
Anas Nashif166f5192018-02-25 08:02:36 -06001629 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001630 */
1631
Allan Stephensc98da842016-11-11 15:45:03 -05001632/**
1633 * @cond INTERNAL_HIDDEN
1634 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001635
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001636struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001637 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001638 union {
1639 _wait_q_t wait_q;
1640
1641 _POLL_EVENT;
1642 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001643
1644 _OBJECT_TRACING_NEXT_PTR(k_queue);
1645};
1646
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001647#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001648 { \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001649 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Michael Hope5f67a612018-03-17 12:44:40 +01001650 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001651 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001652 _OBJECT_TRACING_INIT \
1653 }
1654
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001655#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1656
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001657/**
1658 * INTERNAL_HIDDEN @endcond
1659 */
1660
1661/**
1662 * @defgroup queue_apis Queue APIs
1663 * @ingroup kernel_apis
1664 * @{
1665 */
1666
1667/**
1668 * @brief Initialize a queue.
1669 *
1670 * This routine initializes a queue object, prior to its first use.
1671 *
1672 * @param queue Address of the queue.
1673 *
1674 * @return N/A
1675 */
1676extern void k_queue_init(struct k_queue *queue);
1677
1678/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001679 * @brief Cancel waiting on a queue.
1680 *
1681 * This routine causes first thread pending on @a queue, if any, to
1682 * return from k_queue_get() call with NULL value (as if timeout expired).
1683 *
1684 * @note Can be called by ISRs.
1685 *
1686 * @param queue Address of the queue.
1687 *
1688 * @return N/A
1689 */
1690extern void k_queue_cancel_wait(struct k_queue *queue);
1691
1692/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001693 * @brief Append an element to the end of a queue.
1694 *
1695 * This routine appends a data item to @a queue. A queue data item must be
1696 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1697 * reserved for the kernel's use.
1698 *
1699 * @note Can be called by ISRs.
1700 *
1701 * @param queue Address of the queue.
1702 * @param data Address of the data item.
1703 *
1704 * @return N/A
1705 */
1706extern void k_queue_append(struct k_queue *queue, void *data);
1707
1708/**
1709 * @brief Prepend an element to a queue.
1710 *
1711 * This routine prepends a data item to @a queue. A queue data item must be
1712 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1713 * reserved for the kernel's use.
1714 *
1715 * @note Can be called by ISRs.
1716 *
1717 * @param queue Address of the queue.
1718 * @param data Address of the data item.
1719 *
1720 * @return N/A
1721 */
1722extern void k_queue_prepend(struct k_queue *queue, void *data);
1723
1724/**
1725 * @brief Inserts an element to a queue.
1726 *
1727 * This routine inserts a data item to @a queue after previous item. A queue
1728 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1729 * item are reserved for the kernel's use.
1730 *
1731 * @note Can be called by ISRs.
1732 *
1733 * @param queue Address of the queue.
1734 * @param prev Address of the previous data item.
1735 * @param data Address of the data item.
1736 *
1737 * @return N/A
1738 */
1739extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1740
1741/**
1742 * @brief Atomically append a list of elements to a queue.
1743 *
1744 * This routine adds a list of data items to @a queue in one operation.
1745 * The data items must be in a singly-linked list, with the first 32 bits
1746 * in each data item pointing to the next data item; the list must be
1747 * NULL-terminated.
1748 *
1749 * @note Can be called by ISRs.
1750 *
1751 * @param queue Address of the queue.
1752 * @param head Pointer to first node in singly-linked list.
1753 * @param tail Pointer to last node in singly-linked list.
1754 *
1755 * @return N/A
1756 */
1757extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1758
1759/**
1760 * @brief Atomically add a list of elements to a queue.
1761 *
1762 * This routine adds a list of data items to @a queue in one operation.
1763 * The data items must be in a singly-linked list implemented using a
1764 * sys_slist_t object. Upon completion, the original list is empty.
1765 *
1766 * @note Can be called by ISRs.
1767 *
1768 * @param queue Address of the queue.
1769 * @param list Pointer to sys_slist_t object.
1770 *
1771 * @return N/A
1772 */
1773extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1774
1775/**
1776 * @brief Get an element from a queue.
1777 *
1778 * This routine removes first data item from @a queue. The first 32 bits of the
1779 * data item are reserved for the kernel's use.
1780 *
1781 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1782 *
1783 * @param queue Address of the queue.
1784 * @param timeout Waiting period to obtain a data item (in milliseconds),
1785 * or one of the special values K_NO_WAIT and K_FOREVER.
1786 *
1787 * @return Address of the data item if successful; NULL if returned
1788 * without waiting, or waiting period timed out.
1789 */
Kumar Galacc334c72017-04-21 10:55:34 -05001790extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001791
1792/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001793 * @brief Remove an element from a queue.
1794 *
1795 * This routine removes data item from @a queue. The first 32 bits of the
1796 * data item are reserved for the kernel's use. Removing elements from k_queue
1797 * rely on sys_slist_find_and_remove which is not a constant time operation.
1798 *
1799 * @note Can be called by ISRs
1800 *
1801 * @param queue Address of the queue.
1802 * @param data Address of the data item.
1803 *
1804 * @return true if data item was removed
1805 */
1806static inline bool k_queue_remove(struct k_queue *queue, void *data)
1807{
1808 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1809}
1810
1811/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001812 * @brief Query a queue to see if it has data available.
1813 *
1814 * Note that the data might be already gone by the time this function returns
1815 * if other threads are also trying to read from the queue.
1816 *
1817 * @note Can be called by ISRs.
1818 *
1819 * @param queue Address of the queue.
1820 *
1821 * @return Non-zero if the queue is empty.
1822 * @return 0 if data is available.
1823 */
1824static inline int k_queue_is_empty(struct k_queue *queue)
1825{
1826 return (int)sys_slist_is_empty(&queue->data_q);
1827}
1828
1829/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001830 * @brief Peek element at the head of queue.
1831 *
1832 * Return element from the head of queue without removing it.
1833 *
1834 * @param queue Address of the queue.
1835 *
1836 * @return Head element, or NULL if queue is empty.
1837 */
1838static inline void *k_queue_peek_head(struct k_queue *queue)
1839{
1840 return sys_slist_peek_head(&queue->data_q);
1841}
1842
1843/**
1844 * @brief Peek element at the tail of queue.
1845 *
1846 * Return element from the tail of queue without removing it.
1847 *
1848 * @param queue Address of the queue.
1849 *
1850 * @return Tail element, or NULL if queue is empty.
1851 */
1852static inline void *k_queue_peek_tail(struct k_queue *queue)
1853{
1854 return sys_slist_peek_tail(&queue->data_q);
1855}
1856
1857/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001858 * @brief Statically define and initialize a queue.
1859 *
1860 * The queue can be accessed outside the module where it is defined using:
1861 *
1862 * @code extern struct k_queue <name>; @endcode
1863 *
1864 * @param name Name of the queue.
1865 */
1866#define K_QUEUE_DEFINE(name) \
1867 struct k_queue name \
1868 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001869 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001870
Anas Nashif166f5192018-02-25 08:02:36 -06001871/** @} */
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001872
1873/**
1874 * @cond INTERNAL_HIDDEN
1875 */
1876
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001877struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001878 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001879};
1880
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001881#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001882 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001883 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001884 }
1885
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001886#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1887
Allan Stephensc98da842016-11-11 15:45:03 -05001888/**
1889 * INTERNAL_HIDDEN @endcond
1890 */
1891
1892/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001893 * @defgroup fifo_apis FIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05001894 * @ingroup kernel_apis
1895 * @{
1896 */
1897
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001898/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001899 * @brief Initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001900 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001901 * This routine initializes a FIFO queue, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001902 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001903 * @param fifo Address of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001904 *
1905 * @return N/A
1906 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001907#define k_fifo_init(fifo) \
1908 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001909
1910/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001911 * @brief Cancel waiting on a FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001912 *
1913 * This routine causes first thread pending on @a fifo, if any, to
1914 * return from k_fifo_get() call with NULL value (as if timeout
1915 * expired).
1916 *
1917 * @note Can be called by ISRs.
1918 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001919 * @param fifo Address of the FIFO queue.
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001920 *
1921 * @return N/A
1922 */
1923#define k_fifo_cancel_wait(fifo) \
1924 k_queue_cancel_wait((struct k_queue *) fifo)
1925
1926/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001927 * @brief Add an element to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001928 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001929 * This routine adds a data item to @a fifo. A FIFO data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001930 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1931 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001933 * @note Can be called by ISRs.
1934 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001935 * @param fifo Address of the FIFO.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001936 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001937 *
1938 * @return N/A
1939 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001940#define k_fifo_put(fifo, data) \
1941 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001942
1943/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001944 * @brief Atomically add a list of elements to a FIFO.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001945 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001946 * This routine adds a list of data items to @a fifo in one operation.
1947 * The data items must be in a singly-linked list, with the first 32 bits
1948 * each data item pointing to the next data item; the list must be
1949 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001950 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001951 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001952 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001953 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001954 * @param head Pointer to first node in singly-linked list.
1955 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001956 *
1957 * @return N/A
1958 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001959#define k_fifo_put_list(fifo, head, tail) \
1960 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001961
1962/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001963 * @brief Atomically add a list of elements to a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001964 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001965 * This routine adds a list of data items to @a fifo in one operation.
1966 * The data items must be in a singly-linked list implemented using a
1967 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001968 * and must be re-initialized via sys_slist_init().
1969 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001970 * @note Can be called by ISRs.
1971 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001972 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001973 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001974 *
1975 * @return N/A
1976 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001977#define k_fifo_put_slist(fifo, list) \
1978 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001979
1980/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001981 * @brief Get an element from a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001982 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001983 * This routine removes a data item from @a fifo in a "first in, first out"
1984 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001985 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001986 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1987 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06001988 * @param fifo Address of the FIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001989 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001990 * or one of the special values K_NO_WAIT and K_FOREVER.
1991 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001992 * @return Address of the data item if successful; NULL if returned
1993 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001994 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001995#define k_fifo_get(fifo, timeout) \
1996 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001997
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001998/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06001999 * @brief Query a FIFO queue to see if it has data available.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002000 *
2001 * Note that the data might be already gone by the time this function returns
Anas Nashif585fd1f2018-02-25 08:04:59 -06002002 * if other threads is also trying to read from the FIFO.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002003 *
2004 * @note Can be called by ISRs.
2005 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002006 * @param fifo Address of the FIFO queue.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002007 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002008 * @return Non-zero if the FIFO queue is empty.
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002009 * @return 0 if data is available.
2010 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002011#define k_fifo_is_empty(fifo) \
2012 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05002013
2014/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002015 * @brief Peek element at the head of a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002016 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002017 * Return element from the head of FIFO queue without removing it. A usecase
Ramakrishna Pallala92489ea2018-03-29 22:44:23 +05302018 * for this is if elements of the FIFO object are themselves containers. Then
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002019 * on each iteration of processing, a head container will be peeked,
2020 * and some data processed out of it, and only if the container is empty,
Anas Nashif585fd1f2018-02-25 08:04:59 -06002021 * it will be completely remove from the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002022 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002023 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002024 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002025 * @return Head element, or NULL if the FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002026 */
2027#define k_fifo_peek_head(fifo) \
2028 k_queue_peek_head((struct k_queue *) fifo)
2029
2030/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002031 * @brief Peek element at the tail of FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002032 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002033 * Return element from the tail of FIFO queue (without removing it). A usecase
2034 * for this is if elements of the FIFO queue are themselves containers. Then
2035 * it may be useful to add more data to the last container in a FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002036 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002037 * @param fifo Address of the FIFO queue.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002038 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002039 * @return Tail element, or NULL if a FIFO queue is empty.
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03002040 */
2041#define k_fifo_peek_tail(fifo) \
2042 k_queue_peek_tail((struct k_queue *) fifo)
2043
2044/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002045 * @brief Statically define and initialize a FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002046 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002047 * The FIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002048 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002049 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002050 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002051 * @param name Name of the FIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002052 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002053#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002054 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02002055 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002056 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002057
Anas Nashif166f5192018-02-25 08:02:36 -06002058/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002059
2060/**
2061 * @cond INTERNAL_HIDDEN
2062 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002063
2064struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002065 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002066};
2067
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002068#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05002069 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002070 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05002071 }
2072
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002073#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
2074
Allan Stephensc98da842016-11-11 15:45:03 -05002075/**
2076 * INTERNAL_HIDDEN @endcond
2077 */
2078
2079/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002080 * @defgroup lifo_apis LIFO APIs
Allan Stephensc98da842016-11-11 15:45:03 -05002081 * @ingroup kernel_apis
2082 * @{
2083 */
2084
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002085/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002086 * @brief Initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002087 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002088 * This routine initializes a LIFO queue object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002089 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002090 * @param lifo Address of the LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002091 *
2092 * @return N/A
2093 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002094#define k_lifo_init(lifo) \
2095 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002096
2097/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002098 * @brief Add an element to a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002099 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002100 * This routine adds a data item to @a lifo. A LIFO queue data item must be
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002101 * aligned on a 4-byte boundary, and the first 32 bits of the item are
2102 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002104 * @note Can be called by ISRs.
2105 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002106 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002107 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002108 *
2109 * @return N/A
2110 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002111#define k_lifo_put(lifo, data) \
2112 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002113
2114/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002115 * @brief Get an element from a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002117 * This routine removes a data item from @a lifo in a "last in, first out"
2118 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002119 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002120 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2121 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002122 * @param lifo Address of the LIFO queue.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002123 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002124 * or one of the special values K_NO_WAIT and K_FOREVER.
2125 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002126 * @return Address of the data item if successful; NULL if returned
2127 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002128 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002129#define k_lifo_get(lifo, timeout) \
2130 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002131
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002132/**
Anas Nashif585fd1f2018-02-25 08:04:59 -06002133 * @brief Statically define and initialize a LIFO queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002134 *
Anas Nashif585fd1f2018-02-25 08:04:59 -06002135 * The LIFO queue can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002136 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002137 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002139 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002140 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002141#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002142 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002143 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002144 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002145
Anas Nashif166f5192018-02-25 08:02:36 -06002146/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002147
2148/**
2149 * @cond INTERNAL_HIDDEN
2150 */
Andrew Boief3bee952018-05-02 17:44:39 -07002151#define K_STACK_FLAG_ALLOC BIT(0) /* Buffer was allocated */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002152
2153struct k_stack {
2154 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002155 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002156
Anas Nashif2f203c22016-12-18 06:57:45 -05002157 _OBJECT_TRACING_NEXT_PTR(k_stack);
Andrew Boief3bee952018-05-02 17:44:39 -07002158 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002159};
2160
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002161#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002162 { \
2163 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2164 .base = stack_buffer, \
2165 .next = stack_buffer, \
2166 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002167 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002168 }
2169
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002170#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2171
Allan Stephensc98da842016-11-11 15:45:03 -05002172/**
2173 * INTERNAL_HIDDEN @endcond
2174 */
2175
2176/**
2177 * @defgroup stack_apis Stack APIs
2178 * @ingroup kernel_apis
2179 * @{
2180 */
2181
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002182/**
2183 * @brief Initialize a stack.
2184 *
2185 * This routine initializes a stack object, prior to its first use.
2186 *
2187 * @param stack Address of the stack.
2188 * @param buffer Address of array used to hold stacked values.
2189 * @param num_entries Maximum number of values that can be stacked.
2190 *
2191 * @return N/A
2192 */
Andrew Boief3bee952018-05-02 17:44:39 -07002193void k_stack_init(struct k_stack *stack,
2194 u32_t *buffer, unsigned int num_entries);
2195
2196
2197/**
2198 * @brief Initialize a stack.
2199 *
2200 * This routine initializes a stack object, prior to its first use. Internal
2201 * buffers will be allocated from the calling thread's resource pool.
2202 * This memory will be released if k_stack_cleanup() is called, or
2203 * userspace is enabled and the stack object loses all references to it.
2204 *
2205 * @param stack Address of the stack.
2206 * @param num_entries Maximum number of values that can be stacked.
2207 *
2208 * @return -ENOMEM if memory couldn't be allocated
2209 */
2210
2211__syscall int k_stack_alloc_init(struct k_stack *stack,
2212 unsigned int num_entries);
2213
2214/**
2215 * @brief Release a stack's allocated buffer
2216 *
2217 * If a stack object was given a dynamically allocated buffer via
2218 * k_stack_alloc_init(), this will free it. This function does nothing
2219 * if the buffer wasn't dynamically allocated.
2220 *
2221 * @param stack Address of the stack.
2222 */
2223void k_stack_cleanup(struct k_stack *stack);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002224
2225/**
2226 * @brief Push an element onto a stack.
2227 *
2228 * This routine adds a 32-bit value @a data to @a stack.
2229 *
2230 * @note Can be called by ISRs.
2231 *
2232 * @param stack Address of the stack.
2233 * @param data Value to push onto the stack.
2234 *
2235 * @return N/A
2236 */
Andrew Boiee8734462017-09-29 16:42:07 -07002237__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002238
2239/**
2240 * @brief Pop an element from a stack.
2241 *
2242 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2243 * manner and stores the value in @a data.
2244 *
2245 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2246 *
2247 * @param stack Address of the stack.
2248 * @param data Address of area to hold the value popped from the stack.
2249 * @param timeout Waiting period to obtain a value (in milliseconds),
2250 * or one of the special values K_NO_WAIT and K_FOREVER.
2251 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002252 * @retval 0 Element popped from stack.
2253 * @retval -EBUSY Returned without waiting.
2254 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002255 */
Andrew Boiee8734462017-09-29 16:42:07 -07002256__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002257
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002258/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002259 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002260 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002261 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002262 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002263 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002264 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002265 * @param name Name of the stack.
2266 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002267 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002268#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002269 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002270 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002271 struct k_stack name \
2272 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002273 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002274 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002275
Anas Nashif166f5192018-02-25 08:02:36 -06002276/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002277
Allan Stephens6bba9b02016-11-16 14:56:54 -05002278struct k_work;
2279
Allan Stephensc98da842016-11-11 15:45:03 -05002280/**
2281 * @defgroup workqueue_apis Workqueue Thread APIs
2282 * @ingroup kernel_apis
2283 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002284 */
2285
Allan Stephens6bba9b02016-11-16 14:56:54 -05002286/**
2287 * @typedef k_work_handler_t
2288 * @brief Work item handler function type.
2289 *
2290 * A work item's handler function is executed by a workqueue's thread
2291 * when the work item is processed by the workqueue.
2292 *
2293 * @param work Address of the work item.
2294 *
2295 * @return N/A
2296 */
2297typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002298
2299/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002300 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002301 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002302
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002303struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002304 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002305 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002306};
2307
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002308enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002309 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002310};
2311
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002312struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002313 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002314 k_work_handler_t handler;
2315 atomic_t flags[1];
2316};
2317
Allan Stephens6bba9b02016-11-16 14:56:54 -05002318struct k_delayed_work {
2319 struct k_work work;
2320 struct _timeout timeout;
2321 struct k_work_q *work_q;
2322};
2323
2324extern struct k_work_q k_sys_work_q;
2325
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002326/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002327 * INTERNAL_HIDDEN @endcond
2328 */
2329
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002330#define _K_WORK_INITIALIZER(work_handler) \
2331 { \
2332 ._reserved = NULL, \
2333 .handler = work_handler, \
2334 .flags = { 0 } \
2335 }
2336
2337#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2338
Allan Stephens6bba9b02016-11-16 14:56:54 -05002339/**
2340 * @brief Initialize a statically-defined work item.
2341 *
2342 * This macro can be used to initialize a statically-defined workqueue work
2343 * item, prior to its first use. For example,
2344 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002345 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002346 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002347 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002348 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002349 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002350#define K_WORK_DEFINE(work, work_handler) \
2351 struct k_work work \
2352 __in_section(_k_work, static, work) = \
2353 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002354
2355/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002356 * @brief Initialize a work item.
2357 *
2358 * This routine initializes a workqueue work item, prior to its first use.
2359 *
2360 * @param work Address of work item.
2361 * @param handler Function to invoke each time work item is processed.
2362 *
2363 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002364 */
2365static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2366{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002367 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002368 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002369 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002370}
2371
2372/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002373 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002374 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002375 * This routine submits work item @a work to be processed by workqueue
2376 * @a work_q. If the work item is already pending in the workqueue's queue
2377 * as a result of an earlier submission, this routine has no effect on the
2378 * work item. If the work item has already been processed, or is currently
2379 * being processed, its work is considered complete and the work item can be
2380 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002381 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002382 * @warning
2383 * A submitted work item must not be modified until it has been processed
2384 * by the workqueue.
2385 *
2386 * @note Can be called by ISRs.
2387 *
2388 * @param work_q Address of workqueue.
2389 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002390 *
2391 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002392 */
2393static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2394 struct k_work *work)
2395{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002396 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002397 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002398 }
2399}
2400
2401/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002402 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002403 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002404 * This routine indicates if work item @a work is pending in a workqueue's
2405 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002406 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002407 * @note Can be called by ISRs.
2408 *
2409 * @param work Address of work item.
2410 *
2411 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002412 */
2413static inline int k_work_pending(struct k_work *work)
2414{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002415 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002416}
2417
2418/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002419 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002420 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002421 * This routine starts workqueue @a work_q. The workqueue spawns its work
2422 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002423 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002424 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002425 * @param stack Pointer to work queue thread's stack space, as defined by
2426 * K_THREAD_STACK_DEFINE()
2427 * @param stack_size Size of the work queue thread's stack (in bytes), which
2428 * should either be the same constant passed to
2429 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002430 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002431 *
2432 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002433 */
Andrew Boie507852a2017-07-25 18:47:07 -07002434extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002435 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002436 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002437
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002438/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002439 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002440 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002441 * This routine initializes a workqueue delayed work item, prior to
2442 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002443 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002444 * @param work Address of delayed work item.
2445 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002446 *
2447 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002448 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002449extern void k_delayed_work_init(struct k_delayed_work *work,
2450 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002451
2452/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002453 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002454 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002455 * This routine schedules work item @a work to be processed by workqueue
2456 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002457 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002458 * Only when the countdown completes is the work item actually submitted to
2459 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002460 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002461 * Submitting a previously submitted delayed work item that is still
Andy Ross03c1d282018-02-13 12:13:25 -08002462 * counting down cancels the existing submission and restarts the
2463 * countdown using the new delay. Note that this behavior is
2464 * inherently subject to race conditions with the pre-existing
2465 * timeouts and work queue, so care must be taken to synchronize such
2466 * resubmissions externally.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002467 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002468 * @warning
2469 * A delayed work item must not be modified until it has been processed
2470 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002471 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002472 * @note Can be called by ISRs.
2473 *
2474 * @param work_q Address of workqueue.
2475 * @param work Address of delayed work item.
2476 * @param delay Delay before submitting the work item (in milliseconds).
2477 *
2478 * @retval 0 Work item countdown started.
2479 * @retval -EINPROGRESS Work item is already pending.
2480 * @retval -EINVAL Work item is being processed or has completed its work.
2481 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002482 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002483extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2484 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002485 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002486
2487/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002488 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002489 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002490 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002491 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002492 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002493 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002494 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002495 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002496 * @param work Address of delayed work item.
2497 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002498 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002499 * @retval -EINPROGRESS Work item is already pending.
2500 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002501 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002502extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002503
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002504/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002505 * @brief Submit a work item to the system workqueue.
2506 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002507 * This routine submits work item @a work to be processed by the system
2508 * workqueue. If the work item is already pending in the workqueue's queue
2509 * as a result of an earlier submission, this routine has no effect on the
2510 * work item. If the work item has already been processed, or is currently
2511 * being processed, its work is considered complete and the work item can be
2512 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002513 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002514 * @warning
2515 * Work items submitted to the system workqueue should avoid using handlers
2516 * that block or yield since this may prevent the system workqueue from
2517 * processing other work items in a timely manner.
2518 *
2519 * @note Can be called by ISRs.
2520 *
2521 * @param work Address of work item.
2522 *
2523 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002524 */
2525static inline void k_work_submit(struct k_work *work)
2526{
2527 k_work_submit_to_queue(&k_sys_work_q, work);
2528}
2529
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002530/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002531 * @brief Submit a delayed work item to the system workqueue.
2532 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002533 * This routine schedules work item @a work to be processed by the system
2534 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002535 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002536 * Only when the countdown completes is the work item actually submitted to
2537 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002538 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002539 * Submitting a previously submitted delayed work item that is still
2540 * counting down cancels the existing submission and restarts the countdown
2541 * using the new delay. If the work item is currently pending on the
2542 * workqueue's queue because the countdown has completed it is too late to
2543 * resubmit the item, and resubmission fails without impacting the work item.
2544 * If the work item has already been processed, or is currently being processed,
2545 * its work is considered complete and the work item can be resubmitted.
2546 *
2547 * @warning
2548 * Work items submitted to the system workqueue should avoid using handlers
2549 * that block or yield since this may prevent the system workqueue from
2550 * processing other work items in a timely manner.
2551 *
2552 * @note Can be called by ISRs.
2553 *
2554 * @param work Address of delayed work item.
2555 * @param delay Delay before submitting the work item (in milliseconds).
2556 *
2557 * @retval 0 Work item countdown started.
2558 * @retval -EINPROGRESS Work item is already pending.
2559 * @retval -EINVAL Work item is being processed or has completed its work.
2560 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002561 */
2562static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002563 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002564{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002565 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002566}
2567
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002568/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002569 * @brief Get time remaining before a delayed work gets scheduled.
2570 *
2571 * This routine computes the (approximate) time remaining before a
2572 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002573 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002574 *
2575 * @param work Delayed work item.
2576 *
2577 * @return Remaining time (in milliseconds).
2578 */
Kumar Galacc334c72017-04-21 10:55:34 -05002579static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002580{
2581 return _timeout_remaining_get(&work->timeout);
2582}
2583
Anas Nashif166f5192018-02-25 08:02:36 -06002584/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002585
Allan Stephensc98da842016-11-11 15:45:03 -05002586/**
2587 * @cond INTERNAL_HIDDEN
2588 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002589
2590struct k_mutex {
2591 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002592 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002593 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002594 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002595
Anas Nashif2f203c22016-12-18 06:57:45 -05002596 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002597};
2598
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002599#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002600 { \
2601 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2602 .owner = NULL, \
2603 .lock_count = 0, \
2604 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002605 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002606 }
2607
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002608#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2609
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002610/**
Allan Stephensc98da842016-11-11 15:45:03 -05002611 * INTERNAL_HIDDEN @endcond
2612 */
2613
2614/**
2615 * @defgroup mutex_apis Mutex APIs
2616 * @ingroup kernel_apis
2617 * @{
2618 */
2619
2620/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002621 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002622 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002623 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002624 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002625 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002626 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002627 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002628 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002629#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002630 struct k_mutex name \
2631 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002632 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002633
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002634/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002635 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002636 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002637 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002638 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002639 * Upon completion, the mutex is available and does not have an owner.
2640 *
2641 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002642 *
2643 * @return N/A
2644 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002645__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002646
2647/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002648 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002649 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002650 * This routine locks @a mutex. If the mutex is locked by another thread,
2651 * the calling thread waits until the mutex becomes available or until
2652 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002653 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002654 * A thread is permitted to lock a mutex it has already locked. The operation
2655 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002657 * @param mutex Address of the mutex.
2658 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002659 * or one of the special values K_NO_WAIT and K_FOREVER.
2660 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002661 * @retval 0 Mutex locked.
2662 * @retval -EBUSY Returned without waiting.
2663 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002664 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002665__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002666
2667/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002668 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002669 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002670 * This routine unlocks @a mutex. The mutex must already be locked by the
2671 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002672 *
2673 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002674 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002675 * thread.
2676 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002677 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002678 *
2679 * @return N/A
2680 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002681__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002682
Allan Stephensc98da842016-11-11 15:45:03 -05002683/**
Anas Nashif166f5192018-02-25 08:02:36 -06002684 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05002685 */
2686
2687/**
2688 * @cond INTERNAL_HIDDEN
2689 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002690
2691struct k_sem {
2692 _wait_q_t wait_q;
2693 unsigned int count;
2694 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002695 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002696
Anas Nashif2f203c22016-12-18 06:57:45 -05002697 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002698};
2699
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002700#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002701 { \
2702 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2703 .count = initial_count, \
2704 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002705 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002706 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002707 }
2708
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002709#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2710
Allan Stephensc98da842016-11-11 15:45:03 -05002711/**
2712 * INTERNAL_HIDDEN @endcond
2713 */
2714
2715/**
2716 * @defgroup semaphore_apis Semaphore APIs
2717 * @ingroup kernel_apis
2718 * @{
2719 */
2720
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002721/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002722 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002723 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002724 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002725 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002726 * @param sem Address of the semaphore.
2727 * @param initial_count Initial semaphore count.
2728 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002729 *
2730 * @return N/A
2731 */
Andrew Boie99280232017-09-29 14:17:47 -07002732__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2733 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002734
2735/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002736 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002737 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002738 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002739 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002740 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2741 *
2742 * @param sem Address of the semaphore.
2743 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002744 * or one of the special values K_NO_WAIT and K_FOREVER.
2745 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002746 * @note When porting code from the nanokernel legacy API to the new API, be
2747 * careful with the return value of this function. The return value is the
2748 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2749 * non-zero means failure, while the nano_sem_take family returns 1 for success
2750 * and 0 for failure.
2751 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002752 * @retval 0 Semaphore taken.
2753 * @retval -EBUSY Returned without waiting.
2754 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002755 */
Andrew Boie99280232017-09-29 14:17:47 -07002756__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002757
2758/**
2759 * @brief Give a semaphore.
2760 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002761 * This routine gives @a sem, unless the semaphore is already at its maximum
2762 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002763 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002764 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002765 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002766 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002767 *
2768 * @return N/A
2769 */
Andrew Boie99280232017-09-29 14:17:47 -07002770__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002771
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002772/**
2773 * @brief Reset a semaphore's count to zero.
2774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002775 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002776 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002777 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002778 *
2779 * @return N/A
2780 */
Andrew Boie990bf162017-10-03 12:36:49 -07002781__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002782
Anas Nashif954d5502018-02-25 08:37:28 -06002783/**
2784 * @internal
2785 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002786static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002787{
2788 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002789}
2790
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002791/**
2792 * @brief Get a semaphore's count.
2793 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002794 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002795 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002796 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002798 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002799 */
Andrew Boie990bf162017-10-03 12:36:49 -07002800__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002801
Anas Nashif954d5502018-02-25 08:37:28 -06002802/**
2803 * @internal
2804 */
Andrew Boiefc273c02017-09-23 12:51:23 -07002805static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002806{
2807 return sem->count;
2808}
2809
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002810/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002811 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002813 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002814 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002815 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002817 * @param name Name of the semaphore.
2818 * @param initial_count Initial semaphore count.
2819 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002820 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002821#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002822 struct k_sem name \
2823 __in_section(_k_sem, static, name) = \
Leandro Pereiraf5f95ee2018-04-06 15:55:11 -07002824 _K_SEM_INITIALIZER(name, initial_count, count_limit); \
Rajavardhan Gundi68040c82018-04-27 10:15:15 +05302825 BUILD_ASSERT(((count_limit) != 0) && \
2826 ((initial_count) <= (count_limit)));
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002827
Anas Nashif166f5192018-02-25 08:02:36 -06002828/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002829
2830/**
2831 * @defgroup alert_apis Alert APIs
2832 * @ingroup kernel_apis
2833 * @{
2834 */
2835
Allan Stephens5eceb852016-11-16 10:16:30 -05002836/**
2837 * @typedef k_alert_handler_t
2838 * @brief Alert handler function type.
2839 *
2840 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002841 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002842 * and is only invoked if the alert has been initialized with one.
2843 *
2844 * @param alert Address of the alert.
2845 *
2846 * @return 0 if alert has been consumed; non-zero if alert should pend.
2847 */
2848typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002849
Anas Nashif166f5192018-02-25 08:02:36 -06002850/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05002851
2852/**
2853 * @cond INTERNAL_HIDDEN
2854 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002855
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002856#define K_ALERT_DEFAULT NULL
2857#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002858
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002859struct k_alert {
2860 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002861 atomic_t send_count;
2862 struct k_work work_item;
2863 struct k_sem sem;
2864
Anas Nashif2f203c22016-12-18 06:57:45 -05002865 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002866};
2867
Anas Nashif954d5502018-02-25 08:37:28 -06002868/**
2869 * @internal
2870 */
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002871extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002872
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002873#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002874 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002875 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002876 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002877 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2878 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002879 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002880 }
2881
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002882#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2883
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002884/**
Allan Stephensc98da842016-11-11 15:45:03 -05002885 * INTERNAL_HIDDEN @endcond
2886 */
2887
2888/**
2889 * @addtogroup alert_apis
2890 * @{
2891 */
2892
2893/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002894 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002895 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002896 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002897 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002898 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002899 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002900 * @param name Name of the alert.
2901 * @param alert_handler Action to take when alert is sent. Specify either
2902 * the address of a function to be invoked by the system workqueue
2903 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2904 * K_ALERT_DEFAULT (which causes the alert to pend).
2905 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002906 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002907#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002908 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002909 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002910 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002911 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002912
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002913/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002914 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002915 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002916 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002917 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002918 * @param alert Address of the alert.
2919 * @param handler Action to take when alert is sent. Specify either the address
2920 * of a function to be invoked by the system workqueue thread,
2921 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2922 * K_ALERT_DEFAULT (which causes the alert to pend).
2923 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002924 *
2925 * @return N/A
2926 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002927extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2928 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002929
2930/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002931 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002932 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002933 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002934 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002935 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2936 *
2937 * @param alert Address of the alert.
2938 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002939 * or one of the special values K_NO_WAIT and K_FOREVER.
2940 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002941 * @retval 0 Alert received.
2942 * @retval -EBUSY Returned without waiting.
2943 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002944 */
Andrew Boie310e9872017-09-29 04:41:15 -07002945__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002946
2947/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002948 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002950 * This routine signals @a alert. The action specified for @a alert will
2951 * be taken, which may trigger the execution of an alert handler function
2952 * and/or cause the alert to pend (assuming the alert has not reached its
2953 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002954 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002955 * @note Can be called by ISRs.
2956 *
2957 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002958 *
2959 * @return N/A
2960 */
Andrew Boie310e9872017-09-29 04:41:15 -07002961__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002962
2963/**
Anas Nashif166f5192018-02-25 08:02:36 -06002964 * @}
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002965 */
2966
Allan Stephensc98da842016-11-11 15:45:03 -05002967/**
2968 * @cond INTERNAL_HIDDEN
2969 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002970
2971struct k_msgq {
2972 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002973 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002974 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002975 char *buffer_start;
2976 char *buffer_end;
2977 char *read_ptr;
2978 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002979 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002980
Anas Nashif2f203c22016-12-18 06:57:45 -05002981 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Andrew Boie0fe789f2018-04-12 18:35:56 -07002982 u8_t flags;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002983};
2984
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002985#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002986 { \
2987 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002988 .max_msgs = q_max_msgs, \
2989 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002990 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002991 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002992 .read_ptr = q_buffer, \
2993 .write_ptr = q_buffer, \
2994 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002995 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002996 }
2997
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002998#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2999
Andrew Boie0fe789f2018-04-12 18:35:56 -07003000#define K_MSGQ_FLAG_ALLOC BIT(0)
3001
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303002struct k_msgq_attrs {
3003 size_t msg_size;
3004 u32_t max_msgs;
3005 u32_t used_msgs;
3006};
3007
Peter Mitsis1da807e2016-10-06 11:36:59 -04003008/**
Allan Stephensc98da842016-11-11 15:45:03 -05003009 * INTERNAL_HIDDEN @endcond
3010 */
3011
3012/**
3013 * @defgroup msgq_apis Message Queue APIs
3014 * @ingroup kernel_apis
3015 * @{
3016 */
3017
3018/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003019 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003020 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003021 * The message queue's ring buffer contains space for @a q_max_msgs messages,
3022 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003023 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
3024 * message is similarly aligned to this boundary, @a q_msg_size must also be
3025 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003026 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003027 * The message queue can be accessed outside the module where it is defined
3028 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003029 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003030 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003031 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003032 * @param q_name Name of the message queue.
3033 * @param q_msg_size Message size (in bytes).
3034 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06003035 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04003036 */
3037#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
Andrew Boie0fe789f2018-04-12 18:35:56 -07003038 static char __kernel_noinit __aligned(q_align) \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003039 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003040 struct k_msgq q_name \
3041 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003042 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04003043 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003044
Peter Mitsisd7a37502016-10-13 11:37:40 -04003045/**
3046 * @brief Initialize a message queue.
3047 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003048 * This routine initializes a message queue object, prior to its first use.
3049 *
Allan Stephensda827222016-11-09 14:23:58 -06003050 * The message queue's ring buffer must contain space for @a max_msgs messages,
3051 * each of which is @a msg_size bytes long. The buffer must be aligned to an
3052 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
3053 * that each message is similarly aligned to this boundary, @a q_msg_size
3054 * must also be a multiple of N.
3055 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003056 * @param q Address of the message queue.
3057 * @param buffer Pointer to ring buffer that holds queued messages.
3058 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04003059 * @param max_msgs Maximum number of messages that can be queued.
3060 *
3061 * @return N/A
3062 */
Andrew Boie0fe789f2018-04-12 18:35:56 -07003063void k_msgq_init(struct k_msgq *q, char *buffer, size_t msg_size,
3064 u32_t max_msgs);
3065
3066/**
3067 * @brief Initialize a message queue.
3068 *
3069 * This routine initializes a message queue object, prior to its first use,
3070 * allocating its internal ring buffer from the calling thread's resource
3071 * pool.
3072 *
3073 * Memory allocated for the ring buffer can be released by calling
3074 * k_msgq_cleanup(), or if userspace is enabled and the msgq object loses
3075 * all of its references.
3076 *
3077 * @param q Address of the message queue.
3078 * @param msg_size Message size (in bytes).
3079 * @param max_msgs Maximum number of messages that can be queued.
3080 *
3081 * @return 0 on success, -ENOMEM if there was insufficient memory in the
3082 * thread's resource pool, or -EINVAL if the size parameters cause
3083 * an integer overflow.
3084 */
3085__syscall int k_msgq_alloc_init(struct k_msgq *q, size_t msg_size,
3086 u32_t max_msgs);
3087
3088
3089void k_msgq_cleanup(struct k_msgq *q);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003090
3091/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003092 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003094 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003095 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003096 * @note Can be called by ISRs.
3097 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003098 * @param q Address of the message queue.
3099 * @param data Pointer to the message.
3100 * @param timeout Waiting period to add the message (in milliseconds),
3101 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003102 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003103 * @retval 0 Message sent.
3104 * @retval -ENOMSG Returned without waiting or queue purged.
3105 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003106 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003107__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003108
3109/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003110 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003112 * This routine receives a message from message queue @a q in a "first in,
3113 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003114 *
Allan Stephensc98da842016-11-11 15:45:03 -05003115 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05003116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003117 * @param q Address of the message queue.
3118 * @param data Address of area to hold the received message.
3119 * @param timeout Waiting period to receive the message (in milliseconds),
3120 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003121 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003122 * @retval 0 Message received.
3123 * @retval -ENOMSG Returned without waiting.
3124 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003125 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003126__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04003127
3128/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003129 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003130 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003131 * This routine discards all unreceived messages in a message queue's ring
3132 * buffer. Any threads that are blocked waiting to send a message to the
3133 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003135 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003136 *
3137 * @return N/A
3138 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003139__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003140
Peter Mitsis67be2492016-10-07 11:44:34 -04003141/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003142 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04003143 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003144 * This routine returns the number of unused entries in a message queue's
3145 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04003146 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003147 * @param q Address of the message queue.
3148 *
3149 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04003150 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003151__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
3152
Youvedeep Singh188c1ab2018-03-19 20:02:40 +05303153/**
3154 * @brief Get basic attributes of a message queue.
3155 *
3156 * This routine fetches basic attributes of message queue into attr argument.
3157 *
3158 * @param q Address of the message queue.
3159 * @param attrs pointer to message queue attribute structure.
3160 *
3161 * @return N/A
3162 */
3163__syscall void k_msgq_get_attrs(struct k_msgq *q, struct k_msgq_attrs *attrs);
3164
3165
Andrew Boie82edb6e2017-10-02 10:53:06 -07003166static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04003167{
3168 return q->max_msgs - q->used_msgs;
3169}
3170
Peter Mitsisd7a37502016-10-13 11:37:40 -04003171/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003172 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003174 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003175 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003176 * @param q Address of the message queue.
3177 *
3178 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04003179 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07003180__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
3181
3182static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003183{
3184 return q->used_msgs;
3185}
3186
Anas Nashif166f5192018-02-25 08:02:36 -06003187/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003188
3189/**
3190 * @defgroup mem_pool_apis Memory Pool APIs
3191 * @ingroup kernel_apis
3192 * @{
3193 */
3194
Andy Ross73cb9582017-05-09 10:42:39 -07003195/* Note on sizing: the use of a 20 bit field for block means that,
3196 * assuming a reasonable minimum block size of 16 bytes, we're limited
3197 * to 16M of memory managed by a single pool. Long term it would be
3198 * good to move to a variable bit size based on configuration.
3199 */
3200struct k_mem_block_id {
3201 u32_t pool : 8;
3202 u32_t level : 4;
3203 u32_t block : 20;
3204};
3205
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003206struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003207 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003208 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003209};
3210
Anas Nashif166f5192018-02-25 08:02:36 -06003211/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003212
3213/**
3214 * @defgroup mailbox_apis Mailbox APIs
3215 * @ingroup kernel_apis
3216 * @{
3217 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003218
3219struct k_mbox_msg {
3220 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003221 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003222 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003223 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003224 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003225 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003226 /** sender's message data buffer */
3227 void *tx_data;
3228 /** internal use only - needed for legacy API support */
3229 void *_rx_data;
3230 /** message data block descriptor */
3231 struct k_mem_block tx_block;
3232 /** source thread id */
3233 k_tid_t rx_source_thread;
3234 /** target thread id */
3235 k_tid_t tx_target_thread;
3236 /** internal use only - thread waiting on send (may be a dummy) */
3237 k_tid_t _syncing_thread;
3238#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3239 /** internal use only - semaphore used during asynchronous send */
3240 struct k_sem *_async_sem;
3241#endif
3242};
3243
Allan Stephensc98da842016-11-11 15:45:03 -05003244/**
3245 * @cond INTERNAL_HIDDEN
3246 */
3247
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003248struct k_mbox {
3249 _wait_q_t tx_msg_queue;
3250 _wait_q_t rx_msg_queue;
3251
Anas Nashif2f203c22016-12-18 06:57:45 -05003252 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003253};
3254
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003255#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003256 { \
3257 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3258 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003259 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003260 }
3261
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003262#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3263
Peter Mitsis12092702016-10-14 12:57:23 -04003264/**
Allan Stephensc98da842016-11-11 15:45:03 -05003265 * INTERNAL_HIDDEN @endcond
3266 */
3267
3268/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003269 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003270 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003271 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003272 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003273 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003274 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003275 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003276 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003277#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003278 struct k_mbox name \
3279 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003280 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003281
Peter Mitsis12092702016-10-14 12:57:23 -04003282/**
3283 * @brief Initialize a mailbox.
3284 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003285 * This routine initializes a mailbox object, prior to its first use.
3286 *
3287 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003288 *
3289 * @return N/A
3290 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003291extern void k_mbox_init(struct k_mbox *mbox);
3292
Peter Mitsis12092702016-10-14 12:57:23 -04003293/**
3294 * @brief Send a mailbox message in a synchronous manner.
3295 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003296 * This routine sends a message to @a mbox and waits for a receiver to both
3297 * receive and process it. The message data may be in a buffer, in a memory
3298 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003299 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003300 * @param mbox Address of the mailbox.
3301 * @param tx_msg Address of the transmit message descriptor.
3302 * @param timeout Waiting period for the message to be received (in
3303 * milliseconds), or one of the special values K_NO_WAIT
3304 * and K_FOREVER. Once the message has been received,
3305 * this routine waits as long as necessary for the message
3306 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003307 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003308 * @retval 0 Message sent.
3309 * @retval -ENOMSG Returned without waiting.
3310 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003311 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003312extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003313 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003314
Peter Mitsis12092702016-10-14 12:57:23 -04003315/**
3316 * @brief Send a mailbox message in an asynchronous manner.
3317 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003318 * This routine sends a message to @a mbox without waiting for a receiver
3319 * to process it. The message data may be in a buffer, in a memory pool block,
3320 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3321 * will be given when the message has been both received and completely
3322 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003323 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003324 * @param mbox Address of the mailbox.
3325 * @param tx_msg Address of the transmit message descriptor.
3326 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003327 *
3328 * @return N/A
3329 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003330extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003331 struct k_sem *sem);
3332
Peter Mitsis12092702016-10-14 12:57:23 -04003333/**
3334 * @brief Receive a mailbox message.
3335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003336 * This routine receives a message from @a mbox, then optionally retrieves
3337 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003338 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003339 * @param mbox Address of the mailbox.
3340 * @param rx_msg Address of the receive message descriptor.
3341 * @param buffer Address of the buffer to receive data, or NULL to defer data
3342 * retrieval and message disposal until later.
3343 * @param timeout Waiting period for a message to be received (in
3344 * milliseconds), or one of the special values K_NO_WAIT
3345 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003346 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003347 * @retval 0 Message received.
3348 * @retval -ENOMSG Returned without waiting.
3349 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003350 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003351extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003352 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003353
3354/**
3355 * @brief Retrieve mailbox message data into a buffer.
3356 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003357 * This routine completes the processing of a received message by retrieving
3358 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003359 *
3360 * Alternatively, this routine can be used to dispose of a received message
3361 * without retrieving its data.
3362 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003363 * @param rx_msg Address of the receive message descriptor.
3364 * @param buffer Address of the buffer to receive data, or NULL to discard
3365 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003366 *
3367 * @return N/A
3368 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003369extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003370
3371/**
3372 * @brief Retrieve mailbox message data into a memory pool block.
3373 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003374 * This routine completes the processing of a received message by retrieving
3375 * its data into a memory pool block, then disposing of the message.
3376 * The memory pool block that results from successful retrieval must be
3377 * returned to the pool once the data has been processed, even in cases
3378 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003379 *
3380 * Alternatively, this routine can be used to dispose of a received message
3381 * without retrieving its data. In this case there is no need to return a
3382 * memory pool block to the pool.
3383 *
3384 * This routine allocates a new memory pool block for the data only if the
3385 * data is not already in one. If a new block cannot be allocated, the routine
3386 * returns a failure code and the received message is left unchanged. This
3387 * permits the caller to reattempt data retrieval at a later time or to dispose
3388 * of the received message without retrieving its data.
3389 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003390 * @param rx_msg Address of a receive message descriptor.
3391 * @param pool Address of memory pool, or NULL to discard data.
3392 * @param block Address of the area to hold memory pool block info.
3393 * @param timeout Waiting period to wait for a memory pool block (in
3394 * milliseconds), or one of the special values K_NO_WAIT
3395 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003396 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003397 * @retval 0 Data retrieved.
3398 * @retval -ENOMEM Returned without waiting.
3399 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003400 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003401extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003402 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003403 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003404
Anas Nashif166f5192018-02-25 08:02:36 -06003405/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003406
3407/**
3408 * @cond INTERNAL_HIDDEN
3409 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003410
Andrew Boie44fe8122018-04-12 17:38:12 -07003411#define K_PIPE_FLAG_ALLOC BIT(0) /* Buffer was allocated */
3412
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003413struct k_pipe {
3414 unsigned char *buffer; /* Pipe buffer: may be NULL */
3415 size_t size; /* Buffer size */
3416 size_t bytes_used; /* # bytes used in buffer */
3417 size_t read_index; /* Where in buffer to read from */
3418 size_t write_index; /* Where in buffer to write */
3419
3420 struct {
3421 _wait_q_t readers; /* Reader wait queue */
3422 _wait_q_t writers; /* Writer wait queue */
3423 } wait_q;
3424
Anas Nashif2f203c22016-12-18 06:57:45 -05003425 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Andrew Boie44fe8122018-04-12 17:38:12 -07003426 u8_t flags; /* Flags */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003427};
3428
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003429#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003430 { \
3431 .buffer = pipe_buffer, \
3432 .size = pipe_buffer_size, \
3433 .bytes_used = 0, \
3434 .read_index = 0, \
3435 .write_index = 0, \
3436 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3437 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003438 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003439 }
3440
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003441#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3442
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003443/**
Allan Stephensc98da842016-11-11 15:45:03 -05003444 * INTERNAL_HIDDEN @endcond
3445 */
3446
3447/**
3448 * @defgroup pipe_apis Pipe APIs
3449 * @ingroup kernel_apis
3450 * @{
3451 */
3452
3453/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003454 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003455 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003456 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003457 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003458 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003459 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003460 * @param name Name of the pipe.
3461 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3462 * or zero if no ring buffer is used.
3463 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003464 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003465#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3466 static unsigned char __kernel_noinit __aligned(pipe_align) \
3467 _k_pipe_buf_##name[pipe_buffer_size]; \
3468 struct k_pipe name \
3469 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003470 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003471
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003472/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003473 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003475 * This routine initializes a pipe object, prior to its first use.
3476 *
3477 * @param pipe Address of the pipe.
3478 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3479 * is used.
3480 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3481 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003482 *
3483 * @return N/A
3484 */
Andrew Boie44fe8122018-04-12 17:38:12 -07003485void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer, size_t size);
3486
3487/**
3488 * @brief Release a pipe's allocated buffer
3489 *
3490 * If a pipe object was given a dynamically allocated buffer via
3491 * k_pipe_alloc_init(), this will free it. This function does nothing
3492 * if the buffer wasn't dynamically allocated.
3493 *
3494 * @param pipe Address of the pipe.
3495 */
3496void k_pipe_cleanup(struct k_pipe *pipe);
3497
3498/**
3499 * @brief Initialize a pipe and allocate a buffer for it
3500 *
3501 * Storage for the buffer region will be allocated from the calling thread's
3502 * resource pool. This memory will be released if k_pipe_cleanup() is called,
3503 * or userspace is enabled and the pipe object loses all references to it.
3504 *
3505 * This function should only be called on uninitialized pipe objects.
3506 *
3507 * @param pipe Address of the pipe.
3508 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3509 * buffer is used.
3510 * @retval 0 on success
3511 * @retval -ENOMEM if memory couln't be allocated
3512 */
3513__syscall int k_pipe_alloc_init(struct k_pipe *pipe, size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003514
3515/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003516 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003517 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003518 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003519 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003520 * @param pipe Address of the pipe.
3521 * @param data Address of data to write.
3522 * @param bytes_to_write Size of data (in bytes).
3523 * @param bytes_written Address of area to hold the number of bytes written.
3524 * @param min_xfer Minimum number of bytes to write.
3525 * @param timeout Waiting period to wait for the data to be written (in
3526 * milliseconds), or one of the special values K_NO_WAIT
3527 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003528 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003529 * @retval 0 At least @a min_xfer bytes of data were written.
3530 * @retval -EIO Returned without waiting; zero data bytes were written.
3531 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003532 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003533 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003534__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3535 size_t bytes_to_write, size_t *bytes_written,
3536 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003537
3538/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003539 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003540 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003541 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003542 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003543 * @param pipe Address of the pipe.
3544 * @param data Address to place the data read from pipe.
3545 * @param bytes_to_read Maximum number of data bytes to read.
3546 * @param bytes_read Address of area to hold the number of bytes read.
3547 * @param min_xfer Minimum number of data bytes to read.
3548 * @param timeout Waiting period to wait for the data to be read (in
3549 * milliseconds), or one of the special values K_NO_WAIT
3550 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003551 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003552 * @retval 0 At least @a min_xfer bytes of data were read.
3553 * @retval -EIO Returned without waiting; zero data bytes were read.
3554 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003555 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003556 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003557__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3558 size_t bytes_to_read, size_t *bytes_read,
3559 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003560
3561/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003562 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003563 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003564 * This routine writes the data contained in a memory block to @a pipe.
3565 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003566 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003568 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003569 * @param block Memory block containing data to send
3570 * @param size Number of data bytes in memory block to send
3571 * @param sem Semaphore to signal upon completion (else NULL)
3572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003573 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003574 */
3575extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3576 size_t size, struct k_sem *sem);
3577
Anas Nashif166f5192018-02-25 08:02:36 -06003578/** @} */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003579
Allan Stephensc98da842016-11-11 15:45:03 -05003580/**
3581 * @cond INTERNAL_HIDDEN
3582 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003583
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003584struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003585 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003586 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003587 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003588 char *buffer;
3589 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003590 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003591
Anas Nashif2f203c22016-12-18 06:57:45 -05003592 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003593};
3594
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003595#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003596 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003597 { \
3598 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003599 .num_blocks = slab_num_blocks, \
3600 .block_size = slab_block_size, \
3601 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003602 .free_list = NULL, \
3603 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003604 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003605 }
3606
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003607#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3608
3609
Peter Mitsis578f9112016-10-07 13:50:31 -04003610/**
Allan Stephensc98da842016-11-11 15:45:03 -05003611 * INTERNAL_HIDDEN @endcond
3612 */
3613
3614/**
3615 * @defgroup mem_slab_apis Memory Slab APIs
3616 * @ingroup kernel_apis
3617 * @{
3618 */
3619
3620/**
Allan Stephensda827222016-11-09 14:23:58 -06003621 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003622 *
Allan Stephensda827222016-11-09 14:23:58 -06003623 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003624 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003625 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3626 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003627 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003628 *
Allan Stephensda827222016-11-09 14:23:58 -06003629 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003630 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003631 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003632 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003633 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003634 * @param name Name of the memory slab.
3635 * @param slab_block_size Size of each memory block (in bytes).
3636 * @param slab_num_blocks Number memory blocks.
3637 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003638 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003639#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3640 char __noinit __aligned(slab_align) \
3641 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3642 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003643 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003644 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003645 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003646
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003647/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003648 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003649 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003650 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003651 *
Allan Stephensda827222016-11-09 14:23:58 -06003652 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3653 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3654 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3655 * To ensure that each memory block is similarly aligned to this boundary,
3656 * @a slab_block_size must also be a multiple of N.
3657 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003658 * @param slab Address of the memory slab.
3659 * @param buffer Pointer to buffer used for the memory blocks.
3660 * @param block_size Size of each memory block (in bytes).
3661 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003662 *
3663 * @return N/A
3664 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003665extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003666 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003667
3668/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003669 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003670 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003671 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003672 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003673 * @param slab Address of the memory slab.
3674 * @param mem Pointer to block address area.
3675 * @param timeout Maximum time to wait for operation to complete
3676 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3677 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003678 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003679 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003680 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003681 * @retval -ENOMEM Returned without waiting.
3682 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003683 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003684extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003685 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003686
3687/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003688 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003689 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003690 * This routine releases a previously allocated memory block back to its
3691 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003692 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003693 * @param slab Address of the memory slab.
3694 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003695 *
3696 * @return N/A
3697 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003698extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003699
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003700/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003701 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003702 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003703 * This routine gets the number of memory blocks that are currently
3704 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003706 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003707 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003708 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003709 */
Kumar Galacc334c72017-04-21 10:55:34 -05003710static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003711{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003712 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003713}
3714
Peter Mitsisc001aa82016-10-13 13:53:37 -04003715/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003716 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003717 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003718 * This routine gets the number of memory blocks that are currently
3719 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003720 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003721 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003722 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003723 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003724 */
Kumar Galacc334c72017-04-21 10:55:34 -05003725static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003726{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003727 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003728}
3729
Anas Nashif166f5192018-02-25 08:02:36 -06003730/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003731
3732/**
3733 * @cond INTERNAL_HIDDEN
3734 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003735
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003736struct k_mem_pool {
Andrew Boieaa6de292018-03-06 17:12:37 -08003737 struct sys_mem_pool_base base;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003738 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003739};
3740
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003741/**
Allan Stephensc98da842016-11-11 15:45:03 -05003742 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003743 */
3744
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003745/**
Allan Stephensc98da842016-11-11 15:45:03 -05003746 * @addtogroup mem_pool_apis
3747 * @{
3748 */
3749
3750/**
3751 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003752 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003753 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3754 * long. The memory pool allows blocks to be repeatedly partitioned into
3755 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003756 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003757 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003758 * If the pool is to be accessed outside the module where it is defined, it
3759 * can be declared via
3760 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003761 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003762 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003763 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003764 * @param minsz Size of the smallest blocks in the pool (in bytes).
3765 * @param maxsz Size of the largest blocks in the pool (in bytes).
3766 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003767 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003768 */
Andy Ross73cb9582017-05-09 10:42:39 -07003769#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3770 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3771 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
Andrew Boieaa6de292018-03-06 17:12:37 -08003772 struct sys_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
Andy Ross73cb9582017-05-09 10:42:39 -07003773 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
Andrew Boieaa6de292018-03-06 17:12:37 -08003774 .base = { \
3775 .buf = _mpool_buf_##name, \
3776 .max_sz = maxsz, \
3777 .n_max = nmax, \
3778 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3779 .levels = _mpool_lvls_##name, \
3780 .flags = SYS_MEM_POOL_KERNEL \
3781 } \
Andy Ross73cb9582017-05-09 10:42:39 -07003782 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003783
Peter Mitsis937042c2016-10-13 13:18:26 -04003784/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003785 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003786 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003787 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003788 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003789 * @param pool Address of the memory pool.
3790 * @param block Pointer to block descriptor for the allocated memory.
3791 * @param size Amount of memory to allocate (in bytes).
3792 * @param timeout Maximum time to wait for operation to complete
3793 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3794 * or K_FOREVER to wait as long as necessary.
3795 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003796 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003797 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003798 * @retval -ENOMEM Returned without waiting.
3799 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003800 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003801extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003802 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003803
3804/**
Andrew Boiea2480bd2018-04-12 16:59:02 -07003805 * @brief Allocate memory from a memory pool with malloc() semantics
3806 *
3807 * Such memory must be released using k_free().
3808 *
3809 * @param pool Address of the memory pool.
3810 * @param size Amount of memory to allocate (in bytes).
3811 * @return Address of the allocated memory if successful, otherwise NULL
3812 */
3813extern void *k_mem_pool_malloc(struct k_mem_pool *pool, size_t size);
3814
3815/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003816 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003817 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003818 * This routine releases a previously allocated memory block back to its
3819 * memory pool.
3820 *
3821 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003822 *
3823 * @return N/A
3824 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003825extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003826
3827/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003828 * @brief Free memory allocated from a memory pool.
3829 *
3830 * This routine releases a previously allocated memory block back to its
3831 * memory pool
3832 *
3833 * @param id Memory block identifier.
3834 *
3835 * @return N/A
3836 */
3837extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3838
3839/**
Anas Nashif166f5192018-02-25 08:02:36 -06003840 * @}
Allan Stephensc98da842016-11-11 15:45:03 -05003841 */
3842
3843/**
3844 * @defgroup heap_apis Heap Memory Pool APIs
3845 * @ingroup kernel_apis
3846 * @{
3847 */
3848
3849/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003850 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003852 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003853 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003855 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003857 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003858 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003859extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003860
3861/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003862 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003863 *
3864 * This routine provides traditional free() semantics. The memory being
Andrew Boiea2480bd2018-04-12 16:59:02 -07003865 * returned must have been allocated from the heap memory pool or
3866 * k_mem_pool_malloc().
Peter Mitsis937042c2016-10-13 13:18:26 -04003867 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003868 * If @a ptr is NULL, no operation is performed.
3869 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003870 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003871 *
3872 * @return N/A
3873 */
3874extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003875
Allan Stephensc98da842016-11-11 15:45:03 -05003876/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003877 * @brief Allocate memory from heap, array style
3878 *
3879 * This routine provides traditional calloc() semantics. Memory is
3880 * allocated from the heap memory pool and zeroed.
3881 *
3882 * @param nmemb Number of elements in the requested array
3883 * @param size Size of each array element (in bytes).
3884 *
3885 * @return Address of the allocated memory if successful; otherwise NULL.
3886 */
3887extern void *k_calloc(size_t nmemb, size_t size);
3888
Anas Nashif166f5192018-02-25 08:02:36 -06003889/** @} */
Allan Stephensc98da842016-11-11 15:45:03 -05003890
Benjamin Walshacc68c12017-01-29 18:57:45 -05003891/* polling API - PRIVATE */
3892
Benjamin Walshb0179862017-02-02 16:39:57 -05003893#ifdef CONFIG_POLL
3894#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3895#else
3896#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3897#endif
3898
Benjamin Walshacc68c12017-01-29 18:57:45 -05003899/* private - implementation data created as needed, per-type */
3900struct _poller {
3901 struct k_thread *thread;
3902};
3903
3904/* private - types bit positions */
3905enum _poll_types_bits {
3906 /* can be used to ignore an event */
3907 _POLL_TYPE_IGNORE,
3908
3909 /* to be signaled by k_poll_signal() */
3910 _POLL_TYPE_SIGNAL,
3911
3912 /* semaphore availability */
3913 _POLL_TYPE_SEM_AVAILABLE,
3914
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003915 /* queue/fifo/lifo data availability */
3916 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003917
3918 _POLL_NUM_TYPES
3919};
3920
3921#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3922
3923/* private - states bit positions */
3924enum _poll_states_bits {
3925 /* default state when creating event */
3926 _POLL_STATE_NOT_READY,
3927
Benjamin Walshacc68c12017-01-29 18:57:45 -05003928 /* signaled by k_poll_signal() */
3929 _POLL_STATE_SIGNALED,
3930
3931 /* semaphore is available */
3932 _POLL_STATE_SEM_AVAILABLE,
3933
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003934 /* data is available to read on queue/fifo/lifo */
3935 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003936
3937 _POLL_NUM_STATES
3938};
3939
3940#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3941
3942#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003943 (32 - (0 \
3944 + 8 /* tag */ \
3945 + _POLL_NUM_TYPES \
3946 + _POLL_NUM_STATES \
3947 + 1 /* modes */ \
3948 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003949
Benjamin Walshacc68c12017-01-29 18:57:45 -05003950/* end of polling API - PRIVATE */
3951
3952
3953/**
3954 * @defgroup poll_apis Async polling APIs
3955 * @ingroup kernel_apis
3956 * @{
3957 */
3958
3959/* Public polling API */
3960
3961/* public - values for k_poll_event.type bitfield */
3962#define K_POLL_TYPE_IGNORE 0
3963#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3964#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003965#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3966#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003967
3968/* public - polling modes */
3969enum k_poll_modes {
3970 /* polling thread does not take ownership of objects when available */
3971 K_POLL_MODE_NOTIFY_ONLY = 0,
3972
3973 K_POLL_NUM_MODES
3974};
3975
3976/* public - values for k_poll_event.state bitfield */
3977#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003978#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3979#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003980#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3981#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003982
3983/* public - poll signal object */
3984struct k_poll_signal {
3985 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003986 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003987
3988 /*
3989 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3990 * user resets it to 0.
3991 */
3992 unsigned int signaled;
3993
3994 /* custom result value passed to k_poll_signal() if needed */
3995 int result;
3996};
3997
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003998#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003999 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004000 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004001 .signaled = 0, \
4002 .result = 0, \
4003 }
4004
4005struct k_poll_event {
4006 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004007 sys_dnode_t _node;
4008
4009 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004010 struct _poller *poller;
4011
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004012 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05004013 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004014
Benjamin Walshacc68c12017-01-29 18:57:45 -05004015 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004016 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004017
4018 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05004019 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004020
4021 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05004022 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004023
4024 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05004025 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004026
4027 /* per-type data */
4028 union {
4029 void *obj;
4030 struct k_poll_signal *signal;
4031 struct k_sem *sem;
4032 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02004033 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05004034 };
4035};
4036
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004037#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004038 { \
4039 .poller = NULL, \
4040 .type = event_type, \
4041 .state = K_POLL_STATE_NOT_READY, \
4042 .mode = event_mode, \
4043 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05004044 { .obj = event_obj }, \
4045 }
4046
4047#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
4048 event_tag) \
4049 { \
4050 .type = event_type, \
4051 .tag = event_tag, \
4052 .state = K_POLL_STATE_NOT_READY, \
4053 .mode = event_mode, \
4054 .unused = 0, \
4055 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05004056 }
4057
4058/**
4059 * @brief Initialize one struct k_poll_event instance
4060 *
4061 * After this routine is called on a poll event, the event it ready to be
4062 * placed in an event array to be passed to k_poll().
4063 *
4064 * @param event The event to initialize.
4065 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
4066 * values. Only values that apply to the same object being polled
4067 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
4068 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03004069 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004070 * @param obj Kernel object or poll signal.
4071 *
4072 * @return N/A
4073 */
4074
Kumar Galacc334c72017-04-21 10:55:34 -05004075extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05004076 int mode, void *obj);
4077
4078/**
4079 * @brief Wait for one or many of multiple poll events to occur
4080 *
4081 * This routine allows a thread to wait concurrently for one or many of
4082 * multiple poll events to have occurred. Such events can be a kernel object
4083 * being available, like a semaphore, or a poll signal event.
4084 *
4085 * When an event notifies that a kernel object is available, the kernel object
4086 * is not "given" to the thread calling k_poll(): it merely signals the fact
4087 * that the object was available when the k_poll() call was in effect. Also,
4088 * all threads trying to acquire an object the regular way, i.e. by pending on
4089 * the object, have precedence over the thread polling on the object. This
4090 * means that the polling thread will never get the poll event on an object
4091 * until the object becomes available and its pend queue is empty. For this
4092 * reason, the k_poll() call is more effective when the objects being polled
4093 * only have one thread, the polling thread, trying to acquire them.
4094 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03004095 * When k_poll() returns 0, the caller should loop on all the events that were
4096 * passed to k_poll() and check the state field for the values that were
4097 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004098 *
4099 * Before being reused for another call to k_poll(), the user has to reset the
4100 * state field to K_POLL_STATE_NOT_READY.
4101 *
4102 * @param events An array of pointers to events to be polled for.
4103 * @param num_events The number of events in the array.
4104 * @param timeout Waiting period for an event to be ready (in milliseconds),
4105 * or one of the special values K_NO_WAIT and K_FOREVER.
4106 *
4107 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004108 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02004109 * @retval -EINTR Poller thread has been interrupted.
Benjamin Walshacc68c12017-01-29 18:57:45 -05004110 */
4111
4112extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05004113 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004114
4115/**
Benjamin Walsha304f162017-02-02 16:46:09 -05004116 * @brief Initialize a poll signal object.
4117 *
4118 * Ready a poll signal object to be signaled via k_poll_signal().
4119 *
4120 * @param signal A poll signal.
4121 *
4122 * @return N/A
4123 */
4124
4125extern void k_poll_signal_init(struct k_poll_signal *signal);
4126
4127/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05004128 * @brief Signal a poll signal object.
4129 *
4130 * This routine makes ready a poll signal, which is basically a poll event of
4131 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
4132 * made ready to run. A @a result value can be specified.
4133 *
4134 * The poll signal contains a 'signaled' field that, when set by
4135 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
4136 * be reset by the user before being passed again to k_poll() or k_poll() will
4137 * consider it being signaled, and will return immediately.
4138 *
4139 * @param signal A poll signal.
4140 * @param result The value to store in the result field of the signal.
4141 *
4142 * @retval 0 The signal was delivered successfully.
4143 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
4144 */
4145
4146extern int k_poll_signal(struct k_poll_signal *signal, int result);
4147
Anas Nashif954d5502018-02-25 08:37:28 -06004148/**
4149 * @internal
4150 */
Andy Ross8606fab2018-03-26 10:54:40 -07004151extern void _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05004152
Anas Nashif166f5192018-02-25 08:02:36 -06004153/** @} */
Benjamin Walshacc68c12017-01-29 18:57:45 -05004154
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05004155/**
4156 * @brief Make the CPU idle.
4157 *
4158 * This function makes the CPU idle until an event wakes it up.
4159 *
4160 * In a regular system, the idle thread should be the only thread responsible
4161 * for making the CPU idle and triggering any type of power management.
4162 * However, in some more constrained systems, such as a single-threaded system,
4163 * the only thread would be responsible for this if needed.
4164 *
4165 * @return N/A
4166 */
4167extern void k_cpu_idle(void);
4168
4169/**
4170 * @brief Make the CPU idle in an atomic fashion.
4171 *
4172 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4173 * must be done atomically before making the CPU idle.
4174 *
4175 * @param key Interrupt locking key obtained from irq_lock().
4176 *
4177 * @return N/A
4178 */
4179extern void k_cpu_atomic_idle(unsigned int key);
4180
Anas Nashif954d5502018-02-25 08:37:28 -06004181
4182/**
4183 * @internal
4184 */
Kumar Galacc334c72017-04-21 10:55:34 -05004185extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004186
Andrew Boiecdb94d62017-04-18 15:22:05 -07004187#ifdef _ARCH_EXCEPT
4188/* This archtecture has direct support for triggering a CPU exception */
4189#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4190#else
4191
Andrew Boiecdb94d62017-04-18 15:22:05 -07004192/* NOTE: This is the implementation for arches that do not implement
4193 * _ARCH_EXCEPT() to generate a real CPU exception.
4194 *
4195 * We won't have a real exception frame to determine the PC value when
4196 * the oops occurred, so print file and line number before we jump into
4197 * the fatal error handler.
4198 */
4199#define _k_except_reason(reason) do { \
4200 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4201 _NanoFatalErrorHandler(reason, &_default_esf); \
4202 CODE_UNREACHABLE; \
4203 } while (0)
4204
4205#endif /* _ARCH__EXCEPT */
4206
4207/**
4208 * @brief Fatally terminate a thread
4209 *
4210 * This should be called when a thread has encountered an unrecoverable
4211 * runtime condition and needs to terminate. What this ultimately
4212 * means is determined by the _fatal_error_handler() implementation, which
4213 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4214 *
4215 * If this is called from ISR context, the default system fatal error handler
4216 * will treat it as an unrecoverable system error, just like k_panic().
4217 */
4218#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4219
4220/**
4221 * @brief Fatally terminate the system
4222 *
4223 * This should be called when the Zephyr kernel has encountered an
4224 * unrecoverable runtime condition and needs to terminate. What this ultimately
4225 * means is determined by the _fatal_error_handler() implementation, which
4226 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4227 */
4228#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4229
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004230/*
4231 * private APIs that are utilized by one or more public APIs
4232 */
4233
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004234#ifdef CONFIG_MULTITHREADING
Anas Nashif954d5502018-02-25 08:37:28 -06004235/**
4236 * @internal
4237 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004238extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004239#else
Anas Nashif954d5502018-02-25 08:37:28 -06004240/**
4241 * @internal
4242 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004243#define _init_static_threads() do { } while ((0))
4244#endif
4245
Anas Nashif954d5502018-02-25 08:37:28 -06004246/**
4247 * @internal
4248 */
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004249extern int _is_thread_essential(void);
Anas Nashif954d5502018-02-25 08:37:28 -06004250/**
4251 * @internal
4252 */
Allan Stephens1342adb2016-11-03 13:54:53 -05004253extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004254
Andrew Boiedc5d9352017-06-02 12:56:47 -07004255/* arch/cpu.h may declare an architecture or platform-specific macro
4256 * for properly declaring stacks, compatible with MMU/MPU constraints if
4257 * enabled
4258 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004259
4260/**
4261 * @brief Obtain an extern reference to a stack
4262 *
4263 * This macro properly brings the symbol of a thread stack declared
4264 * elsewhere into scope.
4265 *
4266 * @param sym Thread stack symbol name
4267 */
4268#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4269
Andrew Boiedc5d9352017-06-02 12:56:47 -07004270#ifdef _ARCH_THREAD_STACK_DEFINE
4271#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4272#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4273 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4274#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4275#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004276static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004277{
4278 return _ARCH_THREAD_STACK_BUFFER(sym);
4279}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004280#else
4281/**
4282 * @brief Declare a toplevel thread stack memory region
4283 *
4284 * This declares a region of memory suitable for use as a thread's stack.
4285 *
4286 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4287 * 'noinit' section so that it isn't zeroed at boot
4288 *
Andrew Boie507852a2017-07-25 18:47:07 -07004289 * The declared symbol will always be a k_thread_stack_t which can be passed to
4290 * k_thread_create, but should otherwise not be manipulated. If the buffer
4291 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004292 *
4293 * It is legal to precede this definition with the 'static' keyword.
4294 *
4295 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4296 * parameter of k_thread_create(), it may not be the same as the
4297 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4298 *
4299 * @param sym Thread stack symbol name
4300 * @param size Size of the stack memory region
4301 */
4302#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004303 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004304
4305/**
4306 * @brief Declare a toplevel array of thread stack memory regions
4307 *
4308 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4309 * definition for additional details and constraints.
4310 *
4311 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4312 * 'noinit' section so that it isn't zeroed at boot
4313 *
4314 * @param sym Thread stack symbol name
4315 * @param nmemb Number of stacks to declare
4316 * @param size Size of the stack memory region
4317 */
4318
4319#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004320 struct _k_thread_stack_element __noinit \
4321 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004322
4323/**
4324 * @brief Declare an embedded stack memory region
4325 *
4326 * Used for stacks embedded within other data structures. Use is highly
4327 * discouraged but in some cases necessary. For memory protection scenarios,
4328 * it is very important that any RAM preceding this member not be writable
4329 * by threads else a stack overflow will lead to silent corruption. In other
4330 * words, the containing data structure should live in RAM owned by the kernel.
4331 *
4332 * @param sym Thread stack symbol name
4333 * @param size Size of the stack memory region
4334 */
4335#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004336 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004337
4338/**
4339 * @brief Return the size in bytes of a stack memory region
4340 *
4341 * Convenience macro for passing the desired stack size to k_thread_create()
4342 * since the underlying implementation may actually create something larger
4343 * (for instance a guard area).
4344 *
4345 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004346 * passed to K_THREAD_STACK_DEFINE.
4347 *
4348 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4349 * it is not guaranteed to return the original value since each array
4350 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004351 *
4352 * @param sym Stack memory symbol
4353 * @return Size of the stack
4354 */
4355#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4356
4357/**
4358 * @brief Get a pointer to the physical stack buffer
4359 *
4360 * Convenience macro to get at the real underlying stack buffer used by
4361 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4362 * This is really only intended for diagnostic tools which want to examine
4363 * stack memory contents.
4364 *
4365 * @param sym Declared stack symbol name
4366 * @return The buffer itself, a char *
4367 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004368static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004369{
4370 return (char *)sym;
4371}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004372
4373#endif /* _ARCH_DECLARE_STACK */
4374
Chunlin Hane9c97022017-07-07 20:29:30 +08004375/**
4376 * @defgroup mem_domain_apis Memory domain APIs
4377 * @ingroup kernel_apis
4378 * @{
4379 */
4380
4381/** @def MEM_PARTITION_ENTRY
4382 * @brief Used to declare a memory partition entry
4383 */
4384#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4385 {\
4386 .start = _start, \
4387 .size = _size, \
4388 .attr = _attr, \
4389 }
4390
4391/** @def K_MEM_PARTITION_DEFINE
4392 * @brief Used to declare a memory partition
4393 */
4394#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4395#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4396 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304397 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004398 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4399#else
4400#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304401 __kernel struct k_mem_partition name =\
Chunlin Hane9c97022017-07-07 20:29:30 +08004402 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4403#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4404
Chunlin Hane9c97022017-07-07 20:29:30 +08004405/* memory partition */
4406struct k_mem_partition {
4407 /* start address of memory partition */
4408 u32_t start;
4409 /* size of memory partition */
4410 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304411#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004412 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304413 k_mem_partition_attr_t attr;
4414#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004415};
4416
Adithya Baglody3a6d72e2018-02-13 16:44:44 +05304417/* memory domian
4418 * Note: Always declare this structure with __kernel prefix
4419 */
Chunlin Hane9c97022017-07-07 20:29:30 +08004420struct k_mem_domain {
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304421#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004422 /* partitions in the domain */
4423 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304424#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004425 /* domain q */
4426 sys_dlist_t mem_domain_q;
Leandro Pereira08de6582018-02-28 14:22:57 -08004427 /* number of partitions in the domain */
4428 u8_t num_partitions;
Chunlin Hane9c97022017-07-07 20:29:30 +08004429};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304430
Chunlin Hane9c97022017-07-07 20:29:30 +08004431
4432/**
4433 * @brief Initialize a memory domain.
4434 *
4435 * Initialize a memory domain with given name and memory partitions.
4436 *
4437 * @param domain The memory domain to be initialized.
4438 * @param num_parts The number of array items of "parts" parameter.
4439 * @param parts An array of pointers to the memory partitions. Can be NULL
4440 * if num_parts is zero.
4441 */
4442
Leandro Pereira08de6582018-02-28 14:22:57 -08004443extern void k_mem_domain_init(struct k_mem_domain *domain, u8_t num_parts,
Chunlin Hane9c97022017-07-07 20:29:30 +08004444 struct k_mem_partition *parts[]);
4445/**
4446 * @brief Destroy a memory domain.
4447 *
4448 * Destroy a memory domain.
4449 *
4450 * @param domain The memory domain to be destroyed.
4451 */
4452
4453extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4454
4455/**
4456 * @brief Add a memory partition into a memory domain.
4457 *
4458 * Add a memory partition into a memory domain.
4459 *
4460 * @param domain The memory domain to be added a memory partition.
4461 * @param part The memory partition to be added
4462 */
4463
4464extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4465 struct k_mem_partition *part);
4466
4467/**
4468 * @brief Remove a memory partition from a memory domain.
4469 *
4470 * Remove a memory partition from a memory domain.
4471 *
4472 * @param domain The memory domain to be removed a memory partition.
4473 * @param part The memory partition to be removed
4474 */
4475
4476extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4477 struct k_mem_partition *part);
4478
4479/**
4480 * @brief Add a thread into a memory domain.
4481 *
4482 * Add a thread into a memory domain.
4483 *
4484 * @param domain The memory domain that the thread is going to be added into.
4485 * @param thread ID of thread going to be added into the memory domain.
4486 */
4487
4488extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4489 k_tid_t thread);
4490
4491/**
4492 * @brief Remove a thread from its memory domain.
4493 *
4494 * Remove a thread from its memory domain.
4495 *
4496 * @param thread ID of thread going to be removed from its memory domain.
4497 */
4498
4499extern void k_mem_domain_remove_thread(k_tid_t thread);
4500
Anas Nashif166f5192018-02-25 08:02:36 -06004501/** @} */
Chunlin Hane9c97022017-07-07 20:29:30 +08004502
Andrew Boie756f9072017-10-10 16:01:49 -07004503/**
4504 * @brief Emit a character buffer to the console device
4505 *
4506 * @param c String of characters to print
4507 * @param n The length of the string
4508 */
4509__syscall void k_str_out(char *c, size_t n);
4510
Andy Rosse7172672018-01-24 15:48:32 -08004511/**
4512 * @brief Start a numbered CPU on a MP-capable system
4513
4514 * This starts and initializes a specific CPU. The main thread on
4515 * startup is running on CPU zero, other processors are numbered
4516 * sequentially. On return from this function, the CPU is known to
4517 * have begun operating and will enter the provided function. Its
David B. Kinder3314c362018-04-05 15:15:35 -07004518 * interrupts will be initialized but disabled such that irq_unlock()
Andy Rosse7172672018-01-24 15:48:32 -08004519 * with the provided key will work to enable them.
4520 *
4521 * Normally, in SMP mode this function will be called by the kernel
4522 * initialization and should not be used as a user API. But it is
4523 * defined here for special-purpose apps which want Zephyr running on
4524 * one core and to use others for design-specific processing.
4525 *
4526 * @param cpu_num Integer number of the CPU
4527 * @param stack Stack memory for the CPU
4528 * @param sz Stack buffer size, in bytes
4529 * @param fn Function to begin running on the CPU. First argument is
4530 * an irq_unlock() key.
4531 * @param arg Untyped argument to be passed to "fn"
4532 */
4533extern void _arch_start_cpu(int cpu_num, k_thread_stack_t *stack, int sz,
4534 void (*fn)(int, void *), void *arg);
4535
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004536#ifdef __cplusplus
4537}
4538#endif
4539
Andrew Boiee004dec2016-11-07 09:01:19 -08004540#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4541/*
4542 * Define new and delete operators.
4543 * At this moment, the operators do nothing since objects are supposed
4544 * to be statically allocated.
4545 */
4546inline void operator delete(void *ptr)
4547{
4548 (void)ptr;
4549}
4550
4551inline void operator delete[](void *ptr)
4552{
4553 (void)ptr;
4554}
4555
4556inline void *operator new(size_t size)
4557{
4558 (void)size;
4559 return NULL;
4560}
4561
4562inline void *operator new[](size_t size)
4563{
4564 (void)size;
4565 return NULL;
4566}
4567
4568/* Placement versions of operator new and delete */
4569inline void operator delete(void *ptr1, void *ptr2)
4570{
4571 (void)ptr1;
4572 (void)ptr2;
4573}
4574
4575inline void operator delete[](void *ptr1, void *ptr2)
4576{
4577 (void)ptr1;
4578 (void)ptr2;
4579}
4580
4581inline void *operator new(size_t size, void *ptr)
4582{
4583 (void)size;
4584 return ptr;
4585}
4586
4587inline void *operator new[](size_t size, void *ptr)
4588{
4589 (void)size;
4590 return ptr;
4591}
4592
4593#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4594
Andrew Boiefa94ee72017-09-28 16:54:35 -07004595#include <syscalls/kernel.h>
4596
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004597#endif /* !_ASMLANGUAGE */
4598
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004599#endif /* _kernel__h_ */