blob: 544a9a5d173243a0064b39f05d460d57ab40ba1a [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>
Anas Nashifea8c6aad2016-12-23 07:32:56 -050028#include <kernel_version.h>
Leandro Pereiraadce1d12017-10-13 15:45:02 -070029#include <random/rand32.h>
Andrew Boie73abd322017-04-04 13:19:13 -070030#include <kernel_arch_thread.h>
Andrew Boie13ca6fe2017-09-23 12:05:49 -070031#include <syscall.h>
Andrew Boie43263fc2017-11-02 11:07:31 -070032#include <misc/printk.h>
33#include <arch/cpu.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040034
35#ifdef __cplusplus
36extern "C" {
37#endif
38
Anas Nashifbbb157d2017-01-15 08:46:31 -050039/**
40 * @brief Kernel APIs
41 * @defgroup kernel_apis Kernel APIs
42 * @{
43 * @}
44 */
45
Anas Nashif61f4b242016-11-18 10:53:59 -050046#ifdef CONFIG_KERNEL_DEBUG
Benjamin Walsh456c6da2016-09-02 18:55:39 -040047#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
48#else
49#define K_DEBUG(fmt, ...)
50#endif
51
Benjamin Walsh2f280412017-01-14 19:23:46 -050052#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
53#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
54#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
55#elif defined(CONFIG_COOP_ENABLED)
56#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
57#define _NUM_PREEMPT_PRIO (0)
58#elif defined(CONFIG_PREEMPT_ENABLED)
59#define _NUM_COOP_PRIO (0)
60#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
61#else
62#error "invalid configuration"
63#endif
64
65#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040066#define K_PRIO_PREEMPT(x) (x)
67
Benjamin Walsh456c6da2016-09-02 18:55:39 -040068#define K_ANY NULL
69#define K_END NULL
70
Benjamin Walshedb35702017-01-14 18:47:22 -050071#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040072#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050073#elif defined(CONFIG_COOP_ENABLED)
74#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
75#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040076#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050077#else
78#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040079#endif
80
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050081#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040082#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
83#else
84#define K_LOWEST_THREAD_PRIO -1
85#endif
86
Benjamin Walshfab8d922016-11-08 15:36:36 -050087#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
88
Benjamin Walsh456c6da2016-09-02 18:55:39 -040089#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
90#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
91
92typedef sys_dlist_t _wait_q_t;
93
Anas Nashif2f203c22016-12-18 06:57:45 -050094#ifdef CONFIG_OBJECT_TRACING
95#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
96#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040097#else
Anas Nashif2f203c22016-12-18 06:57:45 -050098#define _OBJECT_TRACING_INIT
99#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400100#endif
101
Benjamin Walshacc68c12017-01-29 18:57:45 -0500102#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300103#define _POLL_EVENT_OBJ_INIT(obj) \
104 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
105#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500106#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300107#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500108#define _POLL_EVENT
109#endif
110
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500111struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400112struct k_mutex;
113struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400114struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400115struct k_msgq;
116struct k_mbox;
117struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200118struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400119struct k_fifo;
120struct k_lifo;
121struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400122struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400123struct k_mem_pool;
124struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500125struct k_poll_event;
126struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800127struct k_mem_domain;
128struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400129
Andrew Boie5bd891d2017-09-27 12:59:28 -0700130/* This enumeration needs to be kept in sync with the lists of kernel objects
131 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
132 * function in kernel/userspace.c
133 */
Andrew Boie945af952017-08-22 13:15:23 -0700134enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700135 K_OBJ_ANY,
136
Andrew Boie5bd891d2017-09-27 12:59:28 -0700137 /* Core kernel objects */
Andrew Boie945af952017-08-22 13:15:23 -0700138 K_OBJ_ALERT,
Andrew Boie945af952017-08-22 13:15:23 -0700139 K_OBJ_MSGQ,
140 K_OBJ_MUTEX,
141 K_OBJ_PIPE,
142 K_OBJ_SEM,
143 K_OBJ_STACK,
144 K_OBJ_THREAD,
145 K_OBJ_TIMER,
Andrew Boiebca15da2017-10-15 14:17:48 -0700146 K_OBJ__THREAD_STACK_ELEMENT,
Andrew Boie945af952017-08-22 13:15:23 -0700147
Andrew Boie5bd891d2017-09-27 12:59:28 -0700148 /* Driver subsystems */
149 K_OBJ_DRIVER_ADC,
150 K_OBJ_DRIVER_AIO_CMP,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700151 K_OBJ_DRIVER_COUNTER,
152 K_OBJ_DRIVER_CRYPTO,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700153 K_OBJ_DRIVER_FLASH,
154 K_OBJ_DRIVER_GPIO,
155 K_OBJ_DRIVER_I2C,
156 K_OBJ_DRIVER_I2S,
157 K_OBJ_DRIVER_IPM,
158 K_OBJ_DRIVER_PINMUX,
159 K_OBJ_DRIVER_PWM,
Leandro Pereirada9b0dd2017-10-13 16:30:55 -0700160 K_OBJ_DRIVER_ENTROPY,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700161 K_OBJ_DRIVER_RTC,
162 K_OBJ_DRIVER_SENSOR,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700163 K_OBJ_DRIVER_SPI,
164 K_OBJ_DRIVER_UART,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700165
Andrew Boie945af952017-08-22 13:15:23 -0700166 K_OBJ_LAST
167};
168
169#ifdef CONFIG_USERSPACE
170/* Table generated by gperf, these objects are retrieved via
171 * _k_object_find() */
172struct _k_object {
173 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700174 u8_t perms[CONFIG_MAX_THREAD_BYTES];
175 u8_t type;
176 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700177 u32_t data;
Andrew Boie945af952017-08-22 13:15:23 -0700178} __packed;
179
Andrew Boie877f82e2017-10-17 11:20:22 -0700180struct _k_object_assignment {
181 struct k_thread *thread;
182 void * const *objects;
183};
184
185/**
186 * @brief Grant a static thread access to a list of kernel objects
187 *
188 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
189 * a set of kernel objects. These objects do not need to be in an initialized
190 * state. The permissions will be granted when the threads are initialized
191 * in the early boot sequence.
192 *
193 * All arguments beyond the first must be pointers to kernel objects.
194 *
195 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
196 */
197#define K_THREAD_ACCESS_GRANT(name_, ...) \
198 static void * const _CONCAT(_object_list_, name_)[] = \
199 { __VA_ARGS__, NULL }; \
200 static __used __in_section_unique(object_access) \
201 const struct _k_object_assignment \
202 _CONCAT(_object_access_, name_) = \
203 { (&_k_thread_obj_ ## name_), \
204 (_CONCAT(_object_list_, name_)) }
205
Andrew Boie945af952017-08-22 13:15:23 -0700206#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700207#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie945af952017-08-22 13:15:23 -0700208
209/**
210 * Lookup a kernel object and init its metadata if it exists
211 *
212 * Calling this on an object will make it usable from userspace.
213 * Intended to be called as the last statement in kernel object init
214 * functions.
215 *
216 * @param object Address of the kernel object
217 */
218void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700219#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700220
221#define K_THREAD_ACCESS_GRANT(thread, ...)
222
Andrew Boie743e4682017-10-04 12:25:50 -0700223static inline void _k_object_init(void *obj)
224{
225 ARG_UNUSED(obj);
226}
227
228static inline void _impl_k_object_access_grant(void *object,
229 struct k_thread *thread)
230{
231 ARG_UNUSED(object);
232 ARG_UNUSED(thread);
233}
234
Andrew Boiea89bf012017-10-09 14:47:55 -0700235static inline void _impl_k_object_access_revoke(void *object,
236 struct k_thread *thread)
237{
238 ARG_UNUSED(object);
239 ARG_UNUSED(thread);
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 */
270__syscall void k_object_access_revoke(void *object, struct k_thread *thread);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700271
272/**
273 * grant all present and future threads access to an object
274 *
275 * If the caller is from supervisor mode, or the caller is from user mode and
276 * have sufficient permissions on the object, then that object will have
277 * permissions granted to it for *all* current and future threads running in
278 * the system, effectively becoming a public kernel object.
279 *
280 * Use of this API should be avoided on systems that are running untrusted code
281 * as it is possible for such code to derive the addresses of kernel objects
282 * and perform unwanted operations on them.
283 *
Andrew Boie04caa672017-10-13 13:57:07 -0700284 * It is not possible to revoke permissions on public objects; once public,
285 * any thread may use it.
286 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700287 * @param object Address of kernel object
288 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700289void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700290
Andrew Boiebca15da2017-10-15 14:17:48 -0700291/* Using typedef deliberately here, this is quite intended to be an opaque
292 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
293 *
294 * The purpose of this data type is to clearly distinguish between the
295 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
296 * buffer which composes the stack data actually used by the underlying
297 * thread; they cannot be used interchangably as some arches precede the
298 * stack buffer region with guard areas that trigger a MPU or MMU fault
299 * if written to.
300 *
301 * APIs that want to work with the buffer inside should continue to use
302 * char *.
303 *
304 * Stacks should always be created with K_THREAD_STACK_DEFINE().
305 */
306struct __packed _k_thread_stack_element {
307 char data;
308};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700309typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700310
Andrew Boie73abd322017-04-04 13:19:13 -0700311/* timeouts */
312
313struct _timeout;
314typedef void (*_timeout_func_t)(struct _timeout *t);
315
316struct _timeout {
317 sys_dnode_t node;
318 struct k_thread *thread;
319 sys_dlist_t *wait_q;
320 s32_t delta_ticks_from_prev;
321 _timeout_func_t func;
322};
323
324extern s32_t _timeout_remaining_get(struct _timeout *timeout);
325
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700326/**
327 * @typedef k_thread_entry_t
328 * @brief Thread entry point function type.
329 *
330 * A thread's entry point function is invoked when the thread starts executing.
331 * Up to 3 argument values can be passed to the function.
332 *
333 * The thread terminates execution permanently if the entry point function
334 * returns. The thread is responsible for releasing any shared resources
335 * it may own (such as mutexes and dynamically allocated memory), prior to
336 * returning.
337 *
338 * @param p1 First argument.
339 * @param p2 Second argument.
340 * @param p3 Third argument.
341 *
342 * @return N/A
343 */
344typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700345
346#ifdef CONFIG_THREAD_MONITOR
347struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700348 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700349 void *parameter1;
350 void *parameter2;
351 void *parameter3;
352};
353#endif
354
355/* can be used for creating 'dummy' threads, e.g. for pending on objects */
356struct _thread_base {
357
358 /* this thread's entry in a ready/wait queue */
359 sys_dnode_t k_q_node;
360
361 /* user facing 'thread options'; values defined in include/kernel.h */
362 u8_t user_options;
363
364 /* thread state */
365 u8_t thread_state;
366
367 /*
368 * scheduler lock count and thread priority
369 *
370 * These two fields control the preemptibility of a thread.
371 *
372 * When the scheduler is locked, sched_locked is decremented, which
373 * means that the scheduler is locked for values from 0xff to 0x01. A
374 * thread is coop if its prio is negative, thus 0x80 to 0xff when
375 * looked at the value as unsigned.
376 *
377 * By putting them end-to-end, this means that a thread is
378 * non-preemptible if the bundled value is greater than or equal to
379 * 0x0080.
380 */
381 union {
382 struct {
383#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
384 u8_t sched_locked;
385 s8_t prio;
386#else /* LITTLE and PDP */
387 s8_t prio;
388 u8_t sched_locked;
389#endif
390 };
391 u16_t preempt;
392 };
393
394 /* data returned by APIs */
395 void *swap_data;
396
397#ifdef CONFIG_SYS_CLOCK_EXISTS
398 /* this thread's entry in a timeout queue */
399 struct _timeout timeout;
400#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700401};
402
403typedef struct _thread_base _thread_base_t;
404
405#if defined(CONFIG_THREAD_STACK_INFO)
406/* Contains the stack information of a thread */
407struct _thread_stack_info {
408 /* Stack Start */
409 u32_t start;
410 /* Stack Size */
411 u32_t size;
412};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700413
414typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700415#endif /* CONFIG_THREAD_STACK_INFO */
416
Chunlin Hane9c97022017-07-07 20:29:30 +0800417#if defined(CONFIG_USERSPACE)
418struct _mem_domain_info {
419 /* memory domain queue node */
420 sys_dnode_t mem_domain_q_node;
421 /* memory domain of the thread */
422 struct k_mem_domain *mem_domain;
423};
424
425#endif /* CONFIG_USERSPACE */
426
Andrew Boie73abd322017-04-04 13:19:13 -0700427struct k_thread {
428
429 struct _thread_base base;
430
431 /* defined by the architecture, but all archs need these */
432 struct _caller_saved caller_saved;
433 struct _callee_saved callee_saved;
434
435 /* static thread init data */
436 void *init_data;
437
438 /* abort function */
439 void (*fn_abort)(void);
440
441#if defined(CONFIG_THREAD_MONITOR)
442 /* thread entry and parameters description */
443 struct __thread_entry *entry;
444
445 /* next item in list of all threads */
446 struct k_thread *next_thread;
447#endif
448
449#ifdef CONFIG_THREAD_CUSTOM_DATA
450 /* crude thread-local storage */
451 void *custom_data;
452#endif
453
454#ifdef CONFIG_ERRNO
455 /* per-thread errno variable */
456 int errno_var;
457#endif
458
459#if defined(CONFIG_THREAD_STACK_INFO)
460 /* Stack Info */
461 struct _thread_stack_info stack_info;
462#endif /* CONFIG_THREAD_STACK_INFO */
463
Chunlin Hane9c97022017-07-07 20:29:30 +0800464#if defined(CONFIG_USERSPACE)
465 /* memory domain info of the thread */
466 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700467 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700468 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800469#endif /* CONFIG_USERSPACE */
470
Andrew Boie73abd322017-04-04 13:19:13 -0700471 /* arch-specifics: must always be at the end */
472 struct _thread_arch arch;
473};
474
475typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400476typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700477#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400478
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400479enum execution_context_types {
480 K_ISR = 0,
481 K_COOP_THREAD,
482 K_PREEMPT_THREAD,
483};
484
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400485/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100486 * @defgroup profiling_apis Profiling APIs
487 * @ingroup kernel_apis
488 * @{
489 */
490
491/**
492 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
493 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700494 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100495 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
496 *
497 * CONFIG_MAIN_STACK_SIZE
498 * CONFIG_IDLE_STACK_SIZE
499 * CONFIG_ISR_STACK_SIZE
500 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
501 *
502 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
503 * produce output.
504 *
505 * @return N/A
506 */
507extern void k_call_stacks_analyze(void);
508
509/**
510 * @} end defgroup profiling_apis
511 */
512
513/**
Allan Stephensc98da842016-11-11 15:45:03 -0500514 * @defgroup thread_apis Thread APIs
515 * @ingroup kernel_apis
516 * @{
517 */
518
Benjamin Walshed240f22017-01-22 13:05:08 -0500519#endif /* !_ASMLANGUAGE */
520
521
522/*
523 * Thread user options. May be needed by assembly code. Common part uses low
524 * bits, arch-specific use high bits.
525 */
526
527/* system thread that must not abort */
528#define K_ESSENTIAL (1 << 0)
529
530#if defined(CONFIG_FP_SHARING)
531/* thread uses floating point registers */
532#define K_FP_REGS (1 << 1)
533#endif
534
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700535/* This thread has dropped from supervisor mode to user mode and consequently
536 * has additional restrictions
537 */
538#define K_USER (1 << 2)
539
Andrew Boie47f8fd12017-10-05 11:11:02 -0700540/* Indicates that the thread being created should inherit all kernel object
541 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
542 * is not enabled.
543 */
544#define K_INHERIT_PERMS (1 << 3)
545
Benjamin Walshed240f22017-01-22 13:05:08 -0500546#ifdef CONFIG_X86
547/* x86 Bitmask definitions for threads user options */
548
549#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
550/* thread uses SSEx (and also FP) registers */
551#define K_SSE_REGS (1 << 7)
552#endif
553#endif
554
555/* end - thread options */
556
557#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400558/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700559 * @brief Create a thread.
560 *
561 * This routine initializes a thread, then schedules it for execution.
562 *
563 * The new thread may be scheduled for immediate execution or a delayed start.
564 * If the newly spawned thread does not have a delayed start the kernel
565 * scheduler may preempt the current thread to allow the new thread to
566 * execute.
567 *
568 * Thread options are architecture-specific, and can include K_ESSENTIAL,
569 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
570 * them using "|" (the logical OR operator).
571 *
572 * Historically, users often would use the beginning of the stack memory region
573 * to store the struct k_thread data, although corruption will occur if the
574 * stack overflows this region and stack protection features may not detect this
575 * situation.
576 *
577 * @param new_thread Pointer to uninitialized struct k_thread
578 * @param stack Pointer to the stack space.
579 * @param stack_size Stack size in bytes.
580 * @param entry Thread entry function.
581 * @param p1 1st entry point parameter.
582 * @param p2 2nd entry point parameter.
583 * @param p3 3rd entry point parameter.
584 * @param prio Thread priority.
585 * @param options Thread options.
586 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
587 *
588 * @return ID of new thread.
589 */
Andrew Boie662c3452017-10-02 10:51:18 -0700590__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700591 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700592 size_t stack_size,
593 k_thread_entry_t entry,
594 void *p1, void *p2, void *p3,
595 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700596
Andrew Boie3f091b52017-08-30 14:34:14 -0700597/**
598 * @brief Drop a thread's privileges permanently to user mode
599 *
600 * @param entry Function to start executing from
601 * @param p1 1st entry point parameter
602 * @param p2 2nd entry point parameter
603 * @param p3 3rd entry point parameter
604 */
605extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
606 void *p1, void *p2,
607 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700608
Andrew Boied26cf2d2017-03-30 13:07:02 -0700609/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700610 * @brief Grant a thread access to a NULL-terminated set of kernel objects
611 *
612 * This is a convenience function. For the provided thread, grant access to
613 * the remaining arguments, which must be pointers to kernel objects.
614 * The final argument must be a NULL.
615 *
616 * The thread object must be initialized (i.e. running). The objects don't
617 * need to be.
618 *
619 * @param thread Thread to grant access to objects
620 * @param ... NULL-terminated list of kernel object pointers
621 */
622extern void __attribute__((sentinel))
623 k_thread_access_grant(struct k_thread *thread, ...);
624
625/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500626 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400627 *
Allan Stephensc98da842016-11-11 15:45:03 -0500628 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500629 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500631 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400632 *
633 * @return N/A
634 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700635__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400636
637/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500638 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400639 *
640 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500641 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400642 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400643 * @return N/A
644 */
Kumar Galacc334c72017-04-21 10:55:34 -0500645extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400646
647/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500648 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400649 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500650 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400651 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500652 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400653 *
654 * @return N/A
655 */
Andrew Boie468190a2017-09-29 14:00:48 -0700656__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400657
658/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500659 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400660 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500661 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400662 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500663 * If @a thread is not currently sleeping, the routine has no effect.
664 *
665 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400666 *
667 * @return N/A
668 */
Andrew Boie468190a2017-09-29 14:00:48 -0700669__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400670
671/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500672 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400673 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500674 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400675 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700676__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400677
678/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500679 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400680 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500681 * This routine prevents @a thread from executing if it has not yet started
682 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400683 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500684 * @param thread ID of thread to cancel.
685 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700686 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500687 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400688 */
Andrew Boie468190a2017-09-29 14:00:48 -0700689__syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400690
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400691/**
Allan Stephensc98da842016-11-11 15:45:03 -0500692 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500694 * This routine permanently stops execution of @a thread. The thread is taken
695 * off all kernel queues it is part of (i.e. the ready queue, the timeout
696 * queue, or a kernel object wait queue). However, any kernel resources the
697 * thread might currently own (such as mutexes or memory blocks) are not
698 * released. It is the responsibility of the caller of this routine to ensure
699 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500701 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400702 *
703 * @return N/A
704 */
Andrew Boie468190a2017-09-29 14:00:48 -0700705__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400706
Andrew Boie7d627c52017-08-30 11:01:56 -0700707
708/**
709 * @brief Start an inactive thread
710 *
711 * If a thread was created with K_FOREVER in the delay parameter, it will
712 * not be added to the scheduling queue until this function is called
713 * on it.
714 *
715 * @param thread thread to start
716 */
Andrew Boie468190a2017-09-29 14:00:48 -0700717__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700718
Allan Stephensc98da842016-11-11 15:45:03 -0500719/**
720 * @cond INTERNAL_HIDDEN
721 */
722
Benjamin Walshd211a522016-12-06 11:44:01 -0500723/* timeout has timed out and is not on _timeout_q anymore */
724#define _EXPIRED (-2)
725
726/* timeout is not in use */
727#define _INACTIVE (-1)
728
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400729struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700730 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700731 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400732 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700733 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500734 void *init_p1;
735 void *init_p2;
736 void *init_p3;
737 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500738 u32_t init_options;
739 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500740 void (*init_abort)(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400741};
742
Andrew Boied26cf2d2017-03-30 13:07:02 -0700743#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400744 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500745 prio, options, delay, abort, groups) \
746 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700747 .init_thread = (thread), \
748 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500749 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700750 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400751 .init_p1 = (void *)p1, \
752 .init_p2 = (void *)p2, \
753 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500754 .init_prio = (prio), \
755 .init_options = (options), \
756 .init_delay = (delay), \
757 .init_abort = (abort), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400758 }
759
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400760/**
Allan Stephensc98da842016-11-11 15:45:03 -0500761 * INTERNAL_HIDDEN @endcond
762 */
763
764/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500765 * @brief Statically define and initialize a thread.
766 *
767 * The thread may be scheduled for immediate execution or a delayed start.
768 *
769 * Thread options are architecture-specific, and can include K_ESSENTIAL,
770 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
771 * them using "|" (the logical OR operator).
772 *
773 * The ID of the thread can be accessed using:
774 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500775 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500776 *
777 * @param name Name of the thread.
778 * @param stack_size Stack size in bytes.
779 * @param entry Thread entry function.
780 * @param p1 1st entry point parameter.
781 * @param p2 2nd entry point parameter.
782 * @param p3 3rd entry point parameter.
783 * @param prio Thread priority.
784 * @param options Thread options.
785 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400786 *
787 * @internal It has been observed that the x86 compiler by default aligns
788 * these _static_thread_data structures to 32-byte boundaries, thereby
789 * wasting space. To work around this, force a 4-byte alignment.
790 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500791#define K_THREAD_DEFINE(name, stack_size, \
792 entry, p1, p2, p3, \
793 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700794 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700795 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500796 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500797 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700798 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
799 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500800 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700801 NULL, 0); \
802 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400803
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400804/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500805 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400806 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500807 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500809 * @param thread ID of thread whose priority is needed.
810 *
811 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400812 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700813__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400814
815/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500816 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400817 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500818 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819 *
820 * Rescheduling can occur immediately depending on the priority @a thread is
821 * set to:
822 *
823 * - If its priority is raised above the priority of the caller of this
824 * function, and the caller is preemptible, @a thread will be scheduled in.
825 *
826 * - If the caller operates on itself, it lowers its priority below that of
827 * other threads in the system, and the caller is preemptible, the thread of
828 * highest priority will be scheduled in.
829 *
830 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
831 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
832 * highest priority.
833 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500834 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400835 * @param prio New priority.
836 *
837 * @warning Changing the priority of a thread currently involved in mutex
838 * priority inheritance may result in undefined behavior.
839 *
840 * @return N/A
841 */
Andrew Boie468190a2017-09-29 14:00:48 -0700842__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400843
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400844/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500845 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500847 * This routine prevents the kernel scheduler from making @a thread the
848 * current thread. All other internal operations on @a thread are still
849 * performed; for example, any timeout it is waiting on keeps ticking,
850 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500852 * If @a thread is already suspended, the routine has no effect.
853 *
854 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400855 *
856 * @return N/A
857 */
Andrew Boie468190a2017-09-29 14:00:48 -0700858__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400859
860/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500861 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400862 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500863 * This routine allows the kernel scheduler to make @a thread the current
864 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500866 * If @a thread is not currently suspended, the routine has no effect.
867 *
868 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400869 *
870 * @return N/A
871 */
Andrew Boie468190a2017-09-29 14:00:48 -0700872__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400873
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400874/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500875 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400876 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500877 * This routine specifies how the scheduler will perform time slicing of
878 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400879 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500880 * To enable time slicing, @a slice must be non-zero. The scheduler
881 * ensures that no thread runs for more than the specified time limit
882 * before other threads of that priority are given a chance to execute.
883 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700884 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500886 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400887 * execute. Once the scheduler selects a thread for execution, there is no
888 * minimum guaranteed time the thread will execute before threads of greater or
889 * equal priority are scheduled.
890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500891 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400892 * for execution, this routine has no effect; the thread is immediately
893 * rescheduled after the slice period expires.
894 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500895 * To disable timeslicing, set both @a slice and @a prio to zero.
896 *
897 * @param slice Maximum time slice length (in milliseconds).
898 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400899 *
900 * @return N/A
901 */
Kumar Galacc334c72017-04-21 10:55:34 -0500902extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400903
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400904/**
Allan Stephensc98da842016-11-11 15:45:03 -0500905 * @} end defgroup thread_apis
906 */
907
908/**
909 * @addtogroup isr_apis
910 * @{
911 */
912
913/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500914 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400915 *
Allan Stephensc98da842016-11-11 15:45:03 -0500916 * This routine allows the caller to customize its actions, depending on
917 * whether it is a thread or an ISR.
918 *
919 * @note Can be called by ISRs.
920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500921 * @return 0 if invoked by a thread.
922 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400923 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500924extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400925
Benjamin Walsh445830d2016-11-10 15:54:27 -0500926/**
927 * @brief Determine if code is running in a preemptible thread.
928 *
Allan Stephensc98da842016-11-11 15:45:03 -0500929 * This routine allows the caller to customize its actions, depending on
930 * whether it can be preempted by another thread. The routine returns a 'true'
931 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500932 *
Allan Stephensc98da842016-11-11 15:45:03 -0500933 * - The code is running in a thread, not at ISR.
934 * - The thread's priority is in the preemptible range.
935 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500936 *
Allan Stephensc98da842016-11-11 15:45:03 -0500937 * @note Can be called by ISRs.
938 *
939 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500940 * @return Non-zero if invoked by a preemptible thread.
941 */
Andrew Boie468190a2017-09-29 14:00:48 -0700942__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -0500943
Allan Stephensc98da842016-11-11 15:45:03 -0500944/**
945 * @} end addtogroup isr_apis
946 */
947
948/**
949 * @addtogroup thread_apis
950 * @{
951 */
952
953/**
954 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500955 *
Allan Stephensc98da842016-11-11 15:45:03 -0500956 * This routine prevents the current thread from being preempted by another
957 * thread by instructing the scheduler to treat it as a cooperative thread.
958 * If the thread subsequently performs an operation that makes it unready,
959 * it will be context switched out in the normal manner. When the thread
960 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500961 *
Allan Stephensc98da842016-11-11 15:45:03 -0500962 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500963 *
Allan Stephensc98da842016-11-11 15:45:03 -0500964 * @note k_sched_lock() and k_sched_unlock() should normally be used
965 * when the operation being performed can be safely interrupted by ISRs.
966 * However, if the amount of processing involved is very small, better
967 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500968 *
969 * @return N/A
970 */
971extern void k_sched_lock(void);
972
Allan Stephensc98da842016-11-11 15:45:03 -0500973/**
974 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500975 *
Allan Stephensc98da842016-11-11 15:45:03 -0500976 * This routine reverses the effect of a previous call to k_sched_lock().
977 * A thread must call the routine once for each time it called k_sched_lock()
978 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500979 *
980 * @return N/A
981 */
982extern void k_sched_unlock(void);
983
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400984/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500985 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400986 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500987 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500989 * Custom data is not used by the kernel itself, and is freely available
990 * for a thread to use as it sees fit. It can be used as a framework
991 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500993 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400994 *
995 * @return N/A
996 */
Andrew Boie468190a2017-09-29 14:00:48 -0700997__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400998
999/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001000 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001001 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001002 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001004 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001005 */
Andrew Boie468190a2017-09-29 14:00:48 -07001006__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001007
1008/**
Allan Stephensc98da842016-11-11 15:45:03 -05001009 * @} end addtogroup thread_apis
1010 */
1011
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001012#include <sys_clock.h>
1013
Allan Stephensc2f15a42016-11-17 12:24:22 -05001014/**
1015 * @addtogroup clock_apis
1016 * @{
1017 */
1018
1019/**
1020 * @brief Generate null timeout delay.
1021 *
1022 * This macro generates a timeout delay that that instructs a kernel API
1023 * not to wait if the requested operation cannot be performed immediately.
1024 *
1025 * @return Timeout delay value.
1026 */
1027#define K_NO_WAIT 0
1028
1029/**
1030 * @brief Generate timeout delay from milliseconds.
1031 *
1032 * This macro generates a timeout delay that that instructs a kernel API
1033 * to wait up to @a ms milliseconds to perform the requested operation.
1034 *
1035 * @param ms Duration in milliseconds.
1036 *
1037 * @return Timeout delay value.
1038 */
Johan Hedberg14471692016-11-13 10:52:15 +02001039#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001040
1041/**
1042 * @brief Generate timeout delay from seconds.
1043 *
1044 * This macro generates a timeout delay that that instructs a kernel API
1045 * to wait up to @a s seconds to perform the requested operation.
1046 *
1047 * @param s Duration in seconds.
1048 *
1049 * @return Timeout delay value.
1050 */
Johan Hedberg14471692016-11-13 10:52:15 +02001051#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001052
1053/**
1054 * @brief Generate timeout delay from minutes.
1055 *
1056 * This macro generates a timeout delay that that instructs a kernel API
1057 * to wait up to @a m minutes to perform the requested operation.
1058 *
1059 * @param m Duration in minutes.
1060 *
1061 * @return Timeout delay value.
1062 */
Johan Hedberg14471692016-11-13 10:52:15 +02001063#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001064
1065/**
1066 * @brief Generate timeout delay from hours.
1067 *
1068 * This macro generates a timeout delay that that instructs a kernel API
1069 * to wait up to @a h hours to perform the requested operation.
1070 *
1071 * @param h Duration in hours.
1072 *
1073 * @return Timeout delay value.
1074 */
Johan Hedberg14471692016-11-13 10:52:15 +02001075#define K_HOURS(h) K_MINUTES((h) * 60)
1076
Allan Stephensc98da842016-11-11 15:45:03 -05001077/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001078 * @brief Generate infinite timeout delay.
1079 *
1080 * This macro generates a timeout delay that that instructs a kernel API
1081 * to wait as long as necessary to perform the requested operation.
1082 *
1083 * @return Timeout delay value.
1084 */
1085#define K_FOREVER (-1)
1086
1087/**
1088 * @} end addtogroup clock_apis
1089 */
1090
1091/**
Allan Stephensc98da842016-11-11 15:45:03 -05001092 * @cond INTERNAL_HIDDEN
1093 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001094
Benjamin Walsh62092182016-12-20 14:39:08 -05001095/* kernel clocks */
1096
1097#if (sys_clock_ticks_per_sec == 1000) || \
1098 (sys_clock_ticks_per_sec == 500) || \
1099 (sys_clock_ticks_per_sec == 250) || \
1100 (sys_clock_ticks_per_sec == 125) || \
1101 (sys_clock_ticks_per_sec == 100) || \
1102 (sys_clock_ticks_per_sec == 50) || \
1103 (sys_clock_ticks_per_sec == 25) || \
1104 (sys_clock_ticks_per_sec == 20) || \
1105 (sys_clock_ticks_per_sec == 10) || \
1106 (sys_clock_ticks_per_sec == 1)
1107
1108 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1109#else
1110 /* yields horrible 64-bit math on many architectures: try to avoid */
1111 #define _NON_OPTIMIZED_TICKS_PER_SEC
1112#endif
1113
1114#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001115extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001116#else
Kumar Galacc334c72017-04-21 10:55:34 -05001117static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001118{
Kumar Galacc334c72017-04-21 10:55:34 -05001119 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001120}
1121#endif
1122
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001123/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001124#ifdef CONFIG_TICKLESS_KERNEL
1125#define _TICK_ALIGN 0
1126#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001127#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001128#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001129
Kumar Galacc334c72017-04-21 10:55:34 -05001130static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001131{
Benjamin Walsh62092182016-12-20 14:39:08 -05001132#ifdef CONFIG_SYS_CLOCK_EXISTS
1133
1134#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001135 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001136#else
Kumar Galacc334c72017-04-21 10:55:34 -05001137 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001138#endif
1139
1140#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001141 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001142 return 0;
1143#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001144}
1145
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001146struct k_timer {
1147 /*
1148 * _timeout structure must be first here if we want to use
1149 * dynamic timer allocation. timeout.node is used in the double-linked
1150 * list of free timers
1151 */
1152 struct _timeout timeout;
1153
Allan Stephens45bfa372016-10-12 12:39:42 -05001154 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001155 _wait_q_t wait_q;
1156
1157 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001158 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001159
1160 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001161 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001162
1163 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001164 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001165
Allan Stephens45bfa372016-10-12 12:39:42 -05001166 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001167 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001168
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001169 /* user-specific data, also used to support legacy features */
1170 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001171
Anas Nashif2f203c22016-12-18 06:57:45 -05001172 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001173};
1174
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001175#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001176 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001177 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001178 .timeout.wait_q = NULL, \
1179 .timeout.thread = NULL, \
1180 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001181 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001182 .expiry_fn = expiry, \
1183 .stop_fn = stop, \
1184 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001185 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001186 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001187 }
1188
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001189#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1190
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001191/**
Allan Stephensc98da842016-11-11 15:45:03 -05001192 * INTERNAL_HIDDEN @endcond
1193 */
1194
1195/**
1196 * @defgroup timer_apis Timer APIs
1197 * @ingroup kernel_apis
1198 * @{
1199 */
1200
1201/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001202 * @typedef k_timer_expiry_t
1203 * @brief Timer expiry function type.
1204 *
1205 * A timer's expiry function is executed by the system clock interrupt handler
1206 * each time the timer expires. The expiry function is optional, and is only
1207 * invoked if the timer has been initialized with one.
1208 *
1209 * @param timer Address of timer.
1210 *
1211 * @return N/A
1212 */
1213typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1214
1215/**
1216 * @typedef k_timer_stop_t
1217 * @brief Timer stop function type.
1218 *
1219 * A timer's stop function is executed if the timer is stopped prematurely.
1220 * The function runs in the context of the thread that stops the timer.
1221 * The stop function is optional, and is only invoked if the timer has been
1222 * initialized with one.
1223 *
1224 * @param timer Address of timer.
1225 *
1226 * @return N/A
1227 */
1228typedef void (*k_timer_stop_t)(struct k_timer *timer);
1229
1230/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001231 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001232 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001233 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001234 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001235 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001236 *
1237 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001238 * @param expiry_fn Function to invoke each time the timer expires.
1239 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001240 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001241#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001242 struct k_timer name \
1243 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001244 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001245
Allan Stephens45bfa372016-10-12 12:39:42 -05001246/**
1247 * @brief Initialize a timer.
1248 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001249 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001250 *
1251 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001252 * @param expiry_fn Function to invoke each time the timer expires.
1253 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001254 *
1255 * @return N/A
1256 */
1257extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001258 k_timer_expiry_t expiry_fn,
1259 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001260
Allan Stephens45bfa372016-10-12 12:39:42 -05001261/**
1262 * @brief Start a timer.
1263 *
1264 * This routine starts a timer, and resets its status to zero. The timer
1265 * begins counting down using the specified duration and period values.
1266 *
1267 * Attempting to start a timer that is already running is permitted.
1268 * The timer's status is reset to zero and the timer begins counting down
1269 * using the new duration and period values.
1270 *
1271 * @param timer Address of timer.
1272 * @param duration Initial timer duration (in milliseconds).
1273 * @param period Timer period (in milliseconds).
1274 *
1275 * @return N/A
1276 */
Andrew Boiea354d492017-09-29 16:22:28 -07001277__syscall void k_timer_start(struct k_timer *timer,
1278 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001279
1280/**
1281 * @brief Stop a timer.
1282 *
1283 * This routine stops a running timer prematurely. The timer's stop function,
1284 * if one exists, is invoked by the caller.
1285 *
1286 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001287 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001288 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001289 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1290 * if @a k_timer_stop is to be called from ISRs.
1291 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001292 * @param timer Address of timer.
1293 *
1294 * @return N/A
1295 */
Andrew Boiea354d492017-09-29 16:22:28 -07001296__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001297
1298/**
1299 * @brief Read timer status.
1300 *
1301 * This routine reads the timer's status, which indicates the number of times
1302 * it has expired since its status was last read.
1303 *
1304 * Calling this routine resets the timer's status to zero.
1305 *
1306 * @param timer Address of timer.
1307 *
1308 * @return Timer status.
1309 */
Andrew Boiea354d492017-09-29 16:22:28 -07001310__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001311
1312/**
1313 * @brief Synchronize thread to timer expiration.
1314 *
1315 * This routine blocks the calling thread until the timer's status is non-zero
1316 * (indicating that it has expired at least once since it was last examined)
1317 * or the timer is stopped. If the timer status is already non-zero,
1318 * or the timer is already stopped, the caller continues without waiting.
1319 *
1320 * Calling this routine resets the timer's status to zero.
1321 *
1322 * This routine must not be used by interrupt handlers, since they are not
1323 * allowed to block.
1324 *
1325 * @param timer Address of timer.
1326 *
1327 * @return Timer status.
1328 */
Andrew Boiea354d492017-09-29 16:22:28 -07001329__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001330
1331/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001332 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001333 *
1334 * This routine computes the (approximate) time remaining before a running
1335 * timer next expires. If the timer is not running, it returns zero.
1336 *
1337 * @param timer Address of timer.
1338 *
1339 * @return Remaining time (in milliseconds).
1340 */
Andrew Boiea354d492017-09-29 16:22:28 -07001341__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1342
1343static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001344{
1345 return _timeout_remaining_get(&timer->timeout);
1346}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001347
Allan Stephensc98da842016-11-11 15:45:03 -05001348/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001349 * @brief Associate user-specific data with a timer.
1350 *
1351 * This routine records the @a user_data with the @a timer, to be retrieved
1352 * later.
1353 *
1354 * It can be used e.g. in a timer handler shared across multiple subsystems to
1355 * retrieve data specific to the subsystem this timer is associated with.
1356 *
1357 * @param timer Address of timer.
1358 * @param user_data User data to associate with the timer.
1359 *
1360 * @return N/A
1361 */
Andrew Boiea354d492017-09-29 16:22:28 -07001362__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1363
1364static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1365 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001366{
1367 timer->user_data = user_data;
1368}
1369
1370/**
1371 * @brief Retrieve the user-specific data from a timer.
1372 *
1373 * @param timer Address of timer.
1374 *
1375 * @return The user data.
1376 */
Andrew Boiea354d492017-09-29 16:22:28 -07001377__syscall void *k_timer_user_data_get(struct k_timer *timer);
1378
1379static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001380{
1381 return timer->user_data;
1382}
1383
1384/**
Allan Stephensc98da842016-11-11 15:45:03 -05001385 * @} end defgroup timer_apis
1386 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001387
Allan Stephensc98da842016-11-11 15:45:03 -05001388/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001389 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001390 * @{
1391 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001392
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001393/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001394 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001395 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001396 * This routine returns the elapsed time since the system booted,
1397 * in milliseconds.
1398 *
1399 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001400 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001401__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001402
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001403#ifdef CONFIG_TICKLESS_KERNEL
1404/**
1405 * @brief Enable clock always on in tickless kernel
1406 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001407 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001408 * there are no timer events programmed in tickless kernel
1409 * scheduling. This is necessary if the clock is used to track
1410 * passage of time.
1411 *
1412 * @retval prev_status Previous status of always on flag
1413 */
1414static inline int k_enable_sys_clock_always_on(void)
1415{
1416 int prev_status = _sys_clock_always_on;
1417
1418 _sys_clock_always_on = 1;
1419 _enable_sys_clock();
1420
1421 return prev_status;
1422}
1423
1424/**
1425 * @brief Disable clock always on in tickless kernel
1426 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001427 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001428 * there are no timer events programmed in tickless kernel
1429 * scheduling. To save power, this routine should be called
1430 * immediately when clock is not used to track time.
1431 */
1432static inline void k_disable_sys_clock_always_on(void)
1433{
1434 _sys_clock_always_on = 0;
1435}
1436#else
1437#define k_enable_sys_clock_always_on() do { } while ((0))
1438#define k_disable_sys_clock_always_on() do { } while ((0))
1439#endif
1440
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001441/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001442 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001443 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001444 * This routine returns the lower 32-bits of the elapsed time since the system
1445 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001446 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001447 * This routine can be more efficient than k_uptime_get(), as it reduces the
1448 * need for interrupt locking and 64-bit math. However, the 32-bit result
1449 * cannot hold a system uptime time larger than approximately 50 days, so the
1450 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001451 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001452 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001453 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001454__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001455
1456/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001457 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001458 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001459 * This routine computes the elapsed time between the current system uptime
1460 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001461 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001462 * @param reftime Pointer to a reference time, which is updated to the current
1463 * uptime upon return.
1464 *
1465 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001466 */
Kumar Galacc334c72017-04-21 10:55:34 -05001467extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001468
1469/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001470 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001471 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001472 * This routine computes the elapsed time between the current system uptime
1473 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001475 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1476 * need for interrupt locking and 64-bit math. However, the 32-bit result
1477 * cannot hold an elapsed time larger than approximately 50 days, so the
1478 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001480 * @param reftime Pointer to a reference time, which is updated to the current
1481 * uptime upon return.
1482 *
1483 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001484 */
Kumar Galacc334c72017-04-21 10:55:34 -05001485extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001486
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001487/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001488 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001489 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001490 * This routine returns the current time, as measured by the system's hardware
1491 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001492 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001493 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001494 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001495#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001496
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001497/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001498 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001499 */
1500
Allan Stephensc98da842016-11-11 15:45:03 -05001501/**
1502 * @cond INTERNAL_HIDDEN
1503 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001504
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001505struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001506 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001507 union {
1508 _wait_q_t wait_q;
1509
1510 _POLL_EVENT;
1511 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001512
1513 _OBJECT_TRACING_NEXT_PTR(k_queue);
1514};
1515
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001516#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001517 { \
1518 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1519 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001520 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001521 _OBJECT_TRACING_INIT \
1522 }
1523
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001524#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1525
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001526/**
1527 * INTERNAL_HIDDEN @endcond
1528 */
1529
1530/**
1531 * @defgroup queue_apis Queue APIs
1532 * @ingroup kernel_apis
1533 * @{
1534 */
1535
1536/**
1537 * @brief Initialize a queue.
1538 *
1539 * This routine initializes a queue object, prior to its first use.
1540 *
1541 * @param queue Address of the queue.
1542 *
1543 * @return N/A
1544 */
1545extern void k_queue_init(struct k_queue *queue);
1546
1547/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001548 * @brief Cancel waiting on a queue.
1549 *
1550 * This routine causes first thread pending on @a queue, if any, to
1551 * return from k_queue_get() call with NULL value (as if timeout expired).
1552 *
1553 * @note Can be called by ISRs.
1554 *
1555 * @param queue Address of the queue.
1556 *
1557 * @return N/A
1558 */
1559extern void k_queue_cancel_wait(struct k_queue *queue);
1560
1561/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001562 * @brief Append an element to the end of a queue.
1563 *
1564 * This routine appends a data item to @a queue. A queue data item must be
1565 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1566 * reserved for the kernel's use.
1567 *
1568 * @note Can be called by ISRs.
1569 *
1570 * @param queue Address of the queue.
1571 * @param data Address of the data item.
1572 *
1573 * @return N/A
1574 */
1575extern void k_queue_append(struct k_queue *queue, void *data);
1576
1577/**
1578 * @brief Prepend an element to a queue.
1579 *
1580 * This routine prepends a data item to @a queue. A queue data item must be
1581 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1582 * reserved for the kernel's use.
1583 *
1584 * @note Can be called by ISRs.
1585 *
1586 * @param queue Address of the queue.
1587 * @param data Address of the data item.
1588 *
1589 * @return N/A
1590 */
1591extern void k_queue_prepend(struct k_queue *queue, void *data);
1592
1593/**
1594 * @brief Inserts an element to a queue.
1595 *
1596 * This routine inserts a data item to @a queue after previous item. A queue
1597 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1598 * item are reserved for the kernel's use.
1599 *
1600 * @note Can be called by ISRs.
1601 *
1602 * @param queue Address of the queue.
1603 * @param prev Address of the previous data item.
1604 * @param data Address of the data item.
1605 *
1606 * @return N/A
1607 */
1608extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1609
1610/**
1611 * @brief Atomically append a list of elements to a queue.
1612 *
1613 * This routine adds a list of data items to @a queue in one operation.
1614 * The data items must be in a singly-linked list, with the first 32 bits
1615 * in each data item pointing to the next data item; the list must be
1616 * NULL-terminated.
1617 *
1618 * @note Can be called by ISRs.
1619 *
1620 * @param queue Address of the queue.
1621 * @param head Pointer to first node in singly-linked list.
1622 * @param tail Pointer to last node in singly-linked list.
1623 *
1624 * @return N/A
1625 */
1626extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1627
1628/**
1629 * @brief Atomically add a list of elements to a queue.
1630 *
1631 * This routine adds a list of data items to @a queue in one operation.
1632 * The data items must be in a singly-linked list implemented using a
1633 * sys_slist_t object. Upon completion, the original list is empty.
1634 *
1635 * @note Can be called by ISRs.
1636 *
1637 * @param queue Address of the queue.
1638 * @param list Pointer to sys_slist_t object.
1639 *
1640 * @return N/A
1641 */
1642extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1643
1644/**
1645 * @brief Get an element from a queue.
1646 *
1647 * This routine removes first data item from @a queue. The first 32 bits of the
1648 * data item are reserved for the kernel's use.
1649 *
1650 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1651 *
1652 * @param queue Address of the queue.
1653 * @param timeout Waiting period to obtain a data item (in milliseconds),
1654 * or one of the special values K_NO_WAIT and K_FOREVER.
1655 *
1656 * @return Address of the data item if successful; NULL if returned
1657 * without waiting, or waiting period timed out.
1658 */
Kumar Galacc334c72017-04-21 10:55:34 -05001659extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001660
1661/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001662 * @brief Remove an element from a queue.
1663 *
1664 * This routine removes data item from @a queue. The first 32 bits of the
1665 * data item are reserved for the kernel's use. Removing elements from k_queue
1666 * rely on sys_slist_find_and_remove which is not a constant time operation.
1667 *
1668 * @note Can be called by ISRs
1669 *
1670 * @param queue Address of the queue.
1671 * @param data Address of the data item.
1672 *
1673 * @return true if data item was removed
1674 */
1675static inline bool k_queue_remove(struct k_queue *queue, void *data)
1676{
1677 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1678}
1679
1680/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001681 * @brief Query a queue to see if it has data available.
1682 *
1683 * Note that the data might be already gone by the time this function returns
1684 * if other threads are also trying to read from the queue.
1685 *
1686 * @note Can be called by ISRs.
1687 *
1688 * @param queue Address of the queue.
1689 *
1690 * @return Non-zero if the queue is empty.
1691 * @return 0 if data is available.
1692 */
1693static inline int k_queue_is_empty(struct k_queue *queue)
1694{
1695 return (int)sys_slist_is_empty(&queue->data_q);
1696}
1697
1698/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001699 * @brief Peek element at the head of queue.
1700 *
1701 * Return element from the head of queue without removing it.
1702 *
1703 * @param queue Address of the queue.
1704 *
1705 * @return Head element, or NULL if queue is empty.
1706 */
1707static inline void *k_queue_peek_head(struct k_queue *queue)
1708{
1709 return sys_slist_peek_head(&queue->data_q);
1710}
1711
1712/**
1713 * @brief Peek element at the tail of queue.
1714 *
1715 * Return element from the tail of queue without removing it.
1716 *
1717 * @param queue Address of the queue.
1718 *
1719 * @return Tail element, or NULL if queue is empty.
1720 */
1721static inline void *k_queue_peek_tail(struct k_queue *queue)
1722{
1723 return sys_slist_peek_tail(&queue->data_q);
1724}
1725
1726/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001727 * @brief Statically define and initialize a queue.
1728 *
1729 * The queue can be accessed outside the module where it is defined using:
1730 *
1731 * @code extern struct k_queue <name>; @endcode
1732 *
1733 * @param name Name of the queue.
1734 */
1735#define K_QUEUE_DEFINE(name) \
1736 struct k_queue name \
1737 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001738 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001739
1740/**
1741 * @} end defgroup queue_apis
1742 */
1743
1744/**
1745 * @cond INTERNAL_HIDDEN
1746 */
1747
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001748struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001749 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001750};
1751
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001752#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001753 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001754 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001755 }
1756
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001757#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1758
Allan Stephensc98da842016-11-11 15:45:03 -05001759/**
1760 * INTERNAL_HIDDEN @endcond
1761 */
1762
1763/**
1764 * @defgroup fifo_apis Fifo APIs
1765 * @ingroup kernel_apis
1766 * @{
1767 */
1768
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001769/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001770 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001771 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001772 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001773 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001774 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001775 *
1776 * @return N/A
1777 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001778#define k_fifo_init(fifo) \
1779 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001780
1781/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001782 * @brief Cancel waiting on a fifo.
1783 *
1784 * This routine causes first thread pending on @a fifo, if any, to
1785 * return from k_fifo_get() call with NULL value (as if timeout
1786 * expired).
1787 *
1788 * @note Can be called by ISRs.
1789 *
1790 * @param fifo Address of the fifo.
1791 *
1792 * @return N/A
1793 */
1794#define k_fifo_cancel_wait(fifo) \
1795 k_queue_cancel_wait((struct k_queue *) fifo)
1796
1797/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001798 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001799 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001800 * This routine adds a data item to @a fifo. A fifo data item must be
1801 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1802 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001804 * @note Can be called by ISRs.
1805 *
1806 * @param fifo Address of the fifo.
1807 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001808 *
1809 * @return N/A
1810 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001811#define k_fifo_put(fifo, data) \
1812 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001813
1814/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001815 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001817 * This routine adds a list of data items to @a fifo in one operation.
1818 * The data items must be in a singly-linked list, with the first 32 bits
1819 * each data item pointing to the next data item; the list must be
1820 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001821 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001822 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001824 * @param fifo Address of the fifo.
1825 * @param head Pointer to first node in singly-linked list.
1826 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001827 *
1828 * @return N/A
1829 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001830#define k_fifo_put_list(fifo, head, tail) \
1831 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001832
1833/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001834 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001835 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001836 * This routine adds a list of data items to @a fifo in one operation.
1837 * The data items must be in a singly-linked list implemented using a
1838 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001839 * and must be re-initialized via sys_slist_init().
1840 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001841 * @note Can be called by ISRs.
1842 *
1843 * @param fifo Address of the fifo.
1844 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001845 *
1846 * @return N/A
1847 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001848#define k_fifo_put_slist(fifo, list) \
1849 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001850
1851/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001852 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001854 * This routine removes a data item from @a fifo in a "first in, first out"
1855 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001856 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001857 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1858 *
1859 * @param fifo Address of the fifo.
1860 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001861 * or one of the special values K_NO_WAIT and K_FOREVER.
1862 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001863 * @return Address of the data item if successful; NULL if returned
1864 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001865 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001866#define k_fifo_get(fifo, timeout) \
1867 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001868
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001869/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001870 * @brief Query a fifo to see if it has data available.
1871 *
1872 * Note that the data might be already gone by the time this function returns
1873 * if other threads is also trying to read from the fifo.
1874 *
1875 * @note Can be called by ISRs.
1876 *
1877 * @param fifo Address of the fifo.
1878 *
1879 * @return Non-zero if the fifo is empty.
1880 * @return 0 if data is available.
1881 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001882#define k_fifo_is_empty(fifo) \
1883 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001884
1885/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001886 * @brief Peek element at the head of fifo.
1887 *
1888 * Return element from the head of fifo without removing it. A usecase
1889 * for this is if elements of the fifo are themselves containers. Then
1890 * on each iteration of processing, a head container will be peeked,
1891 * and some data processed out of it, and only if the container is empty,
1892 * it will be completely remove from the fifo.
1893 *
1894 * @param fifo Address of the fifo.
1895 *
1896 * @return Head element, or NULL if the fifo is empty.
1897 */
1898#define k_fifo_peek_head(fifo) \
1899 k_queue_peek_head((struct k_queue *) fifo)
1900
1901/**
1902 * @brief Peek element at the tail of fifo.
1903 *
1904 * Return element from the tail of fifo (without removing it). A usecase
1905 * for this is if elements of the fifo are themselves containers. Then
1906 * it may be useful to add more data to the last container in fifo.
1907 *
1908 * @param fifo Address of the fifo.
1909 *
1910 * @return Tail element, or NULL if fifo is empty.
1911 */
1912#define k_fifo_peek_tail(fifo) \
1913 k_queue_peek_tail((struct k_queue *) fifo)
1914
1915/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001916 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001917 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001918 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001919 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001920 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001921 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001922 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001923 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001924#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001925 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001926 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001927 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001928
Allan Stephensc98da842016-11-11 15:45:03 -05001929/**
1930 * @} end defgroup fifo_apis
1931 */
1932
1933/**
1934 * @cond INTERNAL_HIDDEN
1935 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001936
1937struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001938 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001939};
1940
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001941#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001942 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001943 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001944 }
1945
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001946#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1947
Allan Stephensc98da842016-11-11 15:45:03 -05001948/**
1949 * INTERNAL_HIDDEN @endcond
1950 */
1951
1952/**
1953 * @defgroup lifo_apis Lifo APIs
1954 * @ingroup kernel_apis
1955 * @{
1956 */
1957
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001958/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001959 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001960 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001961 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001963 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001964 *
1965 * @return N/A
1966 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001967#define k_lifo_init(lifo) \
1968 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001969
1970/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001971 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001973 * This routine adds a data item to @a lifo. A lifo data item must be
1974 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1975 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001976 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001977 * @note Can be called by ISRs.
1978 *
1979 * @param lifo Address of the lifo.
1980 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001981 *
1982 * @return N/A
1983 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001984#define k_lifo_put(lifo, data) \
1985 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001986
1987/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001988 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001989 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001990 * This routine removes a data item from @a lifo in a "last in, first out"
1991 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001992 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001993 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1994 *
1995 * @param lifo Address of the lifo.
1996 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001997 * or one of the special values K_NO_WAIT and K_FOREVER.
1998 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001999 * @return Address of the data item if successful; NULL if returned
2000 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002001 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002002#define k_lifo_get(lifo, timeout) \
2003 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002004
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002005/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002006 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002007 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002008 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002009 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002010 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002011 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002012 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002013 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002014#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002015 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002016 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002017 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002018
Allan Stephensc98da842016-11-11 15:45:03 -05002019/**
2020 * @} end defgroup lifo_apis
2021 */
2022
2023/**
2024 * @cond INTERNAL_HIDDEN
2025 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002026
2027struct k_stack {
2028 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002029 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002030
Anas Nashif2f203c22016-12-18 06:57:45 -05002031 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002032};
2033
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002034#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002035 { \
2036 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2037 .base = stack_buffer, \
2038 .next = stack_buffer, \
2039 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002040 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002041 }
2042
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002043#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2044
Allan Stephensc98da842016-11-11 15:45:03 -05002045/**
2046 * INTERNAL_HIDDEN @endcond
2047 */
2048
2049/**
2050 * @defgroup stack_apis Stack APIs
2051 * @ingroup kernel_apis
2052 * @{
2053 */
2054
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002055/**
2056 * @brief Initialize a stack.
2057 *
2058 * This routine initializes a stack object, prior to its first use.
2059 *
2060 * @param stack Address of the stack.
2061 * @param buffer Address of array used to hold stacked values.
2062 * @param num_entries Maximum number of values that can be stacked.
2063 *
2064 * @return N/A
2065 */
Andrew Boiee8734462017-09-29 16:42:07 -07002066__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002067 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002068
2069/**
2070 * @brief Push an element onto a stack.
2071 *
2072 * This routine adds a 32-bit value @a data to @a stack.
2073 *
2074 * @note Can be called by ISRs.
2075 *
2076 * @param stack Address of the stack.
2077 * @param data Value to push onto the stack.
2078 *
2079 * @return N/A
2080 */
Andrew Boiee8734462017-09-29 16:42:07 -07002081__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002082
2083/**
2084 * @brief Pop an element from a stack.
2085 *
2086 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2087 * manner and stores the value in @a data.
2088 *
2089 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2090 *
2091 * @param stack Address of the stack.
2092 * @param data Address of area to hold the value popped from the stack.
2093 * @param timeout Waiting period to obtain a value (in milliseconds),
2094 * or one of the special values K_NO_WAIT and K_FOREVER.
2095 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002096 * @retval 0 Element popped from stack.
2097 * @retval -EBUSY Returned without waiting.
2098 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002099 */
Andrew Boiee8734462017-09-29 16:42:07 -07002100__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002101
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002102/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002103 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002104 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002105 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002106 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002107 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002108 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002109 * @param name Name of the stack.
2110 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002111 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002112#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002113 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002114 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002115 struct k_stack name \
2116 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002117 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002118 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002119
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002120/**
Allan Stephensc98da842016-11-11 15:45:03 -05002121 * @} end defgroup stack_apis
2122 */
2123
Allan Stephens6bba9b02016-11-16 14:56:54 -05002124struct k_work;
2125
Allan Stephensc98da842016-11-11 15:45:03 -05002126/**
2127 * @defgroup workqueue_apis Workqueue Thread APIs
2128 * @ingroup kernel_apis
2129 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002130 */
2131
Allan Stephens6bba9b02016-11-16 14:56:54 -05002132/**
2133 * @typedef k_work_handler_t
2134 * @brief Work item handler function type.
2135 *
2136 * A work item's handler function is executed by a workqueue's thread
2137 * when the work item is processed by the workqueue.
2138 *
2139 * @param work Address of the work item.
2140 *
2141 * @return N/A
2142 */
2143typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002144
2145/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002146 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002147 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002148
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002149struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002150 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002151 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002152};
2153
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002154enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002155 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002156};
2157
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002158struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002159 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002160 k_work_handler_t handler;
2161 atomic_t flags[1];
2162};
2163
Allan Stephens6bba9b02016-11-16 14:56:54 -05002164struct k_delayed_work {
2165 struct k_work work;
2166 struct _timeout timeout;
2167 struct k_work_q *work_q;
2168};
2169
2170extern struct k_work_q k_sys_work_q;
2171
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002172/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002173 * INTERNAL_HIDDEN @endcond
2174 */
2175
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002176#define _K_WORK_INITIALIZER(work_handler) \
2177 { \
2178 ._reserved = NULL, \
2179 .handler = work_handler, \
2180 .flags = { 0 } \
2181 }
2182
2183#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2184
Allan Stephens6bba9b02016-11-16 14:56:54 -05002185/**
2186 * @brief Initialize a statically-defined work item.
2187 *
2188 * This macro can be used to initialize a statically-defined workqueue work
2189 * item, prior to its first use. For example,
2190 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002191 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002192 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002193 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002194 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002195 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002196#define K_WORK_DEFINE(work, work_handler) \
2197 struct k_work work \
2198 __in_section(_k_work, static, work) = \
2199 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002200
2201/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002202 * @brief Initialize a work item.
2203 *
2204 * This routine initializes a workqueue work item, prior to its first use.
2205 *
2206 * @param work Address of work item.
2207 * @param handler Function to invoke each time work item is processed.
2208 *
2209 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002210 */
2211static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2212{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002213 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002214 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002215 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002216}
2217
2218/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002219 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002220 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002221 * This routine submits work item @a work to be processed by workqueue
2222 * @a work_q. If the work item is already pending in the workqueue's queue
2223 * as a result of an earlier submission, this routine has no effect on the
2224 * work item. If the work item has already been processed, or is currently
2225 * being processed, its work is considered complete and the work item can be
2226 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002227 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002228 * @warning
2229 * A submitted work item must not be modified until it has been processed
2230 * by the workqueue.
2231 *
2232 * @note Can be called by ISRs.
2233 *
2234 * @param work_q Address of workqueue.
2235 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002236 *
2237 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002238 */
2239static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2240 struct k_work *work)
2241{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002242 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002243 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002244 }
2245}
2246
2247/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002248 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002249 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002250 * This routine indicates if work item @a work is pending in a workqueue's
2251 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002252 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002253 * @note Can be called by ISRs.
2254 *
2255 * @param work Address of work item.
2256 *
2257 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002258 */
2259static inline int k_work_pending(struct k_work *work)
2260{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002261 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002262}
2263
2264/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002265 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002266 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002267 * This routine starts workqueue @a work_q. The workqueue spawns its work
2268 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002269 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002270 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002271 * @param stack Pointer to work queue thread's stack space, as defined by
2272 * K_THREAD_STACK_DEFINE()
2273 * @param stack_size Size of the work queue thread's stack (in bytes), which
2274 * should either be the same constant passed to
2275 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002276 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002277 *
2278 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002279 */
Andrew Boie507852a2017-07-25 18:47:07 -07002280extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002281 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002282 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002283
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002284/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002285 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002286 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002287 * This routine initializes a workqueue delayed work item, prior to
2288 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002289 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002290 * @param work Address of delayed work item.
2291 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002292 *
2293 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002294 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002295extern void k_delayed_work_init(struct k_delayed_work *work,
2296 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002297
2298/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002299 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002300 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002301 * This routine schedules work item @a work to be processed by workqueue
2302 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002303 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002304 * Only when the countdown completes is the work item actually submitted to
2305 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002306 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002307 * Submitting a previously submitted delayed work item that is still
2308 * counting down cancels the existing submission and restarts the countdown
2309 * using the new delay. If the work item is currently pending on the
2310 * workqueue's queue because the countdown has completed it is too late to
2311 * resubmit the item, and resubmission fails without impacting the work item.
2312 * If the work item has already been processed, or is currently being processed,
2313 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002314 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002315 * @warning
2316 * A delayed work item must not be modified until it has been processed
2317 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002318 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002319 * @note Can be called by ISRs.
2320 *
2321 * @param work_q Address of workqueue.
2322 * @param work Address of delayed work item.
2323 * @param delay Delay before submitting the work item (in milliseconds).
2324 *
2325 * @retval 0 Work item countdown started.
2326 * @retval -EINPROGRESS Work item is already pending.
2327 * @retval -EINVAL Work item is being processed or has completed its work.
2328 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002329 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002330extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2331 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002332 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002333
2334/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002335 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002336 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002337 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002338 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002339 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002340 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002341 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002342 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002343 * @param work Address of delayed work item.
2344 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002345 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002346 * @retval -EINPROGRESS Work item is already pending.
2347 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002348 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002349extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002350
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002351/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002352 * @brief Submit a work item to the system workqueue.
2353 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002354 * This routine submits work item @a work to be processed by the system
2355 * workqueue. If the work item is already pending in the workqueue's queue
2356 * as a result of an earlier submission, this routine has no effect on the
2357 * work item. If the work item has already been processed, or is currently
2358 * being processed, its work is considered complete and the work item can be
2359 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002360 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002361 * @warning
2362 * Work items submitted to the system workqueue should avoid using handlers
2363 * that block or yield since this may prevent the system workqueue from
2364 * processing other work items in a timely manner.
2365 *
2366 * @note Can be called by ISRs.
2367 *
2368 * @param work Address of work item.
2369 *
2370 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002371 */
2372static inline void k_work_submit(struct k_work *work)
2373{
2374 k_work_submit_to_queue(&k_sys_work_q, work);
2375}
2376
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002377/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002378 * @brief Submit a delayed work item to the system workqueue.
2379 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002380 * This routine schedules work item @a work to be processed by the system
2381 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002382 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002383 * Only when the countdown completes is the work item actually submitted to
2384 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002385 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002386 * Submitting a previously submitted delayed work item that is still
2387 * counting down cancels the existing submission and restarts the countdown
2388 * using the new delay. If the work item is currently pending on the
2389 * workqueue's queue because the countdown has completed it is too late to
2390 * resubmit the item, and resubmission fails without impacting the work item.
2391 * If the work item has already been processed, or is currently being processed,
2392 * its work is considered complete and the work item can be resubmitted.
2393 *
2394 * @warning
2395 * Work items submitted to the system workqueue should avoid using handlers
2396 * that block or yield since this may prevent the system workqueue from
2397 * processing other work items in a timely manner.
2398 *
2399 * @note Can be called by ISRs.
2400 *
2401 * @param work Address of delayed work item.
2402 * @param delay Delay before submitting the work item (in milliseconds).
2403 *
2404 * @retval 0 Work item countdown started.
2405 * @retval -EINPROGRESS Work item is already pending.
2406 * @retval -EINVAL Work item is being processed or has completed its work.
2407 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002408 */
2409static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002410 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002411{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002412 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002413}
2414
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002415/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002416 * @brief Get time remaining before a delayed work gets scheduled.
2417 *
2418 * This routine computes the (approximate) time remaining before a
2419 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002420 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002421 *
2422 * @param work Delayed work item.
2423 *
2424 * @return Remaining time (in milliseconds).
2425 */
Kumar Galacc334c72017-04-21 10:55:34 -05002426static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002427{
2428 return _timeout_remaining_get(&work->timeout);
2429}
2430
2431/**
Allan Stephensc98da842016-11-11 15:45:03 -05002432 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002433 */
2434
Allan Stephensc98da842016-11-11 15:45:03 -05002435/**
2436 * @cond INTERNAL_HIDDEN
2437 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438
2439struct k_mutex {
2440 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002441 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002442 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002443 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002444
Anas Nashif2f203c22016-12-18 06:57:45 -05002445 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002446};
2447
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002448#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002449 { \
2450 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2451 .owner = NULL, \
2452 .lock_count = 0, \
2453 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002454 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002455 }
2456
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002457#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2458
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002459/**
Allan Stephensc98da842016-11-11 15:45:03 -05002460 * INTERNAL_HIDDEN @endcond
2461 */
2462
2463/**
2464 * @defgroup mutex_apis Mutex APIs
2465 * @ingroup kernel_apis
2466 * @{
2467 */
2468
2469/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002470 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002471 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002472 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002473 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002474 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002475 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002476 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002477 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002478#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002479 struct k_mutex name \
2480 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002481 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002482
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002483/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002484 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002485 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002486 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002487 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002488 * Upon completion, the mutex is available and does not have an owner.
2489 *
2490 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002491 *
2492 * @return N/A
2493 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002494__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002495
2496/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002497 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002498 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002499 * This routine locks @a mutex. If the mutex is locked by another thread,
2500 * the calling thread waits until the mutex becomes available or until
2501 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002502 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002503 * A thread is permitted to lock a mutex it has already locked. The operation
2504 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002505 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002506 * @param mutex Address of the mutex.
2507 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002508 * or one of the special values K_NO_WAIT and K_FOREVER.
2509 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002510 * @retval 0 Mutex locked.
2511 * @retval -EBUSY Returned without waiting.
2512 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002513 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002514__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002515
2516/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002517 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002518 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002519 * This routine unlocks @a mutex. The mutex must already be locked by the
2520 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002521 *
2522 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002523 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002524 * thread.
2525 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002526 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002527 *
2528 * @return N/A
2529 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002530__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002531
Allan Stephensc98da842016-11-11 15:45:03 -05002532/**
2533 * @} end defgroup mutex_apis
2534 */
2535
2536/**
2537 * @cond INTERNAL_HIDDEN
2538 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002539
2540struct k_sem {
2541 _wait_q_t wait_q;
2542 unsigned int count;
2543 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002544 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002545
Anas Nashif2f203c22016-12-18 06:57:45 -05002546 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002547};
2548
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002549#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002550 { \
2551 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2552 .count = initial_count, \
2553 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002554 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002555 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002556 }
2557
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002558#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2559
Allan Stephensc98da842016-11-11 15:45:03 -05002560/**
2561 * INTERNAL_HIDDEN @endcond
2562 */
2563
2564/**
2565 * @defgroup semaphore_apis Semaphore APIs
2566 * @ingroup kernel_apis
2567 * @{
2568 */
2569
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002570/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002571 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002572 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002573 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002574 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002575 * @param sem Address of the semaphore.
2576 * @param initial_count Initial semaphore count.
2577 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002578 *
2579 * @return N/A
2580 */
Andrew Boie99280232017-09-29 14:17:47 -07002581__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2582 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002583
2584/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002585 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002586 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002587 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002589 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2590 *
2591 * @param sem Address of the semaphore.
2592 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002593 * or one of the special values K_NO_WAIT and K_FOREVER.
2594 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002595 * @note When porting code from the nanokernel legacy API to the new API, be
2596 * careful with the return value of this function. The return value is the
2597 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2598 * non-zero means failure, while the nano_sem_take family returns 1 for success
2599 * and 0 for failure.
2600 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002601 * @retval 0 Semaphore taken.
2602 * @retval -EBUSY Returned without waiting.
2603 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002604 */
Andrew Boie99280232017-09-29 14:17:47 -07002605__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002606
2607/**
2608 * @brief Give a semaphore.
2609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002610 * This routine gives @a sem, unless the semaphore is already at its maximum
2611 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002612 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002613 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002614 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002615 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002616 *
2617 * @return N/A
2618 */
Andrew Boie99280232017-09-29 14:17:47 -07002619__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002620
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002621/**
2622 * @brief Reset a semaphore's count to zero.
2623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002624 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002626 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002627 *
2628 * @return N/A
2629 */
Andrew Boie990bf162017-10-03 12:36:49 -07002630__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002631
2632static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002633{
2634 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002635}
2636
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002637/**
2638 * @brief Get a semaphore's count.
2639 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002640 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002641 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002642 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002644 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002645 */
Andrew Boie990bf162017-10-03 12:36:49 -07002646__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002647
2648static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002649{
2650 return sem->count;
2651}
2652
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002653/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002654 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002656 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002657 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002658 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002659 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002660 * @param name Name of the semaphore.
2661 * @param initial_count Initial semaphore count.
2662 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002663 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002664#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002665 struct k_sem name \
2666 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002667 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002668
Allan Stephensc98da842016-11-11 15:45:03 -05002669/**
2670 * @} end defgroup semaphore_apis
2671 */
2672
2673/**
2674 * @defgroup alert_apis Alert APIs
2675 * @ingroup kernel_apis
2676 * @{
2677 */
2678
Allan Stephens5eceb852016-11-16 10:16:30 -05002679/**
2680 * @typedef k_alert_handler_t
2681 * @brief Alert handler function type.
2682 *
2683 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002684 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002685 * and is only invoked if the alert has been initialized with one.
2686 *
2687 * @param alert Address of the alert.
2688 *
2689 * @return 0 if alert has been consumed; non-zero if alert should pend.
2690 */
2691typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002692
2693/**
2694 * @} end defgroup alert_apis
2695 */
2696
2697/**
2698 * @cond INTERNAL_HIDDEN
2699 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002700
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002701#define K_ALERT_DEFAULT NULL
2702#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002703
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002704struct k_alert {
2705 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002706 atomic_t send_count;
2707 struct k_work work_item;
2708 struct k_sem sem;
2709
Anas Nashif2f203c22016-12-18 06:57:45 -05002710 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002711};
2712
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002713extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002714
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002715#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002716 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002717 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002718 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002719 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2720 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002721 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002722 }
2723
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002724#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2725
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002726/**
Allan Stephensc98da842016-11-11 15:45:03 -05002727 * INTERNAL_HIDDEN @endcond
2728 */
2729
2730/**
2731 * @addtogroup alert_apis
2732 * @{
2733 */
2734
2735/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002736 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002737 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002738 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002739 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002740 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002741 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002742 * @param name Name of the alert.
2743 * @param alert_handler Action to take when alert is sent. Specify either
2744 * the address of a function to be invoked by the system workqueue
2745 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2746 * K_ALERT_DEFAULT (which causes the alert to pend).
2747 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002748 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002749#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002750 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002751 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002752 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002753 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002754
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002755/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002756 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002757 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002758 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002760 * @param alert Address of the alert.
2761 * @param handler Action to take when alert is sent. Specify either the address
2762 * of a function to be invoked by the system workqueue thread,
2763 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2764 * K_ALERT_DEFAULT (which causes the alert to pend).
2765 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002766 *
2767 * @return N/A
2768 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002769extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2770 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002771
2772/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002773 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002775 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002776 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002777 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2778 *
2779 * @param alert Address of the alert.
2780 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002781 * or one of the special values K_NO_WAIT and K_FOREVER.
2782 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002783 * @retval 0 Alert received.
2784 * @retval -EBUSY Returned without waiting.
2785 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002786 */
Andrew Boie310e9872017-09-29 04:41:15 -07002787__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002788
2789/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002790 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002791 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002792 * This routine signals @a alert. The action specified for @a alert will
2793 * be taken, which may trigger the execution of an alert handler function
2794 * and/or cause the alert to pend (assuming the alert has not reached its
2795 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002796 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002797 * @note Can be called by ISRs.
2798 *
2799 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002800 *
2801 * @return N/A
2802 */
Andrew Boie310e9872017-09-29 04:41:15 -07002803__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002804
2805/**
Allan Stephensc98da842016-11-11 15:45:03 -05002806 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002807 */
2808
Allan Stephensc98da842016-11-11 15:45:03 -05002809/**
2810 * @cond INTERNAL_HIDDEN
2811 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002812
2813struct k_msgq {
2814 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002815 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002816 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002817 char *buffer_start;
2818 char *buffer_end;
2819 char *read_ptr;
2820 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002821 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002822
Anas Nashif2f203c22016-12-18 06:57:45 -05002823 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002824};
2825
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002826#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002827 { \
2828 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002829 .max_msgs = q_max_msgs, \
2830 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002831 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002832 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002833 .read_ptr = q_buffer, \
2834 .write_ptr = q_buffer, \
2835 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002836 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002837 }
2838
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002839#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2840
Peter Mitsis1da807e2016-10-06 11:36:59 -04002841/**
Allan Stephensc98da842016-11-11 15:45:03 -05002842 * INTERNAL_HIDDEN @endcond
2843 */
2844
2845/**
2846 * @defgroup msgq_apis Message Queue APIs
2847 * @ingroup kernel_apis
2848 * @{
2849 */
2850
2851/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002852 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002854 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2855 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002856 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2857 * message is similarly aligned to this boundary, @a q_msg_size must also be
2858 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002860 * The message queue can be accessed outside the module where it is defined
2861 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002862 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002863 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002865 * @param q_name Name of the message queue.
2866 * @param q_msg_size Message size (in bytes).
2867 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002868 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002869 */
2870#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2871 static char __noinit __aligned(q_align) \
2872 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002873 struct k_msgq q_name \
2874 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002875 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002876 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002877
Peter Mitsisd7a37502016-10-13 11:37:40 -04002878/**
2879 * @brief Initialize a message queue.
2880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002881 * This routine initializes a message queue object, prior to its first use.
2882 *
Allan Stephensda827222016-11-09 14:23:58 -06002883 * The message queue's ring buffer must contain space for @a max_msgs messages,
2884 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2885 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2886 * that each message is similarly aligned to this boundary, @a q_msg_size
2887 * must also be a multiple of N.
2888 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002889 * @param q Address of the message queue.
2890 * @param buffer Pointer to ring buffer that holds queued messages.
2891 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002892 * @param max_msgs Maximum number of messages that can be queued.
2893 *
2894 * @return N/A
2895 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002896__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2897 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002898
2899/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002900 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002901 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002902 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002903 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002904 * @note Can be called by ISRs.
2905 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002906 * @param q Address of the message queue.
2907 * @param data Pointer to the message.
2908 * @param timeout Waiting period to add the message (in milliseconds),
2909 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002910 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002911 * @retval 0 Message sent.
2912 * @retval -ENOMSG Returned without waiting or queue purged.
2913 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002914 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002915__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002916
2917/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002918 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002920 * This routine receives a message from message queue @a q in a "first in,
2921 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002922 *
Allan Stephensc98da842016-11-11 15:45:03 -05002923 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002924 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002925 * @param q Address of the message queue.
2926 * @param data Address of area to hold the received message.
2927 * @param timeout Waiting period to receive the message (in milliseconds),
2928 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002929 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002930 * @retval 0 Message received.
2931 * @retval -ENOMSG Returned without waiting.
2932 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002933 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002934__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002935
2936/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002937 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002938 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002939 * This routine discards all unreceived messages in a message queue's ring
2940 * buffer. Any threads that are blocked waiting to send a message to the
2941 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002942 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002943 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002944 *
2945 * @return N/A
2946 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002947__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002948
Peter Mitsis67be2492016-10-07 11:44:34 -04002949/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002950 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002951 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002952 * This routine returns the number of unused entries in a message queue's
2953 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002954 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002955 * @param q Address of the message queue.
2956 *
2957 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002958 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002959__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
2960
2961static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002962{
2963 return q->max_msgs - q->used_msgs;
2964}
2965
Peter Mitsisd7a37502016-10-13 11:37:40 -04002966/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002967 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002968 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002969 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002971 * @param q Address of the message queue.
2972 *
2973 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002974 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002975__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
2976
2977static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002978{
2979 return q->used_msgs;
2980}
2981
Allan Stephensc98da842016-11-11 15:45:03 -05002982/**
2983 * @} end defgroup msgq_apis
2984 */
2985
2986/**
2987 * @defgroup mem_pool_apis Memory Pool APIs
2988 * @ingroup kernel_apis
2989 * @{
2990 */
2991
Andy Ross73cb9582017-05-09 10:42:39 -07002992/* Note on sizing: the use of a 20 bit field for block means that,
2993 * assuming a reasonable minimum block size of 16 bytes, we're limited
2994 * to 16M of memory managed by a single pool. Long term it would be
2995 * good to move to a variable bit size based on configuration.
2996 */
2997struct k_mem_block_id {
2998 u32_t pool : 8;
2999 u32_t level : 4;
3000 u32_t block : 20;
3001};
3002
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003003struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003004 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003005 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003006};
3007
Allan Stephensc98da842016-11-11 15:45:03 -05003008/**
3009 * @} end defgroup mem_pool_apis
3010 */
3011
3012/**
3013 * @defgroup mailbox_apis Mailbox APIs
3014 * @ingroup kernel_apis
3015 * @{
3016 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003017
3018struct k_mbox_msg {
3019 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003020 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003021 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003022 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003023 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003024 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003025 /** sender's message data buffer */
3026 void *tx_data;
3027 /** internal use only - needed for legacy API support */
3028 void *_rx_data;
3029 /** message data block descriptor */
3030 struct k_mem_block tx_block;
3031 /** source thread id */
3032 k_tid_t rx_source_thread;
3033 /** target thread id */
3034 k_tid_t tx_target_thread;
3035 /** internal use only - thread waiting on send (may be a dummy) */
3036 k_tid_t _syncing_thread;
3037#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3038 /** internal use only - semaphore used during asynchronous send */
3039 struct k_sem *_async_sem;
3040#endif
3041};
3042
Allan Stephensc98da842016-11-11 15:45:03 -05003043/**
3044 * @cond INTERNAL_HIDDEN
3045 */
3046
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003047struct k_mbox {
3048 _wait_q_t tx_msg_queue;
3049 _wait_q_t rx_msg_queue;
3050
Anas Nashif2f203c22016-12-18 06:57:45 -05003051 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003052};
3053
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003054#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003055 { \
3056 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3057 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003058 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003059 }
3060
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003061#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3062
Peter Mitsis12092702016-10-14 12:57:23 -04003063/**
Allan Stephensc98da842016-11-11 15:45:03 -05003064 * INTERNAL_HIDDEN @endcond
3065 */
3066
3067/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003068 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003069 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003070 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003071 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003072 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003073 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003074 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003075 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003076#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003077 struct k_mbox name \
3078 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003079 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003080
Peter Mitsis12092702016-10-14 12:57:23 -04003081/**
3082 * @brief Initialize a mailbox.
3083 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003084 * This routine initializes a mailbox object, prior to its first use.
3085 *
3086 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003087 *
3088 * @return N/A
3089 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003090extern void k_mbox_init(struct k_mbox *mbox);
3091
Peter Mitsis12092702016-10-14 12:57:23 -04003092/**
3093 * @brief Send a mailbox message in a synchronous manner.
3094 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003095 * This routine sends a message to @a mbox and waits for a receiver to both
3096 * receive and process it. The message data may be in a buffer, in a memory
3097 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003098 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003099 * @param mbox Address of the mailbox.
3100 * @param tx_msg Address of the transmit message descriptor.
3101 * @param timeout Waiting period for the message to be received (in
3102 * milliseconds), or one of the special values K_NO_WAIT
3103 * and K_FOREVER. Once the message has been received,
3104 * this routine waits as long as necessary for the message
3105 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003106 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003107 * @retval 0 Message sent.
3108 * @retval -ENOMSG Returned without waiting.
3109 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003110 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003111extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003112 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003113
Peter Mitsis12092702016-10-14 12:57:23 -04003114/**
3115 * @brief Send a mailbox message in an asynchronous manner.
3116 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003117 * This routine sends a message to @a mbox without waiting for a receiver
3118 * to process it. The message data may be in a buffer, in a memory pool block,
3119 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3120 * will be given when the message has been both received and completely
3121 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003122 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003123 * @param mbox Address of the mailbox.
3124 * @param tx_msg Address of the transmit message descriptor.
3125 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003126 *
3127 * @return N/A
3128 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003129extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003130 struct k_sem *sem);
3131
Peter Mitsis12092702016-10-14 12:57:23 -04003132/**
3133 * @brief Receive a mailbox message.
3134 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003135 * This routine receives a message from @a mbox, then optionally retrieves
3136 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003137 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003138 * @param mbox Address of the mailbox.
3139 * @param rx_msg Address of the receive message descriptor.
3140 * @param buffer Address of the buffer to receive data, or NULL to defer data
3141 * retrieval and message disposal until later.
3142 * @param timeout Waiting period for a message to be received (in
3143 * milliseconds), or one of the special values K_NO_WAIT
3144 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003145 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003146 * @retval 0 Message received.
3147 * @retval -ENOMSG Returned without waiting.
3148 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003149 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003150extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003151 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003152
3153/**
3154 * @brief Retrieve mailbox message data into a buffer.
3155 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003156 * This routine completes the processing of a received message by retrieving
3157 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003158 *
3159 * Alternatively, this routine can be used to dispose of a received message
3160 * without retrieving its data.
3161 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003162 * @param rx_msg Address of the receive message descriptor.
3163 * @param buffer Address of the buffer to receive data, or NULL to discard
3164 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003165 *
3166 * @return N/A
3167 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003168extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003169
3170/**
3171 * @brief Retrieve mailbox message data into a memory pool block.
3172 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003173 * This routine completes the processing of a received message by retrieving
3174 * its data into a memory pool block, then disposing of the message.
3175 * The memory pool block that results from successful retrieval must be
3176 * returned to the pool once the data has been processed, even in cases
3177 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003178 *
3179 * Alternatively, this routine can be used to dispose of a received message
3180 * without retrieving its data. In this case there is no need to return a
3181 * memory pool block to the pool.
3182 *
3183 * This routine allocates a new memory pool block for the data only if the
3184 * data is not already in one. If a new block cannot be allocated, the routine
3185 * returns a failure code and the received message is left unchanged. This
3186 * permits the caller to reattempt data retrieval at a later time or to dispose
3187 * of the received message without retrieving its data.
3188 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003189 * @param rx_msg Address of a receive message descriptor.
3190 * @param pool Address of memory pool, or NULL to discard data.
3191 * @param block Address of the area to hold memory pool block info.
3192 * @param timeout Waiting period to wait for a memory pool block (in
3193 * milliseconds), or one of the special values K_NO_WAIT
3194 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003195 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003196 * @retval 0 Data retrieved.
3197 * @retval -ENOMEM Returned without waiting.
3198 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003199 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003200extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003201 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003202 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003203
Allan Stephensc98da842016-11-11 15:45:03 -05003204/**
3205 * @} end defgroup mailbox_apis
3206 */
3207
3208/**
3209 * @cond INTERNAL_HIDDEN
3210 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003211
3212struct k_pipe {
3213 unsigned char *buffer; /* Pipe buffer: may be NULL */
3214 size_t size; /* Buffer size */
3215 size_t bytes_used; /* # bytes used in buffer */
3216 size_t read_index; /* Where in buffer to read from */
3217 size_t write_index; /* Where in buffer to write */
3218
3219 struct {
3220 _wait_q_t readers; /* Reader wait queue */
3221 _wait_q_t writers; /* Writer wait queue */
3222 } wait_q;
3223
Anas Nashif2f203c22016-12-18 06:57:45 -05003224 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003225};
3226
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003227#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003228 { \
3229 .buffer = pipe_buffer, \
3230 .size = pipe_buffer_size, \
3231 .bytes_used = 0, \
3232 .read_index = 0, \
3233 .write_index = 0, \
3234 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3235 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003236 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003237 }
3238
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003239#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3240
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003241/**
Allan Stephensc98da842016-11-11 15:45:03 -05003242 * INTERNAL_HIDDEN @endcond
3243 */
3244
3245/**
3246 * @defgroup pipe_apis Pipe APIs
3247 * @ingroup kernel_apis
3248 * @{
3249 */
3250
3251/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003252 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003253 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003254 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003255 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003256 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003257 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003258 * @param name Name of the pipe.
3259 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3260 * or zero if no ring buffer is used.
3261 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003262 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003263#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3264 static unsigned char __noinit __aligned(pipe_align) \
3265 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003266 struct k_pipe name \
3267 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003268 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003269
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003270/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003271 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003272 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003273 * This routine initializes a pipe object, prior to its first use.
3274 *
3275 * @param pipe Address of the pipe.
3276 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3277 * is used.
3278 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3279 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003280 *
3281 * @return N/A
3282 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003283__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3284 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003285
3286/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003287 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003288 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003289 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003290 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003291 * @param pipe Address of the pipe.
3292 * @param data Address of data to write.
3293 * @param bytes_to_write Size of data (in bytes).
3294 * @param bytes_written Address of area to hold the number of bytes written.
3295 * @param min_xfer Minimum number of bytes to write.
3296 * @param timeout Waiting period to wait for the data to be written (in
3297 * milliseconds), or one of the special values K_NO_WAIT
3298 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003299 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003300 * @retval 0 At least @a min_xfer bytes of data were written.
3301 * @retval -EIO Returned without waiting; zero data bytes were written.
3302 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003303 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003304 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003305__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3306 size_t bytes_to_write, size_t *bytes_written,
3307 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003308
3309/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003310 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003311 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003312 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003313 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003314 * @param pipe Address of the pipe.
3315 * @param data Address to place the data read from pipe.
3316 * @param bytes_to_read Maximum number of data bytes to read.
3317 * @param bytes_read Address of area to hold the number of bytes read.
3318 * @param min_xfer Minimum number of data bytes to read.
3319 * @param timeout Waiting period to wait for the data to be read (in
3320 * milliseconds), or one of the special values K_NO_WAIT
3321 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003322 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003323 * @retval 0 At least @a min_xfer bytes of data were read.
3324 * @retval -EIO Returned without waiting; zero data bytes were read.
3325 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003326 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003327 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003328__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3329 size_t bytes_to_read, size_t *bytes_read,
3330 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003331
3332/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003333 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003334 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003335 * This routine writes the data contained in a memory block to @a pipe.
3336 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003337 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003338 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003339 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003340 * @param block Memory block containing data to send
3341 * @param size Number of data bytes in memory block to send
3342 * @param sem Semaphore to signal upon completion (else NULL)
3343 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003344 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003345 */
3346extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3347 size_t size, struct k_sem *sem);
3348
3349/**
Allan Stephensc98da842016-11-11 15:45:03 -05003350 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003351 */
3352
Allan Stephensc98da842016-11-11 15:45:03 -05003353/**
3354 * @cond INTERNAL_HIDDEN
3355 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003356
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003357struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003358 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003359 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003360 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003361 char *buffer;
3362 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003363 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003364
Anas Nashif2f203c22016-12-18 06:57:45 -05003365 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003366};
3367
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003368#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003369 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003370 { \
3371 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003372 .num_blocks = slab_num_blocks, \
3373 .block_size = slab_block_size, \
3374 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003375 .free_list = NULL, \
3376 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003377 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003378 }
3379
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003380#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3381
3382
Peter Mitsis578f9112016-10-07 13:50:31 -04003383/**
Allan Stephensc98da842016-11-11 15:45:03 -05003384 * INTERNAL_HIDDEN @endcond
3385 */
3386
3387/**
3388 * @defgroup mem_slab_apis Memory Slab APIs
3389 * @ingroup kernel_apis
3390 * @{
3391 */
3392
3393/**
Allan Stephensda827222016-11-09 14:23:58 -06003394 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003395 *
Allan Stephensda827222016-11-09 14:23:58 -06003396 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003397 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003398 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3399 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003400 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003401 *
Allan Stephensda827222016-11-09 14:23:58 -06003402 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003403 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003404 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003405 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003406 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003407 * @param name Name of the memory slab.
3408 * @param slab_block_size Size of each memory block (in bytes).
3409 * @param slab_num_blocks Number memory blocks.
3410 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003411 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003412#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3413 char __noinit __aligned(slab_align) \
3414 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3415 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003416 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003417 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003418 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003419
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003420/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003421 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003422 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003423 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003424 *
Allan Stephensda827222016-11-09 14:23:58 -06003425 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3426 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3427 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3428 * To ensure that each memory block is similarly aligned to this boundary,
3429 * @a slab_block_size must also be a multiple of N.
3430 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003431 * @param slab Address of the memory slab.
3432 * @param buffer Pointer to buffer used for the memory blocks.
3433 * @param block_size Size of each memory block (in bytes).
3434 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003435 *
3436 * @return N/A
3437 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003438extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003439 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003440
3441/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003442 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003443 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003444 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003446 * @param slab Address of the memory slab.
3447 * @param mem Pointer to block address area.
3448 * @param timeout Maximum time to wait for operation to complete
3449 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3450 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003451 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003452 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003453 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003454 * @retval -ENOMEM Returned without waiting.
3455 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003456 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003457extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003458 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003459
3460/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003461 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003462 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003463 * This routine releases a previously allocated memory block back to its
3464 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003465 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003466 * @param slab Address of the memory slab.
3467 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003468 *
3469 * @return N/A
3470 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003471extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003472
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003473/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003474 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003475 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003476 * This routine gets the number of memory blocks that are currently
3477 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003478 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003479 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003480 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003481 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003482 */
Kumar Galacc334c72017-04-21 10:55:34 -05003483static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003484{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003485 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003486}
3487
Peter Mitsisc001aa82016-10-13 13:53:37 -04003488/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003489 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003490 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003491 * This routine gets the number of memory blocks that are currently
3492 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003493 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003494 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003495 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003496 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003497 */
Kumar Galacc334c72017-04-21 10:55:34 -05003498static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003499{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003500 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003501}
3502
Allan Stephensc98da842016-11-11 15:45:03 -05003503/**
3504 * @} end defgroup mem_slab_apis
3505 */
3506
3507/**
3508 * @cond INTERNAL_HIDDEN
3509 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003510
Andy Ross73cb9582017-05-09 10:42:39 -07003511struct k_mem_pool_lvl {
3512 union {
3513 u32_t *bits_p;
3514 u32_t bits;
3515 };
3516 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003517};
3518
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003519struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003520 void *buf;
3521 size_t max_sz;
3522 u16_t n_max;
3523 u8_t n_levels;
3524 u8_t max_inline_level;
3525 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003526 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003527};
3528
Andy Ross73cb9582017-05-09 10:42:39 -07003529#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003530
Andy Ross73cb9582017-05-09 10:42:39 -07003531#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3532
Andy Ross8cf7ff52017-11-13 14:59:13 -08003533#define __MPOOL_LVLS(maxsz, minsz) \
3534 (_MPOOL_HAVE_LVL((maxsz), (minsz), 0) + \
3535 _MPOOL_HAVE_LVL((maxsz), (minsz), 1) + \
3536 _MPOOL_HAVE_LVL((maxsz), (minsz), 2) + \
3537 _MPOOL_HAVE_LVL((maxsz), (minsz), 3) + \
3538 _MPOOL_HAVE_LVL((maxsz), (minsz), 4) + \
3539 _MPOOL_HAVE_LVL((maxsz), (minsz), 5) + \
3540 _MPOOL_HAVE_LVL((maxsz), (minsz), 6) + \
3541 _MPOOL_HAVE_LVL((maxsz), (minsz), 7) + \
3542 _MPOOL_HAVE_LVL((maxsz), (minsz), 8) + \
3543 _MPOOL_HAVE_LVL((maxsz), (minsz), 9) + \
3544 _MPOOL_HAVE_LVL((maxsz), (minsz), 10) + \
3545 _MPOOL_HAVE_LVL((maxsz), (minsz), 11) + \
3546 _MPOOL_HAVE_LVL((maxsz), (minsz), 12) + \
3547 _MPOOL_HAVE_LVL((maxsz), (minsz), 13) + \
3548 _MPOOL_HAVE_LVL((maxsz), (minsz), 14) + \
3549 _MPOOL_HAVE_LVL((maxsz), (minsz), 15))
3550
3551#define _MPOOL_MINBLK sizeof(sys_dnode_t)
3552
3553#define _MPOOL_LVLS(max, min) \
3554 __MPOOL_LVLS((max), (min) >= _MPOOL_MINBLK ? (min) : _MPOOL_MINBLK)
Andy Ross73cb9582017-05-09 10:42:39 -07003555
3556/* Rounds the needed bits up to integer multiples of u32_t */
3557#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3558 ((((n_max) << (2*(l))) + 31) / 32)
3559
3560/* One word gets stored free unioned with the pointer, otherwise the
3561 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003562 */
Andy Ross73cb9582017-05-09 10:42:39 -07003563#define _MPOOL_LBIT_WORDS(n_max, l) \
3564 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3565 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003566
Andy Ross73cb9582017-05-09 10:42:39 -07003567/* How many bytes for the bitfields of a single level? */
3568#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3569 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3570 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003571
Andy Ross73cb9582017-05-09 10:42:39 -07003572/* Size of the bitmap array that follows the buffer in allocated memory */
3573#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3574 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3575 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3576 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3577 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3578 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3579 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3580 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3581 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3582 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3583 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3584 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3585 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3586 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3587 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3588 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3589 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003590
3591/**
Allan Stephensc98da842016-11-11 15:45:03 -05003592 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003593 */
3594
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003595/**
Allan Stephensc98da842016-11-11 15:45:03 -05003596 * @addtogroup mem_pool_apis
3597 * @{
3598 */
3599
3600/**
3601 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003603 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3604 * long. The memory pool allows blocks to be repeatedly partitioned into
3605 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003606 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003607 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003608 * If the pool is to be accessed outside the module where it is defined, it
3609 * can be declared via
3610 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003611 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003612 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003613 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003614 * @param minsz Size of the smallest blocks in the pool (in bytes).
3615 * @param maxsz Size of the largest blocks in the pool (in bytes).
3616 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003617 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003618 */
Andy Ross73cb9582017-05-09 10:42:39 -07003619#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3620 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3621 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3622 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3623 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3624 .buf = _mpool_buf_##name, \
3625 .max_sz = maxsz, \
3626 .n_max = nmax, \
3627 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3628 .levels = _mpool_lvls_##name, \
3629 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003630
Peter Mitsis937042c2016-10-13 13:18:26 -04003631/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003632 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003633 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003634 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003635 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003636 * @param pool Address of the memory pool.
3637 * @param block Pointer to block descriptor for the allocated memory.
3638 * @param size Amount of memory to allocate (in bytes).
3639 * @param timeout Maximum time to wait for operation to complete
3640 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3641 * or K_FOREVER to wait as long as necessary.
3642 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003643 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003644 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003645 * @retval -ENOMEM Returned without waiting.
3646 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003647 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003648extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003649 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003650
3651/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003652 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003653 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003654 * This routine releases a previously allocated memory block back to its
3655 * memory pool.
3656 *
3657 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003658 *
3659 * @return N/A
3660 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003661extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003662
3663/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003664 * @brief Free memory allocated from a memory pool.
3665 *
3666 * This routine releases a previously allocated memory block back to its
3667 * memory pool
3668 *
3669 * @param id Memory block identifier.
3670 *
3671 * @return N/A
3672 */
3673extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3674
3675/**
Allan Stephensc98da842016-11-11 15:45:03 -05003676 * @} end addtogroup mem_pool_apis
3677 */
3678
3679/**
3680 * @defgroup heap_apis Heap Memory Pool APIs
3681 * @ingroup kernel_apis
3682 * @{
3683 */
3684
3685/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003686 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003687 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003688 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003689 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003690 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003691 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003692 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003693 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003694 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003695extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003696
3697/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003698 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003699 *
3700 * This routine provides traditional free() semantics. The memory being
3701 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003702 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003703 * If @a ptr is NULL, no operation is performed.
3704 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003705 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003706 *
3707 * @return N/A
3708 */
3709extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003710
Allan Stephensc98da842016-11-11 15:45:03 -05003711/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003712 * @brief Allocate memory from heap, array style
3713 *
3714 * This routine provides traditional calloc() semantics. Memory is
3715 * allocated from the heap memory pool and zeroed.
3716 *
3717 * @param nmemb Number of elements in the requested array
3718 * @param size Size of each array element (in bytes).
3719 *
3720 * @return Address of the allocated memory if successful; otherwise NULL.
3721 */
3722extern void *k_calloc(size_t nmemb, size_t size);
3723
3724/**
Allan Stephensc98da842016-11-11 15:45:03 -05003725 * @} end defgroup heap_apis
3726 */
3727
Benjamin Walshacc68c12017-01-29 18:57:45 -05003728/* polling API - PRIVATE */
3729
Benjamin Walshb0179862017-02-02 16:39:57 -05003730#ifdef CONFIG_POLL
3731#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3732#else
3733#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3734#endif
3735
Benjamin Walshacc68c12017-01-29 18:57:45 -05003736/* private - implementation data created as needed, per-type */
3737struct _poller {
3738 struct k_thread *thread;
3739};
3740
3741/* private - types bit positions */
3742enum _poll_types_bits {
3743 /* can be used to ignore an event */
3744 _POLL_TYPE_IGNORE,
3745
3746 /* to be signaled by k_poll_signal() */
3747 _POLL_TYPE_SIGNAL,
3748
3749 /* semaphore availability */
3750 _POLL_TYPE_SEM_AVAILABLE,
3751
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003752 /* queue/fifo/lifo data availability */
3753 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003754
3755 _POLL_NUM_TYPES
3756};
3757
3758#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3759
3760/* private - states bit positions */
3761enum _poll_states_bits {
3762 /* default state when creating event */
3763 _POLL_STATE_NOT_READY,
3764
Benjamin Walshacc68c12017-01-29 18:57:45 -05003765 /* signaled by k_poll_signal() */
3766 _POLL_STATE_SIGNALED,
3767
3768 /* semaphore is available */
3769 _POLL_STATE_SEM_AVAILABLE,
3770
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003771 /* data is available to read on queue/fifo/lifo */
3772 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003773
3774 _POLL_NUM_STATES
3775};
3776
3777#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3778
3779#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003780 (32 - (0 \
3781 + 8 /* tag */ \
3782 + _POLL_NUM_TYPES \
3783 + _POLL_NUM_STATES \
3784 + 1 /* modes */ \
3785 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003786
Benjamin Walshacc68c12017-01-29 18:57:45 -05003787/* end of polling API - PRIVATE */
3788
3789
3790/**
3791 * @defgroup poll_apis Async polling APIs
3792 * @ingroup kernel_apis
3793 * @{
3794 */
3795
3796/* Public polling API */
3797
3798/* public - values for k_poll_event.type bitfield */
3799#define K_POLL_TYPE_IGNORE 0
3800#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3801#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003802#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3803#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003804
3805/* public - polling modes */
3806enum k_poll_modes {
3807 /* polling thread does not take ownership of objects when available */
3808 K_POLL_MODE_NOTIFY_ONLY = 0,
3809
3810 K_POLL_NUM_MODES
3811};
3812
3813/* public - values for k_poll_event.state bitfield */
3814#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003815#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3816#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003817#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3818#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003819
3820/* public - poll signal object */
3821struct k_poll_signal {
3822 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003823 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003824
3825 /*
3826 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3827 * user resets it to 0.
3828 */
3829 unsigned int signaled;
3830
3831 /* custom result value passed to k_poll_signal() if needed */
3832 int result;
3833};
3834
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003835#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003836 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003837 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003838 .signaled = 0, \
3839 .result = 0, \
3840 }
3841
3842struct k_poll_event {
3843 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003844 sys_dnode_t _node;
3845
3846 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003847 struct _poller *poller;
3848
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003849 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003850 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003851
Benjamin Walshacc68c12017-01-29 18:57:45 -05003852 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003853 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003854
3855 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003856 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003857
3858 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003859 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003860
3861 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003862 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003863
3864 /* per-type data */
3865 union {
3866 void *obj;
3867 struct k_poll_signal *signal;
3868 struct k_sem *sem;
3869 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003870 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003871 };
3872};
3873
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003874#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003875 { \
3876 .poller = NULL, \
3877 .type = event_type, \
3878 .state = K_POLL_STATE_NOT_READY, \
3879 .mode = event_mode, \
3880 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003881 { .obj = event_obj }, \
3882 }
3883
3884#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3885 event_tag) \
3886 { \
3887 .type = event_type, \
3888 .tag = event_tag, \
3889 .state = K_POLL_STATE_NOT_READY, \
3890 .mode = event_mode, \
3891 .unused = 0, \
3892 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003893 }
3894
3895/**
3896 * @brief Initialize one struct k_poll_event instance
3897 *
3898 * After this routine is called on a poll event, the event it ready to be
3899 * placed in an event array to be passed to k_poll().
3900 *
3901 * @param event The event to initialize.
3902 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3903 * values. Only values that apply to the same object being polled
3904 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3905 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003906 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003907 * @param obj Kernel object or poll signal.
3908 *
3909 * @return N/A
3910 */
3911
Kumar Galacc334c72017-04-21 10:55:34 -05003912extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003913 int mode, void *obj);
3914
3915/**
3916 * @brief Wait for one or many of multiple poll events to occur
3917 *
3918 * This routine allows a thread to wait concurrently for one or many of
3919 * multiple poll events to have occurred. Such events can be a kernel object
3920 * being available, like a semaphore, or a poll signal event.
3921 *
3922 * When an event notifies that a kernel object is available, the kernel object
3923 * is not "given" to the thread calling k_poll(): it merely signals the fact
3924 * that the object was available when the k_poll() call was in effect. Also,
3925 * all threads trying to acquire an object the regular way, i.e. by pending on
3926 * the object, have precedence over the thread polling on the object. This
3927 * means that the polling thread will never get the poll event on an object
3928 * until the object becomes available and its pend queue is empty. For this
3929 * reason, the k_poll() call is more effective when the objects being polled
3930 * only have one thread, the polling thread, trying to acquire them.
3931 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003932 * When k_poll() returns 0, the caller should loop on all the events that were
3933 * passed to k_poll() and check the state field for the values that were
3934 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003935 *
3936 * Before being reused for another call to k_poll(), the user has to reset the
3937 * state field to K_POLL_STATE_NOT_READY.
3938 *
3939 * @param events An array of pointers to events to be polled for.
3940 * @param num_events The number of events in the array.
3941 * @param timeout Waiting period for an event to be ready (in milliseconds),
3942 * or one of the special values K_NO_WAIT and K_FOREVER.
3943 *
3944 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003945 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02003946 * @retval -EINTR Poller thread has been interrupted.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003947 */
3948
3949extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003950 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003951
3952/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003953 * @brief Initialize a poll signal object.
3954 *
3955 * Ready a poll signal object to be signaled via k_poll_signal().
3956 *
3957 * @param signal A poll signal.
3958 *
3959 * @return N/A
3960 */
3961
3962extern void k_poll_signal_init(struct k_poll_signal *signal);
3963
3964/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003965 * @brief Signal a poll signal object.
3966 *
3967 * This routine makes ready a poll signal, which is basically a poll event of
3968 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3969 * made ready to run. A @a result value can be specified.
3970 *
3971 * The poll signal contains a 'signaled' field that, when set by
3972 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3973 * be reset by the user before being passed again to k_poll() or k_poll() will
3974 * consider it being signaled, and will return immediately.
3975 *
3976 * @param signal A poll signal.
3977 * @param result The value to store in the result field of the signal.
3978 *
3979 * @retval 0 The signal was delivered successfully.
3980 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3981 */
3982
3983extern int k_poll_signal(struct k_poll_signal *signal, int result);
3984
3985/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003986extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003987
3988/**
3989 * @} end defgroup poll_apis
3990 */
3991
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003992/**
3993 * @brief Make the CPU idle.
3994 *
3995 * This function makes the CPU idle until an event wakes it up.
3996 *
3997 * In a regular system, the idle thread should be the only thread responsible
3998 * for making the CPU idle and triggering any type of power management.
3999 * However, in some more constrained systems, such as a single-threaded system,
4000 * the only thread would be responsible for this if needed.
4001 *
4002 * @return N/A
4003 */
4004extern void k_cpu_idle(void);
4005
4006/**
4007 * @brief Make the CPU idle in an atomic fashion.
4008 *
4009 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4010 * must be done atomically before making the CPU idle.
4011 *
4012 * @param key Interrupt locking key obtained from irq_lock().
4013 *
4014 * @return N/A
4015 */
4016extern void k_cpu_atomic_idle(unsigned int key);
4017
Kumar Galacc334c72017-04-21 10:55:34 -05004018extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004019
Andrew Boiecdb94d62017-04-18 15:22:05 -07004020#ifdef _ARCH_EXCEPT
4021/* This archtecture has direct support for triggering a CPU exception */
4022#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4023#else
4024
Andrew Boiecdb94d62017-04-18 15:22:05 -07004025/* NOTE: This is the implementation for arches that do not implement
4026 * _ARCH_EXCEPT() to generate a real CPU exception.
4027 *
4028 * We won't have a real exception frame to determine the PC value when
4029 * the oops occurred, so print file and line number before we jump into
4030 * the fatal error handler.
4031 */
4032#define _k_except_reason(reason) do { \
4033 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4034 _NanoFatalErrorHandler(reason, &_default_esf); \
4035 CODE_UNREACHABLE; \
4036 } while (0)
4037
4038#endif /* _ARCH__EXCEPT */
4039
4040/**
4041 * @brief Fatally terminate a thread
4042 *
4043 * This should be called when a thread has encountered an unrecoverable
4044 * runtime condition and needs to terminate. What this ultimately
4045 * means is determined by the _fatal_error_handler() implementation, which
4046 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4047 *
4048 * If this is called from ISR context, the default system fatal error handler
4049 * will treat it as an unrecoverable system error, just like k_panic().
4050 */
4051#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4052
4053/**
4054 * @brief Fatally terminate the system
4055 *
4056 * This should be called when the Zephyr kernel has encountered an
4057 * unrecoverable runtime condition and needs to terminate. What this ultimately
4058 * means is determined by the _fatal_error_handler() implementation, which
4059 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4060 */
4061#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4062
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004063/*
4064 * private APIs that are utilized by one or more public APIs
4065 */
4066
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004067#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004068extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004069#else
4070#define _init_static_threads() do { } while ((0))
4071#endif
4072
4073extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05004074extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004075
Andrew Boiedc5d9352017-06-02 12:56:47 -07004076/* arch/cpu.h may declare an architecture or platform-specific macro
4077 * for properly declaring stacks, compatible with MMU/MPU constraints if
4078 * enabled
4079 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004080
4081/**
4082 * @brief Obtain an extern reference to a stack
4083 *
4084 * This macro properly brings the symbol of a thread stack declared
4085 * elsewhere into scope.
4086 *
4087 * @param sym Thread stack symbol name
4088 */
4089#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4090
Andrew Boiedc5d9352017-06-02 12:56:47 -07004091#ifdef _ARCH_THREAD_STACK_DEFINE
4092#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4093#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4094 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4095#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4096#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004097static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004098{
4099 return _ARCH_THREAD_STACK_BUFFER(sym);
4100}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004101#else
4102/**
4103 * @brief Declare a toplevel thread stack memory region
4104 *
4105 * This declares a region of memory suitable for use as a thread's stack.
4106 *
4107 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4108 * 'noinit' section so that it isn't zeroed at boot
4109 *
Andrew Boie507852a2017-07-25 18:47:07 -07004110 * The declared symbol will always be a k_thread_stack_t which can be passed to
4111 * k_thread_create, but should otherwise not be manipulated. If the buffer
4112 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004113 *
4114 * It is legal to precede this definition with the 'static' keyword.
4115 *
4116 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4117 * parameter of k_thread_create(), it may not be the same as the
4118 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4119 *
4120 * @param sym Thread stack symbol name
4121 * @param size Size of the stack memory region
4122 */
4123#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004124 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004125
4126/**
4127 * @brief Declare a toplevel array of thread stack memory regions
4128 *
4129 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4130 * definition for additional details and constraints.
4131 *
4132 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4133 * 'noinit' section so that it isn't zeroed at boot
4134 *
4135 * @param sym Thread stack symbol name
4136 * @param nmemb Number of stacks to declare
4137 * @param size Size of the stack memory region
4138 */
4139
4140#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004141 struct _k_thread_stack_element __noinit \
4142 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004143
4144/**
4145 * @brief Declare an embedded stack memory region
4146 *
4147 * Used for stacks embedded within other data structures. Use is highly
4148 * discouraged but in some cases necessary. For memory protection scenarios,
4149 * it is very important that any RAM preceding this member not be writable
4150 * by threads else a stack overflow will lead to silent corruption. In other
4151 * words, the containing data structure should live in RAM owned by the kernel.
4152 *
4153 * @param sym Thread stack symbol name
4154 * @param size Size of the stack memory region
4155 */
4156#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004157 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004158
4159/**
4160 * @brief Return the size in bytes of a stack memory region
4161 *
4162 * Convenience macro for passing the desired stack size to k_thread_create()
4163 * since the underlying implementation may actually create something larger
4164 * (for instance a guard area).
4165 *
4166 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004167 * passed to K_THREAD_STACK_DEFINE.
4168 *
4169 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4170 * it is not guaranteed to return the original value since each array
4171 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004172 *
4173 * @param sym Stack memory symbol
4174 * @return Size of the stack
4175 */
4176#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4177
4178/**
4179 * @brief Get a pointer to the physical stack buffer
4180 *
4181 * Convenience macro to get at the real underlying stack buffer used by
4182 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4183 * This is really only intended for diagnostic tools which want to examine
4184 * stack memory contents.
4185 *
4186 * @param sym Declared stack symbol name
4187 * @return The buffer itself, a char *
4188 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004189static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004190{
4191 return (char *)sym;
4192}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004193
4194#endif /* _ARCH_DECLARE_STACK */
4195
Chunlin Hane9c97022017-07-07 20:29:30 +08004196/**
4197 * @defgroup mem_domain_apis Memory domain APIs
4198 * @ingroup kernel_apis
4199 * @{
4200 */
4201
4202/** @def MEM_PARTITION_ENTRY
4203 * @brief Used to declare a memory partition entry
4204 */
4205#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4206 {\
4207 .start = _start, \
4208 .size = _size, \
4209 .attr = _attr, \
4210 }
4211
4212/** @def K_MEM_PARTITION_DEFINE
4213 * @brief Used to declare a memory partition
4214 */
4215#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4216#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4217 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
4218 struct k_mem_partition name = \
4219 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4220#else
4221#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4222 struct k_mem_partition name = \
4223 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4224#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4225
Chunlin Hane9c97022017-07-07 20:29:30 +08004226/* memory partition */
4227struct k_mem_partition {
4228 /* start address of memory partition */
4229 u32_t start;
4230 /* size of memory partition */
4231 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304232#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004233 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304234 k_mem_partition_attr_t attr;
4235#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004236};
4237
Chunlin Hane9c97022017-07-07 20:29:30 +08004238/* memory domian */
4239struct k_mem_domain {
4240 /* number of partitions in the domain */
4241 u32_t num_partitions;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304242#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004243 /* partitions in the domain */
4244 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304245#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004246 /* domain q */
4247 sys_dlist_t mem_domain_q;
4248};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304249
Chunlin Hane9c97022017-07-07 20:29:30 +08004250
4251/**
4252 * @brief Initialize a memory domain.
4253 *
4254 * Initialize a memory domain with given name and memory partitions.
4255 *
4256 * @param domain The memory domain to be initialized.
4257 * @param num_parts The number of array items of "parts" parameter.
4258 * @param parts An array of pointers to the memory partitions. Can be NULL
4259 * if num_parts is zero.
4260 */
4261
4262extern void k_mem_domain_init(struct k_mem_domain *domain, u32_t num_parts,
4263 struct k_mem_partition *parts[]);
4264/**
4265 * @brief Destroy a memory domain.
4266 *
4267 * Destroy a memory domain.
4268 *
4269 * @param domain The memory domain to be destroyed.
4270 */
4271
4272extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4273
4274/**
4275 * @brief Add a memory partition into a memory domain.
4276 *
4277 * Add a memory partition into a memory domain.
4278 *
4279 * @param domain The memory domain to be added a memory partition.
4280 * @param part The memory partition to be added
4281 */
4282
4283extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4284 struct k_mem_partition *part);
4285
4286/**
4287 * @brief Remove a memory partition from a memory domain.
4288 *
4289 * Remove a memory partition from a memory domain.
4290 *
4291 * @param domain The memory domain to be removed a memory partition.
4292 * @param part The memory partition to be removed
4293 */
4294
4295extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4296 struct k_mem_partition *part);
4297
4298/**
4299 * @brief Add a thread into a memory domain.
4300 *
4301 * Add a thread into a memory domain.
4302 *
4303 * @param domain The memory domain that the thread is going to be added into.
4304 * @param thread ID of thread going to be added into the memory domain.
4305 */
4306
4307extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4308 k_tid_t thread);
4309
4310/**
4311 * @brief Remove a thread from its memory domain.
4312 *
4313 * Remove a thread from its memory domain.
4314 *
4315 * @param thread ID of thread going to be removed from its memory domain.
4316 */
4317
4318extern void k_mem_domain_remove_thread(k_tid_t thread);
4319
4320/**
4321 * @} end defgroup mem_domain_apis
4322 */
4323
Andrew Boie756f9072017-10-10 16:01:49 -07004324/**
4325 * @brief Emit a character buffer to the console device
4326 *
4327 * @param c String of characters to print
4328 * @param n The length of the string
4329 */
4330__syscall void k_str_out(char *c, size_t n);
4331
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004332#ifdef __cplusplus
4333}
4334#endif
4335
Andrew Boiee004dec2016-11-07 09:01:19 -08004336#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4337/*
4338 * Define new and delete operators.
4339 * At this moment, the operators do nothing since objects are supposed
4340 * to be statically allocated.
4341 */
4342inline void operator delete(void *ptr)
4343{
4344 (void)ptr;
4345}
4346
4347inline void operator delete[](void *ptr)
4348{
4349 (void)ptr;
4350}
4351
4352inline void *operator new(size_t size)
4353{
4354 (void)size;
4355 return NULL;
4356}
4357
4358inline void *operator new[](size_t size)
4359{
4360 (void)size;
4361 return NULL;
4362}
4363
4364/* Placement versions of operator new and delete */
4365inline void operator delete(void *ptr1, void *ptr2)
4366{
4367 (void)ptr1;
4368 (void)ptr2;
4369}
4370
4371inline void operator delete[](void *ptr1, void *ptr2)
4372{
4373 (void)ptr1;
4374 (void)ptr2;
4375}
4376
4377inline void *operator new(size_t size, void *ptr)
4378{
4379 (void)size;
4380 return ptr;
4381}
4382
4383inline void *operator new[](size_t size, void *ptr)
4384{
4385 (void)size;
4386 return ptr;
4387}
4388
4389#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4390
Andrew Boiefa94ee72017-09-28 16:54:35 -07004391#include <syscalls/kernel.h>
4392
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004393#endif /* !_ASMLANGUAGE */
4394
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004395#endif /* _kernel__h_ */