blob: 1ec102afc4e91e90a9b030f2a98a114e13a286da [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);
Kumar Galacc334c72017-04-21 10:55:34 -0500741 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400742};
743
Andrew Boied26cf2d2017-03-30 13:07:02 -0700744#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400745 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500746 prio, options, delay, abort, groups) \
747 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700748 .init_thread = (thread), \
749 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500750 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700751 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400752 .init_p1 = (void *)p1, \
753 .init_p2 = (void *)p2, \
754 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500755 .init_prio = (prio), \
756 .init_options = (options), \
757 .init_delay = (delay), \
758 .init_abort = (abort), \
759 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400760 }
761
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400762/**
Allan Stephensc98da842016-11-11 15:45:03 -0500763 * INTERNAL_HIDDEN @endcond
764 */
765
766/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500767 * @brief Statically define and initialize a thread.
768 *
769 * The thread may be scheduled for immediate execution or a delayed start.
770 *
771 * Thread options are architecture-specific, and can include K_ESSENTIAL,
772 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
773 * them using "|" (the logical OR operator).
774 *
775 * The ID of the thread can be accessed using:
776 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500777 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500778 *
779 * @param name Name of the thread.
780 * @param stack_size Stack size in bytes.
781 * @param entry Thread entry function.
782 * @param p1 1st entry point parameter.
783 * @param p2 2nd entry point parameter.
784 * @param p3 3rd entry point parameter.
785 * @param prio Thread priority.
786 * @param options Thread options.
787 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400788 *
789 * @internal It has been observed that the x86 compiler by default aligns
790 * these _static_thread_data structures to 32-byte boundaries, thereby
791 * wasting space. To work around this, force a 4-byte alignment.
792 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500793#define K_THREAD_DEFINE(name, stack_size, \
794 entry, p1, p2, p3, \
795 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700796 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700797 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500798 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500799 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700800 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
801 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500802 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700803 NULL, 0); \
804 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400805
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400806/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500807 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400808 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500809 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400810 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500811 * @param thread ID of thread whose priority is needed.
812 *
813 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400814 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700815__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400816
817/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500818 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400819 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500820 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400821 *
822 * Rescheduling can occur immediately depending on the priority @a thread is
823 * set to:
824 *
825 * - If its priority is raised above the priority of the caller of this
826 * function, and the caller is preemptible, @a thread will be scheduled in.
827 *
828 * - If the caller operates on itself, it lowers its priority below that of
829 * other threads in the system, and the caller is preemptible, the thread of
830 * highest priority will be scheduled in.
831 *
832 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
833 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
834 * highest priority.
835 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500836 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400837 * @param prio New priority.
838 *
839 * @warning Changing the priority of a thread currently involved in mutex
840 * priority inheritance may result in undefined behavior.
841 *
842 * @return N/A
843 */
Andrew Boie468190a2017-09-29 14:00:48 -0700844__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400845
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400846/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500847 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500849 * This routine prevents the kernel scheduler from making @a thread the
850 * current thread. All other internal operations on @a thread are still
851 * performed; for example, any timeout it is waiting on keeps ticking,
852 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400853 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500854 * If @a thread is already suspended, the routine has no effect.
855 *
856 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400857 *
858 * @return N/A
859 */
Andrew Boie468190a2017-09-29 14:00:48 -0700860__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400861
862/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500863 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400864 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500865 * This routine allows the kernel scheduler to make @a thread the current
866 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400867 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500868 * If @a thread is not currently suspended, the routine has no effect.
869 *
870 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400871 *
872 * @return N/A
873 */
Andrew Boie468190a2017-09-29 14:00:48 -0700874__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400875
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400876/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500877 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400878 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500879 * This routine specifies how the scheduler will perform time slicing of
880 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500882 * To enable time slicing, @a slice must be non-zero. The scheduler
883 * ensures that no thread runs for more than the specified time limit
884 * before other threads of that priority are given a chance to execute.
885 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700886 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400887 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500888 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400889 * execute. Once the scheduler selects a thread for execution, there is no
890 * minimum guaranteed time the thread will execute before threads of greater or
891 * equal priority are scheduled.
892 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500893 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400894 * for execution, this routine has no effect; the thread is immediately
895 * rescheduled after the slice period expires.
896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500897 * To disable timeslicing, set both @a slice and @a prio to zero.
898 *
899 * @param slice Maximum time slice length (in milliseconds).
900 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400901 *
902 * @return N/A
903 */
Kumar Galacc334c72017-04-21 10:55:34 -0500904extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400905
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400906/**
Allan Stephensc98da842016-11-11 15:45:03 -0500907 * @} end defgroup thread_apis
908 */
909
910/**
911 * @addtogroup isr_apis
912 * @{
913 */
914
915/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500916 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400917 *
Allan Stephensc98da842016-11-11 15:45:03 -0500918 * This routine allows the caller to customize its actions, depending on
919 * whether it is a thread or an ISR.
920 *
921 * @note Can be called by ISRs.
922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500923 * @return 0 if invoked by a thread.
924 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400925 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500926extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400927
Benjamin Walsh445830d2016-11-10 15:54:27 -0500928/**
929 * @brief Determine if code is running in a preemptible thread.
930 *
Allan Stephensc98da842016-11-11 15:45:03 -0500931 * This routine allows the caller to customize its actions, depending on
932 * whether it can be preempted by another thread. The routine returns a 'true'
933 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500934 *
Allan Stephensc98da842016-11-11 15:45:03 -0500935 * - The code is running in a thread, not at ISR.
936 * - The thread's priority is in the preemptible range.
937 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500938 *
Allan Stephensc98da842016-11-11 15:45:03 -0500939 * @note Can be called by ISRs.
940 *
941 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500942 * @return Non-zero if invoked by a preemptible thread.
943 */
Andrew Boie468190a2017-09-29 14:00:48 -0700944__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -0500945
Allan Stephensc98da842016-11-11 15:45:03 -0500946/**
947 * @} end addtogroup isr_apis
948 */
949
950/**
951 * @addtogroup thread_apis
952 * @{
953 */
954
955/**
956 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500957 *
Allan Stephensc98da842016-11-11 15:45:03 -0500958 * This routine prevents the current thread from being preempted by another
959 * thread by instructing the scheduler to treat it as a cooperative thread.
960 * If the thread subsequently performs an operation that makes it unready,
961 * it will be context switched out in the normal manner. When the thread
962 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500963 *
Allan Stephensc98da842016-11-11 15:45:03 -0500964 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500965 *
Allan Stephensc98da842016-11-11 15:45:03 -0500966 * @note k_sched_lock() and k_sched_unlock() should normally be used
967 * when the operation being performed can be safely interrupted by ISRs.
968 * However, if the amount of processing involved is very small, better
969 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500970 *
971 * @return N/A
972 */
973extern void k_sched_lock(void);
974
Allan Stephensc98da842016-11-11 15:45:03 -0500975/**
976 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500977 *
Allan Stephensc98da842016-11-11 15:45:03 -0500978 * This routine reverses the effect of a previous call to k_sched_lock().
979 * A thread must call the routine once for each time it called k_sched_lock()
980 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500981 *
982 * @return N/A
983 */
984extern void k_sched_unlock(void);
985
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400986/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500987 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400988 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500989 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400990 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500991 * Custom data is not used by the kernel itself, and is freely available
992 * for a thread to use as it sees fit. It can be used as a framework
993 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400994 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500995 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400996 *
997 * @return N/A
998 */
Andrew Boie468190a2017-09-29 14:00:48 -0700999__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001000
1001/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001002 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001003 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001004 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001005 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001006 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001007 */
Andrew Boie468190a2017-09-29 14:00:48 -07001008__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001009
1010/**
Allan Stephensc98da842016-11-11 15:45:03 -05001011 * @} end addtogroup thread_apis
1012 */
1013
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001014#include <sys_clock.h>
1015
Allan Stephensc2f15a42016-11-17 12:24:22 -05001016/**
1017 * @addtogroup clock_apis
1018 * @{
1019 */
1020
1021/**
1022 * @brief Generate null timeout delay.
1023 *
1024 * This macro generates a timeout delay that that instructs a kernel API
1025 * not to wait if the requested operation cannot be performed immediately.
1026 *
1027 * @return Timeout delay value.
1028 */
1029#define K_NO_WAIT 0
1030
1031/**
1032 * @brief Generate timeout delay from milliseconds.
1033 *
1034 * This macro generates a timeout delay that that instructs a kernel API
1035 * to wait up to @a ms milliseconds to perform the requested operation.
1036 *
1037 * @param ms Duration in milliseconds.
1038 *
1039 * @return Timeout delay value.
1040 */
Johan Hedberg14471692016-11-13 10:52:15 +02001041#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001042
1043/**
1044 * @brief Generate timeout delay from seconds.
1045 *
1046 * This macro generates a timeout delay that that instructs a kernel API
1047 * to wait up to @a s seconds to perform the requested operation.
1048 *
1049 * @param s Duration in seconds.
1050 *
1051 * @return Timeout delay value.
1052 */
Johan Hedberg14471692016-11-13 10:52:15 +02001053#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001054
1055/**
1056 * @brief Generate timeout delay from minutes.
1057 *
1058 * This macro generates a timeout delay that that instructs a kernel API
1059 * to wait up to @a m minutes to perform the requested operation.
1060 *
1061 * @param m Duration in minutes.
1062 *
1063 * @return Timeout delay value.
1064 */
Johan Hedberg14471692016-11-13 10:52:15 +02001065#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001066
1067/**
1068 * @brief Generate timeout delay from hours.
1069 *
1070 * This macro generates a timeout delay that that instructs a kernel API
1071 * to wait up to @a h hours to perform the requested operation.
1072 *
1073 * @param h Duration in hours.
1074 *
1075 * @return Timeout delay value.
1076 */
Johan Hedberg14471692016-11-13 10:52:15 +02001077#define K_HOURS(h) K_MINUTES((h) * 60)
1078
Allan Stephensc98da842016-11-11 15:45:03 -05001079/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001080 * @brief Generate infinite timeout delay.
1081 *
1082 * This macro generates a timeout delay that that instructs a kernel API
1083 * to wait as long as necessary to perform the requested operation.
1084 *
1085 * @return Timeout delay value.
1086 */
1087#define K_FOREVER (-1)
1088
1089/**
1090 * @} end addtogroup clock_apis
1091 */
1092
1093/**
Allan Stephensc98da842016-11-11 15:45:03 -05001094 * @cond INTERNAL_HIDDEN
1095 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001096
Benjamin Walsh62092182016-12-20 14:39:08 -05001097/* kernel clocks */
1098
1099#if (sys_clock_ticks_per_sec == 1000) || \
1100 (sys_clock_ticks_per_sec == 500) || \
1101 (sys_clock_ticks_per_sec == 250) || \
1102 (sys_clock_ticks_per_sec == 125) || \
1103 (sys_clock_ticks_per_sec == 100) || \
1104 (sys_clock_ticks_per_sec == 50) || \
1105 (sys_clock_ticks_per_sec == 25) || \
1106 (sys_clock_ticks_per_sec == 20) || \
1107 (sys_clock_ticks_per_sec == 10) || \
1108 (sys_clock_ticks_per_sec == 1)
1109
1110 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1111#else
1112 /* yields horrible 64-bit math on many architectures: try to avoid */
1113 #define _NON_OPTIMIZED_TICKS_PER_SEC
1114#endif
1115
1116#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001117extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001118#else
Kumar Galacc334c72017-04-21 10:55:34 -05001119static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001120{
Kumar Galacc334c72017-04-21 10:55:34 -05001121 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001122}
1123#endif
1124
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001125/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001126#ifdef CONFIG_TICKLESS_KERNEL
1127#define _TICK_ALIGN 0
1128#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001129#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001130#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001131
Kumar Galacc334c72017-04-21 10:55:34 -05001132static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001133{
Benjamin Walsh62092182016-12-20 14:39:08 -05001134#ifdef CONFIG_SYS_CLOCK_EXISTS
1135
1136#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001137 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001138#else
Kumar Galacc334c72017-04-21 10:55:34 -05001139 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001140#endif
1141
1142#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001143 __ASSERT(ticks == 0, "");
1144 return 0;
1145#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001146}
1147
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001148struct k_timer {
1149 /*
1150 * _timeout structure must be first here if we want to use
1151 * dynamic timer allocation. timeout.node is used in the double-linked
1152 * list of free timers
1153 */
1154 struct _timeout timeout;
1155
Allan Stephens45bfa372016-10-12 12:39:42 -05001156 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001157 _wait_q_t wait_q;
1158
1159 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001160 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001161
1162 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001163 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001164
1165 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001166 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001167
Allan Stephens45bfa372016-10-12 12:39:42 -05001168 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001169 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001170
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001171 /* user-specific data, also used to support legacy features */
1172 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001173
Anas Nashif2f203c22016-12-18 06:57:45 -05001174 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001175};
1176
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001177#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001178 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001179 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001180 .timeout.wait_q = NULL, \
1181 .timeout.thread = NULL, \
1182 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001183 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001184 .expiry_fn = expiry, \
1185 .stop_fn = stop, \
1186 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001187 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001188 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001189 }
1190
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001191#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1192
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001193/**
Allan Stephensc98da842016-11-11 15:45:03 -05001194 * INTERNAL_HIDDEN @endcond
1195 */
1196
1197/**
1198 * @defgroup timer_apis Timer APIs
1199 * @ingroup kernel_apis
1200 * @{
1201 */
1202
1203/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001204 * @typedef k_timer_expiry_t
1205 * @brief Timer expiry function type.
1206 *
1207 * A timer's expiry function is executed by the system clock interrupt handler
1208 * each time the timer expires. The expiry function is optional, and is only
1209 * invoked if the timer has been initialized with one.
1210 *
1211 * @param timer Address of timer.
1212 *
1213 * @return N/A
1214 */
1215typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1216
1217/**
1218 * @typedef k_timer_stop_t
1219 * @brief Timer stop function type.
1220 *
1221 * A timer's stop function is executed if the timer is stopped prematurely.
1222 * The function runs in the context of the thread that stops the timer.
1223 * The stop function is optional, and is only invoked if the timer has been
1224 * initialized with one.
1225 *
1226 * @param timer Address of timer.
1227 *
1228 * @return N/A
1229 */
1230typedef void (*k_timer_stop_t)(struct k_timer *timer);
1231
1232/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001233 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001234 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001235 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001236 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001237 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001238 *
1239 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001240 * @param expiry_fn Function to invoke each time the timer expires.
1241 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001242 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001243#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001244 struct k_timer name \
1245 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001246 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001247
Allan Stephens45bfa372016-10-12 12:39:42 -05001248/**
1249 * @brief Initialize a timer.
1250 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001251 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001252 *
1253 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001254 * @param expiry_fn Function to invoke each time the timer expires.
1255 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001256 *
1257 * @return N/A
1258 */
1259extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001260 k_timer_expiry_t expiry_fn,
1261 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001262
Allan Stephens45bfa372016-10-12 12:39:42 -05001263/**
1264 * @brief Start a timer.
1265 *
1266 * This routine starts a timer, and resets its status to zero. The timer
1267 * begins counting down using the specified duration and period values.
1268 *
1269 * Attempting to start a timer that is already running is permitted.
1270 * The timer's status is reset to zero and the timer begins counting down
1271 * using the new duration and period values.
1272 *
1273 * @param timer Address of timer.
1274 * @param duration Initial timer duration (in milliseconds).
1275 * @param period Timer period (in milliseconds).
1276 *
1277 * @return N/A
1278 */
Andrew Boiea354d492017-09-29 16:22:28 -07001279__syscall void k_timer_start(struct k_timer *timer,
1280 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001281
1282/**
1283 * @brief Stop a timer.
1284 *
1285 * This routine stops a running timer prematurely. The timer's stop function,
1286 * if one exists, is invoked by the caller.
1287 *
1288 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001289 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001290 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001291 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1292 * if @a k_timer_stop is to be called from ISRs.
1293 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001294 * @param timer Address of timer.
1295 *
1296 * @return N/A
1297 */
Andrew Boiea354d492017-09-29 16:22:28 -07001298__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001299
1300/**
1301 * @brief Read timer status.
1302 *
1303 * This routine reads the timer's status, which indicates the number of times
1304 * it has expired since its status was last read.
1305 *
1306 * Calling this routine resets the timer's status to zero.
1307 *
1308 * @param timer Address of timer.
1309 *
1310 * @return Timer status.
1311 */
Andrew Boiea354d492017-09-29 16:22:28 -07001312__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001313
1314/**
1315 * @brief Synchronize thread to timer expiration.
1316 *
1317 * This routine blocks the calling thread until the timer's status is non-zero
1318 * (indicating that it has expired at least once since it was last examined)
1319 * or the timer is stopped. If the timer status is already non-zero,
1320 * or the timer is already stopped, the caller continues without waiting.
1321 *
1322 * Calling this routine resets the timer's status to zero.
1323 *
1324 * This routine must not be used by interrupt handlers, since they are not
1325 * allowed to block.
1326 *
1327 * @param timer Address of timer.
1328 *
1329 * @return Timer status.
1330 */
Andrew Boiea354d492017-09-29 16:22:28 -07001331__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001332
1333/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001334 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001335 *
1336 * This routine computes the (approximate) time remaining before a running
1337 * timer next expires. If the timer is not running, it returns zero.
1338 *
1339 * @param timer Address of timer.
1340 *
1341 * @return Remaining time (in milliseconds).
1342 */
Andrew Boiea354d492017-09-29 16:22:28 -07001343__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1344
1345static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001346{
1347 return _timeout_remaining_get(&timer->timeout);
1348}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001349
Allan Stephensc98da842016-11-11 15:45:03 -05001350/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001351 * @brief Associate user-specific data with a timer.
1352 *
1353 * This routine records the @a user_data with the @a timer, to be retrieved
1354 * later.
1355 *
1356 * It can be used e.g. in a timer handler shared across multiple subsystems to
1357 * retrieve data specific to the subsystem this timer is associated with.
1358 *
1359 * @param timer Address of timer.
1360 * @param user_data User data to associate with the timer.
1361 *
1362 * @return N/A
1363 */
Andrew Boiea354d492017-09-29 16:22:28 -07001364__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1365
1366static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1367 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001368{
1369 timer->user_data = user_data;
1370}
1371
1372/**
1373 * @brief Retrieve the user-specific data from a timer.
1374 *
1375 * @param timer Address of timer.
1376 *
1377 * @return The user data.
1378 */
Andrew Boiea354d492017-09-29 16:22:28 -07001379__syscall void *k_timer_user_data_get(struct k_timer *timer);
1380
1381static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001382{
1383 return timer->user_data;
1384}
1385
1386/**
Allan Stephensc98da842016-11-11 15:45:03 -05001387 * @} end defgroup timer_apis
1388 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001389
Allan Stephensc98da842016-11-11 15:45:03 -05001390/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001391 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001392 * @{
1393 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001394
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001395/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001396 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001397 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001398 * This routine returns the elapsed time since the system booted,
1399 * in milliseconds.
1400 *
1401 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001402 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001403__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001404
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001405#ifdef CONFIG_TICKLESS_KERNEL
1406/**
1407 * @brief Enable clock always on in tickless kernel
1408 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001409 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001410 * there are no timer events programmed in tickless kernel
1411 * scheduling. This is necessary if the clock is used to track
1412 * passage of time.
1413 *
1414 * @retval prev_status Previous status of always on flag
1415 */
1416static inline int k_enable_sys_clock_always_on(void)
1417{
1418 int prev_status = _sys_clock_always_on;
1419
1420 _sys_clock_always_on = 1;
1421 _enable_sys_clock();
1422
1423 return prev_status;
1424}
1425
1426/**
1427 * @brief Disable clock always on in tickless kernel
1428 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001429 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001430 * there are no timer events programmed in tickless kernel
1431 * scheduling. To save power, this routine should be called
1432 * immediately when clock is not used to track time.
1433 */
1434static inline void k_disable_sys_clock_always_on(void)
1435{
1436 _sys_clock_always_on = 0;
1437}
1438#else
1439#define k_enable_sys_clock_always_on() do { } while ((0))
1440#define k_disable_sys_clock_always_on() do { } while ((0))
1441#endif
1442
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001443/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001444 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001446 * This routine returns the lower 32-bits of the elapsed time since the system
1447 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001448 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001449 * This routine can be more efficient than k_uptime_get(), as it reduces the
1450 * need for interrupt locking and 64-bit math. However, the 32-bit result
1451 * cannot hold a system uptime time larger than approximately 50 days, so the
1452 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001453 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001454 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001455 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001456__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001457
1458/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001459 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001460 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001461 * This routine computes the elapsed time between the current system uptime
1462 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001463 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001464 * @param reftime Pointer to a reference time, which is updated to the current
1465 * uptime upon return.
1466 *
1467 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001468 */
Kumar Galacc334c72017-04-21 10:55:34 -05001469extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001470
1471/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001472 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001474 * This routine computes the elapsed time between the current system uptime
1475 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001476 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001477 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1478 * need for interrupt locking and 64-bit math. However, the 32-bit result
1479 * cannot hold an elapsed time larger than approximately 50 days, so the
1480 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001482 * @param reftime Pointer to a reference time, which is updated to the current
1483 * uptime upon return.
1484 *
1485 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001486 */
Kumar Galacc334c72017-04-21 10:55:34 -05001487extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001488
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001489/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001490 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001492 * This routine returns the current time, as measured by the system's hardware
1493 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001495 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001496 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001497#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001498
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001499/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001500 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001501 */
1502
Allan Stephensc98da842016-11-11 15:45:03 -05001503/**
1504 * @cond INTERNAL_HIDDEN
1505 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001506
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001507struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001508 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001509 union {
1510 _wait_q_t wait_q;
1511
1512 _POLL_EVENT;
1513 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001514
1515 _OBJECT_TRACING_NEXT_PTR(k_queue);
1516};
1517
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001518#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001519 { \
1520 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1521 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001522 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001523 _OBJECT_TRACING_INIT \
1524 }
1525
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001526#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1527
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001528/**
1529 * INTERNAL_HIDDEN @endcond
1530 */
1531
1532/**
1533 * @defgroup queue_apis Queue APIs
1534 * @ingroup kernel_apis
1535 * @{
1536 */
1537
1538/**
1539 * @brief Initialize a queue.
1540 *
1541 * This routine initializes a queue object, prior to its first use.
1542 *
1543 * @param queue Address of the queue.
1544 *
1545 * @return N/A
1546 */
1547extern void k_queue_init(struct k_queue *queue);
1548
1549/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001550 * @brief Cancel waiting on a queue.
1551 *
1552 * This routine causes first thread pending on @a queue, if any, to
1553 * return from k_queue_get() call with NULL value (as if timeout expired).
1554 *
1555 * @note Can be called by ISRs.
1556 *
1557 * @param queue Address of the queue.
1558 *
1559 * @return N/A
1560 */
1561extern void k_queue_cancel_wait(struct k_queue *queue);
1562
1563/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001564 * @brief Append an element to the end of a queue.
1565 *
1566 * This routine appends a data item to @a queue. A queue data item must be
1567 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1568 * reserved for the kernel's use.
1569 *
1570 * @note Can be called by ISRs.
1571 *
1572 * @param queue Address of the queue.
1573 * @param data Address of the data item.
1574 *
1575 * @return N/A
1576 */
1577extern void k_queue_append(struct k_queue *queue, void *data);
1578
1579/**
1580 * @brief Prepend an element to a queue.
1581 *
1582 * This routine prepends a data item to @a queue. A queue data item must be
1583 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1584 * reserved for the kernel's use.
1585 *
1586 * @note Can be called by ISRs.
1587 *
1588 * @param queue Address of the queue.
1589 * @param data Address of the data item.
1590 *
1591 * @return N/A
1592 */
1593extern void k_queue_prepend(struct k_queue *queue, void *data);
1594
1595/**
1596 * @brief Inserts an element to a queue.
1597 *
1598 * This routine inserts a data item to @a queue after previous item. A queue
1599 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1600 * item are reserved for the kernel's use.
1601 *
1602 * @note Can be called by ISRs.
1603 *
1604 * @param queue Address of the queue.
1605 * @param prev Address of the previous data item.
1606 * @param data Address of the data item.
1607 *
1608 * @return N/A
1609 */
1610extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1611
1612/**
1613 * @brief Atomically append a list of elements to a queue.
1614 *
1615 * This routine adds a list of data items to @a queue in one operation.
1616 * The data items must be in a singly-linked list, with the first 32 bits
1617 * in each data item pointing to the next data item; the list must be
1618 * NULL-terminated.
1619 *
1620 * @note Can be called by ISRs.
1621 *
1622 * @param queue Address of the queue.
1623 * @param head Pointer to first node in singly-linked list.
1624 * @param tail Pointer to last node in singly-linked list.
1625 *
1626 * @return N/A
1627 */
1628extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1629
1630/**
1631 * @brief Atomically add a list of elements to a queue.
1632 *
1633 * This routine adds a list of data items to @a queue in one operation.
1634 * The data items must be in a singly-linked list implemented using a
1635 * sys_slist_t object. Upon completion, the original list is empty.
1636 *
1637 * @note Can be called by ISRs.
1638 *
1639 * @param queue Address of the queue.
1640 * @param list Pointer to sys_slist_t object.
1641 *
1642 * @return N/A
1643 */
1644extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1645
1646/**
1647 * @brief Get an element from a queue.
1648 *
1649 * This routine removes first data item from @a queue. The first 32 bits of the
1650 * data item are reserved for the kernel's use.
1651 *
1652 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1653 *
1654 * @param queue Address of the queue.
1655 * @param timeout Waiting period to obtain a data item (in milliseconds),
1656 * or one of the special values K_NO_WAIT and K_FOREVER.
1657 *
1658 * @return Address of the data item if successful; NULL if returned
1659 * without waiting, or waiting period timed out.
1660 */
Kumar Galacc334c72017-04-21 10:55:34 -05001661extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001662
1663/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001664 * @brief Remove an element from a queue.
1665 *
1666 * This routine removes data item from @a queue. The first 32 bits of the
1667 * data item are reserved for the kernel's use. Removing elements from k_queue
1668 * rely on sys_slist_find_and_remove which is not a constant time operation.
1669 *
1670 * @note Can be called by ISRs
1671 *
1672 * @param queue Address of the queue.
1673 * @param data Address of the data item.
1674 *
1675 * @return true if data item was removed
1676 */
1677static inline bool k_queue_remove(struct k_queue *queue, void *data)
1678{
1679 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1680}
1681
1682/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001683 * @brief Query a queue to see if it has data available.
1684 *
1685 * Note that the data might be already gone by the time this function returns
1686 * if other threads are also trying to read from the queue.
1687 *
1688 * @note Can be called by ISRs.
1689 *
1690 * @param queue Address of the queue.
1691 *
1692 * @return Non-zero if the queue is empty.
1693 * @return 0 if data is available.
1694 */
1695static inline int k_queue_is_empty(struct k_queue *queue)
1696{
1697 return (int)sys_slist_is_empty(&queue->data_q);
1698}
1699
1700/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001701 * @brief Peek element at the head of queue.
1702 *
1703 * Return element from the head of queue without removing it.
1704 *
1705 * @param queue Address of the queue.
1706 *
1707 * @return Head element, or NULL if queue is empty.
1708 */
1709static inline void *k_queue_peek_head(struct k_queue *queue)
1710{
1711 return sys_slist_peek_head(&queue->data_q);
1712}
1713
1714/**
1715 * @brief Peek element at the tail of queue.
1716 *
1717 * Return element from the tail of queue without removing it.
1718 *
1719 * @param queue Address of the queue.
1720 *
1721 * @return Tail element, or NULL if queue is empty.
1722 */
1723static inline void *k_queue_peek_tail(struct k_queue *queue)
1724{
1725 return sys_slist_peek_tail(&queue->data_q);
1726}
1727
1728/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001729 * @brief Statically define and initialize a queue.
1730 *
1731 * The queue can be accessed outside the module where it is defined using:
1732 *
1733 * @code extern struct k_queue <name>; @endcode
1734 *
1735 * @param name Name of the queue.
1736 */
1737#define K_QUEUE_DEFINE(name) \
1738 struct k_queue name \
1739 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001740 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001741
1742/**
1743 * @} end defgroup queue_apis
1744 */
1745
1746/**
1747 * @cond INTERNAL_HIDDEN
1748 */
1749
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001750struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001751 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001752};
1753
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001754#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001755 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001756 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001757 }
1758
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001759#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1760
Allan Stephensc98da842016-11-11 15:45:03 -05001761/**
1762 * INTERNAL_HIDDEN @endcond
1763 */
1764
1765/**
1766 * @defgroup fifo_apis Fifo APIs
1767 * @ingroup kernel_apis
1768 * @{
1769 */
1770
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001771/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001772 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001773 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001774 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001775 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001776 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001777 *
1778 * @return N/A
1779 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001780#define k_fifo_init(fifo) \
1781 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001782
1783/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001784 * @brief Cancel waiting on a fifo.
1785 *
1786 * This routine causes first thread pending on @a fifo, if any, to
1787 * return from k_fifo_get() call with NULL value (as if timeout
1788 * expired).
1789 *
1790 * @note Can be called by ISRs.
1791 *
1792 * @param fifo Address of the fifo.
1793 *
1794 * @return N/A
1795 */
1796#define k_fifo_cancel_wait(fifo) \
1797 k_queue_cancel_wait((struct k_queue *) fifo)
1798
1799/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001800 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001801 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001802 * This routine adds a data item to @a fifo. A fifo data item must be
1803 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1804 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001805 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001806 * @note Can be called by ISRs.
1807 *
1808 * @param fifo Address of the fifo.
1809 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001810 *
1811 * @return N/A
1812 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001813#define k_fifo_put(fifo, data) \
1814 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001815
1816/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001817 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001819 * This routine adds a list of data items to @a fifo in one operation.
1820 * The data items must be in a singly-linked list, with the first 32 bits
1821 * each data item pointing to the next data item; the list must be
1822 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001823 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001824 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001825 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001826 * @param fifo Address of the fifo.
1827 * @param head Pointer to first node in singly-linked list.
1828 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001829 *
1830 * @return N/A
1831 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001832#define k_fifo_put_list(fifo, head, tail) \
1833 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001834
1835/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001836 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001837 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001838 * This routine adds a list of data items to @a fifo in one operation.
1839 * The data items must be in a singly-linked list implemented using a
1840 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001841 * and must be re-initialized via sys_slist_init().
1842 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001843 * @note Can be called by ISRs.
1844 *
1845 * @param fifo Address of the fifo.
1846 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001847 *
1848 * @return N/A
1849 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001850#define k_fifo_put_slist(fifo, list) \
1851 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001852
1853/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001854 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001856 * This routine removes a data item from @a fifo in a "first in, first out"
1857 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001858 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001859 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1860 *
1861 * @param fifo Address of the fifo.
1862 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001863 * or one of the special values K_NO_WAIT and K_FOREVER.
1864 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001865 * @return Address of the data item if successful; NULL if returned
1866 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001867 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001868#define k_fifo_get(fifo, timeout) \
1869 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001870
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001871/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001872 * @brief Query a fifo to see if it has data available.
1873 *
1874 * Note that the data might be already gone by the time this function returns
1875 * if other threads is also trying to read from the fifo.
1876 *
1877 * @note Can be called by ISRs.
1878 *
1879 * @param fifo Address of the fifo.
1880 *
1881 * @return Non-zero if the fifo is empty.
1882 * @return 0 if data is available.
1883 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001884#define k_fifo_is_empty(fifo) \
1885 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001886
1887/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001888 * @brief Peek element at the head of fifo.
1889 *
1890 * Return element from the head of fifo without removing it. A usecase
1891 * for this is if elements of the fifo are themselves containers. Then
1892 * on each iteration of processing, a head container will be peeked,
1893 * and some data processed out of it, and only if the container is empty,
1894 * it will be completely remove from the fifo.
1895 *
1896 * @param fifo Address of the fifo.
1897 *
1898 * @return Head element, or NULL if the fifo is empty.
1899 */
1900#define k_fifo_peek_head(fifo) \
1901 k_queue_peek_head((struct k_queue *) fifo)
1902
1903/**
1904 * @brief Peek element at the tail of fifo.
1905 *
1906 * Return element from the tail of fifo (without removing it). A usecase
1907 * for this is if elements of the fifo are themselves containers. Then
1908 * it may be useful to add more data to the last container in fifo.
1909 *
1910 * @param fifo Address of the fifo.
1911 *
1912 * @return Tail element, or NULL if fifo is empty.
1913 */
1914#define k_fifo_peek_tail(fifo) \
1915 k_queue_peek_tail((struct k_queue *) fifo)
1916
1917/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001918 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001920 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001921 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001922 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001923 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001924 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001925 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001926#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001927 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001928 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001929 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001930
Allan Stephensc98da842016-11-11 15:45:03 -05001931/**
1932 * @} end defgroup fifo_apis
1933 */
1934
1935/**
1936 * @cond INTERNAL_HIDDEN
1937 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001938
1939struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001940 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001941};
1942
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001943#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001944 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001945 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001946 }
1947
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001948#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1949
Allan Stephensc98da842016-11-11 15:45:03 -05001950/**
1951 * INTERNAL_HIDDEN @endcond
1952 */
1953
1954/**
1955 * @defgroup lifo_apis Lifo APIs
1956 * @ingroup kernel_apis
1957 * @{
1958 */
1959
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001960/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001961 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001962 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001963 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001964 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001965 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001966 *
1967 * @return N/A
1968 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001969#define k_lifo_init(lifo) \
1970 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001971
1972/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001973 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001974 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001975 * This routine adds a data item to @a lifo. A lifo data item must be
1976 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1977 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001978 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001979 * @note Can be called by ISRs.
1980 *
1981 * @param lifo Address of the lifo.
1982 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001983 *
1984 * @return N/A
1985 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001986#define k_lifo_put(lifo, data) \
1987 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001988
1989/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001990 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001991 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001992 * This routine removes a data item from @a lifo in a "last in, first out"
1993 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001994 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001995 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1996 *
1997 * @param lifo Address of the lifo.
1998 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001999 * or one of the special values K_NO_WAIT and K_FOREVER.
2000 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002001 * @return Address of the data item if successful; NULL if returned
2002 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002003 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002004#define k_lifo_get(lifo, timeout) \
2005 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002006
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002007/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002008 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002009 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002010 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002011 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002012 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002013 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002014 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002015 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002016#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002017 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002018 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002019 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002020
Allan Stephensc98da842016-11-11 15:45:03 -05002021/**
2022 * @} end defgroup lifo_apis
2023 */
2024
2025/**
2026 * @cond INTERNAL_HIDDEN
2027 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002028
2029struct k_stack {
2030 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002031 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002032
Anas Nashif2f203c22016-12-18 06:57:45 -05002033 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002034};
2035
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002036#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002037 { \
2038 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2039 .base = stack_buffer, \
2040 .next = stack_buffer, \
2041 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002042 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002043 }
2044
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002045#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2046
Allan Stephensc98da842016-11-11 15:45:03 -05002047/**
2048 * INTERNAL_HIDDEN @endcond
2049 */
2050
2051/**
2052 * @defgroup stack_apis Stack APIs
2053 * @ingroup kernel_apis
2054 * @{
2055 */
2056
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002057/**
2058 * @brief Initialize a stack.
2059 *
2060 * This routine initializes a stack object, prior to its first use.
2061 *
2062 * @param stack Address of the stack.
2063 * @param buffer Address of array used to hold stacked values.
2064 * @param num_entries Maximum number of values that can be stacked.
2065 *
2066 * @return N/A
2067 */
Andrew Boiee8734462017-09-29 16:42:07 -07002068__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002069 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002070
2071/**
2072 * @brief Push an element onto a stack.
2073 *
2074 * This routine adds a 32-bit value @a data to @a stack.
2075 *
2076 * @note Can be called by ISRs.
2077 *
2078 * @param stack Address of the stack.
2079 * @param data Value to push onto the stack.
2080 *
2081 * @return N/A
2082 */
Andrew Boiee8734462017-09-29 16:42:07 -07002083__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002084
2085/**
2086 * @brief Pop an element from a stack.
2087 *
2088 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2089 * manner and stores the value in @a data.
2090 *
2091 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2092 *
2093 * @param stack Address of the stack.
2094 * @param data Address of area to hold the value popped from the stack.
2095 * @param timeout Waiting period to obtain a value (in milliseconds),
2096 * or one of the special values K_NO_WAIT and K_FOREVER.
2097 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002098 * @retval 0 Element popped from stack.
2099 * @retval -EBUSY Returned without waiting.
2100 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002101 */
Andrew Boiee8734462017-09-29 16:42:07 -07002102__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002103
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002104/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002105 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002106 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002107 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002108 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002109 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002110 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002111 * @param name Name of the stack.
2112 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002113 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002114#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002115 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002116 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002117 struct k_stack name \
2118 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002119 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002120 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002121
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002122/**
Allan Stephensc98da842016-11-11 15:45:03 -05002123 * @} end defgroup stack_apis
2124 */
2125
Allan Stephens6bba9b02016-11-16 14:56:54 -05002126struct k_work;
2127
Allan Stephensc98da842016-11-11 15:45:03 -05002128/**
2129 * @defgroup workqueue_apis Workqueue Thread APIs
2130 * @ingroup kernel_apis
2131 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002132 */
2133
Allan Stephens6bba9b02016-11-16 14:56:54 -05002134/**
2135 * @typedef k_work_handler_t
2136 * @brief Work item handler function type.
2137 *
2138 * A work item's handler function is executed by a workqueue's thread
2139 * when the work item is processed by the workqueue.
2140 *
2141 * @param work Address of the work item.
2142 *
2143 * @return N/A
2144 */
2145typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002146
2147/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002148 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002149 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002150
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002151struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002152 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002153 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002154};
2155
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002156enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002157 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002158};
2159
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002160struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002161 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002162 k_work_handler_t handler;
2163 atomic_t flags[1];
2164};
2165
Allan Stephens6bba9b02016-11-16 14:56:54 -05002166struct k_delayed_work {
2167 struct k_work work;
2168 struct _timeout timeout;
2169 struct k_work_q *work_q;
2170};
2171
2172extern struct k_work_q k_sys_work_q;
2173
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002174/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002175 * INTERNAL_HIDDEN @endcond
2176 */
2177
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002178#define _K_WORK_INITIALIZER(work_handler) \
2179 { \
2180 ._reserved = NULL, \
2181 .handler = work_handler, \
2182 .flags = { 0 } \
2183 }
2184
2185#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2186
Allan Stephens6bba9b02016-11-16 14:56:54 -05002187/**
2188 * @brief Initialize a statically-defined work item.
2189 *
2190 * This macro can be used to initialize a statically-defined workqueue work
2191 * item, prior to its first use. For example,
2192 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002193 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002194 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002195 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002196 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002197 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002198#define K_WORK_DEFINE(work, work_handler) \
2199 struct k_work work \
2200 __in_section(_k_work, static, work) = \
2201 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002202
2203/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002204 * @brief Initialize a work item.
2205 *
2206 * This routine initializes a workqueue work item, prior to its first use.
2207 *
2208 * @param work Address of work item.
2209 * @param handler Function to invoke each time work item is processed.
2210 *
2211 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002212 */
2213static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2214{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002215 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002216 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002217 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002218}
2219
2220/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002221 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002222 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002223 * This routine submits work item @a work to be processed by workqueue
2224 * @a work_q. If the work item is already pending in the workqueue's queue
2225 * as a result of an earlier submission, this routine has no effect on the
2226 * work item. If the work item has already been processed, or is currently
2227 * being processed, its work is considered complete and the work item can be
2228 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002229 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002230 * @warning
2231 * A submitted work item must not be modified until it has been processed
2232 * by the workqueue.
2233 *
2234 * @note Can be called by ISRs.
2235 *
2236 * @param work_q Address of workqueue.
2237 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002238 *
2239 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002240 */
2241static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2242 struct k_work *work)
2243{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002244 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002245 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002246 }
2247}
2248
2249/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002250 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002251 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002252 * This routine indicates if work item @a work is pending in a workqueue's
2253 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002254 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002255 * @note Can be called by ISRs.
2256 *
2257 * @param work Address of work item.
2258 *
2259 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002260 */
2261static inline int k_work_pending(struct k_work *work)
2262{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002263 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002264}
2265
2266/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002267 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002268 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002269 * This routine starts workqueue @a work_q. The workqueue spawns its work
2270 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002271 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002272 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002273 * @param stack Pointer to work queue thread's stack space, as defined by
2274 * K_THREAD_STACK_DEFINE()
2275 * @param stack_size Size of the work queue thread's stack (in bytes), which
2276 * should either be the same constant passed to
2277 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002278 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002279 *
2280 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002281 */
Andrew Boie507852a2017-07-25 18:47:07 -07002282extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002283 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002284 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002285
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002286/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002287 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002288 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002289 * This routine initializes a workqueue delayed work item, prior to
2290 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002291 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002292 * @param work Address of delayed work item.
2293 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002294 *
2295 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002296 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002297extern void k_delayed_work_init(struct k_delayed_work *work,
2298 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002299
2300/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002301 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002302 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002303 * This routine schedules work item @a work to be processed by workqueue
2304 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002305 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002306 * Only when the countdown completes is the work item actually submitted to
2307 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002308 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002309 * Submitting a previously submitted delayed work item that is still
2310 * counting down cancels the existing submission and restarts the countdown
2311 * using the new delay. If the work item is currently pending on the
2312 * workqueue's queue because the countdown has completed it is too late to
2313 * resubmit the item, and resubmission fails without impacting the work item.
2314 * If the work item has already been processed, or is currently being processed,
2315 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002316 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002317 * @warning
2318 * A delayed work item must not be modified until it has been processed
2319 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002320 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002321 * @note Can be called by ISRs.
2322 *
2323 * @param work_q Address of workqueue.
2324 * @param work Address of delayed work item.
2325 * @param delay Delay before submitting the work item (in milliseconds).
2326 *
2327 * @retval 0 Work item countdown started.
2328 * @retval -EINPROGRESS Work item is already pending.
2329 * @retval -EINVAL Work item is being processed or has completed its work.
2330 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002331 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002332extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2333 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002334 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002335
2336/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002337 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002338 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002339 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002340 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002341 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002342 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002343 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002344 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002345 * @param work Address of delayed work item.
2346 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002347 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002348 * @retval -EINPROGRESS Work item is already pending.
2349 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002350 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002351extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002352
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002353/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002354 * @brief Submit a work item to the system workqueue.
2355 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002356 * This routine submits work item @a work to be processed by the system
2357 * workqueue. If the work item is already pending in the workqueue's queue
2358 * as a result of an earlier submission, this routine has no effect on the
2359 * work item. If the work item has already been processed, or is currently
2360 * being processed, its work is considered complete and the work item can be
2361 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002362 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002363 * @warning
2364 * Work items submitted to the system workqueue should avoid using handlers
2365 * that block or yield since this may prevent the system workqueue from
2366 * processing other work items in a timely manner.
2367 *
2368 * @note Can be called by ISRs.
2369 *
2370 * @param work Address of work item.
2371 *
2372 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002373 */
2374static inline void k_work_submit(struct k_work *work)
2375{
2376 k_work_submit_to_queue(&k_sys_work_q, work);
2377}
2378
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002379/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002380 * @brief Submit a delayed work item to the system workqueue.
2381 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002382 * This routine schedules work item @a work to be processed by the system
2383 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002384 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002385 * Only when the countdown completes is the work item actually submitted to
2386 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002387 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002388 * Submitting a previously submitted delayed work item that is still
2389 * counting down cancels the existing submission and restarts the countdown
2390 * using the new delay. If the work item is currently pending on the
2391 * workqueue's queue because the countdown has completed it is too late to
2392 * resubmit the item, and resubmission fails without impacting the work item.
2393 * If the work item has already been processed, or is currently being processed,
2394 * its work is considered complete and the work item can be resubmitted.
2395 *
2396 * @warning
2397 * Work items submitted to the system workqueue should avoid using handlers
2398 * that block or yield since this may prevent the system workqueue from
2399 * processing other work items in a timely manner.
2400 *
2401 * @note Can be called by ISRs.
2402 *
2403 * @param work Address of delayed work item.
2404 * @param delay Delay before submitting the work item (in milliseconds).
2405 *
2406 * @retval 0 Work item countdown started.
2407 * @retval -EINPROGRESS Work item is already pending.
2408 * @retval -EINVAL Work item is being processed or has completed its work.
2409 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002410 */
2411static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002412 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002413{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002414 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002415}
2416
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002417/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002418 * @brief Get time remaining before a delayed work gets scheduled.
2419 *
2420 * This routine computes the (approximate) time remaining before a
2421 * delayed work gets executed. If the delayed work is not waiting to be
2422 * schedules, it returns zero.
2423 *
2424 * @param work Delayed work item.
2425 *
2426 * @return Remaining time (in milliseconds).
2427 */
Kumar Galacc334c72017-04-21 10:55:34 -05002428static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002429{
2430 return _timeout_remaining_get(&work->timeout);
2431}
2432
2433/**
Allan Stephensc98da842016-11-11 15:45:03 -05002434 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002435 */
2436
Allan Stephensc98da842016-11-11 15:45:03 -05002437/**
2438 * @cond INTERNAL_HIDDEN
2439 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002440
2441struct k_mutex {
2442 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002443 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002444 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002445 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002446
Anas Nashif2f203c22016-12-18 06:57:45 -05002447 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002448};
2449
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002450#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002451 { \
2452 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2453 .owner = NULL, \
2454 .lock_count = 0, \
2455 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002456 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002457 }
2458
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002459#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2460
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002461/**
Allan Stephensc98da842016-11-11 15:45:03 -05002462 * INTERNAL_HIDDEN @endcond
2463 */
2464
2465/**
2466 * @defgroup mutex_apis Mutex APIs
2467 * @ingroup kernel_apis
2468 * @{
2469 */
2470
2471/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002472 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002474 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002475 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002476 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002478 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002479 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002480#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002481 struct k_mutex name \
2482 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002483 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002484
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002485/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002486 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002487 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002488 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002489 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002490 * Upon completion, the mutex is available and does not have an owner.
2491 *
2492 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002493 *
2494 * @return N/A
2495 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002496__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002497
2498/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002499 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002500 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002501 * This routine locks @a mutex. If the mutex is locked by another thread,
2502 * the calling thread waits until the mutex becomes available or until
2503 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002504 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002505 * A thread is permitted to lock a mutex it has already locked. The operation
2506 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002507 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002508 * @param mutex Address of the mutex.
2509 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002510 * or one of the special values K_NO_WAIT and K_FOREVER.
2511 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002512 * @retval 0 Mutex locked.
2513 * @retval -EBUSY Returned without waiting.
2514 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002515 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002516__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002517
2518/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002519 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002521 * This routine unlocks @a mutex. The mutex must already be locked by the
2522 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002523 *
2524 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002525 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002526 * thread.
2527 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002528 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002529 *
2530 * @return N/A
2531 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002532__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002533
Allan Stephensc98da842016-11-11 15:45:03 -05002534/**
2535 * @} end defgroup mutex_apis
2536 */
2537
2538/**
2539 * @cond INTERNAL_HIDDEN
2540 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002541
2542struct k_sem {
2543 _wait_q_t wait_q;
2544 unsigned int count;
2545 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002546 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002547
Anas Nashif2f203c22016-12-18 06:57:45 -05002548 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002549};
2550
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002551#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002552 { \
2553 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2554 .count = initial_count, \
2555 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002556 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002557 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002558 }
2559
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002560#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2561
Allan Stephensc98da842016-11-11 15:45:03 -05002562/**
2563 * INTERNAL_HIDDEN @endcond
2564 */
2565
2566/**
2567 * @defgroup semaphore_apis Semaphore APIs
2568 * @ingroup kernel_apis
2569 * @{
2570 */
2571
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002572/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002573 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002574 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002575 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002576 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002577 * @param sem Address of the semaphore.
2578 * @param initial_count Initial semaphore count.
2579 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002580 *
2581 * @return N/A
2582 */
Andrew Boie99280232017-09-29 14:17:47 -07002583__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2584 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002585
2586/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002587 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002588 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002589 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002590 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002591 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2592 *
2593 * @param sem Address of the semaphore.
2594 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002595 * or one of the special values K_NO_WAIT and K_FOREVER.
2596 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002597 * @note When porting code from the nanokernel legacy API to the new API, be
2598 * careful with the return value of this function. The return value is the
2599 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2600 * non-zero means failure, while the nano_sem_take family returns 1 for success
2601 * and 0 for failure.
2602 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002603 * @retval 0 Semaphore taken.
2604 * @retval -EBUSY Returned without waiting.
2605 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002606 */
Andrew Boie99280232017-09-29 14:17:47 -07002607__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002608
2609/**
2610 * @brief Give a semaphore.
2611 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002612 * This routine gives @a sem, unless the semaphore is already at its maximum
2613 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002614 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002615 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002616 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002617 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002618 *
2619 * @return N/A
2620 */
Andrew Boie99280232017-09-29 14:17:47 -07002621__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002622
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002623/**
2624 * @brief Reset a semaphore's count to zero.
2625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002626 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002627 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002628 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002629 *
2630 * @return N/A
2631 */
Andrew Boie990bf162017-10-03 12:36:49 -07002632__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002633
2634static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002635{
2636 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002637}
2638
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002639/**
2640 * @brief Get a semaphore's count.
2641 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002642 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002644 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002645 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002646 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002647 */
Andrew Boie990bf162017-10-03 12:36:49 -07002648__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002649
2650static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002651{
2652 return sem->count;
2653}
2654
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002655/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002656 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002657 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002658 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002659 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002660 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002661 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002662 * @param name Name of the semaphore.
2663 * @param initial_count Initial semaphore count.
2664 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002665 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002666#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002667 struct k_sem name \
2668 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002669 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002670
Allan Stephensc98da842016-11-11 15:45:03 -05002671/**
2672 * @} end defgroup semaphore_apis
2673 */
2674
2675/**
2676 * @defgroup alert_apis Alert APIs
2677 * @ingroup kernel_apis
2678 * @{
2679 */
2680
Allan Stephens5eceb852016-11-16 10:16:30 -05002681/**
2682 * @typedef k_alert_handler_t
2683 * @brief Alert handler function type.
2684 *
2685 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002686 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002687 * and is only invoked if the alert has been initialized with one.
2688 *
2689 * @param alert Address of the alert.
2690 *
2691 * @return 0 if alert has been consumed; non-zero if alert should pend.
2692 */
2693typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002694
2695/**
2696 * @} end defgroup alert_apis
2697 */
2698
2699/**
2700 * @cond INTERNAL_HIDDEN
2701 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002702
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002703#define K_ALERT_DEFAULT NULL
2704#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002705
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002706struct k_alert {
2707 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002708 atomic_t send_count;
2709 struct k_work work_item;
2710 struct k_sem sem;
2711
Anas Nashif2f203c22016-12-18 06:57:45 -05002712 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002713};
2714
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002715extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002716
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002717#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002718 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002719 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002720 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002721 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2722 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002723 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002724 }
2725
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002726#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2727
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002728/**
Allan Stephensc98da842016-11-11 15:45:03 -05002729 * INTERNAL_HIDDEN @endcond
2730 */
2731
2732/**
2733 * @addtogroup alert_apis
2734 * @{
2735 */
2736
2737/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002738 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002739 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002740 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002741 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002742 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002743 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002744 * @param name Name of the alert.
2745 * @param alert_handler Action to take when alert is sent. Specify either
2746 * the address of a function to be invoked by the system workqueue
2747 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2748 * K_ALERT_DEFAULT (which causes the alert to pend).
2749 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002750 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002751#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002752 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002753 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002754 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002755 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002756
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002757/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002758 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002759 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002760 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002761 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002762 * @param alert Address of the alert.
2763 * @param handler Action to take when alert is sent. Specify either the address
2764 * of a function to be invoked by the system workqueue thread,
2765 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2766 * K_ALERT_DEFAULT (which causes the alert to pend).
2767 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002768 *
2769 * @return N/A
2770 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002771extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2772 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002773
2774/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002775 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002776 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002777 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002778 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002779 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2780 *
2781 * @param alert Address of the alert.
2782 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002783 * or one of the special values K_NO_WAIT and K_FOREVER.
2784 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002785 * @retval 0 Alert received.
2786 * @retval -EBUSY Returned without waiting.
2787 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002788 */
Andrew Boie310e9872017-09-29 04:41:15 -07002789__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002790
2791/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002792 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002793 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002794 * This routine signals @a alert. The action specified for @a alert will
2795 * be taken, which may trigger the execution of an alert handler function
2796 * and/or cause the alert to pend (assuming the alert has not reached its
2797 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002799 * @note Can be called by ISRs.
2800 *
2801 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002802 *
2803 * @return N/A
2804 */
Andrew Boie310e9872017-09-29 04:41:15 -07002805__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002806
2807/**
Allan Stephensc98da842016-11-11 15:45:03 -05002808 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002809 */
2810
Allan Stephensc98da842016-11-11 15:45:03 -05002811/**
2812 * @cond INTERNAL_HIDDEN
2813 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002814
2815struct k_msgq {
2816 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002817 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002818 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002819 char *buffer_start;
2820 char *buffer_end;
2821 char *read_ptr;
2822 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002823 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002824
Anas Nashif2f203c22016-12-18 06:57:45 -05002825 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002826};
2827
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002828#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002829 { \
2830 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002831 .max_msgs = q_max_msgs, \
2832 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002833 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002834 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002835 .read_ptr = q_buffer, \
2836 .write_ptr = q_buffer, \
2837 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002838 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002839 }
2840
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002841#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2842
Peter Mitsis1da807e2016-10-06 11:36:59 -04002843/**
Allan Stephensc98da842016-11-11 15:45:03 -05002844 * INTERNAL_HIDDEN @endcond
2845 */
2846
2847/**
2848 * @defgroup msgq_apis Message Queue APIs
2849 * @ingroup kernel_apis
2850 * @{
2851 */
2852
2853/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002854 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002855 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002856 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2857 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002858 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2859 * message is similarly aligned to this boundary, @a q_msg_size must also be
2860 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002861 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002862 * The message queue can be accessed outside the module where it is defined
2863 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002864 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002865 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002867 * @param q_name Name of the message queue.
2868 * @param q_msg_size Message size (in bytes).
2869 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002870 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002871 */
2872#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2873 static char __noinit __aligned(q_align) \
2874 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002875 struct k_msgq q_name \
2876 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002877 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002878 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002879
Peter Mitsisd7a37502016-10-13 11:37:40 -04002880/**
2881 * @brief Initialize a message queue.
2882 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002883 * This routine initializes a message queue object, prior to its first use.
2884 *
Allan Stephensda827222016-11-09 14:23:58 -06002885 * The message queue's ring buffer must contain space for @a max_msgs messages,
2886 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2887 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2888 * that each message is similarly aligned to this boundary, @a q_msg_size
2889 * must also be a multiple of N.
2890 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002891 * @param q Address of the message queue.
2892 * @param buffer Pointer to ring buffer that holds queued messages.
2893 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002894 * @param max_msgs Maximum number of messages that can be queued.
2895 *
2896 * @return N/A
2897 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002898__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2899 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002900
2901/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002902 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002903 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002904 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002905 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002906 * @note Can be called by ISRs.
2907 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002908 * @param q Address of the message queue.
2909 * @param data Pointer to the message.
2910 * @param timeout Waiting period to add the message (in milliseconds),
2911 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002912 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002913 * @retval 0 Message sent.
2914 * @retval -ENOMSG Returned without waiting or queue purged.
2915 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002916 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002917__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002918
2919/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002920 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002921 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002922 * This routine receives a message from message queue @a q in a "first in,
2923 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002924 *
Allan Stephensc98da842016-11-11 15:45:03 -05002925 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002926 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002927 * @param q Address of the message queue.
2928 * @param data Address of area to hold the received message.
2929 * @param timeout Waiting period to receive the message (in milliseconds),
2930 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002931 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002932 * @retval 0 Message received.
2933 * @retval -ENOMSG Returned without waiting.
2934 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002935 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002936__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002937
2938/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002939 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002940 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002941 * This routine discards all unreceived messages in a message queue's ring
2942 * buffer. Any threads that are blocked waiting to send a message to the
2943 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002944 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002945 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002946 *
2947 * @return N/A
2948 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002949__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002950
Peter Mitsis67be2492016-10-07 11:44:34 -04002951/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002952 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002953 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002954 * This routine returns the number of unused entries in a message queue's
2955 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002956 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002957 * @param q Address of the message queue.
2958 *
2959 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002960 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002961__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
2962
2963static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002964{
2965 return q->max_msgs - q->used_msgs;
2966}
2967
Peter Mitsisd7a37502016-10-13 11:37:40 -04002968/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002969 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002970 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002971 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002972 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002973 * @param q Address of the message queue.
2974 *
2975 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002976 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002977__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
2978
2979static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002980{
2981 return q->used_msgs;
2982}
2983
Allan Stephensc98da842016-11-11 15:45:03 -05002984/**
2985 * @} end defgroup msgq_apis
2986 */
2987
2988/**
2989 * @defgroup mem_pool_apis Memory Pool APIs
2990 * @ingroup kernel_apis
2991 * @{
2992 */
2993
Andy Ross73cb9582017-05-09 10:42:39 -07002994/* Note on sizing: the use of a 20 bit field for block means that,
2995 * assuming a reasonable minimum block size of 16 bytes, we're limited
2996 * to 16M of memory managed by a single pool. Long term it would be
2997 * good to move to a variable bit size based on configuration.
2998 */
2999struct k_mem_block_id {
3000 u32_t pool : 8;
3001 u32_t level : 4;
3002 u32_t block : 20;
3003};
3004
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003005struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003006 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003007 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003008};
3009
Allan Stephensc98da842016-11-11 15:45:03 -05003010/**
3011 * @} end defgroup mem_pool_apis
3012 */
3013
3014/**
3015 * @defgroup mailbox_apis Mailbox APIs
3016 * @ingroup kernel_apis
3017 * @{
3018 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003019
3020struct k_mbox_msg {
3021 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003022 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003023 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003024 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003025 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003026 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003027 /** sender's message data buffer */
3028 void *tx_data;
3029 /** internal use only - needed for legacy API support */
3030 void *_rx_data;
3031 /** message data block descriptor */
3032 struct k_mem_block tx_block;
3033 /** source thread id */
3034 k_tid_t rx_source_thread;
3035 /** target thread id */
3036 k_tid_t tx_target_thread;
3037 /** internal use only - thread waiting on send (may be a dummy) */
3038 k_tid_t _syncing_thread;
3039#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3040 /** internal use only - semaphore used during asynchronous send */
3041 struct k_sem *_async_sem;
3042#endif
3043};
3044
Allan Stephensc98da842016-11-11 15:45:03 -05003045/**
3046 * @cond INTERNAL_HIDDEN
3047 */
3048
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003049struct k_mbox {
3050 _wait_q_t tx_msg_queue;
3051 _wait_q_t rx_msg_queue;
3052
Anas Nashif2f203c22016-12-18 06:57:45 -05003053 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003054};
3055
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003056#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003057 { \
3058 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3059 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003060 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003061 }
3062
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003063#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3064
Peter Mitsis12092702016-10-14 12:57:23 -04003065/**
Allan Stephensc98da842016-11-11 15:45:03 -05003066 * INTERNAL_HIDDEN @endcond
3067 */
3068
3069/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003070 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003071 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003072 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003073 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003074 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003075 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003076 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003077 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003078#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003079 struct k_mbox name \
3080 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003081 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003082
Peter Mitsis12092702016-10-14 12:57:23 -04003083/**
3084 * @brief Initialize a mailbox.
3085 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003086 * This routine initializes a mailbox object, prior to its first use.
3087 *
3088 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003089 *
3090 * @return N/A
3091 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003092extern void k_mbox_init(struct k_mbox *mbox);
3093
Peter Mitsis12092702016-10-14 12:57:23 -04003094/**
3095 * @brief Send a mailbox message in a synchronous manner.
3096 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003097 * This routine sends a message to @a mbox and waits for a receiver to both
3098 * receive and process it. The message data may be in a buffer, in a memory
3099 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003100 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003101 * @param mbox Address of the mailbox.
3102 * @param tx_msg Address of the transmit message descriptor.
3103 * @param timeout Waiting period for the message to be received (in
3104 * milliseconds), or one of the special values K_NO_WAIT
3105 * and K_FOREVER. Once the message has been received,
3106 * this routine waits as long as necessary for the message
3107 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003108 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003109 * @retval 0 Message sent.
3110 * @retval -ENOMSG Returned without waiting.
3111 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003112 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003113extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003114 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003115
Peter Mitsis12092702016-10-14 12:57:23 -04003116/**
3117 * @brief Send a mailbox message in an asynchronous manner.
3118 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003119 * This routine sends a message to @a mbox without waiting for a receiver
3120 * to process it. The message data may be in a buffer, in a memory pool block,
3121 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3122 * will be given when the message has been both received and completely
3123 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003124 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003125 * @param mbox Address of the mailbox.
3126 * @param tx_msg Address of the transmit message descriptor.
3127 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003128 *
3129 * @return N/A
3130 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003131extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003132 struct k_sem *sem);
3133
Peter Mitsis12092702016-10-14 12:57:23 -04003134/**
3135 * @brief Receive a mailbox message.
3136 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003137 * This routine receives a message from @a mbox, then optionally retrieves
3138 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003139 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003140 * @param mbox Address of the mailbox.
3141 * @param rx_msg Address of the receive message descriptor.
3142 * @param buffer Address of the buffer to receive data, or NULL to defer data
3143 * retrieval and message disposal until later.
3144 * @param timeout Waiting period for a message to be received (in
3145 * milliseconds), or one of the special values K_NO_WAIT
3146 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003147 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003148 * @retval 0 Message received.
3149 * @retval -ENOMSG Returned without waiting.
3150 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003151 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003152extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003153 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003154
3155/**
3156 * @brief Retrieve mailbox message data into a buffer.
3157 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003158 * This routine completes the processing of a received message by retrieving
3159 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003160 *
3161 * Alternatively, this routine can be used to dispose of a received message
3162 * without retrieving its data.
3163 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003164 * @param rx_msg Address of the receive message descriptor.
3165 * @param buffer Address of the buffer to receive data, or NULL to discard
3166 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003167 *
3168 * @return N/A
3169 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003170extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003171
3172/**
3173 * @brief Retrieve mailbox message data into a memory pool block.
3174 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003175 * This routine completes the processing of a received message by retrieving
3176 * its data into a memory pool block, then disposing of the message.
3177 * The memory pool block that results from successful retrieval must be
3178 * returned to the pool once the data has been processed, even in cases
3179 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003180 *
3181 * Alternatively, this routine can be used to dispose of a received message
3182 * without retrieving its data. In this case there is no need to return a
3183 * memory pool block to the pool.
3184 *
3185 * This routine allocates a new memory pool block for the data only if the
3186 * data is not already in one. If a new block cannot be allocated, the routine
3187 * returns a failure code and the received message is left unchanged. This
3188 * permits the caller to reattempt data retrieval at a later time or to dispose
3189 * of the received message without retrieving its data.
3190 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003191 * @param rx_msg Address of a receive message descriptor.
3192 * @param pool Address of memory pool, or NULL to discard data.
3193 * @param block Address of the area to hold memory pool block info.
3194 * @param timeout Waiting period to wait for a memory pool block (in
3195 * milliseconds), or one of the special values K_NO_WAIT
3196 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003197 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003198 * @retval 0 Data retrieved.
3199 * @retval -ENOMEM Returned without waiting.
3200 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003201 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003202extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003203 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003204 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003205
Allan Stephensc98da842016-11-11 15:45:03 -05003206/**
3207 * @} end defgroup mailbox_apis
3208 */
3209
3210/**
3211 * @cond INTERNAL_HIDDEN
3212 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003213
3214struct k_pipe {
3215 unsigned char *buffer; /* Pipe buffer: may be NULL */
3216 size_t size; /* Buffer size */
3217 size_t bytes_used; /* # bytes used in buffer */
3218 size_t read_index; /* Where in buffer to read from */
3219 size_t write_index; /* Where in buffer to write */
3220
3221 struct {
3222 _wait_q_t readers; /* Reader wait queue */
3223 _wait_q_t writers; /* Writer wait queue */
3224 } wait_q;
3225
Anas Nashif2f203c22016-12-18 06:57:45 -05003226 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003227};
3228
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003229#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003230 { \
3231 .buffer = pipe_buffer, \
3232 .size = pipe_buffer_size, \
3233 .bytes_used = 0, \
3234 .read_index = 0, \
3235 .write_index = 0, \
3236 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3237 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003238 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003239 }
3240
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003241#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3242
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003243/**
Allan Stephensc98da842016-11-11 15:45:03 -05003244 * INTERNAL_HIDDEN @endcond
3245 */
3246
3247/**
3248 * @defgroup pipe_apis Pipe APIs
3249 * @ingroup kernel_apis
3250 * @{
3251 */
3252
3253/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003254 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003255 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003256 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003257 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003258 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003259 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003260 * @param name Name of the pipe.
3261 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3262 * or zero if no ring buffer is used.
3263 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003264 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003265#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3266 static unsigned char __noinit __aligned(pipe_align) \
3267 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003268 struct k_pipe name \
3269 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003270 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003271
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003272/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003273 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003274 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003275 * This routine initializes a pipe object, prior to its first use.
3276 *
3277 * @param pipe Address of the pipe.
3278 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3279 * is used.
3280 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3281 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003282 *
3283 * @return N/A
3284 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003285__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3286 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003287
3288/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003289 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003290 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003291 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003292 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003293 * @param pipe Address of the pipe.
3294 * @param data Address of data to write.
3295 * @param bytes_to_write Size of data (in bytes).
3296 * @param bytes_written Address of area to hold the number of bytes written.
3297 * @param min_xfer Minimum number of bytes to write.
3298 * @param timeout Waiting period to wait for the data to be written (in
3299 * milliseconds), or one of the special values K_NO_WAIT
3300 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003301 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003302 * @retval 0 At least @a min_xfer bytes of data were written.
3303 * @retval -EIO Returned without waiting; zero data bytes were written.
3304 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003305 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003306 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003307__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3308 size_t bytes_to_write, size_t *bytes_written,
3309 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003310
3311/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003312 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003313 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003314 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003315 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003316 * @param pipe Address of the pipe.
3317 * @param data Address to place the data read from pipe.
3318 * @param bytes_to_read Maximum number of data bytes to read.
3319 * @param bytes_read Address of area to hold the number of bytes read.
3320 * @param min_xfer Minimum number of data bytes to read.
3321 * @param timeout Waiting period to wait for the data to be read (in
3322 * milliseconds), or one of the special values K_NO_WAIT
3323 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003324 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003325 * @retval 0 At least @a min_xfer bytes of data were read.
3326 * @retval -EIO Returned without waiting; zero data bytes were read.
3327 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003328 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003329 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003330__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3331 size_t bytes_to_read, size_t *bytes_read,
3332 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003333
3334/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003335 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003336 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003337 * This routine writes the data contained in a memory block to @a pipe.
3338 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003339 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003340 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003341 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003342 * @param block Memory block containing data to send
3343 * @param size Number of data bytes in memory block to send
3344 * @param sem Semaphore to signal upon completion (else NULL)
3345 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003346 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003347 */
3348extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3349 size_t size, struct k_sem *sem);
3350
3351/**
Allan Stephensc98da842016-11-11 15:45:03 -05003352 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003353 */
3354
Allan Stephensc98da842016-11-11 15:45:03 -05003355/**
3356 * @cond INTERNAL_HIDDEN
3357 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003358
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003359struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003360 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003361 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003362 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003363 char *buffer;
3364 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003365 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003366
Anas Nashif2f203c22016-12-18 06:57:45 -05003367 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003368};
3369
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003370#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003371 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003372 { \
3373 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003374 .num_blocks = slab_num_blocks, \
3375 .block_size = slab_block_size, \
3376 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003377 .free_list = NULL, \
3378 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003379 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003380 }
3381
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003382#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3383
3384
Peter Mitsis578f9112016-10-07 13:50:31 -04003385/**
Allan Stephensc98da842016-11-11 15:45:03 -05003386 * INTERNAL_HIDDEN @endcond
3387 */
3388
3389/**
3390 * @defgroup mem_slab_apis Memory Slab APIs
3391 * @ingroup kernel_apis
3392 * @{
3393 */
3394
3395/**
Allan Stephensda827222016-11-09 14:23:58 -06003396 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003397 *
Allan Stephensda827222016-11-09 14:23:58 -06003398 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003399 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003400 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3401 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003402 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003403 *
Allan Stephensda827222016-11-09 14:23:58 -06003404 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003405 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003406 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003407 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003408 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003409 * @param name Name of the memory slab.
3410 * @param slab_block_size Size of each memory block (in bytes).
3411 * @param slab_num_blocks Number memory blocks.
3412 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003413 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003414#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3415 char __noinit __aligned(slab_align) \
3416 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3417 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003418 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003419 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003420 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003421
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003422/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003423 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003424 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003425 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003426 *
Allan Stephensda827222016-11-09 14:23:58 -06003427 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3428 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3429 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3430 * To ensure that each memory block is similarly aligned to this boundary,
3431 * @a slab_block_size must also be a multiple of N.
3432 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003433 * @param slab Address of the memory slab.
3434 * @param buffer Pointer to buffer used for the memory blocks.
3435 * @param block_size Size of each memory block (in bytes).
3436 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003437 *
3438 * @return N/A
3439 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003440extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003441 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003442
3443/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003444 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003445 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003446 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003447 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003448 * @param slab Address of the memory slab.
3449 * @param mem Pointer to block address area.
3450 * @param timeout Maximum time to wait for operation to complete
3451 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3452 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003453 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003454 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003455 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003456 * @retval -ENOMEM Returned without waiting.
3457 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003458 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003459extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003460 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003461
3462/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003463 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003464 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003465 * This routine releases a previously allocated memory block back to its
3466 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003467 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003468 * @param slab Address of the memory slab.
3469 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003470 *
3471 * @return N/A
3472 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003473extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003474
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003475/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003476 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003477 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003478 * This routine gets the number of memory blocks that are currently
3479 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003480 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003481 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003483 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003484 */
Kumar Galacc334c72017-04-21 10:55:34 -05003485static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003486{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003487 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003488}
3489
Peter Mitsisc001aa82016-10-13 13:53:37 -04003490/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003491 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003492 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003493 * This routine gets the number of memory blocks that are currently
3494 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003495 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003496 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003497 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003498 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003499 */
Kumar Galacc334c72017-04-21 10:55:34 -05003500static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003501{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003502 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003503}
3504
Allan Stephensc98da842016-11-11 15:45:03 -05003505/**
3506 * @} end defgroup mem_slab_apis
3507 */
3508
3509/**
3510 * @cond INTERNAL_HIDDEN
3511 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003512
Andy Ross73cb9582017-05-09 10:42:39 -07003513struct k_mem_pool_lvl {
3514 union {
3515 u32_t *bits_p;
3516 u32_t bits;
3517 };
3518 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003519};
3520
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003521struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003522 void *buf;
3523 size_t max_sz;
3524 u16_t n_max;
3525 u8_t n_levels;
3526 u8_t max_inline_level;
3527 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003528 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003529};
3530
Andy Ross73cb9582017-05-09 10:42:39 -07003531#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003532
Andy Ross73cb9582017-05-09 10:42:39 -07003533#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3534
3535#define _MPOOL_LVLS(maxsz, minsz) \
3536 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3537 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3538 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3539 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3540 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3541 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3542 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3543 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3544 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3545 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3546 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3547 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3548 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3549 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3550 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3551 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3552
3553/* Rounds the needed bits up to integer multiples of u32_t */
3554#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3555 ((((n_max) << (2*(l))) + 31) / 32)
3556
3557/* One word gets stored free unioned with the pointer, otherwise the
3558 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003559 */
Andy Ross73cb9582017-05-09 10:42:39 -07003560#define _MPOOL_LBIT_WORDS(n_max, l) \
3561 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3562 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003563
Andy Ross73cb9582017-05-09 10:42:39 -07003564/* How many bytes for the bitfields of a single level? */
3565#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3566 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3567 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003568
Andy Ross73cb9582017-05-09 10:42:39 -07003569/* Size of the bitmap array that follows the buffer in allocated memory */
3570#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3571 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3572 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3573 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3574 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3575 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3576 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3577 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3578 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3579 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3580 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3581 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3582 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3583 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3584 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3585 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3586 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003587
3588/**
Allan Stephensc98da842016-11-11 15:45:03 -05003589 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003590 */
3591
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003592/**
Allan Stephensc98da842016-11-11 15:45:03 -05003593 * @addtogroup mem_pool_apis
3594 * @{
3595 */
3596
3597/**
3598 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003599 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003600 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3601 * long. The memory pool allows blocks to be repeatedly partitioned into
3602 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003603 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003604 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003605 * If the pool is to be accessed outside the module where it is defined, it
3606 * can be declared via
3607 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003608 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003610 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003611 * @param minsz Size of the smallest blocks in the pool (in bytes).
3612 * @param maxsz Size of the largest blocks in the pool (in bytes).
3613 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003614 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003615 */
Andy Ross73cb9582017-05-09 10:42:39 -07003616#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3617 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3618 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3619 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3620 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3621 .buf = _mpool_buf_##name, \
3622 .max_sz = maxsz, \
3623 .n_max = nmax, \
3624 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3625 .levels = _mpool_lvls_##name, \
3626 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003627
Peter Mitsis937042c2016-10-13 13:18:26 -04003628/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003629 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003630 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003631 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003632 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003633 * @param pool Address of the memory pool.
3634 * @param block Pointer to block descriptor for the allocated memory.
3635 * @param size Amount of memory to allocate (in bytes).
3636 * @param timeout Maximum time to wait for operation to complete
3637 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3638 * or K_FOREVER to wait as long as necessary.
3639 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003640 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003641 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003642 * @retval -ENOMEM Returned without waiting.
3643 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003644 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003645extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003646 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003647
3648/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003649 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003651 * This routine releases a previously allocated memory block back to its
3652 * memory pool.
3653 *
3654 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003655 *
3656 * @return N/A
3657 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003658extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003659
3660/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003661 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003662 *
Andy Ross73cb9582017-05-09 10:42:39 -07003663 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003664 *
Andy Ross73cb9582017-05-09 10:42:39 -07003665 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003666 *
3667 * @return N/A
3668 */
Andy Ross73cb9582017-05-09 10:42:39 -07003669static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003670
3671/**
Allan Stephensc98da842016-11-11 15:45:03 -05003672 * @} end addtogroup mem_pool_apis
3673 */
3674
3675/**
3676 * @defgroup heap_apis Heap Memory Pool APIs
3677 * @ingroup kernel_apis
3678 * @{
3679 */
3680
3681/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003682 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003683 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003684 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003685 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003686 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003687 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003688 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003689 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003690 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003691extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003692
3693/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003694 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003695 *
3696 * This routine provides traditional free() semantics. The memory being
3697 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003698 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003699 * If @a ptr is NULL, no operation is performed.
3700 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003701 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003702 *
3703 * @return N/A
3704 */
3705extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003706
Allan Stephensc98da842016-11-11 15:45:03 -05003707/**
3708 * @} end defgroup heap_apis
3709 */
3710
Benjamin Walshacc68c12017-01-29 18:57:45 -05003711/* polling API - PRIVATE */
3712
Benjamin Walshb0179862017-02-02 16:39:57 -05003713#ifdef CONFIG_POLL
3714#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3715#else
3716#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3717#endif
3718
Benjamin Walshacc68c12017-01-29 18:57:45 -05003719/* private - implementation data created as needed, per-type */
3720struct _poller {
3721 struct k_thread *thread;
3722};
3723
3724/* private - types bit positions */
3725enum _poll_types_bits {
3726 /* can be used to ignore an event */
3727 _POLL_TYPE_IGNORE,
3728
3729 /* to be signaled by k_poll_signal() */
3730 _POLL_TYPE_SIGNAL,
3731
3732 /* semaphore availability */
3733 _POLL_TYPE_SEM_AVAILABLE,
3734
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003735 /* queue/fifo/lifo data availability */
3736 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003737
3738 _POLL_NUM_TYPES
3739};
3740
3741#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3742
3743/* private - states bit positions */
3744enum _poll_states_bits {
3745 /* default state when creating event */
3746 _POLL_STATE_NOT_READY,
3747
Benjamin Walshacc68c12017-01-29 18:57:45 -05003748 /* signaled by k_poll_signal() */
3749 _POLL_STATE_SIGNALED,
3750
3751 /* semaphore is available */
3752 _POLL_STATE_SEM_AVAILABLE,
3753
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003754 /* data is available to read on queue/fifo/lifo */
3755 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003756
3757 _POLL_NUM_STATES
3758};
3759
3760#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3761
3762#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003763 (32 - (0 \
3764 + 8 /* tag */ \
3765 + _POLL_NUM_TYPES \
3766 + _POLL_NUM_STATES \
3767 + 1 /* modes */ \
3768 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003769
Benjamin Walshacc68c12017-01-29 18:57:45 -05003770/* end of polling API - PRIVATE */
3771
3772
3773/**
3774 * @defgroup poll_apis Async polling APIs
3775 * @ingroup kernel_apis
3776 * @{
3777 */
3778
3779/* Public polling API */
3780
3781/* public - values for k_poll_event.type bitfield */
3782#define K_POLL_TYPE_IGNORE 0
3783#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3784#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003785#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3786#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003787
3788/* public - polling modes */
3789enum k_poll_modes {
3790 /* polling thread does not take ownership of objects when available */
3791 K_POLL_MODE_NOTIFY_ONLY = 0,
3792
3793 K_POLL_NUM_MODES
3794};
3795
3796/* public - values for k_poll_event.state bitfield */
3797#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003798#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3799#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003800#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3801#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003802
3803/* public - poll signal object */
3804struct k_poll_signal {
3805 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003806 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003807
3808 /*
3809 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3810 * user resets it to 0.
3811 */
3812 unsigned int signaled;
3813
3814 /* custom result value passed to k_poll_signal() if needed */
3815 int result;
3816};
3817
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003818#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003819 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003820 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003821 .signaled = 0, \
3822 .result = 0, \
3823 }
3824
3825struct k_poll_event {
3826 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003827 sys_dnode_t _node;
3828
3829 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003830 struct _poller *poller;
3831
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003832 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003833 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003834
Benjamin Walshacc68c12017-01-29 18:57:45 -05003835 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003836 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003837
3838 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003839 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003840
3841 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003842 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003843
3844 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003845 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003846
3847 /* per-type data */
3848 union {
3849 void *obj;
3850 struct k_poll_signal *signal;
3851 struct k_sem *sem;
3852 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003853 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003854 };
3855};
3856
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003857#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003858 { \
3859 .poller = NULL, \
3860 .type = event_type, \
3861 .state = K_POLL_STATE_NOT_READY, \
3862 .mode = event_mode, \
3863 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003864 { .obj = event_obj }, \
3865 }
3866
3867#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3868 event_tag) \
3869 { \
3870 .type = event_type, \
3871 .tag = event_tag, \
3872 .state = K_POLL_STATE_NOT_READY, \
3873 .mode = event_mode, \
3874 .unused = 0, \
3875 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003876 }
3877
3878/**
3879 * @brief Initialize one struct k_poll_event instance
3880 *
3881 * After this routine is called on a poll event, the event it ready to be
3882 * placed in an event array to be passed to k_poll().
3883 *
3884 * @param event The event to initialize.
3885 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3886 * values. Only values that apply to the same object being polled
3887 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3888 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003889 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003890 * @param obj Kernel object or poll signal.
3891 *
3892 * @return N/A
3893 */
3894
Kumar Galacc334c72017-04-21 10:55:34 -05003895extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003896 int mode, void *obj);
3897
3898/**
3899 * @brief Wait for one or many of multiple poll events to occur
3900 *
3901 * This routine allows a thread to wait concurrently for one or many of
3902 * multiple poll events to have occurred. Such events can be a kernel object
3903 * being available, like a semaphore, or a poll signal event.
3904 *
3905 * When an event notifies that a kernel object is available, the kernel object
3906 * is not "given" to the thread calling k_poll(): it merely signals the fact
3907 * that the object was available when the k_poll() call was in effect. Also,
3908 * all threads trying to acquire an object the regular way, i.e. by pending on
3909 * the object, have precedence over the thread polling on the object. This
3910 * means that the polling thread will never get the poll event on an object
3911 * until the object becomes available and its pend queue is empty. For this
3912 * reason, the k_poll() call is more effective when the objects being polled
3913 * only have one thread, the polling thread, trying to acquire them.
3914 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003915 * When k_poll() returns 0, the caller should loop on all the events that were
3916 * passed to k_poll() and check the state field for the values that were
3917 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003918 *
3919 * Before being reused for another call to k_poll(), the user has to reset the
3920 * state field to K_POLL_STATE_NOT_READY.
3921 *
3922 * @param events An array of pointers to events to be polled for.
3923 * @param num_events The number of events in the array.
3924 * @param timeout Waiting period for an event to be ready (in milliseconds),
3925 * or one of the special values K_NO_WAIT and K_FOREVER.
3926 *
3927 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003928 * @retval -EAGAIN Waiting period timed out.
3929 */
3930
3931extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003932 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003933
3934/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003935 * @brief Initialize a poll signal object.
3936 *
3937 * Ready a poll signal object to be signaled via k_poll_signal().
3938 *
3939 * @param signal A poll signal.
3940 *
3941 * @return N/A
3942 */
3943
3944extern void k_poll_signal_init(struct k_poll_signal *signal);
3945
3946/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003947 * @brief Signal a poll signal object.
3948 *
3949 * This routine makes ready a poll signal, which is basically a poll event of
3950 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3951 * made ready to run. A @a result value can be specified.
3952 *
3953 * The poll signal contains a 'signaled' field that, when set by
3954 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3955 * be reset by the user before being passed again to k_poll() or k_poll() will
3956 * consider it being signaled, and will return immediately.
3957 *
3958 * @param signal A poll signal.
3959 * @param result The value to store in the result field of the signal.
3960 *
3961 * @retval 0 The signal was delivered successfully.
3962 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3963 */
3964
3965extern int k_poll_signal(struct k_poll_signal *signal, int result);
3966
3967/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003968extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003969
3970/**
3971 * @} end defgroup poll_apis
3972 */
3973
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003974/**
3975 * @brief Make the CPU idle.
3976 *
3977 * This function makes the CPU idle until an event wakes it up.
3978 *
3979 * In a regular system, the idle thread should be the only thread responsible
3980 * for making the CPU idle and triggering any type of power management.
3981 * However, in some more constrained systems, such as a single-threaded system,
3982 * the only thread would be responsible for this if needed.
3983 *
3984 * @return N/A
3985 */
3986extern void k_cpu_idle(void);
3987
3988/**
3989 * @brief Make the CPU idle in an atomic fashion.
3990 *
3991 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3992 * must be done atomically before making the CPU idle.
3993 *
3994 * @param key Interrupt locking key obtained from irq_lock().
3995 *
3996 * @return N/A
3997 */
3998extern void k_cpu_atomic_idle(unsigned int key);
3999
Kumar Galacc334c72017-04-21 10:55:34 -05004000extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004001
Andrew Boiecdb94d62017-04-18 15:22:05 -07004002#ifdef _ARCH_EXCEPT
4003/* This archtecture has direct support for triggering a CPU exception */
4004#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4005#else
4006
Andrew Boiecdb94d62017-04-18 15:22:05 -07004007/* NOTE: This is the implementation for arches that do not implement
4008 * _ARCH_EXCEPT() to generate a real CPU exception.
4009 *
4010 * We won't have a real exception frame to determine the PC value when
4011 * the oops occurred, so print file and line number before we jump into
4012 * the fatal error handler.
4013 */
4014#define _k_except_reason(reason) do { \
4015 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4016 _NanoFatalErrorHandler(reason, &_default_esf); \
4017 CODE_UNREACHABLE; \
4018 } while (0)
4019
4020#endif /* _ARCH__EXCEPT */
4021
4022/**
4023 * @brief Fatally terminate a thread
4024 *
4025 * This should be called when a thread has encountered an unrecoverable
4026 * runtime condition and needs to terminate. What this ultimately
4027 * means is determined by the _fatal_error_handler() implementation, which
4028 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4029 *
4030 * If this is called from ISR context, the default system fatal error handler
4031 * will treat it as an unrecoverable system error, just like k_panic().
4032 */
4033#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4034
4035/**
4036 * @brief Fatally terminate the system
4037 *
4038 * This should be called when the Zephyr kernel has encountered an
4039 * unrecoverable runtime condition and needs to terminate. What this ultimately
4040 * means is determined by the _fatal_error_handler() implementation, which
4041 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4042 */
4043#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4044
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004045/*
4046 * private APIs that are utilized by one or more public APIs
4047 */
4048
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004049#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004050extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004051#else
4052#define _init_static_threads() do { } while ((0))
4053#endif
4054
4055extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05004056extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004057
Andrew Boiedc5d9352017-06-02 12:56:47 -07004058/* arch/cpu.h may declare an architecture or platform-specific macro
4059 * for properly declaring stacks, compatible with MMU/MPU constraints if
4060 * enabled
4061 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004062
4063/**
4064 * @brief Obtain an extern reference to a stack
4065 *
4066 * This macro properly brings the symbol of a thread stack declared
4067 * elsewhere into scope.
4068 *
4069 * @param sym Thread stack symbol name
4070 */
4071#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4072
Andrew Boiedc5d9352017-06-02 12:56:47 -07004073#ifdef _ARCH_THREAD_STACK_DEFINE
4074#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4075#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4076 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4077#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4078#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004079static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004080{
4081 return _ARCH_THREAD_STACK_BUFFER(sym);
4082}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004083#else
4084/**
4085 * @brief Declare a toplevel thread stack memory region
4086 *
4087 * This declares a region of memory suitable for use as a thread's stack.
4088 *
4089 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4090 * 'noinit' section so that it isn't zeroed at boot
4091 *
Andrew Boie507852a2017-07-25 18:47:07 -07004092 * The declared symbol will always be a k_thread_stack_t which can be passed to
4093 * k_thread_create, but should otherwise not be manipulated. If the buffer
4094 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004095 *
4096 * It is legal to precede this definition with the 'static' keyword.
4097 *
4098 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4099 * parameter of k_thread_create(), it may not be the same as the
4100 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4101 *
4102 * @param sym Thread stack symbol name
4103 * @param size Size of the stack memory region
4104 */
4105#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004106 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004107
4108/**
4109 * @brief Declare a toplevel array of thread stack memory regions
4110 *
4111 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4112 * definition for additional details and constraints.
4113 *
4114 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4115 * 'noinit' section so that it isn't zeroed at boot
4116 *
4117 * @param sym Thread stack symbol name
4118 * @param nmemb Number of stacks to declare
4119 * @param size Size of the stack memory region
4120 */
4121
4122#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004123 struct _k_thread_stack_element __noinit \
4124 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004125
4126/**
4127 * @brief Declare an embedded stack memory region
4128 *
4129 * Used for stacks embedded within other data structures. Use is highly
4130 * discouraged but in some cases necessary. For memory protection scenarios,
4131 * it is very important that any RAM preceding this member not be writable
4132 * by threads else a stack overflow will lead to silent corruption. In other
4133 * words, the containing data structure should live in RAM owned by the kernel.
4134 *
4135 * @param sym Thread stack symbol name
4136 * @param size Size of the stack memory region
4137 */
4138#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004139 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004140
4141/**
4142 * @brief Return the size in bytes of a stack memory region
4143 *
4144 * Convenience macro for passing the desired stack size to k_thread_create()
4145 * since the underlying implementation may actually create something larger
4146 * (for instance a guard area).
4147 *
4148 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004149 * passed to K_THREAD_STACK_DEFINE.
4150 *
4151 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4152 * it is not guaranteed to return the original value since each array
4153 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004154 *
4155 * @param sym Stack memory symbol
4156 * @return Size of the stack
4157 */
4158#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4159
4160/**
4161 * @brief Get a pointer to the physical stack buffer
4162 *
4163 * Convenience macro to get at the real underlying stack buffer used by
4164 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4165 * This is really only intended for diagnostic tools which want to examine
4166 * stack memory contents.
4167 *
4168 * @param sym Declared stack symbol name
4169 * @return The buffer itself, a char *
4170 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004171static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004172{
4173 return (char *)sym;
4174}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004175
4176#endif /* _ARCH_DECLARE_STACK */
4177
Chunlin Hane9c97022017-07-07 20:29:30 +08004178/**
4179 * @defgroup mem_domain_apis Memory domain APIs
4180 * @ingroup kernel_apis
4181 * @{
4182 */
4183
4184/** @def MEM_PARTITION_ENTRY
4185 * @brief Used to declare a memory partition entry
4186 */
4187#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4188 {\
4189 .start = _start, \
4190 .size = _size, \
4191 .attr = _attr, \
4192 }
4193
4194/** @def K_MEM_PARTITION_DEFINE
4195 * @brief Used to declare a memory partition
4196 */
4197#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4198#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4199 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
4200 struct k_mem_partition name = \
4201 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4202#else
4203#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4204 struct k_mem_partition name = \
4205 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4206#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4207
Chunlin Hane9c97022017-07-07 20:29:30 +08004208/* memory partition */
4209struct k_mem_partition {
4210 /* start address of memory partition */
4211 u32_t start;
4212 /* size of memory partition */
4213 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304214#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004215 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304216 k_mem_partition_attr_t attr;
4217#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004218};
4219
Chunlin Hane9c97022017-07-07 20:29:30 +08004220/* memory domian */
4221struct k_mem_domain {
4222 /* number of partitions in the domain */
4223 u32_t num_partitions;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304224#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004225 /* partitions in the domain */
4226 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304227#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004228 /* domain q */
4229 sys_dlist_t mem_domain_q;
4230};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304231
Chunlin Hane9c97022017-07-07 20:29:30 +08004232
4233/**
4234 * @brief Initialize a memory domain.
4235 *
4236 * Initialize a memory domain with given name and memory partitions.
4237 *
4238 * @param domain The memory domain to be initialized.
4239 * @param num_parts The number of array items of "parts" parameter.
4240 * @param parts An array of pointers to the memory partitions. Can be NULL
4241 * if num_parts is zero.
4242 */
4243
4244extern void k_mem_domain_init(struct k_mem_domain *domain, u32_t num_parts,
4245 struct k_mem_partition *parts[]);
4246/**
4247 * @brief Destroy a memory domain.
4248 *
4249 * Destroy a memory domain.
4250 *
4251 * @param domain The memory domain to be destroyed.
4252 */
4253
4254extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4255
4256/**
4257 * @brief Add a memory partition into a memory domain.
4258 *
4259 * Add a memory partition into a memory domain.
4260 *
4261 * @param domain The memory domain to be added a memory partition.
4262 * @param part The memory partition to be added
4263 */
4264
4265extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4266 struct k_mem_partition *part);
4267
4268/**
4269 * @brief Remove a memory partition from a memory domain.
4270 *
4271 * Remove a memory partition from a memory domain.
4272 *
4273 * @param domain The memory domain to be removed a memory partition.
4274 * @param part The memory partition to be removed
4275 */
4276
4277extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4278 struct k_mem_partition *part);
4279
4280/**
4281 * @brief Add a thread into a memory domain.
4282 *
4283 * Add a thread into a memory domain.
4284 *
4285 * @param domain The memory domain that the thread is going to be added into.
4286 * @param thread ID of thread going to be added into the memory domain.
4287 */
4288
4289extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4290 k_tid_t thread);
4291
4292/**
4293 * @brief Remove a thread from its memory domain.
4294 *
4295 * Remove a thread from its memory domain.
4296 *
4297 * @param thread ID of thread going to be removed from its memory domain.
4298 */
4299
4300extern void k_mem_domain_remove_thread(k_tid_t thread);
4301
4302/**
4303 * @} end defgroup mem_domain_apis
4304 */
4305
Andrew Boie756f9072017-10-10 16:01:49 -07004306/**
4307 * @brief Emit a character buffer to the console device
4308 *
4309 * @param c String of characters to print
4310 * @param n The length of the string
4311 */
4312__syscall void k_str_out(char *c, size_t n);
4313
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004314#ifdef __cplusplus
4315}
4316#endif
4317
Andrew Boiee004dec2016-11-07 09:01:19 -08004318#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4319/*
4320 * Define new and delete operators.
4321 * At this moment, the operators do nothing since objects are supposed
4322 * to be statically allocated.
4323 */
4324inline void operator delete(void *ptr)
4325{
4326 (void)ptr;
4327}
4328
4329inline void operator delete[](void *ptr)
4330{
4331 (void)ptr;
4332}
4333
4334inline void *operator new(size_t size)
4335{
4336 (void)size;
4337 return NULL;
4338}
4339
4340inline void *operator new[](size_t size)
4341{
4342 (void)size;
4343 return NULL;
4344}
4345
4346/* Placement versions of operator new and delete */
4347inline void operator delete(void *ptr1, void *ptr2)
4348{
4349 (void)ptr1;
4350 (void)ptr2;
4351}
4352
4353inline void operator delete[](void *ptr1, void *ptr2)
4354{
4355 (void)ptr1;
4356 (void)ptr2;
4357}
4358
4359inline void *operator new(size_t size, void *ptr)
4360{
4361 (void)size;
4362 return ptr;
4363}
4364
4365inline void *operator new[](size_t size, void *ptr)
4366{
4367 (void)size;
4368 return ptr;
4369}
4370
4371#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4372
Andrew Boiefa94ee72017-09-28 16:54:35 -07004373#include <syscalls/kernel.h>
4374
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004375#endif /* !_ASMLANGUAGE */
4376
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004377#endif /* _kernel__h_ */