blob: 02824d82e6718052eea887c7f408b68f6bb86a7b [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>
Anas Nashifa6149502017-01-17 07:47:31 -050029#include <drivers/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>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040032
33#ifdef __cplusplus
34extern "C" {
35#endif
36
Anas Nashifbbb157d2017-01-15 08:46:31 -050037/**
38 * @brief Kernel APIs
39 * @defgroup kernel_apis Kernel APIs
40 * @{
41 * @}
42 */
43
Anas Nashif61f4b242016-11-18 10:53:59 -050044#ifdef CONFIG_KERNEL_DEBUG
45#include <misc/printk.h>
Benjamin Walsh456c6da2016-09-02 18:55:39 -040046#define K_DEBUG(fmt, ...) printk("[%s] " fmt, __func__, ##__VA_ARGS__)
47#else
48#define K_DEBUG(fmt, ...)
49#endif
50
Benjamin Walsh2f280412017-01-14 19:23:46 -050051#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
52#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES)
53#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
54#elif defined(CONFIG_COOP_ENABLED)
55#define _NUM_COOP_PRIO (CONFIG_NUM_COOP_PRIORITIES + 1)
56#define _NUM_PREEMPT_PRIO (0)
57#elif defined(CONFIG_PREEMPT_ENABLED)
58#define _NUM_COOP_PRIO (0)
59#define _NUM_PREEMPT_PRIO (CONFIG_NUM_PREEMPT_PRIORITIES + 1)
60#else
61#error "invalid configuration"
62#endif
63
64#define K_PRIO_COOP(x) (-(_NUM_COOP_PRIO - (x)))
Benjamin Walsh456c6da2016-09-02 18:55:39 -040065#define K_PRIO_PREEMPT(x) (x)
66
Benjamin Walsh456c6da2016-09-02 18:55:39 -040067#define K_ANY NULL
68#define K_END NULL
69
Benjamin Walshedb35702017-01-14 18:47:22 -050070#if defined(CONFIG_COOP_ENABLED) && defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040071#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES)
Benjamin Walshedb35702017-01-14 18:47:22 -050072#elif defined(CONFIG_COOP_ENABLED)
73#define K_HIGHEST_THREAD_PRIO (-CONFIG_NUM_COOP_PRIORITIES - 1)
74#elif defined(CONFIG_PREEMPT_ENABLED)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040075#define K_HIGHEST_THREAD_PRIO 0
Benjamin Walshedb35702017-01-14 18:47:22 -050076#else
77#error "invalid configuration"
Benjamin Walsh456c6da2016-09-02 18:55:39 -040078#endif
79
Benjamin Walsh7fa3cd72017-01-14 18:49:11 -050080#ifdef CONFIG_PREEMPT_ENABLED
Benjamin Walsh456c6da2016-09-02 18:55:39 -040081#define K_LOWEST_THREAD_PRIO CONFIG_NUM_PREEMPT_PRIORITIES
82#else
83#define K_LOWEST_THREAD_PRIO -1
84#endif
85
Benjamin Walshfab8d922016-11-08 15:36:36 -050086#define K_IDLE_PRIO K_LOWEST_THREAD_PRIO
87
Benjamin Walsh456c6da2016-09-02 18:55:39 -040088#define K_HIGHEST_APPLICATION_THREAD_PRIO (K_HIGHEST_THREAD_PRIO)
89#define K_LOWEST_APPLICATION_THREAD_PRIO (K_LOWEST_THREAD_PRIO - 1)
90
91typedef sys_dlist_t _wait_q_t;
92
Anas Nashif2f203c22016-12-18 06:57:45 -050093#ifdef CONFIG_OBJECT_TRACING
94#define _OBJECT_TRACING_NEXT_PTR(type) struct type *__next
95#define _OBJECT_TRACING_INIT .__next = NULL,
Benjamin Walsh456c6da2016-09-02 18:55:39 -040096#else
Anas Nashif2f203c22016-12-18 06:57:45 -050097#define _OBJECT_TRACING_INIT
98#define _OBJECT_TRACING_NEXT_PTR(type)
Benjamin Walsh456c6da2016-09-02 18:55:39 -040099#endif
100
Benjamin Walshacc68c12017-01-29 18:57:45 -0500101#ifdef CONFIG_POLL
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300102#define _POLL_EVENT_OBJ_INIT(obj) \
103 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events),
104#define _POLL_EVENT sys_dlist_t poll_events
Benjamin Walshacc68c12017-01-29 18:57:45 -0500105#else
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +0300106#define _POLL_EVENT_OBJ_INIT(obj)
Benjamin Walshacc68c12017-01-29 18:57:45 -0500107#define _POLL_EVENT
108#endif
109
Benjamin Walshf6ca7de2016-11-08 10:36:50 -0500110struct k_thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400111struct k_mutex;
112struct k_sem;
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -0400113struct k_alert;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400114struct k_msgq;
115struct k_mbox;
116struct k_pipe;
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +0200117struct k_queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400118struct k_fifo;
119struct k_lifo;
120struct k_stack;
Benjamin Walsh7ef0f622016-10-24 17:04:43 -0400121struct k_mem_slab;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400122struct k_mem_pool;
123struct k_timer;
Benjamin Walshacc68c12017-01-29 18:57:45 -0500124struct k_poll_event;
125struct k_poll_signal;
Chunlin Hane9c97022017-07-07 20:29:30 +0800126struct k_mem_domain;
127struct k_mem_partition;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400128
Andrew Boie5bd891d2017-09-27 12:59:28 -0700129/* This enumeration needs to be kept in sync with the lists of kernel objects
130 * and subsystems in scripts/gen_kobject_list.py, as well as the otype_to_str()
131 * function in kernel/userspace.c
132 */
Andrew Boie945af952017-08-22 13:15:23 -0700133enum k_objects {
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700134 K_OBJ_ANY,
135
Andrew Boie5bd891d2017-09-27 12:59:28 -0700136 /* Core kernel objects */
Andrew Boie945af952017-08-22 13:15:23 -0700137 K_OBJ_ALERT,
Andrew Boie945af952017-08-22 13:15:23 -0700138 K_OBJ_MSGQ,
139 K_OBJ_MUTEX,
140 K_OBJ_PIPE,
141 K_OBJ_SEM,
142 K_OBJ_STACK,
143 K_OBJ_THREAD,
144 K_OBJ_TIMER,
Andrew Boiebca15da2017-10-15 14:17:48 -0700145 K_OBJ__THREAD_STACK_ELEMENT,
Andrew Boie945af952017-08-22 13:15:23 -0700146
Andrew Boie5bd891d2017-09-27 12:59:28 -0700147 /* Driver subsystems */
148 K_OBJ_DRIVER_ADC,
149 K_OBJ_DRIVER_AIO_CMP,
150 K_OBJ_DRIVER_CLOCK_CONTROL,
151 K_OBJ_DRIVER_COUNTER,
152 K_OBJ_DRIVER_CRYPTO,
153 K_OBJ_DRIVER_DMA,
154 K_OBJ_DRIVER_ETH,
155 K_OBJ_DRIVER_FLASH,
156 K_OBJ_DRIVER_GPIO,
157 K_OBJ_DRIVER_I2C,
158 K_OBJ_DRIVER_I2S,
159 K_OBJ_DRIVER_IPM,
160 K_OBJ_DRIVER_PINMUX,
161 K_OBJ_DRIVER_PWM,
162 K_OBJ_DRIVER_RANDOM,
163 K_OBJ_DRIVER_RTC,
164 K_OBJ_DRIVER_SENSOR,
165 K_OBJ_DRIVER_SHARED_IRQ,
166 K_OBJ_DRIVER_SPI,
167 K_OBJ_DRIVER_UART,
168 K_OBJ_DRIVER_WDT,
169
Andrew Boie945af952017-08-22 13:15:23 -0700170 K_OBJ_LAST
171};
172
173#ifdef CONFIG_USERSPACE
174/* Table generated by gperf, these objects are retrieved via
175 * _k_object_find() */
176struct _k_object {
177 char *name;
Andrew Boiea811af32017-10-14 13:50:26 -0700178 u8_t perms[CONFIG_MAX_THREAD_BYTES];
179 u8_t type;
180 u8_t flags;
Andrew Boiebca15da2017-10-15 14:17:48 -0700181 u32_t data;
Andrew Boie945af952017-08-22 13:15:23 -0700182} __packed;
183
Andrew Boie877f82e2017-10-17 11:20:22 -0700184struct _k_object_assignment {
185 struct k_thread *thread;
186 void * const *objects;
187};
188
189/**
190 * @brief Grant a static thread access to a list of kernel objects
191 *
192 * For threads declared with K_THREAD_DEFINE(), grant the thread access to
193 * a set of kernel objects. These objects do not need to be in an initialized
194 * state. The permissions will be granted when the threads are initialized
195 * in the early boot sequence.
196 *
197 * All arguments beyond the first must be pointers to kernel objects.
198 *
199 * @param name_ Name of the thread, as passed to K_THREAD_DEFINE()
200 */
201#define K_THREAD_ACCESS_GRANT(name_, ...) \
202 static void * const _CONCAT(_object_list_, name_)[] = \
203 { __VA_ARGS__, NULL }; \
204 static __used __in_section_unique(object_access) \
205 const struct _k_object_assignment \
206 _CONCAT(_object_access_, name_) = \
207 { (&_k_thread_obj_ ## name_), \
208 (_CONCAT(_object_list_, name_)) }
209
Andrew Boie945af952017-08-22 13:15:23 -0700210#define K_OBJ_FLAG_INITIALIZED BIT(0)
Andrew Boie04caa672017-10-13 13:57:07 -0700211#define K_OBJ_FLAG_PUBLIC BIT(1)
Andrew Boie945af952017-08-22 13:15:23 -0700212
213/**
214 * Lookup a kernel object and init its metadata if it exists
215 *
216 * Calling this on an object will make it usable from userspace.
217 * Intended to be called as the last statement in kernel object init
218 * functions.
219 *
220 * @param object Address of the kernel object
221 */
222void _k_object_init(void *obj);
Andrew Boie743e4682017-10-04 12:25:50 -0700223#else
Andrew Boie877f82e2017-10-17 11:20:22 -0700224
225#define K_THREAD_ACCESS_GRANT(thread, ...)
226
Andrew Boie743e4682017-10-04 12:25:50 -0700227static inline void _k_object_init(void *obj)
228{
229 ARG_UNUSED(obj);
230}
231
232static inline void _impl_k_object_access_grant(void *object,
233 struct k_thread *thread)
234{
235 ARG_UNUSED(object);
236 ARG_UNUSED(thread);
237}
238
Andrew Boiea89bf012017-10-09 14:47:55 -0700239static inline void _impl_k_object_access_revoke(void *object,
240 struct k_thread *thread)
241{
242 ARG_UNUSED(object);
243 ARG_UNUSED(thread);
244}
245
Andrew Boie41bab6e2017-10-14 14:42:23 -0700246static inline void k_object_access_all_grant(void *object)
Andrew Boie743e4682017-10-04 12:25:50 -0700247{
248 ARG_UNUSED(object);
249}
250#endif /* !CONFIG_USERSPACE */
Andrew Boie945af952017-08-22 13:15:23 -0700251
252/**
253 * grant a thread access to a kernel object
254 *
255 * The thread will be granted access to the object if the caller is from
256 * supervisor mode, or the caller is from user mode AND has permissions
Andrew Boiea89bf012017-10-09 14:47:55 -0700257 * on both the object and the thread whose access is being granted.
Andrew Boie945af952017-08-22 13:15:23 -0700258 *
259 * @param object Address of kernel object
260 * @param thread Thread to grant access to the object
261 */
Andrew Boie743e4682017-10-04 12:25:50 -0700262__syscall void k_object_access_grant(void *object, struct k_thread *thread);
Andrew Boie945af952017-08-22 13:15:23 -0700263
Andrew Boiea89bf012017-10-09 14:47:55 -0700264/**
265 * grant a thread access to a kernel object
266 *
267 * The thread will lose access to the object if the caller is from
268 * supervisor mode, or the caller is from user mode AND has permissions
269 * on both the object and the thread whose access is being revoked.
270 *
271 * @param object Address of kernel object
272 * @param thread Thread to remove access to the object
273 */
274__syscall void k_object_access_revoke(void *object, struct k_thread *thread);
Andrew Boie3b5ae802017-10-04 12:10:32 -0700275
276/**
277 * grant all present and future threads access to an object
278 *
279 * If the caller is from supervisor mode, or the caller is from user mode and
280 * have sufficient permissions on the object, then that object will have
281 * permissions granted to it for *all* current and future threads running in
282 * the system, effectively becoming a public kernel object.
283 *
284 * Use of this API should be avoided on systems that are running untrusted code
285 * as it is possible for such code to derive the addresses of kernel objects
286 * and perform unwanted operations on them.
287 *
Andrew Boie04caa672017-10-13 13:57:07 -0700288 * It is not possible to revoke permissions on public objects; once public,
289 * any thread may use it.
290 *
Andrew Boie3b5ae802017-10-04 12:10:32 -0700291 * @param object Address of kernel object
292 */
Andrew Boie41bab6e2017-10-14 14:42:23 -0700293void k_object_access_all_grant(void *object);
Andrew Boie945af952017-08-22 13:15:23 -0700294
Andrew Boiebca15da2017-10-15 14:17:48 -0700295/* Using typedef deliberately here, this is quite intended to be an opaque
296 * type. K_THREAD_STACK_BUFFER() should be used to access the data within.
297 *
298 * The purpose of this data type is to clearly distinguish between the
299 * declared symbol for a stack (of type k_thread_stack_t) and the underlying
300 * buffer which composes the stack data actually used by the underlying
301 * thread; they cannot be used interchangably as some arches precede the
302 * stack buffer region with guard areas that trigger a MPU or MMU fault
303 * if written to.
304 *
305 * APIs that want to work with the buffer inside should continue to use
306 * char *.
307 *
308 * Stacks should always be created with K_THREAD_STACK_DEFINE().
309 */
310struct __packed _k_thread_stack_element {
311 char data;
312};
Andrew Boiec5c104f2017-10-16 14:46:34 -0700313typedef struct _k_thread_stack_element k_thread_stack_t;
Andrew Boiebca15da2017-10-15 14:17:48 -0700314
Andrew Boie73abd322017-04-04 13:19:13 -0700315/* timeouts */
316
317struct _timeout;
318typedef void (*_timeout_func_t)(struct _timeout *t);
319
320struct _timeout {
321 sys_dnode_t node;
322 struct k_thread *thread;
323 sys_dlist_t *wait_q;
324 s32_t delta_ticks_from_prev;
325 _timeout_func_t func;
326};
327
328extern s32_t _timeout_remaining_get(struct _timeout *timeout);
329
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700330/**
331 * @typedef k_thread_entry_t
332 * @brief Thread entry point function type.
333 *
334 * A thread's entry point function is invoked when the thread starts executing.
335 * Up to 3 argument values can be passed to the function.
336 *
337 * The thread terminates execution permanently if the entry point function
338 * returns. The thread is responsible for releasing any shared resources
339 * it may own (such as mutexes and dynamically allocated memory), prior to
340 * returning.
341 *
342 * @param p1 First argument.
343 * @param p2 Second argument.
344 * @param p3 Third argument.
345 *
346 * @return N/A
347 */
348typedef void (*k_thread_entry_t)(void *p1, void *p2, void *p3);
Andrew Boie73abd322017-04-04 13:19:13 -0700349
350#ifdef CONFIG_THREAD_MONITOR
351struct __thread_entry {
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700352 k_thread_entry_t pEntry;
Andrew Boie73abd322017-04-04 13:19:13 -0700353 void *parameter1;
354 void *parameter2;
355 void *parameter3;
356};
357#endif
358
359/* can be used for creating 'dummy' threads, e.g. for pending on objects */
360struct _thread_base {
361
362 /* this thread's entry in a ready/wait queue */
363 sys_dnode_t k_q_node;
364
365 /* user facing 'thread options'; values defined in include/kernel.h */
366 u8_t user_options;
367
368 /* thread state */
369 u8_t thread_state;
370
371 /*
372 * scheduler lock count and thread priority
373 *
374 * These two fields control the preemptibility of a thread.
375 *
376 * When the scheduler is locked, sched_locked is decremented, which
377 * means that the scheduler is locked for values from 0xff to 0x01. A
378 * thread is coop if its prio is negative, thus 0x80 to 0xff when
379 * looked at the value as unsigned.
380 *
381 * By putting them end-to-end, this means that a thread is
382 * non-preemptible if the bundled value is greater than or equal to
383 * 0x0080.
384 */
385 union {
386 struct {
387#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
388 u8_t sched_locked;
389 s8_t prio;
390#else /* LITTLE and PDP */
391 s8_t prio;
392 u8_t sched_locked;
393#endif
394 };
395 u16_t preempt;
396 };
397
398 /* data returned by APIs */
399 void *swap_data;
400
401#ifdef CONFIG_SYS_CLOCK_EXISTS
402 /* this thread's entry in a timeout queue */
403 struct _timeout timeout;
404#endif
Andrew Boie2acfcd62017-08-30 14:31:03 -0700405
406#ifdef CONFIG_USERSPACE
407 /* Bit position in kernel object permissions bitfield for this thread */
408 unsigned int perm_index;
409#endif
Andrew Boie73abd322017-04-04 13:19:13 -0700410};
411
412typedef struct _thread_base _thread_base_t;
413
414#if defined(CONFIG_THREAD_STACK_INFO)
415/* Contains the stack information of a thread */
416struct _thread_stack_info {
417 /* Stack Start */
418 u32_t start;
419 /* Stack Size */
420 u32_t size;
421};
Andrew Boie41c68ec2017-05-11 15:38:20 -0700422
423typedef struct _thread_stack_info _thread_stack_info_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700424#endif /* CONFIG_THREAD_STACK_INFO */
425
Chunlin Hane9c97022017-07-07 20:29:30 +0800426#if defined(CONFIG_USERSPACE)
427struct _mem_domain_info {
428 /* memory domain queue node */
429 sys_dnode_t mem_domain_q_node;
430 /* memory domain of the thread */
431 struct k_mem_domain *mem_domain;
432};
433
434#endif /* CONFIG_USERSPACE */
435
Andrew Boie73abd322017-04-04 13:19:13 -0700436struct k_thread {
437
438 struct _thread_base base;
439
440 /* defined by the architecture, but all archs need these */
441 struct _caller_saved caller_saved;
442 struct _callee_saved callee_saved;
443
444 /* static thread init data */
445 void *init_data;
446
447 /* abort function */
448 void (*fn_abort)(void);
449
450#if defined(CONFIG_THREAD_MONITOR)
451 /* thread entry and parameters description */
452 struct __thread_entry *entry;
453
454 /* next item in list of all threads */
455 struct k_thread *next_thread;
456#endif
457
458#ifdef CONFIG_THREAD_CUSTOM_DATA
459 /* crude thread-local storage */
460 void *custom_data;
461#endif
462
463#ifdef CONFIG_ERRNO
464 /* per-thread errno variable */
465 int errno_var;
466#endif
467
468#if defined(CONFIG_THREAD_STACK_INFO)
469 /* Stack Info */
470 struct _thread_stack_info stack_info;
471#endif /* CONFIG_THREAD_STACK_INFO */
472
Chunlin Hane9c97022017-07-07 20:29:30 +0800473#if defined(CONFIG_USERSPACE)
474 /* memory domain info of the thread */
475 struct _mem_domain_info mem_domain_info;
Andrew Boiebca15da2017-10-15 14:17:48 -0700476 /* Base address of thread stack */
Andrew Boiec5c104f2017-10-16 14:46:34 -0700477 k_thread_stack_t *stack_obj;
Chunlin Hane9c97022017-07-07 20:29:30 +0800478#endif /* CONFIG_USERSPACE */
479
Andrew Boie73abd322017-04-04 13:19:13 -0700480 /* arch-specifics: must always be at the end */
481 struct _thread_arch arch;
482};
483
484typedef struct k_thread _thread_t;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -0400485typedef struct k_thread *k_tid_t;
Andrew Boie73abd322017-04-04 13:19:13 -0700486#define tcs k_thread
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400487
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400488enum execution_context_types {
489 K_ISR = 0,
490 K_COOP_THREAD,
491 K_PREEMPT_THREAD,
492};
493
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400494/**
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100495 * @defgroup profiling_apis Profiling APIs
496 * @ingroup kernel_apis
497 * @{
498 */
499
500/**
501 * @brief Analyze the main, idle, interrupt and system workqueue call stacks
502 *
Andrew Boiedc5d9352017-06-02 12:56:47 -0700503 * This routine calls @ref STACK_ANALYZE on the 4 call stacks declared and
Carles Cuficb0cf9f2017-01-10 10:57:38 +0100504 * maintained by the kernel. The sizes of those 4 call stacks are defined by:
505 *
506 * CONFIG_MAIN_STACK_SIZE
507 * CONFIG_IDLE_STACK_SIZE
508 * CONFIG_ISR_STACK_SIZE
509 * CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE
510 *
511 * @note CONFIG_INIT_STACKS and CONFIG_PRINTK must be set for this function to
512 * produce output.
513 *
514 * @return N/A
515 */
516extern void k_call_stacks_analyze(void);
517
518/**
519 * @} end defgroup profiling_apis
520 */
521
522/**
Allan Stephensc98da842016-11-11 15:45:03 -0500523 * @defgroup thread_apis Thread APIs
524 * @ingroup kernel_apis
525 * @{
526 */
527
Benjamin Walshed240f22017-01-22 13:05:08 -0500528#endif /* !_ASMLANGUAGE */
529
530
531/*
532 * Thread user options. May be needed by assembly code. Common part uses low
533 * bits, arch-specific use high bits.
534 */
535
536/* system thread that must not abort */
537#define K_ESSENTIAL (1 << 0)
538
539#if defined(CONFIG_FP_SHARING)
540/* thread uses floating point registers */
541#define K_FP_REGS (1 << 1)
542#endif
543
Andrew Boie5cfa5dc2017-08-30 14:17:44 -0700544/* This thread has dropped from supervisor mode to user mode and consequently
545 * has additional restrictions
546 */
547#define K_USER (1 << 2)
548
Andrew Boie47f8fd12017-10-05 11:11:02 -0700549/* Indicates that the thread being created should inherit all kernel object
550 * permissions from the thread that created it. No effect if CONFIG_USERSPACE
551 * is not enabled.
552 */
553#define K_INHERIT_PERMS (1 << 3)
554
Benjamin Walshed240f22017-01-22 13:05:08 -0500555#ifdef CONFIG_X86
556/* x86 Bitmask definitions for threads user options */
557
558#if defined(CONFIG_FP_SHARING) && defined(CONFIG_SSE)
559/* thread uses SSEx (and also FP) registers */
560#define K_SSE_REGS (1 << 7)
561#endif
562#endif
563
564/* end - thread options */
565
566#if !defined(_ASMLANGUAGE)
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400567/**
Andrew Boied26cf2d2017-03-30 13:07:02 -0700568 * @brief Create a thread.
569 *
570 * This routine initializes a thread, then schedules it for execution.
571 *
572 * The new thread may be scheduled for immediate execution or a delayed start.
573 * If the newly spawned thread does not have a delayed start the kernel
574 * scheduler may preempt the current thread to allow the new thread to
575 * execute.
576 *
577 * Thread options are architecture-specific, and can include K_ESSENTIAL,
578 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
579 * them using "|" (the logical OR operator).
580 *
581 * Historically, users often would use the beginning of the stack memory region
582 * to store the struct k_thread data, although corruption will occur if the
583 * stack overflows this region and stack protection features may not detect this
584 * situation.
585 *
586 * @param new_thread Pointer to uninitialized struct k_thread
587 * @param stack Pointer to the stack space.
588 * @param stack_size Stack size in bytes.
589 * @param entry Thread entry function.
590 * @param p1 1st entry point parameter.
591 * @param p2 2nd entry point parameter.
592 * @param p3 3rd entry point parameter.
593 * @param prio Thread priority.
594 * @param options Thread options.
595 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
596 *
597 * @return ID of new thread.
598 */
Andrew Boie662c3452017-10-02 10:51:18 -0700599__syscall k_tid_t k_thread_create(struct k_thread *new_thread,
Andrew Boiec5c104f2017-10-16 14:46:34 -0700600 k_thread_stack_t *stack,
Andrew Boie662c3452017-10-02 10:51:18 -0700601 size_t stack_size,
602 k_thread_entry_t entry,
603 void *p1, void *p2, void *p3,
604 int prio, u32_t options, s32_t delay);
Andrew Boied26cf2d2017-03-30 13:07:02 -0700605
Andrew Boie3f091b52017-08-30 14:34:14 -0700606/**
607 * @brief Drop a thread's privileges permanently to user mode
608 *
609 * @param entry Function to start executing from
610 * @param p1 1st entry point parameter
611 * @param p2 2nd entry point parameter
612 * @param p3 3rd entry point parameter
613 */
614extern FUNC_NORETURN void k_thread_user_mode_enter(k_thread_entry_t entry,
615 void *p1, void *p2,
616 void *p3);
Andrew Boie3f091b52017-08-30 14:34:14 -0700617
Andrew Boied26cf2d2017-03-30 13:07:02 -0700618/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500619 * @brief Put the current thread to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400620 *
Allan Stephensc98da842016-11-11 15:45:03 -0500621 * This routine puts the current thread to sleep for @a duration
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500622 * milliseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500624 * @param duration Number of milliseconds to sleep.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400625 *
626 * @return N/A
627 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700628__syscall void k_sleep(s32_t duration);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400629
630/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500631 * @brief Cause the current thread to busy wait.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400632 *
633 * This routine causes the current thread to execute a "do nothing" loop for
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500634 * @a usec_to_wait microseconds.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400635 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400636 * @return N/A
637 */
Kumar Galacc334c72017-04-21 10:55:34 -0500638extern void k_busy_wait(u32_t usec_to_wait);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400639
640/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500641 * @brief Yield the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400642 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500643 * This routine causes the current thread to yield execution to another
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400644 * thread of the same or higher priority. If there are no other ready threads
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500645 * of the same or higher priority, the routine returns immediately.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400646 *
647 * @return N/A
648 */
Andrew Boie468190a2017-09-29 14:00:48 -0700649__syscall void k_yield(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400650
651/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500652 * @brief Wake up a sleeping thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400653 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500654 * This routine prematurely wakes up @a thread from sleeping.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400655 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500656 * If @a thread is not currently sleeping, the routine has no effect.
657 *
658 * @param thread ID of thread to wake.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400659 *
660 * @return N/A
661 */
Andrew Boie468190a2017-09-29 14:00:48 -0700662__syscall void k_wakeup(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400663
664/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500665 * @brief Get thread ID of the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400666 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500667 * @return ID of current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400668 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700669__syscall k_tid_t k_current_get(void);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400670
671/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500672 * @brief Cancel thread performing a delayed start.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400673 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500674 * This routine prevents @a thread from executing if it has not yet started
675 * execution. The thread must be re-spawned before it will execute.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400676 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500677 * @param thread ID of thread to cancel.
678 *
David B. Kinder8b986d72017-04-18 15:56:26 -0700679 * @retval 0 Thread spawning canceled.
Allan Stephens9ef50f42016-11-16 15:33:31 -0500680 * @retval -EINVAL Thread has already started executing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400681 */
Andrew Boie468190a2017-09-29 14:00:48 -0700682__syscall int k_thread_cancel(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400683
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400684/**
Allan Stephensc98da842016-11-11 15:45:03 -0500685 * @brief Abort a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400686 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500687 * This routine permanently stops execution of @a thread. The thread is taken
688 * off all kernel queues it is part of (i.e. the ready queue, the timeout
689 * queue, or a kernel object wait queue). However, any kernel resources the
690 * thread might currently own (such as mutexes or memory blocks) are not
691 * released. It is the responsibility of the caller of this routine to ensure
692 * all necessary cleanup is performed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500694 * @param thread ID of thread to abort.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400695 *
696 * @return N/A
697 */
Andrew Boie468190a2017-09-29 14:00:48 -0700698__syscall void k_thread_abort(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400699
Andrew Boie7d627c52017-08-30 11:01:56 -0700700
701/**
702 * @brief Start an inactive thread
703 *
704 * If a thread was created with K_FOREVER in the delay parameter, it will
705 * not be added to the scheduling queue until this function is called
706 * on it.
707 *
708 * @param thread thread to start
709 */
Andrew Boie468190a2017-09-29 14:00:48 -0700710__syscall void k_thread_start(k_tid_t thread);
Andrew Boie7d627c52017-08-30 11:01:56 -0700711
Allan Stephensc98da842016-11-11 15:45:03 -0500712/**
713 * @cond INTERNAL_HIDDEN
714 */
715
Benjamin Walshd211a522016-12-06 11:44:01 -0500716/* timeout has timed out and is not on _timeout_q anymore */
717#define _EXPIRED (-2)
718
719/* timeout is not in use */
720#define _INACTIVE (-1)
721
Peter Mitsisa04c0d72016-09-28 19:26:00 -0400722struct _static_thread_data {
Andrew Boied26cf2d2017-03-30 13:07:02 -0700723 struct k_thread *init_thread;
Andrew Boiec5c104f2017-10-16 14:46:34 -0700724 k_thread_stack_t *init_stack;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400725 unsigned int init_stack_size;
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700726 k_thread_entry_t init_entry;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500727 void *init_p1;
728 void *init_p2;
729 void *init_p3;
730 int init_prio;
Kumar Galacc334c72017-04-21 10:55:34 -0500731 u32_t init_options;
732 s32_t init_delay;
Allan Stephens7c5bffa2016-10-26 10:01:28 -0500733 void (*init_abort)(void);
Kumar Galacc334c72017-04-21 10:55:34 -0500734 u32_t init_groups;
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400735};
736
Andrew Boied26cf2d2017-03-30 13:07:02 -0700737#define _THREAD_INITIALIZER(thread, stack, stack_size, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400738 entry, p1, p2, p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500739 prio, options, delay, abort, groups) \
740 { \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700741 .init_thread = (thread), \
742 .init_stack = (stack), \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500743 .init_stack_size = (stack_size), \
Andrew Boie1e06ffc2017-09-11 09:30:04 -0700744 .init_entry = (k_thread_entry_t)entry, \
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400745 .init_p1 = (void *)p1, \
746 .init_p2 = (void *)p2, \
747 .init_p3 = (void *)p3, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500748 .init_prio = (prio), \
749 .init_options = (options), \
750 .init_delay = (delay), \
751 .init_abort = (abort), \
752 .init_groups = (groups), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400753 }
754
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400755/**
Allan Stephensc98da842016-11-11 15:45:03 -0500756 * INTERNAL_HIDDEN @endcond
757 */
758
759/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500760 * @brief Statically define and initialize a thread.
761 *
762 * The thread may be scheduled for immediate execution or a delayed start.
763 *
764 * Thread options are architecture-specific, and can include K_ESSENTIAL,
765 * K_FP_REGS, and K_SSE_REGS. Multiple options may be specified by separating
766 * them using "|" (the logical OR operator).
767 *
768 * The ID of the thread can be accessed using:
769 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -0500770 * @code extern const k_tid_t <name>; @endcode
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500771 *
772 * @param name Name of the thread.
773 * @param stack_size Stack size in bytes.
774 * @param entry Thread entry function.
775 * @param p1 1st entry point parameter.
776 * @param p2 2nd entry point parameter.
777 * @param p3 3rd entry point parameter.
778 * @param prio Thread priority.
779 * @param options Thread options.
780 * @param delay Scheduling delay (in milliseconds), or K_NO_WAIT (for no delay).
Peter Mitsisb2fd5be2016-10-11 12:06:25 -0400781 *
782 * @internal It has been observed that the x86 compiler by default aligns
783 * these _static_thread_data structures to 32-byte boundaries, thereby
784 * wasting space. To work around this, force a 4-byte alignment.
785 */
Allan Stephens6cfe1322016-10-26 10:16:51 -0500786#define K_THREAD_DEFINE(name, stack_size, \
787 entry, p1, p2, p3, \
788 prio, options, delay) \
Andrew Boiedc5d9352017-06-02 12:56:47 -0700789 K_THREAD_STACK_DEFINE(_k_thread_stack_##name, stack_size); \
Andrew Boie8749c262017-08-29 12:18:07 -0700790 struct k_thread __kernel _k_thread_obj_##name; \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500791 struct _static_thread_data _k_thread_data_##name __aligned(4) \
Allan Stephense7d2cc22016-10-19 16:10:46 -0500792 __in_section(_static_thread_data, static, name) = \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700793 _THREAD_INITIALIZER(&_k_thread_obj_##name, \
794 _k_thread_stack_##name, stack_size, \
Allan Stephens6cfe1322016-10-26 10:16:51 -0500795 entry, p1, p2, p3, prio, options, delay, \
Andrew Boied26cf2d2017-03-30 13:07:02 -0700796 NULL, 0); \
797 const k_tid_t name = (k_tid_t)&_k_thread_obj_##name
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400798
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400799/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500800 * @brief Get a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400801 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500802 * This routine gets the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400803 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500804 * @param thread ID of thread whose priority is needed.
805 *
806 * @return Priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400807 */
Andrew Boie76c04a22017-09-27 14:45:10 -0700808__syscall int k_thread_priority_get(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400809
810/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500811 * @brief Set a thread's priority.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400812 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500813 * This routine immediately changes the priority of @a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400814 *
815 * Rescheduling can occur immediately depending on the priority @a thread is
816 * set to:
817 *
818 * - If its priority is raised above the priority of the caller of this
819 * function, and the caller is preemptible, @a thread will be scheduled in.
820 *
821 * - If the caller operates on itself, it lowers its priority below that of
822 * other threads in the system, and the caller is preemptible, the thread of
823 * highest priority will be scheduled in.
824 *
825 * Priority can be assigned in the range of -CONFIG_NUM_COOP_PRIORITIES to
826 * CONFIG_NUM_PREEMPT_PRIORITIES-1, where -CONFIG_NUM_COOP_PRIORITIES is the
827 * highest priority.
828 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500829 * @param thread ID of thread whose priority is to be set.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400830 * @param prio New priority.
831 *
832 * @warning Changing the priority of a thread currently involved in mutex
833 * priority inheritance may result in undefined behavior.
834 *
835 * @return N/A
836 */
Andrew Boie468190a2017-09-29 14:00:48 -0700837__syscall void k_thread_priority_set(k_tid_t thread, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400838
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400839/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500840 * @brief Suspend a thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400841 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500842 * This routine prevents the kernel scheduler from making @a thread the
843 * current thread. All other internal operations on @a thread are still
844 * performed; for example, any timeout it is waiting on keeps ticking,
845 * kernel objects it is waiting on are still handed to it, etc.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400846 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500847 * If @a thread is already suspended, the routine has no effect.
848 *
849 * @param thread ID of thread to suspend.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400850 *
851 * @return N/A
852 */
Andrew Boie468190a2017-09-29 14:00:48 -0700853__syscall void k_thread_suspend(k_tid_t thread);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400854
855/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500856 * @brief Resume a suspended thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400857 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500858 * This routine allows the kernel scheduler to make @a thread the current
859 * thread, when it is next eligible for that role.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400860 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500861 * If @a thread is not currently suspended, the routine has no effect.
862 *
863 * @param thread ID of thread to resume.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400864 *
865 * @return N/A
866 */
Andrew Boie468190a2017-09-29 14:00:48 -0700867__syscall void k_thread_resume(k_tid_t thread);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400868
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400869/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500870 * @brief Set time-slicing period and scope.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400871 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500872 * This routine specifies how the scheduler will perform time slicing of
873 * preemptible threads.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400874 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500875 * To enable time slicing, @a slice must be non-zero. The scheduler
876 * ensures that no thread runs for more than the specified time limit
877 * before other threads of that priority are given a chance to execute.
878 * Any thread whose priority is higher than @a prio is exempted, and may
David B. Kinder8b986d72017-04-18 15:56:26 -0700879 * execute as long as desired without being preempted due to time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400880 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500881 * Time slicing only limits the maximum amount of time a thread may continuously
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400882 * execute. Once the scheduler selects a thread for execution, there is no
883 * minimum guaranteed time the thread will execute before threads of greater or
884 * equal priority are scheduled.
885 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500886 * When the current thread is the only one of that priority eligible
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400887 * for execution, this routine has no effect; the thread is immediately
888 * rescheduled after the slice period expires.
889 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500890 * To disable timeslicing, set both @a slice and @a prio to zero.
891 *
892 * @param slice Maximum time slice length (in milliseconds).
893 * @param prio Highest thread priority level eligible for time slicing.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400894 *
895 * @return N/A
896 */
Kumar Galacc334c72017-04-21 10:55:34 -0500897extern void k_sched_time_slice_set(s32_t slice, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400898
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400899/**
Allan Stephensc98da842016-11-11 15:45:03 -0500900 * @} end defgroup thread_apis
901 */
902
903/**
904 * @addtogroup isr_apis
905 * @{
906 */
907
908/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500909 * @brief Determine if code is running at interrupt level.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400910 *
Allan Stephensc98da842016-11-11 15:45:03 -0500911 * This routine allows the caller to customize its actions, depending on
912 * whether it is a thread or an ISR.
913 *
914 * @note Can be called by ISRs.
915 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500916 * @return 0 if invoked by a thread.
917 * @return Non-zero if invoked by an ISR.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400918 */
Benjamin Walshc7ba8b12016-11-08 16:12:59 -0500919extern int k_is_in_isr(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -0400920
Benjamin Walsh445830d2016-11-10 15:54:27 -0500921/**
922 * @brief Determine if code is running in a preemptible thread.
923 *
Allan Stephensc98da842016-11-11 15:45:03 -0500924 * This routine allows the caller to customize its actions, depending on
925 * whether it can be preempted by another thread. The routine returns a 'true'
926 * value if all of the following conditions are met:
Benjamin Walsh445830d2016-11-10 15:54:27 -0500927 *
Allan Stephensc98da842016-11-11 15:45:03 -0500928 * - The code is running in a thread, not at ISR.
929 * - The thread's priority is in the preemptible range.
930 * - The thread has not locked the scheduler.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500931 *
Allan Stephensc98da842016-11-11 15:45:03 -0500932 * @note Can be called by ISRs.
933 *
934 * @return 0 if invoked by an ISR or by a cooperative thread.
Benjamin Walsh445830d2016-11-10 15:54:27 -0500935 * @return Non-zero if invoked by a preemptible thread.
936 */
Andrew Boie468190a2017-09-29 14:00:48 -0700937__syscall int k_is_preempt_thread(void);
Benjamin Walsh445830d2016-11-10 15:54:27 -0500938
Allan Stephensc98da842016-11-11 15:45:03 -0500939/**
940 * @} end addtogroup isr_apis
941 */
942
943/**
944 * @addtogroup thread_apis
945 * @{
946 */
947
948/**
949 * @brief Lock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500950 *
Allan Stephensc98da842016-11-11 15:45:03 -0500951 * This routine prevents the current thread from being preempted by another
952 * thread by instructing the scheduler to treat it as a cooperative thread.
953 * If the thread subsequently performs an operation that makes it unready,
954 * it will be context switched out in the normal manner. When the thread
955 * again becomes the current thread, its non-preemptible status is maintained.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500956 *
Allan Stephensc98da842016-11-11 15:45:03 -0500957 * This routine can be called recursively.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500958 *
Allan Stephensc98da842016-11-11 15:45:03 -0500959 * @note k_sched_lock() and k_sched_unlock() should normally be used
960 * when the operation being performed can be safely interrupted by ISRs.
961 * However, if the amount of processing involved is very small, better
962 * performance may be obtained by using irq_lock() and irq_unlock().
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500963 *
964 * @return N/A
965 */
966extern void k_sched_lock(void);
967
Allan Stephensc98da842016-11-11 15:45:03 -0500968/**
969 * @brief Unlock the scheduler.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500970 *
Allan Stephensc98da842016-11-11 15:45:03 -0500971 * This routine reverses the effect of a previous call to k_sched_lock().
972 * A thread must call the routine once for each time it called k_sched_lock()
973 * before the thread becomes preemptible.
Benjamin Walshd7ad1762016-11-10 14:46:58 -0500974 *
975 * @return N/A
976 */
977extern void k_sched_unlock(void);
978
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400979/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500980 * @brief Set current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400981 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500982 * This routine sets the custom data for the current thread to @ value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400983 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500984 * Custom data is not used by the kernel itself, and is freely available
985 * for a thread to use as it sees fit. It can be used as a framework
986 * upon which to build thread-local storage.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400987 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500988 * @param value New custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400989 *
990 * @return N/A
991 */
Andrew Boie468190a2017-09-29 14:00:48 -0700992__syscall void k_thread_custom_data_set(void *value);
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400993
994/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500995 * @brief Get current thread's custom data.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400996 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500997 * This routine returns the custom data for the current thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -0400998 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -0500999 * @return Current custom data value.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001000 */
Andrew Boie468190a2017-09-29 14:00:48 -07001001__syscall void *k_thread_custom_data_get(void);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001002
1003/**
Allan Stephensc98da842016-11-11 15:45:03 -05001004 * @} end addtogroup thread_apis
1005 */
1006
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001007#include <sys_clock.h>
1008
Allan Stephensc2f15a42016-11-17 12:24:22 -05001009/**
1010 * @addtogroup clock_apis
1011 * @{
1012 */
1013
1014/**
1015 * @brief Generate null timeout delay.
1016 *
1017 * This macro generates a timeout delay that that instructs a kernel API
1018 * not to wait if the requested operation cannot be performed immediately.
1019 *
1020 * @return Timeout delay value.
1021 */
1022#define K_NO_WAIT 0
1023
1024/**
1025 * @brief Generate timeout delay from milliseconds.
1026 *
1027 * This macro generates a timeout delay that that instructs a kernel API
1028 * to wait up to @a ms milliseconds to perform the requested operation.
1029 *
1030 * @param ms Duration in milliseconds.
1031 *
1032 * @return Timeout delay value.
1033 */
Johan Hedberg14471692016-11-13 10:52:15 +02001034#define K_MSEC(ms) (ms)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001035
1036/**
1037 * @brief Generate timeout delay from seconds.
1038 *
1039 * This macro generates a timeout delay that that instructs a kernel API
1040 * to wait up to @a s seconds to perform the requested operation.
1041 *
1042 * @param s Duration in seconds.
1043 *
1044 * @return Timeout delay value.
1045 */
Johan Hedberg14471692016-11-13 10:52:15 +02001046#define K_SECONDS(s) K_MSEC((s) * MSEC_PER_SEC)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001047
1048/**
1049 * @brief Generate timeout delay from minutes.
1050 *
1051 * This macro generates a timeout delay that that instructs a kernel API
1052 * to wait up to @a m minutes to perform the requested operation.
1053 *
1054 * @param m Duration in minutes.
1055 *
1056 * @return Timeout delay value.
1057 */
Johan Hedberg14471692016-11-13 10:52:15 +02001058#define K_MINUTES(m) K_SECONDS((m) * 60)
Allan Stephensc2f15a42016-11-17 12:24:22 -05001059
1060/**
1061 * @brief Generate timeout delay from hours.
1062 *
1063 * This macro generates a timeout delay that that instructs a kernel API
1064 * to wait up to @a h hours to perform the requested operation.
1065 *
1066 * @param h Duration in hours.
1067 *
1068 * @return Timeout delay value.
1069 */
Johan Hedberg14471692016-11-13 10:52:15 +02001070#define K_HOURS(h) K_MINUTES((h) * 60)
1071
Allan Stephensc98da842016-11-11 15:45:03 -05001072/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001073 * @brief Generate infinite timeout delay.
1074 *
1075 * This macro generates a timeout delay that that instructs a kernel API
1076 * to wait as long as necessary to perform the requested operation.
1077 *
1078 * @return Timeout delay value.
1079 */
1080#define K_FOREVER (-1)
1081
1082/**
1083 * @} end addtogroup clock_apis
1084 */
1085
1086/**
Allan Stephensc98da842016-11-11 15:45:03 -05001087 * @cond INTERNAL_HIDDEN
1088 */
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001089
Benjamin Walsh62092182016-12-20 14:39:08 -05001090/* kernel clocks */
1091
1092#if (sys_clock_ticks_per_sec == 1000) || \
1093 (sys_clock_ticks_per_sec == 500) || \
1094 (sys_clock_ticks_per_sec == 250) || \
1095 (sys_clock_ticks_per_sec == 125) || \
1096 (sys_clock_ticks_per_sec == 100) || \
1097 (sys_clock_ticks_per_sec == 50) || \
1098 (sys_clock_ticks_per_sec == 25) || \
1099 (sys_clock_ticks_per_sec == 20) || \
1100 (sys_clock_ticks_per_sec == 10) || \
1101 (sys_clock_ticks_per_sec == 1)
1102
1103 #define _ms_per_tick (MSEC_PER_SEC / sys_clock_ticks_per_sec)
1104#else
1105 /* yields horrible 64-bit math on many architectures: try to avoid */
1106 #define _NON_OPTIMIZED_TICKS_PER_SEC
1107#endif
1108
1109#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001110extern s32_t _ms_to_ticks(s32_t ms);
Benjamin Walsh62092182016-12-20 14:39:08 -05001111#else
Kumar Galacc334c72017-04-21 10:55:34 -05001112static ALWAYS_INLINE s32_t _ms_to_ticks(s32_t ms)
Benjamin Walsh62092182016-12-20 14:39:08 -05001113{
Kumar Galacc334c72017-04-21 10:55:34 -05001114 return (s32_t)ceiling_fraction((u32_t)ms, _ms_per_tick);
Benjamin Walsh62092182016-12-20 14:39:08 -05001115}
1116#endif
1117
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001118/* added tick needed to account for tick in progress */
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001119#ifdef CONFIG_TICKLESS_KERNEL
1120#define _TICK_ALIGN 0
1121#else
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001122#define _TICK_ALIGN 1
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001123#endif
Allan Stephens6c98c4d2016-10-17 14:34:53 -05001124
Kumar Galacc334c72017-04-21 10:55:34 -05001125static inline s64_t __ticks_to_ms(s64_t ticks)
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001126{
Benjamin Walsh62092182016-12-20 14:39:08 -05001127#ifdef CONFIG_SYS_CLOCK_EXISTS
1128
1129#ifdef _NON_OPTIMIZED_TICKS_PER_SEC
Kumar Galacc334c72017-04-21 10:55:34 -05001130 return (MSEC_PER_SEC * (u64_t)ticks) / sys_clock_ticks_per_sec;
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001131#else
Kumar Galacc334c72017-04-21 10:55:34 -05001132 return (u64_t)ticks * _ms_per_tick;
Benjamin Walsh62092182016-12-20 14:39:08 -05001133#endif
1134
1135#else
Benjamin Walsh57d55dc2016-10-04 16:58:08 -04001136 __ASSERT(ticks == 0, "");
1137 return 0;
1138#endif
Benjamin Walsha9604bd2016-09-21 11:05:56 -04001139}
1140
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001141struct k_timer {
1142 /*
1143 * _timeout structure must be first here if we want to use
1144 * dynamic timer allocation. timeout.node is used in the double-linked
1145 * list of free timers
1146 */
1147 struct _timeout timeout;
1148
Allan Stephens45bfa372016-10-12 12:39:42 -05001149 /* wait queue for the (single) thread waiting on this timer */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001150 _wait_q_t wait_q;
1151
1152 /* runs in ISR context */
Allan Stephens45bfa372016-10-12 12:39:42 -05001153 void (*expiry_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001154
1155 /* runs in the context of the thread that calls k_timer_stop() */
Allan Stephens45bfa372016-10-12 12:39:42 -05001156 void (*stop_fn)(struct k_timer *);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001157
1158 /* timer period */
Kumar Galacc334c72017-04-21 10:55:34 -05001159 s32_t period;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001160
Allan Stephens45bfa372016-10-12 12:39:42 -05001161 /* timer status */
Kumar Galacc334c72017-04-21 10:55:34 -05001162 u32_t status;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001163
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001164 /* user-specific data, also used to support legacy features */
1165 void *user_data;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001166
Anas Nashif2f203c22016-12-18 06:57:45 -05001167 _OBJECT_TRACING_NEXT_PTR(k_timer);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001168};
1169
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001170#define _K_TIMER_INITIALIZER(obj, expiry, stop) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001171 { \
Benjamin Walshd211a522016-12-06 11:44:01 -05001172 .timeout.delta_ticks_from_prev = _INACTIVE, \
Allan Stephens1342adb2016-11-03 13:54:53 -05001173 .timeout.wait_q = NULL, \
1174 .timeout.thread = NULL, \
1175 .timeout.func = _timer_expiration_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001176 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Allan Stephens1342adb2016-11-03 13:54:53 -05001177 .expiry_fn = expiry, \
1178 .stop_fn = stop, \
1179 .status = 0, \
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001180 .user_data = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05001181 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001182 }
1183
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001184#define K_TIMER_INITIALIZER DEPRECATED_MACRO _K_TIMER_INITIALIZER
1185
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001186/**
Allan Stephensc98da842016-11-11 15:45:03 -05001187 * INTERNAL_HIDDEN @endcond
1188 */
1189
1190/**
1191 * @defgroup timer_apis Timer APIs
1192 * @ingroup kernel_apis
1193 * @{
1194 */
1195
1196/**
Allan Stephens5eceb852016-11-16 10:16:30 -05001197 * @typedef k_timer_expiry_t
1198 * @brief Timer expiry function type.
1199 *
1200 * A timer's expiry function is executed by the system clock interrupt handler
1201 * each time the timer expires. The expiry function is optional, and is only
1202 * invoked if the timer has been initialized with one.
1203 *
1204 * @param timer Address of timer.
1205 *
1206 * @return N/A
1207 */
1208typedef void (*k_timer_expiry_t)(struct k_timer *timer);
1209
1210/**
1211 * @typedef k_timer_stop_t
1212 * @brief Timer stop function type.
1213 *
1214 * A timer's stop function is executed if the timer is stopped prematurely.
1215 * The function runs in the context of the thread that stops the timer.
1216 * The stop function is optional, and is only invoked if the timer has been
1217 * initialized with one.
1218 *
1219 * @param timer Address of timer.
1220 *
1221 * @return N/A
1222 */
1223typedef void (*k_timer_stop_t)(struct k_timer *timer);
1224
1225/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001226 * @brief Statically define and initialize a timer.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001227 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001228 * The timer can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001229 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001230 * @code extern struct k_timer <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001231 *
1232 * @param name Name of the timer variable.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001233 * @param expiry_fn Function to invoke each time the timer expires.
1234 * @param stop_fn Function to invoke if the timer is stopped while running.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001235 */
Allan Stephens1342adb2016-11-03 13:54:53 -05001236#define K_TIMER_DEFINE(name, expiry_fn, stop_fn) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001237 struct k_timer name \
1238 __in_section(_k_timer, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001239 _K_TIMER_INITIALIZER(name, expiry_fn, stop_fn)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001240
Allan Stephens45bfa372016-10-12 12:39:42 -05001241/**
1242 * @brief Initialize a timer.
1243 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001244 * This routine initializes a timer, prior to its first use.
Allan Stephens45bfa372016-10-12 12:39:42 -05001245 *
1246 * @param timer Address of timer.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001247 * @param expiry_fn Function to invoke each time the timer expires.
1248 * @param stop_fn Function to invoke if the timer is stopped while running.
Allan Stephens45bfa372016-10-12 12:39:42 -05001249 *
1250 * @return N/A
1251 */
1252extern void k_timer_init(struct k_timer *timer,
Allan Stephens5eceb852016-11-16 10:16:30 -05001253 k_timer_expiry_t expiry_fn,
1254 k_timer_stop_t stop_fn);
Andy Ross8d8b2ac2016-09-23 10:08:54 -07001255
Allan Stephens45bfa372016-10-12 12:39:42 -05001256/**
1257 * @brief Start a timer.
1258 *
1259 * This routine starts a timer, and resets its status to zero. The timer
1260 * begins counting down using the specified duration and period values.
1261 *
1262 * Attempting to start a timer that is already running is permitted.
1263 * The timer's status is reset to zero and the timer begins counting down
1264 * using the new duration and period values.
1265 *
1266 * @param timer Address of timer.
1267 * @param duration Initial timer duration (in milliseconds).
1268 * @param period Timer period (in milliseconds).
1269 *
1270 * @return N/A
1271 */
Andrew Boiea354d492017-09-29 16:22:28 -07001272__syscall void k_timer_start(struct k_timer *timer,
1273 s32_t duration, s32_t period);
Allan Stephens45bfa372016-10-12 12:39:42 -05001274
1275/**
1276 * @brief Stop a timer.
1277 *
1278 * This routine stops a running timer prematurely. The timer's stop function,
1279 * if one exists, is invoked by the caller.
1280 *
1281 * Attempting to stop a timer that is not running is permitted, but has no
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001282 * effect on the timer.
Allan Stephens45bfa372016-10-12 12:39:42 -05001283 *
Anas Nashif4fb12ae2017-02-01 20:06:55 -05001284 * @note Can be called by ISRs. The stop handler has to be callable from ISRs
1285 * if @a k_timer_stop is to be called from ISRs.
1286 *
Allan Stephens45bfa372016-10-12 12:39:42 -05001287 * @param timer Address of timer.
1288 *
1289 * @return N/A
1290 */
Andrew Boiea354d492017-09-29 16:22:28 -07001291__syscall void k_timer_stop(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001292
1293/**
1294 * @brief Read timer status.
1295 *
1296 * This routine reads the timer's status, which indicates the number of times
1297 * it has expired since its status was last read.
1298 *
1299 * Calling this routine resets the timer's status to zero.
1300 *
1301 * @param timer Address of timer.
1302 *
1303 * @return Timer status.
1304 */
Andrew Boiea354d492017-09-29 16:22:28 -07001305__syscall u32_t k_timer_status_get(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001306
1307/**
1308 * @brief Synchronize thread to timer expiration.
1309 *
1310 * This routine blocks the calling thread until the timer's status is non-zero
1311 * (indicating that it has expired at least once since it was last examined)
1312 * or the timer is stopped. If the timer status is already non-zero,
1313 * or the timer is already stopped, the caller continues without waiting.
1314 *
1315 * Calling this routine resets the timer's status to zero.
1316 *
1317 * This routine must not be used by interrupt handlers, since they are not
1318 * allowed to block.
1319 *
1320 * @param timer Address of timer.
1321 *
1322 * @return Timer status.
1323 */
Andrew Boiea354d492017-09-29 16:22:28 -07001324__syscall u32_t k_timer_status_sync(struct k_timer *timer);
Allan Stephens45bfa372016-10-12 12:39:42 -05001325
1326/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001327 * @brief Get time remaining before a timer next expires.
Allan Stephens45bfa372016-10-12 12:39:42 -05001328 *
1329 * This routine computes the (approximate) time remaining before a running
1330 * timer next expires. If the timer is not running, it returns zero.
1331 *
1332 * @param timer Address of timer.
1333 *
1334 * @return Remaining time (in milliseconds).
1335 */
Andrew Boiea354d492017-09-29 16:22:28 -07001336__syscall s32_t k_timer_remaining_get(struct k_timer *timer);
1337
1338static inline s32_t _impl_k_timer_remaining_get(struct k_timer *timer)
Johan Hedbergf99ad3f2016-12-09 10:39:49 +02001339{
1340 return _timeout_remaining_get(&timer->timeout);
1341}
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001342
Allan Stephensc98da842016-11-11 15:45:03 -05001343/**
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001344 * @brief Associate user-specific data with a timer.
1345 *
1346 * This routine records the @a user_data with the @a timer, to be retrieved
1347 * later.
1348 *
1349 * It can be used e.g. in a timer handler shared across multiple subsystems to
1350 * retrieve data specific to the subsystem this timer is associated with.
1351 *
1352 * @param timer Address of timer.
1353 * @param user_data User data to associate with the timer.
1354 *
1355 * @return N/A
1356 */
Andrew Boiea354d492017-09-29 16:22:28 -07001357__syscall void k_timer_user_data_set(struct k_timer *timer, void *user_data);
1358
1359static inline void _impl_k_timer_user_data_set(struct k_timer *timer,
1360 void *user_data)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001361{
1362 timer->user_data = user_data;
1363}
1364
1365/**
1366 * @brief Retrieve the user-specific data from a timer.
1367 *
1368 * @param timer Address of timer.
1369 *
1370 * @return The user data.
1371 */
Andrew Boiea354d492017-09-29 16:22:28 -07001372__syscall void *k_timer_user_data_get(struct k_timer *timer);
1373
1374static inline void *_impl_k_timer_user_data_get(struct k_timer *timer)
Benjamin Walshe4e98f92017-01-12 19:38:53 -05001375{
1376 return timer->user_data;
1377}
1378
1379/**
Allan Stephensc98da842016-11-11 15:45:03 -05001380 * @} end defgroup timer_apis
1381 */
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001382
Allan Stephensc98da842016-11-11 15:45:03 -05001383/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001384 * @addtogroup clock_apis
Allan Stephensc98da842016-11-11 15:45:03 -05001385 * @{
1386 */
Allan Stephens45bfa372016-10-12 12:39:42 -05001387
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001388/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001389 * @brief Get system uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001390 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001391 * This routine returns the elapsed time since the system booted,
1392 * in milliseconds.
1393 *
1394 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001395 */
Andrew Boiea73d3732017-10-08 12:23:55 -07001396__syscall s64_t k_uptime_get(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001397
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001398#ifdef CONFIG_TICKLESS_KERNEL
1399/**
1400 * @brief Enable clock always on in tickless kernel
1401 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001402 * This routine enables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001403 * there are no timer events programmed in tickless kernel
1404 * scheduling. This is necessary if the clock is used to track
1405 * passage of time.
1406 *
1407 * @retval prev_status Previous status of always on flag
1408 */
1409static inline int k_enable_sys_clock_always_on(void)
1410{
1411 int prev_status = _sys_clock_always_on;
1412
1413 _sys_clock_always_on = 1;
1414 _enable_sys_clock();
1415
1416 return prev_status;
1417}
1418
1419/**
1420 * @brief Disable clock always on in tickless kernel
1421 *
David B. Kinderfc5f2b32017-05-02 17:21:56 -07001422 * This routine disables keeping the clock running when
Ramesh Thomas89ffd442017-02-05 19:37:19 -08001423 * there are no timer events programmed in tickless kernel
1424 * scheduling. To save power, this routine should be called
1425 * immediately when clock is not used to track time.
1426 */
1427static inline void k_disable_sys_clock_always_on(void)
1428{
1429 _sys_clock_always_on = 0;
1430}
1431#else
1432#define k_enable_sys_clock_always_on() do { } while ((0))
1433#define k_disable_sys_clock_always_on() do { } while ((0))
1434#endif
1435
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001436/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001437 * @brief Get system uptime (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001439 * This routine returns the lower 32-bits of the elapsed time since the system
1440 * booted, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001441 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001442 * This routine can be more efficient than k_uptime_get(), as it reduces the
1443 * need for interrupt locking and 64-bit math. However, the 32-bit result
1444 * cannot hold a system uptime time larger than approximately 50 days, so the
1445 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001446 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001447 * @return Current uptime.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001448 */
Andrew Boie76c04a22017-09-27 14:45:10 -07001449__syscall u32_t k_uptime_get_32(void);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001450
1451/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001452 * @brief Get elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001453 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001454 * This routine computes the elapsed time between the current system uptime
1455 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001456 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001457 * @param reftime Pointer to a reference time, which is updated to the current
1458 * uptime upon return.
1459 *
1460 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001461 */
Kumar Galacc334c72017-04-21 10:55:34 -05001462extern s64_t k_uptime_delta(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001463
1464/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001465 * @brief Get elapsed time (32-bit version).
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001466 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001467 * This routine computes the elapsed time between the current system uptime
1468 * and an earlier reference time, in milliseconds.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001469 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001470 * This routine can be more efficient than k_uptime_delta(), as it reduces the
1471 * need for interrupt locking and 64-bit math. However, the 32-bit result
1472 * cannot hold an elapsed time larger than approximately 50 days, so the
1473 * caller must handle possible rollovers.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001474 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001475 * @param reftime Pointer to a reference time, which is updated to the current
1476 * uptime upon return.
1477 *
1478 * @return Elapsed time.
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001479 */
Kumar Galacc334c72017-04-21 10:55:34 -05001480extern u32_t k_uptime_delta_32(s64_t *reftime);
Benjamin Walshba5ddc12016-09-21 16:01:22 -04001481
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001482/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001483 * @brief Read the hardware clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001484 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001485 * This routine returns the current time, as measured by the system's hardware
1486 * clock.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001487 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001488 * @return Current hardware clock up-counter (in cycles).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001489 */
Andrew Boiee08d07c2017-02-15 13:40:17 -08001490#define k_cycle_get_32() _arch_k_cycle_get_32()
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001491
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001492/**
Allan Stephensc2f15a42016-11-17 12:24:22 -05001493 * @} end addtogroup clock_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001494 */
1495
Allan Stephensc98da842016-11-11 15:45:03 -05001496/**
1497 * @cond INTERNAL_HIDDEN
1498 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001499
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001500struct k_queue {
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001501 sys_slist_t data_q;
Luiz Augusto von Dentz84db6412017-07-13 12:43:59 +03001502 union {
1503 _wait_q_t wait_q;
1504
1505 _POLL_EVENT;
1506 };
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001507
1508 _OBJECT_TRACING_NEXT_PTR(k_queue);
1509};
1510
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001511#define _K_QUEUE_INITIALIZER(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001512 { \
1513 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
1514 .data_q = SYS_SLIST_STATIC_INIT(&obj.data_q), \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03001515 _POLL_EVENT_OBJ_INIT(obj) \
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001516 _OBJECT_TRACING_INIT \
1517 }
1518
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001519#define K_QUEUE_INITIALIZER DEPRECATED_MACRO _K_QUEUE_INITIALIZER
1520
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001521/**
1522 * INTERNAL_HIDDEN @endcond
1523 */
1524
1525/**
1526 * @defgroup queue_apis Queue APIs
1527 * @ingroup kernel_apis
1528 * @{
1529 */
1530
1531/**
1532 * @brief Initialize a queue.
1533 *
1534 * This routine initializes a queue object, prior to its first use.
1535 *
1536 * @param queue Address of the queue.
1537 *
1538 * @return N/A
1539 */
1540extern void k_queue_init(struct k_queue *queue);
1541
1542/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001543 * @brief Cancel waiting on a queue.
1544 *
1545 * This routine causes first thread pending on @a queue, if any, to
1546 * return from k_queue_get() call with NULL value (as if timeout expired).
1547 *
1548 * @note Can be called by ISRs.
1549 *
1550 * @param queue Address of the queue.
1551 *
1552 * @return N/A
1553 */
1554extern void k_queue_cancel_wait(struct k_queue *queue);
1555
1556/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001557 * @brief Append an element to the end of a queue.
1558 *
1559 * This routine appends a data item to @a queue. A queue data item must be
1560 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1561 * reserved for the kernel's use.
1562 *
1563 * @note Can be called by ISRs.
1564 *
1565 * @param queue Address of the queue.
1566 * @param data Address of the data item.
1567 *
1568 * @return N/A
1569 */
1570extern void k_queue_append(struct k_queue *queue, void *data);
1571
1572/**
1573 * @brief Prepend an element to a queue.
1574 *
1575 * This routine prepends a data item to @a queue. A queue data item must be
1576 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1577 * reserved for the kernel's use.
1578 *
1579 * @note Can be called by ISRs.
1580 *
1581 * @param queue Address of the queue.
1582 * @param data Address of the data item.
1583 *
1584 * @return N/A
1585 */
1586extern void k_queue_prepend(struct k_queue *queue, void *data);
1587
1588/**
1589 * @brief Inserts an element to a queue.
1590 *
1591 * This routine inserts a data item to @a queue after previous item. A queue
1592 * data item must be aligned on a 4-byte boundary, and the first 32 bits of the
1593 * item are reserved for the kernel's use.
1594 *
1595 * @note Can be called by ISRs.
1596 *
1597 * @param queue Address of the queue.
1598 * @param prev Address of the previous data item.
1599 * @param data Address of the data item.
1600 *
1601 * @return N/A
1602 */
1603extern void k_queue_insert(struct k_queue *queue, void *prev, void *data);
1604
1605/**
1606 * @brief Atomically append a list of elements to a queue.
1607 *
1608 * This routine adds a list of data items to @a queue in one operation.
1609 * The data items must be in a singly-linked list, with the first 32 bits
1610 * in each data item pointing to the next data item; the list must be
1611 * NULL-terminated.
1612 *
1613 * @note Can be called by ISRs.
1614 *
1615 * @param queue Address of the queue.
1616 * @param head Pointer to first node in singly-linked list.
1617 * @param tail Pointer to last node in singly-linked list.
1618 *
1619 * @return N/A
1620 */
1621extern void k_queue_append_list(struct k_queue *queue, void *head, void *tail);
1622
1623/**
1624 * @brief Atomically add a list of elements to a queue.
1625 *
1626 * This routine adds a list of data items to @a queue in one operation.
1627 * The data items must be in a singly-linked list implemented using a
1628 * sys_slist_t object. Upon completion, the original list is empty.
1629 *
1630 * @note Can be called by ISRs.
1631 *
1632 * @param queue Address of the queue.
1633 * @param list Pointer to sys_slist_t object.
1634 *
1635 * @return N/A
1636 */
1637extern void k_queue_merge_slist(struct k_queue *queue, sys_slist_t *list);
1638
1639/**
1640 * @brief Get an element from a queue.
1641 *
1642 * This routine removes first data item from @a queue. The first 32 bits of the
1643 * data item are reserved for the kernel's use.
1644 *
1645 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1646 *
1647 * @param queue Address of the queue.
1648 * @param timeout Waiting period to obtain a data item (in milliseconds),
1649 * or one of the special values K_NO_WAIT and K_FOREVER.
1650 *
1651 * @return Address of the data item if successful; NULL if returned
1652 * without waiting, or waiting period timed out.
1653 */
Kumar Galacc334c72017-04-21 10:55:34 -05001654extern void *k_queue_get(struct k_queue *queue, s32_t timeout);
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001655
1656/**
Luiz Augusto von Dentz50b93772017-07-03 16:52:45 +03001657 * @brief Remove an element from a queue.
1658 *
1659 * This routine removes data item from @a queue. The first 32 bits of the
1660 * data item are reserved for the kernel's use. Removing elements from k_queue
1661 * rely on sys_slist_find_and_remove which is not a constant time operation.
1662 *
1663 * @note Can be called by ISRs
1664 *
1665 * @param queue Address of the queue.
1666 * @param data Address of the data item.
1667 *
1668 * @return true if data item was removed
1669 */
1670static inline bool k_queue_remove(struct k_queue *queue, void *data)
1671{
1672 return sys_slist_find_and_remove(&queue->data_q, (sys_snode_t *)data);
1673}
1674
1675/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001676 * @brief Query a queue to see if it has data available.
1677 *
1678 * Note that the data might be already gone by the time this function returns
1679 * if other threads are also trying to read from the queue.
1680 *
1681 * @note Can be called by ISRs.
1682 *
1683 * @param queue Address of the queue.
1684 *
1685 * @return Non-zero if the queue is empty.
1686 * @return 0 if data is available.
1687 */
1688static inline int k_queue_is_empty(struct k_queue *queue)
1689{
1690 return (int)sys_slist_is_empty(&queue->data_q);
1691}
1692
1693/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001694 * @brief Peek element at the head of queue.
1695 *
1696 * Return element from the head of queue without removing it.
1697 *
1698 * @param queue Address of the queue.
1699 *
1700 * @return Head element, or NULL if queue is empty.
1701 */
1702static inline void *k_queue_peek_head(struct k_queue *queue)
1703{
1704 return sys_slist_peek_head(&queue->data_q);
1705}
1706
1707/**
1708 * @brief Peek element at the tail of queue.
1709 *
1710 * Return element from the tail of queue without removing it.
1711 *
1712 * @param queue Address of the queue.
1713 *
1714 * @return Tail element, or NULL if queue is empty.
1715 */
1716static inline void *k_queue_peek_tail(struct k_queue *queue)
1717{
1718 return sys_slist_peek_tail(&queue->data_q);
1719}
1720
1721/**
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001722 * @brief Statically define and initialize a queue.
1723 *
1724 * The queue can be accessed outside the module where it is defined using:
1725 *
1726 * @code extern struct k_queue <name>; @endcode
1727 *
1728 * @param name Name of the queue.
1729 */
1730#define K_QUEUE_DEFINE(name) \
1731 struct k_queue name \
1732 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001733 _K_QUEUE_INITIALIZER(name)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02001734
1735/**
1736 * @} end defgroup queue_apis
1737 */
1738
1739/**
1740 * @cond INTERNAL_HIDDEN
1741 */
1742
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001743struct k_fifo {
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001744 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001745};
1746
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001747#define _K_FIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001748 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001749 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001750 }
1751
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001752#define K_FIFO_INITIALIZER DEPRECATED_MACRO _K_FIFO_INITIALIZER
1753
Allan Stephensc98da842016-11-11 15:45:03 -05001754/**
1755 * INTERNAL_HIDDEN @endcond
1756 */
1757
1758/**
1759 * @defgroup fifo_apis Fifo APIs
1760 * @ingroup kernel_apis
1761 * @{
1762 */
1763
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001764/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001765 * @brief Initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001766 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001767 * This routine initializes a fifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001768 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001769 * @param fifo Address of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001770 *
1771 * @return N/A
1772 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001773#define k_fifo_init(fifo) \
1774 k_queue_init((struct k_queue *) fifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001775
1776/**
Paul Sokolovsky3f507072017-04-25 17:54:31 +03001777 * @brief Cancel waiting on a fifo.
1778 *
1779 * This routine causes first thread pending on @a fifo, if any, to
1780 * return from k_fifo_get() call with NULL value (as if timeout
1781 * expired).
1782 *
1783 * @note Can be called by ISRs.
1784 *
1785 * @param fifo Address of the fifo.
1786 *
1787 * @return N/A
1788 */
1789#define k_fifo_cancel_wait(fifo) \
1790 k_queue_cancel_wait((struct k_queue *) fifo)
1791
1792/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001793 * @brief Add an element to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001794 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001795 * This routine adds a data item to @a fifo. A fifo data item must be
1796 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1797 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001798 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001799 * @note Can be called by ISRs.
1800 *
1801 * @param fifo Address of the fifo.
1802 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001803 *
1804 * @return N/A
1805 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001806#define k_fifo_put(fifo, data) \
1807 k_queue_append((struct k_queue *) fifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001808
1809/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001810 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001811 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001812 * This routine adds a list of data items to @a fifo in one operation.
1813 * The data items must be in a singly-linked list, with the first 32 bits
1814 * each data item pointing to the next data item; the list must be
1815 * NULL-terminated.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001816 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001817 * @note Can be called by ISRs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001818 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001819 * @param fifo Address of the fifo.
1820 * @param head Pointer to first node in singly-linked list.
1821 * @param tail Pointer to last node in singly-linked list.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001822 *
1823 * @return N/A
1824 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001825#define k_fifo_put_list(fifo, head, tail) \
1826 k_queue_append_list((struct k_queue *) fifo, head, tail)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001827
1828/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001829 * @brief Atomically add a list of elements to a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001830 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001831 * This routine adds a list of data items to @a fifo in one operation.
1832 * The data items must be in a singly-linked list implemented using a
1833 * sys_slist_t object. Upon completion, the sys_slist_t object is invalid
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001834 * and must be re-initialized via sys_slist_init().
1835 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001836 * @note Can be called by ISRs.
1837 *
1838 * @param fifo Address of the fifo.
1839 * @param list Pointer to sys_slist_t object.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001840 *
1841 * @return N/A
1842 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001843#define k_fifo_put_slist(fifo, list) \
1844 k_queue_merge_slist((struct k_queue *) fifo, list)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001845
1846/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001847 * @brief Get an element from a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001849 * This routine removes a data item from @a fifo in a "first in, first out"
1850 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001851 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001852 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1853 *
1854 * @param fifo Address of the fifo.
1855 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001856 * or one of the special values K_NO_WAIT and K_FOREVER.
1857 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001858 * @return Address of the data item if successful; NULL if returned
1859 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001860 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001861#define k_fifo_get(fifo, timeout) \
1862 k_queue_get((struct k_queue *) fifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001863
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001864/**
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001865 * @brief Query a fifo to see if it has data available.
1866 *
1867 * Note that the data might be already gone by the time this function returns
1868 * if other threads is also trying to read from the fifo.
1869 *
1870 * @note Can be called by ISRs.
1871 *
1872 * @param fifo Address of the fifo.
1873 *
1874 * @return Non-zero if the fifo is empty.
1875 * @return 0 if data is available.
1876 */
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001877#define k_fifo_is_empty(fifo) \
1878 k_queue_is_empty((struct k_queue *) fifo)
Benjamin Walsh39b80d82017-01-28 10:06:07 -05001879
1880/**
Paul Sokolovsky16bb3ec2017-06-08 17:13:03 +03001881 * @brief Peek element at the head of fifo.
1882 *
1883 * Return element from the head of fifo without removing it. A usecase
1884 * for this is if elements of the fifo are themselves containers. Then
1885 * on each iteration of processing, a head container will be peeked,
1886 * and some data processed out of it, and only if the container is empty,
1887 * it will be completely remove from the fifo.
1888 *
1889 * @param fifo Address of the fifo.
1890 *
1891 * @return Head element, or NULL if the fifo is empty.
1892 */
1893#define k_fifo_peek_head(fifo) \
1894 k_queue_peek_head((struct k_queue *) fifo)
1895
1896/**
1897 * @brief Peek element at the tail of fifo.
1898 *
1899 * Return element from the tail of fifo (without removing it). A usecase
1900 * for this is if elements of the fifo are themselves containers. Then
1901 * it may be useful to add more data to the last container in fifo.
1902 *
1903 * @param fifo Address of the fifo.
1904 *
1905 * @return Tail element, or NULL if fifo is empty.
1906 */
1907#define k_fifo_peek_tail(fifo) \
1908 k_queue_peek_tail((struct k_queue *) fifo)
1909
1910/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001911 * @brief Statically define and initialize a fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001912 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001913 * The fifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001914 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05001915 * @code extern struct k_fifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001916 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001917 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001918 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001919#define K_FIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05001920 struct k_fifo name \
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02001921 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001922 _K_FIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001923
Allan Stephensc98da842016-11-11 15:45:03 -05001924/**
1925 * @} end defgroup fifo_apis
1926 */
1927
1928/**
1929 * @cond INTERNAL_HIDDEN
1930 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001931
1932struct k_lifo {
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001933 struct k_queue _queue;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001934};
1935
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001936#define _K_LIFO_INITIALIZER(obj) \
Allan Stephensc98da842016-11-11 15:45:03 -05001937 { \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001938 ._queue = _K_QUEUE_INITIALIZER(obj._queue) \
Allan Stephensc98da842016-11-11 15:45:03 -05001939 }
1940
Andrew Boie65a9d2a2017-06-27 10:51:23 -07001941#define K_LIFO_INITIALIZER DEPRECATED_MACRO _K_LIFO_INITIALIZER
1942
Allan Stephensc98da842016-11-11 15:45:03 -05001943/**
1944 * INTERNAL_HIDDEN @endcond
1945 */
1946
1947/**
1948 * @defgroup lifo_apis Lifo APIs
1949 * @ingroup kernel_apis
1950 * @{
1951 */
1952
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001953/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001954 * @brief Initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001955 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001956 * This routine initializes a lifo object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001957 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001958 * @param lifo Address of the lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001959 *
1960 * @return N/A
1961 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001962#define k_lifo_init(lifo) \
1963 k_queue_init((struct k_queue *) lifo)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001964
1965/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001966 * @brief Add an element to a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001967 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001968 * This routine adds a data item to @a lifo. A lifo data item must be
1969 * aligned on a 4-byte boundary, and the first 32 bits of the item are
1970 * reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001971 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001972 * @note Can be called by ISRs.
1973 *
1974 * @param lifo Address of the lifo.
1975 * @param data Address of the data item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001976 *
1977 * @return N/A
1978 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001979#define k_lifo_put(lifo, data) \
1980 k_queue_prepend((struct k_queue *) lifo, data)
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001981
1982/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001983 * @brief Get an element from a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001984 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001985 * This routine removes a data item from @a lifo in a "last in, first out"
1986 * manner. The first 32 bits of the data item are reserved for the kernel's use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001987 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05001988 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
1989 *
1990 * @param lifo Address of the lifo.
1991 * @param timeout Waiting period to obtain a data item (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001992 * or one of the special values K_NO_WAIT and K_FOREVER.
1993 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05001994 * @return Address of the data item if successful; NULL if returned
1995 * without waiting, or waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04001996 */
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02001997#define k_lifo_get(lifo, timeout) \
1998 k_queue_get((struct k_queue *) lifo, timeout)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04001999
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002000/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002001 * @brief Statically define and initialize a lifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002002 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002003 * The lifo can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002004 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002005 * @code extern struct k_lifo <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002006 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002007 * @param name Name of the fifo.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002008 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002009#define K_LIFO_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002010 struct k_lifo name \
Luiz Augusto von Dentz0dc4dd42017-02-21 15:49:52 +02002011 __in_section(_k_queue, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002012 _K_LIFO_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002013
Allan Stephensc98da842016-11-11 15:45:03 -05002014/**
2015 * @} end defgroup lifo_apis
2016 */
2017
2018/**
2019 * @cond INTERNAL_HIDDEN
2020 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002021
2022struct k_stack {
2023 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05002024 u32_t *base, *next, *top;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002025
Anas Nashif2f203c22016-12-18 06:57:45 -05002026 _OBJECT_TRACING_NEXT_PTR(k_stack);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002027};
2028
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002029#define _K_STACK_INITIALIZER(obj, stack_buffer, stack_num_entries) \
Allan Stephensc98da842016-11-11 15:45:03 -05002030 { \
2031 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2032 .base = stack_buffer, \
2033 .next = stack_buffer, \
2034 .top = stack_buffer + stack_num_entries, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002035 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002036 }
2037
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002038#define K_STACK_INITIALIZER DEPRECATED_MACRO _K_STACK_INITIALIZER
2039
Allan Stephensc98da842016-11-11 15:45:03 -05002040/**
2041 * INTERNAL_HIDDEN @endcond
2042 */
2043
2044/**
2045 * @defgroup stack_apis Stack APIs
2046 * @ingroup kernel_apis
2047 * @{
2048 */
2049
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002050/**
2051 * @brief Initialize a stack.
2052 *
2053 * This routine initializes a stack object, prior to its first use.
2054 *
2055 * @param stack Address of the stack.
2056 * @param buffer Address of array used to hold stacked values.
2057 * @param num_entries Maximum number of values that can be stacked.
2058 *
2059 * @return N/A
2060 */
Andrew Boiee8734462017-09-29 16:42:07 -07002061__syscall void k_stack_init(struct k_stack *stack,
Andrew Boie8e3e6d02017-10-12 14:01:25 -07002062 u32_t *buffer, unsigned int num_entries);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002063
2064/**
2065 * @brief Push an element onto a stack.
2066 *
2067 * This routine adds a 32-bit value @a data to @a stack.
2068 *
2069 * @note Can be called by ISRs.
2070 *
2071 * @param stack Address of the stack.
2072 * @param data Value to push onto the stack.
2073 *
2074 * @return N/A
2075 */
Andrew Boiee8734462017-09-29 16:42:07 -07002076__syscall void k_stack_push(struct k_stack *stack, u32_t data);
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002077
2078/**
2079 * @brief Pop an element from a stack.
2080 *
2081 * This routine removes a 32-bit value from @a stack in a "last in, first out"
2082 * manner and stores the value in @a data.
2083 *
2084 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2085 *
2086 * @param stack Address of the stack.
2087 * @param data Address of area to hold the value popped from the stack.
2088 * @param timeout Waiting period to obtain a value (in milliseconds),
2089 * or one of the special values K_NO_WAIT and K_FOREVER.
2090 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002091 * @retval 0 Element popped from stack.
2092 * @retval -EBUSY Returned without waiting.
2093 * @retval -EAGAIN Waiting period timed out.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002094 */
Andrew Boiee8734462017-09-29 16:42:07 -07002095__syscall int k_stack_pop(struct k_stack *stack, u32_t *data, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002096
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002097/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002098 * @brief Statically define and initialize a stack
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002099 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002100 * The stack can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002101 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002102 * @code extern struct k_stack <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002103 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002104 * @param name Name of the stack.
2105 * @param stack_num_entries Maximum number of values that can be stacked.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002106 */
Peter Mitsis602e6a82016-10-17 11:48:43 -04002107#define K_STACK_DEFINE(name, stack_num_entries) \
Kumar Galacc334c72017-04-21 10:55:34 -05002108 u32_t __noinit \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002109 _k_stack_buf_##name[stack_num_entries]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002110 struct k_stack name \
2111 __in_section(_k_stack, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002112 _K_STACK_INITIALIZER(name, _k_stack_buf_##name, \
Peter Mitsis602e6a82016-10-17 11:48:43 -04002113 stack_num_entries)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002114
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002115/**
Allan Stephensc98da842016-11-11 15:45:03 -05002116 * @} end defgroup stack_apis
2117 */
2118
Allan Stephens6bba9b02016-11-16 14:56:54 -05002119struct k_work;
2120
Allan Stephensc98da842016-11-11 15:45:03 -05002121/**
2122 * @defgroup workqueue_apis Workqueue Thread APIs
2123 * @ingroup kernel_apis
2124 * @{
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002125 */
2126
Allan Stephens6bba9b02016-11-16 14:56:54 -05002127/**
2128 * @typedef k_work_handler_t
2129 * @brief Work item handler function type.
2130 *
2131 * A work item's handler function is executed by a workqueue's thread
2132 * when the work item is processed by the workqueue.
2133 *
2134 * @param work Address of the work item.
2135 *
2136 * @return N/A
2137 */
2138typedef void (*k_work_handler_t)(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002139
2140/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002141 * @cond INTERNAL_HIDDEN
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002142 */
Allan Stephens6bba9b02016-11-16 14:56:54 -05002143
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002144struct k_work_q {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002145 struct k_queue queue;
Andrew Boied26cf2d2017-03-30 13:07:02 -07002146 struct k_thread thread;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002147};
2148
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002149enum {
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002150 K_WORK_STATE_PENDING, /* Work item pending state */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002151};
2152
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002153struct k_work {
Luiz Augusto von Dentzadb581b2017-07-03 19:09:44 +03002154 void *_reserved; /* Used by k_queue implementation. */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002155 k_work_handler_t handler;
2156 atomic_t flags[1];
2157};
2158
Allan Stephens6bba9b02016-11-16 14:56:54 -05002159struct k_delayed_work {
2160 struct k_work work;
2161 struct _timeout timeout;
2162 struct k_work_q *work_q;
2163};
2164
2165extern struct k_work_q k_sys_work_q;
2166
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002167/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002168 * INTERNAL_HIDDEN @endcond
2169 */
2170
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002171#define _K_WORK_INITIALIZER(work_handler) \
2172 { \
2173 ._reserved = NULL, \
2174 .handler = work_handler, \
2175 .flags = { 0 } \
2176 }
2177
2178#define K_WORK_INITIALIZER DEPRECATED_MACRO _K_WORK_INITIALIZER
2179
Allan Stephens6bba9b02016-11-16 14:56:54 -05002180/**
2181 * @brief Initialize a statically-defined work item.
2182 *
2183 * This macro can be used to initialize a statically-defined workqueue work
2184 * item, prior to its first use. For example,
2185 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002186 * @code static K_WORK_DEFINE(<work>, <work_handler>); @endcode
Allan Stephens6bba9b02016-11-16 14:56:54 -05002187 *
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002188 * @param work Symbol name for work item object
Allan Stephens6bba9b02016-11-16 14:56:54 -05002189 * @param work_handler Function to invoke each time work item is processed.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002190 */
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002191#define K_WORK_DEFINE(work, work_handler) \
2192 struct k_work work \
2193 __in_section(_k_work, static, work) = \
2194 _K_WORK_INITIALIZER(work_handler)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002195
2196/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002197 * @brief Initialize a work item.
2198 *
2199 * This routine initializes a workqueue work item, prior to its first use.
2200 *
2201 * @param work Address of work item.
2202 * @param handler Function to invoke each time work item is processed.
2203 *
2204 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002205 */
2206static inline void k_work_init(struct k_work *work, k_work_handler_t handler)
2207{
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002208 atomic_clear_bit(work->flags, K_WORK_STATE_PENDING);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002209 work->handler = handler;
Andrew Boie945af952017-08-22 13:15:23 -07002210 _k_object_init(work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002211}
2212
2213/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002214 * @brief Submit a work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002215 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002216 * This routine submits work item @a work to be processed by workqueue
2217 * @a work_q. If the work item is already pending in the workqueue's queue
2218 * as a result of an earlier submission, this routine has no effect on the
2219 * work item. If the work item has already been processed, or is currently
2220 * being processed, its work is considered complete and the work item can be
2221 * resubmitted.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002222 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002223 * @warning
2224 * A submitted work item must not be modified until it has been processed
2225 * by the workqueue.
2226 *
2227 * @note Can be called by ISRs.
2228 *
2229 * @param work_q Address of workqueue.
2230 * @param work Address of work item.
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002231 *
2232 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002233 */
2234static inline void k_work_submit_to_queue(struct k_work_q *work_q,
2235 struct k_work *work)
2236{
Luiz Augusto von Dentz4ab9d322016-09-26 09:39:27 +03002237 if (!atomic_test_and_set_bit(work->flags, K_WORK_STATE_PENDING)) {
Luiz Augusto von Dentzc1fa82b2017-07-03 19:24:10 +03002238 k_queue_append(&work_q->queue, work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002239 }
2240}
2241
2242/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002243 * @brief Check if a work item is pending.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002244 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002245 * This routine indicates if work item @a work is pending in a workqueue's
2246 * queue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002247 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002248 * @note Can be called by ISRs.
2249 *
2250 * @param work Address of work item.
2251 *
2252 * @return 1 if work item is pending, or 0 if it is not pending.
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002253 */
2254static inline int k_work_pending(struct k_work *work)
2255{
Iván Briano9c7b5ea2016-10-04 18:11:05 -03002256 return atomic_test_bit(work->flags, K_WORK_STATE_PENDING);
Luiz Augusto von Dentzee1e99b2016-09-26 09:36:49 +03002257}
2258
2259/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002260 * @brief Start a workqueue.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002261 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002262 * This routine starts workqueue @a work_q. The workqueue spawns its work
2263 * processing thread, which runs forever.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002264 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002265 * @param work_q Address of workqueue.
Andrew Boiedc5d9352017-06-02 12:56:47 -07002266 * @param stack Pointer to work queue thread's stack space, as defined by
2267 * K_THREAD_STACK_DEFINE()
2268 * @param stack_size Size of the work queue thread's stack (in bytes), which
2269 * should either be the same constant passed to
2270 * K_THREAD_STACK_DEFINE() or the value of K_THREAD_STACK_SIZEOF().
Allan Stephens6bba9b02016-11-16 14:56:54 -05002271 * @param prio Priority of the work queue's thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002272 *
2273 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002274 */
Andrew Boie507852a2017-07-25 18:47:07 -07002275extern void k_work_q_start(struct k_work_q *work_q,
Andrew Boiec5c104f2017-10-16 14:46:34 -07002276 k_thread_stack_t *stack,
Benjamin Walsh669360d2016-11-14 16:46:14 -05002277 size_t stack_size, int prio);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002278
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002279/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002280 * @brief Initialize a delayed work item.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002281 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002282 * This routine initializes a workqueue delayed work item, prior to
2283 * its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002284 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002285 * @param work Address of delayed work item.
2286 * @param handler Function to invoke each time work item is processed.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002287 *
2288 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002289 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002290extern void k_delayed_work_init(struct k_delayed_work *work,
2291 k_work_handler_t handler);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002292
2293/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002294 * @brief Submit a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002295 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002296 * This routine schedules work item @a work to be processed by workqueue
2297 * @a work_q after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002298 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002299 * Only when the countdown completes is the work item actually submitted to
2300 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002301 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002302 * Submitting a previously submitted delayed work item that is still
2303 * counting down cancels the existing submission and restarts the countdown
2304 * using the new delay. If the work item is currently pending on the
2305 * workqueue's queue because the countdown has completed it is too late to
2306 * resubmit the item, and resubmission fails without impacting the work item.
2307 * If the work item has already been processed, or is currently being processed,
2308 * its work is considered complete and the work item can be resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002309 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002310 * @warning
2311 * A delayed work item must not be modified until it has been processed
2312 * by the workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002313 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002314 * @note Can be called by ISRs.
2315 *
2316 * @param work_q Address of workqueue.
2317 * @param work Address of delayed work item.
2318 * @param delay Delay before submitting the work item (in milliseconds).
2319 *
2320 * @retval 0 Work item countdown started.
2321 * @retval -EINPROGRESS Work item is already pending.
2322 * @retval -EINVAL Work item is being processed or has completed its work.
2323 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002324 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002325extern int k_delayed_work_submit_to_queue(struct k_work_q *work_q,
2326 struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002327 s32_t delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002328
2329/**
Allan Stephens6bba9b02016-11-16 14:56:54 -05002330 * @brief Cancel a delayed work item.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002331 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002332 * This routine cancels the submission of delayed work item @a work.
David B. Kinder8b986d72017-04-18 15:56:26 -07002333 * A delayed work item can only be canceled while its countdown is still
Allan Stephens6bba9b02016-11-16 14:56:54 -05002334 * underway.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002335 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002336 * @note Can be called by ISRs.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002337 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002338 * @param work Address of delayed work item.
2339 *
David B. Kinder8b986d72017-04-18 15:56:26 -07002340 * @retval 0 Work item countdown canceled.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002341 * @retval -EINPROGRESS Work item is already pending.
2342 * @retval -EINVAL Work item is being processed or has completed its work.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002343 */
Benjamin Walsh72e5a392016-09-30 11:32:33 -04002344extern int k_delayed_work_cancel(struct k_delayed_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002345
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002346/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002347 * @brief Submit a work item to the system workqueue.
2348 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002349 * This routine submits work item @a work to be processed by the system
2350 * workqueue. If the work item is already pending in the workqueue's queue
2351 * as a result of an earlier submission, this routine has no effect on the
2352 * work item. If the work item has already been processed, or is currently
2353 * being processed, its work is considered complete and the work item can be
2354 * resubmitted.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002355 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002356 * @warning
2357 * Work items submitted to the system workqueue should avoid using handlers
2358 * that block or yield since this may prevent the system workqueue from
2359 * processing other work items in a timely manner.
2360 *
2361 * @note Can be called by ISRs.
2362 *
2363 * @param work Address of work item.
2364 *
2365 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002366 */
2367static inline void k_work_submit(struct k_work *work)
2368{
2369 k_work_submit_to_queue(&k_sys_work_q, work);
2370}
2371
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002372/**
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002373 * @brief Submit a delayed work item to the system workqueue.
2374 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002375 * This routine schedules work item @a work to be processed by the system
2376 * workqueue after a delay of @a delay milliseconds. The routine initiates
David B. Kinder8b986d72017-04-18 15:56:26 -07002377 * an asynchronous countdown for the work item and then returns to the caller.
Allan Stephens6bba9b02016-11-16 14:56:54 -05002378 * Only when the countdown completes is the work item actually submitted to
2379 * the workqueue and becomes pending.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002380 *
Allan Stephens6bba9b02016-11-16 14:56:54 -05002381 * Submitting a previously submitted delayed work item that is still
2382 * counting down cancels the existing submission and restarts the countdown
2383 * using the new delay. If the work item is currently pending on the
2384 * workqueue's queue because the countdown has completed it is too late to
2385 * resubmit the item, and resubmission fails without impacting the work item.
2386 * If the work item has already been processed, or is currently being processed,
2387 * its work is considered complete and the work item can be resubmitted.
2388 *
2389 * @warning
2390 * Work items submitted to the system workqueue should avoid using handlers
2391 * that block or yield since this may prevent the system workqueue from
2392 * processing other work items in a timely manner.
2393 *
2394 * @note Can be called by ISRs.
2395 *
2396 * @param work Address of delayed work item.
2397 * @param delay Delay before submitting the work item (in milliseconds).
2398 *
2399 * @retval 0 Work item countdown started.
2400 * @retval -EINPROGRESS Work item is already pending.
2401 * @retval -EINVAL Work item is being processed or has completed its work.
2402 * @retval -EADDRINUSE Work item is pending on a different workqueue.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002403 */
2404static inline int k_delayed_work_submit(struct k_delayed_work *work,
Kumar Galacc334c72017-04-21 10:55:34 -05002405 s32_t delay)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002406{
Allan Stephens6c98c4d2016-10-17 14:34:53 -05002407 return k_delayed_work_submit_to_queue(&k_sys_work_q, work, delay);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002408}
2409
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002410/**
Johan Hedbergc8201b22016-12-09 10:42:22 +02002411 * @brief Get time remaining before a delayed work gets scheduled.
2412 *
2413 * This routine computes the (approximate) time remaining before a
2414 * delayed work gets executed. If the delayed work is not waiting to be
2415 * schedules, it returns zero.
2416 *
2417 * @param work Delayed work item.
2418 *
2419 * @return Remaining time (in milliseconds).
2420 */
Kumar Galacc334c72017-04-21 10:55:34 -05002421static inline s32_t k_delayed_work_remaining_get(struct k_delayed_work *work)
Johan Hedbergc8201b22016-12-09 10:42:22 +02002422{
2423 return _timeout_remaining_get(&work->timeout);
2424}
2425
2426/**
Allan Stephensc98da842016-11-11 15:45:03 -05002427 * @} end defgroup workqueue_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002428 */
2429
Allan Stephensc98da842016-11-11 15:45:03 -05002430/**
2431 * @cond INTERNAL_HIDDEN
2432 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002433
2434struct k_mutex {
2435 _wait_q_t wait_q;
Benjamin Walshb7ef0cb2016-10-05 17:32:01 -04002436 struct k_thread *owner;
Kumar Galacc334c72017-04-21 10:55:34 -05002437 u32_t lock_count;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002438 int owner_orig_prio;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002439
Anas Nashif2f203c22016-12-18 06:57:45 -05002440 _OBJECT_TRACING_NEXT_PTR(k_mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002441};
2442
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002443#define _K_MUTEX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002444 { \
2445 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2446 .owner = NULL, \
2447 .lock_count = 0, \
2448 .owner_orig_prio = K_LOWEST_THREAD_PRIO, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002449 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002450 }
2451
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002452#define K_MUTEX_INITIALIZER DEPRECATED_MACRO _K_MUTEX_INITIALIZER
2453
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002454/**
Allan Stephensc98da842016-11-11 15:45:03 -05002455 * INTERNAL_HIDDEN @endcond
2456 */
2457
2458/**
2459 * @defgroup mutex_apis Mutex APIs
2460 * @ingroup kernel_apis
2461 * @{
2462 */
2463
2464/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002465 * @brief Statically define and initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002466 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002467 * The mutex can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002468 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002469 * @code extern struct k_mutex <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002470 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002471 * @param name Name of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002472 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002473#define K_MUTEX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002474 struct k_mutex name \
2475 __in_section(_k_mutex, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002476 _K_MUTEX_INITIALIZER(name)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002477
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002478/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002479 * @brief Initialize a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002480 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002481 * This routine initializes a mutex object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002482 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002483 * Upon completion, the mutex is available and does not have an owner.
2484 *
2485 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002486 *
2487 * @return N/A
2488 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002489__syscall void k_mutex_init(struct k_mutex *mutex);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002490
2491/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002492 * @brief Lock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002493 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002494 * This routine locks @a mutex. If the mutex is locked by another thread,
2495 * the calling thread waits until the mutex becomes available or until
2496 * a timeout occurs.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002497 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002498 * A thread is permitted to lock a mutex it has already locked. The operation
2499 * completes immediately and the lock count is increased by 1.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002500 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002501 * @param mutex Address of the mutex.
2502 * @param timeout Waiting period to lock the mutex (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002503 * or one of the special values K_NO_WAIT and K_FOREVER.
2504 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002505 * @retval 0 Mutex locked.
2506 * @retval -EBUSY Returned without waiting.
2507 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002508 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002509__syscall int k_mutex_lock(struct k_mutex *mutex, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002510
2511/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002512 * @brief Unlock a mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002513 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002514 * This routine unlocks @a mutex. The mutex must already be locked by the
2515 * calling thread.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002516 *
2517 * The mutex cannot be claimed by another thread until it has been unlocked by
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002518 * the calling thread as many times as it was previously locked by that
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002519 * thread.
2520 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002521 * @param mutex Address of the mutex.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002522 *
2523 * @return N/A
2524 */
Andrew Boie2f7519b2017-09-29 03:33:06 -07002525__syscall void k_mutex_unlock(struct k_mutex *mutex);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002526
Allan Stephensc98da842016-11-11 15:45:03 -05002527/**
2528 * @} end defgroup mutex_apis
2529 */
2530
2531/**
2532 * @cond INTERNAL_HIDDEN
2533 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002534
2535struct k_sem {
2536 _wait_q_t wait_q;
2537 unsigned int count;
2538 unsigned int limit;
Benjamin Walshacc68c12017-01-29 18:57:45 -05002539 _POLL_EVENT;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002540
Anas Nashif2f203c22016-12-18 06:57:45 -05002541 _OBJECT_TRACING_NEXT_PTR(k_sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002542};
2543
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002544#define _K_SEM_INITIALIZER(obj, initial_count, count_limit) \
Allan Stephensc98da842016-11-11 15:45:03 -05002545 { \
2546 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
2547 .count = initial_count, \
2548 .limit = count_limit, \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03002549 _POLL_EVENT_OBJ_INIT(obj) \
Anas Nashif2f203c22016-12-18 06:57:45 -05002550 _OBJECT_TRACING_INIT \
Allan Stephensc98da842016-11-11 15:45:03 -05002551 }
2552
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002553#define K_SEM_INITIALIZER DEPRECATED_MACRO _K_SEM_INITIALIZER
2554
Allan Stephensc98da842016-11-11 15:45:03 -05002555/**
2556 * INTERNAL_HIDDEN @endcond
2557 */
2558
2559/**
2560 * @defgroup semaphore_apis Semaphore APIs
2561 * @ingroup kernel_apis
2562 * @{
2563 */
2564
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002565/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002566 * @brief Initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002567 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002568 * This routine initializes a semaphore object, prior to its first use.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002569 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002570 * @param sem Address of the semaphore.
2571 * @param initial_count Initial semaphore count.
2572 * @param limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002573 *
2574 * @return N/A
2575 */
Andrew Boie99280232017-09-29 14:17:47 -07002576__syscall void k_sem_init(struct k_sem *sem, unsigned int initial_count,
2577 unsigned int limit);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002578
2579/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002580 * @brief Take a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002581 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002582 * This routine takes @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002583 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002584 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2585 *
2586 * @param sem Address of the semaphore.
2587 * @param timeout Waiting period to take the semaphore (in milliseconds),
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002588 * or one of the special values K_NO_WAIT and K_FOREVER.
2589 *
Benjamin Walsh91f834c2016-12-01 11:39:49 -05002590 * @note When porting code from the nanokernel legacy API to the new API, be
2591 * careful with the return value of this function. The return value is the
2592 * reverse of the one of nano_sem_take family of APIs: 0 means success, and
2593 * non-zero means failure, while the nano_sem_take family returns 1 for success
2594 * and 0 for failure.
2595 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002596 * @retval 0 Semaphore taken.
2597 * @retval -EBUSY Returned without waiting.
2598 * @retval -EAGAIN Waiting period timed out.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002599 */
Andrew Boie99280232017-09-29 14:17:47 -07002600__syscall int k_sem_take(struct k_sem *sem, s32_t timeout);
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002601
2602/**
2603 * @brief Give a semaphore.
2604 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002605 * This routine gives @a sem, unless the semaphore is already at its maximum
2606 * permitted count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002607 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002608 * @note Can be called by ISRs.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002609 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002610 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002611 *
2612 * @return N/A
2613 */
Andrew Boie99280232017-09-29 14:17:47 -07002614__syscall void k_sem_give(struct k_sem *sem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002615
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002616/**
2617 * @brief Reset a semaphore's count to zero.
2618 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002619 * This routine sets the count of @a sem to zero.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002620 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002621 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002622 *
2623 * @return N/A
2624 */
Andrew Boie990bf162017-10-03 12:36:49 -07002625__syscall void k_sem_reset(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002626
2627static inline void _impl_k_sem_reset(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002628{
2629 sem->count = 0;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002630}
2631
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002632/**
2633 * @brief Get a semaphore's count.
2634 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002635 * This routine returns the current count of @a sem.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002636 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002637 * @param sem Address of the semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002638 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002639 * @return Current semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002640 */
Andrew Boie990bf162017-10-03 12:36:49 -07002641__syscall unsigned int k_sem_count_get(struct k_sem *sem);
Andrew Boiefc273c02017-09-23 12:51:23 -07002642
2643static inline unsigned int _impl_k_sem_count_get(struct k_sem *sem)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002644{
2645 return sem->count;
2646}
2647
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002648/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002649 * @brief Statically define and initialize a semaphore.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002650 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002651 * The semaphore can be accessed outside the module where it is defined using:
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002652 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002653 * @code extern struct k_sem <name>; @endcode
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002654 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002655 * @param name Name of the semaphore.
2656 * @param initial_count Initial semaphore count.
2657 * @param count_limit Maximum permitted semaphore count.
Benjamin Walshb9c1a062016-10-15 17:12:35 -04002658 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002659#define K_SEM_DEFINE(name, initial_count, count_limit) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002660 struct k_sem name \
2661 __in_section(_k_sem, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002662 _K_SEM_INITIALIZER(name, initial_count, count_limit)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002663
Allan Stephensc98da842016-11-11 15:45:03 -05002664/**
2665 * @} end defgroup semaphore_apis
2666 */
2667
2668/**
2669 * @defgroup alert_apis Alert APIs
2670 * @ingroup kernel_apis
2671 * @{
2672 */
2673
Allan Stephens5eceb852016-11-16 10:16:30 -05002674/**
2675 * @typedef k_alert_handler_t
2676 * @brief Alert handler function type.
2677 *
2678 * An alert's alert handler function is invoked by the system workqueue
David B. Kinder8b986d72017-04-18 15:56:26 -07002679 * when the alert is signaled. The alert handler function is optional,
Allan Stephens5eceb852016-11-16 10:16:30 -05002680 * and is only invoked if the alert has been initialized with one.
2681 *
2682 * @param alert Address of the alert.
2683 *
2684 * @return 0 if alert has been consumed; non-zero if alert should pend.
2685 */
2686typedef int (*k_alert_handler_t)(struct k_alert *alert);
Allan Stephensc98da842016-11-11 15:45:03 -05002687
2688/**
2689 * @} end defgroup alert_apis
2690 */
2691
2692/**
2693 * @cond INTERNAL_HIDDEN
2694 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002695
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002696#define K_ALERT_DEFAULT NULL
2697#define K_ALERT_IGNORE ((void *)(-1))
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002698
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002699struct k_alert {
2700 k_alert_handler_t handler;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002701 atomic_t send_count;
2702 struct k_work work_item;
2703 struct k_sem sem;
2704
Anas Nashif2f203c22016-12-18 06:57:45 -05002705 _OBJECT_TRACING_NEXT_PTR(k_alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002706};
2707
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002708extern void _alert_deliver(struct k_work *work);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002709
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002710#define _K_ALERT_INITIALIZER(obj, alert_handler, max_num_pending_alerts) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002711 { \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002712 .handler = (k_alert_handler_t)alert_handler, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002713 .send_count = ATOMIC_INIT(0), \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002714 .work_item = _K_WORK_INITIALIZER(_alert_deliver), \
2715 .sem = _K_SEM_INITIALIZER(obj.sem, 0, max_num_pending_alerts), \
Anas Nashif2f203c22016-12-18 06:57:45 -05002716 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002717 }
2718
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002719#define K_ALERT_INITIALIZER DEPRECATED_MACRO _K_ALERT_INITIALIZER
2720
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002721/**
Allan Stephensc98da842016-11-11 15:45:03 -05002722 * INTERNAL_HIDDEN @endcond
2723 */
2724
2725/**
2726 * @addtogroup alert_apis
2727 * @{
2728 */
2729
2730/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002731 * @brief Statically define and initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002732 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002733 * The alert can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002734 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002735 * @code extern struct k_alert <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002736 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002737 * @param name Name of the alert.
2738 * @param alert_handler Action to take when alert is sent. Specify either
2739 * the address of a function to be invoked by the system workqueue
2740 * thread, K_ALERT_IGNORE (which causes the alert to be ignored), or
2741 * K_ALERT_DEFAULT (which causes the alert to pend).
2742 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002743 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002744#define K_ALERT_DEFINE(name, alert_handler, max_num_pending_alerts) \
Benjamin Walsh31a3f6a2016-10-25 13:28:35 -04002745 struct k_alert name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002746 __in_section(_k_alert, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002747 _K_ALERT_INITIALIZER(name, alert_handler, \
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002748 max_num_pending_alerts)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002749
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002750/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002751 * @brief Initialize an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002752 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002753 * This routine initializes an alert object, prior to its first use.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002754 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002755 * @param alert Address of the alert.
2756 * @param handler Action to take when alert is sent. Specify either the address
2757 * of a function to be invoked by the system workqueue thread,
2758 * K_ALERT_IGNORE (which causes the alert to be ignored), or
2759 * K_ALERT_DEFAULT (which causes the alert to pend).
2760 * @param max_num_pending_alerts Maximum number of pending alerts.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002761 *
2762 * @return N/A
2763 */
Peter Mitsis058fa4e2016-10-25 14:42:30 -04002764extern void k_alert_init(struct k_alert *alert, k_alert_handler_t handler,
2765 unsigned int max_num_pending_alerts);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002766
2767/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002768 * @brief Receive an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002769 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002770 * This routine receives a pending alert for @a alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002771 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002772 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
2773 *
2774 * @param alert Address of the alert.
2775 * @param timeout Waiting period to receive the alert (in milliseconds),
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002776 * or one of the special values K_NO_WAIT and K_FOREVER.
2777 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002778 * @retval 0 Alert received.
2779 * @retval -EBUSY Returned without waiting.
2780 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002781 */
Andrew Boie310e9872017-09-29 04:41:15 -07002782__syscall int k_alert_recv(struct k_alert *alert, s32_t timeout);
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002783
2784/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002785 * @brief Signal an alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002786 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002787 * This routine signals @a alert. The action specified for @a alert will
2788 * be taken, which may trigger the execution of an alert handler function
2789 * and/or cause the alert to pend (assuming the alert has not reached its
2790 * maximum number of pending alerts).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002791 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002792 * @note Can be called by ISRs.
2793 *
2794 * @param alert Address of the alert.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002795 *
2796 * @return N/A
2797 */
Andrew Boie310e9872017-09-29 04:41:15 -07002798__syscall void k_alert_send(struct k_alert *alert);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002799
2800/**
Allan Stephensc98da842016-11-11 15:45:03 -05002801 * @} end addtogroup alert_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002802 */
2803
Allan Stephensc98da842016-11-11 15:45:03 -05002804/**
2805 * @cond INTERNAL_HIDDEN
2806 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002807
2808struct k_msgq {
2809 _wait_q_t wait_q;
Peter Mitsis026b4ed2016-10-13 11:41:45 -04002810 size_t msg_size;
Kumar Galacc334c72017-04-21 10:55:34 -05002811 u32_t max_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002812 char *buffer_start;
2813 char *buffer_end;
2814 char *read_ptr;
2815 char *write_ptr;
Kumar Galacc334c72017-04-21 10:55:34 -05002816 u32_t used_msgs;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002817
Anas Nashif2f203c22016-12-18 06:57:45 -05002818 _OBJECT_TRACING_NEXT_PTR(k_msgq);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002819};
2820
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002821#define _K_MSGQ_INITIALIZER(obj, q_buffer, q_msg_size, q_max_msgs) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002822 { \
2823 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002824 .max_msgs = q_max_msgs, \
2825 .msg_size = q_msg_size, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002826 .buffer_start = q_buffer, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002827 .buffer_end = q_buffer + (q_max_msgs * q_msg_size), \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002828 .read_ptr = q_buffer, \
2829 .write_ptr = q_buffer, \
2830 .used_msgs = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05002831 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002832 }
2833
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002834#define K_MSGQ_INITIALIZER DEPRECATED_MACRO _K_MSGQ_INITIALIZER
2835
Peter Mitsis1da807e2016-10-06 11:36:59 -04002836/**
Allan Stephensc98da842016-11-11 15:45:03 -05002837 * INTERNAL_HIDDEN @endcond
2838 */
2839
2840/**
2841 * @defgroup msgq_apis Message Queue APIs
2842 * @ingroup kernel_apis
2843 * @{
2844 */
2845
2846/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002847 * @brief Statically define and initialize a message queue.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002848 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002849 * The message queue's ring buffer contains space for @a q_max_msgs messages,
2850 * each of which is @a q_msg_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06002851 * @a q_align -byte boundary, which must be a power of 2. To ensure that each
2852 * message is similarly aligned to this boundary, @a q_msg_size must also be
2853 * a multiple of @a q_align.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002854 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002855 * The message queue can be accessed outside the module where it is defined
2856 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002857 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05002858 * @code extern struct k_msgq <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04002859 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002860 * @param q_name Name of the message queue.
2861 * @param q_msg_size Message size (in bytes).
2862 * @param q_max_msgs Maximum number of messages that can be queued.
Allan Stephensda827222016-11-09 14:23:58 -06002863 * @param q_align Alignment of the message queue's ring buffer.
Peter Mitsis1da807e2016-10-06 11:36:59 -04002864 */
2865#define K_MSGQ_DEFINE(q_name, q_msg_size, q_max_msgs, q_align) \
2866 static char __noinit __aligned(q_align) \
2867 _k_fifo_buf_##q_name[(q_max_msgs) * (q_msg_size)]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05002868 struct k_msgq q_name \
2869 __in_section(_k_msgq, static, q_name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07002870 _K_MSGQ_INITIALIZER(q_name, _k_fifo_buf_##q_name, \
Peter Mitsis1da807e2016-10-06 11:36:59 -04002871 q_msg_size, q_max_msgs)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002872
Peter Mitsisd7a37502016-10-13 11:37:40 -04002873/**
2874 * @brief Initialize a message queue.
2875 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002876 * This routine initializes a message queue object, prior to its first use.
2877 *
Allan Stephensda827222016-11-09 14:23:58 -06002878 * The message queue's ring buffer must contain space for @a max_msgs messages,
2879 * each of which is @a msg_size bytes long. The buffer must be aligned to an
2880 * N-byte boundary, where N is a power of 2 (i.e. 1, 2, 4, ...). To ensure
2881 * that each message is similarly aligned to this boundary, @a q_msg_size
2882 * must also be a multiple of N.
2883 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002884 * @param q Address of the message queue.
2885 * @param buffer Pointer to ring buffer that holds queued messages.
2886 * @param msg_size Message size (in bytes).
Peter Mitsisd7a37502016-10-13 11:37:40 -04002887 * @param max_msgs Maximum number of messages that can be queued.
2888 *
2889 * @return N/A
2890 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002891__syscall void k_msgq_init(struct k_msgq *q, char *buffer,
2892 size_t msg_size, u32_t max_msgs);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002893
2894/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002895 * @brief Send a message to a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002896 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002897 * This routine sends a message to message queue @a q.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002898 *
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002899 * @note Can be called by ISRs.
2900 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002901 * @param q Address of the message queue.
2902 * @param data Pointer to the message.
2903 * @param timeout Waiting period to add the message (in milliseconds),
2904 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002905 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002906 * @retval 0 Message sent.
2907 * @retval -ENOMSG Returned without waiting or queue purged.
2908 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002909 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002910__syscall int k_msgq_put(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002911
2912/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002913 * @brief Receive a message from a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002914 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002915 * This routine receives a message from message queue @a q in a "first in,
2916 * first out" manner.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002917 *
Allan Stephensc98da842016-11-11 15:45:03 -05002918 * @note Can be called by ISRs, but @a timeout must be set to K_NO_WAIT.
Benjamin Walsh8215ce12016-11-09 19:45:19 -05002919 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002920 * @param q Address of the message queue.
2921 * @param data Address of area to hold the received message.
2922 * @param timeout Waiting period to receive the message (in milliseconds),
2923 * or one of the special values K_NO_WAIT and K_FOREVER.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002924 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05002925 * @retval 0 Message received.
2926 * @retval -ENOMSG Returned without waiting.
2927 * @retval -EAGAIN Waiting period timed out.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002928 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002929__syscall int k_msgq_get(struct k_msgq *q, void *data, s32_t timeout);
Peter Mitsisd7a37502016-10-13 11:37:40 -04002930
2931/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002932 * @brief Purge a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002933 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002934 * This routine discards all unreceived messages in a message queue's ring
2935 * buffer. Any threads that are blocked waiting to send a message to the
2936 * message queue are unblocked and see an -ENOMSG error code.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002937 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002938 * @param q Address of the message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002939 *
2940 * @return N/A
2941 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002942__syscall void k_msgq_purge(struct k_msgq *q);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002943
Peter Mitsis67be2492016-10-07 11:44:34 -04002944/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002945 * @brief Get the amount of free space in a message queue.
Peter Mitsis67be2492016-10-07 11:44:34 -04002946 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002947 * This routine returns the number of unused entries in a message queue's
2948 * ring buffer.
Peter Mitsis67be2492016-10-07 11:44:34 -04002949 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002950 * @param q Address of the message queue.
2951 *
2952 * @return Number of unused ring buffer entries.
Peter Mitsis67be2492016-10-07 11:44:34 -04002953 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002954__syscall u32_t k_msgq_num_free_get(struct k_msgq *q);
2955
2956static inline u32_t _impl_k_msgq_num_free_get(struct k_msgq *q)
Peter Mitsis67be2492016-10-07 11:44:34 -04002957{
2958 return q->max_msgs - q->used_msgs;
2959}
2960
Peter Mitsisd7a37502016-10-13 11:37:40 -04002961/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002962 * @brief Get the number of messages in a message queue.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002963 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002964 * This routine returns the number of messages in a message queue's ring buffer.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002965 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05002966 * @param q Address of the message queue.
2967 *
2968 * @return Number of messages.
Peter Mitsisd7a37502016-10-13 11:37:40 -04002969 */
Andrew Boie82edb6e2017-10-02 10:53:06 -07002970__syscall u32_t k_msgq_num_used_get(struct k_msgq *q);
2971
2972static inline u32_t _impl_k_msgq_num_used_get(struct k_msgq *q)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002973{
2974 return q->used_msgs;
2975}
2976
Allan Stephensc98da842016-11-11 15:45:03 -05002977/**
2978 * @} end defgroup msgq_apis
2979 */
2980
2981/**
2982 * @defgroup mem_pool_apis Memory Pool APIs
2983 * @ingroup kernel_apis
2984 * @{
2985 */
2986
Andy Ross73cb9582017-05-09 10:42:39 -07002987/* Note on sizing: the use of a 20 bit field for block means that,
2988 * assuming a reasonable minimum block size of 16 bytes, we're limited
2989 * to 16M of memory managed by a single pool. Long term it would be
2990 * good to move to a variable bit size based on configuration.
2991 */
2992struct k_mem_block_id {
2993 u32_t pool : 8;
2994 u32_t level : 4;
2995 u32_t block : 20;
2996};
2997
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002998struct k_mem_block {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04002999 void *data;
Andy Ross73cb9582017-05-09 10:42:39 -07003000 struct k_mem_block_id id;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003001};
3002
Allan Stephensc98da842016-11-11 15:45:03 -05003003/**
3004 * @} end defgroup mem_pool_apis
3005 */
3006
3007/**
3008 * @defgroup mailbox_apis Mailbox APIs
3009 * @ingroup kernel_apis
3010 * @{
3011 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003012
3013struct k_mbox_msg {
3014 /** internal use only - needed for legacy API support */
Kumar Galacc334c72017-04-21 10:55:34 -05003015 u32_t _mailbox;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003016 /** size of message (in bytes) */
Peter Mitsisd93078c2016-10-14 12:59:37 -04003017 size_t size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003018 /** application-defined information value */
Kumar Galacc334c72017-04-21 10:55:34 -05003019 u32_t info;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003020 /** sender's message data buffer */
3021 void *tx_data;
3022 /** internal use only - needed for legacy API support */
3023 void *_rx_data;
3024 /** message data block descriptor */
3025 struct k_mem_block tx_block;
3026 /** source thread id */
3027 k_tid_t rx_source_thread;
3028 /** target thread id */
3029 k_tid_t tx_target_thread;
3030 /** internal use only - thread waiting on send (may be a dummy) */
3031 k_tid_t _syncing_thread;
3032#if (CONFIG_NUM_MBOX_ASYNC_MSGS > 0)
3033 /** internal use only - semaphore used during asynchronous send */
3034 struct k_sem *_async_sem;
3035#endif
3036};
3037
Allan Stephensc98da842016-11-11 15:45:03 -05003038/**
3039 * @cond INTERNAL_HIDDEN
3040 */
3041
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003042struct k_mbox {
3043 _wait_q_t tx_msg_queue;
3044 _wait_q_t rx_msg_queue;
3045
Anas Nashif2f203c22016-12-18 06:57:45 -05003046 _OBJECT_TRACING_NEXT_PTR(k_mbox);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003047};
3048
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003049#define _K_MBOX_INITIALIZER(obj) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003050 { \
3051 .tx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.tx_msg_queue), \
3052 .rx_msg_queue = SYS_DLIST_STATIC_INIT(&obj.rx_msg_queue), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003053 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003054 }
3055
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003056#define K_MBOX_INITIALIZER DEPRECATED_MACRO _K_MBOX_INITIALIZER
3057
Peter Mitsis12092702016-10-14 12:57:23 -04003058/**
Allan Stephensc98da842016-11-11 15:45:03 -05003059 * INTERNAL_HIDDEN @endcond
3060 */
3061
3062/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003063 * @brief Statically define and initialize a mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003064 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003065 * The mailbox is to be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003066 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003067 * @code extern struct k_mbox <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003068 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003069 * @param name Name of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003070 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003071#define K_MBOX_DEFINE(name) \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003072 struct k_mbox name \
3073 __in_section(_k_mbox, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003074 _K_MBOX_INITIALIZER(name) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003075
Peter Mitsis12092702016-10-14 12:57:23 -04003076/**
3077 * @brief Initialize a mailbox.
3078 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003079 * This routine initializes a mailbox object, prior to its first use.
3080 *
3081 * @param mbox Address of the mailbox.
Peter Mitsis12092702016-10-14 12:57:23 -04003082 *
3083 * @return N/A
3084 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003085extern void k_mbox_init(struct k_mbox *mbox);
3086
Peter Mitsis12092702016-10-14 12:57:23 -04003087/**
3088 * @brief Send a mailbox message in a synchronous manner.
3089 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003090 * This routine sends a message to @a mbox and waits for a receiver to both
3091 * receive and process it. The message data may be in a buffer, in a memory
3092 * pool block, or non-existent (i.e. an empty message).
Peter Mitsis12092702016-10-14 12:57:23 -04003093 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003094 * @param mbox Address of the mailbox.
3095 * @param tx_msg Address of the transmit message descriptor.
3096 * @param timeout Waiting period for the message to be received (in
3097 * milliseconds), or one of the special values K_NO_WAIT
3098 * and K_FOREVER. Once the message has been received,
3099 * this routine waits as long as necessary for the message
3100 * to be completely processed.
Peter Mitsis12092702016-10-14 12:57:23 -04003101 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003102 * @retval 0 Message sent.
3103 * @retval -ENOMSG Returned without waiting.
3104 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003105 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003106extern int k_mbox_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003107 s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003108
Peter Mitsis12092702016-10-14 12:57:23 -04003109/**
3110 * @brief Send a mailbox message in an asynchronous manner.
3111 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003112 * This routine sends a message to @a mbox without waiting for a receiver
3113 * to process it. The message data may be in a buffer, in a memory pool block,
3114 * or non-existent (i.e. an empty message). Optionally, the semaphore @a sem
3115 * will be given when the message has been both received and completely
3116 * processed by the receiver.
Peter Mitsis12092702016-10-14 12:57:23 -04003117 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003118 * @param mbox Address of the mailbox.
3119 * @param tx_msg Address of the transmit message descriptor.
3120 * @param sem Address of a semaphore, or NULL if none is needed.
Peter Mitsis12092702016-10-14 12:57:23 -04003121 *
3122 * @return N/A
3123 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003124extern void k_mbox_async_put(struct k_mbox *mbox, struct k_mbox_msg *tx_msg,
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003125 struct k_sem *sem);
3126
Peter Mitsis12092702016-10-14 12:57:23 -04003127/**
3128 * @brief Receive a mailbox message.
3129 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003130 * This routine receives a message from @a mbox, then optionally retrieves
3131 * its data and disposes of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003132 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003133 * @param mbox Address of the mailbox.
3134 * @param rx_msg Address of the receive message descriptor.
3135 * @param buffer Address of the buffer to receive data, or NULL to defer data
3136 * retrieval and message disposal until later.
3137 * @param timeout Waiting period for a message to be received (in
3138 * milliseconds), or one of the special values K_NO_WAIT
3139 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003140 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003141 * @retval 0 Message received.
3142 * @retval -ENOMSG Returned without waiting.
3143 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003144 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003145extern int k_mbox_get(struct k_mbox *mbox, struct k_mbox_msg *rx_msg,
Kumar Galacc334c72017-04-21 10:55:34 -05003146 void *buffer, s32_t timeout);
Peter Mitsis12092702016-10-14 12:57:23 -04003147
3148/**
3149 * @brief Retrieve mailbox message data into a buffer.
3150 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003151 * This routine completes the processing of a received message by retrieving
3152 * its data into a buffer, then disposing of the message.
Peter Mitsis12092702016-10-14 12:57:23 -04003153 *
3154 * Alternatively, this routine can be used to dispose of a received message
3155 * without retrieving its data.
3156 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003157 * @param rx_msg Address of the receive message descriptor.
3158 * @param buffer Address of the buffer to receive data, or NULL to discard
3159 * the data.
Peter Mitsis12092702016-10-14 12:57:23 -04003160 *
3161 * @return N/A
3162 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003163extern void k_mbox_data_get(struct k_mbox_msg *rx_msg, void *buffer);
Peter Mitsis12092702016-10-14 12:57:23 -04003164
3165/**
3166 * @brief Retrieve mailbox message data into a memory pool block.
3167 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003168 * This routine completes the processing of a received message by retrieving
3169 * its data into a memory pool block, then disposing of the message.
3170 * The memory pool block that results from successful retrieval must be
3171 * returned to the pool once the data has been processed, even in cases
3172 * where zero bytes of data are retrieved.
Peter Mitsis12092702016-10-14 12:57:23 -04003173 *
3174 * Alternatively, this routine can be used to dispose of a received message
3175 * without retrieving its data. In this case there is no need to return a
3176 * memory pool block to the pool.
3177 *
3178 * This routine allocates a new memory pool block for the data only if the
3179 * data is not already in one. If a new block cannot be allocated, the routine
3180 * returns a failure code and the received message is left unchanged. This
3181 * permits the caller to reattempt data retrieval at a later time or to dispose
3182 * of the received message without retrieving its data.
3183 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003184 * @param rx_msg Address of a receive message descriptor.
3185 * @param pool Address of memory pool, or NULL to discard data.
3186 * @param block Address of the area to hold memory pool block info.
3187 * @param timeout Waiting period to wait for a memory pool block (in
3188 * milliseconds), or one of the special values K_NO_WAIT
3189 * and K_FOREVER.
Peter Mitsis12092702016-10-14 12:57:23 -04003190 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003191 * @retval 0 Data retrieved.
3192 * @retval -ENOMEM Returned without waiting.
3193 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis12092702016-10-14 12:57:23 -04003194 */
Peter Mitsis40680f62016-10-14 10:04:55 -04003195extern int k_mbox_data_block_get(struct k_mbox_msg *rx_msg,
Peter Mitsis0cb65c32016-09-29 14:07:36 -04003196 struct k_mem_pool *pool,
Kumar Galacc334c72017-04-21 10:55:34 -05003197 struct k_mem_block *block, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003198
Allan Stephensc98da842016-11-11 15:45:03 -05003199/**
3200 * @} end defgroup mailbox_apis
3201 */
3202
3203/**
3204 * @cond INTERNAL_HIDDEN
3205 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003206
3207struct k_pipe {
3208 unsigned char *buffer; /* Pipe buffer: may be NULL */
3209 size_t size; /* Buffer size */
3210 size_t bytes_used; /* # bytes used in buffer */
3211 size_t read_index; /* Where in buffer to read from */
3212 size_t write_index; /* Where in buffer to write */
3213
3214 struct {
3215 _wait_q_t readers; /* Reader wait queue */
3216 _wait_q_t writers; /* Writer wait queue */
3217 } wait_q;
3218
Anas Nashif2f203c22016-12-18 06:57:45 -05003219 _OBJECT_TRACING_NEXT_PTR(k_pipe);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003220};
3221
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003222#define _K_PIPE_INITIALIZER(obj, pipe_buffer, pipe_buffer_size) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003223 { \
3224 .buffer = pipe_buffer, \
3225 .size = pipe_buffer_size, \
3226 .bytes_used = 0, \
3227 .read_index = 0, \
3228 .write_index = 0, \
3229 .wait_q.writers = SYS_DLIST_STATIC_INIT(&obj.wait_q.writers), \
3230 .wait_q.readers = SYS_DLIST_STATIC_INIT(&obj.wait_q.readers), \
Anas Nashif2f203c22016-12-18 06:57:45 -05003231 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003232 }
3233
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003234#define K_PIPE_INITIALIZER DEPRECATED_MACRO _K_PIPE_INITIALIZER
3235
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003236/**
Allan Stephensc98da842016-11-11 15:45:03 -05003237 * INTERNAL_HIDDEN @endcond
3238 */
3239
3240/**
3241 * @defgroup pipe_apis Pipe APIs
3242 * @ingroup kernel_apis
3243 * @{
3244 */
3245
3246/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003247 * @brief Statically define and initialize a pipe.
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003248 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003249 * The pipe can be accessed outside the module where it is defined using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003250 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003251 * @code extern struct k_pipe <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003252 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003253 * @param name Name of the pipe.
3254 * @param pipe_buffer_size Size of the pipe's ring buffer (in bytes),
3255 * or zero if no ring buffer is used.
3256 * @param pipe_align Alignment of the pipe's ring buffer (power of 2).
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003257 */
Peter Mitsise5d9c582016-10-14 14:44:57 -04003258#define K_PIPE_DEFINE(name, pipe_buffer_size, pipe_align) \
3259 static unsigned char __noinit __aligned(pipe_align) \
3260 _k_pipe_buf_##name[pipe_buffer_size]; \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003261 struct k_pipe name \
3262 __in_section(_k_pipe, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003263 _K_PIPE_INITIALIZER(name, _k_pipe_buf_##name, pipe_buffer_size)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003264
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003265/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003266 * @brief Initialize a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003267 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003268 * This routine initializes a pipe object, prior to its first use.
3269 *
3270 * @param pipe Address of the pipe.
3271 * @param buffer Address of the pipe's ring buffer, or NULL if no ring buffer
3272 * is used.
3273 * @param size Size of the pipe's ring buffer (in bytes), or zero if no ring
3274 * buffer is used.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003275 *
3276 * @return N/A
3277 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003278__syscall void k_pipe_init(struct k_pipe *pipe, unsigned char *buffer,
3279 size_t size);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003280
3281/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003282 * @brief Write data to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003283 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003284 * This routine writes up to @a bytes_to_write bytes of data to @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003285 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003286 * @param pipe Address of the pipe.
3287 * @param data Address of data to write.
3288 * @param bytes_to_write Size of data (in bytes).
3289 * @param bytes_written Address of area to hold the number of bytes written.
3290 * @param min_xfer Minimum number of bytes to write.
3291 * @param timeout Waiting period to wait for the data to be written (in
3292 * milliseconds), or one of the special values K_NO_WAIT
3293 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003294 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003295 * @retval 0 At least @a min_xfer bytes of data were written.
3296 * @retval -EIO Returned without waiting; zero data bytes were written.
3297 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003298 * minus one data bytes were written.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003299 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003300__syscall int k_pipe_put(struct k_pipe *pipe, void *data,
3301 size_t bytes_to_write, size_t *bytes_written,
3302 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003303
3304/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003305 * @brief Read data from a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003306 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003307 * This routine reads up to @a bytes_to_read bytes of data from @a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003308 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003309 * @param pipe Address of the pipe.
3310 * @param data Address to place the data read from pipe.
3311 * @param bytes_to_read Maximum number of data bytes to read.
3312 * @param bytes_read Address of area to hold the number of bytes read.
3313 * @param min_xfer Minimum number of data bytes to read.
3314 * @param timeout Waiting period to wait for the data to be read (in
3315 * milliseconds), or one of the special values K_NO_WAIT
3316 * and K_FOREVER.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003317 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003318 * @retval 0 At least @a min_xfer bytes of data were read.
3319 * @retval -EIO Returned without waiting; zero data bytes were read.
3320 * @retval -EAGAIN Waiting period timed out; between zero and @a min_xfer
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003321 * minus one data bytes were read.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003322 */
Andrew Boieb9a05782017-09-29 16:05:32 -07003323__syscall int k_pipe_get(struct k_pipe *pipe, void *data,
3324 size_t bytes_to_read, size_t *bytes_read,
3325 size_t min_xfer, s32_t timeout);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003326
3327/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003328 * @brief Write memory block to a pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003329 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003330 * This routine writes the data contained in a memory block to @a pipe.
3331 * Once all of the data in the block has been written to the pipe, it will
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003332 * free the memory block @a block and give the semaphore @a sem (if specified).
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003333 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003334 * @param pipe Address of the pipe.
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003335 * @param block Memory block containing data to send
3336 * @param size Number of data bytes in memory block to send
3337 * @param sem Semaphore to signal upon completion (else NULL)
3338 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003339 * @return N/A
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003340 */
3341extern void k_pipe_block_put(struct k_pipe *pipe, struct k_mem_block *block,
3342 size_t size, struct k_sem *sem);
3343
3344/**
Allan Stephensc98da842016-11-11 15:45:03 -05003345 * @} end defgroup pipe_apis
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003346 */
3347
Allan Stephensc98da842016-11-11 15:45:03 -05003348/**
3349 * @cond INTERNAL_HIDDEN
3350 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003351
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003352struct k_mem_slab {
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003353 _wait_q_t wait_q;
Kumar Galacc334c72017-04-21 10:55:34 -05003354 u32_t num_blocks;
Peter Mitsisfb02d572016-10-13 16:55:45 -04003355 size_t block_size;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003356 char *buffer;
3357 char *free_list;
Kumar Galacc334c72017-04-21 10:55:34 -05003358 u32_t num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003359
Anas Nashif2f203c22016-12-18 06:57:45 -05003360 _OBJECT_TRACING_NEXT_PTR(k_mem_slab);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003361};
3362
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003363#define _K_MEM_SLAB_INITIALIZER(obj, slab_buffer, slab_block_size, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003364 slab_num_blocks) \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003365 { \
3366 .wait_q = SYS_DLIST_STATIC_INIT(&obj.wait_q), \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003367 .num_blocks = slab_num_blocks, \
3368 .block_size = slab_block_size, \
3369 .buffer = slab_buffer, \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003370 .free_list = NULL, \
3371 .num_used = 0, \
Anas Nashif2f203c22016-12-18 06:57:45 -05003372 _OBJECT_TRACING_INIT \
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003373 }
3374
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003375#define K_MEM_SLAB_INITIALIZER DEPRECATED_MACRO _K_MEM_SLAB_INITIALIZER
3376
3377
Peter Mitsis578f9112016-10-07 13:50:31 -04003378/**
Allan Stephensc98da842016-11-11 15:45:03 -05003379 * INTERNAL_HIDDEN @endcond
3380 */
3381
3382/**
3383 * @defgroup mem_slab_apis Memory Slab APIs
3384 * @ingroup kernel_apis
3385 * @{
3386 */
3387
3388/**
Allan Stephensda827222016-11-09 14:23:58 -06003389 * @brief Statically define and initialize a memory slab.
Peter Mitsis578f9112016-10-07 13:50:31 -04003390 *
Allan Stephensda827222016-11-09 14:23:58 -06003391 * The memory slab's buffer contains @a slab_num_blocks memory blocks
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003392 * that are @a slab_block_size bytes long. The buffer is aligned to a
Allan Stephensda827222016-11-09 14:23:58 -06003393 * @a slab_align -byte boundary. To ensure that each memory block is similarly
3394 * aligned to this boundary, @a slab_block_size must also be a multiple of
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003395 * @a slab_align.
Peter Mitsis578f9112016-10-07 13:50:31 -04003396 *
Allan Stephensda827222016-11-09 14:23:58 -06003397 * The memory slab can be accessed outside the module where it is defined
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003398 * using:
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003399 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003400 * @code extern struct k_mem_slab <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003401 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003402 * @param name Name of the memory slab.
3403 * @param slab_block_size Size of each memory block (in bytes).
3404 * @param slab_num_blocks Number memory blocks.
3405 * @param slab_align Alignment of the memory slab's buffer (power of 2).
Peter Mitsis578f9112016-10-07 13:50:31 -04003406 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003407#define K_MEM_SLAB_DEFINE(name, slab_block_size, slab_num_blocks, slab_align) \
3408 char __noinit __aligned(slab_align) \
3409 _k_mem_slab_buf_##name[(slab_num_blocks) * (slab_block_size)]; \
3410 struct k_mem_slab name \
Allan Stephense7d2cc22016-10-19 16:10:46 -05003411 __in_section(_k_mem_slab, static, name) = \
Andrew Boie65a9d2a2017-06-27 10:51:23 -07003412 _K_MEM_SLAB_INITIALIZER(name, _k_mem_slab_buf_##name, \
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003413 slab_block_size, slab_num_blocks)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003414
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003415/**
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003416 * @brief Initialize a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003417 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003418 * Initializes a memory slab, prior to its first use.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003419 *
Allan Stephensda827222016-11-09 14:23:58 -06003420 * The memory slab's buffer contains @a slab_num_blocks memory blocks
3421 * that are @a slab_block_size bytes long. The buffer must be aligned to an
3422 * N-byte boundary, where N is a power of 2 larger than 2 (i.e. 4, 8, 16, ...).
3423 * To ensure that each memory block is similarly aligned to this boundary,
3424 * @a slab_block_size must also be a multiple of N.
3425 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003426 * @param slab Address of the memory slab.
3427 * @param buffer Pointer to buffer used for the memory blocks.
3428 * @param block_size Size of each memory block (in bytes).
3429 * @param num_blocks Number of memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003430 *
3431 * @return N/A
3432 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003433extern void k_mem_slab_init(struct k_mem_slab *slab, void *buffer,
Kumar Galacc334c72017-04-21 10:55:34 -05003434 size_t block_size, u32_t num_blocks);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003435
3436/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003437 * @brief Allocate memory from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003438 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003439 * This routine allocates a memory block from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003440 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003441 * @param slab Address of the memory slab.
3442 * @param mem Pointer to block address area.
3443 * @param timeout Maximum time to wait for operation to complete
3444 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3445 * or K_FOREVER to wait as long as necessary.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003446 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003447 * @retval 0 Memory allocated. The block address area pointed at by @a mem
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003448 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003449 * @retval -ENOMEM Returned without waiting.
3450 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003451 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003452extern int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem,
Kumar Galacc334c72017-04-21 10:55:34 -05003453 s32_t timeout);
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003454
3455/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003456 * @brief Free memory allocated from a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003457 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003458 * This routine releases a previously allocated memory block back to its
3459 * associated memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003460 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003461 * @param slab Address of the memory slab.
3462 * @param mem Pointer to block address area (as set by k_mem_slab_alloc()).
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003463 *
3464 * @return N/A
3465 */
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003466extern void k_mem_slab_free(struct k_mem_slab *slab, void **mem);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003467
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003468/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003469 * @brief Get the number of used blocks in a memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003470 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003471 * This routine gets the number of memory blocks that are currently
3472 * allocated in @a slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003473 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003474 * @param slab Address of the memory slab.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003475 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003476 * @return Number of allocated memory blocks.
Peter Mitsis4a5d62f2016-10-13 16:53:30 -04003477 */
Kumar Galacc334c72017-04-21 10:55:34 -05003478static inline u32_t k_mem_slab_num_used_get(struct k_mem_slab *slab)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003479{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003480 return slab->num_used;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003481}
3482
Peter Mitsisc001aa82016-10-13 13:53:37 -04003483/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003484 * @brief Get the number of unused blocks in a memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003485 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003486 * This routine gets the number of memory blocks that are currently
3487 * unallocated in @a slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003488 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003489 * @param slab Address of the memory slab.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003490 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003491 * @return Number of unallocated memory blocks.
Peter Mitsisc001aa82016-10-13 13:53:37 -04003492 */
Kumar Galacc334c72017-04-21 10:55:34 -05003493static inline u32_t k_mem_slab_num_free_get(struct k_mem_slab *slab)
Peter Mitsisc001aa82016-10-13 13:53:37 -04003494{
Benjamin Walsh7ef0f622016-10-24 17:04:43 -04003495 return slab->num_blocks - slab->num_used;
Peter Mitsisc001aa82016-10-13 13:53:37 -04003496}
3497
Allan Stephensc98da842016-11-11 15:45:03 -05003498/**
3499 * @} end defgroup mem_slab_apis
3500 */
3501
3502/**
3503 * @cond INTERNAL_HIDDEN
3504 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003505
Andy Ross73cb9582017-05-09 10:42:39 -07003506struct k_mem_pool_lvl {
3507 union {
3508 u32_t *bits_p;
3509 u32_t bits;
3510 };
3511 sys_dlist_t free_list;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003512};
3513
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003514struct k_mem_pool {
Andy Ross73cb9582017-05-09 10:42:39 -07003515 void *buf;
3516 size_t max_sz;
3517 u16_t n_max;
3518 u8_t n_levels;
3519 u8_t max_inline_level;
3520 struct k_mem_pool_lvl *levels;
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003521 _wait_q_t wait_q;
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003522};
3523
Andy Ross73cb9582017-05-09 10:42:39 -07003524#define _ALIGN4(n) ((((n)+3)/4)*4)
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003525
Andy Ross73cb9582017-05-09 10:42:39 -07003526#define _MPOOL_HAVE_LVL(max, min, l) (((max) >> (2*(l))) >= (min) ? 1 : 0)
3527
3528#define _MPOOL_LVLS(maxsz, minsz) \
3529 (_MPOOL_HAVE_LVL(maxsz, minsz, 0) + \
3530 _MPOOL_HAVE_LVL(maxsz, minsz, 1) + \
3531 _MPOOL_HAVE_LVL(maxsz, minsz, 2) + \
3532 _MPOOL_HAVE_LVL(maxsz, minsz, 3) + \
3533 _MPOOL_HAVE_LVL(maxsz, minsz, 4) + \
3534 _MPOOL_HAVE_LVL(maxsz, minsz, 5) + \
3535 _MPOOL_HAVE_LVL(maxsz, minsz, 6) + \
3536 _MPOOL_HAVE_LVL(maxsz, minsz, 7) + \
3537 _MPOOL_HAVE_LVL(maxsz, minsz, 8) + \
3538 _MPOOL_HAVE_LVL(maxsz, minsz, 9) + \
3539 _MPOOL_HAVE_LVL(maxsz, minsz, 10) + \
3540 _MPOOL_HAVE_LVL(maxsz, minsz, 11) + \
3541 _MPOOL_HAVE_LVL(maxsz, minsz, 12) + \
3542 _MPOOL_HAVE_LVL(maxsz, minsz, 13) + \
3543 _MPOOL_HAVE_LVL(maxsz, minsz, 14) + \
3544 _MPOOL_HAVE_LVL(maxsz, minsz, 15))
3545
3546/* Rounds the needed bits up to integer multiples of u32_t */
3547#define _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) \
3548 ((((n_max) << (2*(l))) + 31) / 32)
3549
3550/* One word gets stored free unioned with the pointer, otherwise the
3551 * calculated unclamped value
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003552 */
Andy Ross73cb9582017-05-09 10:42:39 -07003553#define _MPOOL_LBIT_WORDS(n_max, l) \
3554 (_MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l) < 2 ? 0 \
3555 : _MPOOL_LBIT_WORDS_UNCLAMPED(n_max, l))
Allan Stephensc98da842016-11-11 15:45:03 -05003556
Andy Ross73cb9582017-05-09 10:42:39 -07003557/* How many bytes for the bitfields of a single level? */
3558#define _MPOOL_LBIT_BYTES(maxsz, minsz, l, n_max) \
3559 (_MPOOL_LVLS((maxsz), (minsz)) >= (l) ? \
3560 4 * _MPOOL_LBIT_WORDS((n_max), l) : 0)
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003561
Andy Ross73cb9582017-05-09 10:42:39 -07003562/* Size of the bitmap array that follows the buffer in allocated memory */
3563#define _MPOOL_BITS_SIZE(maxsz, minsz, n_max) \
3564 (_MPOOL_LBIT_BYTES(maxsz, minsz, 0, n_max) + \
3565 _MPOOL_LBIT_BYTES(maxsz, minsz, 1, n_max) + \
3566 _MPOOL_LBIT_BYTES(maxsz, minsz, 2, n_max) + \
3567 _MPOOL_LBIT_BYTES(maxsz, minsz, 3, n_max) + \
3568 _MPOOL_LBIT_BYTES(maxsz, minsz, 4, n_max) + \
3569 _MPOOL_LBIT_BYTES(maxsz, minsz, 5, n_max) + \
3570 _MPOOL_LBIT_BYTES(maxsz, minsz, 6, n_max) + \
3571 _MPOOL_LBIT_BYTES(maxsz, minsz, 7, n_max) + \
3572 _MPOOL_LBIT_BYTES(maxsz, minsz, 8, n_max) + \
3573 _MPOOL_LBIT_BYTES(maxsz, minsz, 9, n_max) + \
3574 _MPOOL_LBIT_BYTES(maxsz, minsz, 10, n_max) + \
3575 _MPOOL_LBIT_BYTES(maxsz, minsz, 11, n_max) + \
3576 _MPOOL_LBIT_BYTES(maxsz, minsz, 12, n_max) + \
3577 _MPOOL_LBIT_BYTES(maxsz, minsz, 13, n_max) + \
3578 _MPOOL_LBIT_BYTES(maxsz, minsz, 14, n_max) + \
3579 _MPOOL_LBIT_BYTES(maxsz, minsz, 15, n_max))
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003580
3581/**
Allan Stephensc98da842016-11-11 15:45:03 -05003582 * INTERNAL_HIDDEN @endcond
Dmitriy Korovkin07414672016-11-03 13:35:42 -04003583 */
3584
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003585/**
Allan Stephensc98da842016-11-11 15:45:03 -05003586 * @addtogroup mem_pool_apis
3587 * @{
3588 */
3589
3590/**
3591 * @brief Statically define and initialize a memory pool.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003592 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003593 * The memory pool's buffer contains @a n_max blocks that are @a max_size bytes
3594 * long. The memory pool allows blocks to be repeatedly partitioned into
3595 * quarters, down to blocks of @a min_size bytes long. The buffer is aligned
Andy Ross73cb9582017-05-09 10:42:39 -07003596 * to a @a align -byte boundary.
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003597 *
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003598 * If the pool is to be accessed outside the module where it is defined, it
3599 * can be declared via
3600 *
Allan Stephens82d4c3a2016-11-17 09:23:46 -05003601 * @code extern struct k_mem_pool <name>; @endcode
Peter Mitsis348eb4c2016-10-26 11:22:14 -04003602 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003603 * @param name Name of the memory pool.
Andy Ross73cb9582017-05-09 10:42:39 -07003604 * @param minsz Size of the smallest blocks in the pool (in bytes).
3605 * @param maxsz Size of the largest blocks in the pool (in bytes).
3606 * @param nmax Number of maximum sized blocks in the pool.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003607 * @param align Alignment of the pool's buffer (power of 2).
Peter Mitsis2a2b0752016-10-06 16:27:01 -04003608 */
Andy Ross73cb9582017-05-09 10:42:39 -07003609#define K_MEM_POOL_DEFINE(name, minsz, maxsz, nmax, align) \
3610 char __aligned(align) _mpool_buf_##name[_ALIGN4(maxsz * nmax) \
3611 + _MPOOL_BITS_SIZE(maxsz, minsz, nmax)]; \
3612 struct k_mem_pool_lvl _mpool_lvls_##name[_MPOOL_LVLS(maxsz, minsz)]; \
3613 struct k_mem_pool name __in_section(_k_mem_pool, static, name) = { \
3614 .buf = _mpool_buf_##name, \
3615 .max_sz = maxsz, \
3616 .n_max = nmax, \
3617 .n_levels = _MPOOL_LVLS(maxsz, minsz), \
3618 .levels = _mpool_lvls_##name, \
3619 }
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003620
Peter Mitsis937042c2016-10-13 13:18:26 -04003621/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003622 * @brief Allocate memory from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003623 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003624 * This routine allocates a memory block from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003625 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003626 * @param pool Address of the memory pool.
3627 * @param block Pointer to block descriptor for the allocated memory.
3628 * @param size Amount of memory to allocate (in bytes).
3629 * @param timeout Maximum time to wait for operation to complete
3630 * (in milliseconds). Use K_NO_WAIT to return without waiting,
3631 * or K_FOREVER to wait as long as necessary.
3632 *
Allan Stephens9ef50f42016-11-16 15:33:31 -05003633 * @retval 0 Memory allocated. The @a data field of the block descriptor
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003634 * is set to the starting address of the memory block.
Allan Stephens9ef50f42016-11-16 15:33:31 -05003635 * @retval -ENOMEM Returned without waiting.
3636 * @retval -EAGAIN Waiting period timed out.
Peter Mitsis937042c2016-10-13 13:18:26 -04003637 */
Dmitriy Korovkin3c426882016-09-01 18:14:17 -04003638extern int k_mem_pool_alloc(struct k_mem_pool *pool, struct k_mem_block *block,
Kumar Galacc334c72017-04-21 10:55:34 -05003639 size_t size, s32_t timeout);
Peter Mitsis937042c2016-10-13 13:18:26 -04003640
3641/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003642 * @brief Free memory allocated from a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003643 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003644 * This routine releases a previously allocated memory block back to its
3645 * memory pool.
3646 *
3647 * @param block Pointer to block descriptor for the allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003648 *
3649 * @return N/A
3650 */
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003651extern void k_mem_pool_free(struct k_mem_block *block);
Peter Mitsis937042c2016-10-13 13:18:26 -04003652
3653/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003654 * @brief Defragment a memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003655 *
Andy Ross73cb9582017-05-09 10:42:39 -07003656 * This is a no-op API preserved for backward compatibility only.
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003657 *
Andy Ross73cb9582017-05-09 10:42:39 -07003658 * @param pool Unused
Peter Mitsis937042c2016-10-13 13:18:26 -04003659 *
3660 * @return N/A
3661 */
Andy Ross73cb9582017-05-09 10:42:39 -07003662static inline void __deprecated k_mem_pool_defrag(struct k_mem_pool *pool) {}
Peter Mitsis937042c2016-10-13 13:18:26 -04003663
3664/**
Allan Stephensc98da842016-11-11 15:45:03 -05003665 * @} end addtogroup mem_pool_apis
3666 */
3667
3668/**
3669 * @defgroup heap_apis Heap Memory Pool APIs
3670 * @ingroup kernel_apis
3671 * @{
3672 */
3673
3674/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003675 * @brief Allocate memory from heap.
Peter Mitsis937042c2016-10-13 13:18:26 -04003676 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003677 * This routine provides traditional malloc() semantics. Memory is
Allan Stephens480a1312016-10-13 15:44:48 -05003678 * allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003679 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003680 * @param size Amount of memory requested (in bytes).
Peter Mitsis937042c2016-10-13 13:18:26 -04003681 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003682 * @return Address of the allocated memory if successful; otherwise NULL.
Peter Mitsis937042c2016-10-13 13:18:26 -04003683 */
Peter Mitsis5f399242016-10-13 13:26:25 -04003684extern void *k_malloc(size_t size);
Peter Mitsis937042c2016-10-13 13:18:26 -04003685
3686/**
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003687 * @brief Free memory allocated from heap.
Allan Stephens480a1312016-10-13 15:44:48 -05003688 *
3689 * This routine provides traditional free() semantics. The memory being
3690 * returned must have been allocated from the heap memory pool.
Peter Mitsis937042c2016-10-13 13:18:26 -04003691 *
Anas Nashif345fdd52016-12-20 08:36:04 -05003692 * If @a ptr is NULL, no operation is performed.
3693 *
Allan Stephens5a7a86c2016-11-04 13:53:19 -05003694 * @param ptr Pointer to previously allocated memory.
Peter Mitsis937042c2016-10-13 13:18:26 -04003695 *
3696 * @return N/A
3697 */
3698extern void k_free(void *ptr);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003699
Allan Stephensc98da842016-11-11 15:45:03 -05003700/**
3701 * @} end defgroup heap_apis
3702 */
3703
Benjamin Walshacc68c12017-01-29 18:57:45 -05003704/* polling API - PRIVATE */
3705
Benjamin Walshb0179862017-02-02 16:39:57 -05003706#ifdef CONFIG_POLL
3707#define _INIT_OBJ_POLL_EVENT(obj) do { (obj)->poll_event = NULL; } while ((0))
3708#else
3709#define _INIT_OBJ_POLL_EVENT(obj) do { } while ((0))
3710#endif
3711
Benjamin Walshacc68c12017-01-29 18:57:45 -05003712/* private - implementation data created as needed, per-type */
3713struct _poller {
3714 struct k_thread *thread;
3715};
3716
3717/* private - types bit positions */
3718enum _poll_types_bits {
3719 /* can be used to ignore an event */
3720 _POLL_TYPE_IGNORE,
3721
3722 /* to be signaled by k_poll_signal() */
3723 _POLL_TYPE_SIGNAL,
3724
3725 /* semaphore availability */
3726 _POLL_TYPE_SEM_AVAILABLE,
3727
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003728 /* queue/fifo/lifo data availability */
3729 _POLL_TYPE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003730
3731 _POLL_NUM_TYPES
3732};
3733
3734#define _POLL_TYPE_BIT(type) (1 << ((type) - 1))
3735
3736/* private - states bit positions */
3737enum _poll_states_bits {
3738 /* default state when creating event */
3739 _POLL_STATE_NOT_READY,
3740
Benjamin Walshacc68c12017-01-29 18:57:45 -05003741 /* signaled by k_poll_signal() */
3742 _POLL_STATE_SIGNALED,
3743
3744 /* semaphore is available */
3745 _POLL_STATE_SEM_AVAILABLE,
3746
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003747 /* data is available to read on queue/fifo/lifo */
3748 _POLL_STATE_DATA_AVAILABLE,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003749
3750 _POLL_NUM_STATES
3751};
3752
3753#define _POLL_STATE_BIT(state) (1 << ((state) - 1))
3754
3755#define _POLL_EVENT_NUM_UNUSED_BITS \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003756 (32 - (0 \
3757 + 8 /* tag */ \
3758 + _POLL_NUM_TYPES \
3759 + _POLL_NUM_STATES \
3760 + 1 /* modes */ \
3761 ))
Benjamin Walshacc68c12017-01-29 18:57:45 -05003762
3763#if _POLL_EVENT_NUM_UNUSED_BITS < 0
3764#error overflow of 32-bit word in struct k_poll_event
3765#endif
3766
3767/* end of polling API - PRIVATE */
3768
3769
3770/**
3771 * @defgroup poll_apis Async polling APIs
3772 * @ingroup kernel_apis
3773 * @{
3774 */
3775
3776/* Public polling API */
3777
3778/* public - values for k_poll_event.type bitfield */
3779#define K_POLL_TYPE_IGNORE 0
3780#define K_POLL_TYPE_SIGNAL _POLL_TYPE_BIT(_POLL_TYPE_SIGNAL)
3781#define K_POLL_TYPE_SEM_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003782#define K_POLL_TYPE_DATA_AVAILABLE _POLL_TYPE_BIT(_POLL_TYPE_DATA_AVAILABLE)
3783#define K_POLL_TYPE_FIFO_DATA_AVAILABLE K_POLL_TYPE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003784
3785/* public - polling modes */
3786enum k_poll_modes {
3787 /* polling thread does not take ownership of objects when available */
3788 K_POLL_MODE_NOTIFY_ONLY = 0,
3789
3790 K_POLL_NUM_MODES
3791};
3792
3793/* public - values for k_poll_event.state bitfield */
3794#define K_POLL_STATE_NOT_READY 0
Benjamin Walshacc68c12017-01-29 18:57:45 -05003795#define K_POLL_STATE_SIGNALED _POLL_STATE_BIT(_POLL_STATE_SIGNALED)
3796#define K_POLL_STATE_SEM_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_SEM_AVAILABLE)
Luiz Augusto von Dentza7ddb872017-02-21 14:50:42 +02003797#define K_POLL_STATE_DATA_AVAILABLE _POLL_STATE_BIT(_POLL_STATE_DATA_AVAILABLE)
3798#define K_POLL_STATE_FIFO_DATA_AVAILABLE K_POLL_STATE_DATA_AVAILABLE
Benjamin Walshacc68c12017-01-29 18:57:45 -05003799
3800/* public - poll signal object */
3801struct k_poll_signal {
3802 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003803 sys_dlist_t poll_events;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003804
3805 /*
3806 * 1 if the event has been signaled, 0 otherwise. Stays set to 1 until
3807 * user resets it to 0.
3808 */
3809 unsigned int signaled;
3810
3811 /* custom result value passed to k_poll_signal() if needed */
3812 int result;
3813};
3814
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003815#define K_POLL_SIGNAL_INITIALIZER(obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003816 { \
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003817 .poll_events = SYS_DLIST_STATIC_INIT(&obj.poll_events), \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003818 .signaled = 0, \
3819 .result = 0, \
3820 }
3821
3822struct k_poll_event {
3823 /* PRIVATE - DO NOT TOUCH */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003824 sys_dnode_t _node;
3825
3826 /* PRIVATE - DO NOT TOUCH */
Benjamin Walshacc68c12017-01-29 18:57:45 -05003827 struct _poller *poller;
3828
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003829 /* optional user-specified tag, opaque, untouched by the API */
Kumar Galacc334c72017-04-21 10:55:34 -05003830 u32_t tag:8;
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003831
Benjamin Walshacc68c12017-01-29 18:57:45 -05003832 /* bitfield of event types (bitwise-ORed K_POLL_TYPE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003833 u32_t type:_POLL_NUM_TYPES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003834
3835 /* bitfield of event states (bitwise-ORed K_POLL_STATE_xxx values) */
Kumar Galacc334c72017-04-21 10:55:34 -05003836 u32_t state:_POLL_NUM_STATES;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003837
3838 /* mode of operation, from enum k_poll_modes */
Kumar Galacc334c72017-04-21 10:55:34 -05003839 u32_t mode:1;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003840
3841 /* unused bits in 32-bit word */
Kumar Galacc334c72017-04-21 10:55:34 -05003842 u32_t unused:_POLL_EVENT_NUM_UNUSED_BITS;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003843
3844 /* per-type data */
3845 union {
3846 void *obj;
3847 struct k_poll_signal *signal;
3848 struct k_sem *sem;
3849 struct k_fifo *fifo;
Luiz Augusto von Dentze5ed88f2017-02-21 15:27:20 +02003850 struct k_queue *queue;
Benjamin Walshacc68c12017-01-29 18:57:45 -05003851 };
3852};
3853
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003854#define K_POLL_EVENT_INITIALIZER(event_type, event_mode, event_obj) \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003855 { \
3856 .poller = NULL, \
3857 .type = event_type, \
3858 .state = K_POLL_STATE_NOT_READY, \
3859 .mode = event_mode, \
3860 .unused = 0, \
Benjamin Walsh969d4a72017-02-02 11:25:11 -05003861 { .obj = event_obj }, \
3862 }
3863
3864#define K_POLL_EVENT_STATIC_INITIALIZER(event_type, event_mode, event_obj, \
3865 event_tag) \
3866 { \
3867 .type = event_type, \
3868 .tag = event_tag, \
3869 .state = K_POLL_STATE_NOT_READY, \
3870 .mode = event_mode, \
3871 .unused = 0, \
3872 { .obj = event_obj }, \
Benjamin Walshacc68c12017-01-29 18:57:45 -05003873 }
3874
3875/**
3876 * @brief Initialize one struct k_poll_event instance
3877 *
3878 * After this routine is called on a poll event, the event it ready to be
3879 * placed in an event array to be passed to k_poll().
3880 *
3881 * @param event The event to initialize.
3882 * @param type A bitfield of the types of event, from the K_POLL_TYPE_xxx
3883 * values. Only values that apply to the same object being polled
3884 * can be used together. Choosing K_POLL_TYPE_IGNORE disables the
3885 * event.
Paul Sokolovskycfef9792017-07-18 11:53:06 +03003886 * @param mode Future. Use K_POLL_MODE_NOTIFY_ONLY.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003887 * @param obj Kernel object or poll signal.
3888 *
3889 * @return N/A
3890 */
3891
Kumar Galacc334c72017-04-21 10:55:34 -05003892extern void k_poll_event_init(struct k_poll_event *event, u32_t type,
Benjamin Walshacc68c12017-01-29 18:57:45 -05003893 int mode, void *obj);
3894
3895/**
3896 * @brief Wait for one or many of multiple poll events to occur
3897 *
3898 * This routine allows a thread to wait concurrently for one or many of
3899 * multiple poll events to have occurred. Such events can be a kernel object
3900 * being available, like a semaphore, or a poll signal event.
3901 *
3902 * When an event notifies that a kernel object is available, the kernel object
3903 * is not "given" to the thread calling k_poll(): it merely signals the fact
3904 * that the object was available when the k_poll() call was in effect. Also,
3905 * all threads trying to acquire an object the regular way, i.e. by pending on
3906 * the object, have precedence over the thread polling on the object. This
3907 * means that the polling thread will never get the poll event on an object
3908 * until the object becomes available and its pend queue is empty. For this
3909 * reason, the k_poll() call is more effective when the objects being polled
3910 * only have one thread, the polling thread, trying to acquire them.
3911 *
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003912 * When k_poll() returns 0, the caller should loop on all the events that were
3913 * passed to k_poll() and check the state field for the values that were
3914 * expected and take the associated actions.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003915 *
3916 * Before being reused for another call to k_poll(), the user has to reset the
3917 * state field to K_POLL_STATE_NOT_READY.
3918 *
3919 * @param events An array of pointers to events to be polled for.
3920 * @param num_events The number of events in the array.
3921 * @param timeout Waiting period for an event to be ready (in milliseconds),
3922 * or one of the special values K_NO_WAIT and K_FOREVER.
3923 *
3924 * @retval 0 One or more events are ready.
Benjamin Walshacc68c12017-01-29 18:57:45 -05003925 * @retval -EAGAIN Waiting period timed out.
3926 */
3927
3928extern int k_poll(struct k_poll_event *events, int num_events,
Kumar Galacc334c72017-04-21 10:55:34 -05003929 s32_t timeout);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003930
3931/**
Benjamin Walsha304f162017-02-02 16:46:09 -05003932 * @brief Initialize a poll signal object.
3933 *
3934 * Ready a poll signal object to be signaled via k_poll_signal().
3935 *
3936 * @param signal A poll signal.
3937 *
3938 * @return N/A
3939 */
3940
3941extern void k_poll_signal_init(struct k_poll_signal *signal);
3942
3943/**
Benjamin Walshacc68c12017-01-29 18:57:45 -05003944 * @brief Signal a poll signal object.
3945 *
3946 * This routine makes ready a poll signal, which is basically a poll event of
3947 * type K_POLL_TYPE_SIGNAL. If a thread was polling on that event, it will be
3948 * made ready to run. A @a result value can be specified.
3949 *
3950 * The poll signal contains a 'signaled' field that, when set by
3951 * k_poll_signal(), stays set until the user sets it back to 0. It thus has to
3952 * be reset by the user before being passed again to k_poll() or k_poll() will
3953 * consider it being signaled, and will return immediately.
3954 *
3955 * @param signal A poll signal.
3956 * @param result The value to store in the result field of the signal.
3957 *
3958 * @retval 0 The signal was delivered successfully.
3959 * @retval -EAGAIN The polling thread's timeout is in the process of expiring.
3960 */
3961
3962extern int k_poll_signal(struct k_poll_signal *signal, int result);
3963
3964/* private internal function */
Luiz Augusto von Dentz7d01c5e2017-08-21 10:49:29 +03003965extern int _handle_obj_poll_events(sys_dlist_t *events, u32_t state);
Benjamin Walshacc68c12017-01-29 18:57:45 -05003966
3967/**
3968 * @} end defgroup poll_apis
3969 */
3970
Benjamin Walshc3a2bbb2016-12-14 13:04:36 -05003971/**
3972 * @brief Make the CPU idle.
3973 *
3974 * This function makes the CPU idle until an event wakes it up.
3975 *
3976 * In a regular system, the idle thread should be the only thread responsible
3977 * for making the CPU idle and triggering any type of power management.
3978 * However, in some more constrained systems, such as a single-threaded system,
3979 * the only thread would be responsible for this if needed.
3980 *
3981 * @return N/A
3982 */
3983extern void k_cpu_idle(void);
3984
3985/**
3986 * @brief Make the CPU idle in an atomic fashion.
3987 *
3988 * Similar to k_cpu_idle(), but called with interrupts locked if operations
3989 * must be done atomically before making the CPU idle.
3990 *
3991 * @param key Interrupt locking key obtained from irq_lock().
3992 *
3993 * @return N/A
3994 */
3995extern void k_cpu_atomic_idle(unsigned int key);
3996
Kumar Galacc334c72017-04-21 10:55:34 -05003997extern void _sys_power_save_idle_exit(s32_t ticks);
Andrew Boie350f88d2017-01-18 13:13:45 -08003998
Benjamin Walsh456c6da2016-09-02 18:55:39 -04003999#include <arch/cpu.h>
4000
Andrew Boiecdb94d62017-04-18 15:22:05 -07004001#ifdef _ARCH_EXCEPT
4002/* This archtecture has direct support for triggering a CPU exception */
4003#define _k_except_reason(reason) _ARCH_EXCEPT(reason)
4004#else
4005
4006#include <misc/printk.h>
4007
4008/* NOTE: This is the implementation for arches that do not implement
4009 * _ARCH_EXCEPT() to generate a real CPU exception.
4010 *
4011 * We won't have a real exception frame to determine the PC value when
4012 * the oops occurred, so print file and line number before we jump into
4013 * the fatal error handler.
4014 */
4015#define _k_except_reason(reason) do { \
4016 printk("@ %s:%d:\n", __FILE__, __LINE__); \
4017 _NanoFatalErrorHandler(reason, &_default_esf); \
4018 CODE_UNREACHABLE; \
4019 } while (0)
4020
4021#endif /* _ARCH__EXCEPT */
4022
4023/**
4024 * @brief Fatally terminate a thread
4025 *
4026 * This should be called when a thread has encountered an unrecoverable
4027 * runtime condition and needs to terminate. What this ultimately
4028 * means is determined by the _fatal_error_handler() implementation, which
4029 * will be called will reason code _NANO_ERR_KERNEL_OOPS.
4030 *
4031 * If this is called from ISR context, the default system fatal error handler
4032 * will treat it as an unrecoverable system error, just like k_panic().
4033 */
4034#define k_oops() _k_except_reason(_NANO_ERR_KERNEL_OOPS)
4035
4036/**
4037 * @brief Fatally terminate the system
4038 *
4039 * This should be called when the Zephyr kernel has encountered an
4040 * unrecoverable runtime condition and needs to terminate. What this ultimately
4041 * means is determined by the _fatal_error_handler() implementation, which
4042 * will be called will reason code _NANO_ERR_KERNEL_PANIC.
4043 */
4044#define k_panic() _k_except_reason(_NANO_ERR_KERNEL_PANIC)
4045
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004046/*
4047 * private APIs that are utilized by one or more public APIs
4048 */
4049
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004050#ifdef CONFIG_MULTITHREADING
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004051extern void _init_static_threads(void);
Benjamin Walshb12a8e02016-12-14 15:24:12 -05004052#else
4053#define _init_static_threads() do { } while ((0))
4054#endif
4055
4056extern int _is_thread_essential(void);
Allan Stephens1342adb2016-11-03 13:54:53 -05004057extern void _timer_expiration_handler(struct _timeout *t);
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004058
Andrew Boiedc5d9352017-06-02 12:56:47 -07004059/* arch/cpu.h may declare an architecture or platform-specific macro
4060 * for properly declaring stacks, compatible with MMU/MPU constraints if
4061 * enabled
4062 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004063
4064/**
4065 * @brief Obtain an extern reference to a stack
4066 *
4067 * This macro properly brings the symbol of a thread stack declared
4068 * elsewhere into scope.
4069 *
4070 * @param sym Thread stack symbol name
4071 */
4072#define K_THREAD_STACK_EXTERN(sym) extern k_thread_stack_t sym[]
4073
Andrew Boiedc5d9352017-06-02 12:56:47 -07004074#ifdef _ARCH_THREAD_STACK_DEFINE
4075#define K_THREAD_STACK_DEFINE(sym, size) _ARCH_THREAD_STACK_DEFINE(sym, size)
4076#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
4077 _ARCH_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size)
4078#define K_THREAD_STACK_MEMBER(sym, size) _ARCH_THREAD_STACK_MEMBER(sym, size)
4079#define K_THREAD_STACK_SIZEOF(sym) _ARCH_THREAD_STACK_SIZEOF(sym)
Andrew Boiec5c104f2017-10-16 14:46:34 -07004080static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004081{
4082 return _ARCH_THREAD_STACK_BUFFER(sym);
4083}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004084#else
4085/**
4086 * @brief Declare a toplevel thread stack memory region
4087 *
4088 * This declares a region of memory suitable for use as a thread's stack.
4089 *
4090 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4091 * 'noinit' section so that it isn't zeroed at boot
4092 *
Andrew Boie507852a2017-07-25 18:47:07 -07004093 * The declared symbol will always be a k_thread_stack_t which can be passed to
4094 * k_thread_create, but should otherwise not be manipulated. If the buffer
4095 * inside needs to be examined, use K_THREAD_STACK_BUFFER().
Andrew Boiedc5d9352017-06-02 12:56:47 -07004096 *
4097 * It is legal to precede this definition with the 'static' keyword.
4098 *
4099 * It is NOT legal to take the sizeof(sym) and pass that to the stackSize
4100 * parameter of k_thread_create(), it may not be the same as the
4101 * 'size' parameter. Use K_THREAD_STACK_SIZEOF() instead.
4102 *
4103 * @param sym Thread stack symbol name
4104 * @param size Size of the stack memory region
4105 */
4106#define K_THREAD_STACK_DEFINE(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004107 struct _k_thread_stack_element __noinit __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004108
4109/**
4110 * @brief Declare a toplevel array of thread stack memory regions
4111 *
4112 * Create an array of equally sized stacks. See K_THREAD_STACK_DEFINE
4113 * definition for additional details and constraints.
4114 *
4115 * This is the generic, historical definition. Align to STACK_ALIGN and put in
4116 * 'noinit' section so that it isn't zeroed at boot
4117 *
4118 * @param sym Thread stack symbol name
4119 * @param nmemb Number of stacks to declare
4120 * @param size Size of the stack memory region
4121 */
4122
4123#define K_THREAD_STACK_ARRAY_DEFINE(sym, nmemb, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004124 struct _k_thread_stack_element __noinit \
4125 __aligned(STACK_ALIGN) sym[nmemb][size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004126
4127/**
4128 * @brief Declare an embedded stack memory region
4129 *
4130 * Used for stacks embedded within other data structures. Use is highly
4131 * discouraged but in some cases necessary. For memory protection scenarios,
4132 * it is very important that any RAM preceding this member not be writable
4133 * by threads else a stack overflow will lead to silent corruption. In other
4134 * words, the containing data structure should live in RAM owned by the kernel.
4135 *
4136 * @param sym Thread stack symbol name
4137 * @param size Size of the stack memory region
4138 */
4139#define K_THREAD_STACK_MEMBER(sym, size) \
Andrew Boie507852a2017-07-25 18:47:07 -07004140 struct _k_thread_stack_element __aligned(STACK_ALIGN) sym[size]
Andrew Boiedc5d9352017-06-02 12:56:47 -07004141
4142/**
4143 * @brief Return the size in bytes of a stack memory region
4144 *
4145 * Convenience macro for passing the desired stack size to k_thread_create()
4146 * since the underlying implementation may actually create something larger
4147 * (for instance a guard area).
4148 *
4149 * The value returned here is guaranteed to match the 'size' parameter
Andrew Boiebefb0692017-07-20 14:22:23 -07004150 * passed to K_THREAD_STACK_DEFINE.
4151 *
4152 * Do not use this for stacks declared with K_THREAD_STACK_ARRAY_DEFINE(),
4153 * it is not guaranteed to return the original value since each array
4154 * element must be aligned.
Andrew Boiedc5d9352017-06-02 12:56:47 -07004155 *
4156 * @param sym Stack memory symbol
4157 * @return Size of the stack
4158 */
4159#define K_THREAD_STACK_SIZEOF(sym) sizeof(sym)
4160
4161/**
4162 * @brief Get a pointer to the physical stack buffer
4163 *
4164 * Convenience macro to get at the real underlying stack buffer used by
4165 * the CPU. Guaranteed to be a character pointer of size K_THREAD_STACK_SIZEOF.
4166 * This is really only intended for diagnostic tools which want to examine
4167 * stack memory contents.
4168 *
4169 * @param sym Declared stack symbol name
4170 * @return The buffer itself, a char *
4171 */
Andrew Boiec5c104f2017-10-16 14:46:34 -07004172static inline char *K_THREAD_STACK_BUFFER(k_thread_stack_t *sym)
Andrew Boie507852a2017-07-25 18:47:07 -07004173{
4174 return (char *)sym;
4175}
Andrew Boiedc5d9352017-06-02 12:56:47 -07004176
4177#endif /* _ARCH_DECLARE_STACK */
4178
Chunlin Hane9c97022017-07-07 20:29:30 +08004179/**
4180 * @defgroup mem_domain_apis Memory domain APIs
4181 * @ingroup kernel_apis
4182 * @{
4183 */
4184
4185/** @def MEM_PARTITION_ENTRY
4186 * @brief Used to declare a memory partition entry
4187 */
4188#define MEM_PARTITION_ENTRY(_start, _size, _attr) \
4189 {\
4190 .start = _start, \
4191 .size = _size, \
4192 .attr = _attr, \
4193 }
4194
4195/** @def K_MEM_PARTITION_DEFINE
4196 * @brief Used to declare a memory partition
4197 */
4198#ifdef _ARCH_MEM_PARTITION_ALIGN_CHECK
4199#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4200 _ARCH_MEM_PARTITION_ALIGN_CHECK(start, size); \
4201 struct k_mem_partition name = \
4202 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4203#else
4204#define K_MEM_PARTITION_DEFINE(name, start, size, attr) \
4205 struct k_mem_partition name = \
4206 MEM_PARTITION_ENTRY((u32_t)start, size, attr)
4207#endif /* _ARCH_MEM_PARTITION_ALIGN_CHECK */
4208
4209
4210/* memory partition */
4211struct k_mem_partition {
4212 /* start address of memory partition */
4213 u32_t start;
4214 /* size of memory partition */
4215 u32_t size;
4216 /* attribute of memory partition */
4217 u32_t attr;
4218};
4219
4220#if defined(CONFIG_USERSPACE)
4221/* memory domian */
4222struct k_mem_domain {
4223 /* number of partitions in the domain */
4224 u32_t num_partitions;
4225 /* partitions in the domain */
4226 struct k_mem_partition partitions[CONFIG_MAX_DOMAIN_PARTITIONS];
4227 /* domain q */
4228 sys_dlist_t mem_domain_q;
4229};
4230#endif /* CONFIG_USERSPACE */
4231
4232/**
4233 * @brief Initialize a memory domain.
4234 *
4235 * Initialize a memory domain with given name and memory partitions.
4236 *
4237 * @param domain The memory domain to be initialized.
4238 * @param num_parts The number of array items of "parts" parameter.
4239 * @param parts An array of pointers to the memory partitions. Can be NULL
4240 * if num_parts is zero.
4241 */
4242
4243extern void k_mem_domain_init(struct k_mem_domain *domain, u32_t num_parts,
4244 struct k_mem_partition *parts[]);
4245/**
4246 * @brief Destroy a memory domain.
4247 *
4248 * Destroy a memory domain.
4249 *
4250 * @param domain The memory domain to be destroyed.
4251 */
4252
4253extern void k_mem_domain_destroy(struct k_mem_domain *domain);
4254
4255/**
4256 * @brief Add a memory partition into a memory domain.
4257 *
4258 * Add a memory partition into a memory domain.
4259 *
4260 * @param domain The memory domain to be added a memory partition.
4261 * @param part The memory partition to be added
4262 */
4263
4264extern void k_mem_domain_add_partition(struct k_mem_domain *domain,
4265 struct k_mem_partition *part);
4266
4267/**
4268 * @brief Remove a memory partition from a memory domain.
4269 *
4270 * Remove a memory partition from a memory domain.
4271 *
4272 * @param domain The memory domain to be removed a memory partition.
4273 * @param part The memory partition to be removed
4274 */
4275
4276extern void k_mem_domain_remove_partition(struct k_mem_domain *domain,
4277 struct k_mem_partition *part);
4278
4279/**
4280 * @brief Add a thread into a memory domain.
4281 *
4282 * Add a thread into a memory domain.
4283 *
4284 * @param domain The memory domain that the thread is going to be added into.
4285 * @param thread ID of thread going to be added into the memory domain.
4286 */
4287
4288extern void k_mem_domain_add_thread(struct k_mem_domain *domain,
4289 k_tid_t thread);
4290
4291/**
4292 * @brief Remove a thread from its memory domain.
4293 *
4294 * Remove a thread from its memory domain.
4295 *
4296 * @param thread ID of thread going to be removed from its memory domain.
4297 */
4298
4299extern void k_mem_domain_remove_thread(k_tid_t thread);
4300
4301/**
4302 * @} end defgroup mem_domain_apis
4303 */
4304
Andrew Boie756f9072017-10-10 16:01:49 -07004305/**
4306 * @brief Emit a character buffer to the console device
4307 *
4308 * @param c String of characters to print
4309 * @param n The length of the string
4310 */
4311__syscall void k_str_out(char *c, size_t n);
4312
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004313#ifdef __cplusplus
4314}
4315#endif
4316
Andrew Boiee004dec2016-11-07 09:01:19 -08004317#if defined(CONFIG_CPLUSPLUS) && defined(__cplusplus)
4318/*
4319 * Define new and delete operators.
4320 * At this moment, the operators do nothing since objects are supposed
4321 * to be statically allocated.
4322 */
4323inline void operator delete(void *ptr)
4324{
4325 (void)ptr;
4326}
4327
4328inline void operator delete[](void *ptr)
4329{
4330 (void)ptr;
4331}
4332
4333inline void *operator new(size_t size)
4334{
4335 (void)size;
4336 return NULL;
4337}
4338
4339inline void *operator new[](size_t size)
4340{
4341 (void)size;
4342 return NULL;
4343}
4344
4345/* Placement versions of operator new and delete */
4346inline void operator delete(void *ptr1, void *ptr2)
4347{
4348 (void)ptr1;
4349 (void)ptr2;
4350}
4351
4352inline void operator delete[](void *ptr1, void *ptr2)
4353{
4354 (void)ptr1;
4355 (void)ptr2;
4356}
4357
4358inline void *operator new(size_t size, void *ptr)
4359{
4360 (void)size;
4361 return ptr;
4362}
4363
4364inline void *operator new[](size_t size, void *ptr)
4365{
4366 (void)size;
4367 return ptr;
4368}
4369
4370#endif /* defined(CONFIG_CPLUSPLUS) && defined(__cplusplus) */
4371
Andrew Boiefa94ee72017-09-28 16:54:35 -07004372#include <syscalls/kernel.h>
4373
Benjamin Walshdfa7ce52017-01-22 17:06:05 -05004374#endif /* !_ASMLANGUAGE */
4375
Benjamin Walsh456c6da2016-09-02 18:55:39 -04004376#endif /* _kernel__h_ */