blob: f7994383494ba2de6a25dd87b9a1038c2caf74a2 [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 Boiece6c8f32018-02-09 13:58:37 -0800153 K_OBJ_DRIVER_DMA,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700154 K_OBJ_DRIVER_FLASH,
155 K_OBJ_DRIVER_GPIO,
156 K_OBJ_DRIVER_I2C,
157 K_OBJ_DRIVER_I2S,
158 K_OBJ_DRIVER_IPM,
159 K_OBJ_DRIVER_PINMUX,
160 K_OBJ_DRIVER_PWM,
Leandro Pereirada9b0dd2017-10-13 16:30:55 -0700161 K_OBJ_DRIVER_ENTROPY,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700162 K_OBJ_DRIVER_RTC,
163 K_OBJ_DRIVER_SENSOR,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700164 K_OBJ_DRIVER_SPI,
165 K_OBJ_DRIVER_UART,
Andrew Boie5bd891d2017-09-27 12:59:28 -0700166
Andrew Boie945af952017-08-22 13:15:23 -0700167 K_OBJ_LAST
168};
169
170#ifdef CONFIG_USERSPACE
171/* Table generated by gperf, these objects are retrieved via
172 * _k_object_find() */
173struct _k_object {
174 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700175 u8_t perms[CONFIG_MAX_THREAD_BYTES];
176 u8_t type;
177 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700178 u32_t data;
Andrew Boie945af952017-08-22 13:15:23 -0700179} __packed;
180
Andrew Boie877f82e2017-10-17 11:20:22 -0700181struct _k_object_assignment {
182 struct k_thread *thread;
183 void * const *objects;
184};
185
186/**
187 * @brief Grant a static thread access to a list of kernel objects
188 *
189 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
190 * a set of kernel objects. These objects do not need to be in an initialized
191 * state. The permissions will be granted when the threads are initialized
192 * in the early boot sequence.
193 *
194 * All arguments beyond the first must be pointers to kernel objects.
195 *
196 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
197 */
198#define K_THREAD_ACCESS_GRANT(name_, ...) \
199 static void * const _CONCAT(_object_list_, name_)[] = \
200 { __VA_ARGS__, NULL }; \
201 static __used __in_section_unique(object_access) \
202 const struct _k_object_assignment \
203 _CONCAT(_object_access_, name_) = \
204 { (&_k_thread_obj_ ## name_), \
205 (_CONCAT(_object_list_, name_)) }
206
Andrew Boie945af952017-08-22 13:15:23 -0700207#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700208#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie945af952017-08-22 13:15:23 -0700209
210/**
211 * Lookup a kernel object and init its metadata if it exists
212 *
213 * Calling this on an object will make it usable from userspace.
214 * Intended to be called as the last statement in kernel object init
215 * functions.
216 *
217 * @param object Address of the kernel object
218 */
219void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700220#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700221
222#define K_THREAD_ACCESS_GRANT(thread, ...)
223
Andrew Boie743e4682017-10-04 12:25:50 -0700224static inline void _k_object_init(void *obj)
225{
226 ARG_UNUSED(obj);
227}
228
229static inline void _impl_k_object_access_grant(void *object,
230 struct k_thread *thread)
231{
232 ARG_UNUSED(object);
233 ARG_UNUSED(thread);
234}
235
Andrew Boiea89bf012017-10-09 14:47:55 -0700236static inline void _impl_k_object_access_revoke(void *object,
237 struct k_thread *thread)
238{
239 ARG_UNUSED(object);
240 ARG_UNUSED(thread);
241}
242
Andrew Boie41bab6e2017-10-14 14:42:23 -0700243static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700244{
245 ARG_UNUSED(object);
246}
247#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700248
249/**
250 * grant a thread access to a kernel object
251 *
252 * The thread will be granted access to the object if the caller is from
253 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700254 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700255 *
256 * @param object Address of kernel object
257 * @param thread Thread to grant access to the object
258 */
Andrew Boie743e4682017-10-04 12:25:50 -0700259__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700260
Andrew Boiea89bf012017-10-09 14:47:55 -0700261/**
262 * grant a thread access to a kernel object
263 *
264 * The thread will lose access to the object if the caller is from
265 * supervisor mode, or the caller is from user mode AND has permissions
266 * on both the object and the thread whose access is being revoked.
267 *
268 * @param object Address of kernel object
269 * @param thread Thread to remove access to the object
270 */
271__syscall void k_object_access_revoke(void *object, struct k_thread *thread);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700272
273/**
274 * grant all present and future threads access to an object
275 *
276 * If the caller is from supervisor mode, or the caller is from user mode and
277 * have sufficient permissions on the object, then that object will have
278 * permissions granted to it for *all* current and future threads running in
279 * the system, effectively becoming a public kernel object.
280 *
281 * Use of this API should be avoided on systems that are running untrusted code
282 * as it is possible for such code to derive the addresses of kernel objects
283 * and perform unwanted operations on them.
284 *
Andrew Boie04caa672017-10-13 13:57:07 -0700285 * It is not possible to revoke permissions on public objects; once public,
286 * any thread may use it.
287 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700288 * @param object Address of kernel object
289 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700290void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700291
Andrew Boiebca15da2017-10-15 14:17:48 -0700292/* Using typedef deliberately here, this is quite intended to be an opaque
293 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
294 *
295 * The purpose of this data type is to clearly distinguish between the
296 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
297 * buffer which composes the stack data actually used by the underlying
298 * thread; they cannot be used interchangably as some arches precede the
299 * stack buffer region with guard areas that trigger a MPU or MMU fault
300 * if written to.
301 *
302 * APIs that want to work with the buffer inside should continue to use
303 * char *.
304 *
305 * Stacks should always be created with K_THREAD_STACK_DEFINE().
306 */
307struct __packed _k_thread_stack_element {
308 char data;
309};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700310typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700311
Andrew Boie73abd322017-04-04 13:19:13 -0700312/* timeouts */
313
314struct _timeout;
315typedef void (*_timeout_func_t)(struct _timeout *t);
316
317struct _timeout {
318 sys_dnode_t node;
319 struct k_thread *thread;
320 sys_dlist_t *wait_q;
321 s32_t delta_ticks_from_prev;
322 _timeout_func_t func;
323};
324
325extern s32_t _timeout_remaining_get(struct _timeout *timeout);
326
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700327/**
328 * @typedef k_thread_entry_t
329 * @brief Thread entry point function type.
330 *
331 * A thread's entry point function is invoked when the thread starts executing.
332 * Up to 3 argument values can be passed to the function.
333 *
334 * The thread terminates execution permanently if the entry point function
335 * returns. The thread is responsible for releasing any shared resources
336 * it may own (such as mutexes and dynamically allocated memory), prior to
337 * returning.
338 *
339 * @param p1 First argument.
340 * @param p2 Second argument.
341 * @param p3 Third argument.
342 *
343 * @return N/A
344 */
345typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700346
347#ifdef CONFIG_THREAD_MONITOR
348struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700349 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700350 void *parameter1;
351 void *parameter2;
352 void *parameter3;
353};
354#endif
355
356/* can be used for creating 'dummy' threads, e.g. for pending on objects */
357struct _thread_base {
358
359 /* this thread's entry in a ready/wait queue */
360 sys_dnode_t k_q_node;
361
362 /* user facing 'thread options'; values defined in include/kernel.h */
363 u8_t user_options;
364
365 /* thread state */
366 u8_t thread_state;
367
368 /*
369 * scheduler lock count and thread priority
370 *
371 * These two fields control the preemptibility of a thread.
372 *
373 * When the scheduler is locked, sched_locked is decremented, which
374 * means that the scheduler is locked for values from 0xff to 0x01. A
375 * thread is coop if its prio is negative, thus 0x80 to 0xff when
376 * looked at the value as unsigned.
377 *
378 * By putting them end-to-end, this means that a thread is
379 * non-preemptible if the bundled value is greater than or equal to
380 * 0x0080.
381 */
382 union {
383 struct {
384#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
385 u8_t sched_locked;
386 s8_t prio;
387#else /* LITTLE and PDP */
388 s8_t prio;
389 u8_t sched_locked;
390#endif
391 };
392 u16_t preempt;
393 };
394
395 /* data returned by APIs */
396 void *swap_data;
397
398#ifdef CONFIG_SYS_CLOCK_EXISTS
399 /* this thread's entry in a timeout queue */
400 struct _timeout timeout;
401#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700402};
403
404typedef struct _thread_base _thread_base_t;
405
406#if defined(CONFIG_THREAD_STACK_INFO)
407/* Contains the stack information of a thread */
408struct _thread_stack_info {
409 /* Stack Start */
410 u32_t start;
411 /* Stack Size */
412 u32_t size;
413};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700414
415typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700416#endif /* CONFIG_THREAD_STACK_INFO */
417
Chunlin Hane9c97022017-07-07 20:29:30 +0800418#if defined(CONFIG_USERSPACE)
419struct _mem_domain_info {
420 /* memory domain queue node */
421 sys_dnode_t mem_domain_q_node;
422 /* memory domain of the thread */
423 struct k_mem_domain *mem_domain;
424};
425
426#endif /* CONFIG_USERSPACE */
427
Andrew Boie73abd322017-04-04 13:19:13 -0700428struct k_thread {
429
430 struct _thread_base base;
431
432 /* defined by the architecture, but all archs need these */
433 struct _caller_saved caller_saved;
434 struct _callee_saved callee_saved;
435
436 /* static thread init data */
437 void *init_data;
438
439 /* abort function */
440 void (*fn_abort)(void);
441
442#if defined(CONFIG_THREAD_MONITOR)
443 /* thread entry and parameters description */
444 struct __thread_entry *entry;
445
446 /* next item in list of all threads */
447 struct k_thread *next_thread;
448#endif
449
450#ifdef CONFIG_THREAD_CUSTOM_DATA
451 /* crude thread-local storage */
452 void *custom_data;
453#endif
454
455#ifdef CONFIG_ERRNO
456 /* per-thread errno variable */
457 int errno_var;
458#endif
459
460#if defined(CONFIG_THREAD_STACK_INFO)
461 /* Stack Info */
462 struct _thread_stack_info stack_info;
463#endif /* CONFIG_THREAD_STACK_INFO */
464
Chunlin Hane9c97022017-07-07 20:29:30 +0800465#if defined(CONFIG_USERSPACE)
466 /* memory domain info of the thread */
467 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700468 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700469 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800470#endif /* CONFIG_USERSPACE */
471
Andrew Boie73abd322017-04-04 13:19:13 -0700472 /* arch-specifics: must always be at the end */
473 struct _thread_arch arch;
474};
475
476typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400477typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700478#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400479
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400480enum execution_context_types {
481 K_ISR = 0,
482 K_COOP_THREAD,
483 K_PREEMPT_THREAD,
484};
485
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400486/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100487 * @defgroup profiling_apis Profiling APIs
488 * @ingroup kernel_apis
489 * @{
490 */
491
492/**
493 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
494 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700495 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100496 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
497 *
498 * CONFIG_MAIN_STACK_SIZE
499 * CONFIG_IDLE_STACK_SIZE
500 * CONFIG_ISR_STACK_SIZE
501 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
502 *
503 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
504 * produce output.
505 *
506 * @return N/A
507 */
508extern void k_call_stacks_analyze(void);
509
510/**
511 * @} end defgroup profiling_apis
512 */
513
514/**
Allan Stephensc98da842016-11-11 15:45:03 -0500515 * @defgroup thread_apis Thread APIs
516 * @ingroup kernel_apis
517 * @{
518 */
519
Benjamin Walshed240f22017-01-22 13:05:08 -0500520#endif /* !_ASMLANGUAGE */
521
522
523/*
524 * Thread user options. May be needed by assembly code. Common part uses low
525 * bits, arch-specific use high bits.
526 */
527
528/* system thread that must not abort */
529#define K_ESSENTIAL (1 << 0)
530
531#if defined(CONFIG_FP_SHARING)
532/* thread uses floating point registers */
533#define K_FP_REGS (1 << 1)
534#endif
535
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700536/* This thread has dropped from supervisor mode to user mode and consequently
537 * has additional restrictions
538 */
539#define K_USER (1 << 2)
540
Andrew Boie47f8fd12017-10-05 11:11:02 -0700541/* Indicates that the thread being created should inherit all kernel object
542 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
543 * is not enabled.
544 */
545#define K_INHERIT_PERMS (1 << 3)
546
Benjamin Walshed240f22017-01-22 13:05:08 -0500547#ifdef CONFIG_X86
548/* x86 Bitmask definitions for threads user options */
549
550#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
551/* thread uses SSEx (and also FP) registers */
552#define K_SSE_REGS (1 << 7)
553#endif
554#endif
555
556/* end - thread options */
557
558#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400559/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700560 * @brief Create a thread.
561 *
562 * This routine initializes a thread, then schedules it for execution.
563 *
564 * The new thread may be scheduled for immediate execution or a delayed start.
565 * If the newly spawned thread does not have a delayed start the kernel
566 * scheduler may preempt the current thread to allow the new thread to
567 * execute.
568 *
569 * Thread options are architecture-specific, and can include K_ESSENTIAL,
570 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
571 * them using "|" (the logical OR operator).
572 *
573 * Historically, users often would use the beginning of the stack memory region
574 * to store the struct k_thread data, although corruption will occur if the
575 * stack overflows this region and stack protection features may not detect this
576 * situation.
577 *
578 * @param new_thread Pointer to uninitialized struct k_thread
579 * @param stack Pointer to the stack space.
580 * @param stack_size Stack size in bytes.
581 * @param entry Thread entry function.
582 * @param p1 1st entry point parameter.
583 * @param p2 2nd entry point parameter.
584 * @param p3 3rd entry point parameter.
585 * @param prio Thread priority.
586 * @param options Thread options.
587 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
588 *
589 * @return ID of new thread.
590 */
Andrew Boie662c3452017-10-02 10:51:18 -0700591__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700592 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700593 size_t stack_size,
594 k_thread_entry_t entry,
595 void *p1, void *p2, void *p3,
596 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700597
Andrew Boie3f091b52017-08-30 14:34:14 -0700598/**
599 * @brief Drop a thread's privileges permanently to user mode
600 *
601 * @param entry Function to start executing from
602 * @param p1 1st entry point parameter
603 * @param p2 2nd entry point parameter
604 * @param p3 3rd entry point parameter
605 */
606extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
607 void *p1, void *p2,
608 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700609
Andrew Boied26cf2d2017-03-30 13:07:02 -0700610/**
Andrew Boiee12857a2017-10-17 11:38:26 -0700611 * @brief Grant a thread access to a NULL-terminated set of kernel objects
612 *
613 * This is a convenience function. For the provided thread, grant access to
614 * the remaining arguments, which must be pointers to kernel objects.
615 * The final argument must be a NULL.
616 *
617 * The thread object must be initialized (i.e. running). The objects don't
618 * need to be.
619 *
620 * @param thread Thread to grant access to objects
621 * @param ... NULL-terminated list of kernel object pointers
622 */
623extern void __attribute__((sentinel))
624 k_thread_access_grant(struct k_thread *thread, ...);
625
626/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500627 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400628 *
Allan Stephensc98da842016-11-11 15:45:03 -0500629 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500630 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400631 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500632 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400633 *
634 * @return N/A
635 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700636__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400637
638/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500639 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400640 *
641 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500642 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400643 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400644 * @return N/A
645 */
Kumar Galacc334c72017-04-21 10:55:34 -0500646extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400647
648/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500649 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500651 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400652 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500653 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400654 *
655 * @return N/A
656 */
Andrew Boie468190a2017-09-29 14:00:48 -0700657__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400658
659/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500660 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400661 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500662 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400663 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500664 * If @a thread is not currently sleeping, the routine has no effect.
665 *
666 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400667 *
668 * @return N/A
669 */
Andrew Boie468190a2017-09-29 14:00:48 -0700670__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400671
672/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500673 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400674 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500675 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400676 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700677__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400678
679/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500680 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400681 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500682 * This routine prevents @a thread from executing if it has not yet started
683 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400684 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500685 * @param thread ID of thread to cancel.
686 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700687 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500688 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400689 */
Andrew Boie468190a2017-09-29 14:00:48 -0700690__syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400691
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400692/**
Allan Stephensc98da842016-11-11 15:45:03 -0500693 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400694 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500695 * This routine permanently stops execution of @a thread. The thread is taken
696 * off all kernel queues it is part of (i.e. the ready queue, the timeout
697 * queue, or a kernel object wait queue). However, any kernel resources the
698 * thread might currently own (such as mutexes or memory blocks) are not
699 * released. It is the responsibility of the caller of this routine to ensure
700 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400701 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500702 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400703 *
704 * @return N/A
705 */
Andrew Boie468190a2017-09-29 14:00:48 -0700706__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400707
Andrew Boie7d627c52017-08-30 11:01:56 -0700708
709/**
710 * @brief Start an inactive thread
711 *
712 * If a thread was created with K_FOREVER in the delay parameter, it will
713 * not be added to the scheduling queue until this function is called
714 * on it.
715 *
716 * @param thread thread to start
717 */
Andrew Boie468190a2017-09-29 14:00:48 -0700718__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700719
Allan Stephensc98da842016-11-11 15:45:03 -0500720/**
721 * @cond INTERNAL_HIDDEN
722 */
723
Benjamin Walshd211a522016-12-06 11:44:01 -0500724/* timeout has timed out and is not on _timeout_q anymore */
725#define _EXPIRED (-2)
726
727/* timeout is not in use */
728#define _INACTIVE (-1)
729
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400730struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700731 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700732 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400733 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700734 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500735 void *init_p1;
736 void *init_p2;
737 void *init_p3;
738 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500739 u32_t init_options;
740 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500741 void (*init_abort)(void);
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), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400759 }
760
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400761/**
Allan Stephensc98da842016-11-11 15:45:03 -0500762 * INTERNAL_HIDDEN @endcond
763 */
764
765/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500766 * @brief Statically define and initialize a thread.
767 *
768 * The thread may be scheduled for immediate execution or a delayed start.
769 *
770 * Thread options are architecture-specific, and can include K_ESSENTIAL,
771 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
772 * them using "|" (the logical OR operator).
773 *
774 * The ID of the thread can be accessed using:
775 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500776 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500777 *
778 * @param name Name of the thread.
779 * @param stack_size Stack size in bytes.
780 * @param entry Thread entry function.
781 * @param p1 1st entry point parameter.
782 * @param p2 2nd entry point parameter.
783 * @param p3 3rd entry point parameter.
784 * @param prio Thread priority.
785 * @param options Thread options.
786 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400787 *
788 * @internal It has been observed that the x86 compiler by default aligns
789 * these _static_thread_data structures to 32-byte boundaries, thereby
790 * wasting space. To work around this, force a 4-byte alignment.
791 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500792#define K_THREAD_DEFINE(name, stack_size, \
793 entry, p1, p2, p3, \
794 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700795 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700796 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500797 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500798 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700799 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
800 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500801 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700802 NULL, 0); \
803 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400804
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400805/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500806 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400807 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500808 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400809 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500810 * @param thread ID of thread whose priority is needed.
811 *
812 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400813 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700814__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400815
816/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500817 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500819 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400820 *
821 * Rescheduling can occur immediately depending on the priority @a thread is
822 * set to:
823 *
824 * - If its priority is raised above the priority of the caller of this
825 * function, and the caller is preemptible, @a thread will be scheduled in.
826 *
827 * - If the caller operates on itself, it lowers its priority below that of
828 * other threads in the system, and the caller is preemptible, the thread of
829 * highest priority will be scheduled in.
830 *
831 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
832 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
833 * highest priority.
834 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500835 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400836 * @param prio New priority.
837 *
838 * @warning Changing the priority of a thread currently involved in mutex
839 * priority inheritance may result in undefined behavior.
840 *
841 * @return N/A
842 */
Andrew Boie468190a2017-09-29 14:00:48 -0700843__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400844
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400845/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500846 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400847 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500848 * This routine prevents the kernel scheduler from making @a thread the
849 * current thread. All other internal operations on @a thread are still
850 * performed; for example, any timeout it is waiting on keeps ticking,
851 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400852 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500853 * If @a thread is already suspended, the routine has no effect.
854 *
855 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400856 *
857 * @return N/A
858 */
Andrew Boie468190a2017-09-29 14:00:48 -0700859__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400860
861/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500862 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400863 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500864 * This routine allows the kernel scheduler to make @a thread the current
865 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400866 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500867 * If @a thread is not currently suspended, the routine has no effect.
868 *
869 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400870 *
871 * @return N/A
872 */
Andrew Boie468190a2017-09-29 14:00:48 -0700873__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400874
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400875/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500876 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400877 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500878 * This routine specifies how the scheduler will perform time slicing of
879 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500881 * To enable time slicing, @a slice must be non-zero. The scheduler
882 * ensures that no thread runs for more than the specified time limit
883 * before other threads of that priority are given a chance to execute.
884 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700885 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400886 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500887 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400888 * execute. Once the scheduler selects a thread for execution, there is no
889 * minimum guaranteed time the thread will execute before threads of greater or
890 * equal priority are scheduled.
891 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500892 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400893 * for execution, this routine has no effect; the thread is immediately
894 * rescheduled after the slice period expires.
895 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500896 * To disable timeslicing, set both @a slice and @a prio to zero.
897 *
898 * @param slice Maximum time slice length (in milliseconds).
899 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400900 *
901 * @return N/A
902 */
Kumar Galacc334c72017-04-21 10:55:34 -0500903extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400904
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400905/**
Allan Stephensc98da842016-11-11 15:45:03 -0500906 * @} end defgroup thread_apis
907 */
908
909/**
910 * @addtogroup isr_apis
911 * @{
912 */
913
914/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500915 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400916 *
Allan Stephensc98da842016-11-11 15:45:03 -0500917 * This routine allows the caller to customize its actions, depending on
918 * whether it is a thread or an ISR.
919 *
920 * @note Can be called by ISRs.
921 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500922 * @return 0 if invoked by a thread.
923 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400924 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500925extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400926
Benjamin Walsh445830d2016-11-10 15:54:27 -0500927/**
928 * @brief Determine if code is running in a preemptible thread.
929 *
Allan Stephensc98da842016-11-11 15:45:03 -0500930 * This routine allows the caller to customize its actions, depending on
931 * whether it can be preempted by another thread. The routine returns a 'true'
932 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500933 *
Allan Stephensc98da842016-11-11 15:45:03 -0500934 * - The code is running in a thread, not at ISR.
935 * - The thread's priority is in the preemptible range.
936 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500937 *
Allan Stephensc98da842016-11-11 15:45:03 -0500938 * @note Can be called by ISRs.
939 *
940 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500941 * @return Non-zero if invoked by a preemptible thread.
942 */
Andrew Boie468190a2017-09-29 14:00:48 -0700943__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -0500944
Allan Stephensc98da842016-11-11 15:45:03 -0500945/**
946 * @} end addtogroup isr_apis
947 */
948
949/**
950 * @addtogroup thread_apis
951 * @{
952 */
953
954/**
955 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500956 *
Allan Stephensc98da842016-11-11 15:45:03 -0500957 * This routine prevents the current thread from being preempted by another
958 * thread by instructing the scheduler to treat it as a cooperative thread.
959 * If the thread subsequently performs an operation that makes it unready,
960 * it will be context switched out in the normal manner. When the thread
961 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500962 *
Allan Stephensc98da842016-11-11 15:45:03 -0500963 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500964 *
Allan Stephensc98da842016-11-11 15:45:03 -0500965 * @note k_sched_lock() and k_sched_unlock() should normally be used
966 * when the operation being performed can be safely interrupted by ISRs.
967 * However, if the amount of processing involved is very small, better
968 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500969 *
970 * @return N/A
971 */
972extern void k_sched_lock(void);
973
Allan Stephensc98da842016-11-11 15:45:03 -0500974/**
975 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500976 *
Allan Stephensc98da842016-11-11 15:45:03 -0500977 * This routine reverses the effect of a previous call to k_sched_lock().
978 * A thread must call the routine once for each time it called k_sched_lock()
979 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500980 *
981 * @return N/A
982 */
983extern void k_sched_unlock(void);
984
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400985/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500986 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400987 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500988 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400989 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500990 * Custom data is not used by the kernel itself, and is freely available
991 * for a thread to use as it sees fit. It can be used as a framework
992 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500994 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400995 *
996 * @return N/A
997 */
Andrew Boie468190a2017-09-29 14:00:48 -0700998__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400999
1000/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001001 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001002 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001003 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001004 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001005 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001006 */
Andrew Boie468190a2017-09-29 14:00:48 -07001007__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001008
1009/**
Allan Stephensc98da842016-11-11 15:45:03 -05001010 * @} end addtogroup thread_apis
1011 */
1012
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001013#include <sys_clock.h>
1014
Allan Stephensc2f15a42016-11-17 12:24:22 -05001015/**
1016 * @addtogroup clock_apis
1017 * @{
1018 */
1019
1020/**
1021 * @brief Generate null timeout delay.
1022 *
1023 * This macro generates a timeout delay that that instructs a kernel API
1024 * not to wait if the requested operation cannot be performed immediately.
1025 *
1026 * @return Timeout delay value.
1027 */
1028#define K_NO_WAIT 0
1029
1030/**
1031 * @brief Generate timeout delay from milliseconds.
1032 *
1033 * This macro generates a timeout delay that that instructs a kernel API
1034 * to wait up to @a ms milliseconds to perform the requested operation.
1035 *
1036 * @param ms Duration in milliseconds.
1037 *
1038 * @return Timeout delay value.
1039 */
Johan Hedberg14471692016-11-13 10:52:15 +02001040#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001041
1042/**
1043 * @brief Generate timeout delay from seconds.
1044 *
1045 * This macro generates a timeout delay that that instructs a kernel API
1046 * to wait up to @a s seconds to perform the requested operation.
1047 *
1048 * @param s Duration in seconds.
1049 *
1050 * @return Timeout delay value.
1051 */
Johan Hedberg14471692016-11-13 10:52:15 +02001052#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001053
1054/**
1055 * @brief Generate timeout delay from minutes.
1056 *
1057 * This macro generates a timeout delay that that instructs a kernel API
1058 * to wait up to @a m minutes to perform the requested operation.
1059 *
1060 * @param m Duration in minutes.
1061 *
1062 * @return Timeout delay value.
1063 */
Johan Hedberg14471692016-11-13 10:52:15 +02001064#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001065
1066/**
1067 * @brief Generate timeout delay from hours.
1068 *
1069 * This macro generates a timeout delay that that instructs a kernel API
1070 * to wait up to @a h hours to perform the requested operation.
1071 *
1072 * @param h Duration in hours.
1073 *
1074 * @return Timeout delay value.
1075 */
Johan Hedberg14471692016-11-13 10:52:15 +02001076#define K_HOURS(h) K_MINUTES((h) * 60)
1077
Allan Stephensc98da842016-11-11 15:45:03 -05001078/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001079 * @brief Generate infinite timeout delay.
1080 *
1081 * This macro generates a timeout delay that that instructs a kernel API
1082 * to wait as long as necessary to perform the requested operation.
1083 *
1084 * @return Timeout delay value.
1085 */
1086#define K_FOREVER (-1)
1087
1088/**
1089 * @} end addtogroup clock_apis
1090 */
1091
1092/**
Allan Stephensc98da842016-11-11 15:45:03 -05001093 * @cond INTERNAL_HIDDEN
1094 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001095
Benjamin Walsh62092182016-12-20 14:39:08 -05001096/* kernel clocks */
1097
1098#if (sys_clock_ticks_per_sec == 1000) || \
1099 (sys_clock_ticks_per_sec == 500) || \
1100 (sys_clock_ticks_per_sec == 250) || \
1101 (sys_clock_ticks_per_sec == 125) || \
1102 (sys_clock_ticks_per_sec == 100) || \
1103 (sys_clock_ticks_per_sec == 50) || \
1104 (sys_clock_ticks_per_sec == 25) || \
1105 (sys_clock_ticks_per_sec == 20) || \
1106 (sys_clock_ticks_per_sec == 10) || \
1107 (sys_clock_ticks_per_sec == 1)
1108
1109 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1110#else
1111 /* yields horrible 64-bit math on many architectures: try to avoid */
1112 #define _NON_OPTIMIZED_TICKS_PER_SEC
1113#endif
1114
1115#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001116extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001117#else
Kumar Galacc334c72017-04-21 10:55:34 -05001118static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001119{
Kumar Galacc334c72017-04-21 10:55:34 -05001120 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001121}
1122#endif
1123
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001124/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001125#ifdef CONFIG_TICKLESS_KERNEL
1126#define _TICK_ALIGN 0
1127#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001128#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001129#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001130
Kumar Galacc334c72017-04-21 10:55:34 -05001131static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001132{
Benjamin Walsh62092182016-12-20 14:39:08 -05001133#ifdef CONFIG_SYS_CLOCK_EXISTS
1134
1135#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001136 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001137#else
Kumar Galacc334c72017-04-21 10:55:34 -05001138 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001139#endif
1140
1141#else
Anas Nashif7b9d8992018-01-09 09:13:28 -05001142 __ASSERT(ticks == 0, "ticks not zero");
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001143 return 0;
1144#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001145}
1146
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001147struct k_timer {
1148 /*
1149 * _timeout structure must be first here if we want to use
1150 * dynamic timer allocation. timeout.node is used in the double-linked
1151 * list of free timers
1152 */
1153 struct _timeout timeout;
1154
Allan Stephens45bfa372016-10-12 12:39:42 -05001155 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001156 _wait_q_t wait_q;
1157
1158 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001159 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001160
1161 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001162 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001163
1164 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001165 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001166
Allan Stephens45bfa372016-10-12 12:39:42 -05001167 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001168 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001169
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001170 /* user-specific data, also used to support legacy features */
1171 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001172
Anas Nashif2f203c22016-12-18 06:57:45 -05001173 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001174};
1175
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001176#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001177 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001178 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001179 .timeout.wait_q = NULL, \
1180 .timeout.thread = NULL, \
1181 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001182 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001183 .expiry_fn = expiry, \
1184 .stop_fn = stop, \
1185 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001186 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001187 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001188 }
1189
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001190#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1191
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001192/**
Allan Stephensc98da842016-11-11 15:45:03 -05001193 * INTERNAL_HIDDEN @endcond
1194 */
1195
1196/**
1197 * @defgroup timer_apis Timer APIs
1198 * @ingroup kernel_apis
1199 * @{
1200 */
1201
1202/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001203 * @typedef k_timer_expiry_t
1204 * @brief Timer expiry function type.
1205 *
1206 * A timer's expiry function is executed by the system clock interrupt handler
1207 * each time the timer expires. The expiry function is optional, and is only
1208 * invoked if the timer has been initialized with one.
1209 *
1210 * @param timer Address of timer.
1211 *
1212 * @return N/A
1213 */
1214typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1215
1216/**
1217 * @typedef k_timer_stop_t
1218 * @brief Timer stop function type.
1219 *
1220 * A timer's stop function is executed if the timer is stopped prematurely.
1221 * The function runs in the context of the thread that stops the timer.
1222 * The stop function is optional, and is only invoked if the timer has been
1223 * initialized with one.
1224 *
1225 * @param timer Address of timer.
1226 *
1227 * @return N/A
1228 */
1229typedef void (*k_timer_stop_t)(struct k_timer *timer);
1230
1231/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001232 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001233 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001234 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001235 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001236 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001237 *
1238 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001239 * @param expiry_fn Function to invoke each time the timer expires.
1240 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001241 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001242#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001243 struct k_timer name \
1244 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001245 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001246
Allan Stephens45bfa372016-10-12 12:39:42 -05001247/**
1248 * @brief Initialize a timer.
1249 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001250 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001251 *
1252 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001253 * @param expiry_fn Function to invoke each time the timer expires.
1254 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001255 *
1256 * @return N/A
1257 */
1258extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001259 k_timer_expiry_t expiry_fn,
1260 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001261
Allan Stephens45bfa372016-10-12 12:39:42 -05001262/**
1263 * @brief Start a timer.
1264 *
1265 * This routine starts a timer, and resets its status to zero. The timer
1266 * begins counting down using the specified duration and period values.
1267 *
1268 * Attempting to start a timer that is already running is permitted.
1269 * The timer's status is reset to zero and the timer begins counting down
1270 * using the new duration and period values.
1271 *
1272 * @param timer Address of timer.
1273 * @param duration Initial timer duration (in milliseconds).
1274 * @param period Timer period (in milliseconds).
1275 *
1276 * @return N/A
1277 */
Andrew Boiea354d492017-09-29 16:22:28 -07001278__syscall void k_timer_start(struct k_timer *timer,
1279 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001280
1281/**
1282 * @brief Stop a timer.
1283 *
1284 * This routine stops a running timer prematurely. The timer's stop function,
1285 * if one exists, is invoked by the caller.
1286 *
1287 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001288 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001289 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001290 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1291 * if @a k_timer_stop is to be called from ISRs.
1292 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001293 * @param timer Address of timer.
1294 *
1295 * @return N/A
1296 */
Andrew Boiea354d492017-09-29 16:22:28 -07001297__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001298
1299/**
1300 * @brief Read timer status.
1301 *
1302 * This routine reads the timer's status, which indicates the number of times
1303 * it has expired since its status was last read.
1304 *
1305 * Calling this routine resets the timer's status to zero.
1306 *
1307 * @param timer Address of timer.
1308 *
1309 * @return Timer status.
1310 */
Andrew Boiea354d492017-09-29 16:22:28 -07001311__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001312
1313/**
1314 * @brief Synchronize thread to timer expiration.
1315 *
1316 * This routine blocks the calling thread until the timer's status is non-zero
1317 * (indicating that it has expired at least once since it was last examined)
1318 * or the timer is stopped. If the timer status is already non-zero,
1319 * or the timer is already stopped, the caller continues without waiting.
1320 *
1321 * Calling this routine resets the timer's status to zero.
1322 *
1323 * This routine must not be used by interrupt handlers, since they are not
1324 * allowed to block.
1325 *
1326 * @param timer Address of timer.
1327 *
1328 * @return Timer status.
1329 */
Andrew Boiea354d492017-09-29 16:22:28 -07001330__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001331
1332/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001333 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001334 *
1335 * This routine computes the (approximate) time remaining before a running
1336 * timer next expires. If the timer is not running, it returns zero.
1337 *
1338 * @param timer Address of timer.
1339 *
1340 * @return Remaining time (in milliseconds).
1341 */
Andrew Boiea354d492017-09-29 16:22:28 -07001342__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1343
1344static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001345{
1346 return _timeout_remaining_get(&timer->timeout);
1347}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001348
Allan Stephensc98da842016-11-11 15:45:03 -05001349/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001350 * @brief Associate user-specific data with a timer.
1351 *
1352 * This routine records the @a user_data with the @a timer, to be retrieved
1353 * later.
1354 *
1355 * It can be used e.g. in a timer handler shared across multiple subsystems to
1356 * retrieve data specific to the subsystem this timer is associated with.
1357 *
1358 * @param timer Address of timer.
1359 * @param user_data User data to associate with the timer.
1360 *
1361 * @return N/A
1362 */
Andrew Boiea354d492017-09-29 16:22:28 -07001363__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1364
1365static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1366 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001367{
1368 timer->user_data = user_data;
1369}
1370
1371/**
1372 * @brief Retrieve the user-specific data from a timer.
1373 *
1374 * @param timer Address of timer.
1375 *
1376 * @return The user data.
1377 */
Andrew Boiea354d492017-09-29 16:22:28 -07001378__syscall void *k_timer_user_data_get(struct k_timer *timer);
1379
1380static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001381{
1382 return timer->user_data;
1383}
1384
1385/**
Allan Stephensc98da842016-11-11 15:45:03 -05001386 * @} end defgroup timer_apis
1387 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001388
Allan Stephensc98da842016-11-11 15:45:03 -05001389/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001390 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001391 * @{
1392 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001393
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001394/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001395 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001396 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001397 * This routine returns the elapsed time since the system booted,
1398 * in milliseconds.
1399 *
1400 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001401 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001402__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001403
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001404#ifdef CONFIG_TICKLESS_KERNEL
1405/**
1406 * @brief Enable clock always on in tickless kernel
1407 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001408 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001409 * there are no timer events programmed in tickless kernel
1410 * scheduling. This is necessary if the clock is used to track
1411 * passage of time.
1412 *
1413 * @retval prev_status Previous status of always on flag
1414 */
1415static inline int k_enable_sys_clock_always_on(void)
1416{
1417 int prev_status = _sys_clock_always_on;
1418
1419 _sys_clock_always_on = 1;
1420 _enable_sys_clock();
1421
1422 return prev_status;
1423}
1424
1425/**
1426 * @brief Disable clock always on in tickless kernel
1427 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001428 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001429 * there are no timer events programmed in tickless kernel
1430 * scheduling. To save power, this routine should be called
1431 * immediately when clock is not used to track time.
1432 */
1433static inline void k_disable_sys_clock_always_on(void)
1434{
1435 _sys_clock_always_on = 0;
1436}
1437#else
1438#define k_enable_sys_clock_always_on() do { } while ((0))
1439#define k_disable_sys_clock_always_on() do { } while ((0))
1440#endif
1441
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001442/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001443 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001444 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001445 * This routine returns the lower 32-bits of the elapsed time since the system
1446 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001447 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001448 * This routine can be more efficient than k_uptime_get(), as it reduces the
1449 * need for interrupt locking and 64-bit math. However, the 32-bit result
1450 * cannot hold a system uptime time larger than approximately 50 days, so the
1451 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001452 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001453 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001454 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001455__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001456
1457/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001458 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001459 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001460 * This routine computes the elapsed time between the current system uptime
1461 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001462 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001463 * @param reftime Pointer to a reference time, which is updated to the current
1464 * uptime upon return.
1465 *
1466 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001467 */
Kumar Galacc334c72017-04-21 10:55:34 -05001468extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001469
1470/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001471 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001472 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001473 * This routine computes the elapsed time between the current system uptime
1474 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001475 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001476 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1477 * need for interrupt locking and 64-bit math. However, the 32-bit result
1478 * cannot hold an elapsed time larger than approximately 50 days, so the
1479 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001480 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001481 * @param reftime Pointer to a reference time, which is updated to the current
1482 * uptime upon return.
1483 *
1484 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001485 */
Kumar Galacc334c72017-04-21 10:55:34 -05001486extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001487
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001488/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001489 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001490 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001491 * This routine returns the current time, as measured by the system's hardware
1492 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001493 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001494 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001495 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001496#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001497
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001498/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001499 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001500 */
1501
Allan Stephensc98da842016-11-11 15:45:03 -05001502/**
1503 * @cond INTERNAL_HIDDEN
1504 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001505
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001506struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001507 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001508 union {
1509 _wait_q_t wait_q;
1510
1511 _POLL_EVENT;
1512 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001513
1514 _OBJECT_TRACING_NEXT_PTR(k_queue);
1515};
1516
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001517#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001518 { \
1519 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1520 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001521 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001522 _OBJECT_TRACING_INIT \
1523 }
1524
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001525#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1526
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001527/**
1528 * INTERNAL_HIDDEN @endcond
1529 */
1530
1531/**
1532 * @defgroup queue_apis Queue APIs
1533 * @ingroup kernel_apis
1534 * @{
1535 */
1536
1537/**
1538 * @brief Initialize a queue.
1539 *
1540 * This routine initializes a queue object, prior to its first use.
1541 *
1542 * @param queue Address of the queue.
1543 *
1544 * @return N/A
1545 */
1546extern void k_queue_init(struct k_queue *queue);
1547
1548/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001549 * @brief Cancel waiting on a queue.
1550 *
1551 * This routine causes first thread pending on @a queue, if any, to
1552 * return from k_queue_get() call with NULL value (as if timeout expired).
1553 *
1554 * @note Can be called by ISRs.
1555 *
1556 * @param queue Address of the queue.
1557 *
1558 * @return N/A
1559 */
1560extern void k_queue_cancel_wait(struct k_queue *queue);
1561
1562/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001563 * @brief Append an element to the end of a queue.
1564 *
1565 * This routine appends a data item to @a queue. A queue data item must be
1566 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1567 * reserved for the kernel's use.
1568 *
1569 * @note Can be called by ISRs.
1570 *
1571 * @param queue Address of the queue.
1572 * @param data Address of the data item.
1573 *
1574 * @return N/A
1575 */
1576extern void k_queue_append(struct k_queue *queue, void *data);
1577
1578/**
1579 * @brief Prepend an element to a queue.
1580 *
1581 * This routine prepends a data item to @a queue. A queue data item must be
1582 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1583 * reserved for the kernel's use.
1584 *
1585 * @note Can be called by ISRs.
1586 *
1587 * @param queue Address of the queue.
1588 * @param data Address of the data item.
1589 *
1590 * @return N/A
1591 */
1592extern void k_queue_prepend(struct k_queue *queue, void *data);
1593
1594/**
1595 * @brief Inserts an element to a queue.
1596 *
1597 * This routine inserts a data item to @a queue after previous item. A queue
1598 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1599 * item are reserved for the kernel's use.
1600 *
1601 * @note Can be called by ISRs.
1602 *
1603 * @param queue Address of the queue.
1604 * @param prev Address of the previous data item.
1605 * @param data Address of the data item.
1606 *
1607 * @return N/A
1608 */
1609extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1610
1611/**
1612 * @brief Atomically append a list of elements to a queue.
1613 *
1614 * This routine adds a list of data items to @a queue in one operation.
1615 * The data items must be in a singly-linked list, with the first 32 bits
1616 * in each data item pointing to the next data item; the list must be
1617 * NULL-terminated.
1618 *
1619 * @note Can be called by ISRs.
1620 *
1621 * @param queue Address of the queue.
1622 * @param head Pointer to first node in singly-linked list.
1623 * @param tail Pointer to last node in singly-linked list.
1624 *
1625 * @return N/A
1626 */
1627extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1628
1629/**
1630 * @brief Atomically add a list of elements to a queue.
1631 *
1632 * This routine adds a list of data items to @a queue in one operation.
1633 * The data items must be in a singly-linked list implemented using a
1634 * sys_slist_t object. Upon completion, the original list is empty.
1635 *
1636 * @note Can be called by ISRs.
1637 *
1638 * @param queue Address of the queue.
1639 * @param list Pointer to sys_slist_t object.
1640 *
1641 * @return N/A
1642 */
1643extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1644
1645/**
1646 * @brief Get an element from a queue.
1647 *
1648 * This routine removes first data item from @a queue. The first 32 bits of the
1649 * data item are reserved for the kernel's use.
1650 *
1651 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1652 *
1653 * @param queue Address of the queue.
1654 * @param timeout Waiting period to obtain a data item (in milliseconds),
1655 * or one of the special values K_NO_WAIT and K_FOREVER.
1656 *
1657 * @return Address of the data item if successful; NULL if returned
1658 * without waiting, or waiting period timed out.
1659 */
Kumar Galacc334c72017-04-21 10:55:34 -05001660extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001661
1662/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001663 * @brief Remove an element from a queue.
1664 *
1665 * This routine removes data item from @a queue. The first 32 bits of the
1666 * data item are reserved for the kernel's use. Removing elements from k_queue
1667 * rely on sys_slist_find_and_remove which is not a constant time operation.
1668 *
1669 * @note Can be called by ISRs
1670 *
1671 * @param queue Address of the queue.
1672 * @param data Address of the data item.
1673 *
1674 * @return true if data item was removed
1675 */
1676static inline bool k_queue_remove(struct k_queue *queue, void *data)
1677{
1678 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1679}
1680
1681/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001682 * @brief Query a queue to see if it has data available.
1683 *
1684 * Note that the data might be already gone by the time this function returns
1685 * if other threads are also trying to read from the queue.
1686 *
1687 * @note Can be called by ISRs.
1688 *
1689 * @param queue Address of the queue.
1690 *
1691 * @return Non-zero if the queue is empty.
1692 * @return 0 if data is available.
1693 */
1694static inline int k_queue_is_empty(struct k_queue *queue)
1695{
1696 return (int)sys_slist_is_empty(&queue->data_q);
1697}
1698
1699/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001700 * @brief Peek element at the head of queue.
1701 *
1702 * Return element from the head of queue without removing it.
1703 *
1704 * @param queue Address of the queue.
1705 *
1706 * @return Head element, or NULL if queue is empty.
1707 */
1708static inline void *k_queue_peek_head(struct k_queue *queue)
1709{
1710 return sys_slist_peek_head(&queue->data_q);
1711}
1712
1713/**
1714 * @brief Peek element at the tail of queue.
1715 *
1716 * Return element from the tail of queue without removing it.
1717 *
1718 * @param queue Address of the queue.
1719 *
1720 * @return Tail element, or NULL if queue is empty.
1721 */
1722static inline void *k_queue_peek_tail(struct k_queue *queue)
1723{
1724 return sys_slist_peek_tail(&queue->data_q);
1725}
1726
1727/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001728 * @brief Statically define and initialize a queue.
1729 *
1730 * The queue can be accessed outside the module where it is defined using:
1731 *
1732 * @code extern struct k_queue <name>; @endcode
1733 *
1734 * @param name Name of the queue.
1735 */
1736#define K_QUEUE_DEFINE(name) \
1737 struct k_queue name \
1738 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001739 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001740
1741/**
1742 * @} end defgroup queue_apis
1743 */
1744
1745/**
1746 * @cond INTERNAL_HIDDEN
1747 */
1748
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001749struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001750 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001751};
1752
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001753#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001754 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001755 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001756 }
1757
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001758#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1759
Allan Stephensc98da842016-11-11 15:45:03 -05001760/**
1761 * INTERNAL_HIDDEN @endcond
1762 */
1763
1764/**
1765 * @defgroup fifo_apis Fifo APIs
1766 * @ingroup kernel_apis
1767 * @{
1768 */
1769
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001770/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001771 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001772 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001773 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001774 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001775 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001776 *
1777 * @return N/A
1778 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001779#define k_fifo_init(fifo) \
1780 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001781
1782/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001783 * @brief Cancel waiting on a fifo.
1784 *
1785 * This routine causes first thread pending on @a fifo, if any, to
1786 * return from k_fifo_get() call with NULL value (as if timeout
1787 * expired).
1788 *
1789 * @note Can be called by ISRs.
1790 *
1791 * @param fifo Address of the fifo.
1792 *
1793 * @return N/A
1794 */
1795#define k_fifo_cancel_wait(fifo) \
1796 k_queue_cancel_wait((struct k_queue *) fifo)
1797
1798/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001799 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001800 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001801 * This routine adds a data item to @a fifo. A fifo data item must be
1802 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1803 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001804 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001805 * @note Can be called by ISRs.
1806 *
1807 * @param fifo Address of the fifo.
1808 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001809 *
1810 * @return N/A
1811 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001812#define k_fifo_put(fifo, data) \
1813 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001814
1815/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001816 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001817 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001818 * This routine adds a list of data items to @a fifo in one operation.
1819 * The data items must be in a singly-linked list, with the first 32 bits
1820 * each data item pointing to the next data item; the list must be
1821 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001822 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001823 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001824 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001825 * @param fifo Address of the fifo.
1826 * @param head Pointer to first node in singly-linked list.
1827 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001828 *
1829 * @return N/A
1830 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001831#define k_fifo_put_list(fifo, head, tail) \
1832 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001833
1834/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001835 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001836 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001837 * This routine adds a list of data items to @a fifo in one operation.
1838 * The data items must be in a singly-linked list implemented using a
1839 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001840 * and must be re-initialized via sys_slist_init().
1841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001842 * @note Can be called by ISRs.
1843 *
1844 * @param fifo Address of the fifo.
1845 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001846 *
1847 * @return N/A
1848 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001849#define k_fifo_put_slist(fifo, list) \
1850 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001851
1852/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001853 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001855 * This routine removes a data item from @a fifo in a "first in, first out"
1856 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001858 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1859 *
1860 * @param fifo Address of the fifo.
1861 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001862 * or one of the special values K_NO_WAIT and K_FOREVER.
1863 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001864 * @return Address of the data item if successful; NULL if returned
1865 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001866 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001867#define k_fifo_get(fifo, timeout) \
1868 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001869
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001870/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001871 * @brief Query a fifo to see if it has data available.
1872 *
1873 * Note that the data might be already gone by the time this function returns
1874 * if other threads is also trying to read from the fifo.
1875 *
1876 * @note Can be called by ISRs.
1877 *
1878 * @param fifo Address of the fifo.
1879 *
1880 * @return Non-zero if the fifo is empty.
1881 * @return 0 if data is available.
1882 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001883#define k_fifo_is_empty(fifo) \
1884 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001885
1886/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001887 * @brief Peek element at the head of fifo.
1888 *
1889 * Return element from the head of fifo without removing it. A usecase
1890 * for this is if elements of the fifo are themselves containers. Then
1891 * on each iteration of processing, a head container will be peeked,
1892 * and some data processed out of it, and only if the container is empty,
1893 * it will be completely remove from the fifo.
1894 *
1895 * @param fifo Address of the fifo.
1896 *
1897 * @return Head element, or NULL if the fifo is empty.
1898 */
1899#define k_fifo_peek_head(fifo) \
1900 k_queue_peek_head((struct k_queue *) fifo)
1901
1902/**
1903 * @brief Peek element at the tail of fifo.
1904 *
1905 * Return element from the tail of fifo (without removing it). A usecase
1906 * for this is if elements of the fifo are themselves containers. Then
1907 * it may be useful to add more data to the last container in fifo.
1908 *
1909 * @param fifo Address of the fifo.
1910 *
1911 * @return Tail element, or NULL if fifo is empty.
1912 */
1913#define k_fifo_peek_tail(fifo) \
1914 k_queue_peek_tail((struct k_queue *) fifo)
1915
1916/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001917 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001918 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001919 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001920 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001921 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001922 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001923 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001924 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001925#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001926 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001927 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001928 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001929
Allan Stephensc98da842016-11-11 15:45:03 -05001930/**
1931 * @} end defgroup fifo_apis
1932 */
1933
1934/**
1935 * @cond INTERNAL_HIDDEN
1936 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001937
1938struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001939 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001940};
1941
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001942#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001943 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001944 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001945 }
1946
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001947#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1948
Allan Stephensc98da842016-11-11 15:45:03 -05001949/**
1950 * INTERNAL_HIDDEN @endcond
1951 */
1952
1953/**
1954 * @defgroup lifo_apis Lifo APIs
1955 * @ingroup kernel_apis
1956 * @{
1957 */
1958
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001959/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001960 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001961 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001962 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001963 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001964 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001965 *
1966 * @return N/A
1967 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001968#define k_lifo_init(lifo) \
1969 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001970
1971/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001972 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001973 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001974 * This routine adds a data item to @a lifo. A lifo data item must be
1975 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1976 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001977 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001978 * @note Can be called by ISRs.
1979 *
1980 * @param lifo Address of the lifo.
1981 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001982 *
1983 * @return N/A
1984 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001985#define k_lifo_put(lifo, data) \
1986 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001987
1988/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001989 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001990 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001991 * This routine removes a data item from @a lifo in a "last in, first out"
1992 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001993 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001994 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1995 *
1996 * @param lifo Address of the lifo.
1997 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001998 * or one of the special values K_NO_WAIT and K_FOREVER.
1999 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002000 * @return Address of the data item if successful; NULL if returned
2001 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002002 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002003#define k_lifo_get(lifo, timeout) \
2004 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002005
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002006/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002007 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002008 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002009 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002010 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002011 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002012 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002013 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002014 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002015#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002016 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002017 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002018 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002019
Allan Stephensc98da842016-11-11 15:45:03 -05002020/**
2021 * @} end defgroup lifo_apis
2022 */
2023
2024/**
2025 * @cond INTERNAL_HIDDEN
2026 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002027
2028struct k_stack {
2029 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002030 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002031
Anas Nashif2f203c22016-12-18 06:57:45 -05002032 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002033};
2034
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002035#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002036 { \
2037 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2038 .base = stack_buffer, \
2039 .next = stack_buffer, \
2040 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002041 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002042 }
2043
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002044#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2045
Allan Stephensc98da842016-11-11 15:45:03 -05002046/**
2047 * INTERNAL_HIDDEN @endcond
2048 */
2049
2050/**
2051 * @defgroup stack_apis Stack APIs
2052 * @ingroup kernel_apis
2053 * @{
2054 */
2055
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002056/**
2057 * @brief Initialize a stack.
2058 *
2059 * This routine initializes a stack object, prior to its first use.
2060 *
2061 * @param stack Address of the stack.
2062 * @param buffer Address of array used to hold stacked values.
2063 * @param num_entries Maximum number of values that can be stacked.
2064 *
2065 * @return N/A
2066 */
Andrew Boiee8734462017-09-29 16:42:07 -07002067__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002068 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002069
2070/**
2071 * @brief Push an element onto a stack.
2072 *
2073 * This routine adds a 32-bit value @a data to @a stack.
2074 *
2075 * @note Can be called by ISRs.
2076 *
2077 * @param stack Address of the stack.
2078 * @param data Value to push onto the stack.
2079 *
2080 * @return N/A
2081 */
Andrew Boiee8734462017-09-29 16:42:07 -07002082__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002083
2084/**
2085 * @brief Pop an element from a stack.
2086 *
2087 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2088 * manner and stores the value in @a data.
2089 *
2090 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2091 *
2092 * @param stack Address of the stack.
2093 * @param data Address of area to hold the value popped from the stack.
2094 * @param timeout Waiting period to obtain a value (in milliseconds),
2095 * or one of the special values K_NO_WAIT and K_FOREVER.
2096 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002097 * @retval 0 Element popped from stack.
2098 * @retval -EBUSY Returned without waiting.
2099 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002100 */
Andrew Boiee8734462017-09-29 16:42:07 -07002101__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002102
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002103/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002104 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002105 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002106 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002107 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002108 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002109 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002110 * @param name Name of the stack.
2111 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002112 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002113#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002114 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002115 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002116 struct k_stack name \
2117 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002118 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002119 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002120
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002121/**
Allan Stephensc98da842016-11-11 15:45:03 -05002122 * @} end defgroup stack_apis
2123 */
2124
Allan Stephens6bba9b02016-11-16 14:56:54 -05002125struct k_work;
2126
Allan Stephensc98da842016-11-11 15:45:03 -05002127/**
2128 * @defgroup workqueue_apis Workqueue Thread APIs
2129 * @ingroup kernel_apis
2130 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002131 */
2132
Allan Stephens6bba9b02016-11-16 14:56:54 -05002133/**
2134 * @typedef k_work_handler_t
2135 * @brief Work item handler function type.
2136 *
2137 * A work item's handler function is executed by a workqueue's thread
2138 * when the work item is processed by the workqueue.
2139 *
2140 * @param work Address of the work item.
2141 *
2142 * @return N/A
2143 */
2144typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002145
2146/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002147 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002148 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002149
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002150struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002151 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002152 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002153};
2154
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002155enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002156 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002157};
2158
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002159struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002160 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002161 k_work_handler_t handler;
2162 atomic_t flags[1];
2163};
2164
Allan Stephens6bba9b02016-11-16 14:56:54 -05002165struct k_delayed_work {
2166 struct k_work work;
2167 struct _timeout timeout;
2168 struct k_work_q *work_q;
2169};
2170
2171extern struct k_work_q k_sys_work_q;
2172
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002173/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002174 * INTERNAL_HIDDEN @endcond
2175 */
2176
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002177#define _K_WORK_INITIALIZER(work_handler) \
2178 { \
2179 ._reserved = NULL, \
2180 .handler = work_handler, \
2181 .flags = { 0 } \
2182 }
2183
2184#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2185
Allan Stephens6bba9b02016-11-16 14:56:54 -05002186/**
2187 * @brief Initialize a statically-defined work item.
2188 *
2189 * This macro can be used to initialize a statically-defined workqueue work
2190 * item, prior to its first use. For example,
2191 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002192 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002193 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002194 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002195 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002196 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002197#define K_WORK_DEFINE(work, work_handler) \
2198 struct k_work work \
2199 __in_section(_k_work, static, work) = \
2200 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002201
2202/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002203 * @brief Initialize a work item.
2204 *
2205 * This routine initializes a workqueue work item, prior to its first use.
2206 *
2207 * @param work Address of work item.
2208 * @param handler Function to invoke each time work item is processed.
2209 *
2210 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002211 */
2212static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2213{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002214 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002215 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002216 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002217}
2218
2219/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002220 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002221 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002222 * This routine submits work item @a work to be processed by workqueue
2223 * @a work_q. If the work item is already pending in the workqueue's queue
2224 * as a result of an earlier submission, this routine has no effect on the
2225 * work item. If the work item has already been processed, or is currently
2226 * being processed, its work is considered complete and the work item can be
2227 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002228 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002229 * @warning
2230 * A submitted work item must not be modified until it has been processed
2231 * by the workqueue.
2232 *
2233 * @note Can be called by ISRs.
2234 *
2235 * @param work_q Address of workqueue.
2236 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002237 *
2238 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002239 */
2240static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2241 struct k_work *work)
2242{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002243 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002244 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002245 }
2246}
2247
2248/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002249 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002250 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002251 * This routine indicates if work item @a work is pending in a workqueue's
2252 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002253 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002254 * @note Can be called by ISRs.
2255 *
2256 * @param work Address of work item.
2257 *
2258 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002259 */
2260static inline int k_work_pending(struct k_work *work)
2261{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002262 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002263}
2264
2265/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002266 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002267 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002268 * This routine starts workqueue @a work_q. The workqueue spawns its work
2269 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002270 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002271 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002272 * @param stack Pointer to work queue thread's stack space, as defined by
2273 * K_THREAD_STACK_DEFINE()
2274 * @param stack_size Size of the work queue thread's stack (in bytes), which
2275 * should either be the same constant passed to
2276 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002277 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002278 *
2279 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002280 */
Andrew Boie507852a2017-07-25 18:47:07 -07002281extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002282 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002283 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002284
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002285/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002286 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002287 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002288 * This routine initializes a workqueue delayed work item, prior to
2289 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002290 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002291 * @param work Address of delayed work item.
2292 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002293 *
2294 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002295 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002296extern void k_delayed_work_init(struct k_delayed_work *work,
2297 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002298
2299/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002300 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002301 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002302 * This routine schedules work item @a work to be processed by workqueue
2303 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002304 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002305 * Only when the countdown completes is the work item actually submitted to
2306 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002307 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002308 * Submitting a previously submitted delayed work item that is still
2309 * counting down cancels the existing submission and restarts the countdown
2310 * using the new delay. If the work item is currently pending on the
2311 * workqueue's queue because the countdown has completed it is too late to
2312 * resubmit the item, and resubmission fails without impacting the work item.
2313 * If the work item has already been processed, or is currently being processed,
2314 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002315 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002316 * @warning
2317 * A delayed work item must not be modified until it has been processed
2318 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002319 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002320 * @note Can be called by ISRs.
2321 *
2322 * @param work_q Address of workqueue.
2323 * @param work Address of delayed work item.
2324 * @param delay Delay before submitting the work item (in milliseconds).
2325 *
2326 * @retval 0 Work item countdown started.
2327 * @retval -EINPROGRESS Work item is already pending.
2328 * @retval -EINVAL Work item is being processed or has completed its work.
2329 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002330 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002331extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2332 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002333 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002334
2335/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002336 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002337 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002338 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002339 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002340 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002341 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002342 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002343 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002344 * @param work Address of delayed work item.
2345 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002346 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002347 * @retval -EINPROGRESS Work item is already pending.
2348 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002349 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002350extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002351
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002352/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002353 * @brief Submit a work item to the system workqueue.
2354 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002355 * This routine submits work item @a work to be processed by the system
2356 * workqueue. If the work item is already pending in the workqueue's queue
2357 * as a result of an earlier submission, this routine has no effect on the
2358 * work item. If the work item has already been processed, or is currently
2359 * being processed, its work is considered complete and the work item can be
2360 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002361 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002362 * @warning
2363 * Work items submitted to the system workqueue should avoid using handlers
2364 * that block or yield since this may prevent the system workqueue from
2365 * processing other work items in a timely manner.
2366 *
2367 * @note Can be called by ISRs.
2368 *
2369 * @param work Address of work item.
2370 *
2371 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002372 */
2373static inline void k_work_submit(struct k_work *work)
2374{
2375 k_work_submit_to_queue(&k_sys_work_q, work);
2376}
2377
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002378/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002379 * @brief Submit a delayed work item to the system workqueue.
2380 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002381 * This routine schedules work item @a work to be processed by the system
2382 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002383 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002384 * Only when the countdown completes is the work item actually submitted to
2385 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002386 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002387 * Submitting a previously submitted delayed work item that is still
2388 * counting down cancels the existing submission and restarts the countdown
2389 * using the new delay. If the work item is currently pending on the
2390 * workqueue's queue because the countdown has completed it is too late to
2391 * resubmit the item, and resubmission fails without impacting the work item.
2392 * If the work item has already been processed, or is currently being processed,
2393 * its work is considered complete and the work item can be resubmitted.
2394 *
2395 * @warning
2396 * Work items submitted to the system workqueue should avoid using handlers
2397 * that block or yield since this may prevent the system workqueue from
2398 * processing other work items in a timely manner.
2399 *
2400 * @note Can be called by ISRs.
2401 *
2402 * @param work Address of delayed work item.
2403 * @param delay Delay before submitting the work item (in milliseconds).
2404 *
2405 * @retval 0 Work item countdown started.
2406 * @retval -EINPROGRESS Work item is already pending.
2407 * @retval -EINVAL Work item is being processed or has completed its work.
2408 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002409 */
2410static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002411 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002412{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002413 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002414}
2415
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002416/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002417 * @brief Get time remaining before a delayed work gets scheduled.
2418 *
2419 * This routine computes the (approximate) time remaining before a
2420 * delayed work gets executed. If the delayed work is not waiting to be
Paul Sokolovskye25df542017-12-28 15:40:21 +02002421 * scheduled, it returns zero.
Johan Hedbergc8201b22016-12-09 10:42:22 +02002422 *
2423 * @param work Delayed work item.
2424 *
2425 * @return Remaining time (in milliseconds).
2426 */
Kumar Galacc334c72017-04-21 10:55:34 -05002427static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002428{
2429 return _timeout_remaining_get(&work->timeout);
2430}
2431
2432/**
Allan Stephensc98da842016-11-11 15:45:03 -05002433 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002434 */
2435
Allan Stephensc98da842016-11-11 15:45:03 -05002436/**
2437 * @cond INTERNAL_HIDDEN
2438 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002439
2440struct k_mutex {
2441 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002442 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002443 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002444 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002445
Anas Nashif2f203c22016-12-18 06:57:45 -05002446 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002447};
2448
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002449#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002450 { \
2451 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2452 .owner = NULL, \
2453 .lock_count = 0, \
2454 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002455 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002456 }
2457
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002458#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2459
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002460/**
Allan Stephensc98da842016-11-11 15:45:03 -05002461 * INTERNAL_HIDDEN @endcond
2462 */
2463
2464/**
2465 * @defgroup mutex_apis Mutex APIs
2466 * @ingroup kernel_apis
2467 * @{
2468 */
2469
2470/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002471 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002472 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002473 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002474 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002475 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002476 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002477 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002478 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002479#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002480 struct k_mutex name \
2481 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002482 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002483
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002484/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002485 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002486 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002487 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002489 * Upon completion, the mutex is available and does not have an owner.
2490 *
2491 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002492 *
2493 * @return N/A
2494 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002495__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002496
2497/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002498 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002499 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002500 * This routine locks @a mutex. If the mutex is locked by another thread,
2501 * the calling thread waits until the mutex becomes available or until
2502 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002503 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002504 * A thread is permitted to lock a mutex it has already locked. The operation
2505 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002506 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002507 * @param mutex Address of the mutex.
2508 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002509 * or one of the special values K_NO_WAIT and K_FOREVER.
2510 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002511 * @retval 0 Mutex locked.
2512 * @retval -EBUSY Returned without waiting.
2513 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002514 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002515__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002516
2517/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002518 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002519 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002520 * This routine unlocks @a mutex. The mutex must already be locked by the
2521 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002522 *
2523 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002524 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002525 * thread.
2526 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002527 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002528 *
2529 * @return N/A
2530 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002531__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002532
Allan Stephensc98da842016-11-11 15:45:03 -05002533/**
2534 * @} end defgroup mutex_apis
2535 */
2536
2537/**
2538 * @cond INTERNAL_HIDDEN
2539 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002540
2541struct k_sem {
2542 _wait_q_t wait_q;
2543 unsigned int count;
2544 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002545 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002546
Anas Nashif2f203c22016-12-18 06:57:45 -05002547 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002548};
2549
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002550#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002551 { \
2552 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2553 .count = initial_count, \
2554 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002555 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002556 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002557 }
2558
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002559#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2560
Allan Stephensc98da842016-11-11 15:45:03 -05002561/**
2562 * INTERNAL_HIDDEN @endcond
2563 */
2564
2565/**
2566 * @defgroup semaphore_apis Semaphore APIs
2567 * @ingroup kernel_apis
2568 * @{
2569 */
2570
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002571/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002572 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002573 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002574 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002575 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002576 * @param sem Address of the semaphore.
2577 * @param initial_count Initial semaphore count.
2578 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002579 *
2580 * @return N/A
2581 */
Andrew Boie99280232017-09-29 14:17:47 -07002582__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2583 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002584
2585/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002586 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002587 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002588 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002589 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002590 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2591 *
2592 * @param sem Address of the semaphore.
2593 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002594 * or one of the special values K_NO_WAIT and K_FOREVER.
2595 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002596 * @note When porting code from the nanokernel legacy API to the new API, be
2597 * careful with the return value of this function. The return value is the
2598 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2599 * non-zero means failure, while the nano_sem_take family returns 1 for success
2600 * and 0 for failure.
2601 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002602 * @retval 0 Semaphore taken.
2603 * @retval -EBUSY Returned without waiting.
2604 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002605 */
Andrew Boie99280232017-09-29 14:17:47 -07002606__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002607
2608/**
2609 * @brief Give a semaphore.
2610 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002611 * This routine gives @a sem, unless the semaphore is already at its maximum
2612 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002613 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002614 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002615 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002616 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002617 *
2618 * @return N/A
2619 */
Andrew Boie99280232017-09-29 14:17:47 -07002620__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002621
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002622/**
2623 * @brief Reset a semaphore's count to zero.
2624 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002625 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002626 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002627 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002628 *
2629 * @return N/A
2630 */
Andrew Boie990bf162017-10-03 12:36:49 -07002631__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002632
2633static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002634{
2635 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002636}
2637
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002638/**
2639 * @brief Get a semaphore's count.
2640 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002641 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002642 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002643 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002644 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002645 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002646 */
Andrew Boie990bf162017-10-03 12:36:49 -07002647__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002648
2649static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002650{
2651 return sem->count;
2652}
2653
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002654/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002655 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002656 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002657 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002658 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002659 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002660 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002661 * @param name Name of the semaphore.
2662 * @param initial_count Initial semaphore count.
2663 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002664 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002665#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002666 struct k_sem name \
2667 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002668 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002669
Allan Stephensc98da842016-11-11 15:45:03 -05002670/**
2671 * @} end defgroup semaphore_apis
2672 */
2673
2674/**
2675 * @defgroup alert_apis Alert APIs
2676 * @ingroup kernel_apis
2677 * @{
2678 */
2679
Allan Stephens5eceb852016-11-16 10:16:30 -05002680/**
2681 * @typedef k_alert_handler_t
2682 * @brief Alert handler function type.
2683 *
2684 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002685 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002686 * and is only invoked if the alert has been initialized with one.
2687 *
2688 * @param alert Address of the alert.
2689 *
2690 * @return 0 if alert has been consumed; non-zero if alert should pend.
2691 */
2692typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002693
2694/**
2695 * @} end defgroup alert_apis
2696 */
2697
2698/**
2699 * @cond INTERNAL_HIDDEN
2700 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002701
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002702#define K_ALERT_DEFAULT NULL
2703#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002704
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002705struct k_alert {
2706 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002707 atomic_t send_count;
2708 struct k_work work_item;
2709 struct k_sem sem;
2710
Anas Nashif2f203c22016-12-18 06:57:45 -05002711 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002712};
2713
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002714extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002715
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002716#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002717 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002718 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002719 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002720 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2721 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002722 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002723 }
2724
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002725#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2726
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002727/**
Allan Stephensc98da842016-11-11 15:45:03 -05002728 * INTERNAL_HIDDEN @endcond
2729 */
2730
2731/**
2732 * @addtogroup alert_apis
2733 * @{
2734 */
2735
2736/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002737 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002738 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002739 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002740 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002741 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002742 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002743 * @param name Name of the alert.
2744 * @param alert_handler Action to take when alert is sent. Specify either
2745 * the address of a function to be invoked by the system workqueue
2746 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2747 * K_ALERT_DEFAULT (which causes the alert to pend).
2748 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002749 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002750#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002751 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002752 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002753 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002754 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002755
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002756/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002757 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002758 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002759 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002760 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002761 * @param alert Address of the alert.
2762 * @param handler Action to take when alert is sent. Specify either the address
2763 * of a function to be invoked by the system workqueue thread,
2764 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2765 * K_ALERT_DEFAULT (which causes the alert to pend).
2766 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002767 *
2768 * @return N/A
2769 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002770extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2771 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002772
2773/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002774 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002775 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002776 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002777 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002778 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2779 *
2780 * @param alert Address of the alert.
2781 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002782 * or one of the special values K_NO_WAIT and K_FOREVER.
2783 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002784 * @retval 0 Alert received.
2785 * @retval -EBUSY Returned without waiting.
2786 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002787 */
Andrew Boie310e9872017-09-29 04:41:15 -07002788__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002789
2790/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002791 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002792 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002793 * This routine signals @a alert. The action specified for @a alert will
2794 * be taken, which may trigger the execution of an alert handler function
2795 * and/or cause the alert to pend (assuming the alert has not reached its
2796 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002797 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002798 * @note Can be called by ISRs.
2799 *
2800 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002801 *
2802 * @return N/A
2803 */
Andrew Boie310e9872017-09-29 04:41:15 -07002804__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002805
2806/**
Allan Stephensc98da842016-11-11 15:45:03 -05002807 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002808 */
2809
Allan Stephensc98da842016-11-11 15:45:03 -05002810/**
2811 * @cond INTERNAL_HIDDEN
2812 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002813
2814struct k_msgq {
2815 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002816 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002817 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002818 char *buffer_start;
2819 char *buffer_end;
2820 char *read_ptr;
2821 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002822 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002823
Anas Nashif2f203c22016-12-18 06:57:45 -05002824 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002825};
2826
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002827#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002828 { \
2829 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002830 .max_msgs = q_max_msgs, \
2831 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002832 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002833 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002834 .read_ptr = q_buffer, \
2835 .write_ptr = q_buffer, \
2836 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002837 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002838 }
2839
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002840#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2841
Peter Mitsis1da807e2016-10-06 11:36:59 -04002842/**
Allan Stephensc98da842016-11-11 15:45:03 -05002843 * INTERNAL_HIDDEN @endcond
2844 */
2845
2846/**
2847 * @defgroup msgq_apis Message Queue APIs
2848 * @ingroup kernel_apis
2849 * @{
2850 */
2851
2852/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002853 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002855 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2856 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002857 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2858 * message is similarly aligned to this boundary, @a q_msg_size must also be
2859 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002861 * The message queue can be accessed outside the module where it is defined
2862 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002863 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002864 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002865 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002866 * @param q_name Name of the message queue.
2867 * @param q_msg_size Message size (in bytes).
2868 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002869 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002870 */
2871#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2872 static char __noinit __aligned(q_align) \
2873 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002874 struct k_msgq q_name \
2875 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002876 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002877 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002878
Peter Mitsisd7a37502016-10-13 11:37:40 -04002879/**
2880 * @brief Initialize a message queue.
2881 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002882 * This routine initializes a message queue object, prior to its first use.
2883 *
Allan Stephensda827222016-11-09 14:23:58 -06002884 * The message queue's ring buffer must contain space for @a max_msgs messages,
2885 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2886 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2887 * that each message is similarly aligned to this boundary, @a q_msg_size
2888 * must also be a multiple of N.
2889 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002890 * @param q Address of the message queue.
2891 * @param buffer Pointer to ring buffer that holds queued messages.
2892 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002893 * @param max_msgs Maximum number of messages that can be queued.
2894 *
2895 * @return N/A
2896 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002897__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2898 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002899
2900/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002901 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002902 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002903 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002904 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002905 * @note Can be called by ISRs.
2906 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002907 * @param q Address of the message queue.
2908 * @param data Pointer to the message.
2909 * @param timeout Waiting period to add the message (in milliseconds),
2910 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002911 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002912 * @retval 0 Message sent.
2913 * @retval -ENOMSG Returned without waiting or queue purged.
2914 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002915 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002916__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002917
2918/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002919 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002920 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002921 * This routine receives a message from message queue @a q in a "first in,
2922 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002923 *
Allan Stephensc98da842016-11-11 15:45:03 -05002924 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002925 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002926 * @param q Address of the message queue.
2927 * @param data Address of area to hold the received message.
2928 * @param timeout Waiting period to receive the message (in milliseconds),
2929 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002930 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002931 * @retval 0 Message received.
2932 * @retval -ENOMSG Returned without waiting.
2933 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002934 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002935__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002936
2937/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002938 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002939 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002940 * This routine discards all unreceived messages in a message queue's ring
2941 * buffer. Any threads that are blocked waiting to send a message to the
2942 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002943 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002944 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002945 *
2946 * @return N/A
2947 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002948__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002949
Peter Mitsis67be2492016-10-07 11:44:34 -04002950/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002951 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002952 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002953 * This routine returns the number of unused entries in a message queue's
2954 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002956 * @param q Address of the message queue.
2957 *
2958 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002959 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002960__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
2961
2962static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002963{
2964 return q->max_msgs - q->used_msgs;
2965}
2966
Peter Mitsisd7a37502016-10-13 11:37:40 -04002967/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002968 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002969 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002970 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002972 * @param q Address of the message queue.
2973 *
2974 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002975 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002976__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
2977
2978static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002979{
2980 return q->used_msgs;
2981}
2982
Allan Stephensc98da842016-11-11 15:45:03 -05002983/**
2984 * @} end defgroup msgq_apis
2985 */
2986
2987/**
2988 * @defgroup mem_pool_apis Memory Pool APIs
2989 * @ingroup kernel_apis
2990 * @{
2991 */
2992
Andy Ross73cb9582017-05-09 10:42:39 -07002993/* Note on sizing: the use of a 20 bit field for block means that,
2994 * assuming a reasonable minimum block size of 16 bytes, we're limited
2995 * to 16M of memory managed by a single pool. Long term it would be
2996 * good to move to a variable bit size based on configuration.
2997 */
2998struct k_mem_block_id {
2999 u32_t pool : 8;
3000 u32_t level : 4;
3001 u32_t block : 20;
3002};
3003
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003004struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003005 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003006 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003007};
3008
Allan Stephensc98da842016-11-11 15:45:03 -05003009/**
3010 * @} end defgroup mem_pool_apis
3011 */
3012
3013/**
3014 * @defgroup mailbox_apis Mailbox APIs
3015 * @ingroup kernel_apis
3016 * @{
3017 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003018
3019struct k_mbox_msg {
3020 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003021 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003022 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003023 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003024 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003025 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003026 /** sender's message data buffer */
3027 void *tx_data;
3028 /** internal use only - needed for legacy API support */
3029 void *_rx_data;
3030 /** message data block descriptor */
3031 struct k_mem_block tx_block;
3032 /** source thread id */
3033 k_tid_t rx_source_thread;
3034 /** target thread id */
3035 k_tid_t tx_target_thread;
3036 /** internal use only - thread waiting on send (may be a dummy) */
3037 k_tid_t _syncing_thread;
3038#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3039 /** internal use only - semaphore used during asynchronous send */
3040 struct k_sem *_async_sem;
3041#endif
3042};
3043
Allan Stephensc98da842016-11-11 15:45:03 -05003044/**
3045 * @cond INTERNAL_HIDDEN
3046 */
3047
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003048struct k_mbox {
3049 _wait_q_t tx_msg_queue;
3050 _wait_q_t rx_msg_queue;
3051
Anas Nashif2f203c22016-12-18 06:57:45 -05003052 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003053};
3054
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003055#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003056 { \
3057 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3058 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003059 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003060 }
3061
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003062#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3063
Peter Mitsis12092702016-10-14 12:57:23 -04003064/**
Allan Stephensc98da842016-11-11 15:45:03 -05003065 * INTERNAL_HIDDEN @endcond
3066 */
3067
3068/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003069 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003070 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003071 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003072 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003073 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003074 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003075 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003076 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003077#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003078 struct k_mbox name \
3079 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003080 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003081
Peter Mitsis12092702016-10-14 12:57:23 -04003082/**
3083 * @brief Initialize a mailbox.
3084 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003085 * This routine initializes a mailbox object, prior to its first use.
3086 *
3087 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003088 *
3089 * @return N/A
3090 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003091extern void k_mbox_init(struct k_mbox *mbox);
3092
Peter Mitsis12092702016-10-14 12:57:23 -04003093/**
3094 * @brief Send a mailbox message in a synchronous manner.
3095 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003096 * This routine sends a message to @a mbox and waits for a receiver to both
3097 * receive and process it. The message data may be in a buffer, in a memory
3098 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003099 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003100 * @param mbox Address of the mailbox.
3101 * @param tx_msg Address of the transmit message descriptor.
3102 * @param timeout Waiting period for the message to be received (in
3103 * milliseconds), or one of the special values K_NO_WAIT
3104 * and K_FOREVER. Once the message has been received,
3105 * this routine waits as long as necessary for the message
3106 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003107 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003108 * @retval 0 Message sent.
3109 * @retval -ENOMSG Returned without waiting.
3110 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003111 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003112extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003113 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003114
Peter Mitsis12092702016-10-14 12:57:23 -04003115/**
3116 * @brief Send a mailbox message in an asynchronous manner.
3117 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003118 * This routine sends a message to @a mbox without waiting for a receiver
3119 * to process it. The message data may be in a buffer, in a memory pool block,
3120 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3121 * will be given when the message has been both received and completely
3122 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003123 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003124 * @param mbox Address of the mailbox.
3125 * @param tx_msg Address of the transmit message descriptor.
3126 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003127 *
3128 * @return N/A
3129 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003130extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003131 struct k_sem *sem);
3132
Peter Mitsis12092702016-10-14 12:57:23 -04003133/**
3134 * @brief Receive a mailbox message.
3135 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003136 * This routine receives a message from @a mbox, then optionally retrieves
3137 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003138 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003139 * @param mbox Address of the mailbox.
3140 * @param rx_msg Address of the receive message descriptor.
3141 * @param buffer Address of the buffer to receive data, or NULL to defer data
3142 * retrieval and message disposal until later.
3143 * @param timeout Waiting period for a message to be received (in
3144 * milliseconds), or one of the special values K_NO_WAIT
3145 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003146 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003147 * @retval 0 Message received.
3148 * @retval -ENOMSG Returned without waiting.
3149 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003150 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003151extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003152 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003153
3154/**
3155 * @brief Retrieve mailbox message data into a buffer.
3156 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003157 * This routine completes the processing of a received message by retrieving
3158 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003159 *
3160 * Alternatively, this routine can be used to dispose of a received message
3161 * without retrieving its data.
3162 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003163 * @param rx_msg Address of the receive message descriptor.
3164 * @param buffer Address of the buffer to receive data, or NULL to discard
3165 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003166 *
3167 * @return N/A
3168 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003169extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003170
3171/**
3172 * @brief Retrieve mailbox message data into a memory pool block.
3173 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003174 * This routine completes the processing of a received message by retrieving
3175 * its data into a memory pool block, then disposing of the message.
3176 * The memory pool block that results from successful retrieval must be
3177 * returned to the pool once the data has been processed, even in cases
3178 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003179 *
3180 * Alternatively, this routine can be used to dispose of a received message
3181 * without retrieving its data. In this case there is no need to return a
3182 * memory pool block to the pool.
3183 *
3184 * This routine allocates a new memory pool block for the data only if the
3185 * data is not already in one. If a new block cannot be allocated, the routine
3186 * returns a failure code and the received message is left unchanged. This
3187 * permits the caller to reattempt data retrieval at a later time or to dispose
3188 * of the received message without retrieving its data.
3189 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003190 * @param rx_msg Address of a receive message descriptor.
3191 * @param pool Address of memory pool, or NULL to discard data.
3192 * @param block Address of the area to hold memory pool block info.
3193 * @param timeout Waiting period to wait for a memory pool block (in
3194 * milliseconds), or one of the special values K_NO_WAIT
3195 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003196 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003197 * @retval 0 Data retrieved.
3198 * @retval -ENOMEM Returned without waiting.
3199 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003200 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003201extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003202 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003203 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003204
Allan Stephensc98da842016-11-11 15:45:03 -05003205/**
3206 * @} end defgroup mailbox_apis
3207 */
3208
3209/**
3210 * @cond INTERNAL_HIDDEN
3211 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003212
3213struct k_pipe {
3214 unsigned char *buffer; /* Pipe buffer: may be NULL */
3215 size_t size; /* Buffer size */
3216 size_t bytes_used; /* # bytes used in buffer */
3217 size_t read_index; /* Where in buffer to read from */
3218 size_t write_index; /* Where in buffer to write */
3219
3220 struct {
3221 _wait_q_t readers; /* Reader wait queue */
3222 _wait_q_t writers; /* Writer wait queue */
3223 } wait_q;
3224
Anas Nashif2f203c22016-12-18 06:57:45 -05003225 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003226};
3227
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003228#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003229 { \
3230 .buffer = pipe_buffer, \
3231 .size = pipe_buffer_size, \
3232 .bytes_used = 0, \
3233 .read_index = 0, \
3234 .write_index = 0, \
3235 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3236 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003237 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003238 }
3239
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003240#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3241
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003242/**
Allan Stephensc98da842016-11-11 15:45:03 -05003243 * INTERNAL_HIDDEN @endcond
3244 */
3245
3246/**
3247 * @defgroup pipe_apis Pipe APIs
3248 * @ingroup kernel_apis
3249 * @{
3250 */
3251
3252/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003253 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003254 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003255 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003256 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003257 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003258 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003259 * @param name Name of the pipe.
3260 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3261 * or zero if no ring buffer is used.
3262 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003263 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003264#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3265 static unsigned char __noinit __aligned(pipe_align) \
3266 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003267 struct k_pipe name \
3268 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003269 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003270
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003271/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003272 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003273 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003274 * This routine initializes a pipe object, prior to its first use.
3275 *
3276 * @param pipe Address of the pipe.
3277 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3278 * is used.
3279 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3280 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003281 *
3282 * @return N/A
3283 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003284__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3285 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003286
3287/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003288 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003289 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003290 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003291 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003292 * @param pipe Address of the pipe.
3293 * @param data Address of data to write.
3294 * @param bytes_to_write Size of data (in bytes).
3295 * @param bytes_written Address of area to hold the number of bytes written.
3296 * @param min_xfer Minimum number of bytes to write.
3297 * @param timeout Waiting period to wait for the data to be written (in
3298 * milliseconds), or one of the special values K_NO_WAIT
3299 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003300 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003301 * @retval 0 At least @a min_xfer bytes of data were written.
3302 * @retval -EIO Returned without waiting; zero data bytes were written.
3303 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003304 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003305 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003306__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3307 size_t bytes_to_write, size_t *bytes_written,
3308 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003309
3310/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003311 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003312 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003313 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003314 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003315 * @param pipe Address of the pipe.
3316 * @param data Address to place the data read from pipe.
3317 * @param bytes_to_read Maximum number of data bytes to read.
3318 * @param bytes_read Address of area to hold the number of bytes read.
3319 * @param min_xfer Minimum number of data bytes to read.
3320 * @param timeout Waiting period to wait for the data to be read (in
3321 * milliseconds), or one of the special values K_NO_WAIT
3322 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003323 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003324 * @retval 0 At least @a min_xfer bytes of data were read.
3325 * @retval -EIO Returned without waiting; zero data bytes were read.
3326 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003327 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003328 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003329__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3330 size_t bytes_to_read, size_t *bytes_read,
3331 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003332
3333/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003334 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003335 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003336 * This routine writes the data contained in a memory block to @a pipe.
3337 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003338 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003339 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003340 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003341 * @param block Memory block containing data to send
3342 * @param size Number of data bytes in memory block to send
3343 * @param sem Semaphore to signal upon completion (else NULL)
3344 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003345 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003346 */
3347extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3348 size_t size, struct k_sem *sem);
3349
3350/**
Allan Stephensc98da842016-11-11 15:45:03 -05003351 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003352 */
3353
Allan Stephensc98da842016-11-11 15:45:03 -05003354/**
3355 * @cond INTERNAL_HIDDEN
3356 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003357
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003358struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003359 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003360 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003361 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003362 char *buffer;
3363 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003364 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003365
Anas Nashif2f203c22016-12-18 06:57:45 -05003366 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003367};
3368
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003369#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003370 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003371 { \
3372 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003373 .num_blocks = slab_num_blocks, \
3374 .block_size = slab_block_size, \
3375 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003376 .free_list = NULL, \
3377 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003378 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003379 }
3380
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003381#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3382
3383
Peter Mitsis578f9112016-10-07 13:50:31 -04003384/**
Allan Stephensc98da842016-11-11 15:45:03 -05003385 * INTERNAL_HIDDEN @endcond
3386 */
3387
3388/**
3389 * @defgroup mem_slab_apis Memory Slab APIs
3390 * @ingroup kernel_apis
3391 * @{
3392 */
3393
3394/**
Allan Stephensda827222016-11-09 14:23:58 -06003395 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003396 *
Allan Stephensda827222016-11-09 14:23:58 -06003397 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003398 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003399 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3400 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003401 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003402 *
Allan Stephensda827222016-11-09 14:23:58 -06003403 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003404 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003405 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003406 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003407 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003408 * @param name Name of the memory slab.
3409 * @param slab_block_size Size of each memory block (in bytes).
3410 * @param slab_num_blocks Number memory blocks.
3411 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003412 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003413#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3414 char __noinit __aligned(slab_align) \
3415 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3416 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003417 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003418 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003419 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003420
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003421/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003422 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003423 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003424 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003425 *
Allan Stephensda827222016-11-09 14:23:58 -06003426 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3427 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3428 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3429 * To ensure that each memory block is similarly aligned to this boundary,
3430 * @a slab_block_size must also be a multiple of N.
3431 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003432 * @param slab Address of the memory slab.
3433 * @param buffer Pointer to buffer used for the memory blocks.
3434 * @param block_size Size of each memory block (in bytes).
3435 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003436 *
3437 * @return N/A
3438 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003439extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003440 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003441
3442/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003443 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003444 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003445 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003446 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003447 * @param slab Address of the memory slab.
3448 * @param mem Pointer to block address area.
3449 * @param timeout Maximum time to wait for operation to complete
3450 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3451 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003452 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003453 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003454 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003455 * @retval -ENOMEM Returned without waiting.
3456 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003457 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003458extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003459 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003460
3461/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003462 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003463 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003464 * This routine releases a previously allocated memory block back to its
3465 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003466 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003467 * @param slab Address of the memory slab.
3468 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003469 *
3470 * @return N/A
3471 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003472extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003473
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003474/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003475 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003476 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003477 * This routine gets the number of memory blocks that are currently
3478 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003479 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003480 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003481 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003482 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003483 */
Kumar Galacc334c72017-04-21 10:55:34 -05003484static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003485{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003486 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003487}
3488
Peter Mitsisc001aa82016-10-13 13:53:37 -04003489/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003490 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003491 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003492 * This routine gets the number of memory blocks that are currently
3493 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003494 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003495 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003496 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003497 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003498 */
Kumar Galacc334c72017-04-21 10:55:34 -05003499static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003500{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003501 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003502}
3503
Allan Stephensc98da842016-11-11 15:45:03 -05003504/**
3505 * @} end defgroup mem_slab_apis
3506 */
3507
3508/**
3509 * @cond INTERNAL_HIDDEN
3510 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003511
Andy Ross73cb9582017-05-09 10:42:39 -07003512struct k_mem_pool_lvl {
3513 union {
3514 u32_t *bits_p;
3515 u32_t bits;
3516 };
3517 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003518};
3519
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003520struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003521 void *buf;
3522 size_t max_sz;
3523 u16_t n_max;
3524 u8_t n_levels;
3525 u8_t max_inline_level;
3526 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003527 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003528};
3529
Andy Ross73cb9582017-05-09 10:42:39 -07003530#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003531
Andy Ross73cb9582017-05-09 10:42:39 -07003532#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3533
Andy Ross8cf7ff52017-11-13 14:59:13 -08003534#define __MPOOL_LVLS(maxsz, minsz) \
3535 (_MPOOL_HAVE_LVL((maxsz), (minsz), 0) + \
3536 _MPOOL_HAVE_LVL((maxsz), (minsz), 1) + \
3537 _MPOOL_HAVE_LVL((maxsz), (minsz), 2) + \
3538 _MPOOL_HAVE_LVL((maxsz), (minsz), 3) + \
3539 _MPOOL_HAVE_LVL((maxsz), (minsz), 4) + \
3540 _MPOOL_HAVE_LVL((maxsz), (minsz), 5) + \
3541 _MPOOL_HAVE_LVL((maxsz), (minsz), 6) + \
3542 _MPOOL_HAVE_LVL((maxsz), (minsz), 7) + \
3543 _MPOOL_HAVE_LVL((maxsz), (minsz), 8) + \
3544 _MPOOL_HAVE_LVL((maxsz), (minsz), 9) + \
3545 _MPOOL_HAVE_LVL((maxsz), (minsz), 10) + \
3546 _MPOOL_HAVE_LVL((maxsz), (minsz), 11) + \
3547 _MPOOL_HAVE_LVL((maxsz), (minsz), 12) + \
3548 _MPOOL_HAVE_LVL((maxsz), (minsz), 13) + \
3549 _MPOOL_HAVE_LVL((maxsz), (minsz), 14) + \
3550 _MPOOL_HAVE_LVL((maxsz), (minsz), 15))
3551
3552#define _MPOOL_MINBLK sizeof(sys_dnode_t)
3553
3554#define _MPOOL_LVLS(max, min) \
3555 __MPOOL_LVLS((max), (min) >= _MPOOL_MINBLK ? (min) : _MPOOL_MINBLK)
Andy Ross73cb9582017-05-09 10:42:39 -07003556
3557/* Rounds the needed bits up to integer multiples of u32_t */
3558#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3559 ((((n_max) << (2*(l))) + 31) / 32)
3560
3561/* One word gets stored free unioned with the pointer, otherwise the
3562 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003563 */
Andy Ross73cb9582017-05-09 10:42:39 -07003564#define _MPOOL_LBIT_WORDS(n_max, l) \
3565 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3566 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003567
Andy Ross73cb9582017-05-09 10:42:39 -07003568/* How many bytes for the bitfields of a single level? */
3569#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3570 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3571 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003572
Andy Ross73cb9582017-05-09 10:42:39 -07003573/* Size of the bitmap array that follows the buffer in allocated memory */
3574#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3575 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3576 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3577 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3578 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3579 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3580 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3581 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3582 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3583 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3584 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3585 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3586 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3587 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3588 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3589 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3590 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003591
3592/**
Allan Stephensc98da842016-11-11 15:45:03 -05003593 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003594 */
3595
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003596/**
Allan Stephensc98da842016-11-11 15:45:03 -05003597 * @addtogroup mem_pool_apis
3598 * @{
3599 */
3600
3601/**
3602 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003603 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003604 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3605 * long. The memory pool allows blocks to be repeatedly partitioned into
3606 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003607 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003608 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003609 * If the pool is to be accessed outside the module where it is defined, it
3610 * can be declared via
3611 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003612 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003613 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003614 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003615 * @param minsz Size of the smallest blocks in the pool (in bytes).
3616 * @param maxsz Size of the largest blocks in the pool (in bytes).
3617 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003618 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003619 */
Andy Ross73cb9582017-05-09 10:42:39 -07003620#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3621 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3622 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3623 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3624 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3625 .buf = _mpool_buf_##name, \
3626 .max_sz = maxsz, \
3627 .n_max = nmax, \
3628 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3629 .levels = _mpool_lvls_##name, \
3630 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003631
Peter Mitsis937042c2016-10-13 13:18:26 -04003632/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003633 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003635 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003636 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003637 * @param pool Address of the memory pool.
3638 * @param block Pointer to block descriptor for the allocated memory.
3639 * @param size Amount of memory to allocate (in bytes).
3640 * @param timeout Maximum time to wait for operation to complete
3641 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3642 * or K_FOREVER to wait as long as necessary.
3643 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003644 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003645 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003646 * @retval -ENOMEM Returned without waiting.
3647 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003648 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003649extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003650 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003651
3652/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003653 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003654 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003655 * This routine releases a previously allocated memory block back to its
3656 * memory pool.
3657 *
3658 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003659 *
3660 * @return N/A
3661 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003662extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003663
3664/**
Johan Hedberg7d887cb2018-01-11 20:45:27 +02003665 * @brief Free memory allocated from a memory pool.
3666 *
3667 * This routine releases a previously allocated memory block back to its
3668 * memory pool
3669 *
3670 * @param id Memory block identifier.
3671 *
3672 * @return N/A
3673 */
3674extern void k_mem_pool_free_id(struct k_mem_block_id *id);
3675
3676/**
Allan Stephensc98da842016-11-11 15:45:03 -05003677 * @} end addtogroup mem_pool_apis
3678 */
3679
3680/**
3681 * @defgroup heap_apis Heap Memory Pool APIs
3682 * @ingroup kernel_apis
3683 * @{
3684 */
3685
3686/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003687 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003688 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003689 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003690 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003691 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003692 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003694 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003695 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003696extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003697
3698/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003699 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003700 *
3701 * This routine provides traditional free() semantics. The memory being
3702 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003703 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003704 * If @a ptr is NULL, no operation is performed.
3705 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003706 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003707 *
3708 * @return N/A
3709 */
3710extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003711
Allan Stephensc98da842016-11-11 15:45:03 -05003712/**
Andrew Boie7f95e832017-11-08 14:40:01 -08003713 * @brief Allocate memory from heap, array style
3714 *
3715 * This routine provides traditional calloc() semantics. Memory is
3716 * allocated from the heap memory pool and zeroed.
3717 *
3718 * @param nmemb Number of elements in the requested array
3719 * @param size Size of each array element (in bytes).
3720 *
3721 * @return Address of the allocated memory if successful; otherwise NULL.
3722 */
3723extern void *k_calloc(size_t nmemb, size_t size);
3724
3725/**
Allan Stephensc98da842016-11-11 15:45:03 -05003726 * @} end defgroup heap_apis
3727 */
3728
Benjamin Walshacc68c12017-01-29 18:57:45 -05003729/* polling API - PRIVATE */
3730
Benjamin Walshb0179862017-02-02 16:39:57 -05003731#ifdef CONFIG_POLL
3732#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3733#else
3734#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3735#endif
3736
Benjamin Walshacc68c12017-01-29 18:57:45 -05003737/* private - implementation data created as needed, per-type */
3738struct _poller {
3739 struct k_thread *thread;
3740};
3741
3742/* private - types bit positions */
3743enum _poll_types_bits {
3744 /* can be used to ignore an event */
3745 _POLL_TYPE_IGNORE,
3746
3747 /* to be signaled by k_poll_signal() */
3748 _POLL_TYPE_SIGNAL,
3749
3750 /* semaphore availability */
3751 _POLL_TYPE_SEM_AVAILABLE,
3752
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003753 /* queue/fifo/lifo data availability */
3754 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003755
3756 _POLL_NUM_TYPES
3757};
3758
3759#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3760
3761/* private - states bit positions */
3762enum _poll_states_bits {
3763 /* default state when creating event */
3764 _POLL_STATE_NOT_READY,
3765
Benjamin Walshacc68c12017-01-29 18:57:45 -05003766 /* signaled by k_poll_signal() */
3767 _POLL_STATE_SIGNALED,
3768
3769 /* semaphore is available */
3770 _POLL_STATE_SEM_AVAILABLE,
3771
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003772 /* data is available to read on queue/fifo/lifo */
3773 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003774
3775 _POLL_NUM_STATES
3776};
3777
3778#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3779
3780#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003781 (32 - (0 \
3782 + 8 /* tag */ \
3783 + _POLL_NUM_TYPES \
3784 + _POLL_NUM_STATES \
3785 + 1 /* modes */ \
3786 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003787
Benjamin Walshacc68c12017-01-29 18:57:45 -05003788/* end of polling API - PRIVATE */
3789
3790
3791/**
3792 * @defgroup poll_apis Async polling APIs
3793 * @ingroup kernel_apis
3794 * @{
3795 */
3796
3797/* Public polling API */
3798
3799/* public - values for k_poll_event.type bitfield */
3800#define K_POLL_TYPE_IGNORE 0
3801#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3802#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003803#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3804#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003805
3806/* public - polling modes */
3807enum k_poll_modes {
3808 /* polling thread does not take ownership of objects when available */
3809 K_POLL_MODE_NOTIFY_ONLY = 0,
3810
3811 K_POLL_NUM_MODES
3812};
3813
3814/* public - values for k_poll_event.state bitfield */
3815#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003816#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3817#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003818#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3819#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003820
3821/* public - poll signal object */
3822struct k_poll_signal {
3823 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003824 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003825
3826 /*
3827 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3828 * user resets it to 0.
3829 */
3830 unsigned int signaled;
3831
3832 /* custom result value passed to k_poll_signal() if needed */
3833 int result;
3834};
3835
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003836#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003837 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003838 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003839 .signaled = 0, \
3840 .result = 0, \
3841 }
3842
3843struct k_poll_event {
3844 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003845 sys_dnode_t _node;
3846
3847 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003848 struct _poller *poller;
3849
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003850 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003851 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003852
Benjamin Walshacc68c12017-01-29 18:57:45 -05003853 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003854 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003855
3856 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003857 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003858
3859 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003860 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003861
3862 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003863 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003864
3865 /* per-type data */
3866 union {
3867 void *obj;
3868 struct k_poll_signal *signal;
3869 struct k_sem *sem;
3870 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003871 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003872 };
3873};
3874
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003875#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003876 { \
3877 .poller = NULL, \
3878 .type = event_type, \
3879 .state = K_POLL_STATE_NOT_READY, \
3880 .mode = event_mode, \
3881 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003882 { .obj = event_obj }, \
3883 }
3884
3885#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3886 event_tag) \
3887 { \
3888 .type = event_type, \
3889 .tag = event_tag, \
3890 .state = K_POLL_STATE_NOT_READY, \
3891 .mode = event_mode, \
3892 .unused = 0, \
3893 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003894 }
3895
3896/**
3897 * @brief Initialize one struct k_poll_event instance
3898 *
3899 * After this routine is called on a poll event, the event it ready to be
3900 * placed in an event array to be passed to k_poll().
3901 *
3902 * @param event The event to initialize.
3903 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3904 * values. Only values that apply to the same object being polled
3905 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3906 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003907 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003908 * @param obj Kernel object or poll signal.
3909 *
3910 * @return N/A
3911 */
3912
Kumar Galacc334c72017-04-21 10:55:34 -05003913extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003914 int mode, void *obj);
3915
3916/**
3917 * @brief Wait for one or many of multiple poll events to occur
3918 *
3919 * This routine allows a thread to wait concurrently for one or many of
3920 * multiple poll events to have occurred. Such events can be a kernel object
3921 * being available, like a semaphore, or a poll signal event.
3922 *
3923 * When an event notifies that a kernel object is available, the kernel object
3924 * is not "given" to the thread calling k_poll(): it merely signals the fact
3925 * that the object was available when the k_poll() call was in effect. Also,
3926 * all threads trying to acquire an object the regular way, i.e. by pending on
3927 * the object, have precedence over the thread polling on the object. This
3928 * means that the polling thread will never get the poll event on an object
3929 * until the object becomes available and its pend queue is empty. For this
3930 * reason, the k_poll() call is more effective when the objects being polled
3931 * only have one thread, the polling thread, trying to acquire them.
3932 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003933 * When k_poll() returns 0, the caller should loop on all the events that were
3934 * passed to k_poll() and check the state field for the values that were
3935 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003936 *
3937 * Before being reused for another call to k_poll(), the user has to reset the
3938 * state field to K_POLL_STATE_NOT_READY.
3939 *
3940 * @param events An array of pointers to events to be polled for.
3941 * @param num_events The number of events in the array.
3942 * @param timeout Waiting period for an event to be ready (in milliseconds),
3943 * or one of the special values K_NO_WAIT and K_FOREVER.
3944 *
3945 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003946 * @retval -EAGAIN Waiting period timed out.
Luiz Augusto von Dentz8beb5862017-11-20 18:53:15 +02003947 * @retval -EINTR Poller thread has been interrupted.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003948 */
3949
3950extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003951 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003952
3953/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003954 * @brief Initialize a poll signal object.
3955 *
3956 * Ready a poll signal object to be signaled via k_poll_signal().
3957 *
3958 * @param signal A poll signal.
3959 *
3960 * @return N/A
3961 */
3962
3963extern void k_poll_signal_init(struct k_poll_signal *signal);
3964
3965/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003966 * @brief Signal a poll signal object.
3967 *
3968 * This routine makes ready a poll signal, which is basically a poll event of
3969 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3970 * made ready to run. A @a result value can be specified.
3971 *
3972 * The poll signal contains a 'signaled' field that, when set by
3973 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3974 * be reset by the user before being passed again to k_poll() or k_poll() will
3975 * consider it being signaled, and will return immediately.
3976 *
3977 * @param signal A poll signal.
3978 * @param result The value to store in the result field of the signal.
3979 *
3980 * @retval 0 The signal was delivered successfully.
3981 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3982 */
3983
3984extern int k_poll_signal(struct k_poll_signal *signal, int result);
3985
3986/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003987extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003988
3989/**
3990 * @} end defgroup poll_apis
3991 */
3992
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003993/**
3994 * @brief Make the CPU idle.
3995 *
3996 * This function makes the CPU idle until an event wakes it up.
3997 *
3998 * In a regular system, the idle thread should be the only thread responsible
3999 * for making the CPU idle and triggering any type of power management.
4000 * However, in some more constrained systems, such as a single-threaded system,
4001 * the only thread would be responsible for this if needed.
4002 *
4003 * @return N/A
4004 */
4005extern void k_cpu_idle(void);
4006
4007/**
4008 * @brief Make the CPU idle in an atomic fashion.
4009 *
4010 * Similar to k_cpu_idle(), but called with interrupts locked if operations
4011 * must be done atomically before making the CPU idle.
4012 *
4013 * @param key Interrupt locking key obtained from irq_lock().
4014 *
4015 * @return N/A
4016 */
4017extern void k_cpu_atomic_idle(unsigned int key);
4018
Kumar Galacc334c72017-04-21 10:55:34 -05004019extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08004020
Andrew Boiecdb94d62017-04-18 15:22:05 -07004021#ifdef _ARCH_EXCEPT
4022/* This archtecture has direct support for triggering a CPU exception */
4023#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4024#else
4025
Andrew Boiecdb94d62017-04-18 15:22:05 -07004026/* NOTE: This is the implementation for arches that do not implement
4027 * _ARCH_EXCEPT() to generate a real CPU exception.
4028 *
4029 * We won't have a real exception frame to determine the PC value when
4030 * the oops occurred, so print file and line number before we jump into
4031 * the fatal error handler.
4032 */
4033#define _k_except_reason(reason) do { \
4034 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4035 _NanoFatalErrorHandler(reason, &_default_esf); \
4036 CODE_UNREACHABLE; \
4037 } while (0)
4038
4039#endif /* _ARCH__EXCEPT */
4040
4041/**
4042 * @brief Fatally terminate a thread
4043 *
4044 * This should be called when a thread has encountered an unrecoverable
4045 * runtime condition and needs to terminate. What this ultimately
4046 * means is determined by the _fatal_error_handler() implementation, which
4047 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4048 *
4049 * If this is called from ISR context, the default system fatal error handler
4050 * will treat it as an unrecoverable system error, just like k_panic().
4051 */
4052#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4053
4054/**
4055 * @brief Fatally terminate the system
4056 *
4057 * This should be called when the Zephyr kernel has encountered an
4058 * unrecoverable runtime condition and needs to terminate. What this ultimately
4059 * means is determined by the _fatal_error_handler() implementation, which
4060 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4061 */
4062#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4063
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004064/*
4065 * private APIs that are utilized by one or more public APIs
4066 */
4067
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004068#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004069extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004070#else
4071#define _init_static_threads() do { } while ((0))
4072#endif
4073
4074extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05004075extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004076
Andrew Boiedc5d9352017-06-02 12:56:47 -07004077/* arch/cpu.h may declare an architecture or platform-specific macro
4078 * for properly declaring stacks, compatible with MMU/MPU constraints if
4079 * enabled
4080 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004081
4082/**
4083 * @brief Obtain an extern reference to a stack
4084 *
4085 * This macro properly brings the symbol of a thread stack declared
4086 * elsewhere into scope.
4087 *
4088 * @param sym Thread stack symbol name
4089 */
4090#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4091
Andrew Boiedc5d9352017-06-02 12:56:47 -07004092#ifdef _ARCH_THREAD_STACK_DEFINE
4093#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4094#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4095 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4096#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4097#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004098static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004099{
4100 return _ARCH_THREAD_STACK_BUFFER(sym);
4101}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004102#else
4103/**
4104 * @brief Declare a toplevel thread stack memory region
4105 *
4106 * This declares a region of memory suitable for use as a thread's stack.
4107 *
4108 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4109 * 'noinit' section so that it isn't zeroed at boot
4110 *
Andrew Boie507852a2017-07-25 18:47:07 -07004111 * The declared symbol will always be a k_thread_stack_t which can be passed to
4112 * k_thread_create, but should otherwise not be manipulated. If the buffer
4113 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004114 *
4115 * It is legal to precede this definition with the 'static' keyword.
4116 *
4117 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4118 * parameter of k_thread_create(), it may not be the same as the
4119 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4120 *
4121 * @param sym Thread stack symbol name
4122 * @param size Size of the stack memory region
4123 */
4124#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004125 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004126
4127/**
4128 * @brief Declare a toplevel array of thread stack memory regions
4129 *
4130 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4131 * definition for additional details and constraints.
4132 *
4133 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4134 * 'noinit' section so that it isn't zeroed at boot
4135 *
4136 * @param sym Thread stack symbol name
4137 * @param nmemb Number of stacks to declare
4138 * @param size Size of the stack memory region
4139 */
4140
4141#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004142 struct _k_thread_stack_element __noinit \
4143 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004144
4145/**
4146 * @brief Declare an embedded stack memory region
4147 *
4148 * Used for stacks embedded within other data structures. Use is highly
4149 * discouraged but in some cases necessary. For memory protection scenarios,
4150 * it is very important that any RAM preceding this member not be writable
4151 * by threads else a stack overflow will lead to silent corruption. In other
4152 * words, the containing data structure should live in RAM owned by the kernel.
4153 *
4154 * @param sym Thread stack symbol name
4155 * @param size Size of the stack memory region
4156 */
4157#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004158 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004159
4160/**
4161 * @brief Return the size in bytes of a stack memory region
4162 *
4163 * Convenience macro for passing the desired stack size to k_thread_create()
4164 * since the underlying implementation may actually create something larger
4165 * (for instance a guard area).
4166 *
4167 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004168 * passed to K_THREAD_STACK_DEFINE.
4169 *
4170 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4171 * it is not guaranteed to return the original value since each array
4172 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004173 *
4174 * @param sym Stack memory symbol
4175 * @return Size of the stack
4176 */
4177#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4178
4179/**
4180 * @brief Get a pointer to the physical stack buffer
4181 *
4182 * Convenience macro to get at the real underlying stack buffer used by
4183 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4184 * This is really only intended for diagnostic tools which want to examine
4185 * stack memory contents.
4186 *
4187 * @param sym Declared stack symbol name
4188 * @return The buffer itself, a char *
4189 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004190static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004191{
4192 return (char *)sym;
4193}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004194
4195#endif /* _ARCH_DECLARE_STACK */
4196
Chunlin Hane9c97022017-07-07 20:29:30 +08004197/**
4198 * @defgroup mem_domain_apis Memory domain APIs
4199 * @ingroup kernel_apis
4200 * @{
4201 */
4202
4203/** @def MEM_PARTITION_ENTRY
4204 * @brief Used to declare a memory partition entry
4205 */
4206#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4207 {\
4208 .start = _start, \
4209 .size = _size, \
4210 .attr = _attr, \
4211 }
4212
4213/** @def K_MEM_PARTITION_DEFINE
4214 * @brief Used to declare a memory partition
4215 */
4216#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4217#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4218 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
4219 struct k_mem_partition name = \
4220 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4221#else
4222#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4223 struct k_mem_partition name = \
4224 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4225#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4226
Chunlin Hane9c97022017-07-07 20:29:30 +08004227/* memory partition */
4228struct k_mem_partition {
4229 /* start address of memory partition */
4230 u32_t start;
4231 /* size of memory partition */
4232 u32_t size;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304233#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004234 /* attribute of memory partition */
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304235 k_mem_partition_attr_t attr;
4236#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004237};
4238
Chunlin Hane9c97022017-07-07 20:29:30 +08004239/* memory domian */
4240struct k_mem_domain {
4241 /* number of partitions in the domain */
4242 u32_t num_partitions;
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304243#ifdef CONFIG_USERSPACE
Chunlin Hane9c97022017-07-07 20:29:30 +08004244 /* partitions in the domain */
4245 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304246#endif /* CONFIG_USERSPACE */
Chunlin Hane9c97022017-07-07 20:29:30 +08004247 /* domain q */
4248 sys_dlist_t mem_domain_q;
4249};
Adithya Baglody83bedcc2017-10-06 15:43:11 +05304250
Chunlin Hane9c97022017-07-07 20:29:30 +08004251
4252/**
4253 * @brief Initialize a memory domain.
4254 *
4255 * Initialize a memory domain with given name and memory partitions.
4256 *
4257 * @param domain The memory domain to be initialized.
4258 * @param num_parts The number of array items of "parts" parameter.
4259 * @param parts An array of pointers to the memory partitions. Can be NULL
4260 * if num_parts is zero.
4261 */
4262
4263extern void k_mem_domain_init(struct k_mem_domain *domain, u32_t num_parts,
4264 struct k_mem_partition *parts[]);
4265/**
4266 * @brief Destroy a memory domain.
4267 *
4268 * Destroy a memory domain.
4269 *
4270 * @param domain The memory domain to be destroyed.
4271 */
4272
4273extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4274
4275/**
4276 * @brief Add a memory partition into a memory domain.
4277 *
4278 * Add a memory partition into a memory domain.
4279 *
4280 * @param domain The memory domain to be added a memory partition.
4281 * @param part The memory partition to be added
4282 */
4283
4284extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4285 struct k_mem_partition *part);
4286
4287/**
4288 * @brief Remove a memory partition from a memory domain.
4289 *
4290 * Remove a memory partition from a memory domain.
4291 *
4292 * @param domain The memory domain to be removed a memory partition.
4293 * @param part The memory partition to be removed
4294 */
4295
4296extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4297 struct k_mem_partition *part);
4298
4299/**
4300 * @brief Add a thread into a memory domain.
4301 *
4302 * Add a thread into a memory domain.
4303 *
4304 * @param domain The memory domain that the thread is going to be added into.
4305 * @param thread ID of thread going to be added into the memory domain.
4306 */
4307
4308extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4309 k_tid_t thread);
4310
4311/**
4312 * @brief Remove a thread from its memory domain.
4313 *
4314 * Remove a thread from its memory domain.
4315 *
4316 * @param thread ID of thread going to be removed from its memory domain.
4317 */
4318
4319extern void k_mem_domain_remove_thread(k_tid_t thread);
4320
4321/**
4322 * @} end defgroup mem_domain_apis
4323 */
4324
Andrew Boie756f9072017-10-10 16:01:49 -07004325/**
4326 * @brief Emit a character buffer to the console device
4327 *
4328 * @param c String of characters to print
4329 * @param n The length of the string
4330 */
4331__syscall void k_str_out(char *c, size_t n);
4332
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004333#ifdef __cplusplus
4334}
4335#endif
4336
Andrew Boiee004dec2016-11-07 09:01:19 -08004337#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4338/*
4339 * Define new and delete operators.
4340 * At this moment, the operators do nothing since objects are supposed
4341 * to be statically allocated.
4342 */
4343inline void operator delete(void *ptr)
4344{
4345 (void)ptr;
4346}
4347
4348inline void operator delete[](void *ptr)
4349{
4350 (void)ptr;
4351}
4352
4353inline void *operator new(size_t size)
4354{
4355 (void)size;
4356 return NULL;
4357}
4358
4359inline void *operator new[](size_t size)
4360{
4361 (void)size;
4362 return NULL;
4363}
4364
4365/* Placement versions of operator new and delete */
4366inline void operator delete(void *ptr1, void *ptr2)
4367{
4368 (void)ptr1;
4369 (void)ptr2;
4370}
4371
4372inline void operator delete[](void *ptr1, void *ptr2)
4373{
4374 (void)ptr1;
4375 (void)ptr2;
4376}
4377
4378inline void *operator new(size_t size, void *ptr)
4379{
4380 (void)size;
4381 return ptr;
4382}
4383
4384inline void *operator new[](size_t size, void *ptr)
4385{
4386 (void)size;
4387 return ptr;
4388}
4389
4390#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4391
Andrew Boiefa94ee72017-09-28 16:54:35 -07004392#include <syscalls/kernel.h>
4393
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004394#endif /* !_ASMLANGUAGE */
4395
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004396#endif /* _kernel__h_ */